winpm

kostix (01-Nov-2007) created a Tcl/Tk extension to support processing (a subset of) Windows power management and session management events.

The extension is hosted at https://github.com/kostix/tkwinpm and is now at version 0.1 (first public release). (Before 2015-10-21 the project has been hosted on Google Code at [L1 ].)

The main idea of this extension is to provide a (lighweight) ability for a Tcl/Tk application to "hook" to some Windows power management events (like "the system switched from AC power to the battery", "battery low", etc) and to session management events (like "the user wants to log off", "the system is being shut down", etc). In fact, this extension was written to support graceful disconnects/reconnects to the server(s) on system's suspends/resumes in the Tkabber XMPP client (via its unofficial plugin called "green" [L2 ]).

The package is named winpm and when loaded it registers one command named winpm in the global namespace. All functionality is available via the subcommands of the winpm command.

Modus operandi of a user of this extension is usually to register callback scripts for certain "Windows events" supported by this extension and then introspect the event which triggered the execution of a script and/or the system's power status.

Management of such script bindings (via the winpm bind command) resembles the bind command of Tk.

"Events" are modelled (very) closely to the appropriate Windows messages, e.g. there are events called WM_POWERBROADCAST, WM_QUERYENDSESSION and so on.

For certain "query-style" events it's possible to influence the system's course of action from the respective callback scripts.

Some examples to get the idea:

  • Take some action when the system is about to reboot:
  winpm bind WM_ENDSESSION {
    lassign [winpm info session] final flags
    if {[lsearch $flags *LOGOFF] >= 0} {
      set msg "The user is logging off"
    } else {
      set msg "The system is being shut down"
    }
    if {$final} { append msg " NOW!" }
    warn user -with $msg
    network disconnect
    workplace save_all
    quit
  }
  • Handle the suspend/resume cycles:
  winpm bind PBT_APMSUSPEND {
    warn user -with "suspending..."
    network save_active_connections
    network disconnect
    workplace save_all
  }

  winpm bind PBT_APMRESUMEAUTOMATIC {
    network restore_saved_connections
  }
  • Watching the system battery:
  winpm bind PBT_APMPOWERSTATUSCHANGE {
    lassign [winpm info power] ac batt capa sec full
    if {[string equal $ac OFFLINE]
        && ![string equal $batt UNKNOWN]} {
      set msg "Battery remaining: $capa%"
      if {$sec >= 0} {
        append msg " (estimated $sec seconds to go)"
      }
      puts $msg
    }
  }

The complete documentation is available: [L3 ]

Missing features include:

  • Absence of Tk-style "%-substitutions" in callback scripts (so the scripts must introspect the event they process and/or the system to get the information they want);
  • "Active" intervention in the system's course of work isn't possible, i.e. this extension can't log the user off or shut the system down.