annotate src/X_supp.c @ 77:a6763f080da5

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