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