1 /* -*- mode:c; c-basic-offset: 8; indent-tabs-mode: nil; -*- */
2 /* Tool to set the property _CINNAMON_SESSION_ACCELERATED on the root window */
3 /*
4  * Copyright (C) 2011 Red Hat, Inc.
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  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA  02110-1335  USA
19  *
20  * Author:
21  *   Colin Walters <walters@verbum.org>
22  */
23 
24 #include "config.h"
25 
26 #include <string.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 
30 #include <gtk/gtk.h>
31 #include <gdk/gdkx.h>
32 #include <X11/Xatom.h>
33 
34 /* Wait up to this long for a running check to finish */
35 #define PROPERTY_CHANGE_TIMEOUT 5000
36 
37 /* Values used for the _CINNAMON_SESSION_ACCELERATED root window property */
38 #define NO_ACCEL            0
39 #define HAVE_ACCEL          1
40 #define ACCEL_CHECK_RUNNING 2
41 
42 static Atom is_accelerated_atom;
43 static gboolean property_changed;
44 
45 static void
46 exit_1_message (const char *msg) G_GNUC_NORETURN;
47 
48 static void
exit_1_message(const char * msg)49 exit_1_message (const char *msg)
50 {
51   g_printerr ("%s", msg);
52   exit (1);
53 }
54 
55 static gboolean
on_property_notify_timeout(gpointer data)56 on_property_notify_timeout (gpointer data)
57 {
58         gtk_main_quit ();
59         return FALSE;
60 }
61 
62 static GdkFilterReturn
property_notify_filter(GdkXEvent * xevent,GdkEvent * event,gpointer data)63 property_notify_filter (GdkXEvent *xevent,
64                         GdkEvent  *event,
65                         gpointer   data)
66 {
67         XPropertyEvent *ev = xevent;
68 
69         if (ev->type == PropertyNotify && ev->atom == is_accelerated_atom) {
70                 property_changed = TRUE;
71                 gtk_main_quit ();
72         }
73 
74         return GDK_FILTER_CONTINUE;
75 }
76 
77 static gboolean
wait_for_property_notify(void)78 wait_for_property_notify (void)
79 {
80         GdkDisplay *display = NULL;
81         GdkScreen *screen;
82         GdkWindow *root;
83         Window rootwin;
84 
85         property_changed = FALSE;
86 
87         display = gdk_display_get_default ();
88         screen = gdk_display_get_default_screen (display);
89         root = gdk_screen_get_root_window (screen);
90         rootwin = gdk_x11_window_get_xid (root);
91 
92         XSelectInput (GDK_DISPLAY_XDISPLAY (display), rootwin, PropertyChangeMask);
93         gdk_window_add_filter (root, property_notify_filter, NULL);
94         g_timeout_add (PROPERTY_CHANGE_TIMEOUT, on_property_notify_timeout, NULL);
95 
96         gtk_main ();
97 
98         return property_changed;
99 }
100 
101 int
main(int argc,char ** argv)102 main (int argc, char **argv)
103 {
104         GdkDisplay *display = NULL;
105         int estatus;
106         char *child_argv[] = { LIBEXECDIR "/cinnamon-session-check-accelerated-helper", NULL };
107         Window rootwin;
108         glong is_accelerated;
109         GError *error = NULL;
110 
111         gtk_init (NULL, NULL);
112 
113         display = gdk_display_get_default ();
114         rootwin = gdk_x11_get_default_root_xwindow ();
115 
116         is_accelerated_atom = gdk_x11_get_xatom_by_name_for_display (display, "_CINNAMON_SESSION_ACCELERATED");
117 
118         {
119                 Atom type;
120                 gint format;
121                 gulong nitems;
122                 gulong bytes_after;
123                 guchar *data;
124 
125  read:
126                 gdk_x11_display_error_trap_push (display);
127                 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), rootwin,
128                                     is_accelerated_atom,
129                                     0, G_MAXLONG, False, XA_CARDINAL, &type, &format, &nitems,
130                                     &bytes_after, &data);
131                 gdk_x11_display_error_trap_pop_ignored (display);
132 
133                 if (type == XA_CARDINAL) {
134                         glong *is_accelerated_ptr = (glong*) data;
135 
136                         if (*is_accelerated_ptr == ACCEL_CHECK_RUNNING) {
137                                 /* Test in progress, wait */
138                                 if (wait_for_property_notify ())
139                                         goto read;
140                                 /* else fall through and do the check ourselves */
141 
142                         } else {
143                                 return (*is_accelerated_ptr == 0 ? 1 : 0);
144                         }
145                 }
146         }
147 
148         /* We don't have the property or it's the wrong type.
149          * Try to compute it now.
150          */
151 
152         /* First indicate that a test is in progress */
153         is_accelerated = ACCEL_CHECK_RUNNING;
154         XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
155                          rootwin,
156                          is_accelerated_atom,
157                          XA_CARDINAL, 32, PropModeReplace, (guchar *) &is_accelerated, 1);
158 
159         gdk_display_sync (display);
160 
161         estatus = 1;
162         if (!g_spawn_sync (NULL, (char**)child_argv, NULL, 0,
163                            NULL, NULL, NULL, NULL, &estatus, &error)) {
164                 is_accelerated = FALSE;
165                 g_printerr ("cinnamon-session-check-accelerated: Failed to run helper: %s\n", error->message);
166                 g_clear_error (&error);
167         } else {
168                 is_accelerated = (estatus == 0);
169                 if (!is_accelerated)
170                         g_printerr ("cinnamon-session-check-accelerated: Helper exited with code %d\n", estatus);
171         }
172 
173         XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
174                          rootwin,
175                          is_accelerated_atom,
176                          XA_CARDINAL, 32, PropModeReplace, (guchar *) &is_accelerated, 1);
177 
178         gdk_display_sync (display);
179 
180         return is_accelerated ? 0 : 1;
181 }
182