Advertisement
Guest User

xcb D example program

a guest
Apr 16th, 2013
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.28 KB | None | 0 0
  1. module std.c.linux.X11.xcb.example;
  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.   auto iter = xcb_setup_roots_iterator(xcb_get_setup(c));
  29.   s = iter.data;
  30.  
  31.   w = iter.data.root;
  32.  
  33.  
  34.                        /* create black graphics context */
  35.   g = xcb_generate_id(c);
  36.   mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
  37.   values[0] = (*s).white_pixel;
  38.   values[1] = 0;
  39.   xcb_create_gc(c, g, s.root, mask, &values[0]);
  40.  
  41.                        /* create window */
  42.   w = xcb_generate_id(c);
  43.   mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
  44.   values[0] = s.white_pixel;
  45.   values[1] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_KEY_PRESS;
  46.   xcb_create_window(c, s.root_depth, w, s.root,
  47.                   10, 10, 100, 100, 1,
  48.                   XCB_WINDOW_CLASS_INPUT_OUTPUT, s.root_visual,
  49.                   mask, &values[0]);
  50.  
  51.   writeln("test");                     /* map (show) the window */
  52.   xcb_generic_error_t *err = xcb_request_check(c, xcb_map_window_checked(c, w));
  53.   if(err)
  54.         writeln("there was an error mapping the window");
  55.         //writeln(e.error_code);
  56.   xcb_flush(c);
  57.  
  58.                        /* event loop */
  59.                        
  60.   do{
  61.     e = xcb_wait_for_event(c);
  62.     if(!e)break;
  63.     switch (e.response_type & ~0x80) {
  64.     case XCB_EXPOSE:    /* draw or redraw the window */
  65.       xcb_poly_fill_rectangle(c, w, g,  1, &r);
  66.       xcb_flush(c);
  67.       break;
  68.     case XCB_KEY_PRESS:  /* exit on key press */
  69.       done = 1;
  70.       break;
  71.     default:
  72.       break;
  73.     }
  74.     free(e);
  75.   }while(!done);
  76.                        /* close connection to server */
  77.   xcb_disconnect(c);
  78.  
  79.   return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement