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