summaryrefslogtreecommitdiff
path: root/obt/display.c
blob: 8b06cbfcf6cac47d213fe5a7ec33f12e6fbd1359 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-

   obt/display.c for the Openbox window manager
   Copyright (c) 2007        Dana Jansens

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   See the COPYING file for a copy of the GNU General Public License.
*/

#include "obt/display.h"
#include "obt/prop.h"
#include "obt/internal.h"
#include "obt/keyboard.h"
#include "obt/xqueue.h"

#ifdef HAVE_STRING_H
#  include <string.h>
#endif
#ifdef HAVE_FCNTL_H
#  include <fcntl.h>
#endif
#ifdef HAVE_UNISTD_H
#  include <unistd.h>
#endif

/* from xqueue.c */
extern void xqueue_init(void);
extern void xqueue_destroy(void);

Display* obt_display = NULL;

gboolean obt_display_error_occured = FALSE;

gboolean obt_display_extension_xkb       = FALSE;
gint     obt_display_extension_xkb_basep;
gboolean obt_display_extension_shape     = FALSE;
gint     obt_display_extension_shape_basep;
gboolean obt_display_extension_xinerama  = FALSE;
gint     obt_display_extension_xinerama_basep;
gboolean obt_display_extension_randr     = FALSE;
gint     obt_display_extension_randr_basep;
gboolean obt_display_extension_sync      = FALSE;
gint     obt_display_extension_sync_basep;

static gint xerror_handler(Display *d, XErrorEvent *e);

static gboolean xerror_ignore = FALSE;

gboolean obt_display_open(const char *display_name)
{
    gchar *n;
    Display *d = NULL;

    n = display_name ? g_strdup(display_name) : NULL;
    obt_display = d = XOpenDisplay(n);
    if (d) {
        gint junk, major, minor;
        (void)junk, (void)major, (void)minor;

        if (fcntl(ConnectionNumber(d), F_SETFD, 1) == -1)
            g_message("Failed to set display as close-on-exec");
        XSetErrorHandler(xerror_handler);

        /* read what extensions are present */
#ifdef XKB
        major = XkbMajorVersion;
        minor = XkbMinorVersion;
        obt_display_extension_xkb =
            XkbQueryExtension(d, &junk,
                              &obt_display_extension_xkb_basep, &junk,
                              &major, &minor);
        if (!obt_display_extension_xkb)
            g_message("XKB extension is not present on the server or too old");
#endif

#ifdef SHAPE
        obt_display_extension_shape =
            XShapeQueryExtension(d, &obt_display_extension_shape_basep,
                                 &junk);
        if (!obt_display_extension_shape)
            g_message("X Shape extension is not present on the server");
#endif

#ifdef XINERAMA
        obt_display_extension_xinerama =
            XineramaQueryExtension(d,
                                   &obt_display_extension_xinerama_basep,
                                   &junk) && XineramaIsActive(d);
        if (!obt_display_extension_xinerama)
            g_message("Xinerama extension is not present on the server");
#endif

#ifdef XRANDR
        obt_display_extension_randr =
            XRRQueryExtension(d, &obt_display_extension_randr_basep,
                              &junk);
        if (!obt_display_extension_randr)
            g_message("XRandR extension is not present on the server");
#endif

#ifdef SYNC
        obt_display_extension_sync =
            XSyncQueryExtension(d, &obt_display_extension_sync_basep,
                                &junk) && XSyncInitialize(d, &junk, &junk);
        if (!obt_display_extension_sync)
            g_message("X Sync extension is not present on the server or is an "
                      "incompatible version");
#endif

        obt_prop_startup();
        obt_keyboard_reload();
    }
    g_free(n);

    if (obt_display)
        xqueue_init();

    return obt_display != NULL;
}

void obt_display_close(void)
{
    obt_keyboard_shutdown();
    if (obt_display) {
        xqueue_destroy();
        XCloseDisplay(obt_display);
    }
}

static gint xerror_handler(Display *d, XErrorEvent *e)
{
#ifdef DEBUG
    gchar errtxt[128];

    XGetErrorText(d, e->error_code, errtxt, 127);
    if (!xerror_ignore) {
        if (e->error_code == BadWindow)
            /*g_debug(_("X Error: %s\n"), errtxt)*/;
        else
            g_error("X Error: %s", errtxt);
    } else
        g_debug("Ignoring XError code %d '%s'", e->error_code, errtxt);
#else
    (void)d; (void)e;
#endif

    obt_display_error_occured = TRUE;
    return 0;
}

void obt_display_ignore_errors(gboolean ignore)
{
    XSync(obt_display, FALSE);
    xerror_ignore = ignore;
    if (ignore) obt_display_error_occured = FALSE;
}