77
|
1 #include <stdio.h>
|
|
2 #include <X11/Xlib.h>
|
|
3 #include <X11/Xutil.h>
|
|
4 #include "redraw_man.h"
|
|
5 #include "mb_timer.h"
|
|
6
|
|
7
|
|
8 /*! \brief Dispatch all events in the queue.
|
|
9 */
|
|
10 static void handle_x_event(Display *display,
|
|
11 redraw_man_t *rdman,
|
|
12 mb_tman_t *tman) {
|
|
13 XEvent evt;
|
|
14 XMotionEvent *mevt;
|
|
15 mouse_event_t mouse_event;
|
|
16 shape_t *shape;
|
|
17 subject_t *subject;
|
|
18 ob_factory_t *factory;
|
|
19 co_aix x, y;
|
|
20 int in_stroke;
|
|
21 int but;
|
|
22 int r;
|
|
23
|
|
24 while(XEventsQueued(display, QueuedAfterReading) > 0) {
|
|
25 r = XNextEvent(display, &evt);
|
|
26 if(r == -1)
|
|
27 break;
|
|
28
|
|
29 switch(evt.type) {
|
|
30 case MotionNotify:
|
|
31 mevt = (XMotionEvent *)&evt;
|
|
32 x = mevt->x;
|
|
33 y = mevt->y;
|
|
34 but = 0;
|
|
35 if(mevt->state & Button1Mask)
|
|
36 but |= MOUSE_BUT1;
|
|
37 if(mevt->state & Button2Mask)
|
|
38 but |= MOUSE_BUT2;
|
|
39 if(mevt->state & Button3Mask)
|
|
40 but |= MOUSE_BUT3;
|
|
41
|
|
42 mouse_event.event.type = EVT_MOUSE_MOVE;
|
|
43 mouse_event.x = x;
|
|
44 mouse_event.y = y;
|
|
45 mouse_event.button = but;
|
|
46
|
|
47 shape = find_shape_at_pos(rdman, x, y,
|
|
48 &in_stroke);
|
|
49 subject = sh_get_mouse_event_subject(shape);
|
|
50 factory = rdman_get_ob_factory(rdman);
|
|
51
|
|
52 subject_notify(factory, subject, (event_t *)&mouse_event);
|
|
53 break;
|
|
54
|
|
55 case Expose:
|
|
56 rdman_redraw_area(rdman, evt.xexpose.x, evt.xexpose.y,
|
|
57 evt.xexpose.width, evt.xexpose.height);
|
|
58 break;
|
|
59 }
|
|
60 }
|
|
61 rdman_redraw_changed(rdman);
|
|
62 XFlush(display);
|
|
63 }
|
|
64
|
|
65 /*! \brief Handle connection coming data and timeout of timers.
|
|
66 */
|
|
67 void X_handle_connection(Display *display,
|
|
68 redraw_man_t *rdman,
|
|
69 mb_tman_t *tman) {
|
|
70 int fd;
|
|
71 mb_timeval_t now, tmo;
|
|
72 struct timeval tv;
|
|
73 fd_set rfds;
|
|
74 int nfds;
|
|
75 int r;
|
|
76
|
|
77 fd = XConnectionNumber(display);
|
|
78 nfds = fd + 1;
|
|
79 while(1) {
|
|
80 FD_ZERO(&rfds);
|
|
81 FD_SET(fd, &rfds);
|
|
82
|
|
83 get_now(&now);
|
|
84 r = mb_tman_next_timeout(tman, &now, &tmo);
|
|
85
|
|
86 if(r == 0) {
|
|
87 tv.tv_sec = MB_TIMEVAL_SEC(&tmo);
|
|
88 tv.tv_usec = MB_TIMEVAL_USEC(&tmo);
|
|
89 r = select(nfds, &rfds, NULL, NULL, &tv);
|
|
90 } else
|
|
91 r = select(nfds, &rfds, NULL, NULL, NULL);
|
|
92
|
|
93 if(r == -1) {
|
|
94 perror("select");
|
|
95 break;
|
|
96 }
|
|
97
|
|
98 if(r == 0) {
|
|
99 get_now(&now);
|
|
100 mb_tman_handle_timeout(tman, &now);
|
|
101 rdman_redraw_changed(rdman);
|
|
102 XFlush(display);
|
|
103 } else if(FD_ISSET(fd, &rfds)){
|
|
104 handle_x_event(display, rdman, tman);
|
|
105 }
|
|
106 }
|
|
107 }
|