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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/* Copyright (C) 1998 Redhat Software Inc.
* Code available under the Gnu GPL.
* Authors: Owen Taylor <otaylor@redhat.com>
*/
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <libgnome/libgnome.h>
#include "wm-properties.h"
typedef struct _RestartInfo RestartInfo;
struct _RestartInfo {
GnomeDesktopItem *dentry;
gint retries;
WMResultFunc callback;
gpointer data;
};
gboolean
wm_is_running (void)
{
gboolean result;
XWindowAttributes attrs;
gdk_error_trap_push ();
XGetWindowAttributes (GDK_DISPLAY(), GDK_ROOT_WINDOW(), &attrs);
XSelectInput (GDK_DISPLAY(), GDK_ROOT_WINDOW(),
SubstructureRedirectMask);
XSync (GDK_DISPLAY(), False);
if (gdk_error_trap_pop () == 0) {
result = FALSE;
XSelectInput (GDK_DISPLAY(), GDK_ROOT_WINDOW(),
attrs.your_event_mask);
} else
result = TRUE;
return result;
}
/* Cut and paste from gnome-libs/gnome_win_hints_wm_exists, except that we
* return the xid instead of a window
*/
static Window
find_gnome_wm_window(void)
{
Atom r_type;
int r_format;
unsigned long count;
unsigned long bytes_remain;
unsigned char *prop, *prop2;
GdkAtom cardinal_atom = gdk_atom_intern ("CARDINAL", FALSE);
gdk_error_trap_push ();
if (XGetWindowProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
gdk_x11_atom_to_xatom (gdk_atom_intern ("_WIN_SUPPORTING_WM_CHECK", FALSE)),
0, 1, False, gdk_x11_atom_to_xatom (cardinal_atom),
&r_type, &r_format,
&count, &bytes_remain, &prop) == Success && prop)
{
if (r_type == gdk_x11_atom_to_xatom (cardinal_atom) && r_format == 32 && count == 1)
{
Window n = *(long *)prop;
if (XGetWindowProperty(GDK_DISPLAY(), n,
gdk_x11_atom_to_xatom (gdk_atom_intern ("_WIN_SUPPORTING_WM_CHECK", FALSE)),
0, 1, False, gdk_x11_atom_to_xatom (cardinal_atom),
&r_type, &r_format, &count, &bytes_remain,
&prop2) == Success && prop)
{
if (r_type == gdk_x11_atom_to_xatom (cardinal_atom) && r_format == 32 && count == 1)
{
XFree(prop);
XFree(prop2);
gdk_error_trap_pop ();
return n;
}
XFree(prop2);
}
}
XFree(prop);
}
gdk_error_trap_pop ();
return None;
}
static Window
find_wm_window_from_client (GdkWindow *client)
{
Window window, frame, parent, root;
Window *children;
unsigned int nchildren;
gboolean needs_pop = TRUE;
if (!client)
return None;
frame = None;
window = GDK_WINDOW_XWINDOW (client);
gdk_error_trap_push ();
while (XQueryTree (GDK_DISPLAY(), window,
&root, &parent, &children, &nchildren))
{
if (gdk_error_trap_pop != 0)
{
needs_pop = FALSE;
break;
}
gdk_error_trap_push ();
if (children)
XFree(children);
if (window == root)
break;
if (root == parent) {
frame = window;
break;
}
window = parent;
}
if (needs_pop)
gdk_error_trap_pop ();
return frame;
}
static gboolean
window_has_wm_state (Window window)
{
Atom r_type;
int r_format;
unsigned long count;
unsigned long bytes_remain;
unsigned char *prop;
if (XGetWindowProperty(GDK_DISPLAY(), window,
gdk_x11_atom_to_xatom (gdk_atom_intern ("WM_STATE", FALSE)),
0, 0, False, AnyPropertyType,
&r_type, &r_format,
&count, &bytes_remain, &prop) == Success) {
if (r_type != None) {
XFree(prop);
return TRUE;
}
}
return FALSE;
}
static gboolean
descendent_has_wm_state (Window window)
{
gboolean result = FALSE;
Window parent, root;
Window *children;
unsigned int nchildren;
gint i;
if (!XQueryTree (GDK_DISPLAY(), window,
&root, &parent, &children, &nchildren))
return FALSE;
for (i=0; i<nchildren; i++) {
if (window_has_wm_state (children[i]) ||
descendent_has_wm_state (children[i])) {
result = TRUE;
break;
}
}
if (children)
XFree (children);
return result;
}
/* This function tries to find a window manager frame by
* hunting all the children of the root window
*/
static Window
find_wm_window_from_hunt (void)
{
Window parent, root, frame;
Window *children;
unsigned int nchildren;
gint i;
frame = None;
gdk_error_trap_push ();
XQueryTree (GDK_DISPLAY(), GDK_ROOT_WINDOW (),
&root, &parent, &children, &nchildren);
/* We are looking for a window that doesn't have WIN_STATE
* set on it, but does have a child with WIN_STATE set
*/
for (i=0; i<nchildren; i++) {
if (!window_has_wm_state (children[i]) &&
descendent_has_wm_state (children[i])) {
frame = children[i];
break;
}
}
if (children)
XFree (children);
gdk_error_trap_pop ();
return frame;
}
static Window
find_wm_window (GdkWindow *client)
{
Window wm_window = None;
/* First, try to find a GNOME compliant WM */
wm_window = find_gnome_wm_window();
if (!wm_window) {
wm_window = find_wm_window_from_client (client);
}
if (!wm_window) {
wm_window = find_wm_window_from_hunt ();
}
return wm_window;
}
static gboolean
start_timeout (gpointer data)
{
RestartInfo *info = data;
if (wm_is_running ()) {
info->callback(WM_SUCCESS, info->data);
gnome_desktop_item_unref (info->dentry);
g_free (info);
return FALSE;
} else {
info->retries--;
if (info->retries > 0)
return TRUE;
else {
info->callback(WM_CANT_START, info->data);
gnome_desktop_item_unref (info->dentry);
g_free (info);
return FALSE;
}
}
}
static void
start_do (RestartInfo *info)
{
gnome_desktop_item_launch (info->dentry, 0, 0, NULL);
info->retries = 10;
gtk_timeout_add (1000, start_timeout, info);
}
static gboolean
kill_timeout (gpointer data)
{
RestartInfo *info = data;
if (!wm_is_running ()) {
start_do (info);
return FALSE;
} else {
info->retries--;
if (info->retries > 0)
return TRUE;
else {
info->callback(WM_ALREADY_RUNNING, info->data);
gnome_desktop_item_unref (info->dentry);
g_free (info);
return FALSE;
}
}
}
void
wm_restart (WindowManager *new,
GdkWindow *client,
WMResultFunc callback,
gpointer data)
{
Window wm_window;
RestartInfo *info;
g_return_if_fail (new->is_present);
info = g_new (RestartInfo, 1);
info->dentry = gnome_desktop_item_copy (new->dentry);
info->callback = callback;
info->data = data;
info->retries = 10;
if (wm_is_running ()) {
wm_window = find_wm_window (client);
if (!wm_window) {
(*callback) (WM_ALREADY_RUNNING, data);
gnome_desktop_item_unref (info->dentry);
g_free (info);
} else {
XKillClient (GDK_DISPLAY(), wm_window);
gtk_timeout_add (1000, kill_timeout, info);
}
} else {
start_do (info);
}
}
WindowManager *
wm_guess_current (void)
{
return NULL;
}
|