dtcm: Coverity 89287
[oweals/cde.git] / cde / programs / dtwm / WmMain.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 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 /* 
24  * (c) Copyright 1989, 1990, 1991, 1992 OPEN SOFTWARE FOUNDATION, INC. 
25  * ALL RIGHTS RESERVED 
26 */ 
27 /* 
28  * Motif Release 1.2.1
29 */
30 /*
31  * (c) Copyright 1987, 1988, 1989, 1990 HEWLETT-PACKARD COMPANY */
32
33 /*
34  * Included Files:
35  */
36
37 #include "WmGlobal.h"
38
39 #include <locale.h>
40 #include <Dt/Message.h>
41 #include <Dt/EnvControlP.h>
42 /*
43  * include extern functions
44  */
45
46 #include "WmCEvent.h"
47 #include "WmEvent.h"
48 #include "WmInitWs.h"
49 #include "WmError.h"
50 #include "WmIPC.h"
51 #include "WmBackdrop.h"
52
53
54 /*
55  * Function Declarations:
56  */
57 int WmReturnIdentity (int argc, char *argv[], char *environ[]);
58 #define ManagedRoot(w) (!XFindContext (DISPLAY, (w), wmGD.screenContextType, \
59 (caddr_t *)&pSD) ? (SetActiveScreen (pSD), True) : \
60 (IsBackdropWindow (ACTIVE_PSD, (w))))
61
62 WmScreenData *pSD;
63
64 /*
65  * Global Variables:
66  */
67
68 WmGlobalData wmGD;
69 #ifndef NO_MESSAGE_CATALOG
70 NlsStrings wmNLS;
71 #endif
72 int WmIdentity;
73
74
75 \f
76 /*************************************<->*************************************
77  *
78  *  main (argc, argv, environ)
79  *
80  *
81  *  Description:
82  *  -----------
83  *  This is the main window manager function.  It calls window manager
84  *  initializtion functions and has the main event processing loop.
85  *
86  *
87  *  Inputs:
88  *  ------
89  *  argc = number of command line arguments (+1)
90  *
91  *  argv = window manager command line arguments
92  *
93  *  environ = window manager environment
94  *
95  *************************************<->***********************************/
96
97 int
98 main (int argc, char *argv [], char *environ [])
99 {
100     XEvent      event;
101     Boolean     dispatchEvent;
102
103     setlocale(LC_ALL, "");
104
105     /*
106      * Set up environment variables for this HP DT client
107      */
108     _DtEnvControl(DT_ENV_SET);
109
110     /*
111      * Force LANG lookup early. 
112      * (Front end may change $LANG to 'C' as part
113      *  of string space reduction optimization.)
114      */
115      {
116          char * foo = ((char *)GETMESSAGE(44, 1, ""));
117      }
118     XtSetLanguageProc (NULL, (XtLanguageProc)NULL, NULL);
119
120     /*  
121      * Get Identity
122      */
123     WmIdentity = WmReturnIdentity(argc, argv, environ);
124
125     /*
126      * Initialize the workspace:
127      */
128
129     InitWmGlobal (argc, argv, environ);
130
131     /*
132      * Set up PATH variable if it must run as standalone command
133      * invoker
134      */
135     if (wmGD.dtLite)
136     {
137         _DtEnvControl(DT_ENV_SET_BIN);
138     }
139     
140     /*
141      * MAIN EVENT HANDLING LOOP:
142      */
143
144     for (;;)
145     {
146         XtAppNextEvent (wmGD.mwmAppContext, &event);
147
148
149         /*
150          * Check for, and process non-widget events.  The events may be
151          * reported to the root window, to some client frame window,
152          * to an icon window, or to a "special" window management window.
153          * The lock modifier is "filtered" out for window manager processing.
154          */
155
156         wmGD.attributesWindow = 0L;
157
158         if ((event.type == ButtonPress) || 
159             (event.type == ButtonRelease))
160         {
161             if ((wmGD.evLastButton.button != 0) &&
162                 ReplayedButtonEvent (&(wmGD.evLastButton), 
163                                      &(event.xbutton)))
164             {
165                 wmGD.bReplayedButton = True;
166             }
167             else
168             {
169                 /* save this button for next comparison */
170                 memcpy (&wmGD.evLastButton, &event, sizeof (XButtonEvent));
171                 wmGD.bReplayedButton = False;
172             }
173         }
174         dispatchEvent = True;
175         if (wmGD.menuActive)
176         {
177             /*
178              * Do special menu event preprocessing.
179              */
180
181             if (wmGD.checkHotspot || wmGD.menuUnpostKeySpec ||
182                 wmGD.menuActive->accelKeySpecs)
183             {
184                 dispatchEvent = WmDispatchMenuEvent ((XButtonEvent *) &event);
185             }
186         }
187
188         if (dispatchEvent)
189         {
190             if (ManagedRoot(event.xany.window))
191             {
192                 dispatchEvent = WmDispatchWsEvent (&event);
193             }
194             else
195             {
196                 dispatchEvent = WmDispatchClientEvent (&event);
197             }
198
199             if (dispatchEvent)
200             {
201                 /*
202                  * Dispatch widget related event:
203                  */
204
205                 XtDispatchEvent (&event);
206             }
207         }
208     }
209
210 } /* END OF FUNCTION main */
211
212 /******************************<->*************************************
213  *
214  *  WmReturnIdentity (argc, argv, environ)
215  *
216  *
217  *  Description:
218  *  -----------
219  *  This function checks the last component of the (path)name
220  *  contained in argv[0] and makes a global decision as to whether
221  *  it should fetch resources as mwm or dtwm.
222  *
223  *  Inputs:
224  *  ------
225  *  argc = number of command line arguments (+1)
226  *
227  *  argv = window manager command line arguments
228  *
229  *  environ = window manager environment
230  *
231  ******************************<->***********************************/
232
233 int WmReturnIdentity ( int argc, char *argv[], char *environ[]) 
234 {
235         char *tempString;
236         char *origPtr;
237
238         /* assume it's dtwm until proven differently */
239
240         int retVal = DT_MWM;
241
242         if (!(tempString = 
243               (char *)(XtMalloc ((unsigned int)(strlen (argv[0]) + 1)))))
244         {
245                 Warning(((char *)GETMESSAGE(44, 2, "Insufficient memory for name of window manager")));
246                 exit(WM_ERROR_EXIT_VALUE);
247         }
248
249         origPtr = tempString;
250
251         if (strrchr(argv[0], '/'))
252         {
253                 
254                 strcpy(tempString, (strrchr(argv[0], '/')));
255
256                 tempString++;
257         }
258         else
259                 strcpy(tempString, argv[0]);
260
261         if (!(strcmp(tempString, WM_RESOURCE_NAME)))
262         /*
263          *
264          *   If it's explicity "mwm", then set our identity anew.
265          *
266          */
267         {
268                 retVal = MWM;
269         }
270
271         XtFree((char *)origPtr);
272
273         return(retVal);
274
275 } /* END OF FUNCTION WmReturnIdentity */
276
277 /*************************     eof   ******************************/