ksh: fix up shipin for more modern systems WRT test and wc
[oweals/cde.git] / cde / examples / dtwsm / occupy.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: occupy.c /main/3 1995/10/27 10:41:19 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 /* Demonstrate the DtWsm Workspace API. */
33
34 /*  
35  * include files  
36  */
37
38 #include <stdio.h>
39 #include <Xm/XmAll.h>
40 #include <Dt/Wsm.h>
41
42
43 /*  
44  * functions
45  */
46
47 void main();
48 static void quitCB(); 
49 static void setCB(); 
50 static void showCB(); 
51 static void wsCB(); 
52 static void allWsCB(); 
53 static void SetupWorkspaceButtons ();
54 static void ShowWorkspaceOccupancy();
55 static void SetWorkspaceOccupancy();
56
57 /*  
58  * global variables  
59  */
60
61 static XmString xms;
62 static Widget toplevel;
63 static Widget wChildren [20];
64 static Widget *wWs;
65 static Atom *paWsSet = NULL;
66 static Boolean *pbOccupy;
67 static unsigned long numWorkspaces;
68 static Atom *paWs;
69
70 /* 
71  * button assignments 
72  */
73 #define QUIT_BUTTON     (wChildren[0])
74 #define SEPARATOR       (wChildren[1])
75 #define SET_BUTTON      (wChildren[2])
76 #define SHOW_BUTTON     (wChildren[3])
77 #define ALL_WS_BUTTON   (wChildren[4])
78 #define NUM_CHILDREN    5
79
80 /*
81  * main - main logic for program
82  */
83
84 void main (argc,argv)
85     int argc;
86     char **argv;
87 {
88     Widget frame;
89     Widget form;
90     Widget  top;
91     XtAppContext app_context;
92     Arg args[10];
93     int n;
94     int i;
95
96     /*
97      * initialize toolkit
98      */
99     n = 0;
100     XtSetArg (args[n], XmNallowShellResize, True);  n++;
101     toplevel = XtAppInitialize (&app_context, "Dtoccupy", NULL, 0, &argc, argv,
102                                                                  NULL, args, n);
103
104     n = 0;
105     XtSetArg (args[n], XmNmarginWidth, 10);  n++;
106     XtSetArg (args[n], XmNmarginHeight, 10);  n++;
107     frame = XmCreateFrame (toplevel, "frame", args, n);
108     XtManageChild (frame);
109
110     /*
111      *  manager widget
112      */
113     n = 0;
114     form = XmCreateForm (frame, "form", args, n);
115     XtManageChild (form);
116
117     /*
118      * create compound string for the button text
119      */
120     xms = XmStringCreateLocalized("Quit");
121
122     /*
123      * set up arglist
124      */
125     n = 0;
126     XtSetArg (args[n], XmNlabelType, XmSTRING);  n++;
127     XtSetArg (args[n], XmNlabelString, xms);  n++;
128     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
129     XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM); n++;
130
131     /*
132      * create button
133      */
134     top = QUIT_BUTTON = XtCreateWidget ("button", xmPushButtonWidgetClass,
135                                                                 form, args, n);
136
137     XmStringFree(xms);
138     /*
139      * add callback
140      */
141     XtAddCallback (QUIT_BUTTON, XmNactivateCallback, quitCB, NULL);
142
143     /*  
144      * create compound string for the button text  
145      */
146     xms = XmStringCreateLocalized("Set Occupancy");
147
148     /*  
149      * set up arglist  
150      */
151     n = 0;
152     XtSetArg (args[n], XmNlabelType, XmSTRING);  n++;
153     XtSetArg (args[n], XmNlabelString, xms);  n++;
154     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
155     XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
156     XtSetArg (args[n], XmNtopWidget, top); n++;
157
158     /*  
159      * create button  
160      */
161     top = SET_BUTTON = XtCreateWidget ("button", xmPushButtonWidgetClass,
162           form, args, n);
163
164     XmStringFree(xms);
165     /*  
166      * add callback  
167      */
168     XtAddCallback (SET_BUTTON, XmNactivateCallback, setCB, NULL);
169
170     /*  
171      * create compound string for the button text  
172      */
173     xms = XmStringCreateLocalized("Occupy All Workspaces");
174
175     /*  
176      * set up arglist  
177      */
178     n = 0;
179     XtSetArg (args[n], XmNlabelType, XmSTRING);  n++;
180     XtSetArg (args[n], XmNlabelString, xms);  n++;
181     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
182     XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
183     XtSetArg (args[n], XmNtopWidget, top); n++;
184     /*  
185      * create button  
186      */
187     top = ALL_WS_BUTTON = XtCreateWidget ("button", xmPushButtonWidgetClass,
188           form, args, n);
189
190     XmStringFree(xms);
191
192     /*  
193      * add callback  
194      */
195     XtAddCallback (ALL_WS_BUTTON, XmNactivateCallback, allWsCB, NULL);
196
197     /*  
198      * create compound string for the button text  
199      */
200     xms = XmStringCreateLocalized("Show Occupancy");
201
202     /*  
203      * set up arglist  
204      */
205     n = 0;
206     XtSetArg (args[n], XmNlabelType, XmSTRING);  n++;
207     XtSetArg (args[n], XmNlabelString, xms);  n++;
208     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
209     XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
210     XtSetArg (args[n], XmNtopWidget, top); n++;
211     /*   
212      * create button  
213      */
214     top = SHOW_BUTTON = XtCreateWidget ("button", xmPushButtonWidgetClass,
215       form, args, n);
216
217     XmStringFree(xms);
218     /*  
219      * add callback  
220      */
221     XtAddCallback (SHOW_BUTTON, XmNactivateCallback, showCB, NULL);
222
223     /*  
224      * set up arglist  
225      */
226     n = 0;
227     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
228     XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM); n++;
229     XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
230     XtSetArg (args[n], XmNtopWidget, top); n++;
231     /*  
232      * create separator  
233      */
234     top = SEPARATOR = (Widget) 
235             XmCreateSeparatorGadget (form, "separator", args, n);
236
237     /* 
238      * set up workspace buttons 
239      */
240     SetupWorkspaceButtons (form, top);
241
242     /*  
243      * manage children
244      */
245     XtManageChildren (wChildren, NUM_CHILDREN);
246     XtManageChildren (wWs, numWorkspaces);
247
248     /*  
249      * realize widgets  
250      */
251     XtRealizeWidget (toplevel);
252
253     /* 
254      * set up initial workspace occupancy info 
255      */
256     pbOccupy = (Boolean *) XtMalloc (numWorkspaces * sizeof (Boolean));
257     for (i=0; i<numWorkspaces; i++)
258     {
259         pbOccupy[i] = False;
260     }
261     ShowWorkspaceOccupancy ();
262
263     /*  
264      * process events  
265      */
266     XtAppMainLoop (app_context);
267 }
268
269 /*-------------------------------------------------------------
270  *      Create a set of buttons for the current set of workspaces
271  */
272 static void SetupWorkspaceButtons (form, top)
273 Widget form;
274 Widget top;
275 {
276     Window root;
277     Arg args[10]; /*  arg list          */
278     register int n;     /*  arg count           */
279     register int i;
280     DtWsmWorkspaceInfo *pWsInfo;
281     char *pchWs;
282     Widget left = None;
283     Widget *pWid;
284
285     /*  
286      * get workspace information 
287      */
288     root = XRootWindowOfScreen(XtScreen(form));
289     if (DtWsmGetWorkspaceList (XtDisplay(form), root, 
290                     &paWs, (int *)&numWorkspaces) == Success)
291     {
292         wWs = (Widget *) XtMalloc (numWorkspaces * sizeof (Widget));
293         pWid = wWs;
294         for (i=0; i<numWorkspaces; i++, pWid++)
295         {
296             DtWsmGetWorkspaceInfo (XtDisplay(form), root, paWs[i],
297                 &pWsInfo);
298
299             pchWs = (char *) XmGetAtomName (XtDisplay(form), 
300                                             pWsInfo->workspace);
301             /*  
302              * create compound string for the button text  
303              */
304             xms = XmStringCreateLocalized(pchWs);
305
306             /*  
307              * set up arglist  
308              */
309             n = 0;
310             XtSetArg (args[n], XmNlabelType, XmSTRING);  n++;
311             XtSetArg (args[n], XmNlabelString, xms);  n++;
312             XtSetArg (args[n], XmNindicatorOn, True);  n++;
313             XtSetArg (args[n], XmNindicatorType, XmN_OF_MANY);  n++;
314             if (left == None)
315             {
316                 XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
317             }
318             else
319             {
320                 XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
321                 XtSetArg (args[n], XmNleftWidget, left); n++;
322             }
323             XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
324             XtSetArg (args[n], XmNtopWidget, top); n++;
325             /*  
326              * create button  
327              */
328             *pWid = XtCreateWidget ("button", xmToggleButtonWidgetClass,
329               form, args, n);
330
331             XmStringFree(xms);
332             /*  
333              * add callback  
334              */
335             XtAddCallback (*pWid, XmNvalueChangedCallback, wsCB, 
336                                     (XtPointer) i);
337             left = *pWid;
338         }
339     }
340     else
341     {
342         fprintf(stderr, "Failed to get workspace info -- dtwm not running!\n");
343         exit (1);
344     }
345 }
346
347 /*-------------------------------------------------------------
348  *      Update the workspace buttons with where we are now.
349  */
350 static void ShowWorkspaceOccupancy ()
351 {
352     Arg args[10]; /*  arg list          */
353     register int n;     /*  arg count           */
354     int i,j;
355     Atom *paWsIn = NULL;
356     unsigned long numWsIn;
357     int rval;
358
359     rval = DtWsmGetWorkspacesOccupied(XtDisplay(toplevel), 
360                 XtWindow(toplevel), &paWsIn, &numWsIn );
361
362     if (rval == Success)
363     {
364
365         for (i = 0; i<numWorkspaces; i++)
366         {
367             pbOccupy[i] = False;
368             for (j=0; j<numWsIn; j++)
369             {
370                 if (paWs[i] == paWsIn[j])
371                 {
372                     pbOccupy[i] = True;
373                     break;
374                 }
375             }
376         }
377
378         for (i = 0; i<numWorkspaces; i++)
379         {
380             n = 0;
381             if (pbOccupy[i])
382             {
383                 XtSetArg (args[n], XmNset, True);  n++;
384             }
385             else
386             {
387                 XtSetArg (args[n], XmNset, False);  n++;
388             }
389             XtSetValues (wWs[i], args, n);
390         }
391
392         if (paWsIn) XFree ((char *) paWsIn);
393     }
394 }
395
396 /*-------------------------------------------------------------
397  *      quitCB - callback for button
398  */
399 static void quitCB (w, client_data, call_data) 
400 Widget  w;              /*  widget id           */
401 XtPointer       client_data;    /*  data from application   */
402 XtPointer       call_data;      /*  data from widget class  */
403 {
404     exit (0);
405 }
406
407
408 /*-------------------------------------------------------------
409  *      showCB - callback for show occupancy button
410  */
411 static void showCB (w, client_data, call_data) 
412 Widget  w;              /*  widget id           */
413 XtPointer       client_data;    /*  data from application   */
414 XtPointer       call_data;      /*  data from widget class  */
415 {
416
417     ShowWorkspaceOccupancy();
418
419 }
420
421 /*-------------------------------------------------------------
422  *      setCB - callback for set occupancy button
423  */
424 static void setCB (w, client_data, call_data) 
425 Widget  w;              /*  widget id           */
426 XtPointer       client_data;    /*  data from application   */
427 XtPointer       call_data;      /*  data from widget class  */
428 {
429     int numSet;
430     int i;
431
432     if (!paWsSet)
433     {
434         paWsSet = (Atom *) XtMalloc (numWorkspaces * sizeof(Atom));
435     }
436
437     for (i=0, numSet=0; i<numWorkspaces; i++)
438     {
439         if (pbOccupy[i])
440         {
441             paWsSet[numSet++] = paWs[i];
442         }
443     }
444
445     DtWsmSetWorkspacesOccupied (XtDisplay(toplevel),
446                             XtWindow(toplevel), paWsSet, numSet);
447
448 }
449
450 /*-------------------------------------------------------------
451  *      wsCB - callback for set occupancy button
452  */
453 static void wsCB (w, client_data, call_data) 
454 Widget  w;              /*  widget id           */
455 XtPointer       client_data;    /*  data from application   */
456 XtPointer       call_data;      /*  data from widget class  */
457 {
458     int i = (Atom) client_data; /* ws array index */
459     XmToggleButtonCallbackStruct *ptb = 
460                 (XmToggleButtonCallbackStruct *) call_data;
461
462     if (ptb->reason == XmCR_VALUE_CHANGED)
463     {
464         if (ptb->set)
465         {
466             pbOccupy[i] = True;
467         }
468         else
469         {
470             pbOccupy[i] = False;
471         }
472     }
473 }
474
475 /*-------------------------------------------------------------
476  *      allWsCB - callback for occupy all button
477  */
478 static void allWsCB (w, client_data, call_data) 
479 Widget  w;              /*  widget id           */
480 XtPointer       client_data;    /*  data from application   */
481 XtPointer       call_data;      /*  data from widget class  */
482 {
483     DtWsmOccupyAllWorkspaces (XtDisplay(toplevel), XtWindow(toplevel));
484 }
485