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