2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: session.c /main/3 1995/10/27 10:40:33 rswiston $ */
25 * (c) Copyright 1993, 1994 Hewlett-Packard Company
26 * (c) Copyright 1993, 1994 International Business Machines Corp.
27 * (c) Copyright 1993, 1994 Sun Microsystems, Inc.
28 * (c) Copyright 1993, 1994 Novell, Inc.
35 * Example code for Dt Session Manager conventions and API
39 #include <Xm/RowColumn.h>
40 #include <Xm/ToggleB.h>
41 #include <Xm/Protocols.h>
42 #include <Dt/Session.h>
46 * Define '-session' command line option
49 typedef struct _ApplicationArgs {
53 static XtResource applicationResources[] = {
59 XtOffsetOf(ApplicationArgs,session),
65 static XrmOptionDescRec commandLineOpts[] = {
66 { "-session", "session", XrmoptionSepArg, NULL },
69 static ApplicationArgs applicationArgs;
72 * Simple application state to be preserved across sessions
75 static int lightState = False;
78 * miscellaneous global data
81 static XtAppContext appContext;
82 static Widget toplevel;
84 static char **savedArgv;
86 static void PreserveCommandLine(int, char **);
87 static void SetWmCommand(char *);
88 static void SaveSessionCb(Widget, XtPointer, XtPointer);
89 static void RestoreSession(Widget, char*);
90 static void SaveApplicationState(char *);
91 static void RestoreApplicationState(char *);
94 main(int argc, char **argv)
96 Widget mainWindow, toggle;
100 /* Save the command line before Xt parses out the standard options */
101 PreserveCommandLine(argc, argv);
103 /* Create the application UI */
105 toplevel = XtAppInitialize(&appContext, "Session",
106 commandLineOpts, XtNumber(commandLineOpts),
111 XtGetApplicationResources(toplevel, &applicationArgs,
112 applicationResources,
113 XtNumber(applicationResources),
116 mainWindow = XmCreateWorkArea(toplevel, "mainWindow", NULL, 0);
117 XtManageChild(mainWindow);
119 labelString = XmStringCreateLocalized("Lights");
120 XtSetArg(args[0], XmNlabelString, labelString);
121 toggle = XmCreateToggleButton(mainWindow, "lightsToggle", args, 1);
122 XtManageChild(toggle);
123 XmStringFree(labelString);
125 /* Add callback to detect session manager messages */
127 XmAddWMProtocolCallback(toplevel,
128 XInternAtom(XtDisplay(toplevel), "WM_SAVE_YOURSELF", False),
129 SaveSessionCb, (XtPointer)toplevel);
131 /* Restore state if application was restarted by session manager */
133 if (applicationArgs.session != NULL) {
134 RestoreSession(toplevel, applicationArgs.session);
137 XtRealizeWidget(toplevel);
138 XtAppMainLoop(appContext);
146 static void SaveSessionCb(Widget w, XtPointer cd, XtPointer cb)
148 Widget toplevel = (Widget)cd;
149 char *savePath = NULL;
150 char *saveFile = NULL;
152 DtSessionSavePath(toplevel, &savePath, &saveFile);
154 if (savePath != NULL) {
155 SaveApplicationState(savePath);
156 SetWmCommand(saveFile);
162 static void SaveApplicationState(char *path)
164 Widget toggle = XtNameToWidget(toplevel, "*lightsToggle");
167 lightState = XmToggleButtonGetState(toggle);
169 if ((fp = fopen(path, "w")) != NULL) {
170 fprintf(fp, "%d", lightState);
175 static void PreserveCommandLine(int argc, char **argv)
179 savedArgv = (char **)XtMalloc(argc*sizeof(char *));
181 for (i=0; i < argc; i++) savedArgv[i] = XtNewString(argv[i]);
185 static void SetWmCommand(char *sessionId)
190 wm_command = (char **) XtMalloc((savedArgc+2) * sizeof(char*));
191 wm_command[0] = XtNewString(savedArgv[0]);
192 wm_command[1] = XtNewString("-session");
193 wm_command[2] = XtNewString(sessionId);
195 for (i = 1, j = 3; i < savedArgc; i++) {
196 if (strcmp(savedArgv[i], "-session") == 0) { i++; continue; }
197 wm_command[j] = XtNewString(savedArgv[i]);
201 XSetCommand(XtDisplay(toplevel), XtWindow(toplevel), wm_command, j);
203 for (i=0; i < j; i++) XtFree(wm_command[i]);
204 XtFree((char*)wm_command);
208 * Restore previously saved state
211 static void RestoreSession(Widget w, char *restoreFile)
213 char *restorePath = NULL;
215 DtSessionRestorePath(w, &restorePath, restoreFile);
217 if (restorePath != NULL) {
218 RestoreApplicationState(restorePath);
223 static void RestoreApplicationState(char *path)
225 Widget toggle = XtNameToWidget(toplevel, "*lightsToggle");
228 if ((fp = fopen(path, "r")) != NULL) {
229 fscanf(fp, "%d", &lightState);
233 XmToggleButtonSetState(toggle, lightState, False);