1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include <gflags/gflags.h> 6 #include <stdio.h> 7 8 #include "glinterface.h" 9 #include "main.h" 10 #include "xlib_window.h" 11 12 Display* g_xlib_display = NULL; 13 Window g_xlib_window = 0; 14 15 GLint g_width = WINDOW_WIDTH; 16 GLint g_height = WINDOW_HEIGHT; 17 DEFINE_bool(override_redirect, true, "Use an override redirect window"); 18 19 bool XlibInit() { 20 // Prevent multiple initializations. 21 if (g_xlib_window) 22 return true; 23 24 g_xlib_display = XOpenDisplay(0); 25 if (!g_xlib_display) { 26 printf( 27 "# Error: in xlib_window.cc::XlibInit() could not open " 28 "default display.\n"); 29 return false; 30 } 31 32 int screen = DefaultScreen(g_xlib_display); 33 Window root_window = RootWindow(g_xlib_display, screen); 34 35 XWindowAttributes attributes; 36 XGetWindowAttributes(g_xlib_display, root_window, &attributes); 37 38 g_width = g_width == -1 ? attributes.width : g_width; 39 g_height = g_height == -1 ? attributes.height : g_height; 40 XVisualInfo* xlib_visinfo = g_main_gl_interface->GetXVisual(); 41 42 unsigned long mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask | 43 CWOverrideRedirect; 44 XSetWindowAttributes attr; 45 attr.background_pixel = 0; 46 attr.border_pixel = 0; 47 attr.colormap = XCreateColormap(g_xlib_display, root_window, 48 xlib_visinfo->visual, AllocNone); 49 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask; 50 attr.override_redirect = FLAGS_override_redirect ? True : False; 51 g_xlib_window = XCreateWindow(g_xlib_display, root_window, 0, 0, g_width, 52 g_height, 0, xlib_visinfo->depth, InputOutput, 53 xlib_visinfo->visual, mask, &attr); 54 55 XMapWindow(g_xlib_display, g_xlib_window); 56 XSync(g_xlib_display, True); 57 58 XGetWindowAttributes(g_xlib_display, g_xlib_window, &attributes); 59 g_width = attributes.width; 60 g_height = attributes.height; 61 62 XFree(xlib_visinfo); 63 64 return true; 65 } 66