session.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  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)
  10. * any later version.
  11. *
  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
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: session.c /main/3 1995/10/27 10:40:33 rswiston $ */
  24. /*
  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.
  29. */
  30. /*
  31. * session.c
  32. *
  33. * Example code for Dt Session Manager conventions and API
  34. */
  35. #include <stdio.h>
  36. #include <Xm/RowColumn.h>
  37. #include <Xm/ToggleB.h>
  38. #include <Xm/Protocols.h>
  39. #include <Dt/Session.h>
  40. /*
  41. * Define '-session' command line option
  42. */
  43. typedef struct _ApplicationArgs {
  44. String session;
  45. } ApplicationArgs;
  46. static XtResource applicationResources[] = {
  47. {
  48. "session",
  49. "Session",
  50. XmRString,
  51. sizeof(String),
  52. XtOffsetOf(ApplicationArgs,session),
  53. XmRImmediate,
  54. NULL
  55. },
  56. };
  57. static XrmOptionDescRec commandLineOpts[] = {
  58. { "-session", "session", XrmoptionSepArg, NULL },
  59. };
  60. static ApplicationArgs applicationArgs;
  61. /*
  62. * Simple application state to be preserved across sessions
  63. */
  64. static int lightState = False;
  65. /*
  66. * miscellaneous global data
  67. */
  68. static XtAppContext appContext;
  69. static Widget toplevel;
  70. static int savedArgc;
  71. static char **savedArgv;
  72. static void PreserveCommandLine(int, char **);
  73. static void SetWmCommand(char *);
  74. static void SaveSessionCb(Widget, XtPointer, XtPointer);
  75. static void RestoreSession(Widget, char*);
  76. static void SaveApplicationState(char *);
  77. static void RestoreApplicationState(char *);
  78. main(int argc, char **argv)
  79. {
  80. Widget mainWindow, toggle;
  81. XmString labelString;
  82. Arg args[1];
  83. /* Save the command line before Xt parses out the standard options */
  84. PreserveCommandLine(argc, argv);
  85. /* Create the application UI */
  86. toplevel = XtAppInitialize(&appContext, "Session",
  87. commandLineOpts, XtNumber(commandLineOpts),
  88. &argc, argv,
  89. NULL,
  90. NULL, 0);
  91. XtGetApplicationResources(toplevel, &applicationArgs,
  92. applicationResources,
  93. XtNumber(applicationResources),
  94. NULL, 0);
  95. mainWindow = XmCreateWorkArea(toplevel, "mainWindow", NULL, 0);
  96. XtManageChild(mainWindow);
  97. labelString = XmStringCreateLocalized("Lights");
  98. XtSetArg(args[0], XmNlabelString, labelString);
  99. toggle = XmCreateToggleButton(mainWindow, "lightsToggle", args, 1);
  100. XtManageChild(toggle);
  101. XmStringFree(labelString);
  102. /* Add callback to detect session manager messages */
  103. XmAddWMProtocolCallback(toplevel,
  104. XInternAtom(XtDisplay(toplevel), "WM_SAVE_YOURSELF", False),
  105. SaveSessionCb, (XtPointer)toplevel);
  106. /* Restore state if application was restarted by session manager */
  107. if (applicationArgs.session != NULL) {
  108. RestoreSession(toplevel, applicationArgs.session);
  109. }
  110. XtRealizeWidget(toplevel);
  111. XtAppMainLoop(appContext);
  112. }
  113. /*
  114. * Save session state
  115. */
  116. static void SaveSessionCb(Widget w, XtPointer cd, XtPointer cb)
  117. {
  118. Widget toplevel = (Widget)cd;
  119. char *savePath = NULL;
  120. char *saveFile = NULL;
  121. DtSessionSavePath(toplevel, &savePath, &saveFile);
  122. if (savePath != NULL) {
  123. SaveApplicationState(savePath);
  124. SetWmCommand(saveFile);
  125. XtFree(savePath);
  126. XtFree(saveFile);
  127. }
  128. }
  129. static void SaveApplicationState(char *path)
  130. {
  131. Widget toggle = XtNameToWidget(toplevel, "*lightsToggle");
  132. FILE *fp;
  133. lightState = XmToggleButtonGetState(toggle);
  134. if ((fp = fopen(path, "w")) != NULL) {
  135. fprintf(fp, "%d", lightState);
  136. fclose(fp);
  137. }
  138. }
  139. static void PreserveCommandLine(int argc, char **argv)
  140. {
  141. int i;
  142. savedArgv = (char **)XtMalloc(argc*sizeof(char *));
  143. savedArgc = argc;
  144. for (i=0; i < argc; i++) savedArgv[i] = XtNewString(argv[i]);
  145. }
  146. static void SetWmCommand(char *sessionId)
  147. {
  148. char **wm_command;
  149. int i, j;
  150. wm_command = (char **) XtMalloc((savedArgc+2) * sizeof(char*));
  151. wm_command[0] = XtNewString(savedArgv[0]);
  152. wm_command[1] = XtNewString("-session");
  153. wm_command[2] = XtNewString(sessionId);
  154. for (i = 1, j = 3; i < savedArgc; i++) {
  155. if (strcmp(savedArgv[i], "-session") == 0) { i++; continue; }
  156. wm_command[j] = XtNewString(savedArgv[i]);
  157. j++;
  158. }
  159. XSetCommand(XtDisplay(toplevel), XtWindow(toplevel), wm_command, j);
  160. for (i=0; i < j; i++) XtFree(wm_command[i]);
  161. XtFree((char*)wm_command);
  162. }
  163. /*
  164. * Restore previously saved state
  165. */
  166. static void RestoreSession(Widget w, char *restoreFile)
  167. {
  168. char *restorePath = NULL;
  169. DtSessionRestorePath(w, &restorePath, restoreFile);
  170. if (restorePath != NULL) {
  171. RestoreApplicationState(restorePath);
  172. XtFree(restorePath);
  173. }
  174. }
  175. static void RestoreApplicationState(char *path)
  176. {
  177. Widget toggle = XtNameToWidget(toplevel, "*lightsToggle");
  178. FILE *fp;
  179. if ((fp = fopen(path, "r")) != NULL) {
  180. fscanf(fp, "%d", &lightState);
  181. fclose(fp);
  182. }
  183. XmToggleButtonSetState(toggle, lightState, False);
  184. }