]> icculus.org git repositories - dana/openbox.git/blob - obt/display.c
ignore all BadDamage errors, just like BadWindow. a valid damage id can disappear...
[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 #include "obt/xqueue.h"
24
25 #ifdef HAVE_STRING_H
26 #  include <string.h>
27 #endif
28 #ifdef HAVE_FCNTL_H
29 #  include <fcntl.h>
30 #endif
31 #ifdef HAVE_UNISTD_H
32 #  include <unistd.h>
33 #endif
34
35 /* from xqueue.c */
36 extern void xqueue_init(void);
37 extern void xqueue_destroy(void);
38
39 Display* obt_display = NULL;
40
41 gboolean obt_display_error_occured = FALSE;
42
43 gboolean obt_display_extension_xkb       = FALSE;
44 gint     obt_display_extension_xkb_basep;
45 gboolean obt_display_extension_shape     = FALSE;
46 gint     obt_display_extension_shape_basep;
47 gboolean obt_display_extension_xinerama  = FALSE;
48 gint     obt_display_extension_xinerama_basep;
49 gboolean obt_display_extension_randr     = FALSE;
50 gint     obt_display_extension_randr_basep;
51 gboolean obt_display_extension_sync      = FALSE;
52 gint     obt_display_extension_sync_basep;
53 gboolean obt_display_extension_composite = FALSE;
54 gint     obt_display_extension_composite_basep;
55 gboolean obt_display_extension_damage    = FALSE;
56 gint     obt_display_extension_damage_basep;
57 gint     obt_display_extension_damage_error;
58 gboolean obt_display_extension_render    = FALSE;
59 gint     obt_display_extension_render_basep;
60 gboolean obt_display_extension_fixes     = FALSE;
61 gint     obt_display_extension_fixes_basep;
62
63 static gint xerror_handler(Display *d, XErrorEvent *e);
64
65 static gboolean xerror_ignore = FALSE;
66
67 gboolean obt_display_open(const char *display_name)
68 {
69     gchar *n;
70     Display *d = NULL;
71
72     n = display_name ? g_strdup(display_name) : NULL;
73     obt_display = d = XOpenDisplay(n);
74     if (d) {
75         gint junk, major, minor;
76         (void)junk, (void)major, (void)minor;
77
78         if (fcntl(ConnectionNumber(d), F_SETFD, 1) == -1)
79             g_message("Failed to set display as close-on-exec");
80         XSetErrorHandler(xerror_handler);
81
82         /* read what extensions are present */
83 #ifdef XKB
84         major = XkbMajorVersion;
85         minor = XkbMinorVersion;
86         obt_display_extension_xkb =
87             XkbQueryExtension(d, &junk,
88                               &obt_display_extension_xkb_basep, &junk,
89                               &major, &minor);
90         if (!obt_display_extension_xkb)
91             g_message("XKB extension is not present on the server or too old");
92 #endif
93
94 #ifdef SHAPE
95         obt_display_extension_shape =
96             XShapeQueryExtension(d, &obt_display_extension_shape_basep,
97                                  &junk);
98         if (!obt_display_extension_shape)
99             g_message("X Shape extension is not present on the server");
100 #endif
101
102 #ifdef XINERAMA
103         obt_display_extension_xinerama =
104             XineramaQueryExtension(d,
105                                    &obt_display_extension_xinerama_basep,
106                                    &junk) && XineramaIsActive(d);
107         if (!obt_display_extension_xinerama)
108             g_message("Xinerama extension is not present on the server");
109 #endif
110
111 #ifdef XRANDR
112         obt_display_extension_randr =
113             XRRQueryExtension(d, &obt_display_extension_randr_basep,
114                               &junk);
115         if (!obt_display_extension_randr)
116             g_message("XRandR extension is not present on the server");
117 #endif
118
119 #ifdef SYNC
120         obt_display_extension_sync =
121             XSyncQueryExtension(d, &obt_display_extension_sync_basep,
122                                 &junk) && XSyncInitialize(d, &junk, &junk);
123         if (!obt_display_extension_sync)
124             g_message("X Sync extension is not present on the server");
125 #endif
126
127 #ifdef USE_COMPOSITING
128         if (XCompositeQueryExtension(d, &obt_display_extension_composite_basep,
129                                      &junk))
130         {
131             gint major = 0, minor = 2;
132             XCompositeQueryVersion(d, &major, &minor);
133             /* Version 0.2 is the first version to have the
134                XCompositeNameWindowPixmap() request. */
135             if (major > 0 || minor >= 2)
136                 obt_display_extension_composite = TRUE;
137         }
138         if (!obt_display_extension_composite)
139             g_message("X Composite extension is not present on the server or "
140                       "is an incompatible version");
141
142         obt_display_extension_damage =
143             XDamageQueryExtension(d, &obt_display_extension_damage_basep,
144                                   &obt_display_extension_damage_error);
145         if (!obt_display_extension_damage)
146             g_message("X Damage extension is not present on the server");
147
148         obt_display_extension_render =
149             XRenderQueryExtension(d, &obt_display_extension_render_basep,
150                                   &junk);
151         if (!obt_display_extension_render)
152             g_message("X Render extension is not present on the server");
153
154         obt_display_extension_fixes =
155             XFixesQueryExtension(d, &obt_display_extension_fixes_basep,
156                                   &junk);
157         if (!obt_display_extension_fixes)
158             g_message("X Fixes extension is not present on the server");
159 #endif
160
161         obt_prop_startup();
162         obt_keyboard_reload();
163     }
164     g_free(n);
165
166     if (obt_display)
167         xqueue_init();
168
169     return obt_display != NULL;
170 }
171
172 void obt_display_close(void)
173 {
174     obt_keyboard_shutdown();
175     if (obt_display) {
176         xqueue_destroy();
177         XCloseDisplay(obt_display);
178     }
179 }
180
181 static gint xerror_handler(Display *d, XErrorEvent *e)
182 {
183 #ifdef DEBUG
184     gchar errtxt[128];
185
186     XGetErrorText(d, e->error_code, errtxt, 127);
187     if (!xerror_ignore) {
188         if (e->error_code == BadWindow ||
189             (obt_display_extension_damage &&
190              e->error_code - obt_display_extension_damage_error == BadDamage))
191         {
192             /*g_error("X Error: %d '%s'", e->error_code, errtxt);*/
193         }
194         else
195             g_error("X Error: %s", errtxt);
196     } else
197         g_debug("Ignoring XError code %d '%s'", e->error_code, errtxt);
198 #else
199     (void)d; (void)e;
200 #endif
201
202     obt_display_error_occured = TRUE;
203     return 0;
204 }
205
206 void obt_display_ignore_errors(gboolean ignore)
207 {
208     XSync(obt_display, FALSE);
209     xerror_ignore = ignore;
210     if (ignore) obt_display_error_occured = FALSE;
211 }