Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtlogin / vgutil.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 /* $TOG: vgutil.c /main/11 1998/11/03 19:17:12 mgreess $ */
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  **   File:        vgutil.c
34  **
35  **   Project:     HP Visual User Environment (DT)
36  **
37  **   Description: Utility file for Dtgreet application.
38  **
39  **                This file contains global data declarations, resource
40  **                declarations, and various utility routines.
41  **
42  **
43  **   (c) Copyright 1987, 1988, 1989 by Hewlett-Packard Company
44  **
45  **
46  **
47  ****************************************************************************
48  ************************************<+>*************************************/
49
50
51 /***************************************************************************
52  *
53  *  Includes
54  *
55  ***************************************************************************/
56
57 #include <nl_types.h>
58 #include <stdio.h>
59 #include <setjmp.h>
60 #include <time.h>
61 #include <sys/signal.h>
62 #include <stdarg.h>
63 #define Va_start(a,b) va_start(a,b)
64
65 #include <X11/Xlibint.h>
66 #include <Xm/Xm.h>
67
68 #include "vg.h"
69 #include "vgmsg.h"
70
71 /****************************************************************************
72  *
73  *  Defines
74  *
75  ****************************************************************************/
76 #define NLSPATH_ENV  "NLSPATH=" \
77                         CDE_INSTALLATION_TOP "/nls/msg/%L/%N.cat:" \
78                         CDE_INSTALLATION_TOP "/lib/nls/msg/%L/%N.cat:" \
79                         CDE_INSTALLATION_TOP "/lib/nls/msg/%l/%t/%c/%N.cat:" \
80                         CDE_INSTALLATION_TOP "/lib/nls/msg/%l/%c/%N.cat"
81
82 #define NLS_CATALOG  "dtlogin"
83
84 /***************************************************************************
85  *
86  *  Procedure declarations
87  *
88  ***************************************************************************/
89 static SIGVAL syncTimeout( int arg ) ;
90
91 /***************************************************************************
92  *
93  *  Global variables
94  *
95  ***************************************************************************/
96 static nl_catd  nl_fd;  /* message catalog file descriptor */
97
98 /***************************************************************************
99  *
100  *  ChangeBell
101  *
102  *  turn the keyboard bell on/off
103  *
104  *  Input:  "on",  "off"
105  ***************************************************************************/
106
107 void 
108 ChangeBell( char *string )
109 {
110     static int  percent = -1;
111     
112     XKeyboardControl control;
113     XKeyboardState   state;
114     
115     if ( strcmp (string, "on" ) == 0 )
116         control.bell_percent = percent;
117     
118     if ( strcmp (string, "off") == 0 ) {
119         XGetKeyboardControl(dpyinfo.dpy, &state);
120         percent = state.bell_percent;
121         control.bell_percent = 0; 
122     }   
123
124     if (percent >= 0)
125         XChangeKeyboardControl(dpyinfo.dpy, KBBellPercent, &control);
126 }
127
128
129
130         
131 /***************************************************************************
132  *
133  *  InitArg
134  *
135  *  Initialize the wiget argument list before creating the widget. These
136  *  are resources the user cannot override.
137  *
138  *  The values chosen for these resources are not necessarily the Motif
139  *  defaults, but rather those that are most common for all the widgets. A
140  *  particular widget may override any of these choices before creation.
141  ***************************************************************************/
142
143 static XmTextScanType sarray[] = {XmSELECT_POSITION};
144
145 int 
146 InitArg( WidgetType wtype )
147 {
148     register int i;
149
150     i = 0;
151
152
153     /*
154      *  Core resource set...
155      */
156      
157                                         /* bug workaround in toolkit       */
158     if (wtype == DrawingA   ||
159         wtype == Frame      ||
160         wtype == Label      ||
161         wtype == MessageBox ||
162         wtype == PushB      ||
163         wtype == Text           ) {
164     
165         XtSetArg(argt[i], XmNaccelerators,      NULL                    ); i++;
166     }
167
168
169     if (wtype == DrawingA   ||
170         wtype == Frame      ||
171         wtype == Form       ||
172         wtype == Label      ||
173         wtype == MessageBox ||
174         wtype == PushB      ||
175         wtype == Text           ) {
176     
177         XtSetArg(argt[i], XmNancestorSensitive, True                    ); i++;
178         /*                XmNbackground,        (set by user)           */
179         /*                XmNbackgroundPixmap,  (set by user)           */
180         /*                XmNborderColor,       (set by user)           */
181         /*                XmNborderPixmap,      (set by user)           */
182         XtSetArg(argt[i], XmNborderWidth,       0                       ); i++;
183         /*                XmNcolormap,          (default)               */
184         /*                XmNdepth,             (set by Xt)             */
185         XtSetArg(argt[i], XmNdestroyCallback,   NULL                    ); i++;
186         /*                XmNheight,            (set by user)           */
187         XtSetArg(argt[i], XmNmappedWhenManaged, True                    ); i++;
188         /*                XmNscreen,            (default)               */
189         XtSetArg(argt[i], XmNsensitive,         True                    ); i++;
190         /*                XmNtranslations,      (default)               */
191         /*                XmNwidth,             (set by user)           */
192         /*                XmNx,                 (set by user)           */
193         /*                XmNy,                 (set by user)           */
194     }
195
196
197
198     /*
199      *  Object resource set...
200      */
201      
202     if (wtype == CascadeBG   ||
203         wtype == LabelG      ||
204         wtype == PushBG      ||
205         wtype == SeparatorG  ||
206         wtype == ToggleBG       ) {
207
208         XtSetArg(argt[i], XmNdestroyCallback,   NULL                    ); i++;
209     }
210
211
212
213     /*
214      *  RectObj resource set...
215      */
216      
217     if (wtype == CascadeBG   ||
218         wtype == LabelG      ||
219         wtype == PushBG      ||
220         wtype == SeparatorG  ||
221         wtype == ToggleBG       ) {
222     
223         XtSetArg(argt[i], XmNancestorSensitive, True                    ); i++;
224         XtSetArg(argt[i], XmNborderWidth,       0                       ); i++;
225         /*                XmNheight,            (set by user)           */
226         XtSetArg(argt[i], XmNsensitive,         True                    ); i++;
227         /*                XmNwidth,             (set by user)           */
228         /*                XmNx,                 (set by user)           */
229         /*                XmNy,                 (set by user)           */
230     }
231
232
233
234     /*
235      *  XmGadget  resource set...
236      */
237      
238     if (wtype == CascadeBG   ||
239         wtype == LabelG      ||
240         wtype == PushBG      ||
241         wtype == SeparatorG  ||
242         wtype == ToggleBG       ) {
243     
244         XtSetArg(argt[i], XmNhelpCallback,      NULL                    ); i++;
245         XtSetArg(argt[i], XmNhighlightOnEnter,  True                    ); i++;
246         /*                XmNhighlightThickness,(set by user)           */
247         /*                XmNshadowThickness,   (set by user)           */
248         XtSetArg(argt[i], XmNtraversalOn,       True                    ); i++;
249         XtSetArg(argt[i], XmNunitType,          appInfo.unitType        ); i++;
250         XtSetArg(argt[i], XmNuserData,          NULL                    ); i++;
251     }
252
253
254
255
256     /*
257      *  Composite resource set...
258      */
259      
260     if (wtype == DrawingA   ||
261         wtype == Frame      ||
262         wtype == Form       ||
263         wtype == MessageBox     ) {
264     
265         XtSetArg(argt[i], XmNinsertPosition,    NULL                    ); i++;
266     }
267
268
269
270     /*
271      *  XmPrimitive  resource set...
272      */
273      
274     if (wtype == Label      ||
275         wtype == PushB      ||
276         wtype == Text           ) {
277     
278         /*                XmNbottomShadowColor, (set by user)           */
279         /*                XmNbottomShadowPixmap,(set by user)           */
280         /*                XmNforeground,        (set by user)           */
281         XtSetArg(argt[i], XmNhelpCallback,      NULL                    ); i++;
282         /*                XmNhighlightColor,    (set by user)           */
283         XtSetArg(argt[i], XmNhighlightOnEnter,  False                   ); i++;
284         /*                XmNhighlightPixmap,   (set by user)           */
285         /*                XmNhighlightThickness,(set by user)           */
286         /*                XmNshadowThickness,   (set by user)           */
287         /*                XmNtopShadowColor,    (set by user)           */
288         /*                XmNtopShadowPixmap,   (set by user)           */
289         XtSetArg(argt[i], XmNtraversalOn,       True                    ); i++;
290         XtSetArg(argt[i], XmNunitType,          appInfo.unitType        ); i++;
291         XtSetArg(argt[i], XmNuserData,          NULL                    ); i++;
292     }
293
294
295
296     /*
297      *  XmForm Constraint resource set...
298      */
299      
300     if (wtype == Form) {
301     
302         XtSetArg(argt[i], XmNbottomAttachment,  XmATTACH_NONE           ); i++;
303         XtSetArg(argt[i], XmNleftAttachment,    XmATTACH_NONE           ); i++;
304         XtSetArg(argt[i], XmNresizable,         False                   ); i++;
305         XtSetArg(argt[i], XmNrightAttachment,   XmATTACH_NONE           ); i++;
306         XtSetArg(argt[i], XmNtopAttachment,     XmATTACH_NONE           ); i++;
307     }
308
309
310     /*
311      *  XmManager resource set...
312      */
313      
314     if (wtype == DrawingA   ||
315         wtype == Frame      ||
316         wtype == Form       ||
317         wtype == MessageBox     ) {
318     
319         /*                XmNbottomShadowColor, (set by user)           */
320         /*                XmNbottomShadowPixmap,(set by user)           */
321         /*                XmNforeground,        (set by user)           */
322         XtSetArg(argt[i], XmNhelpCallback,      NULL                    ); i++;
323         /*                XmNhighlightColor,    (set by user)           */
324         /*                XmNhighlightPixmap,   (set by user)           */
325         /*                XmNshadowThickness,   (set by user)           */
326         /*                XmNtopShadowColor,    (set by user)           */
327         /*                XmNtopShadowPixmap,   (set by user)           */
328         XtSetArg(argt[i], XmNunitType,          appInfo.unitType        ); i++;
329         XtSetArg(argt[i], XmNuserData,          NULL                    ); i++;
330     }
331     
332
333
334     /*
335      *  XmBulletinBoard resource set...
336      */
337      
338     if (wtype == Form       ||
339         wtype == MessageBox     ) {
340     
341         XtSetArg(argt[i], XmNallowOverlap,      True                    ); i++;
342         XtSetArg(argt[i], XmNautoUnmanage,      True                    ); i++;
343         XtSetArg(argt[i], XmNbuttonFontList,    appInfo.labelFont       ); i++;
344         /*                XmNcancelButton,      (set by Xm)             */
345         /*                XmNdefaultButton,     (set by Xm)             */
346         XtSetArg(argt[i], XmNdefaultPosition,   True                    ); i++;
347         XtSetArg(argt[i], XmNdialogStyle,       XmDIALOG_MODELESS       ); i++;
348         XtSetArg(argt[i], XmNdialogTitle,       NULL                    ); i++;
349         XtSetArg(argt[i], XmNfocusCallback,     NULL                    ); i++;
350 /*XtSetArg(argt[i], XmNlabelFontList,   appInfo.textFont        ); i++; */
351         XtSetArg(argt[i], XmNmapCallback,       NULL                    ); i++;
352         XtSetArg(argt[i], XmNmarginHeight,      FromMM(10)              ); i++;
353         XtSetArg(argt[i], XmNmarginWidth,       FromMM(10)              ); i++;
354         XtSetArg(argt[i], XmNmapCallback,       NULL                    ); i++;
355         XtSetArg(argt[i], XmNnoResize,          True                    ); i++;
356         XtSetArg(argt[i], XmNresizePolicy,      XmRESIZE_ANY            ); i++;
357         XtSetArg(argt[i], XmNshadowType,        XmSHADOW_OUT            ); i++;
358         XtSetArg(argt[i], XmNstringDirection, XmSTRING_DIRECTION_L_TO_R );i++;
359         XtSetArg(argt[i], XmNtextFontList,      appInfo.textFont        ); i++;
360         XtSetArg(argt[i], XmNtextTranslations,  NULL                    ); i++;
361         XtSetArg(argt[i], XmNunmapCallback,     NULL                    ); i++;
362     }
363     
364
365
366     /*
367      *  XmCascadeButtonGadget resource set...
368      */
369      
370     if (wtype == CascadeBG) {
371     
372         XtSetArg(argt[i], XmNactivateCallback,  NULL                    ); i++;
373         XtSetArg(argt[i], XmNcascadePixmap,     XmUNSPECIFIED_PIXMAP    ); i++;
374         XtSetArg(argt[i], XmNcascadingCallback, NULL                    ); i++;
375         XtSetArg(argt[i], XmNmappingDelay,      100                     ); i++;
376         XtSetArg(argt[i], XmNsubMenuId,         0                       ); i++;
377     }
378     
379
380
381     /*
382      *  XmDrawingArea resource set...
383      */
384      
385     if (wtype == DrawingA) {
386     
387         XtSetArg(argt[i], XmNexposeCallback,    NULL                    ); i++;
388         XtSetArg(argt[i], XmNinputCallback,     NULL                    ); i++;
389         XtSetArg(argt[i], XmNmarginHeight,      0                       ); i++;
390         XtSetArg(argt[i], XmNmarginWidth,       0                       ); i++;
391         XtSetArg(argt[i], XmNresizeCallback,    NULL                    ); i++;
392         XtSetArg(argt[i], XmNresizePolicy,      XmRESIZE_NONE           ); i++;
393     }
394
395
396
397     /*
398      *  XmForm  resource set...
399      */
400      
401     if (wtype == Form) {
402     
403         XtSetArg(argt[i], XmNfractionBase,      100                     ); i++;
404         XtSetArg(argt[i], XmNhorizontalSpacing, 0                       ); i++;
405         XtSetArg(argt[i], XmNrubberPositioning, False                   ); i++;
406         XtSetArg(argt[i], XmNverticalSpacing,   0                       ); i++;
407     }
408     
409
410
411     /*
412      *  XmFrame resource set...
413      */
414      
415     if (wtype == Frame) {
416     
417         XtSetArg(argt[i], XmNmarginHeight,      0                       ); i++;
418         XtSetArg(argt[i], XmNmarginWidth,       0                       ); i++;
419         XtSetArg(argt[i], XmNshadowType,        XmSHADOW_OUT            ); i++;
420     }
421
422
423
424     /*
425      *  XmLabel/XmLabelGadget resource set...
426      */
427
428     if (wtype == CascadeBG   ||
429         wtype == Label       ||
430         wtype == LabelG      ||
431         wtype == PushB       ||
432         wtype == PushBG      ||
433         wtype == ToggleBG       ) {
434     
435         XtSetArg(argt[i], XmNaccelerator,       NULL                    ); i++;
436         XtSetArg(argt[i], XmNacceleratorText,   NULL                    ); i++;
437 /*      XtSetArg(argt[i], XmNalignment,         XmALIGNMENT_CENTER      ); i++;*/
438         /*                XmNalignment,         (default)               */
439         XtSetArg(argt[i], XmNfontList,          appInfo.labelFont       ); i++;
440         XtSetArg(argt[i], XmNlabelInsensitivePixmap,
441                                                 XmUNSPECIFIED_PIXMAP    ); i++;
442         XtSetArg(argt[i], XmNlabelPixmap,       XmUNSPECIFIED_PIXMAP    ); i++;
443         XtSetArg(argt[i], XmNlabelString,       NULL                    ); i++;
444         XtSetArg(argt[i], XmNlabelType,         XmSTRING                ); i++;
445         /*                XmNmarginBottom,      (default)               */
446         /*                XmNmarginHeight,      (default)               */
447         /*                XmNmarginLeft,        (default)               */
448         /*                XmNmarginRight,       (default)               */
449         /*                XmNmarginTop,         (default)               */
450         /*                XmNmarginWidth,       (default)               */
451         XtSetArg(argt[i], XmNmnemonic,          NULL                    ); i++;
452         XtSetArg(argt[i], XmNrecomputeSize,     False                   ); i++;
453         XtSetArg(argt[i], XmNuserData,          NULL                    ); i++;
454         XtSetArg(argt[i], XmNstringDirection, XmSTRING_DIRECTION_L_TO_R ); i++;
455     }
456     
457
458
459     /*
460      *  XmMessageBox resource set...
461      */
462      
463     if (wtype == MessageBox) {
464     
465         XtSetArg(argt[i], XmNcancelCallback,    NULL                    ); i++; 
466         XtSetArg(argt[i], XmNcancelLabelString, NULL                    ); i++;
467         XtSetArg(argt[i], XmNdefaultButtonType, XmDIALOG_OK_BUTTON      ); i++;
468         XtSetArg(argt[i], XmNdialogType,        XmDIALOG_MESSAGE        ); i++;
469         XtSetArg(argt[i], XmNhelpLabelString,   NULL                    ); i++;
470         XtSetArg(argt[i], XmNmessageAlignment,  XmALIGNMENT_BEGINNING   ); i++;
471         XtSetArg(argt[i], XmNmessageString,     NULL                    ); i++;
472         XtSetArg(argt[i], XmNminimizeButtons,   FALSE                   ); i++;
473         XtSetArg(argt[i], XmNokCallback,        NULL                    ); i++; 
474         XtSetArg(argt[i], XmNokLabelString,     NULL                    ); i++;
475         /*                XmNsymbolPixmap,      (set by Xm)             */
476     }
477
478
479
480     /*
481      *  XmPushButton/XmPushButtonGadget resource set...
482      */
483      
484     if (wtype == PushB    ||
485         wtype == PushBG         ) {
486     
487         XtSetArg(argt[i], XmNactivateCallback,          NULL            ); i++;
488         XtSetArg(argt[i], XmNarmCallback,               NULL            ); i++;
489         /*                XmNarmColor,                  (set by user)   */
490         /*                XmNarmPixmap,                 (set by user)   */
491         XtSetArg(argt[i], XmNdisarmCallback,            NULL            ); i++;
492         XtSetArg(argt[i], XmNfillOnArm,                 True            ); i++;
493         XtSetArg(argt[i], XmNshowAsDefault,             0               ); i++;
494     }
495     
496
497
498     /*
499      *  XmSeparatorGadget resource set...
500      */
501      
502     if (wtype == SeparatorG) {
503     
504         XtSetArg(argt[i], XmNmargin,            0                       ); i++;
505         XtSetArg(argt[i], XmNorientation,       XmHORIZONTAL            ); i++;
506         XtSetArg(argt[i], XmNseparatorType,     XmSHADOW_ETCHED_IN      ); i++;
507     }
508
509
510
511     /*
512      *  XmText  resource set...
513      */
514      
515     if (wtype == Text ) {
516     
517         XtSetArg(argt[i], XmNactivateCallback,          NULL            ); i++;
518         XtSetArg(argt[i], XmNautoShowCursorPosition,    True            ); i++;
519         XtSetArg(argt[i], XmNcursorPosition,            0               ); i++;
520         XtSetArg(argt[i], XmNeditable,                  True            ); i++;
521         XtSetArg(argt[i], XmNeditMode,               XmSINGLE_LINE_EDIT ); i++;
522         XtSetArg(argt[i], XmNfocusCallback,             NULL            ); i++;
523         XtSetArg(argt[i], XmNlosingFocusCallback,       NULL            ); i++;
524         XtSetArg(argt[i], XmNmarginHeight,           TEXT_MARGIN_HEIGHT ); i++;
525         XtSetArg(argt[i], XmNmarginWidth,            TEXT_MARGIN_WIDTH  ); i++;
526         XtSetArg(argt[i], XmNmaxLength,                 20              ); i++;
527         XtSetArg(argt[i], XmNmodifyVerifyCallback,      NULL            ); i++;
528         XtSetArg(argt[i], XmNmotionVerifyCallback,      NULL            ); i++;
529         /*                XmNtopPosition,               (default)       */
530         /*                XmNvalue,                     (set by ??)     */
531     
532         /*              XmText Input  Resource Set                      */
533         XtSetArg(argt[i], XmNpendingDelete,             True            ); i++;
534         XtSetArg(argt[i], XmNselectionArray,            sarray          ); i++;
535         XtSetArg(argt[i], XmNselectThreshold,           1000            ); i++;
536     
537         /*              XmText Output Resource Set                      */
538         /*                XmNblinkRate,                 (set by user)   */
539         XtSetArg(argt[i], XmNfontList,               appInfo.labelFont  ); i++;
540         XtSetArg(argt[i], XmNcursorPositionVisible,     True            ); i++;
541         XtSetArg(argt[i], XmNresizeHeight,              False           ); i++;
542         XtSetArg(argt[i], XmNresizeWidth,               False           ); i++;
543         /*                XmNrows,                      (ignored)       */
544         /*                XmNwordWrap,                  (ignored)       */
545     }
546     
547
548
549     /*
550      *  XmToggleButtonGadget resource set...
551      */
552      
553     if (wtype == ToggleBG) {
554     
555         XtSetArg(argt[i], XmNarmCallback,       NULL                    ); i++;
556         XtSetArg(argt[i], XmNdisarmCallback,    NULL                    ); i++;
557         XtSetArg(argt[i], XmNfillOnSelect,      True                    ); i++;
558         XtSetArg(argt[i], XmNindicatorOn,       True                    ); i++;
559         XtSetArg(argt[i], XmNindicatorType,     XmONE_OF_MANY           ); i++;
560         /*                XmNselectColor,       (set by user)           */
561         XtSetArg(argt[i], XmNselectInsensitivePixmap,
562                                                 XmUNSPECIFIED_PIXMAP    ); i++;
563         XtSetArg(argt[i], XmNselectPixmap,      XmUNSPECIFIED_PIXMAP    ); i++;
564         XtSetArg(argt[i], XmNset,               False                   ); i++;
565         /*                XmNspacing,           (default)               */
566         XtSetArg(argt[i], XmNvalueChangedCallback,
567                                                 NULL                    ); i++;
568         XtSetArg(argt[i], XmNvisibleWhenOff,    False                   ); i++;
569     }
570
571
572     return (i);
573 }
574
575
576
577
578 /***************************************************************************
579  *
580  *  FromMM
581  *
582  *  Convert from 100th Millimeter units into the widget's units.
583  *
584  *  This routine does NOT use the Motif Resolution Independence mechanism.
585  *  Unless the user informs the X-server as to the current monitor size (or
586  *  the monitor happens to be the default), it is possible that the
587  *  millimeter screen size returned by the X-server is incorrect.  This
588  *  causes the Motif Resolution Independence mechanism to generate incorrect
589  *  values and skew the visuals.
590  *
591  *  For ease of specification, all sizes internal to Dtgreet are specified
592  *  in a generic unit (approx.  100th Millimeters) and then converted to an
593  *  equivalent pixel value by this routine.  All Dtgreet widgets default
594  *  to pixel unit type.  Displays of the same resolution will contain
595  *  identical pixel values, but the actual size on the screen may vary
596  *  depending on the monitor size.
597  *
598  *  There are three conversion values, one each for LowRes (640x480), MedRes
599  *  (1024x768), and HiRes (1280x1024) displays.  They are calculated to
600  *  produce the same pixel values as does the Motif R.I. mechanism on the
601  *  HP 13" Low Res. display, 16" Med. Res.  display and 19" Hi Res. display, 
602  *  all pleasing visuals.
603  *
604  *  If the user specifies the widgets unit type to be 100TH_MILLIMETERS, the
605  *  Motif Resolution Independence mechanism is reactivated and can be used by
606  *  the user.  The internal sizes specified will not be converted by this
607  *  routine but rather internally by Motif.
608  *
609  ***************************************************************************/
610
611 int 
612 FromMM( int mm )
613 {
614     int                 pixel;
615     
616     if ( appInfo.unitType == Xm100TH_MILLIMETERS )
617         return(mm);
618     
619
620     /*
621      *  convert 100thMillimeters to Pixels. Do not round up to the next
622      *  pixel...
623      */
624      
625     if ( HIRES ) 
626         pixel = (mm * 0.035854);                /* HiRes        */
627
628     else  if ( MEDRES )
629         pixel = (mm * 0.033574);                /* MedRes       */
630
631     else 
632         pixel = (mm * 0.026016);                /* LowRes       */
633
634     return(pixel);
635
636 }
637
638
639
640
641 /***************************************************************************
642  *
643  *  FromPixel
644  *
645  *  Convert from pixel units into the widget's units
646  ***************************************************************************/
647
648 int 
649 FromPixel( Widget w, int orientation, int pixel )
650 {
651     Arg                 argt[10];
652     unsigned char       unit_type;
653     
654     XtSetArg(argt[0], XmNunitType, &unit_type);
655     XtGetValues(w, argt, 1);
656     
657     if (unit_type == XmPIXELS)
658         return(pixel);
659     else
660         return(XmConvertUnits(w,
661                               orientation,
662                               XmPIXELS,
663                               pixel,
664                               (int)unit_type));
665 }
666
667
668
669
670 /***************************************************************************
671  *
672  *  GetBiggest
673  *
674  *  Utility function to help determine largest of a set of widgets.
675  ***************************************************************************/
676
677
678 void 
679 GetBiggest( Widget widget, Dimension *width, Dimension *height )
680 {
681     Dimension new_width, new_height;
682
683     XtSetArg(argt[0], XmNwidth, &new_width);
684     XtSetArg(argt[1], XmNheight, &new_height);
685
686     XtGetValues(widget, argt, 2);
687
688     if (width != NULL && new_width > *width)
689         *width = new_width;
690     if (height != NULL && new_height > *height)
691         *height = new_height;
692 }
693
694
695
696
697 /***************************************************************************
698  *
699  *  LogError
700  *
701  *  send errors to the error log. This routine accepts a variable number
702  *  of arguments.
703  ***************************************************************************/
704
705 void 
706 LogError( unsigned char *fmt, ...)
707 {
708     va_list  args;
709     time_t   timer;
710     
711     Va_start(args,fmt);
712     
713     if (errorLogFile && errorLogFile[0] && 
714         (freopen(errorLogFile, "a", stderr) != NULL)) {
715
716         timer = time(NULL);
717         fprintf(stderr, "\n%s", ctime(&timer));
718         fprintf (stderr, "error (pid %ld): ", (long)getpid());
719
720         vfprintf (stderr, (char *)fmt, args);
721         fflush (stderr);
722     }
723
724     va_end(args);
725 }
726
727
728 /***************************************************************************
729  *
730  *  CloseCatalog
731  *
732  ***************************************************************************/
733
734 void
735 CloseCatalog()
736 {
737     catclose(nl_fd);
738 }
739
740 /***************************************************************************
741  *
742  *  OpenCatalog
743  *
744  ***************************************************************************/
745
746 void
747 OpenCatalog()
748 {
749     static int initialized = 0;
750     char *nlspath, *newnlspath;
751
752     if (initialized) return;
753     initialized = 1;
754
755     nlspath = getenv("NLSPATH");
756  
757     if (NULL == nlspath || 0 == strlen(nlspath))
758     {
759         newnlspath = malloc(8 + strlen(NLSPATH_ENV) + 1);
760         sprintf(newnlspath, "NLSPATH=%s", NLSPATH_ENV);
761     }
762     else
763     {
764         newnlspath = malloc(8 + strlen(nlspath) + 1 + strlen(NLSPATH_ENV) + 1);
765         sprintf(newnlspath, "NLSPATH=%s:%s", nlspath, NLSPATH_ENV);
766     }
767
768     putenv(newnlspath);
769
770     /*
771      * open the message catalog. If the language_specific version cannot
772      * be opened, try the default...
773      */
774     if (NULL != langenv)
775     {
776         nl_fd = catopen(NLS_CATALOG, NL_CAT_LOCALE);
777         if (0 > (int) nl_fd)
778           LogError((unsigned char*) MC_DEF_LOG_NO_MSGCAT, langenv);
779     }
780 }
781
782
783 /***************************************************************************
784  *
785  *  ReadCatalog
786  *
787  *  read a string from the message catalog and convert to unsigned char *
788  ***************************************************************************/
789
790 unsigned char *
791 ReadCatalog(int setn, int msgn, char *dflt)
792 {
793     OpenCatalog();
794     if (0 > (int) nl_fd)
795       return (unsigned char*) dflt;
796     else
797       return (unsigned char*) catgets(nl_fd, setn, msgn, dflt);
798 }
799  
800 /***************************************************************************
801  *
802  *  ReadCatalogXms
803  *
804  *  read a string from the message catalog and convert to compound string
805  ***************************************************************************/
806
807 XmString
808 ReadCatalogXms(int setn, int msgn, char *dflt)
809 {
810     OpenCatalog();
811     if (0 > (int) nl_fd)
812       return XmStringCreateLocalized(dflt);
813     else
814       return XmStringCreateLocalized((char*) ReadCatalog(setn, msgn, dflt));
815 }
816
817
818
819 /***************************************************************************
820  *
821  *  SecureDisplay/UnsecureDisplay
822  *
823  *  grab/release the server and keyboard
824  ***************************************************************************/
825
826 static jmp_buf  syncJump;
827 static int      grabServer;             /* Boolean on grabbing server      */
828 static int      grabTimeout;            /* timeout to grab server          */
829
830 static SIGVAL
831 syncTimeout( int arg )
832 {
833     longjmp (syncJump, 1);
834 }
835
836 int 
837 SecureDisplay( void )
838 {
839     
840     char *t;
841     
842
843     /*
844      *  get grab values from the environment...
845      */
846
847     grabServer  = ((t = (char *)getenv(GRABSERVER))  == NULL ? 0 : atoi(t));
848     grabTimeout = ((t = (char *)getenv(GRABTIMEOUT)) == NULL ? 0 : atoi(t));
849
850
851     /*
852      *  grab server then the keyboard...
853      */
854     signal (SIGALRM, syncTimeout);
855     if (setjmp (syncJump)) {
856         LogError(ReadCatalog(MC_LOG_SET,MC_LOG_NO_SECDPY,MC_DEF_LOG_NO_SECDPY),
857                    dpyinfo.name);
858         return(1);
859     }
860     alarm ((unsigned int) grabTimeout);
861     XGrabServer (dpyinfo.dpy);
862     if (XGrabKeyboard (dpyinfo.dpy,
863                        DefaultRootWindow (dpyinfo.dpy),
864                        True,
865                        GrabModeAsync,
866                        GrabModeAsync,
867                        CurrentTime) != GrabSuccess) {
868         alarm (0);
869         signal (SIGALRM, SIG_DFL);
870         LogError(ReadCatalog(MC_LOG_SET,MC_LOG_NO_SECKEY,MC_DEF_LOG_NO_SECKEY),
871                   dpyinfo.name);
872         return(1);
873     }
874
875     alarm (0);
876     signal (SIGALRM, SIG_DFL);
877 /*    pseudoReset (dpy);*/
878
879     /*
880      *  release server if grab not requested...
881      */
882     if (!grabServer)
883     {
884         XUngrabServer (dpyinfo.dpy);
885         XSync (dpyinfo.dpy, 0);
886     }
887
888     return (0);
889 }
890
891
892 void 
893 UnsecureDisplay( void )
894 {
895     XUngrabKeyboard(dpyinfo.dpy, CurrentTime);
896     if (grabServer)
897         XUngrabServer (dpyinfo.dpy);
898     XSync (dpyinfo.dpy, 0);
899 }
900
901
902
903
904 /***************************************************************************
905  *
906  *  SetResourceDatabase
907  *
908  *  
909  *  set up display's resource database with defaults and user values
910  *
911  *  Starting with DT 3.0, the widget's unit type defaults to XmPIXELS 
912  *  rather than Xm100TH_MILLIMETERS. For backwards compatibility, it is
913  *  necessary to determine if the user has specified Xm100TH_MILLIMETERS
914  *  for the widget's unit type.
915  *
916  ***************************************************************************/
917
918 void 
919 SetResourceDatabase( void )
920 {
921
922 typedef struct {
923         char *name;
924         char *value;
925     } DefResource;
926
927
928     int         i;
929     char        defaultWidth[32], defaultHeight[32];
930     char        *rmtype;        /* for XrmGetResource()                    */
931     XrmValue    rmvalue;        /* for XrmGetResource()                    */
932
933     static XrmDatabase  defDb;
934            XrmDatabase  userDb; 
935
936
937 static DefResource defResource[] = {
938     { "*unitType",                      XmNPIXELS               },
939     { "*matte.x",                        "-1"                    },
940     { "*matte.y",                        "-1"                    },
941     { "*login_matte.topPosition",       LOGIN_TOP_POSITION      },
942     { "*login_matte.bottomPosition",    LOGIN_BOTTOM_POSITION   },
943     { "*login_matte.leftPosition",      LOGIN_LEFT_POSITION     },
944     { "*login_matte.rightPosition",     LOGIN_RIGHT_POSITION    },
945     { "*table.background",              TABLE_BACKGROUND        },
946     { "*login_text.foreground",         TEXT_FOREGROUND         },
947     { "*passwd_text.foreground",        TEXT_FOREGROUND         },
948     };
949
950 # define NUM_RESOURCES   (sizeof defResource / sizeof defResource[0])
951
952
953 static DefResource defResourceBW[] = {
954     { "*background",                    BW_BACKGROUND           },
955     { "*foreground",                    BW_FOREGROUND           },
956     { "*login_text.background",         BW_TEXT_BACKGROUND      },
957     { "*passwd_text.background",        BW_TEXT_BACKGROUND      },
958     };
959
960 # define NUM_BW_RESOURCES   (sizeof defResourceBW / sizeof defResourceBW[0])
961
962
963 static DefResource defResourceGS[] = {
964     { "*logo*Background",               BW_LOGO_BACKGROUND      },
965     };
966
967 # define NUM_GS_RESOURCES   (sizeof defResourceGS / sizeof defResourceGS[0])
968
969
970 static DefResource defResourceCO[] = {
971     { "*background",                    CO_BACKGROUND           },
972     { "*foreground",                    CO_FOREGROUND           },
973     { "*login_text.background",         CO_TEXT_BACKGROUND      },
974     { "*passwd_text.background",        CO_TEXT_BACKGROUND      },
975     { "*highlightColor",                CO_HIGHLIGHT            },
976     { "*XmToggleButtonGadget*selectColor",
977                                                 CO_SELECTCOLOR          },
978     };
979
980 # define NUM_CO_RESOURCES   (sizeof defResourceCO / sizeof defResourceCO[0])
981
982
983 #ifdef VG_TRACE
984     vg_TRACE_EXECUTION("SetResourceDatabase ...");
985 #endif /* VG_TRACE */
986
987     /*
988      *  check if user specified 100TH_MILLIMETERS as the unit type...
989      *
990      *  initialize the local control variable "appInfo.unitType" to the 
991      *  unit type...
992      */
993
994     appInfo.unitType = XmPIXELS;
995     
996     if ( XrmGetResource(XtDatabase(dpyinfo.dpy),
997                    VnNmatteUnitType,  VnCMatteUnitType,
998                    &rmtype, &rmvalue ) ) {
999         
1000         if ( strcmp (rmvalue.addr, XmN100TH_MILLIMETERS) == 0 )
1001             appInfo.unitType = Xm100TH_MILLIMETERS;
1002     }
1003
1004
1005     /*
1006      *  build new resource database with default values....
1007      */
1008      
1009     defDb = XtDatabase(dpyinfo.dpy);
1010     
1011     sprintf(defaultWidth,  "%d", FromMM(MATTE_WIDTH));
1012     sprintf(defaultHeight, "%d", FromMM(MATTE_HEIGHT));
1013
1014         /*
1015     XrmPutStringResource(&defDb, VnNmatteWidth,  defaultWidth);
1016     XrmPutStringResource(&defDb, VnNmatteHeight, defaultHeight);
1017         */
1018
1019     if(!XrmGetResource(defDb, "*labelFont", "*LabelFont",
1020        &rmtype, &rmvalue))
1021         XrmPutStringResource(&defDb, "*labelFont",
1022                              ( HIRES ? LABEL_FONT_HRES : LABEL_FONT));
1023
1024     if(!XrmGetResource(defDb, "*textFont", "*TextFont",
1025        &rmtype, &rmvalue))
1026         XrmPutStringResource(&defDb, "*textFont",
1027                              ( LOWRES ? TEXT_FONT_LRES : TEXT_FONT));
1028
1029     if(!XrmGetResource(defDb, "*greeting.fontList",
1030        "*Greeting.FontList", &rmtype, &rmvalue))
1031         XrmPutStringResource(&defDb, "*greeting.fontList", GREET_FONT);
1032
1033
1034     for (i = 0; i < NUM_RESOURCES; i++)
1035         XrmPutStringResource(&defDb,
1036                              defResource[i].name,
1037                              defResource[i].value);
1038
1039     if ( dpyinfo.depth < 4 )
1040         for (i = 0; i < NUM_BW_RESOURCES; i++)
1041             XrmPutStringResource(&defDb,
1042                                  defResourceBW[i].name,
1043                                  defResourceBW[i].value);
1044     else                                 
1045         for (i = 0; i < NUM_CO_RESOURCES; i++)
1046             XrmPutStringResource(&defDb,
1047                                  defResourceCO[i].name,
1048                                  defResourceCO[i].value);
1049
1050     if ( dpyinfo.visual->class == GrayScale )
1051         for (i = 0; i < NUM_GS_RESOURCES; i++)
1052             XrmPutStringResource(&defDb,
1053                                  defResourceGS[i].name,
1054                                  defResourceGS[i].value);
1055
1056     
1057
1058     /*
1059      *  merge user's specifications with program defaults...
1060      */
1061
1062
1063     /*
1064      *  replace display's resource database with merged database...
1065      */
1066      
1067      
1068     /*
1069      *  Try to compensate if the user specifies the matte dimensions in
1070      *  100TH_MILLIMETERS, but forgets to also specify that unit type...
1071      */
1072
1073     userDb = XtDatabase(dpyinfo.dpy);
1074
1075     if ( XrmGetResource(userDb, VnNmatteWidth, VnCMatteWidth,
1076                    &rmtype, &rmvalue ) ) {
1077
1078         sscanf(rmvalue.addr, "%d", &i);
1079         if ( appInfo.unitType == XmPIXELS  &&  i >= dpyinfo.width ) {
1080             XrmPutStringResource(&userDb, VnNmatteWidth, defaultWidth);
1081             LogError(
1082                 ReadCatalog(MC_LOG_SET,MC_LOG_BAD_MATTE,MC_DEF_LOG_BAD_MATTE),  
1083                 dpyinfo.name);
1084         }
1085     }
1086
1087     if ( XrmGetResource(userDb, VnNmatteHeight, VnCMatteHeight,
1088                    &rmtype, &rmvalue ) ) {
1089
1090         sscanf(rmvalue.addr, "%d", &i);
1091         if ( appInfo.unitType == XmPIXELS  &&  i >= dpyinfo.height ) {
1092             XrmPutStringResource(&userDb, VnNmatteHeight, defaultHeight);
1093             LogError(
1094                 ReadCatalog(MC_LOG_SET,MC_LOG_BAD_HMATTE,MC_DEF_LOG_BAD_HMATTE),
1095                 dpyinfo.name);
1096         }
1097     }
1098     
1099 }    
1100
1101
1102
1103
1104 /***************************************************************************
1105  *
1106  *  ToPixel
1107  *
1108  *  Convert from the widget's units into pixel units
1109  ***************************************************************************/
1110
1111 int 
1112 ToPixel( Widget w, int orientation, int pixel )
1113 {
1114     Arg                 argt[10];
1115     unsigned char       unit_type;
1116     
1117     XtSetArg(argt[0], XmNunitType, &unit_type);
1118     XtGetValues(w, argt, 1);
1119     
1120     if (unit_type == XmPIXELS)
1121         return(pixel);
1122     else {
1123         return(XmConvertUnits(w,
1124                               orientation,
1125                               (int)unit_type,
1126                               pixel,
1127                               XmPIXELS));
1128     }
1129 }
1130
1131
1132
1133 #if 0
1134 /***************************************************************************
1135  *
1136  *  _DtMessage Catalog Stubs
1137  *
1138  *  These stub routines can be used for porting to systems that do not yet
1139  *  support message catalogs. Replace the above "0" with an appropriate
1140  *  define for your target system. Do the same for the external defines
1141  *  in "vg.h".
1142  ***************************************************************************/
1143
1144 nl_catd
1145 catopen(name, oflag)
1146   char    *name;
1147   int      oflag;
1148
1149 {
1150   return (555);
1151 }
1152
1153   
1154 int
1155 catclose(catd)
1156   nl_catd  catd;
1157
1158 {
1159   return;
1160 }
1161   
1162
1163 char *
1164 catgets(catd, set_num, msg_num, def_str)
1165   nl_catd  catd;
1166   int      set_num;
1167   int      msg_num;
1168   char    *def_str;
1169
1170 {
1171     return (def_str);
1172 }    
1173
1174 #endif