77
|
1 #include <stdio.h>
|
78
|
2 #include <stdlib.h>
|
|
3 #include <string.h>
|
77
|
4 #include <X11/Xlib.h>
|
|
5 #include <X11/Xutil.h>
|
78
|
6 #include <cairo.h>
|
|
7 #include <cairo-xlib.h>
|
77
|
8 #include "redraw_man.h"
|
|
9 #include "mb_timer.h"
|
78
|
10 #include "X_supp.h"
|
77
|
11
|
|
12
|
78
|
13 static unsigned int get_button_state(unsigned int state) {
|
|
14 unsigned int but = 0;
|
|
15
|
|
16 if(state & Button1Mask)
|
|
17 but |= MOUSE_BUT1;
|
|
18 if(state & Button2Mask)
|
|
19 but |= MOUSE_BUT2;
|
|
20 if(state & Button3Mask)
|
|
21 but |= MOUSE_BUT3;
|
|
22
|
|
23 return but;
|
|
24 }
|
|
25
|
|
26 static unsigned int get_button(unsigned int button) {
|
|
27 switch(button) {
|
|
28 case Button1:
|
|
29 return MOUSE_BUT1;
|
|
30 case Button2:
|
|
31 return MOUSE_BUT2;
|
|
32 case Button3:
|
|
33 return MOUSE_BUT3;
|
|
34 }
|
|
35 return 0;
|
|
36 }
|
|
37
|
|
38 /*! \brief Notify observers of the shape at specified
|
|
39 * position for mouse event.
|
|
40 *
|
|
41 * Observers of parent shapes may be called if the subject is not
|
|
42 * with SUBF_STOP_PROPAGATE flag. The subject of mouse event
|
|
43 * for a shape is returned by sh_get_mouse_event_subject().
|
|
44 */
|
|
45 static void notify_shapes(redraw_man_t *rdman,
|
|
46 co_aix x, co_aix y, int etype,
|
|
47 unsigned int state,
|
|
48 unsigned int button) {
|
|
49 mouse_event_t mouse_event;
|
|
50 shape_t *shape;
|
|
51 subject_t *subject;
|
|
52 ob_factory_t *factory;
|
|
53 int in_stroke;
|
|
54
|
|
55 mouse_event.event.type = etype;
|
|
56 mouse_event.x = x;
|
|
57 mouse_event.y = y;
|
|
58 mouse_event.but_state = state;
|
|
59 mouse_event.button = button;
|
|
60
|
|
61 shape = find_shape_at_pos(rdman, x, y,
|
|
62 &in_stroke);
|
|
63 if(shape == NULL)
|
|
64 return;
|
|
65 subject = sh_get_mouse_event_subject(shape);
|
|
66 factory = rdman_get_ob_factory(rdman);
|
|
67
|
|
68 subject_notify(factory, subject, (event_t *)&mouse_event);
|
|
69 }
|
|
70
|
|
71 /*! \brief Dispatch all X events in the queue.
|
77
|
72 */
|
|
73 static void handle_x_event(Display *display,
|
|
74 redraw_man_t *rdman,
|
|
75 mb_tman_t *tman) {
|
|
76 XEvent evt;
|
|
77 XMotionEvent *mevt;
|
78
|
78 XButtonEvent *bevt;
|
81
|
79 XExposeEvent *eevt;
|
|
80 co_aix x, y, w, h;
|
|
81
|
|
82 int eflag = 0;
|
82
|
83 int ex1=0, ey1=0, ex2=0, ey2=0;
|
81
|
84
|
78
|
85 unsigned int state, button;
|
77
|
86 int r;
|
|
87
|
|
88 while(XEventsQueued(display, QueuedAfterReading) > 0) {
|
|
89 r = XNextEvent(display, &evt);
|
|
90 if(r == -1)
|
|
91 break;
|
|
92
|
|
93 switch(evt.type) {
|
78
|
94 case ButtonPress:
|
|
95 bevt = (XButtonEvent *)&evt;
|
|
96 x = bevt->x;
|
|
97 y = bevt->y;
|
|
98 state = get_button_state(bevt->state);
|
|
99 button = get_button(bevt->button);
|
|
100
|
|
101 notify_shapes(rdman, x, y, EVT_MOUSE_BUT_PRESS,
|
|
102 state, button);
|
|
103 break;
|
|
104
|
|
105 case ButtonRelease:
|
|
106 bevt = (XButtonEvent *)&evt;
|
|
107 x = bevt->x;
|
|
108 y = bevt->y;
|
|
109 state = get_button_state(bevt->state);
|
|
110 button = get_button(bevt->button);
|
|
111
|
|
112 notify_shapes(rdman, x, y, EVT_MOUSE_BUT_RELEASE,
|
|
113 state, button);
|
|
114 break;
|
|
115
|
77
|
116 case MotionNotify:
|
|
117 mevt = (XMotionEvent *)&evt;
|
|
118 x = mevt->x;
|
|
119 y = mevt->y;
|
78
|
120 state = get_button_state(mevt->state);
|
77
|
121
|
78
|
122 notify_shapes(rdman, x, y, EVT_MOUSE_MOVE, state, 0);
|
77
|
123 break;
|
|
124
|
|
125 case Expose:
|
81
|
126 eevt = &evt.xexpose;
|
|
127 x = eevt->x;
|
|
128 y = eevt->y;
|
|
129 w = eevt->width;
|
|
130 h = eevt->height;
|
|
131
|
|
132 if(eflag) {
|
|
133 if(x < ex1)
|
|
134 ex1 = x;
|
|
135 if(y < ey1)
|
|
136 ey1 = y;
|
|
137 if((x + w) > ex2)
|
|
138 ex2 = x + w;
|
|
139 if((y + h) > ey2)
|
|
140 ey2 = y + h;
|
|
141 } else {
|
|
142 ex1 = x;
|
|
143 ey1 = y;
|
|
144 ex2 = x + w;
|
|
145 ey2 = y + h;
|
|
146 eflag = 1;
|
|
147 }
|
77
|
148 break;
|
|
149 }
|
|
150 }
|
81
|
151 if(eflag) {
|
|
152 rdman_redraw_area(rdman, ex1, ey1, (ex2 - ex1), (ey2 - ey1));
|
|
153 eflag = 0;
|
|
154 }
|
77
|
155 XFlush(display);
|
|
156 }
|
|
157
|
|
158 /*! \brief Handle connection coming data and timeout of timers.
|
78
|
159 *
|
|
160 * \param display is a Display returned by XOpenDisplay().
|
|
161 * \param rdman is a redraw manager.
|
|
162 * \param tman is a timer manager.
|
|
163 *
|
|
164 * The display is managed by specified rdman and tman. rdman draws
|
|
165 * on the display, and tman trigger actions according timers.
|
77
|
166 */
|
78
|
167 void X_MB_handle_connection(Display *display,
|
|
168 redraw_man_t *rdman,
|
|
169 mb_tman_t *tman) {
|
77
|
170 int fd;
|
|
171 mb_timeval_t now, tmo;
|
|
172 struct timeval tv;
|
|
173 fd_set rfds;
|
|
174 int nfds;
|
|
175 int r;
|
|
176
|
78
|
177 rdman_redraw_all(rdman);
|
|
178 XFlush(display);
|
|
179
|
77
|
180 fd = XConnectionNumber(display);
|
|
181 nfds = fd + 1;
|
|
182 while(1) {
|
|
183 FD_ZERO(&rfds);
|
|
184 FD_SET(fd, &rfds);
|
|
185
|
|
186 get_now(&now);
|
|
187 r = mb_tman_next_timeout(tman, &now, &tmo);
|
|
188
|
|
189 if(r == 0) {
|
|
190 tv.tv_sec = MB_TIMEVAL_SEC(&tmo);
|
|
191 tv.tv_usec = MB_TIMEVAL_USEC(&tmo);
|
|
192 r = select(nfds, &rfds, NULL, NULL, &tv);
|
|
193 } else
|
|
194 r = select(nfds, &rfds, NULL, NULL, NULL);
|
|
195
|
|
196 if(r == -1) {
|
|
197 perror("select");
|
|
198 break;
|
|
199 }
|
|
200
|
|
201 if(r == 0) {
|
|
202 get_now(&now);
|
|
203 mb_tman_handle_timeout(tman, &now);
|
|
204 rdman_redraw_changed(rdman);
|
|
205 XFlush(display);
|
|
206 } else if(FD_ISSET(fd, &rfds)){
|
|
207 handle_x_event(display, rdman, tman);
|
|
208 }
|
|
209 }
|
|
210 }
|
78
|
211
|
|
212 #define ERR -1
|
|
213 #define OK 0
|
|
214
|
|
215 static int X_init_connection(const char *display_name,
|
|
216 int w, int h,
|
|
217 Display **displayp,
|
|
218 Visual **visualp,
|
|
219 Window *winp) {
|
|
220 Display *display;
|
|
221 Window root, win;
|
|
222 Visual *visual;
|
|
223 int screen;
|
|
224 XSetWindowAttributes wattr;
|
|
225 int depth;
|
|
226 int x, y;
|
|
227 int r;
|
|
228
|
|
229 display = XOpenDisplay(display_name);
|
|
230 if(display == NULL)
|
|
231 return ERR;
|
|
232
|
|
233 screen = DefaultScreen(display);
|
|
234 root = DefaultRootWindow(display);
|
|
235 visual = DefaultVisual(display, screen);
|
|
236 depth = DefaultDepth(display, screen);
|
|
237 wattr.override_redirect = False;
|
|
238 x = 10;
|
|
239 y = 10;
|
|
240 win = XCreateWindow(display, root,
|
|
241 x, y,
|
|
242 w, h,
|
|
243 1, depth, InputOutput, visual,
|
|
244 CWOverrideRedirect, &wattr);
|
|
245 r = XMapWindow(display, win);
|
|
246 if(r == -1) {
|
|
247 XCloseDisplay(display);
|
|
248 return ERR;
|
|
249 }
|
|
250
|
|
251 XSelectInput(display, win, PointerMotionMask | ExposureMask);
|
|
252 XFlush(display);
|
|
253
|
|
254 *displayp = display;
|
|
255 *visualp = visual;
|
|
256 *winp = win;
|
|
257
|
|
258 return OK;
|
|
259 }
|
|
260
|
|
261 /*! \brief Initialize a MadButterfy runtime for Xlib.
|
|
262 *
|
|
263 * It setups a runtime environment to run MadButterfly with Xlib.
|
|
264 * Users should specify width and height of the opening window.
|
|
265 */
|
|
266 int X_MB_init(const char *display_name,
|
|
267 int w, int h, X_MB_runtime_t *xmb_rt) {
|
|
268 memset(xmb_rt, 0, sizeof(X_MB_runtime_t));
|
|
269
|
|
270 xmb_rt->w = w;
|
|
271 xmb_rt->h = h;
|
|
272 X_init_connection(display_name, w, h, &xmb_rt->display,
|
|
273 &xmb_rt->visual, &xmb_rt->win);
|
|
274
|
|
275 xmb_rt->surface =
|
|
276 cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
|
|
277
|
|
278 xmb_rt->backend_surface =
|
|
279 cairo_xlib_surface_create(xmb_rt->display,
|
|
280 xmb_rt->win,
|
|
281 xmb_rt->visual,
|
|
282 w, h);
|
|
283
|
|
284 xmb_rt->cr = cairo_create(xmb_rt->surface);
|
|
285 xmb_rt->backend_cr = cairo_create(xmb_rt->backend_surface);
|
|
286
|
|
287 cairo_set_source_surface(xmb_rt->backend_cr, xmb_rt->surface, 0, 0);
|
|
288
|
|
289 xmb_rt->rdman = (redraw_man_t *)malloc(sizeof(redraw_man_t));
|
|
290 redraw_man_init(xmb_rt->rdman, xmb_rt->cr, xmb_rt->backend_cr);
|
|
291
|
|
292 xmb_rt->tman = mb_tman_new();
|
|
293
|
|
294 return OK;
|
|
295 }
|
|
296
|
|
297 void X_MB_destroy(X_MB_runtime_t *xmb_rt) {
|
|
298 if(xmb_rt->rdman) {
|
|
299 redraw_man_destroy(xmb_rt->rdman);
|
|
300 free(xmb_rt->rdman);
|
|
301 }
|
|
302
|
|
303 if(xmb_rt->tman)
|
|
304 mb_tman_free(xmb_rt->tman);
|
|
305
|
|
306 if(xmb_rt->cr)
|
|
307 cairo_destroy(xmb_rt->cr);
|
|
308 if(xmb_rt->backend_cr)
|
|
309 cairo_destroy(xmb_rt->backend_cr);
|
|
310
|
|
311 if(xmb_rt->surface)
|
|
312 cairo_surface_destroy(xmb_rt->surface);
|
|
313 if(xmb_rt->backend_surface)
|
|
314 cairo_surface_destroy(xmb_rt->backend_surface);
|
|
315
|
|
316 if(xmb_rt->display)
|
|
317 XCloseDisplay(xmb_rt->display);
|
|
318 }
|
|
319
|