modernc.org/xcb@v1.0.15/examples/drawingprimitives/drawingprimitives.c (about) 1 // +build ignore 2 3 // src: https://www.x.org/releases/current/doc/libxcb/tutorial/index.html#drawingprim 4 5 #include <stdlib.h> 6 #include <stdio.h> 7 8 #include <xcb/xcb.h> 9 10 int 11 main () 12 { 13 xcb_connection_t *c; 14 xcb_screen_t *screen; 15 xcb_drawable_t win; 16 xcb_gcontext_t foreground; 17 xcb_generic_event_t *e; 18 uint32_t mask = 0; 19 uint32_t values[2]; 20 21 /* geometric objects */ 22 xcb_point_t points[] = { 23 {10, 10}, 24 {10, 20}, 25 {20, 10}, 26 {20, 20}}; 27 28 xcb_point_t polyline[] = { 29 {50, 10}, 30 { 5, 20}, /* rest of points are relative */ 31 {25,-20}, 32 {10, 10}}; 33 34 xcb_segment_t segments[] = { 35 {100, 10, 140, 30}, 36 {110, 25, 130, 60}}; 37 38 xcb_rectangle_t rectangles[] = { 39 { 10, 50, 40, 20}, 40 { 80, 50, 10, 40}}; 41 42 xcb_arc_t arcs[] = { 43 {10, 100, 60, 40, 0, 90 << 6}, 44 {90, 100, 55, 40, 0, 270 << 6}}; 45 46 /* Open the connection to the X server */ 47 c = xcb_connect (NULL, NULL); 48 49 /* Get the first screen */ 50 screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data; 51 52 /* Create black (foreground) graphic context */ 53 win = screen->root; 54 55 foreground = xcb_generate_id (c); 56 mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES; 57 values[0] = screen->black_pixel; 58 values[1] = 0; 59 xcb_create_gc (c, foreground, win, mask, values); 60 61 /* Ask for our window's Id */ 62 win = xcb_generate_id(c); 63 64 /* Create the window */ 65 mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; 66 values[0] = screen->white_pixel; 67 values[1] = XCB_EVENT_MASK_EXPOSURE; 68 xcb_create_window (c, /* Connection */ 69 XCB_COPY_FROM_PARENT, /* depth */ 70 win, /* window Id */ 71 screen->root, /* parent window */ 72 0, 0, /* x, y */ 73 150, 150, /* width, height */ 74 10, /* border_width */ 75 XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class */ 76 screen->root_visual, /* visual */ 77 mask, values); /* masks */ 78 79 /* Map the window on the screen */ 80 xcb_map_window (c, win); 81 82 83 /* We flush the request */ 84 xcb_flush (c); 85 86 while ((e = xcb_wait_for_event (c))) { 87 switch (e->response_type & ~0x80) { 88 case XCB_EXPOSE: { 89 /* We draw the points */ 90 xcb_poly_point (c, XCB_COORD_MODE_ORIGIN, win, foreground, 4, points); 91 92 /* We draw the polygonal line */ 93 xcb_poly_line (c, XCB_COORD_MODE_PREVIOUS, win, foreground, 4, polyline); 94 95 /* We draw the segements */ 96 xcb_poly_segment (c, win, foreground, 2, segments); 97 98 /* We draw the rectangles */ 99 xcb_poly_rectangle (c, win, foreground, 2, rectangles); 100 101 /* We draw the arcs */ 102 xcb_poly_arc (c, win, foreground, 2, arcs); 103 104 /* We flush the request */ 105 xcb_flush (c); 106 107 break; 108 } 109 default: { 110 /* Unknown event type, ignore it */ 111 break; 112 } 113 } 114 /* Free the Generic Event */ 115 free (e); 116 } 117 118 return 0; 119 }