1 module std.c.linux.X11.xcb.examle;
2 
3 /* build with: dmd  xcb.d xproto.d example.d -L-lxcb */
4 
5 import std.stdio;
6 import std.c.linux.X11.xcb.xcb;
7 import std.c.linux.X11.xcb.xproto;
8 import std.c.stdlib;
9 int main()
10 {
11   xcb_connection_t    *c;
12   xcb_screen_t        *s;
13   xcb_window_t         w;
14   xcb_gcontext_t       g;
15   xcb_generic_event_t *e;
16   uint             	   mask;
17   uint                 values[2];
18   int                  done = 0;
19   static xcb_rectangle_t      r = { 20, 20, 60, 60 };
20 
21                        /* open connection with the server */
22   c = xcb_connect(null,null);
23   if (xcb_connection_has_error(c)) {
24     writefln("Cannot open display");
25     return 1;
26   }
27                        /* get the first screen */
28   s = xcb_setup_roots_iterator( xcb_get_setup(c) ).data;
29 
30                        /* create black graphics context */
31   g = xcb_generate_id(c);
32   w = s.root;
33   mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
34   values[0] = s.black_pixel;
35   values[1] = 0;
36   xcb_create_gc(c, g, w, mask, &values[0]);
37 
38                        /* create window */
39   w = xcb_generate_id(c);
40   mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
41   values[0] = s.white_pixel;
42   values[1] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_KEY_PRESS;
43   xcb_create_window(c, s.root_depth, w, s.root,
44                   10, 10, 100, 100, 1,
45                   XCB_WINDOW_CLASS_INPUT_OUTPUT, s.root_visual,
46                   mask, &values[0]);
47 
48                        /* map (show) the window */
49   xcb_map_window(c, w);
50   
51   xcb_flush(c);
52 
53                        /* event loop */
54                        
55   do{
56   	e = xcb_wait_for_event(c);
57   	if(!e)break;
58     switch (e.response_type & ~0x80) {
59     case XCB_EXPOSE:    /* draw or redraw the window */
60       xcb_poly_fill_rectangle(c, w, g,  1, &r);
61       xcb_flush(c);
62       break;
63     case XCB_KEY_PRESS:  /* exit on key press */
64       done = 1;
65       break;
66       }
67     free(e);
68   }while(!done);
69                        /* close connection to server */
70   xcb_disconnect(c);
71 
72   return 0;
73 }