I was wondering if there is some API/trick/hack that would allow a laptop to wake up from sleep when it senses that it is connected to power, or that its battery is charging. I know I could build an external device that would send a wake-up network packet when it senses power, but I want to do this purely in software, i.e. via API.

If this cannot be done on Windows, perhaps Linux?