dtwm: Change to ANSI function definitions
[oweals/cde.git] / cde / programs / dtwm / WmError.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, 1993, 1994 OPEN SOFTWARE FOUNDATION, INC. 
25  * ALL RIGHTS RESERVED 
26 */ 
27 /* 
28  * Motif Release 1.2.4
29 */
30 /*
31  * (c) Copyright 1987, 1988, 1989, 1990 HEWLETT-PACKARD COMPANY */
32
33 /*
34  * Included Files:
35  */
36
37 #include "WmGlobal.h"
38 #include <stdio.h>
39 #include <Dt/UserMsg.h>
40 #include "WmXSMP.h"
41
42 /*
43  * Function Declarations:
44  */
45 #include "WmError.h"
46
47 #ifdef DEBUG
48
49 #define E_MAJOR_CODE            0
50 #define E_MINOR_CODE            1
51 #define E_RESOURCE_ID           2
52 #define E_ERROR_SERIAL          3
53 #define E_CURRENT_SERIAL        4
54
55 #define NUM_E_STRINGS           5
56
57 static char *pchErrorFormatNames [NUM_E_STRINGS] = {
58     "MajorCode", 
59     "MinorCode", 
60     "ResourceID", 
61     "ErrorSerial", 
62     "CurrentSerial" 
63 };
64
65 static char *pchDefaultErrorFormat [NUM_E_STRINGS] = {
66     " %d ",
67     " %d ",
68     " %ld ",
69     " %ld ",
70     " %ld "
71 };
72
73 static char *pchErrorFormat [NUM_E_STRINGS];
74
75 #endif /* DEBUG */
76
77
78
79 \f
80 /*************************************<->*************************************
81  *
82  *  WmInitErrorHandler (display)
83  *
84  *
85  *  Description:
86  *  -----------
87  *  This function initializes the window manager error handler.
88  *
89  *
90  *  Inputs:
91  *  ------
92  *  display = display we're talking about
93  *  -------
94  *
95  *************************************<->***********************************/
96
97 void
98 WmInitErrorHandler (Display *display)
99 {
100 #ifdef DEBUG
101     char buffer[BUFSIZ];
102     int i;
103
104     /*
105      * Fetch the X error format strings from XErrorDB
106      */
107     for (i = 0; i< NUM_E_STRINGS; i++)
108     {
109         XGetErrorDatabaseText (display, "XlibMessage", 
110                                pchErrorFormatNames[i], 
111                                pchDefaultErrorFormat[i], buffer, BUFSIZ);
112
113         if ((pchErrorFormat[i] = (char *) XtMalloc (1+strlen(buffer))) == NULL)
114         {
115             Warning ("Insufficient memory for error message initialization.");
116             ExitWM (1);
117         }
118
119         strcpy(pchErrorFormat[i], buffer);
120     }
121
122 #endif /* DEBUG */
123
124     XSetErrorHandler (WmXErrorHandler);
125     XSetIOErrorHandler (WmXIOErrorHandler);
126
127     XtSetWarningHandler (WmXtWarningHandler);
128     XtSetErrorHandler (WmXtErrorHandler);
129
130 } /* END OF FUNCTION WmInitErrorHandler */
131
132 \f
133 /*************************************<->*************************************
134  *
135  *  WmXErrorHandler (display, errorEvent)
136  *
137  *
138  *  Description:
139  *  -----------
140  *  This function is the X error handler that is registered with X to
141  *  handle X errors resulting from window management activities.
142  *
143  *
144  *  Inputs:
145  *  ------
146  *  display = display on which X error occurred
147  *
148  *  errorEvent = pointer to a block of information describing the error
149  *
150  * 
151  *  Outputs:
152  *  -------
153  *  wmGD.errorFlag = set to True
154  *
155  *  Return = 0
156  *
157  *************************************<->***********************************/
158
159 int
160 WmXErrorHandler (Display *display, XErrorEvent *errorEvent)
161 {
162     ClientData *pCD;
163
164 #ifdef DEBUG
165     char buffer[BUFSIZ];
166     char message[BUFSIZ];
167
168     XGetErrorText (display, errorEvent->error_code, buffer, BUFSIZ);
169     Warning ("X error occurred during window management operation");
170     fprintf (stderr, "Description = '%s'\n  ", buffer);
171
172     fprintf (stderr, pchErrorFormat[E_MAJOR_CODE], errorEvent->request_code);
173     sprintf(message, "%d", errorEvent->request_code);
174     XGetErrorDatabaseText (display, "XRequest", message, 
175         " ", buffer, BUFSIZ);
176     fprintf (stderr, " (%s)\n  ", buffer);
177     fprintf (stderr, pchErrorFormat[E_MINOR_CODE], errorEvent->minor_code);
178     fprintf (stderr, "\n  ");
179     fprintf (stderr, pchErrorFormat[E_RESOURCE_ID], errorEvent->resourceid);
180     fprintf (stderr, "\n  ");
181     fprintf (stderr, pchErrorFormat[E_ERROR_SERIAL], errorEvent->serial);
182     fprintf (stderr, "\n  ");
183     fprintf (stderr, pchErrorFormat[E_CURRENT_SERIAL], 
184                         LastKnownRequestProcessed(display));
185     fprintf (stderr, "\n");
186 #endif /* DEBUG */
187
188     /*
189      * Check for a BadWindow error for a managed window.  If this error
190      * is detected indicate in the client data that the window no longer
191      * exists.
192      */
193
194     if ((errorEvent->error_code == BadWindow) &&
195         !XFindContext (DISPLAY, errorEvent->resourceid, wmGD.windowContextType,
196              (caddr_t *)&pCD))
197     {
198         if (errorEvent->resourceid == pCD->client)
199         {
200             pCD->clientFlags |= CLIENT_DESTROYED;
201         }
202     }
203
204     wmGD.errorFlag = True;
205     wmGD.errorResource = errorEvent->resourceid;
206     wmGD.errorRequestCode = errorEvent->request_code;
207
208     return (0);
209
210 } /* END OF FUNCTION WmXErrorHandler */
211
212
213 \f
214 /*************************************<->*************************************
215  *
216  *  WmXIOErrorHandler (display)
217  *
218  *
219  *  Description:
220  *  -----------
221  *  This function is the X IO error handler that is registered with X to
222  *  handle X IO errors.  This function exits the window manager.
223  *
224  *
225  *  Inputs:
226  *  ------
227  *  display = X display on which the X IO error occurred
228  * 
229  *************************************<->***********************************/
230
231 int
232 WmXIOErrorHandler (Display *display)
233 {
234   char  err[100];
235  
236   sprintf (err, "%s: %s\n", "I/O error on display:", XDisplayString(display));
237   Warning(err);
238
239   ExitWM (WM_ERROR_EXIT_VALUE);
240
241   /*NOTREACHED*/
242   return 1;
243 } /* END OF FUNCTIONS WmXIOErrorHandler */
244
245
246 \f
247 /*************************************<->*************************************
248  *
249  *  WmXtErrorHandler (message)
250  *
251  *
252  *  Description:
253  *  -----------
254  *  This function is registered as the X Toolkit fatal error handler.
255  *
256  *
257  *  Inputs:
258  *  ------
259  *  message = pointer to an error message
260  *
261  *************************************<->***********************************/
262
263 void
264 WmXtErrorHandler (char *message)
265 {
266
267     Warning (message);
268     ExitWM (WM_ERROR_EXIT_VALUE);
269
270 } /* END OF FUNCTION WmXtErrorHandler */
271
272
273 \f
274 /*************************************<->*************************************
275  *
276  *  WmXtWarningHandler (message)
277  *
278  *
279  *  Description:
280  *  -----------
281  *  This function is registered as an X Toolkit warning handler.
282  *
283  *
284  *  Inputs:
285  *  ------
286  *  message = pointer to a warning message
287  * 
288  *************************************<->***********************************/
289
290 void
291 WmXtWarningHandler (char *message)
292 {
293
294 #ifdef DEBUG
295     Warning (message);
296 #endif /* DEBUG */
297
298 } /* END OF FUNCTIONS WmXtWarningHandler */
299
300 \f
301 /*************************************<->*************************************
302  *
303  *  Warning (message)
304  *
305  *
306  *  Description:
307  *  -----------
308  *  This function lists a message to stderr.
309  *
310  *
311  *  Inputs:
312  *  ------
313  *  message = pointer to a message string
314  * 
315  *************************************<->***********************************/
316
317 void
318 Warning (char *message)
319 {
320     char pch[MAXWMPATH+1];
321
322     sprintf (pch, "%s: %s\n", 
323         GETMESSAGE(20, 1, "Workspace Manager"), message);
324
325     _DtSimpleError (wmGD.mwmName, DtIgnore, NULL, pch, NULL);
326
327 } /* END OF FUNCTION Warning */
328
329 #ifdef DEBUGGER
330 \f
331 /******************************<->*************************************
332  *
333  *  PrintFormatted (format, message, message, ...)
334  *
335  *
336  *  Description:
337  *  -----------
338  *  This function lists several messages to stderr using fprintf()
339  *  formatting capabilities.
340  *
341  *  Inputs:
342  *  ------
343  *  s0-s9 = pointers to message strings
344  * 
345  *  Comments:
346  *  ------
347  *  Caller must provide his/her own argv[0] to this function.
348  ******************************<->***********************************/
349
350 /*VARARGS1*/
351 void
352 PrintFormatted(char *f, char *s0, char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7, char *s8, char *s9)
353 /* limit of ten args */
354 {
355     fprintf( stderr, f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9);
356     fflush (stderr);
357 } /* END OF FUNCTION PrintFormatted */
358
359 /************************    eof   **************************/
360 #endif /* DEBUGGER */