move the code for tracking what extensions are available on the display into the...
[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        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
21 #ifdef HAVE_STRING_H
22 #  include <string.h>
23 #endif
24 #ifdef HAVE_FCNTL_H
25 #  include <fcntl.h>
26 #endif
27 #ifdef HAVE_UNISTD_H
28 #  include <unistd.h>
29 #endif
30
31 gboolean obt_display_error_occured = FALSE;
32
33 gboolean obt_display_extension_xkb       = FALSE;
34 gint     obt_display_extension_xkb_basep;
35 gboolean obt_display_extension_shape     = FALSE;
36 gint     obt_display_extension_shape_basep;
37 gboolean obt_display_extension_xinerama  = FALSE;
38 gint     obt_display_extension_xinerama_basep;
39 gboolean obt_display_extension_randr     = FALSE;
40 gint     obt_display_extension_randr_basep;
41 gboolean obt_display_extension_sync      = FALSE;
42 gint     obt_display_extension_sync_basep;
43
44 static gint xerror_handler(Display *d, XErrorEvent *e);
45
46 static gboolean xerror_ignore = FALSE;
47
48 Display* obt_display_open(const char *display_name)
49 {
50     gchar *n;
51     Display *d = NULL;
52
53     n = display_name ? g_strdup(display_name) : NULL;
54     d = XOpenDisplay(n);
55     if (d) {
56         gint junk;
57         (void)junk;
58
59         if (fcntl(ConnectionNumber(d), F_SETFD, 1) == -1)
60             g_message("Failed to set display as close-on-exec");
61         XSetErrorHandler(xerror_handler);
62
63         /* read what extensions are present */
64 #ifdef XKB
65         obt_display_extension_xkb =
66             XkbQueryExtension(d, &junk,
67                               &obt_display_extension_xkb_basep, &junk,
68                               NULL, NULL);
69         if (!obt_display_extension_xkb)
70             g_message("XKB extension is not present on the server");
71 #endif
72
73 #ifdef SHAPE
74         obt_display_extension_shape =
75             XShapeQueryExtension(d, &obt_display_extension_shape_basep,
76                                  &junk);
77         if (!obt_display_extension_shape)
78             g_message("X Shape extension is not present on the server");
79 #endif
80
81 #ifdef XINERAMA
82         obt_display_extension_xinerama =
83             XineramaQueryExtension(d,
84                                    &obt_display_extension_xinerama_basep,
85                                    &junk) && XineramaIsActive(d);
86         if (!obt_display_extension_xinerama)
87             g_message("Xinerama extension is not present on the server");
88 #endif
89
90 #ifdef XRANDR
91         obt_display_extension_randr =
92             XRRQueryExtension(d, &obt_display_extension_randr_basep,
93                               &junk);
94         if (!obt_display_extension_randr)
95             g_message("XRandR extension is not present on the server");
96 #endif
97
98 #ifdef SYNC
99         obt_display_extension_sync =
100             XSyncQueryExtension(d, &obt_display_extension_sync_basep,
101                                 &junk) && XSyncInitialize(d, &junk, &junk);
102         if (!obt_display_extension_sync)
103             g_message("X Sync extension is not present on the server or is an "
104                       "incompatible version");
105 #endif
106     }
107     g_free(n);
108
109     return d;
110 }
111
112 void obt_display_close(Display *d)
113 {
114     if (d) XCloseDisplay(d);
115 }
116
117 static gint xerror_handler(Display *d, XErrorEvent *e)
118 {
119 #ifdef DEBUG
120     gchar errtxt[128];
121
122     XGetErrorText(d, e->error_code, errtxt, 127);
123     if (!xerror_ignore) {
124         if (e->error_code == BadWindow)
125             /*g_message(_("X Error: %s\n"), errtxt)*/;
126         else
127             g_error("X Error: %s", errtxt);
128     } else
129         g_message("XError code %d '%s'", e->error_code, errtxt);
130 #else
131     (void)d; (void)e;
132 #endif
133
134     obt_display_error_occured = TRUE;
135     return 0;
136 }
137
138 void obt_display_ignore_errors(Display *d, gboolean ignore)
139 {
140     XSync(d, FALSE);
141     xerror_ignore = ignore;
142     if (ignore) obt_display_error_occured = FALSE;
143 }