modernc.org/99c@v1.0.1-0.20181109153923-a9e8197063d9/examples/xcb/drawingprimitives.c (about)

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