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 "base/logging.h"
9
10#include "glinterface.h"
11#include "main.h"
12#include "xlib_window.h"
13
14
15Display *g_xlib_display = NULL;
16Window g_xlib_window = 0;
17
18GLint g_width = WINDOW_WIDTH;
19GLint g_height = WINDOW_HEIGHT;
20DEFINE_bool(override_redirect, true, "Use an override redirect window");
21
22
23bool XlibInit() {
24  // Prevent multiple initializations.
25  if (g_xlib_window)
26    return true;
27
28  g_xlib_display = XOpenDisplay(0);
29  if (!g_xlib_display) {
30    printf("# Error: in xlib_window.cc::XlibInit() could not open "
31           "default display.\n");
32    return false;
33  }
34
35  int screen = DefaultScreen(g_xlib_display);
36  Window root_window = RootWindow(g_xlib_display, screen);
37
38  XWindowAttributes attributes;
39  XGetWindowAttributes(g_xlib_display, root_window, &attributes);
40
41  g_width = g_width == -1 ? attributes.width : g_width;
42  g_height = g_height == -1 ? attributes.height : g_height;
43  XVisualInfo* xlib_visinfo = g_main_gl_interface->GetXVisual();
44
45  unsigned long mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask |
46    CWOverrideRedirect;
47  XSetWindowAttributes attr;
48  attr.background_pixel = 0;
49  attr.border_pixel = 0;
50  attr.colormap = XCreateColormap(g_xlib_display, root_window,
51                                  xlib_visinfo->visual, AllocNone);
52  attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
53  attr.override_redirect = FLAGS_override_redirect ? True : False;
54  g_xlib_window = XCreateWindow(g_xlib_display, root_window,
55                                0, 0, g_width, g_height, 0,
56                                xlib_visinfo->depth, InputOutput,
57                                xlib_visinfo->visual, mask, &attr);
58
59  XMapWindow(g_xlib_display, g_xlib_window);
60  XSync(g_xlib_display, True);
61
62  XGetWindowAttributes(g_xlib_display, g_xlib_window, &attributes);
63  g_width = attributes.width;
64  g_height = attributes.height;
65
66  XFree(xlib_visinfo);
67
68  return true;
69}
70