comparison src/X_main.c @ 39:db2aa914e14b

timer for animation
author Thinker K.F. Li <thinker@branda.to>
date Wed, 06 Aug 2008 21:05:11 +0800
parents 943acee7f346
children 400b4b5db0dc
comparison
equal deleted inserted replaced
38:8d219ebd729e 39:db2aa914e14b
1 #include <stdio.h> 1 #include <stdio.h>
2 #include <unistd.h> 2 #include <unistd.h>
3 #include <sys/time.h>
4 #include <sys/select.h>
3 #include <X11/Xlib.h> 5 #include <X11/Xlib.h>
4 #include <X11/Xutil.h> 6 #include <X11/Xutil.h>
5 #include <cairo.h> 7 #include <cairo.h>
6 #include <cairo-xlib.h> 8 #include <cairo-xlib.h>
7 9
8 #include <string.h> 10 #include <string.h>
9 #include "shapes.h" 11 #include "shapes.h"
10 #include "redraw_man.h" 12 #include "redraw_man.h"
11 #include "paint.h" 13 #include "paint.h"
14 #include "mb_timer.h"
15
16 #define OK 0
17 #define ERR -1
12 18
13 Display *display; 19 Display *display;
14 Window win; 20 Window win;
21
22 struct test_motion_data {
23 paint_t *text_stroke;
24 redraw_man_t *rdman;
25 };
26
27 void test_motion(mbsec_t sec, mbusec_t usec,
28 mbsec_t now_sec, mbusec_t now_usec,
29 void *arg) {
30 struct test_motion_data *data = (struct test_motion_data *)arg;
31
32 paint_color_set(data->text_stroke, 1, 0.5, 0.5, 0.5);
33 rdman_paint_changed(data->rdman, data->text_stroke);
34 rdman_redraw_changed(data->rdman);
35 }
15 36
16 void hint_shape(redraw_man_t *rdman, shape_t *shape) { 37 void hint_shape(redraw_man_t *rdman, shape_t *shape) {
17 static shape_t *last_shape = NULL; 38 static shape_t *last_shape = NULL;
18 if(last_shape != shape) { 39 if(last_shape != shape) {
19 if(last_shape != NULL && last_shape->stroke != NULL) { 40 if(last_shape != NULL && last_shape->stroke != NULL) {
36 int r; 57 int r;
37 co_aix x, y; 58 co_aix x, y;
38 shape_t *shape = NULL; 59 shape_t *shape = NULL;
39 int in_stroke; 60 int in_stroke;
40 61
41 XSelectInput(display, win, PointerMotionMask | ExposureMask); 62 while(XEventsQueued(display, QueuedAfterReading) > 0) {
42 while((r = XNextEvent(display, &evt)) == 0) { 63 r = XNextEvent(display, &evt);
64 if(r == -1)
65 break;
66
43 switch(evt.type) { 67 switch(evt.type) {
44 case MotionNotify: 68 case MotionNotify:
45 mevt = (XMotionEvent *)&evt; 69 mevt = (XMotionEvent *)&evt;
46 x = mevt->x; 70 x = mevt->x;
47 y = mevt->y; 71 y = mevt->y;
53 /* rdman_redraw_area(rdman, evt.xexpose.x, evt.xexpose.y, 77 /* rdman_redraw_area(rdman, evt.xexpose.x, evt.xexpose.y,
54 evt.xexpose.width, evt.xexpose.height); */ 78 evt.xexpose.width, evt.xexpose.height); */
55 break; 79 break;
56 } 80 }
57 } 81 }
82 XFlush(display);
83 }
84
85 void handle_connection(Display *display, mb_tman_t *tman,
86 redraw_man_t *rdman, int w, int h) {
87 int xcon;
88 fd_set rds;
89 int nfds;
90 struct timeval tmo;
91 int r;
92
93 XSelectInput(display, win, PointerMotionMask | ExposureMask);
94 XFlush(display);
95
96 xcon = XConnectionNumber(display);
97 nfds = xcon + 1;
98 while(1) {
99 FD_ZERO(&rds);
100 FD_SET(xcon, &rds);
101 r = gettimeofday(&tmo, NULL);
102 if(r == -1) {
103 perror("gettimeofday");
104 return;
105 }
106
107 r = mb_tman_next_timeout(tman,
108 tmo.tv_sec, tmo.tv_usec,
109 (mbsec_t *)&tmo.tv_sec,
110 (mbusec_t *)&tmo.tv_usec);
111 if(r != OK)
112 r = select(nfds, &rds, NULL, NULL, NULL);
113 else
114 r = select(nfds, &rds, NULL, NULL, &tmo);
115
116 if(r == -1) {
117 perror("select");
118 return;
119 }
120 if(r == 0) {
121 r = gettimeofday(&tmo, NULL);
122 if(r == -1) {
123 perror("gettimeofday");
124 return;
125 }
126 mb_tman_handle_timeout(tman, tmo.tv_sec, tmo.tv_usec);
127 XFlush(display);
128 } else if(FD_ISSET(xcon, &rds)) {
129 event_interaction(display, rdman, w, h);
130 }
131 }
58 } 132 }
59 133
60 void draw_path(cairo_t *cr, int w, int h) { 134 void draw_path(cairo_t *cr, int w, int h) {
61 cairo_t *tmpcr; 135 cairo_t *tmpcr;
62 cairo_surface_t *tmpsuf; 136 cairo_surface_t *tmpsuf;
66 paint_t *fill1, *fill2, *fill3; 140 paint_t *fill1, *fill2, *fill3;
67 paint_t *stroke, *text_stroke; 141 paint_t *stroke, *text_stroke;
68 shape_t *text; 142 shape_t *text;
69 grad_stop_t fill3_stops[3]; 143 grad_stop_t fill3_stops[3];
70 cairo_font_face_t *face; 144 cairo_font_face_t *face;
145 struct test_motion_data mdata;
146 struct timeval tv;
147 mb_tman_t *tman;
71 int i; 148 int i;
72 149
73 tmpsuf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h); 150 tmpsuf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
74 tmpcr = cairo_create(tmpsuf); 151 tmpcr = cairo_create(tmpsuf);
75 152
211 rdman_coord_changed(&rdman, coord3); 288 rdman_coord_changed(&rdman, coord3);
212 rdman_redraw_changed(&rdman); 289 rdman_redraw_changed(&rdman);
213 XFlush(display); 290 XFlush(display);
214 } 291 }
215 292
216 event_interaction(display, &rdman, w, h); 293 tman = mb_tman_new();
294 if(tman) {
295 mdata.text_stroke = text_stroke;
296 mdata.rdman = &rdman;
297 gettimeofday(&tv, NULL);
298 tv.tv_sec += 3;
299 mb_tman_timeout(tman, tv.tv_sec, tv.tv_usec,
300 test_motion, &mdata);
301
302 handle_connection(display, tman, &rdman, w, h);
303 mb_tman_free(tman);
304 }
217 305
218 fill1->free(fill1); 306 fill1->free(fill1);
219 fill2->free(fill2); 307 fill2->free(fill2);
220 stroke->free(stroke); 308 stroke->free(stroke);
221 text_stroke->free(text_stroke); 309 text_stroke->free(text_stroke);