Mercurial > MadButterfly
comparison src/X_supp.c @ 986:c39d14139ca5 refine_backend_if
Implement IO manager interface for X
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Mon, 22 Nov 2010 00:42:29 +0800 |
parents | 3fe8054457a8 |
children | bf0da8c7d03f |
comparison
equal
deleted
inserted
replaced
985:bab9c0f836b9 | 986:c39d14139ca5 |
---|---|
8 #include <cairo-xlib.h> | 8 #include <cairo-xlib.h> |
9 #include "mb_graph_engine.h" | 9 #include "mb_graph_engine.h" |
10 #include "mb_redraw_man.h" | 10 #include "mb_redraw_man.h" |
11 #include "mb_timer.h" | 11 #include "mb_timer.h" |
12 #include "mb_X_supp.h" | 12 #include "mb_X_supp.h" |
13 #include "mb_backend.h" | |
13 #include "config.h" | 14 #include "config.h" |
14 | 15 |
15 #ifdef XSHM | 16 #ifdef XSHM |
16 /* \sa http://www.xfree86.org/current/mit-shm.html */ | 17 /* \sa http://www.xfree86.org/current/mit-shm.html */ |
17 #include <sys/ipc.h> | 18 #include <sys/ipc.h> |
34 subject_t *kbevents; | 35 subject_t *kbevents; |
35 ob_factory_t *ob_factory; | 36 ob_factory_t *ob_factory; |
36 }; | 37 }; |
37 | 38 |
38 /* @} */ | 39 /* @} */ |
39 #define MAX_MONITORS 200 | |
40 typedef struct { | |
41 int type; | |
42 int fd; | |
43 mb_eventcb_t f; | |
44 void *arg; | |
45 } monitor_t; | |
46 | 40 |
47 struct _X_MB_runtime { | 41 struct _X_MB_runtime { |
48 Display *display; | 42 Display *display; |
49 Window win; | 43 Window win; |
50 Visual *visual; | 44 Visual *visual; |
55 mb_tman_t *tman; | 49 mb_tman_t *tman; |
56 mb_img_ldr_t *img_ldr; | 50 mb_img_ldr_t *img_ldr; |
57 int w, h; | 51 int w, h; |
58 | 52 |
59 X_kb_info_t kbinfo; | 53 X_kb_info_t kbinfo; |
60 monitor_t monitors[MAX_MONITORS]; | 54 mb_IO_man_t *io_man; |
61 int n_monitor; | |
62 | 55 |
63 #ifndef ONLY_MOUSE_MOVE_RAW | 56 #ifndef ONLY_MOUSE_MOVE_RAW |
64 /* States */ | 57 /* States */ |
65 shape_t *last; | 58 shape_t *last; |
66 #endif | 59 #endif |
79 int mflag; | 72 int mflag; |
80 int mx, my; /* Position of last motion event */ | 73 int mx, my; /* Position of last motion event */ |
81 int mbut_state; /* Button state of last motion event */ | 74 int mbut_state; /* Button state of last motion event */ |
82 }; | 75 }; |
83 | 76 |
77 /*! \defgroup x_mb_io IO manager for X. | |
78 * @{ | |
79 */ | |
80 #define MAX_MONITORS 200 | |
81 | |
82 typedef struct { | |
83 int type; | |
84 int fd; | |
85 mb_IO_cb_t cb; | |
86 void *data; | |
87 } monitor_t; | |
88 | |
89 struct _X_MB_IO_man { | |
90 mb_IO_man_t io_man; | |
91 monitor_t monitors[MAX_MONITORS]; | |
92 int n_monitor; | |
93 }; | |
94 | |
95 int _x_mb_io_man_reg(struct _mb_IO_man *io_man, | |
96 int fd, MB_IO_TYPE type, mb_IO_cb_t cb, void *data); | |
97 void _x_mb_io_man_unreg(struct _mb_IO_Man *io_man, int io_hdl); | |
98 mb_IO_man_t *_x_mb_io_man_new(void); | |
99 void _x_mb_io_man_free(mb_IO_man_t *io_man); | |
100 | |
101 static mb_IO_factory_t _X_supp_default_io_factory = { | |
102 _x_mb_io_man_new, | |
103 _x_mb_io_man_free | |
104 }; | |
105 static mb_IO_factory_t *_io_factory = _X_supp_default_io_factory; | |
106 | |
107 static struct _X_MB_IO_man _default_io_man = { | |
108 {_x_mb_io_man_reg, _x_mb_io_man_unreg}, | |
109 , /* monitors */ | |
110 0 /* n_monitor */ | |
111 }; | |
112 | |
113 static mb_IO_man_t * | |
114 _x_mb_io_man_new(void) { | |
115 return (mb_IO_man_t *)&_default_io_man; | |
116 } | |
117 | |
118 static void | |
119 _x_mb_io_man_free(mb_IO_man_t *io_man) { | |
120 } | |
121 | |
122 static int | |
123 _x_mb_io_man_reg(struct _mb_IO_man *io_man, | |
124 int fd, MB_IO_TYPE type, mb_IO_cb_t cb, void *data) { | |
125 struct _x_mb_io_man *xmb_io_man = (struct _x_mb_io_man *)io_man; | |
126 int i; | |
127 | |
128 for(i = 0; i < xmb_io_man->n_monitor; i++) { | |
129 if (xmb_io_man->monitors[i].type == MB_IO_DUMMY) | |
130 break; | |
131 } | |
132 if (i == MAX_MONITORS) | |
133 return ERR; | |
134 | |
135 xmb_io_man->monitors[i].type = type; | |
136 xmb_io_man->monitors[i].fd = fd; | |
137 xmb_io_man->monitors[i].cb = cb; | |
138 xmb_io_man->monitors[i].data = data; | |
139 i++; | |
140 if(i > xmb_io_man->n_monitor) | |
141 xmb_io_man->n_monitor = i; | |
142 return i - 1; | |
143 } | |
144 | |
145 static void | |
146 _x_mb_io_man_unreg(struct _mb_IO_man *io_man, int io_hdl) { | |
147 struct _x_mb_io_man *xmb_io_man = (struct _x_mb_io_man *)io_man; | |
148 | |
149 ASSERT(io_hdl < xmb_io_man->n_monitor); | |
150 xmb_io_man->monitors[io_hdl].type = MB_IO_DUMMY; | |
151 } | |
152 | |
153 /*! \brief Handle connection coming data and timeout of timers. | |
154 * | |
155 * \param display is a Display returned by XOpenDisplay(). | |
156 * \param rdman is a redraw manager. | |
157 * \param tman is a timer manager. | |
158 * | |
159 * The display is managed by specified rdman and tman. rdman draws | |
160 * on the display, and tman trigger actions according timers. | |
161 */ | |
162 static void | |
163 _x_mb_handle_connection(struct _mb_IO_man *io_man) { | |
164 X_MB_runtime_t *rt = (X_MB_runtime_t *) be; | |
165 mb_tman_t *tman = rt->tman; | |
166 int fd; | |
167 mb_timeval_t now, tmo; | |
168 struct timeval tv; | |
169 fd_set rfds, wfds; | |
170 int nfds = 0; | |
171 int r, r1,i; | |
172 | |
173 handle_x_event(rt); | |
174 | |
175 while(1) { | |
176 FD_ZERO(&rfds); | |
177 FD_ZERO(&wfds); | |
178 for(i = 0; i < io_man->n_monitor; i++) { | |
179 if(io_man->monitors[i].type == MB_IO_R || | |
180 io_man->monitors[i].type == MB_IO_RW) { | |
181 FD_SET(io_man->monitors[i].fd, &rfds); | |
182 if(nfds <= io_man->monitors[i].fd) | |
183 nfds = io_man->monitors[i].fd + 1; | |
184 } | |
185 if(io_man->monitors[i].type == MB_IO_W || | |
186 io_man->monitors[i].type == MB_IO_RW) { | |
187 FD_SET(io_man->monitors[i].fd, &wfds); | |
188 if(nfds <= io_man->monitors[i].fd) | |
189 nfds = io_man->monitors[i].fd + 1; | |
190 } | |
191 } | |
192 | |
193 get_now(&now); | |
194 r = mb_tman_next_timeout(tman, &now, &tmo); | |
195 | |
196 if(r == 0) { | |
197 tv.tv_sec = MB_TIMEVAL_SEC(&tmo); | |
198 tv.tv_usec = MB_TIMEVAL_USEC(&tmo); | |
199 r1 = select(nfds, &rfds, NULL, NULL, &tv); | |
200 } else | |
201 r1 = select(nfds, &rfds, NULL, NULL, NULL); | |
202 | |
203 if(r1 == -1) { | |
204 perror("select"); | |
205 break; | |
206 } | |
207 | |
208 if(r1 == 0) { | |
209 get_now(&now); | |
210 mb_tman_handle_timeout(tman, &now); | |
211 } else { | |
212 for(i = 0; i < io_man->n_monitor; i++) { | |
213 if(io_man->monitors[i].type == MB_IO_R || | |
214 io_man->monitors[i].type == MB_IO_RW) { | |
215 if(FD_ISSET(io_man->monitors[i].fd, &rfds)) | |
216 ioman->monitors[i].cb(io_man->monitors[i].fd, | |
217 MB_IO_R, | |
218 rt->monitors[i].data); | |
219 } | |
220 if(io_man->monitors[i].type == MB_IO_W || | |
221 io_man->monitors[i].type == MB_IO_RW) { | |
222 if(FD_ISSET(io_man->monitors[i].fd, &wfds)) | |
223 io_man->monitors[i].cb(io_man->monitors[i].fd, | |
224 MB_IO_W, | |
225 io_man->monitors[i].data); | |
226 } | |
227 } | |
228 } | |
229 } | |
230 } | |
231 | |
232 /* @} */ | |
233 | |
84 #ifdef XSHM | 234 #ifdef XSHM |
85 static void | 235 static void |
86 XSHM_update(X_MB_runtime_t *xmb_rt) { | 236 XSHM_update(X_MB_runtime_t *xmb_rt) { |
87 GC gc; | 237 GC gc; |
88 | 238 |
165 event.keycode = code; | 315 event.keycode = code; |
166 event.sym = sym; | 316 event.sym = sym; |
167 | 317 |
168 subject_notify(kbinfo->kbevents, &event.event); | 318 subject_notify(kbinfo->kbevents, &event.event); |
169 } | 319 } |
170 | |
171 /* @} */ | 320 /* @} */ |
172 | 321 |
173 static unsigned int get_button_state(unsigned int state) { | 322 static unsigned int get_button_state(unsigned int state) { |
174 unsigned int but = 0; | 323 unsigned int but = 0; |
175 | 324 |
496 } | 645 } |
497 } | 646 } |
498 } | 647 } |
499 } | 648 } |
500 | 649 |
501 #define ERR -1 | |
502 #define OK 0 | |
503 | |
504 static int X_init_connection(const char *display_name, | 650 static int X_init_connection(const char *display_name, |
505 int w, int h, | 651 int w, int h, |
506 Display **displayp, | 652 Display **displayp, |
507 Visual **visualp, | 653 Visual **visualp, |
508 Window *winp) { | 654 Window *winp) { |
952 } | 1098 } |
953 } | 1099 } |
954 } | 1100 } |
955 mb_backend_t backend = { X_MB_new, | 1101 mb_backend_t backend = { X_MB_new, |
956 X_MB_new_with_window, | 1102 X_MB_new_with_window, |
1103 | |
957 X_MB_free, | 1104 X_MB_free, |
958 X_MB_add_event, | 1105 X_MB_add_event, |
959 X_MB_remove_event, | 1106 X_MB_remove_event, |
960 X_MB_handle_connection, | 1107 X_MB_handle_connection, |
1108 | |
961 X_MB_kbevents, | 1109 X_MB_kbevents, |
962 X_MB_rdman, | 1110 X_MB_rdman, |
963 X_MB_tman, | 1111 X_MB_tman, |
964 X_MB_ob_factory, | 1112 X_MB_ob_factory, |
965 X_MB_img_ldr | 1113 X_MB_img_ldr |