annotate src/X_main.c @ 16:e17e12b112c4

A simple animation using rdman_redraw_changed().
author Thinker K.F. Li <thinker@branda.to>
date Fri, 01 Aug 2008 18:20:28 +0800
parents c2ce186a5c37
children 0f3baa488a62
rev   line source
3
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
1 #include <stdio.h>
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
2 #include <unistd.h>
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
3 #include <X11/Xlib.h>
6
772511b8b9be Cairo specify RGB values in range 0.0 ~ 1.0.
Thinker K.F. Li <thinker@branda.to>
parents: 5
diff changeset
4 #include <X11/Xutil.h>
3
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
5 #include <cairo.h>
10
7cfecdce94cc Remove warning messages
Thinker K.F. Li <thinker@branda.to>
parents: 9
diff changeset
6 #include <cairo-xlib.h>
3
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
7
5
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents: 3
diff changeset
8 #include <string.h>
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents: 3
diff changeset
9 #include "shapes.h"
12
79e9edf4c00a Add redraw manager
Thinker K.F. Li <thinker@branda.to>
parents: 11
diff changeset
10 #include "redraw_man.h"
5
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents: 3
diff changeset
11
16
e17e12b112c4 A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents: 15
diff changeset
12 Display *display;
e17e12b112c4 A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents: 15
diff changeset
13
5
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents: 3
diff changeset
14 void draw_path(cairo_t *cr, int w, int h) {
12
79e9edf4c00a Add redraw manager
Thinker K.F. Li <thinker@branda.to>
parents: 11
diff changeset
15 redraw_man_t rdman;
5
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents: 3
diff changeset
16 shape_t *path;
12
79e9edf4c00a Add redraw manager
Thinker K.F. Li <thinker@branda.to>
parents: 11
diff changeset
17 coord_t *coord;
16
e17e12b112c4 A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents: 15
diff changeset
18 int i;
12
79e9edf4c00a Add redraw manager
Thinker K.F. Li <thinker@branda.to>
parents: 11
diff changeset
19
15
c2ce186a5c37 X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents: 13
diff changeset
20 redraw_man_init(&rdman, cr);
12
79e9edf4c00a Add redraw manager
Thinker K.F. Li <thinker@branda.to>
parents: 11
diff changeset
21 coord = rdman.root_coord;
5
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents: 3
diff changeset
22
11
128af06c876c Fix the bug that data of a path end with white spaces would make system down
Thinker K.F. Li <thinker@branda.to>
parents: 10
diff changeset
23 path = sh_path_new("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 ");
16
e17e12b112c4 A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents: 15
diff changeset
24 coord->matrix[0] = 0.8;
e17e12b112c4 A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents: 15
diff changeset
25 coord->matrix[1] = 0;
e17e12b112c4 A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents: 15
diff changeset
26 coord->matrix[2] = 20;
e17e12b112c4 A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents: 15
diff changeset
27 coord->matrix[4] = 0.8;
e17e12b112c4 A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents: 15
diff changeset
28 coord->matrix[5] = 20;
15
c2ce186a5c37 X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents: 13
diff changeset
29 rdman_coord_changed(&rdman, coord);
12
79e9edf4c00a Add redraw manager
Thinker K.F. Li <thinker@branda.to>
parents: 11
diff changeset
30 rdman_add_shape(&rdman, (shape_t *)path, coord);
15
c2ce186a5c37 X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents: 13
diff changeset
31
c2ce186a5c37 X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents: 13
diff changeset
32 rdman_redraw_all(&rdman);
c2ce186a5c37 X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents: 13
diff changeset
33
16
e17e12b112c4 A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents: 15
diff changeset
34 XFlush(display);
e17e12b112c4 A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents: 15
diff changeset
35
e17e12b112c4 A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents: 15
diff changeset
36 for(i = 0; i < 10; i++) {
e17e12b112c4 A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents: 15
diff changeset
37 usleep(50000);
e17e12b112c4 A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents: 15
diff changeset
38 coord->matrix[2] += 5;
e17e12b112c4 A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents: 15
diff changeset
39 coord->matrix[5] += 5;
e17e12b112c4 A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents: 15
diff changeset
40 rdman_coord_changed(&rdman, coord);
e17e12b112c4 A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents: 15
diff changeset
41 rdman_redraw_changed(&rdman);
e17e12b112c4 A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents: 15
diff changeset
42 XFlush(display);
e17e12b112c4 A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents: 15
diff changeset
43 }
e17e12b112c4 A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents: 15
diff changeset
44
15
c2ce186a5c37 X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents: 13
diff changeset
45 redraw_man_destroy(&rdman);
13
ed55009d96d3 refactory for redrawing
Thinker K.F. Li <thinker@branda.to>
parents: 12
diff changeset
46 sh_path_free(path);
5
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents: 3
diff changeset
47 }
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents: 3
diff changeset
48
3
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
49 void drawing(cairo_surface_t *surface, int w, int h) {
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
50 cairo_t *cr;
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
51
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
52 cr = cairo_create(surface);
7
569f3168ba53 Clear background & tranform relative pos into absolute ones
Thinker K.F. Li <thinker@branda.to>
parents: 6
diff changeset
53 cairo_set_source_rgb(cr, 0, 0, 0);
569f3168ba53 Clear background & tranform relative pos into absolute ones
Thinker K.F. Li <thinker@branda.to>
parents: 6
diff changeset
54 cairo_paint(cr);
6
772511b8b9be Cairo specify RGB values in range 0.0 ~ 1.0.
Thinker K.F. Li <thinker@branda.to>
parents: 5
diff changeset
55 cairo_set_source_rgb(cr, 0.9, 0.1, 0.1);
5
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents: 3
diff changeset
56 draw_path(cr, w, h);
6
772511b8b9be Cairo specify RGB values in range 0.0 ~ 1.0.
Thinker K.F. Li <thinker@branda.to>
parents: 5
diff changeset
57 cairo_set_source_rgb(cr, 0.5, 0.9, 0.8);
3
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
58 cairo_move_to(cr, 10, h / 2);
12
79e9edf4c00a Add redraw manager
Thinker K.F. Li <thinker@branda.to>
parents: 11
diff changeset
59 cairo_set_font_size(cr, 36.0);
79e9edf4c00a Add redraw manager
Thinker K.F. Li <thinker@branda.to>
parents: 11
diff changeset
60 cairo_text_path(cr, "hello \xe6\xbc\xa2\xe5\xad\x97");
6
772511b8b9be Cairo specify RGB values in range 0.0 ~ 1.0.
Thinker K.F. Li <thinker@branda.to>
parents: 5
diff changeset
61 cairo_set_line_width(cr, 2);
3
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62 cairo_stroke(cr);
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
63 }
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
64
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
65 int
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
66 main(int argc, char * const argv[]) {
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
67 Window root;
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
68 Visual *visual;
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
69 int screen;
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
70 XSetWindowAttributes wattr;
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
71 Window win;
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
72 int depth;
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
73 cairo_surface_t *surface;
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
74 int w, h;
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
75 int x, y;
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
76 int r;
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
77
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
78 display = XOpenDisplay(":0.0");
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
79 if(display == NULL)
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
80 printf("XOpenDisplay\n");
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
81 screen = DefaultScreen(display);
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
82 root = DefaultRootWindow(display);
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
83 visual = DefaultVisual(display, screen);
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
84 depth = DefaultDepth(display, screen);
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
85 wattr.override_redirect = False;
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
86 x = 10;
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
87 y = 10;
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
88 w = 200;
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
89 h = 200;
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
90 win = XCreateWindow(display, root,
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
91 x, y,
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
92 w, h,
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
93 1, depth, InputOutput, visual,
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
94 CWOverrideRedirect, &wattr);
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
95 r = XMapWindow(display, win);
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
96
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
97 surface = cairo_xlib_surface_create(display, win, visual, w, h);
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
98 if(surface == NULL)
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
99 printf("cairo_xlib_surface_create\n");
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
100
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
101 drawing(surface, w, h);
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
102
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
103 XFlush(display);
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
104 sleep(10);
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
105
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
106 XCloseDisplay(display);
10
7cfecdce94cc Remove warning messages
Thinker K.F. Li <thinker@branda.to>
parents: 9
diff changeset
107 return 0;
3
164162781a7a Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
108 }