]> icculus.org git repositories - dana/openbox.git/blob - obt/display.c
97a2992b35a7680cb9b7fe841cf5bff534bb9b50
[dana/openbox.git] / obt / display.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    obt/display.c for the Openbox window manager
4    Copyright (c) 2007-2008   Dana Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "obt/display.h"
20 #include "obt/prop.h"
21 #include "obt/internal.h"
22 #include "obt/keyboard.h"
23
24 #ifdef HAVE_STRING_H
25 #  include <string.h>
26 #endif
27 #ifdef HAVE_FCNTL_H
28 #  include <fcntl.h>
29 #endif
30 #ifdef HAVE_UNISTD_H
31 #  include <unistd.h>
32 #endif
33
34 Display* obt_display = NULL;
35
36 gboolean obt_display_error_occured = FALSE;
37
38 gboolean obt_display_extension_xkb       = FALSE;
39 gint     obt_display_extension_xkb_basep;
40 gboolean obt_display_extension_shape     = FALSE;
41 gint     obt_display_extension_shape_basep;
42 gboolean obt_display_extension_xinerama  = FALSE;
43 gint     obt_display_extension_xinerama_basep;
44 gboolean obt_display_extension_randr     = FALSE;
45 gint     obt_display_extension_randr_basep;
46 gboolean obt_display_extension_sync      = FALSE;
47 gint     obt_display_extension_sync_basep;
48 gboolean obt_display_extension_composite = FALSE;
49 gint     obt_display_extension_composite_basep;
50
51 static gint xerror_handler(Display *d, XErrorEvent *e);
52
53 static gboolean xerror_ignore = FALSE;
54
55 gboolean obt_display_open(const char *display_name)
56 {
57     gchar *n;
58     Display *d = NULL;
59
60     n = display_name ? g_strdup(display_name) : NULL;
61     obt_display = d = XOpenDisplay(n);
62     if (d) {
63         gint junk;
64         (void)junk;
65
66         if (fcntl(ConnectionNumber(d), F_SETFD, 1) == -1)
67             g_message("Failed to set display as close-on-exec");
68         XSetErrorHandler(xerror_handler);
69
70         /* read what extensions are present */
71 #ifdef XKB
72         obt_display_extension_xkb =
73             XkbQueryExtension(d, &junk,
74                               &obt_display_extension_xkb_basep, &junk,
75                               NULL, NULL);
76         if (!obt_display_extension_xkb)
77             g_message("XKB extension is not present on the server");
78 #endif
79
80 #ifdef SHAPE
81         obt_display_extension_shape =
82             XShapeQueryExtension(d, &obt_display_extension_shape_basep,
83                                  &junk);
84         if (!obt_display_extension_shape)
85             g_message("X Shape extension is not present on the server");
86 #endif
87
88 #ifdef XINERAMA
89         obt_display_extension_xinerama =
90             XineramaQueryExtension(d,
91                                    &obt_display_extension_xinerama_basep,
92                                    &junk) && XineramaIsActive(d);
93         if (!obt_display_extension_xinerama)
94             g_message("Xinerama extension is not present on the server");
95 #endif
96
97 #ifdef XRANDR
98         obt_display_extension_randr =
99             XRRQueryExtension(d, &obt_display_extension_randr_basep,
100                               &junk);
101         if (!obt_display_extension_randr)
102             g_message("XRandR extension is not present on the server");
103 #endif
104
105 #ifdef SYNC
106         obt_display_extension_sync =
107             XSyncQueryExtension(d, &obt_display_extension_sync_basep,
108                                 &junk) && XSyncInitialize(d, &junk, &junk);
109         if (!obt_display_extension_sync)
110             g_message("X Sync extension is not present on the server or is an "
111                       "incompatible version");
112 #endif
113
114 #ifdef USE_XCOMPOSITE
115         if (XCompositeQueryExtension(d, &obt_display_extension_composite_basep,
116                                      &junk))
117         {
118             gint major = 0, minor = 2;
119             XCompositeQueryVersion(d, &major, &minor);
120             /* Version 0.2 is the first version to have the
121                XCompositeNameWindowPixmap() request. */
122             if (major > 0 || minor >= 2)
123                 obt_display_extension_composite = TRUE;
124         }
125         if (!obt_display_extension_composite)
126             g_message("X Composite extension is not present on the server or "
127                       "is an incompatible version");
128 #endif
129
130         obt_prop_startup();
131         obt_keyboard_reload();
132     }
133     g_free(n);
134
135     return obt_display != NULL;
136 }
137
138 void obt_display_close()
139 {
140     obt_keyboard_shutdown();
141     if (obt_display) XCloseDisplay(obt_display);
142 }
143
144 static gint xerror_handler(Display *d, XErrorEvent *e)
145 {
146 #ifdef DEBUG
147     gchar errtxt[128];
148
149     XGetErrorText(d, e->error_code, errtxt, 127);
150     if (!xerror_ignore) {
151         if (e->error_code == BadWindow)
152             /*g_message(_("X Error: %s\n"), errtxt)*/;
153         else
154             g_error("X Error: %s", errtxt);
155     } else
156         g_message("Ignoring XError code %d '%s'", e->error_code, errtxt);
157 #else
158     (void)d; (void)e;
159 #endif
160
161     obt_display_error_occured = TRUE;
162     return 0;
163 }
164
165 void obt_display_ignore_errors(gboolean ignore)
166 {
167     XSync(obt_display, FALSE);
168     xerror_ignore = ignore;
169     if (ignore) obt_display_error_occured = FALSE;
170 }