summaryrefslogtreecommitdiff
path: root/tools/gnome-session-check-accelerated.c
blob: 8b69881509f6f88b1540ab1133e0397651cc2d87 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* -*- mode:c; c-basic-offset: 8; indent-tabs-mode: nil; -*- */
/* Tool to set the property _GNOME_SESSION_ACCELERATED on the root window */
/*
 * Copyright (C) 2011 Red Hat, Inc.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Author:
 *   Colin Walters <walters@verbum.org>
 */

#include <string.h>
#include <unistd.h>
#include <stdlib.h>

#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <X11/Xatom.h>
#include <sys/wait.h>

#include "gnome-session-check-accelerated-common.h"

/* Wait up to this long for a running check to finish */
#define PROPERTY_CHANGE_TIMEOUT 5000

/* Values used for the _GNOME_SESSION_ACCELERATED root window property */
#define NO_ACCEL            0
#define HAVE_ACCEL          1
#define ACCEL_CHECK_RUNNING 2

static Atom is_accelerated_atom;
static Atom is_software_rendering_atom;
static gboolean property_changed;

static void
exit_1_message (const char *msg) G_GNUC_NORETURN;

static void
exit_1_message (const char *msg)
{
  g_printerr ("%s", msg);
  exit (1);
}

static gboolean
on_property_notify_timeout (gpointer data)
{
        gtk_main_quit ();
        return FALSE;
}

static GdkFilterReturn
property_notify_filter (GdkXEvent *xevent,
                        GdkEvent  *event,
                        gpointer   data)
{
        XPropertyEvent *ev = xevent;

        if (ev->type == PropertyNotify && ev->atom == is_accelerated_atom) {
                property_changed = TRUE;
                gtk_main_quit ();
        }

        return GDK_FILTER_CONTINUE;
}

static gboolean
wait_for_property_notify (void)
{
        GdkDisplay *display;
        GdkScreen *screen;
        GdkWindow *root;
        Window rootwin;

        property_changed = FALSE;

        display = gdk_display_get_default ();
        screen = gdk_display_get_default_screen (display);
        root = gdk_screen_get_root_window (screen);
        rootwin = gdk_x11_window_get_xid (root);

        XSelectInput (GDK_DISPLAY_XDISPLAY (display), rootwin, PropertyChangeMask);
        gdk_window_add_filter (root, property_notify_filter, NULL);
        g_timeout_add (PROPERTY_CHANGE_TIMEOUT, on_property_notify_timeout, NULL);

        gtk_main ();

        return property_changed;
}

int
main (int argc, char **argv)
{
        GdkDisplay *display = NULL;
        int estatus;
        char *child_argv[] = { LIBEXECDIR "/gnome-session-check-accelerated-gl-helper", NULL };
        Window rootwin;
        glong is_accelerated, is_software_rendering;
        GError *error = NULL;

        /* gnome-session-check-accelerated gets run before X is started in the wayland
         * case, and it currently requires X. Until we have that working, just always
         * assume wayland will work
         */
        if (g_strcmp0 (g_getenv ("XDG_SESSION_DESKTOP"), "gnome-wayland") == 0) {
                return 0;
        }

        gtk_init (NULL, NULL);

        display = gdk_display_get_default ();
        rootwin = gdk_x11_get_default_root_xwindow ();

        is_accelerated_atom = gdk_x11_get_xatom_by_name_for_display (display, "_GNOME_SESSION_ACCELERATED");
        is_software_rendering_atom = gdk_x11_get_xatom_by_name_for_display (display, "_GNOME_IS_SOFTWARE_RENDERING");

        {
                Atom type;
                gint format;
                gulong nitems;
                gulong bytes_after;
                guchar *data;

 read:
                gdk_x11_display_error_trap_push (display);
                XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), rootwin,
                                    is_accelerated_atom,
                                    0, G_MAXLONG, False, XA_CARDINAL, &type, &format, &nitems,
                                    &bytes_after, &data);
                gdk_x11_display_error_trap_pop_ignored (display);

                if (type == XA_CARDINAL) {
                        glong *is_accelerated_ptr = (glong*) data;

                        if (*is_accelerated_ptr == ACCEL_CHECK_RUNNING) {
                                /* Test in progress, wait */
                                if (wait_for_property_notify ())
                                        goto read;
                                /* else fall through and do the check ourselves */

                        } else {
                                return (*is_accelerated_ptr == 0 ? 1 : 0);
                        }
                }
        }

        /* We don't have the property or it's the wrong type.
         * Try to compute it now.
         */

        /* First indicate that a test is in progress */
        is_accelerated = ACCEL_CHECK_RUNNING;
        XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
                         rootwin,
                         is_accelerated_atom,
                         XA_CARDINAL, 32, PropModeReplace, (guchar *) &is_accelerated, 1);

        gdk_display_sync (display);

        estatus = 1;
        if (!g_spawn_sync (NULL, (char**)child_argv, NULL, 0,
                           NULL, NULL, NULL, NULL, &estatus, &error)) {
                is_accelerated = FALSE;
                is_software_rendering = FALSE;
                g_printerr ("gnome-session-check-accelerated: Failed to run helper: %s\n", error->message);
                g_clear_error (&error);
        } else {
                is_accelerated = (WEXITSTATUS(estatus) == HELPER_ACCEL) || (WEXITSTATUS(estatus) == HELPER_SOFTWARE_RENDERING);
                is_software_rendering = (WEXITSTATUS(estatus) == HELPER_SOFTWARE_RENDERING);
                if (!is_accelerated)
                        g_printerr ("gnome-session-check-accelerated: Helper exited with code %d\n", estatus);
        }

	if (is_accelerated) {
		XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
				rootwin,
				is_accelerated_atom,
				XA_CARDINAL, 32, PropModeReplace, (guchar *) &is_accelerated, 1);
	}

	if (is_software_rendering) {
		XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
				rootwin,
				is_software_rendering_atom,
				XA_CARDINAL, 32, PropModeReplace, (guchar *) &is_software_rendering, 1);
	}

        gdk_display_sync (display);

        return is_accelerated ? 0 : 1;
}