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