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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* Metacity window deletion */
/*
* Copyright (C) 2001, 2002 Havoc Pennington
* Copyright (C) 2004 Elijah Newren
*
* 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.
*/
#define _GNU_SOURCE
#define _SVID_SOURCE /* for gethostname() */
#include <config.h>
#include "util.h"
#include "window-private.h"
#include "errors.h"
#include "workspace.h"
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
static void meta_window_present_delete_dialog (MetaWindow *window,
guint32 timestamp);
static void
delete_ping_reply_func (MetaDisplay *display,
Window xwindow,
guint32 timestamp,
void *user_data)
{
meta_topic (META_DEBUG_PING,
"Got reply to delete ping for %s\n",
((MetaWindow*)user_data)->desc);
/* we do nothing */
}
static Window
window_from_string (const char *str)
{
char *end;
unsigned long l;
end = NULL;
l = strtoul (str, &end, 16);
if (end == NULL || end == str)
{
meta_warning (_("Could not parse \"%s\" as an integer"),
str);
return None;
}
if (*end != '\0')
{
meta_warning (_("Did not understand trailing characters \"%s\" in string \"%s\""),
end, str);
return None;
}
return l;
}
static int
pid_from_string (const char *str)
{
char *end;
long l;
end = NULL;
l = strtol (str, &end, 10);
if (end == NULL || end == str)
{
meta_warning (_("Could not parse \"%s\" as an integer"),
str);
return None;
}
if (*end != '\0')
{
meta_warning (_("Did not understand trailing characters \"%s\" in string \"%s\""),
end, str);
return None;
}
return l;
}
static gboolean
parse_dialog_output (const char *str,
int *pid_out,
Window *win_out)
{
char **split;
split = g_strsplit (str, "\n", 2);
if (split && split[0] && split[1])
{
g_strchomp (split[0]);
g_strchomp (split[1]);
*pid_out = pid_from_string (split[0]);
*win_out = window_from_string (split[1]);
g_strfreev (split);
return TRUE;
}
else
{
g_strfreev (split);
meta_warning (_("Failed to parse message \"%s\" from dialog process\n"),
str);
return FALSE;
}
}
static void
search_and_destroy_window (int pid,
Window xwindow)
{
/* Find the window with the given dialog PID,
* double check that it matches "xwindow", then
* kill the window.
*/
GSList *tmp;
gboolean found = FALSE;
GSList *windows;
if (xwindow == None)
{
meta_topic (META_DEBUG_PING,
"Window to destroy is None, doing nothing\n");
return;
}
windows = meta_display_list_windows (meta_get_display ());
tmp = windows;
while (tmp != NULL)
{
MetaWindow *w = tmp->data;
if (w->dialog_pid == pid)
{
if (w->xwindow != xwindow)
meta_topic (META_DEBUG_PING,
"Dialog pid matches but not xwindow (0x%lx vs. 0x%lx)\n",
w->xwindow, xwindow);
else
{
meta_window_kill (w);
found = TRUE;
}
}
tmp = tmp->next;
}
g_slist_free (windows);
if (!found)
meta_topic (META_DEBUG_PING,
"Did not find a window with dialog pid %d xwindow 0x%lx\n",
pid, xwindow);
}
static void
release_window_with_fd (int fd)
{
/* Find the window with the given dialog PID,
* double check that it matches "xwindow", then
* kill the window.
*/
gboolean found = FALSE;
GSList *windows = meta_display_list_windows (meta_get_display ());
GSList *tmp = windows;
while (tmp != NULL)
{
MetaWindow *w = tmp->data;
if (w->dialog_pid >= 0 &&
w->dialog_pipe == fd)
{
meta_topic (META_DEBUG_PING,
"Removing dialog with fd %d pid %d from window %s\n",
fd, w->dialog_pid, w->desc);
meta_window_free_delete_dialog (w);
found = TRUE;
}
tmp = tmp->next;
}
g_slist_free (windows);
if (!found)
meta_topic (META_DEBUG_PING,
"Did not find a window with a dialog pipe %d\n",
fd);
}
static gboolean
io_from_ping_dialog (GIOChannel *channel,
GIOCondition condition,
gpointer data)
{
meta_topic (META_DEBUG_PING,
"IO handler from ping dialog, condition = %x\n",
condition);
if (condition & G_IO_IN)
{
char *str;
gsize len;
GError *err;
/* Go ahead and block for all data from child */
str = NULL;
len = 0;
err = NULL;
g_io_channel_read_to_end (channel,
&str, &len,
&err);
if (err)
{
meta_warning (_("Error reading from dialog display process: %s\n"),
err->message);
g_error_free (err);
}
meta_topic (META_DEBUG_PING,
"Read %" G_GSIZE_FORMAT " bytes strlen %d \"%s\" from child\n",
len, str ? (int) strlen (str) : 0, str ? str : "NULL");
if (len > 0)
{
/* We're supposed to kill the given window */
int pid;
Window xwindow;
if (parse_dialog_output (str, &pid, &xwindow))
search_and_destroy_window (pid, xwindow);
}
g_free (str);
}
release_window_with_fd (g_io_channel_unix_get_fd (channel));
/* Remove the callback */
return FALSE;
}
static void
delete_ping_timeout_func (MetaDisplay *display,
Window xwindow,
guint32 timestamp,
void *user_data)
{
MetaWindow *window = user_data;
GError *err;
int child_pid;
int outpipe;
char *argv[9];
char numbuf[32];
char timestampbuf[32];
char *window_id_str;
char *window_title;
GIOChannel *channel;
meta_topic (META_DEBUG_PING,
"Got delete ping timeout for %s\n",
window->desc);
if (window->dialog_pid >= 0)
{
meta_window_present_delete_dialog (window, timestamp);
return;
}
window_id_str = g_strdup_printf ("0x%lx", window->xwindow);
window_title = g_locale_from_utf8 (window->title, -1, NULL, NULL, NULL);
sprintf (numbuf, "%d", window->screen->number);
sprintf (timestampbuf, "%u", timestamp);
argv[0] = METACITY_LIBEXECDIR"/metacity-dialog";
argv[1] = "--screen";
argv[2] = numbuf;
argv[3] = "--timestamp";
argv[4] = timestampbuf;
argv[5] = "--kill-window-question";
argv[6] = window_title;
argv[7] = window_id_str;
argv[8] = NULL;
err = NULL;
if (!g_spawn_async_with_pipes ("/",
argv,
NULL,
0,
NULL, NULL,
&child_pid,
NULL,
&outpipe,
NULL,
&err))
{
meta_warning (_("Error launching metacity-dialog to ask about killing an application: %s\n"),
err->message);
g_error_free (err);
goto out;
}
window->dialog_pid = child_pid;
window->dialog_pipe = outpipe;
channel = g_io_channel_unix_new (window->dialog_pipe);
g_io_add_watch_full (channel, G_PRIORITY_DEFAULT,
G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
io_from_ping_dialog,
NULL, NULL);
g_io_channel_unref (channel);
out:
g_free (window_title);
g_free (window_id_str);
}
void
meta_window_delete (MetaWindow *window,
guint32 timestamp)
{
meta_error_trap_push (window->display);
if (window->delete_window)
{
meta_topic (META_DEBUG_WINDOW_OPS,
"Deleting %s with delete_window request\n",
window->desc);
meta_window_send_icccm_message (window,
window->display->atom_WM_DELETE_WINDOW,
timestamp);
}
else
{
meta_topic (META_DEBUG_WINDOW_OPS,
"Deleting %s with explicit kill\n",
window->desc);
XKillClient (window->display->xdisplay, window->xwindow);
}
meta_error_trap_pop (window->display, FALSE);
meta_display_ping_window (window->display,
window,
timestamp,
delete_ping_reply_func,
delete_ping_timeout_func,
window);
if (window->has_focus)
{
/* FIXME Clean this up someday
* http://bugzilla.gnome.org/show_bug.cgi?id=108706
*/
#if 0
/* This is unfortunately going to result in weirdness
* if the window doesn't respond to the delete event.
* I don't know how to avoid that though.
*/
meta_topic (META_DEBUG_FOCUS,
"Focusing default window because focus window %s was deleted/killed\n",
window->desc);
meta_workspace_focus_default_window (window->screen->active_workspace,
window);
#else
meta_topic (META_DEBUG_FOCUS,
"Not unfocusing %s on delete/kill\n",
window->desc);
#endif
}
else
{
meta_topic (META_DEBUG_FOCUS,
"Window %s was deleted/killed but didn't have focus\n",
window->desc);
}
}
void
meta_window_kill (MetaWindow *window)
{
char buf[257];
meta_topic (META_DEBUG_WINDOW_OPS,
"Killing %s brutally\n",
window->desc);
if (window->wm_client_machine != NULL &&
window->net_wm_pid > 0)
{
if (gethostname (buf, sizeof(buf)-1) == 0)
{
if (strcmp (buf, window->wm_client_machine) == 0)
{
meta_topic (META_DEBUG_WINDOW_OPS,
"Killing %s with kill()\n",
window->desc);
if (kill (window->net_wm_pid, 9) < 0)
meta_topic (META_DEBUG_WINDOW_OPS,
"Failed to signal %s: %s\n",
window->desc, strerror (errno));
}
}
else
{
meta_warning (_("Failed to get hostname: %s\n"),
strerror (errno));
}
}
meta_topic (META_DEBUG_WINDOW_OPS,
"Disconnecting %s with XKillClient()\n",
window->desc);
meta_error_trap_push (window->display);
XKillClient (window->display->xdisplay, window->xwindow);
meta_error_trap_pop (window->display, FALSE);
}
void
meta_window_free_delete_dialog (MetaWindow *window)
{
if (window->dialog_pid >= 0)
{
kill (window->dialog_pid, 9);
close (window->dialog_pipe);
window->dialog_pid = -1;
window->dialog_pipe = -1;
}
}
static void
meta_window_present_delete_dialog (MetaWindow *window, guint32 timestamp)
{
meta_topic (META_DEBUG_PING,
"Presenting existing ping dialog for %s\n",
window->desc);
if (window->dialog_pid >= 0)
{
GSList *windows;
GSList *tmp;
/* Activate transient for window that belongs to
* metacity-dialog
*/
windows = meta_display_list_windows (window->display);
tmp = windows;
while (tmp != NULL)
{
MetaWindow *w = tmp->data;
if (w->xtransient_for == window->xwindow &&
w->res_class &&
g_ascii_strcasecmp (w->res_class, "metacity-dialog") == 0)
{
meta_window_activate (w, timestamp);
break;
}
tmp = tmp->next;
}
g_slist_free (windows);
}
}
|