svn update: 48958 (latest:48959)
[framework/uifw/ecore.git] / src / lib / ecore_x / xlib / ecore_x_error.c
1 /*
2  * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
3  */
4
5 #ifdef HAVE_CONFIG_H
6 # include <config.h>
7 #endif
8
9 #include <stdlib.h>
10
11 #include "Ecore.h"
12 #include "ecore_private.h"
13 #include "ecore_x_private.h"
14 #include "Ecore_X.h"
15
16 static void _ecore_x_error_handle(Display * d, XErrorEvent * ev);
17 static int  _ecore_x_io_error_handle(Display *d);
18
19 static void (*_error_func) (void *data) = NULL;
20 static void *_error_data = NULL;
21 static void (*_io_error_func) (void *data) = NULL;
22 static void *_io_error_data = NULL;
23 static int   _error_request_code = 0;
24 static int   _error_code = 0;
25
26 /**
27  * Set the error handler.
28  * @param func The error handler function
29  * @param data The data to be passed to the handler function
30  * 
31  * Set the X error handler function
32  */
33 EAPI void
34 ecore_x_error_handler_set(void (*func) (void *data), const void *data)
35 {
36    _error_func = func;
37    _error_data = (void *)data;
38 }
39
40 /**
41  * Set the I/O error handler.
42  * @param func The I/O error handler function
43  * @param data The data to be passed to the handler function
44  * 
45  * Set the X I/O error handler function
46  */
47 EAPI void
48 ecore_x_io_error_handler_set(void (*func) (void *data), const void *data)
49 {
50    _io_error_func = func;
51    _io_error_data = (void *)data;
52 }
53
54 /**
55  * Get the request code that caused the error.
56  * @return The request code causing the X error
57  * 
58  * Return the X request code that caused the last X error
59  */
60 EAPI int
61 ecore_x_error_request_get(void)
62 {
63    return _error_request_code;
64 }
65
66 /**
67  * Get the error code from the error.
68  * @return The error code from the X error
69  * 
70  * Return the error code from the last X error
71  */
72 EAPI int
73 ecore_x_error_code_get(void)
74 {
75    return _error_code;
76 }
77
78 void
79 _ecore_x_error_handler_init(void)
80 {
81    XSetErrorHandler((XErrorHandler)_ecore_x_error_handle);
82    XSetIOErrorHandler((XIOErrorHandler)_ecore_x_io_error_handle);   
83 }
84
85 static void
86 _ecore_x_error_handle(Display *d, XErrorEvent *ev)
87 {
88     if (d == _ecore_x_disp)
89      {
90         _error_request_code = ev->request_code;
91         _error_code = ev->error_code;
92         if (_error_func) _error_func(_error_data);
93      }
94 }
95
96 static int
97 _ecore_x_io_error_handle(Display *d)
98 {
99     if (d == _ecore_x_disp)
100      {
101         if (_io_error_func) _io_error_func(_io_error_data);
102         else exit(-1);
103      }
104    return 0;
105 }