00001 #include <stdio.h>
00002 #include <unistd.h>
00003 #include <sys/time.h>
00004 #include <sys/select.h>
00005 #include <X11/Xlib.h>
00006 #include <X11/Xutil.h>
00007 #include <cairo.h>
00008 #include <cairo-xlib.h>
00009
00010 #include <string.h>
00011 #include "mb_shapes.h"
00012 #include "mb_redraw_man.h"
00013 #include "mb_paint.h"
00014 #include "mb_timer.h"
00015 #include "mb_animate.h"
00016
00017 #define OK 0
00018 #define ERR -1
00019
00020 Display *display;
00021 Window win;
00022
00023 void hint_shape(redraw_man_t *rdman, shape_t *shape) {
00024 static shape_t *last_shape = NULL;
00025 if(last_shape != shape) {
00026 if(last_shape != NULL && last_shape->stroke != NULL) {
00027 last_shape->stroke_width -= 2;
00028 rdman_shape_changed(rdman, last_shape);
00029 }
00030 if(shape != NULL && shape->stroke != NULL) {
00031 shape->stroke_width += 2;
00032 rdman_shape_changed(rdman, shape);
00033 }
00034 }
00035 last_shape = shape;
00036 }
00037
00038 void event_interaction(Display *display,
00039 redraw_man_t *rdman, int w, int h) {
00040 XEvent evt;
00041 XMotionEvent *mevt;
00042 int r;
00043 co_aix x, y;
00044 shape_t *shape = NULL;
00045 int in_stroke;
00046
00047 while(XEventsQueued(display, QueuedAfterReading) > 0) {
00048 r = XNextEvent(display, &evt);
00049 if(r == -1)
00050 break;
00051
00052 switch(evt.type) {
00053 case MotionNotify:
00054 mevt = (XMotionEvent *)&evt;
00055 x = mevt->x;
00056 y = mevt->y;
00057 shape = find_shape_at_pos(rdman, x, y, &in_stroke);
00058 hint_shape(rdman, shape);
00059 break;
00060 case Expose:
00061 rdman_redraw_area(rdman, evt.xexpose.x, evt.xexpose.y,
00062 evt.xexpose.width, evt.xexpose.height);
00063 break;
00064 }
00065 }
00066 rdman_redraw_changed(rdman);
00067 XFlush(display);
00068 }
00069
00070 void handle_connection(Display *display, mb_tman_t *tman,
00071 redraw_man_t *rdman, int w, int h) {
00072 int xcon;
00073 fd_set rds;
00074 int nfds;
00075 struct timeval tmo;
00076 mb_timeval_t mb_tmo, next_mb_tmo;
00077 int r;
00078
00079 XSelectInput(display, win, PointerMotionMask | ExposureMask);
00080 XFlush(display);
00081
00082 xcon = XConnectionNumber(display);
00083 nfds = xcon + 1;
00084
00085 r = gettimeofday(&tmo, NULL);
00086 if(r == -1) {
00087 perror("gettimeofday");
00088 return;
00089 }
00090 MB_TIMEVAL_SET(&mb_tmo, tmo.tv_sec, tmo.tv_usec);
00091
00092 while(1) {
00093 FD_ZERO(&rds);
00094 FD_SET(xcon, &rds);
00095
00096 r = mb_tman_next_timeout(tman, &mb_tmo, &next_mb_tmo);
00097 if(r != OK)
00098 r = select(nfds, &rds, NULL, NULL, NULL);
00099 else {
00100 tmo.tv_sec = MB_TIMEVAL_SEC(&next_mb_tmo);
00101 tmo.tv_usec = MB_TIMEVAL_USEC(&next_mb_tmo);
00102 r = select(nfds, &rds, NULL, NULL, &tmo);
00103 }
00104
00105 if(r == -1) {
00106 perror("select");
00107 return;
00108 }
00109
00110 if(r == 0) {
00111 MB_TIMEVAL_ADD(&mb_tmo, &next_mb_tmo);
00112
00113 mb_tman_handle_timeout(tman, &mb_tmo);
00114 rdman_redraw_changed(rdman);
00115 XFlush(display);
00116 } else if(FD_ISSET(xcon, &rds)) {
00117 event_interaction(display, rdman, w, h);
00118 }
00119 }
00120 }
00121
00122 void draw_path(cairo_t *cr, int w, int h) {
00123 cairo_t *tmpcr;
00124 cairo_surface_t *tmpsuf;
00125 redraw_man_t rdman;
00126 shape_t *path1, *path2, *rect;
00127 coord_t *coord1, *coord2, *coord3;
00128 paint_t *fill1, *fill2, *fill3;
00129 paint_t *text_fill;
00130 grad_stop_t text_stops[2];
00131 paint_t *stroke, *text_stroke;
00132 shape_t *text;
00133 grad_stop_t fill3_stops[3];
00134 cairo_font_face_t *face;
00135 struct timeval tv;
00136 mb_tman_t *tman;
00137 mb_timeval_t mbtv, start, playing;
00138 mb_progm_t *progm;
00139 mb_word_t *word;
00140 mb_action_t *act;
00141
00142 tmpsuf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
00143 tmpcr = cairo_create(tmpsuf);
00144
00145 cairo_set_source_surface(cr, tmpsuf, 0, 0);
00146 redraw_man_init(&rdman, tmpcr, cr);
00147 coord1 = rdman_coord_new(&rdman, rdman.root_coord);
00148 coord2 = rdman_coord_new(&rdman, rdman.root_coord);
00149 coord3 = rdman_coord_new(&rdman, rdman.root_coord);
00150
00151 fill1 = rdman_paint_color_new(&rdman, 1, 1, 0, 0.5);
00152 fill2 = rdman_paint_color_new(&rdman, 0, 1, 1, 0.5);
00153 stroke = rdman_paint_color_new(&rdman, 0.4, 0.4, 0.4, 1);
00154 text_stroke = rdman_paint_color_new(&rdman, 0.5, 0.5, 0.5, 1);
00155
00156 face = cairo_get_font_face(tmpcr);
00157 text = rdman_shape_text_new(&rdman, "hello \xe6\xbc\xa2\xe5\xad\x97",
00158 10, h / 4, 36.0, face);
00159 text_fill = rdman_paint_radial_new(&rdman, 100, h / 4, 70);
00160 grad_stop_init(text_stops, 0, 0.2, 0.9, 0.2, 1);
00161 grad_stop_init(text_stops + 1, 1, 0.9, 0.2, 0.2, 0.1);
00162 paint_radial_stops(text_fill, 2, text_stops);
00163 rdman_paint_stroke(&rdman, text_stroke, text);
00164 text->stroke_width = 0.5;
00165 rdman_paint_fill(&rdman, text_fill, text);
00166 rdman_add_shape(&rdman, text, coord3);
00167
00168 path1 = rdman_shape_path_new(&rdman, "M 22,89.36218 C -34,-0.63782 39,-9.637817 82,12.36218 C 125,34.36218 142,136.36218 142,136.36218 C 100.66667,125.36218 74.26756,123.42795 22,89.36218 z ");
00169 rdman_paint_fill(&rdman, fill1, path1);
00170 rdman_paint_stroke(&rdman, stroke, path1);
00171 coord1->matrix[0] = 0.8;
00172 coord1->matrix[1] = 0;
00173 coord1->matrix[2] = 20;
00174 coord1->matrix[4] = 0.8;
00175 coord1->matrix[5] = 20;
00176
00177 path2 = rdman_shape_path_new(&rdman, "M 22,89.36218 C -34,-0.63782 39,-9.637817 82,12.36218 C 125,34.36218 142,136.36218 142,136.36218 C 100.66667,125.36218 74.26756,123.42795 22,89.36218 z ");
00178 rdman_paint_fill(&rdman, fill2, path2);
00179 rdman_paint_stroke(&rdman, stroke, path2);
00180 coord2->matrix[0] = -0.8;
00181 coord2->matrix[1] = 0;
00182 coord2->matrix[2] = 180;
00183 coord2->matrix[4] = 0.8;
00184 coord2->matrix[5] = 20;
00185
00186 rdman_coord_changed(&rdman, coord1);
00187 rdman_coord_changed(&rdman, coord2);
00188 rdman_add_shape(&rdman, (shape_t *)path1, coord1);
00189 rdman_add_shape(&rdman, (shape_t *)path2, coord2);
00190
00191
00192 fill3 = rdman_paint_linear_new(&rdman, 50, 50, 150, 150);
00193 grad_stop_init(fill3_stops, 0, 1, 0, 0, 0.5);
00194 grad_stop_init(fill3_stops + 1, 0.5, 0, 1, 0, 0.5);
00195 grad_stop_init(fill3_stops + 2, 1, 0, 0, 1, 0.5);
00196 paint_linear_stops(fill3, 3, fill3_stops);
00197 rect = rdman_shape_rect_new(&rdman, 50, 50, 100, 100, 20, 20);
00198 rdman_paint_fill(&rdman, fill3, rect);
00199 rdman_add_shape(&rdman, (shape_t *)rect, rdman.root_coord);
00200
00201 rdman_redraw_all(&rdman);
00202
00203 XFlush(display);
00204
00205 tman = mb_tman_new();
00206 if(tman) {
00207
00208 progm = mb_progm_new(10, &rdman);
00209
00210 MB_TIMEVAL_SET(&start, 0, 0);
00211 MB_TIMEVAL_SET(&playing, 1, 0);
00212 word = mb_progm_next_word(progm, &start, &playing);
00213
00214 act = mb_shift_new(50, 50, coord1, word);
00215 act = mb_shift_new(-50, 50, coord2, word);
00216
00217 MB_TIMEVAL_SET(&start, 1, 0);
00218 MB_TIMEVAL_SET(&playing, 2, 0);
00219 word = mb_progm_next_word(progm, &start, &playing);
00220
00221 act = mb_shift_new(0, 20, coord1, word);
00222 act = mb_shift_new(0, -20, coord2, word);
00223 act = mb_visibility_new(VIS_HIDDEN, coord3, word);
00224
00225 MB_TIMEVAL_SET(&start, 3, 0);
00226 MB_TIMEVAL_SET(&playing, 2, 0);
00227 word = mb_progm_next_word(progm, &start, &playing);
00228
00229 act = mb_shift_new(0, -20, coord1, word);
00230 act = mb_shift_new(0, 20, coord2, word);
00231 act = mb_chgcolor_new(0, 0, 1, 0.5, fill1, word);
00232 act = mb_chgcolor_new(1, 0, 0, 0.5, fill2, word);
00233 act = mb_visibility_new(VIS_VISIBLE, coord3, word);
00234
00235
00236 gettimeofday(&tv, NULL);
00237 MB_TIMEVAL_SET(&mbtv, tv.tv_sec, tv.tv_usec);
00238 mb_progm_start(progm, tman, &mbtv);
00239
00240 handle_connection(display, tman, &rdman, w, h);
00241
00242 mb_progm_free(progm);
00243 mb_tman_free(tman);
00244 }
00245
00246 rdman_paint_free(&rdman, fill1);
00247 rdman_paint_free(&rdman, fill2);
00248 rdman_paint_free(&rdman, stroke);
00249 rdman_paint_free(&rdman, text_stroke);
00250 rdman_paint_free(&rdman, text_fill);
00251 rdman_shape_free(&rdman, path1);
00252 rdman_shape_free(&rdman, path2);
00253 rdman_shape_free(&rdman, rect);
00254 rdman_shape_free(&rdman, text);
00255 redraw_man_destroy(&rdman);
00256 cairo_destroy(tmpcr);
00257 cairo_surface_destroy(tmpsuf);
00258 }
00259
00260 void drawing(cairo_surface_t *surface, int w, int h) {
00261 cairo_t *cr;
00262
00263 cr = cairo_create(surface);
00264 cairo_set_source_rgb(cr, 0, 0, 0);
00265 cairo_paint(cr);
00266 draw_path(cr, w, h);
00267 cairo_destroy(cr);
00268 }
00269
00270 int
00271 main(int argc, char * const argv[]) {
00272 Window root;
00273 Visual *visual;
00274 int screen;
00275 XSetWindowAttributes wattr;
00276 int depth;
00277 cairo_surface_t *surface;
00278 int w, h;
00279 int x, y;
00280 int r;
00281
00282 display = XOpenDisplay(":0.0");
00283 if(display == NULL)
00284 printf("XOpenDisplay\n");
00285 screen = DefaultScreen(display);
00286 root = DefaultRootWindow(display);
00287 visual = DefaultVisual(display, screen);
00288 depth = DefaultDepth(display, screen);
00289 wattr.override_redirect = False;
00290 x = 10;
00291 y = 10;
00292 w = 200;
00293 h = 200;
00294 win = XCreateWindow(display, root,
00295 x, y,
00296 w, h,
00297 1, depth, InputOutput, visual,
00298 CWOverrideRedirect, &wattr);
00299 r = XMapWindow(display, win);
00300
00301 surface = cairo_xlib_surface_create(display, win, visual, w, h);
00302 if(surface == NULL)
00303 printf("cairo_xlib_surface_create\n");
00304
00305 drawing(surface, w, h);
00306
00307 XFlush(display);
00308 sleep(10);
00309
00310 XCloseDisplay(display);
00311 return 0;
00312 }