dtlogin: Enable XDM authentication on FreeBSD
[oweals/cde.git] / cde / examples / dtsession / session.c
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 librararies 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
32 /*
33  * session.c
34  *
35  * Example code for Dt Session Manager conventions and API
36  */
37
38 #include <stdio.h>
39 #include <Xm/RowColumn.h>
40 #include <Xm/ToggleB.h>
41 #include <Xm/Protocols.h>
42 #include <Dt/Session.h>
43
44
45 /*
46  * Define '-session' command line option
47  */
48
49 typedef struct _ApplicationArgs {
50         String session;
51 } ApplicationArgs;
52
53 static XtResource applicationResources[] = {
54         {
55         "session",
56         "Session",
57         XmRString,
58         sizeof(String),
59         XtOffsetOf(ApplicationArgs,session),
60         XmRImmediate,
61         NULL
62         },
63 };
64
65 static XrmOptionDescRec commandLineOpts[] = {
66         { "-session", "session", XrmoptionSepArg, NULL },
67 };
68
69 static ApplicationArgs applicationArgs;
70
71 /*
72  * Simple application state to be preserved across sessions
73  */
74
75 static int lightState = False;
76
77 /*
78  * miscellaneous global data
79  */
80
81 static XtAppContext appContext;
82 static Widget toplevel;
83 static int savedArgc;
84 static char **savedArgv;
85
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 *);
92
93
94 main(int argc, char **argv)
95 {
96     Widget mainWindow, toggle;
97     XmString labelString;
98     Arg args[1];
99     
100     /* Save the command line before Xt parses out the standard options */
101     PreserveCommandLine(argc, argv);
102
103     /* Create the application UI */
104
105     toplevel = XtAppInitialize(&appContext, "Session",
106                                 commandLineOpts, XtNumber(commandLineOpts),
107                                 &argc, argv,
108                                 NULL,
109                                 NULL, 0);
110
111     XtGetApplicationResources(toplevel, &applicationArgs,
112                                 applicationResources,
113                                 XtNumber(applicationResources),
114                                 NULL, 0);
115
116     mainWindow = XmCreateWorkArea(toplevel, "mainWindow", NULL, 0);
117     XtManageChild(mainWindow);
118
119     labelString = XmStringCreateLocalized("Lights");
120     XtSetArg(args[0], XmNlabelString, labelString);
121     toggle = XmCreateToggleButton(mainWindow, "lightsToggle", args, 1);
122     XtManageChild(toggle);
123     XmStringFree(labelString);
124
125     /* Add callback to detect session manager messages */
126
127     XmAddWMProtocolCallback(toplevel,
128                 XInternAtom(XtDisplay(toplevel), "WM_SAVE_YOURSELF", False),
129                 SaveSessionCb, (XtPointer)toplevel);
130
131     /* Restore state if application was restarted by session manager */
132
133     if (applicationArgs.session != NULL) {
134         RestoreSession(toplevel, applicationArgs.session);
135     }
136
137     XtRealizeWidget(toplevel);
138     XtAppMainLoop(appContext);
139 }
140
141
142 /*
143  * Save session state
144  */
145
146 static void SaveSessionCb(Widget w, XtPointer cd, XtPointer cb)
147 {
148     Widget toplevel = (Widget)cd;
149     char *savePath = NULL;
150     char *saveFile = NULL;
151
152     DtSessionSavePath(toplevel, &savePath, &saveFile);
153
154     if (savePath != NULL) {
155         SaveApplicationState(savePath);
156         SetWmCommand(saveFile);
157         XtFree(savePath);
158         XtFree(saveFile);
159     }
160 }
161
162 static void SaveApplicationState(char *path)
163 {
164     Widget toggle = XtNameToWidget(toplevel, "*lightsToggle");
165     FILE *fp;
166
167     lightState = XmToggleButtonGetState(toggle);
168
169     if ((fp = fopen(path, "w")) != NULL) {
170         fprintf(fp, "%d", lightState);
171         fclose(fp);
172     }
173 }
174
175 static void PreserveCommandLine(int argc, char **argv)
176 {
177     int i;
178
179     savedArgv = (char **)XtMalloc(argc*sizeof(char *));
180     savedArgc = argc;
181     for (i=0; i < argc; i++) savedArgv[i] = XtNewString(argv[i]);
182 }
183
184
185 static void SetWmCommand(char *sessionId)
186 {
187     char **wm_command;
188     int i, j;
189
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);
194
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]);
198             j++;
199     }
200
201     XSetCommand(XtDisplay(toplevel), XtWindow(toplevel), wm_command, j);
202
203     for (i=0; i < j; i++) XtFree(wm_command[i]);
204     XtFree((char*)wm_command);
205 }
206
207 /*
208  * Restore previously saved state
209  */
210
211 static void RestoreSession(Widget w, char *restoreFile)
212 {
213     char *restorePath = NULL;
214
215     DtSessionRestorePath(w, &restorePath, restoreFile);
216
217     if (restorePath != NULL) {
218         RestoreApplicationState(restorePath);
219         XtFree(restorePath);
220     }
221 }
222
223 static void RestoreApplicationState(char *path)
224 {
225     Widget toggle = XtNameToWidget(toplevel, "*lightsToggle");
226     FILE *fp;
227
228     if ((fp = fopen(path, "r")) != NULL) {
229         fscanf(fp, "%d", &lightState);
230         fclose(fp);
231     }
232
233     XmToggleButtonSetState(toggle, lightState, False);
234 }
235
236