#include #include #include #include #include #include #include #include static void redraw(cairo_t *ctx) { /* * Paint canvas black */ cairo_set_source_rgb(ctx, 0, 0, 0); cairo_paint(ctx); /* * Draw two identical circles */ cairo_set_source_rgb(ctx, 1, 1, 1); cairo_arc(ctx, 208, 240, 192, 0, 360); cairo_stroke(ctx); cairo_arc(ctx, 816, 240, 192, 0, 360); cairo_stroke(ctx); /* * Draw two smaller circles */ cairo_arc(ctx, 442, 368, 64, 0, 360 * (M_PI/180)); cairo_stroke(ctx); cairo_arc(ctx, 582, 368, 64, 0, 360 * (M_PI/180)); cairo_stroke(ctx); /* * Draw a rectangle */ cairo_move_to(ctx, 432, 48); cairo_line_to(ctx, 592, 48); cairo_line_to(ctx, 592, 288); cairo_line_to(ctx, 432, 288); cairo_line_to(ctx, 432, 48); cairo_set_source_rgb(ctx, 0.75, 0, 0); cairo_fill(ctx); cairo_move_to(ctx, 432, 48); cairo_line_to(ctx, 592, 48); cairo_line_to(ctx, 592, 288); cairo_line_to(ctx, 432, 288); cairo_line_to(ctx, 432, 48); cairo_set_source_rgb(ctx, 1, 1, 1); cairo_stroke(ctx); } static void draw_text(cairo_t *cr) { cairo_select_font_face(cr, "Helvetica", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); cairo_set_font_size(cr, 13); cairo_move_to(cr, 208 + 192, 240); cairo_save(cr); cairo_rotate(cr, 45.0 * (M_PI/180)); cairo_show_text(cr, "10"); cairo_restore(cr); cairo_move_to(cr, 208, 240 + 192); cairo_save(cr); cairo_rotate(cr, 90.0 * (M_PI/180)); cairo_show_text(cr, "20"); cairo_restore(cr); } int main(int argc, char **argv) { Display *display; Window win; Pixmap buf; GC gc; int screen, x = 1024, y = 480; XGCValues values = { .foreground = 0x000000, .background = 0xffffff, .graphics_exposures = 0 }; cairo_surface_t *sfc; cairo_t *ctx; XEvent e; if ((display = XOpenDisplay(NULL)) == NULL) exit(1); screen = DefaultScreen(display); win = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, x, y, 0, 0, 0); gc = XCreateGC(display, win, GCForeground | GCBackground | GCGraphicsExposures, &values); buf = XCreatePixmap(display, win, x, y, 24); XSelectInput(display, win, ExposureMask | ButtonPressMask | KeyPressMask); XMapWindow(display, win); sfc = cairo_xlib_surface_create(display, buf, DefaultVisual(display, screen), x, y); cairo_xlib_surface_set_size(sfc, x, y); ctx = cairo_create(sfc); redraw(ctx); draw_text(ctx); while (1) { XExposeEvent *expose; XNextEvent(cairo_xlib_surface_get_display(sfc), &e); switch (e.type) { case Expose: expose = (XExposeEvent *)&e; XCopyArea(display, buf, win, gc, expose->x, expose->y, expose->width, expose->height, expose->x, expose->y); break; case ButtonPress: case KeyPress: goto done; default: fprintf(stderr, "Dropping unhandled XEvent.type = %d.\n", e.type); } } done: cairo_destroy(ctx); cairo_surface_destroy(sfc); XCloseDisplay(display); return 0; }