Advertisement
Geometrian

Magic and XMoveWindow

Feb 11th, 2015
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.01 KB | None | 0 0
  1. //Compile with "g++ <filename>.cpp -std=c++11 -lX11 -lGL"
  2.  
  3. #include <cassert>
  4. #include <cstdio>
  5.  
  6. #include <X11/Xlib.h>
  7. #include <X11/Xutil.h>
  8. #include <X11/keysym.h>
  9.  
  10. #include <GL/glx.h>
  11.  
  12.  
  13. static Display* display;
  14.  
  15.  
  16. static void _x11_map_window(Window window) {
  17.     printf("Mapping window %lu; RETURN to continue\n",window); getchar();
  18.     XMapWindow(display, window);
  19.     printf("Mapped window!  RETURN to continue\n"); getchar();
  20. }
  21. static void _x11_unmap_window(Window window) {
  22.     printf("Unmapping window %lu; RETURN to continue\n",window); getchar();
  23.     XUnmapWindow(display, window);
  24.     printf("Unmapped window!  RETURN to continue\n"); getchar();
  25. }
  26.  
  27.  
  28. int main(int argc, char* argv[]) {
  29.     /* ##### MAKE DISPLAY ##### */
  30.     display = XOpenDisplay(nullptr);
  31.  
  32.  
  33.     /* ##### MAKE VISUAL INFO. ##### */
  34.     int attributes[] = { //can't be const b/c X11 doesn't like it.  Not sure if that's intentional or just stupid.
  35.         GLX_RGBA, //apparently nothing comes after this?
  36.         GLX_RED_SIZE,    8,
  37.         GLX_GREEN_SIZE,  8,
  38.         GLX_BLUE_SIZE,   8,
  39.         GLX_ALPHA_SIZE,  8,
  40.         //Ideally, the size would be 32 (or at least 24), but I have actually seen
  41.         //  this size (on a modern OS even).
  42.         GLX_DEPTH_SIZE, 16,
  43.         GLX_DOUBLEBUFFER, True,
  44.         None
  45.     };
  46.  
  47.     #pragma GCC diagnostic push
  48.     #pragma GCC diagnostic ignored "-Wold-style-cast" //Because of X11's cruft in "DefaultScreen".
  49.     XVisualInfo* visual_info = glXChooseVisual(display, DefaultScreen(display), attributes);
  50.     #pragma GCC diagnostic pop
  51.     assert(visual_info!=nullptr);
  52.  
  53.  
  54.     /* ##### MAKE WINDOW ##### */
  55.     Window parent = XDefaultRootWindow(display);
  56.  
  57.     Colormap colormap = XCreateColormap(display, parent, visual_info->visual, AllocNone);
  58.  
  59.     XSetWindowAttributes window_attributes_set;
  60.     window_attributes_set.colormap = colormap;
  61.     window_attributes_set.background_pixel = 0; //This and next b/c of http://stackoverflow.com/questions/3645632/how-to-create-a-window-with-a-bit-depth-of-32
  62.     window_attributes_set.border_pixel = 0;     //especially resulting in BadMatch error on Raspberry Pi.  Also changes bit fields below in XCreateWindow.
  63.     window_attributes_set.event_mask = ExposureMask | KeyPressMask;
  64.  
  65.     int position[2]={50,50}, dimensions[2]={128,128};
  66.     Window window = XCreateWindow(
  67.         display, parent,
  68.         position[0],position[1], static_cast<unsigned int>(dimensions[0]),static_cast<unsigned int>(dimensions[1]), //Note: the documentation must be wrong; this thing wants unsigned ints.
  69.         0u,
  70.         visual_info->depth,
  71.         InputOutput,
  72.         visual_info->visual,
  73.         //CWColormap|CWEventMask,
  74.         CWBackPixel|CWColormap|CWBorderPixel | CWEventMask,
  75.         &window_attributes_set
  76.     );
  77.     assert(window!=0);
  78.     printf("Created window %lu\n",window);
  79.     XStoreName(display, window, "[default title]");
  80.     XSelectInput(display, window,
  81.         //http://www.tronche.com/gui/x/xlib/events/mask.html#NoEventMask
  82.         //http://www.tronche.com/gui/x/xlib/events/processing-overview.html
  83.         ExposureMask |
  84.         KeyPressMask | KeyReleaseMask |
  85.         ButtonPressMask | ButtonReleaseMask | //ButtonMotionMask |
  86.         //EnterWindowMask | LeaveWindowMask |
  87.         PointerMotionMask |
  88.         //KeymapStateMask | FocusChangeMask | ColormapChangeMask |
  89.         StructureNotifyMask //Resizing, etc.
  90.         //PropertyChangeMask
  91.     );
  92.  
  93.     Atom wm_delete = XInternAtom(display, "WM_DELETE_WINDOW", True);
  94.     XSetWMProtocols(display, window, &wm_delete, 1);
  95.  
  96.     XMoveWindow(display, window, 100,100);
  97.  
  98.     //As described here: http://stackoverflow.com/questions/14801536/xmovewindow-not-working-before-xmapwindow
  99.     //  "the X server doesn't have to know about a window before it is mapped for the first time".  Hence,
  100.     //  map the window and then unmap it so that the X server knows about it.  This is important because some
  101.     //  functions silently fail (e.g. XMoveWindow) when the X server is oblivious.
  102.     _x11_map_window(window);
  103.     _x11_unmap_window(window);
  104.  
  105.  
  106.     /* ##### MAKE RENDER CONTEXT ##### */
  107.     GLXContext render_context = glXCreateContext(display, visual_info, nullptr, True);
  108.     assert(render_context!=nullptr);
  109.  
  110.  
  111.     /* ##### MORE STUFF WOULD GO HERE ##### */
  112.     while (1);
  113.  
  114.     return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement