annotate src/X_supp.c @ 78:3645e29e4986

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