Session Manager allows applications to save their current state when the user terminates the current session, so that when the user later restarts the session, an application can return to the state it was in. In dtksh, this is accomplished by setting up a handler in a similar way of handling a Close notification. If a handler is not set up, the application has to be restarted manually in the new session, and the application does not retain any state.
To set up a handler to save the current state, the application must:
Define functions to save the state at the end of the session and to restore it on startup
Register interest in the Session Manager notification
Register the function to save the state
At startup, determine whether the saved state should be restored
The following code illustrates this process.
#! /usr/dt/bin/dtksh # Function invoked when the session is being ended by the user SessionCallback() { # Get the name of the file into which we should save our # session information if DtSessionSavePath $TOPLEVEL PATH SAVEFILE; then exec 9>$PATH # Save off whether we are currently in an iconified state if DtShellIsIconified $TOPLEVEL ; then print -u9 `Iconified' else print -u9 `Deiconified' fi # Save off the list of workspaces we currently reside in if DtWsmGetWorkspacesOccupied $(XtDisplay "-" $TOPLEVEL) \ $(XtWindow "-" $TOPLEVEL) \ CURRENT_WS_LIST ; then # Map the comma-separated list of atoms into # their string representation oldIFS=$IFS IFS="," for item in $CURRENT_WS_LIST; do XmGetAtomName NAME $(XtDisplay "-" $TOPLEVEL) \ $item print -u9 $NAME done IFS=$oldIFS fi exec 9<&- # Let the session manager know how to invoke us when # the session is restored DtSetStartupCommand $TOPLEVEL \ "/usr/dt/contrib/dtksh/SessionTest $SAVEFILE" else echo "DtSessionSavePath FAILED!!" exit -3 fi } # Function invoked during a restore session; restores the # application to its previous state RestoreSession() { # Retrieve the path where our session file resides if DtSessionRestorePath $TOPLEVEL PATH $1; then exec 9<$PATH read -u9 ICONIFY # Extract and restore our iconified state case $ICONIFY in Iconified) DtSetIconifyHint $TOPLEVEL True;; *) DtSetIconifyHint $TOPLEVEL False; esac # Extract the list of workspaces we belong in, convert # them to atoms, and ask the Workspace Manager to relocate # us to those workspaces WS_LIST="" while read -u9 NAME do XmInternAtom ATOM $(XtDisplay "-" $TOPLEVEL) \ $NAME False if [ ${#WS_LIST} -gt 0 ]; then WS_LIST=$WS_LIST,$ATOM else WS_LIST=$ATOM fi done DtWsmSetWorkspacesOccupied $(XtDisplay "-" $TOPLEVEL) \ $(XtWindow "-" $TOPLEVEL) $WS_LIST exec 9<&- else echo "DtSessionRestorePath FAILED!!" exit -3 fi } ################## Create the Main UI ####################### XtInitialize TOPLEVEL wmProtTest Dtksh $0 "$@" XtCreateManagedWidget DA da XmDrawingArea $TOPLEVEL \ height:200 width:200 XmInternAtom SAVE_SESSION_ATOM $(XtDisplay "-" $TOPLEVEL) \ "WM_SAVE_YOURSELF" False # If a command-line argument was supplied, then treat it as the # name of the session file if (( $# > 0)) then # Restore to the state specified in the passed-in session file XtSetValues $TOPLEVEL mappedWhenManaged:False XtRealizeWidget $TOPLEVEL XSync $(XtDisplay "-" $TOPLEVEL) False RestoreSession $1 XtSetValues $TOPLEVEL mappedWhenManaged:True XtPopup $TOPLEVEL GrabNone else # This is not a session restore, so come up in the default state XtRealizeWidget $TOPLEVEL XSync $(XtDisplay "-" $TOPLEVEL) False fi # Register the fact that we are interested in participating in # session management XmAddWMProtocols $TOPLEVEL $SAVE_SESSION_ATOM XmAddWMProtocolCallback $TOPLEVEL $SAVE_SESSION_ATOM \ SessionCallback XtMainLoop