ksh: fix up shipin for more modern systems WRT test and wc
[oweals/cde.git] / cde / examples / dtwsm / wsinfo.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: wsinfo.c /main/3 1995/10/27 10:41:27 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 /* Demonstrate the CDE Workspace API. */
32
33 /*  
34  * include files  
35  */
36
37 #include <stdio.h>
38 #include <Xm/XmAll.h>
39 #include <Dt/Wsm.h>
40
41 /*  
42  * functions
43  */
44 void main();
45 static void quitCB(); 
46 static void updateCB(); 
47 static void ShowCurrentWorkspaceInfo();
48 static DtWsmWsChangeProc wschangecb();
49 static void InstallXErrorHandler();
50
51 /*  
52  * global variables  
53  */
54 static Widget toplevel;
55 static Widget wChildren[20];
56
57 /* 
58  * button assignments 
59  */
60 #define QUIT_BUTTON     (wChildren[0])
61 #define UPDATE_BUTTON   (wChildren[1])
62 #define SEPARATOR       (wChildren[2])
63 #define WS_LABEL        (wChildren[3])
64 #define WS_VALUE        (wChildren[4])
65 #define TITLE_LABEL     (wChildren[5])
66 #define TITLE_VALUE     (wChildren[6])
67 #define BACKDROP_LABEL  (wChildren[7])
68 #define BACKDROP_VALUE  (wChildren[8])
69 #define COLORSET_LABEL  (wChildren[9])
70 #define COLORSET_VALUE  (wChildren[10])
71 #define SEPARATOR2      (wChildren[11])
72 #define NUM_CHILDREN    12
73
74 /*
75  * main - main logic for program
76  */
77 void main (argc,argv)
78     int argc;
79     char **argv;
80 {
81     Widget frame;
82     Widget form;
83     Widget  top;
84     XtAppContext app_context;
85     Arg args[10];
86     register int n;
87     register int i;
88     XmString xms, xmsBlank;
89
90     /*  
91      * initialize toolkit  
92      */
93     n = 0;
94     XtSetArg (args[n], XmNallowShellResize, True);  n++;
95     toplevel = XtAppInitialize (&app_context, "Dtwsinfo", NULL, 0, &argc, argv,
96                                                                 NULL, args, n);
97
98     /* 
99      * Set up X error handling
100      */
101     InstallXErrorHandler ();
102
103     n = 0;
104     XtSetArg (args[n], XmNmarginWidth, 10);  n++;
105     XtSetArg (args[n], XmNmarginHeight, 10);  n++;
106     frame = XmCreateFrame (toplevel, "frame", args, n);
107     XtManageChild (frame);
108
109     /*  
110      * manager widget 
111      */
112     n = 0;
113     form = XmCreateForm (frame, "form", args, n);
114     XtManageChild (form);
115
116     /*  
117      * create compound string for blank labels
118      */
119     xmsBlank = XmStringCreateLocalized("   ");
120
121     /*******************************************
122      * QUIT BUTTON
123      ******************************************/
124
125     /*  
126      * create compound string for the button text  
127      */
128     xms = XmStringCreateLocalized("Quit");
129
130     /*  
131      * set up arglist  
132      */
133     n = 0;
134     XtSetArg (args[n], XmNlabelType, XmSTRING);  n++;
135     XtSetArg (args[n], XmNlabelString, xms);  n++;
136     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
137     XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM); n++;
138     /*  
139      * create button  
140      */
141     top = QUIT_BUTTON = XtCreateWidget ("button", xmPushButtonWidgetClass,
142           form, args, n);
143     XmStringFree(xms);
144
145     /*  
146      * add callback  
147      */
148     XtAddCallback (QUIT_BUTTON, XmNactivateCallback, quitCB, NULL);
149
150     /*******************************************
151      * UPDATE WORKSPACE INFO BUTTON
152      ******************************************/
153
154     /*  
155      * create compound string for the button text  
156      */
157     xms = XmStringCreateLocalized("Get workspace info");
158
159     /*  
160      * set up arglist  
161      */
162     n = 0;
163     XtSetArg (args[n], XmNlabelType, XmSTRING);  n++;
164     XtSetArg (args[n], XmNlabelString, xms);  n++;
165     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
166     XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
167     XtSetArg (args[n], XmNtopWidget, top); n++;
168     /*  
169      * create button  
170      */
171     top = UPDATE_BUTTON = XtCreateWidget ("button", xmPushButtonWidgetClass,
172           form, args, n);
173     XmStringFree(xms);
174
175     /*  
176      * add callback  
177      */
178     XtAddCallback (UPDATE_BUTTON, XmNactivateCallback, updateCB, NULL);
179
180     /*******************************************
181      * SEPARATOR
182      ******************************************/
183
184     /*  
185      * set up arglist  
186      */
187     n = 0;
188     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
189     XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM); n++;
190     XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
191     XtSetArg (args[n], XmNtopWidget, top); n++;
192     /*  
193      * create separator  
194      */
195     top = SEPARATOR = (Widget) 
196             XmCreateSeparatorGadget (form, "separator", args, n);
197
198     /*******************************************
199      * LABEL FOR CURRENT WORKSPACE
200      ******************************************/
201
202     /*
203      * NOTE: We assume this is the longest label and lay out the
204      *       other fields based on this assumption.
205      */
206
207     /*  
208      * create compound string for the label text  
209      */
210     xms = XmStringCreateLocalized("Current workspace: ");
211
212     /*  
213      * set up arglist  
214      */
215     n = 0;
216     XtSetArg (args[n], XmNlabelType, XmSTRING);  n++;
217     XtSetArg (args[n], XmNlabelString, xms);  n++;
218     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
219     XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
220     XtSetArg (args[n], XmNtopWidget, top); n++;
221     /*  
222      * create label  
223      */
224     WS_LABEL = XtCreateWidget ("label", xmLabelWidgetClass,
225           form, args, n);
226     XmStringFree (xms);
227
228     /*******************************************
229      * VALUE FOR CURRENT WORKSPACE
230      ******************************************/
231
232     /*  
233      * set up arglist  
234      */
235     n = 0;
236     XtSetArg (args[n], XmNlabelType, XmSTRING);  n++;
237     XtSetArg (args[n], XmNlabelString, xmsBlank);  n++;
238     XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
239     XtSetArg (args[n], XmNleftWidget, WS_LABEL); n++;
240     XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
241     XtSetArg (args[n], XmNtopWidget, top); n++;
242     /*  
243      * create value
244      */
245     top = WS_VALUE = XtCreateWidget ("label", xmLabelWidgetClass,
246           form, args, n);
247
248     /*******************************************
249      * LABEL FOR WORKSPACE TITLE
250      ******************************************/
251
252     /*  
253      * create compound string for the label text  
254      */
255     xms = XmStringCreateLocalized("    title: ");
256
257     /*  
258      * set up arglist  
259      */
260     n = 0;
261     XtSetArg (args[n], XmNlabelType, XmSTRING);  n++;
262     XtSetArg (args[n], XmNlabelString, xms);  n++;
263     XtSetArg (args[n], XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
264     XtSetArg (args[n], XmNrightWidget, WS_LABEL); n++;
265     XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
266     XtSetArg (args[n], XmNtopWidget, top); n++;
267     /*  
268      * create label  
269      */
270     TITLE_LABEL = XtCreateWidget ("label", xmLabelWidgetClass,
271           form, args, n);
272     XmStringFree(xms);
273
274     /*******************************************
275      * VALUE FOR WORKSPACE TITLE
276      ******************************************/
277
278     /*  
279      * set up arglist  
280      */
281     n = 0;
282     XtSetArg (args[n], XmNlabelType, XmSTRING);  n++;
283     XtSetArg (args[n], XmNlabelString, xmsBlank);  n++;
284     XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
285     XtSetArg (args[n], XmNleftWidget, TITLE_LABEL); n++;
286     XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
287     XtSetArg (args[n], XmNtopWidget, top); n++;
288     /*  
289      * create value
290      */
291     top = TITLE_VALUE = XtCreateWidget ("label", xmLabelWidgetClass,
292           form, args, n);
293
294     /*******************************************
295      * LABEL FOR WORKSPACE BACKDROP
296      ******************************************/
297
298     /*  
299      * create compound string for the label text  
300      */
301     xms = XmStringCreateLocalized("    backdrop: ");
302
303     /*  
304      * set up arglist  
305      */
306     n = 0;
307     XtSetArg (args[n], XmNlabelType, XmSTRING);  n++;
308     XtSetArg (args[n], XmNlabelString, xms);  n++;
309     XtSetArg (args[n], XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
310     XtSetArg (args[n], XmNrightWidget, WS_LABEL); n++;
311     XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
312     XtSetArg (args[n], XmNtopWidget, top); n++;
313     /*  
314      * create label  
315      */
316     BACKDROP_LABEL = XtCreateWidget ("label", xmLabelWidgetClass,
317           form, args, n);
318     XmStringFree (xms);
319
320     /*******************************************
321      * VALUE FOR WORKSPACE BACKDROP
322      ******************************************/
323
324     /*  
325      * set up arglist  
326      */
327     n = 0;
328     XtSetArg (args[n], XmNlabelType, XmSTRING);  n++;
329     XtSetArg (args[n], XmNlabelString, xmsBlank);  n++;
330     XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
331     XtSetArg (args[n], XmNleftWidget, BACKDROP_LABEL); n++;
332     XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
333     XtSetArg (args[n], XmNtopWidget, top); n++;
334     /*  
335      * create value
336      */
337     top = BACKDROP_VALUE = XtCreateWidget ("label", xmLabelWidgetClass,
338           form, args, n);
339
340     /*******************************************
341      * LABEL FOR WORKSPACE COLORSET ID
342      ******************************************/
343
344     /*  
345      * create compound string for the label text  
346      */
347     xms = XmStringCreateLocalized("    color set id: ");
348
349     /*  
350      * set up arglist  
351      */
352     n = 0;
353     XtSetArg (args[n], XmNlabelType, XmSTRING);  n++;
354     XtSetArg (args[n], XmNlabelString, xms);  n++;
355     XtSetArg (args[n], XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
356     XtSetArg (args[n], XmNrightWidget, WS_LABEL); n++;
357     XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
358     XtSetArg (args[n], XmNtopWidget, top); n++;
359     /*  
360      * create label  
361      */
362     COLORSET_LABEL = XtCreateWidget ("label", xmLabelWidgetClass,
363           form, args, n);
364     XmStringFree (xms);
365
366     /*******************************************
367      * VALUE FOR WORKSPACE COLORSET ID
368      ******************************************/
369
370     /*  
371      * set up arglist  
372      */
373     n = 0;
374     XtSetArg (args[n], XmNlabelType, XmSTRING);  n++;
375     XtSetArg (args[n], XmNlabelString, xmsBlank);  n++;
376     XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
377     XtSetArg (args[n], XmNleftWidget, COLORSET_LABEL); n++;
378     XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
379     XtSetArg (args[n], XmNtopWidget, top); n++;
380     /*  
381      * create value
382      */
383     top = COLORSET_VALUE = XtCreateWidget ("label", xmLabelWidgetClass,
384           form, args, n);
385
386     /*******************************************
387      * SEPARATOR
388      ******************************************/
389
390     /*  
391      * set up arglist  
392      */
393     n = 0;
394     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
395     XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM); n++;
396     XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
397     XtSetArg (args[n], XmNtopWidget, top); n++;
398     /*  
399      * create separator  
400      */
401     top = SEPARATOR2 = (Widget) 
402             XmCreateSeparatorGadget (form, "separator", args, n);
403
404     /*  
405      * manage children
406      */
407     XtManageChildren (wChildren, NUM_CHILDREN);
408
409     /* 
410      * Show info on current workspace
411      */
412     ShowCurrentWorkspaceInfo ();
413
414     /*  
415      * realize widgets  
416      */
417     XtRealizeWidget (toplevel);
418
419     /*
420      * Do these calls after the shell is realized.  
421      * They need a window created for the top level shell.
422      *
423      * HINT: See XtSetMappedWhenManaged(1) if you want to 
424      *       realize a widget to get a window, but need to
425      *       control when the map of the window occurs.
426      */
427     (void) DtWsmAddCurrentWorkspaceCallback (toplevel,
428                 (DtWsmWsChangeProc) wschangecb, NULL);
429
430     DtWsmOccupyAllWorkspaces (XtDisplay(toplevel), XtWindow(toplevel));
431
432     /*  
433      * process events  
434      */
435     XtAppMainLoop (app_context);
436 }
437
438
439 /*-------------------------------------------------------------
440  *      Update the information for the current workspace
441  */
442 static void ShowCurrentWorkspaceInfo ()
443 {
444     Arg args[10]; /*  arg list          */
445     register int n;     /*  arg count           */
446     Atom aWs;
447     int rval;
448     Display *dpy;
449     Window root;
450     DtWsmWorkspaceInfo *pWsInfo;
451     XmString xms;
452     char pch[512];
453     char *pchName;
454
455     dpy = XtDisplay(toplevel);
456     root = XDefaultRootWindow(dpy);
457
458     rval = DtWsmGetCurrentWorkspace(dpy, root, &aWs);
459
460     if (rval == Success)
461     {
462         rval = DtWsmGetWorkspaceInfo (dpy, root, aWs, &pWsInfo);
463
464         if (rval == Success)
465         {
466             /* workspace name */
467             pchName = XmGetAtomName (dpy, pWsInfo->workspace);
468             xms = XmStringCreateLocalized(pchName);
469
470             n = 0;
471             XtSetArg (args[n], XmNlabelString, xms);  n++;
472             XtSetValues (WS_VALUE, args, n);
473
474             XmStringFree (xms);
475             XtFree (pchName);
476
477             /* workspace title */
478             xms = XmStringCreateLocalized(pWsInfo->pchTitle);
479
480             n = 0;
481             XtSetArg (args[n], XmNlabelString, xms);  n++;
482             XtSetValues (TITLE_VALUE, args, n);
483
484             XmStringFree (xms);
485
486             /* workspace backdrop name */
487             if (pWsInfo->backdropName != None)
488                 pchName = XmGetAtomName (dpy, pWsInfo->backdropName);
489             else
490                 pchName = "<failed to load!>";
491             xms = XmStringCreateLocalized(pchName);
492
493             n = 0;
494             XtSetArg (args[n], XmNlabelString, xms);  n++;
495             XtSetValues (BACKDROP_VALUE, args, n);
496
497             XmStringFree (xms);
498             if (pWsInfo->backdropName != None)
499                 XtFree (pchName);
500
501             /* workspace colorset id */
502             sprintf (pch, "%d", pWsInfo->colorSetId);
503             xms = XmStringCreateLocalized(pch);
504
505             n = 0;
506             XtSetArg (args[n], XmNlabelString, xms);  n++;
507             XtSetValues (COLORSET_VALUE, args, n);
508
509             XmStringFree (xms);
510
511             DtWsmFreeWorkspaceInfo (pWsInfo);
512         }
513     }
514
515     if (rval != Success)
516     {
517         fprintf (stderr, "Failed to get workspace information.\n");
518         fprintf (stderr, "Is dtwm running?\n");
519         exit (1);
520     }
521 }
522
523 /*-------------------------------------------------------------
524  *      quitCB - callback for button
525  */
526 static void quitCB (w, client_data, call_data) 
527 Widget  w;              /*  widget id           */
528 XtPointer       client_data;    /*  data from application   */
529 XtPointer       call_data;      /*  data from widget class  */
530 {
531     exit (0);
532 }
533
534
535 /*-------------------------------------------------------------
536  *      updateCB - callback for show current workspace info
537  */
538 static void updateCB (w, client_data, call_data) 
539 Widget  w;              /*  widget id           */
540 XtPointer       client_data;    /*  data from application   */
541 XtPointer       call_data;      /*  data from widget class  */
542 {
543
544     ShowCurrentWorkspaceInfo ();
545
546 }
547
548 /*-------------------------------------------------------------
549 **      wschangecb - callback for workspace switch
550 */
551 static DtWsmWsChangeProc
552 wschangecb (w, atom, client_data)
553 Widget  w;
554 Atom    atom;
555 XtPointer client_data;
556 {
557     ShowCurrentWorkspaceInfo ();
558 }
559
560
561
562 /*-------------------------------------------------------------
563 **      data for X error handler
564 */
565 #define E_MAJOR_CODE            0
566 #define E_MINOR_CODE            1
567 #define E_RESOURCE_ID           2
568 #define E_ERROR_SERIAL          3
569 #define E_CURRENT_SERIAL        4
570
571 #define NUM_E_STRINGS           5
572
573 static char *pchErrorFormatNames [NUM_E_STRINGS] = {
574     "MajorCode",
575     "MinorCode",
576     "ResourceID",
577     "ErrorSerial",
578     "CurrentSerial"
579 };
580
581 static char *pchErrorFormat [NUM_E_STRINGS];
582
583
584 /*-------------------------------------------------------------
585 **      HandleXErrors - catch X protocol errors
586 */
587 static int
588 _HandleXErrors (display, errorEvent)
589 Display  *display;
590 XErrorEvent *errorEvent;
591 {
592     char buffer[BUFSIZ];
593     char message[BUFSIZ];
594
595     XGetErrorText (display, errorEvent->error_code, buffer, BUFSIZ);
596     fprintf (stderr, "\n*** X error occurred ***\n");
597     fprintf (stderr, "Description = '%s'\n  ", buffer);
598
599     fprintf (stderr, pchErrorFormat[E_MAJOR_CODE], errorEvent->request_code);
600     sprintf(message, "%d", errorEvent->request_code);
601     XGetErrorDatabaseText (display, "XRequest", message, " ", buffer, BUFSIZ);
602     fprintf (stderr, " (%s)\n  ", buffer);
603     fprintf (stderr, pchErrorFormat[E_MINOR_CODE], errorEvent->minor_code);
604     fprintf (stderr, "\n  ");
605     fprintf (stderr, pchErrorFormat[E_RESOURCE_ID], errorEvent->resourceid);
606     fprintf (stderr, "\n  ");
607     fprintf (stderr, pchErrorFormat[E_ERROR_SERIAL], errorEvent->serial);
608     fprintf (stderr, "\n  ");
609     fprintf (stderr, pchErrorFormat[E_CURRENT_SERIAL],
610                         LastKnownRequestProcessed(display));
611     fprintf (stderr, "\n");
612
613     return (0);
614 }
615
616 /*-------------------------------------------------------------
617 **      Setup the X error handler
618 */
619 static void
620 InstallXErrorHandler ( display )
621 Display *display;
622 {
623     char buffer[BUFSIZ];
624     int i;
625
626     /*
627      * Fetch the X error format strings from XErrorDB
628      */
629     for (i = 0; i< NUM_E_STRINGS; i++)
630     {
631         XGetErrorDatabaseText (display, "XlibMessage",
632                     pchErrorFormatNames[i], " %d ", buffer, BUFSIZ);
633
634         pchErrorFormat[i] = (char *) XtMalloc (1+strlen(buffer));
635
636         strcpy(pchErrorFormat[i], buffer);
637     }
638
639     XSetErrorHandler (_HandleXErrors);
640 }