]> Dogcows Code - chaz/openbox/blob - openbox/client.c
Remove an unused variable.
[chaz/openbox] / openbox / client.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 client.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "client.h"
21 #include "debug.h"
22 #include "startupnotify.h"
23 #include "dock.h"
24 #include "xerror.h"
25 #include "screen.h"
26 #include "moveresize.h"
27 #include "ping.h"
28 #include "place.h"
29 #include "prop.h"
30 #include "extensions.h"
31 #include "frame.h"
32 #include "session.h"
33 #include "event.h"
34 #include "grab.h"
35 #include "prompt.h"
36 #include "focus.h"
37 #include "stacking.h"
38 #include "openbox.h"
39 #include "group.h"
40 #include "config.h"
41 #include "menuframe.h"
42 #include "keyboard.h"
43 #include "mouse.h"
44 #include "render/render.h"
45 #include "gettext.h"
46
47 #ifdef HAVE_UNISTD_H
48 # include <unistd.h>
49 #endif
50
51 #ifdef HAVE_SIGNAL_H
52 # include <signal.h> /* for kill() */
53 #endif
54
55 #include <glib.h>
56 #include <X11/Xutil.h>
57
58 /*! The event mask to grab on client windows */
59 #define CLIENT_EVENTMASK (PropertyChangeMask | StructureNotifyMask | \
60 ColormapChangeMask)
61
62 #define CLIENT_NOPROPAGATEMASK (ButtonPressMask | ButtonReleaseMask | \
63 ButtonMotionMask)
64
65 typedef struct
66 {
67 ObClientCallback func;
68 gpointer data;
69 } ClientCallback;
70
71 GList *client_list = NULL;
72
73 static GSList *client_destroy_notifies = NULL;
74 static RrImage *client_default_icon = NULL;
75
76 static void client_get_all(ObClient *self, gboolean real);
77 static void client_get_startup_id(ObClient *self);
78 static void client_get_session_ids(ObClient *self);
79 static void client_get_area(ObClient *self);
80 static void client_get_desktop(ObClient *self);
81 static void client_get_state(ObClient *self);
82 static void client_get_shaped(ObClient *self);
83 static void client_get_mwm_hints(ObClient *self);
84 static void client_get_colormap(ObClient *self);
85 static void client_set_desktop_recursive(ObClient *self,
86 guint target,
87 gboolean donthide,
88 gboolean dontraise);
89 static void client_change_allowed_actions(ObClient *self);
90 static void client_change_state(ObClient *self);
91 static void client_change_wm_state(ObClient *self);
92 static void client_apply_startup_state(ObClient *self,
93 gint x, gint y, gint w, gint h);
94 static void client_restore_session_state(ObClient *self);
95 static gboolean client_restore_session_stacking(ObClient *self);
96 static ObAppSettings *client_get_settings_state(ObClient *self);
97 static void client_update_transient_tree(ObClient *self,
98 ObGroup *oldgroup, ObGroup *newgroup,
99 gboolean oldgtran, gboolean newgtran,
100 ObClient* oldparent,
101 ObClient *newparent);
102 static void client_present(ObClient *self, gboolean here, gboolean raise,
103 gboolean unshade);
104 static GSList *client_search_all_top_parents_internal(ObClient *self,
105 gboolean bylayer,
106 ObStackingLayer layer);
107 static void client_call_notifies(ObClient *self, GSList *list);
108 static void client_ping_event(ObClient *self, gboolean dead);
109 static void client_prompt_kill(ObClient *self);
110
111
112 void client_startup(gboolean reconfig)
113 {
114 if ((client_default_icon = RrImageCacheFind(ob_rr_icons,
115 ob_rr_theme->def_win_icon,
116 ob_rr_theme->def_win_icon_w,
117 ob_rr_theme->def_win_icon_h)))
118 RrImageRef(client_default_icon);
119 else {
120 client_default_icon = RrImageNew(ob_rr_icons);
121 RrImageAddPicture(client_default_icon,
122 ob_rr_theme->def_win_icon,
123 ob_rr_theme->def_win_icon_w,
124 ob_rr_theme->def_win_icon_h);
125 }
126
127 if (reconfig) return;
128
129 client_set_list();
130 }
131
132 void client_shutdown(gboolean reconfig)
133 {
134 RrImageUnref(client_default_icon);
135 client_default_icon = NULL;
136
137 if (reconfig) return;
138 }
139
140 static void client_call_notifies(ObClient *self, GSList *list)
141 {
142 GSList *it;
143
144 for (it = list; it; it = g_slist_next(it)) {
145 ClientCallback *d = it->data;
146 d->func(self, d->data);
147 }
148 }
149
150 void client_add_destroy_notify(ObClientCallback func, gpointer data)
151 {
152 ClientCallback *d = g_new(ClientCallback, 1);
153 d->func = func;
154 d->data = data;
155 client_destroy_notifies = g_slist_prepend(client_destroy_notifies, d);
156 }
157
158 void client_remove_destroy_notify(ObClientCallback func)
159 {
160 GSList *it;
161
162 for (it = client_destroy_notifies; it; it = g_slist_next(it)) {
163 ClientCallback *d = it->data;
164 if (d->func == func) {
165 g_free(d);
166 client_destroy_notifies =
167 g_slist_delete_link(client_destroy_notifies, it);
168 break;
169 }
170 }
171 }
172
173 void client_set_list(void)
174 {
175 Window *windows, *win_it;
176 GList *it;
177 guint size = g_list_length(client_list);
178
179 /* create an array of the window ids */
180 if (size > 0) {
181 windows = g_new(Window, size);
182 win_it = windows;
183 for (it = client_list; it; it = g_list_next(it), ++win_it)
184 *win_it = ((ObClient*)it->data)->window;
185 } else
186 windows = NULL;
187
188 PROP_SETA32(RootWindow(ob_display, ob_screen),
189 net_client_list, window, (gulong*)windows, size);
190
191 if (windows)
192 g_free(windows);
193
194 stacking_set_list();
195 }
196
197 void client_manage_all(void)
198 {
199 guint i, j, nchild;
200 Window w, *children;
201 XWMHints *wmhints;
202 XWindowAttributes attrib;
203
204 XQueryTree(ob_display, RootWindow(ob_display, ob_screen),
205 &w, &w, &children, &nchild);
206
207 /* remove all icon windows from the list */
208 for (i = 0; i < nchild; i++) {
209 if (children[i] == None) continue;
210 wmhints = XGetWMHints(ob_display, children[i]);
211 if (wmhints) {
212 if ((wmhints->flags & IconWindowHint) &&
213 (wmhints->icon_window != children[i]))
214 for (j = 0; j < nchild; j++)
215 if (children[j] == wmhints->icon_window) {
216 children[j] = None;
217 break;
218 }
219 XFree(wmhints);
220 }
221 }
222
223 /* manage windows in reverse order from how they were originally mapped.
224 this is an attempt to manage children windows before their parents, so
225 that when the parent is mapped, it can find the child */
226 for (i = 0; i < nchild; ++i) {
227 if (children[i] == None)
228 continue;
229 if (XGetWindowAttributes(ob_display, children[i], &attrib)) {
230 if (attrib.override_redirect) continue;
231
232 if (attrib.map_state != IsUnmapped)
233 client_manage(children[i], NULL);
234 }
235 }
236 XFree(children);
237 }
238
239 void client_manage(Window window, ObPrompt *prompt)
240 {
241 ObClient *self;
242 XEvent e;
243 XWindowAttributes attrib;
244 XSetWindowAttributes attrib_set;
245 XWMHints *wmhint;
246 gboolean activate = FALSE;
247 ObAppSettings *settings;
248 gboolean transient = FALSE;
249 Rect place, *monitor;
250 Time launch_time, map_time;
251
252 grab_server(TRUE);
253
254 /* check if it has already been unmapped by the time we started
255 mapping. the grab does a sync so we don't have to here */
256 if (XCheckTypedWindowEvent(ob_display, window, DestroyNotify, &e) ||
257 XCheckTypedWindowEvent(ob_display, window, UnmapNotify, &e))
258 {
259 XPutBackEvent(ob_display, &e);
260
261 ob_debug("Trying to manage unmapped window. Aborting that.\n");
262 grab_server(FALSE);
263 return; /* don't manage it */
264 }
265
266 /* make sure it isn't an override-redirect window */
267 if (!XGetWindowAttributes(ob_display, window, &attrib) ||
268 attrib.override_redirect)
269 {
270 grab_server(FALSE);
271 return; /* don't manage it */
272 }
273
274 /* is the window a docking app */
275 if ((wmhint = XGetWMHints(ob_display, window))) {
276 if ((wmhint->flags & StateHint) &&
277 wmhint->initial_state == WithdrawnState)
278 {
279 dock_add(window, wmhint);
280 grab_server(FALSE);
281 XFree(wmhint);
282 return;
283 }
284 XFree(wmhint);
285 }
286
287 ob_debug("Managing window: 0x%lx\n", window);
288
289 map_time = event_get_server_time();
290
291 /* choose the events we want to receive on the CLIENT window
292 (ObPrompt windows can request events too) */
293 attrib_set.event_mask = CLIENT_EVENTMASK |
294 (prompt ? prompt->event_mask : 0);
295 attrib_set.do_not_propagate_mask = CLIENT_NOPROPAGATEMASK;
296 XChangeWindowAttributes(ob_display, window,
297 CWEventMask|CWDontPropagate, &attrib_set);
298
299 /* create the ObClient struct, and populate it from the hints on the
300 window */
301 self = g_new0(ObClient, 1);
302 self->obwin.type = Window_Client;
303 self->window = window;
304 self->prompt = prompt;
305
306 /* non-zero defaults */
307 self->wmstate = WithdrawnState; /* make sure it gets updated first time */
308 self->gravity = NorthWestGravity;
309 self->desktop = screen_num_desktops; /* always an invalid value */
310
311 /* get all the stuff off the window */
312 client_get_all(self, TRUE);
313
314 ob_debug("Window type: %d\n", self->type);
315 ob_debug("Window group: 0x%x\n", self->group?self->group->leader:0);
316
317 /* now we have all of the window's information so we can set this up.
318 do this before creating the frame, so it can tell that we are still
319 mapping and doesn't go applying things right away */
320 client_setup_decor_and_functions(self, FALSE);
321
322 /* specify that if we exit, the window should not be destroyed and
323 should be reparented back to root automatically, unless we are managing
324 an internal ObPrompt window */
325 if (!self->prompt)
326 XChangeSaveSet(ob_display, window, SetModeInsert);
327
328 /* create the decoration frame for the client window */
329 self->frame = frame_new(self);
330
331 frame_grab_client(self->frame);
332
333 /* we've grabbed everything and set everything that we need to at mapping
334 time now */
335 grab_server(FALSE);
336
337 /* per-app settings override stuff from client_get_all, and return the
338 settings for other uses too. the returned settings is a shallow copy,
339 that needs to be freed with g_free(). */
340 settings = client_get_settings_state(self);
341 /* the session should get the last say though */
342 client_restore_session_state(self);
343
344 /* tell startup notification that this app started */
345 launch_time = sn_app_started(self->startup_id, self->class, self->name);
346
347 /* do this after we have a frame.. it uses the frame to help determine the
348 WM_STATE to apply. */
349 client_change_state(self);
350
351 /* add ourselves to the focus order */
352 focus_order_add_new(self);
353
354 /* do this to add ourselves to the stacking list in a non-intrusive way */
355 client_calc_layer(self);
356
357 /* focus the new window? */
358 if (ob_state() != OB_STATE_STARTING &&
359 (!self->session || self->session->focused) &&
360 /* this means focus=true for window is same as config_focus_new=true */
361 ((config_focus_new || (settings && settings->focus == 1)) ||
362 client_search_focus_tree_full(self)) &&
363 /* this checks for focus=false for the window */
364 (!settings || settings->focus != 0) &&
365 focus_valid_target(self, FALSE, FALSE, TRUE, FALSE, FALSE))
366 {
367 activate = TRUE;
368 }
369
370 /* remove the client's border */
371 XSetWindowBorderWidth(ob_display, self->window, 0);
372
373 /* adjust the frame to the client's size before showing or placing
374 the window */
375 frame_adjust_area(self->frame, FALSE, TRUE, FALSE);
376 frame_adjust_client_area(self->frame);
377
378 /* where the frame was placed is where the window was originally */
379 place = self->area;
380 monitor = screen_physical_area_monitor(screen_find_monitor(&place));
381
382 /* figure out placement for the window if the window is new */
383 if (ob_state() == OB_STATE_RUNNING) {
384 ob_debug("Positioned: %s @ %d %d\n",
385 (!self->positioned ? "no" :
386 (self->positioned == PPosition ? "program specified" :
387 (self->positioned == USPosition ? "user specified" :
388 (self->positioned == (PPosition | USPosition) ?
389 "program + user specified" :
390 "BADNESS !?")))), place.x, place.y);
391
392 ob_debug("Sized: %s @ %d %d\n",
393 (!self->sized ? "no" :
394 (self->sized == PSize ? "program specified" :
395 (self->sized == USSize ? "user specified" :
396 (self->sized == (PSize | USSize) ?
397 "program + user specified" :
398 "BADNESS !?")))), place.width, place.height);
399
400 /* splash screens are also returned as TRUE for transient,
401 and so will be forced on screen below */
402 transient = place_client(self, &place.x, &place.y, settings);
403
404 /* make sure the window is visible. */
405 client_find_onscreen(self, &place.x, &place.y,
406 place.width, place.height,
407 /* non-normal clients has less rules, and
408 windows that are being restored from a
409 session do also. we can assume you want
410 it back where you saved it. Clients saying
411 they placed themselves are subjected to
412 harder rules, ones that are placed by
413 place.c or by the user are allowed partially
414 off-screen and on xinerama divides (ie,
415 it is up to the placement routines to avoid
416 the xinerama divides)
417
418 splash screens get "transient" set to TRUE by
419 the place_client call
420 */
421 ob_state() == OB_STATE_RUNNING &&
422 (transient ||
423 (!((self->positioned & USPosition) ||
424 (settings && settings->pos_given)) &&
425 client_normal(self) &&
426 !self->session &&
427 /* don't move oldschool fullscreen windows to
428 fit inside the struts (fixes Acroread, which
429 makes its fullscreen window fit the screen
430 but it is not USSize'd or USPosition'd) */
431 !(self->decorations == 0 &&
432 RECT_EQUAL(place, *monitor)))));
433 }
434
435 /* if the window isn't user-sized, then make it fit inside
436 the visible screen area on its monitor. Use basically the same rules
437 for forcing the window on screen in the client_find_onscreen call.
438
439 do this after place_client, it chooses the monitor!
440
441 splash screens get "transient" set to TRUE by
442 the place_client call
443 */
444 if (ob_state() == OB_STATE_RUNNING &&
445 (transient ||
446 (!(self->sized & USSize || self->positioned & USPosition) &&
447 client_normal(self) &&
448 !self->session &&
449 /* don't shrink oldschool fullscreen windows to fit inside the
450 struts (fixes Acroread, which makes its fullscreen window
451 fit the screen but it is not USSize'd or USPosition'd) */
452 !(self->decorations == 0 && RECT_EQUAL(place, *monitor)))))
453 {
454 Rect *a = screen_area(self->desktop, SCREEN_AREA_ONE_MONITOR, &place);
455
456 /* get the size of the frame */
457 place.width += self->frame->size.left + self->frame->size.right;
458 place.height += self->frame->size.top + self->frame->size.bottom;
459
460 /* fit the window inside the area */
461 place.width = MIN(place.width, a->width);
462 place.height = MIN(place.height, a->height);
463
464 ob_debug("setting window size to %dx%d\n", place.width, place.height);
465
466 /* get the size of the client back */
467 place.width -= self->frame->size.left + self->frame->size.right;
468 place.height -= self->frame->size.top + self->frame->size.bottom;
469
470 g_free(a);
471 }
472
473 ob_debug("placing window 0x%x at %d, %d with size %d x %d. "
474 "some restrictions may apply\n",
475 self->window, place.x, place.y, place.width, place.height);
476 if (self->session)
477 ob_debug(" but session requested %d, %d %d x %d instead, "
478 "overriding\n",
479 self->session->x, self->session->y,
480 self->session->w, self->session->h);
481
482 /* do this after the window is placed, so the premax/prefullscreen numbers
483 won't be all wacko!!
484
485 this also places the window
486 */
487 client_apply_startup_state(self, place.x, place.y,
488 place.width, place.height);
489
490 g_free(monitor);
491 monitor = NULL;
492
493 ob_debug_type(OB_DEBUG_FOCUS, "Going to try activate new window? %s\n",
494 activate ? "yes" : "no");
495 if (activate) {
496 gboolean raise = FALSE;
497
498 /* This is focus stealing prevention */
499 ob_debug_type(OB_DEBUG_FOCUS,
500 "Want to focus new window 0x%x at time %u "
501 "launched at %u (last user interaction time %u)\n",
502 self->window, map_time, launch_time,
503 event_last_user_time);
504
505 if (menu_frame_visible || moveresize_in_progress) {
506 activate = FALSE;
507 raise = TRUE;
508 ob_debug_type(OB_DEBUG_FOCUS,
509 "Not focusing the window because the user is inside "
510 "an Openbox menu or is move/resizing a window and "
511 "we don't want to interrupt them\n");
512 }
513
514 /* if it's on another desktop */
515 else if (!(self->desktop == screen_desktop ||
516 self->desktop == DESKTOP_ALL) &&
517 /* the timestamp is from before you changed desktops */
518 launch_time && screen_desktop_user_time &&
519 !event_time_after(launch_time, screen_desktop_user_time))
520 {
521 activate = FALSE;
522 raise = TRUE;
523 ob_debug_type(OB_DEBUG_FOCUS,
524 "Not focusing the window because its on another "
525 "desktop\n");
526 }
527 /* If something is focused, and it's not our relative... */
528 else if (focus_client && client_search_focus_tree_full(self) == NULL &&
529 client_search_focus_group_full(self) == NULL)
530 {
531 /* If the user is working in another window right now, then don't
532 steal focus */
533 if (event_last_user_time && launch_time &&
534 event_time_after(event_last_user_time, launch_time) &&
535 event_last_user_time != launch_time &&
536 event_time_after(event_last_user_time,
537 map_time - OB_EVENT_USER_TIME_DELAY))
538 {
539 activate = FALSE;
540 ob_debug_type(OB_DEBUG_FOCUS,
541 "Not focusing the window because the user is "
542 "working in another window\n");
543 }
544 /* If it's a transient (and its parents aren't focused) */
545 else if (client_has_parent(self)) {
546 activate = FALSE;
547 ob_debug_type(OB_DEBUG_FOCUS,
548 "Not focusing the window because it is a "
549 "transient, and its relatives aren't focused\n");
550 }
551 /* Don't steal focus from globally active clients.
552 I stole this idea from KWin. It seems nice.
553 */
554 else if (!(focus_client->can_focus ||
555 focus_client->focus_notify))
556 {
557 activate = FALSE;
558 ob_debug_type(OB_DEBUG_FOCUS,
559 "Not focusing the window because a globally "
560 "active client has focus\n");
561 }
562 /* Don't move focus if it's not going to go to this window
563 anyway */
564 else if (client_focus_target(self) != self) {
565 activate = FALSE;
566 raise = TRUE;
567 ob_debug_type(OB_DEBUG_FOCUS,
568 "Not focusing the window because another window "
569 "would get the focus anyway\n");
570 }
571 else if (!(self->desktop == screen_desktop ||
572 self->desktop == DESKTOP_ALL))
573 {
574 activate = FALSE;
575 raise = TRUE;
576 ob_debug_type(OB_DEBUG_FOCUS,
577 "Not focusing the window because it is on "
578 "another desktop and no relatives are focused ");
579 }
580 }
581
582 if (!activate) {
583 ob_debug_type(OB_DEBUG_FOCUS,
584 "Focus stealing prevention activated for %s at "
585 "time %u (last user interaction time %u)\n",
586 self->title, map_time, event_last_user_time);
587 /* if the client isn't focused, then hilite it so the user
588 knows it is there */
589 client_hilite(self, TRUE);
590 /* we may want to raise it even tho we're not activating it */
591 if (raise && !client_restore_session_stacking(self))
592 stacking_raise(CLIENT_AS_WINDOW(self));
593 }
594 }
595 else {
596 /* This may look rather odd. Well it's because new windows are added
597 to the stacking order non-intrusively. If we're not going to focus
598 the new window or hilite it, then we raise it to the top. This will
599 take affect for things that don't get focused like splash screens.
600 Also if you don't have focus_new enabled, then it's going to get
601 raised to the top. Legacy begets legacy I guess?
602 */
603 if (!client_restore_session_stacking(self))
604 stacking_raise(CLIENT_AS_WINDOW(self));
605 }
606
607 mouse_grab_for_client(self, TRUE);
608
609 /* this has to happen before we try focus the window, but we want it to
610 happen after the client's stacking has been determined or it looks bad
611 */
612 {
613 gulong ignore_start;
614 if (!config_focus_under_mouse)
615 ignore_start = event_start_ignore_all_enters();
616
617 client_show(self);
618
619 if (!config_focus_under_mouse)
620 event_end_ignore_all_enters(ignore_start);
621 }
622
623 if (activate) {
624 gboolean stacked = client_restore_session_stacking(self);
625 client_present(self, FALSE, !stacked, TRUE);
626 }
627
628 /* add to client list/map */
629 client_list = g_list_append(client_list, self);
630 g_hash_table_insert(window_map, &self->window, self);
631
632 /* this has to happen after we're in the client_list */
633 if (STRUT_EXISTS(self->strut))
634 screen_update_areas();
635
636 /* update the list hints */
637 client_set_list();
638
639 /* free the ObAppSettings shallow copy */
640 g_free(settings);
641
642 ob_debug("Managed window 0x%lx plate 0x%x (%s)\n",
643 window, self->frame->window, self->class);
644
645 return;
646 }
647
648
649 ObClient *client_fake_manage(Window window)
650 {
651 ObClient *self;
652 ObAppSettings *settings;
653
654 ob_debug("Pretend-managing window: %lx\n", window);
655
656 /* do this minimal stuff to figure out the client's decorations */
657
658 self = g_new0(ObClient, 1);
659 self->window = window;
660
661 client_get_all(self, FALSE);
662 /* per-app settings override stuff, and return the settings for other
663 uses too. this returns a shallow copy that needs to be freed */
664 settings = client_get_settings_state(self);
665
666 client_setup_decor_and_functions(self, FALSE);
667
668 /* create the decoration frame for the client window and adjust its size */
669 self->frame = frame_new(self);
670 frame_adjust_area(self->frame, FALSE, TRUE, TRUE);
671
672 ob_debug("gave extents left %d right %d top %d bottom %d\n",
673 self->frame->size.left, self->frame->size.right,
674 self->frame->size.top, self->frame->size.bottom);
675
676 /* free the ObAppSettings shallow copy */
677 g_free(settings);
678
679 return self;
680 }
681
682 void client_unmanage_all(void)
683 {
684 while (client_list)
685 client_unmanage(client_list->data);
686 }
687
688 void client_unmanage(ObClient *self)
689 {
690 GSList *it;
691 gulong ignore_start;
692
693 ob_debug("Unmanaging window: 0x%x plate 0x%x (%s) (%s)\n",
694 self->window, self->frame->window,
695 self->class, self->title ? self->title : "");
696
697 g_assert(self != NULL);
698
699 /* we dont want events no more. do this before hiding the frame so we
700 don't generate more events */
701 XSelectInput(ob_display, self->window, NoEventMask);
702
703 /* ignore enter events from the unmap so it doesnt mess with the focus */
704 if (!config_focus_under_mouse)
705 ignore_start = event_start_ignore_all_enters();
706
707 frame_hide(self->frame);
708 /* flush to send the hide to the server quickly */
709 XFlush(ob_display);
710
711 if (!config_focus_under_mouse)
712 event_end_ignore_all_enters(ignore_start);
713
714 mouse_grab_for_client(self, FALSE);
715
716 /* remove the window from our save set, unless we are managing an internal
717 ObPrompt window */
718 if (!self->prompt)
719 XChangeSaveSet(ob_display, self->window, SetModeDelete);
720
721 /* update the focus lists */
722 focus_order_remove(self);
723 if (client_focused(self)) {
724 /* don't leave an invalid focus_client */
725 focus_client = NULL;
726 }
727
728 /* if we're prompting to kill the client, close that */
729 prompt_unref(self->kill_prompt);
730 self->kill_prompt = NULL;
731
732 client_list = g_list_remove(client_list, self);
733 stacking_remove(self);
734 g_hash_table_remove(window_map, &self->window);
735
736 /* once the client is out of the list, update the struts to remove its
737 influence */
738 if (STRUT_EXISTS(self->strut))
739 screen_update_areas();
740
741 client_call_notifies(self, client_destroy_notifies);
742
743 /* tell our parent(s) that we're gone */
744 for (it = self->parents; it; it = g_slist_next(it))
745 ((ObClient*)it->data)->transients =
746 g_slist_remove(((ObClient*)it->data)->transients,self);
747
748 /* tell our transients that we're gone */
749 for (it = self->transients; it; it = g_slist_next(it)) {
750 ((ObClient*)it->data)->parents =
751 g_slist_remove(((ObClient*)it->data)->parents, self);
752 /* we could be keeping our children in a higher layer */
753 client_calc_layer(it->data);
754 }
755
756 /* remove from its group */
757 if (self->group) {
758 group_remove(self->group, self);
759 self->group = NULL;
760 }
761
762 /* restore the window's original geometry so it is not lost */
763 {
764 Rect a;
765
766 a = self->area;
767
768 if (self->fullscreen)
769 a = self->pre_fullscreen_area;
770 else if (self->max_horz || self->max_vert) {
771 if (self->max_horz) {
772 a.x = self->pre_max_area.x;
773 a.width = self->pre_max_area.width;
774 }
775 if (self->max_vert) {
776 a.y = self->pre_max_area.y;
777 a.height = self->pre_max_area.height;
778 }
779 }
780
781 self->fullscreen = self->max_horz = self->max_vert = FALSE;
782 /* let it be moved and resized no matter what */
783 self->functions = OB_CLIENT_FUNC_MOVE | OB_CLIENT_FUNC_RESIZE;
784 self->decorations = 0; /* unmanaged windows have no decor */
785
786 /* give the client its border back */
787 XSetWindowBorderWidth(ob_display, self->window, self->border_width);
788
789 client_move_resize(self, a.x, a.y, a.width, a.height);
790 }
791
792 /* reparent the window out of the frame, and free the frame */
793 frame_release_client(self->frame);
794 frame_free(self->frame);
795 self->frame = NULL;
796
797 if (ob_state() != OB_STATE_EXITING) {
798 /* these values should not be persisted across a window
799 unmapping/mapping */
800 PROP_ERASE(self->window, net_wm_desktop);
801 PROP_ERASE(self->window, net_wm_state);
802 PROP_ERASE(self->window, wm_state);
803 } else {
804 /* if we're left in an unmapped state, the client wont be mapped.
805 this is bad, since we will no longer be managing the window on
806 restart */
807 XMapWindow(ob_display, self->window);
808 }
809
810 /* these should not be left on the window ever. other window managers
811 don't necessarily use them and it will mess them up (like compiz) */
812 PROP_ERASE(self->window, net_wm_visible_name);
813 PROP_ERASE(self->window, net_wm_visible_icon_name);
814
815 /* update the list hints */
816 client_set_list();
817
818 ob_debug("Unmanaged window 0x%lx\n", self->window);
819
820 /* free all data allocated in the client struct */
821 RrImageUnref(self->icon_set);
822 g_slist_free(self->transients);
823 g_free(self->startup_id);
824 g_free(self->wm_command);
825 g_free(self->title);
826 g_free(self->icon_title);
827 g_free(self->original_title);
828 g_free(self->name);
829 g_free(self->class);
830 g_free(self->role);
831 g_free(self->client_machine);
832 g_free(self->sm_client_id);
833 g_free(self);
834 }
835
836 void client_fake_unmanage(ObClient *self)
837 {
838 /* this is all that got allocated to get the decorations */
839
840 frame_free(self->frame);
841 g_free(self);
842 }
843
844 /*! Returns a new structure containing the per-app settings for this client.
845 The returned structure needs to be freed with g_free. */
846 static ObAppSettings *client_get_settings_state(ObClient *self)
847 {
848 ObAppSettings *settings;
849 GSList *it;
850
851 settings = config_create_app_settings();
852
853 for (it = config_per_app_settings; it; it = g_slist_next(it)) {
854 ObAppSettings *app = it->data;
855 gboolean match = TRUE;
856
857 g_assert(app->name != NULL || app->class != NULL);
858
859 /* we know that either name or class is not NULL so it will have to
860 match to use the rule */
861 if (app->name &&
862 !g_pattern_match(app->name, strlen(self->name), self->name, NULL))
863 match = FALSE;
864 else if (app->class &&
865 !g_pattern_match(app->class,
866 strlen(self->class), self->class, NULL))
867 match = FALSE;
868 else if (app->role &&
869 !g_pattern_match(app->role,
870 strlen(self->role), self->role, NULL))
871 match = FALSE;
872 else if ((signed)app->type >= 0 && app->type != self->type)
873 match = FALSE;
874
875 if (match) {
876 ob_debug("Window matching: %s\n", app->name);
877
878 /* copy the settings to our struct, overriding the existing
879 settings if they are not defaults */
880 config_app_settings_copy_non_defaults(app, settings);
881 }
882 }
883
884 if (settings->shade != -1)
885 self->shaded = !!settings->shade;
886 if (settings->decor != -1)
887 self->undecorated = !settings->decor;
888 if (settings->iconic != -1)
889 self->iconic = !!settings->iconic;
890 if (settings->skip_pager != -1)
891 self->skip_pager = !!settings->skip_pager;
892 if (settings->skip_taskbar != -1)
893 self->skip_taskbar = !!settings->skip_taskbar;
894
895 if (settings->max_vert != -1)
896 self->max_vert = !!settings->max_vert;
897 if (settings->max_horz != -1)
898 self->max_horz = !!settings->max_horz;
899
900 if (settings->fullscreen != -1)
901 self->fullscreen = !!settings->fullscreen;
902
903 if (settings->desktop) {
904 if (settings->desktop == DESKTOP_ALL)
905 self->desktop = settings->desktop;
906 else if (settings->desktop > 0 &&
907 settings->desktop <= screen_num_desktops)
908 self->desktop = settings->desktop - 1;
909 }
910
911 if (settings->layer == -1) {
912 self->below = TRUE;
913 self->above = FALSE;
914 }
915 else if (settings->layer == 0) {
916 self->below = FALSE;
917 self->above = FALSE;
918 }
919 else if (settings->layer == 1) {
920 self->below = FALSE;
921 self->above = TRUE;
922 }
923 return settings;
924 }
925
926 static void client_restore_session_state(ObClient *self)
927 {
928 GList *it;
929
930 ob_debug_type(OB_DEBUG_SM,
931 "Restore session for client %s\n", self->title);
932
933 if (!(it = session_state_find(self))) {
934 ob_debug_type(OB_DEBUG_SM,
935 "Session data not found for client %s\n", self->title);
936 return;
937 }
938
939 self->session = it->data;
940
941 ob_debug_type(OB_DEBUG_SM, "Session data loaded for client %s\n",
942 self->title);
943
944 RECT_SET_POINT(self->area, self->session->x, self->session->y);
945 self->positioned = USPosition;
946 self->sized = USSize;
947 if (self->session->w > 0)
948 self->area.width = self->session->w;
949 if (self->session->h > 0)
950 self->area.height = self->session->h;
951 XResizeWindow(ob_display, self->window,
952 self->area.width, self->area.height);
953
954 self->desktop = (self->session->desktop == DESKTOP_ALL ?
955 self->session->desktop :
956 MIN(screen_num_desktops - 1, self->session->desktop));
957 PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
958
959 self->shaded = self->session->shaded;
960 self->iconic = self->session->iconic;
961 self->skip_pager = self->session->skip_pager;
962 self->skip_taskbar = self->session->skip_taskbar;
963 self->fullscreen = self->session->fullscreen;
964 self->above = self->session->above;
965 self->below = self->session->below;
966 self->max_horz = self->session->max_horz;
967 self->max_vert = self->session->max_vert;
968 self->undecorated = self->session->undecorated;
969 }
970
971 static gboolean client_restore_session_stacking(ObClient *self)
972 {
973 GList *it, *mypos;
974
975 if (!self->session) return FALSE;
976
977 mypos = g_list_find(session_saved_state, self->session);
978 if (!mypos) return FALSE;
979
980 /* start above me and look for the first client */
981 for (it = g_list_previous(mypos); it; it = g_list_previous(it)) {
982 GList *cit;
983
984 for (cit = client_list; cit; cit = g_list_next(cit)) {
985 ObClient *c = cit->data;
986 /* found a client that was in the session, so go below it */
987 if (c->session == it->data) {
988 stacking_below(CLIENT_AS_WINDOW(self),
989 CLIENT_AS_WINDOW(cit->data));
990 return TRUE;
991 }
992 }
993 }
994 return FALSE;
995 }
996
997 void client_move_onscreen(ObClient *self, gboolean rude)
998 {
999 gint x = self->area.x;
1000 gint y = self->area.y;
1001 if (client_find_onscreen(self, &x, &y,
1002 self->area.width,
1003 self->area.height, rude)) {
1004 client_move(self, x, y);
1005 }
1006 }
1007
1008 gboolean client_find_onscreen(ObClient *self, gint *x, gint *y, gint w, gint h,
1009 gboolean rude)
1010 {
1011 gint ox = *x, oy = *y;
1012 gboolean rudel = rude, ruder = rude, rudet = rude, rudeb = rude;
1013 gint fw, fh;
1014 Rect desired;
1015 guint i;
1016 gboolean found_mon;
1017
1018 RECT_SET(desired, *x, *y, w, h);
1019 frame_rect_to_frame(self->frame, &desired);
1020
1021 /* get where the frame would be */
1022 frame_client_gravity(self->frame, x, y);
1023
1024 /* get the requested size of the window with decorations */
1025 fw = self->frame->size.left + w + self->frame->size.right;
1026 fh = self->frame->size.top + h + self->frame->size.bottom;
1027
1028 /* If rudeness wasn't requested, then still be rude in a given direction
1029 if the client is not moving, only resizing in that direction */
1030 if (!rude) {
1031 Point oldtl, oldtr, oldbl, oldbr;
1032 Point newtl, newtr, newbl, newbr;
1033 gboolean stationary_l, stationary_r, stationary_t, stationary_b;
1034
1035 POINT_SET(oldtl, self->frame->area.x, self->frame->area.y);
1036 POINT_SET(oldbr, self->frame->area.x + self->frame->area.width - 1,
1037 self->frame->area.y + self->frame->area.height - 1);
1038 POINT_SET(oldtr, oldbr.x, oldtl.y);
1039 POINT_SET(oldbl, oldtl.x, oldbr.y);
1040
1041 POINT_SET(newtl, *x, *y);
1042 POINT_SET(newbr, *x + fw - 1, *y + fh - 1);
1043 POINT_SET(newtr, newbr.x, newtl.y);
1044 POINT_SET(newbl, newtl.x, newbr.y);
1045
1046 /* is it moving or just resizing from some corner? */
1047 stationary_l = oldtl.x == newtl.x;
1048 stationary_r = oldtr.x == newtr.x;
1049 stationary_t = oldtl.y == newtl.y;
1050 stationary_b = oldbl.y == newbl.y;
1051
1052 /* if left edge is growing and didnt move right edge */
1053 if (stationary_r && newtl.x < oldtl.x)
1054 rudel = TRUE;
1055 /* if right edge is growing and didnt move left edge */
1056 if (stationary_l && newtr.x > oldtr.x)
1057 ruder = TRUE;
1058 /* if top edge is growing and didnt move bottom edge */
1059 if (stationary_b && newtl.y < oldtl.y)
1060 rudet = TRUE;
1061 /* if bottom edge is growing and didnt move top edge */
1062 if (stationary_t && newbl.y > oldbl.y)
1063 rudeb = TRUE;
1064 }
1065
1066 /* we iterate through every monitor that the window is at least partially
1067 on, to make sure it is obeying the rules on them all
1068
1069 if the window does not appear on any monitors, then use the first one
1070 */
1071 found_mon = FALSE;
1072 for (i = 0; i < screen_num_monitors; ++i) {
1073 Rect *a;
1074
1075 if (!screen_physical_area_monitor_contains(i, &desired)) {
1076 if (i < screen_num_monitors - 1 || found_mon)
1077 continue;
1078
1079 /* the window is not inside any monitor! so just use the first
1080 one */
1081 a = screen_area(self->desktop, 0, NULL);
1082 } else {
1083 found_mon = TRUE;
1084 a = screen_area(self->desktop, SCREEN_AREA_ONE_MONITOR, &desired);
1085 }
1086
1087 /* This makes sure windows aren't entirely outside of the screen so you
1088 can't see them at all.
1089 It makes sure 10% of the window is on the screen at least. And don't
1090 let it move itself off the top of the screen, which would hide the
1091 titlebar on you. (The user can still do this if they want too, it's
1092 only limiting the application.
1093 */
1094 if (client_normal(self)) {
1095 if (!self->strut.right && *x + fw/10 >= a->x + a->width - 1)
1096 *x = a->x + a->width - fw/10;
1097 if (!self->strut.bottom && *y + fh/10 >= a->y + a->height - 1)
1098 *y = a->y + a->height - fh/10;
1099 if (!self->strut.left && *x + fw*9/10 - 1 < a->x)
1100 *x = a->x - fw*9/10;
1101 if (!self->strut.top && *y + fh*9/10 - 1 < a->y)
1102 *y = a->y - fh*9/10;
1103 }
1104
1105 /* This here doesn't let windows even a pixel outside the
1106 struts/screen. When called from client_manage, programs placing
1107 themselves are forced completely onscreen, while things like
1108 xterm -geometry resolution-width/2 will work fine. Trying to
1109 place it completely offscreen will be handled in the above code.
1110 Sorry for this confused comment, i am tired. */
1111 if (rudel && !self->strut.left && *x < a->x) *x = a->x;
1112 if (ruder && !self->strut.right && *x + fw > a->x + a->width)
1113 *x = a->x + MAX(0, a->width - fw);
1114
1115 if (rudet && !self->strut.top && *y < a->y) *y = a->y;
1116 if (rudeb && !self->strut.bottom && *y + fh > a->y + a->height)
1117 *y = a->y + MAX(0, a->height - fh);
1118
1119 g_free(a);
1120 }
1121
1122 /* get where the client should be */
1123 frame_frame_gravity(self->frame, x, y);
1124
1125 return ox != *x || oy != *y;
1126 }
1127
1128 static void client_get_all(ObClient *self, gboolean real)
1129 {
1130 /* this is needed for the frame to set itself up */
1131 client_get_area(self);
1132
1133 /* these things can change the decor and functions of the window */
1134
1135 client_get_mwm_hints(self);
1136 /* this can change the mwmhints for special cases */
1137 client_get_type_and_transientness(self);
1138 client_get_state(self);
1139 client_update_normal_hints(self);
1140
1141 /* get the session related properties, these can change decorations
1142 from per-app settings */
1143 client_get_session_ids(self);
1144
1145 /* now we got everything that can affect the decorations */
1146 if (!real)
1147 return;
1148
1149 /* get this early so we have it for debugging */
1150 client_update_title(self);
1151
1152 client_update_protocols(self);
1153
1154 client_update_wmhints(self);
1155 /* this may have already been called from client_update_wmhints */
1156 if (!self->parents && !self->transient_for_group)
1157 client_update_transient_for(self);
1158
1159 client_get_startup_id(self);
1160 client_get_desktop(self);/* uses transient data/group/startup id if a
1161 desktop is not specified */
1162 client_get_shaped(self);
1163
1164 {
1165 /* a couple type-based defaults for new windows */
1166
1167 /* this makes sure that these windows appear on all desktops */
1168 if (self->type == OB_CLIENT_TYPE_DESKTOP)
1169 self->desktop = DESKTOP_ALL;
1170 }
1171
1172 #ifdef SYNC
1173 client_update_sync_request_counter(self);
1174 #endif
1175
1176 client_get_colormap(self);
1177 client_update_strut(self);
1178 client_update_icons(self);
1179 client_update_icon_geometry(self);
1180 }
1181
1182 static void client_get_startup_id(ObClient *self)
1183 {
1184 if (!(PROP_GETS(self->window, net_startup_id, utf8, &self->startup_id)))
1185 if (self->group)
1186 PROP_GETS(self->group->leader,
1187 net_startup_id, utf8, &self->startup_id);
1188 }
1189
1190 static void client_get_area(ObClient *self)
1191 {
1192 XWindowAttributes wattrib;
1193 Status ret;
1194
1195 ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
1196 g_assert(ret != BadWindow);
1197
1198 RECT_SET(self->area, wattrib.x, wattrib.y, wattrib.width, wattrib.height);
1199 POINT_SET(self->root_pos, wattrib.x, wattrib.y);
1200 self->border_width = wattrib.border_width;
1201
1202 ob_debug("client area: %d %d %d %d bw %d\n", wattrib.x, wattrib.y,
1203 wattrib.width, wattrib.height, wattrib.border_width);
1204 }
1205
1206 static void client_get_desktop(ObClient *self)
1207 {
1208 guint32 d = screen_num_desktops; /* an always-invalid value */
1209
1210 if (PROP_GET32(self->window, net_wm_desktop, cardinal, &d)) {
1211 if (d >= screen_num_desktops && d != DESKTOP_ALL)
1212 self->desktop = screen_num_desktops - 1;
1213 else
1214 self->desktop = d;
1215 ob_debug("client requested desktop 0x%x\n", self->desktop);
1216 } else {
1217 GSList *it;
1218 gboolean first = TRUE;
1219 guint all = screen_num_desktops; /* not a valid value */
1220
1221 /* if they are all on one desktop, then open it on the
1222 same desktop */
1223 for (it = self->parents; it; it = g_slist_next(it)) {
1224 ObClient *c = it->data;
1225
1226 if (c->desktop == DESKTOP_ALL) continue;
1227
1228 if (first) {
1229 all = c->desktop;
1230 first = FALSE;
1231 }
1232 else if (all != c->desktop)
1233 all = screen_num_desktops; /* make it invalid */
1234 }
1235 if (all != screen_num_desktops) {
1236 self->desktop = all;
1237
1238 ob_debug("client desktop set from parents: 0x%x\n",
1239 self->desktop);
1240 }
1241 /* try get from the startup-notification protocol */
1242 else if (sn_get_desktop(self->startup_id, &self->desktop)) {
1243 if (self->desktop >= screen_num_desktops &&
1244 self->desktop != DESKTOP_ALL)
1245 self->desktop = screen_num_desktops - 1;
1246 ob_debug("client desktop set from startup-notification: 0x%x\n",
1247 self->desktop);
1248 }
1249 /* defaults to the current desktop */
1250 else {
1251 self->desktop = screen_desktop;
1252 ob_debug("client desktop set to the current desktop: %d\n",
1253 self->desktop);
1254 }
1255 }
1256 }
1257
1258 static void client_get_state(ObClient *self)
1259 {
1260 guint32 *state;
1261 guint num;
1262
1263 if (PROP_GETA32(self->window, net_wm_state, atom, &state, &num)) {
1264 gulong i;
1265 for (i = 0; i < num; ++i) {
1266 if (state[i] == prop_atoms.net_wm_state_modal)
1267 self->modal = TRUE;
1268 else if (state[i] == prop_atoms.net_wm_state_shaded)
1269 self->shaded = TRUE;
1270 else if (state[i] == prop_atoms.net_wm_state_hidden)
1271 self->iconic = TRUE;
1272 else if (state[i] == prop_atoms.net_wm_state_skip_taskbar)
1273 self->skip_taskbar = TRUE;
1274 else if (state[i] == prop_atoms.net_wm_state_skip_pager)
1275 self->skip_pager = TRUE;
1276 else if (state[i] == prop_atoms.net_wm_state_fullscreen)
1277 self->fullscreen = TRUE;
1278 else if (state[i] == prop_atoms.net_wm_state_maximized_vert)
1279 self->max_vert = TRUE;
1280 else if (state[i] == prop_atoms.net_wm_state_maximized_horz)
1281 self->max_horz = TRUE;
1282 else if (state[i] == prop_atoms.net_wm_state_above)
1283 self->above = TRUE;
1284 else if (state[i] == prop_atoms.net_wm_state_below)
1285 self->below = TRUE;
1286 else if (state[i] == prop_atoms.net_wm_state_demands_attention)
1287 self->demands_attention = TRUE;
1288 else if (state[i] == prop_atoms.ob_wm_state_undecorated)
1289 self->undecorated = TRUE;
1290 }
1291
1292 g_free(state);
1293 }
1294 }
1295
1296 static void client_get_shaped(ObClient *self)
1297 {
1298 self->shaped = FALSE;
1299 #ifdef SHAPE
1300 if (extensions_shape) {
1301 gint foo;
1302 guint ufoo;
1303 gint s;
1304
1305 XShapeSelectInput(ob_display, self->window, ShapeNotifyMask);
1306
1307 XShapeQueryExtents(ob_display, self->window, &s, &foo,
1308 &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
1309 &ufoo);
1310 self->shaped = !!s;
1311 }
1312 #endif
1313 }
1314
1315 void client_update_transient_for(ObClient *self)
1316 {
1317 Window t = None;
1318 ObClient *target = NULL;
1319 gboolean trangroup = FALSE;
1320
1321 if (XGetTransientForHint(ob_display, self->window, &t)) {
1322 if (t != self->window) { /* can't be transient to itself! */
1323 target = g_hash_table_lookup(window_map, &t);
1324 /* if this happens then we need to check for it */
1325 g_assert(target != self);
1326 if (target && !WINDOW_IS_CLIENT(target)) {
1327 /* watch out for windows with a parent that is something
1328 different, like a dockapp for example */
1329 target = NULL;
1330 }
1331 }
1332
1333 /* Setting the transient_for to Root is actually illegal, however
1334 applications from time have done this to specify transient for
1335 their group */
1336 if (!target && self->group && t == RootWindow(ob_display, ob_screen))
1337 trangroup = TRUE;
1338 } else if (self->group && self->transient)
1339 trangroup = TRUE;
1340
1341 client_update_transient_tree(self, self->group, self->group,
1342 self->transient_for_group, trangroup,
1343 client_direct_parent(self), target);
1344 self->transient_for_group = trangroup;
1345
1346 }
1347
1348 static void client_update_transient_tree(ObClient *self,
1349 ObGroup *oldgroup, ObGroup *newgroup,
1350 gboolean oldgtran, gboolean newgtran,
1351 ObClient* oldparent,
1352 ObClient *newparent)
1353 {
1354 GSList *it, *next;
1355 ObClient *c;
1356
1357 g_assert(!oldgtran || oldgroup);
1358 g_assert(!newgtran || newgroup);
1359 g_assert((!oldgtran && !oldparent) ||
1360 (oldgtran && !oldparent) ||
1361 (!oldgtran && oldparent));
1362 g_assert((!newgtran && !newparent) ||
1363 (newgtran && !newparent) ||
1364 (!newgtran && newparent));
1365
1366 /* * *
1367 Group transient windows are not allowed to have other group
1368 transient windows as their children.
1369 * * */
1370
1371
1372 /* No change has occured */
1373 if (oldgroup == newgroup &&
1374 oldgtran == newgtran &&
1375 oldparent == newparent) return;
1376
1377 /** Remove the client from the transient tree **/
1378
1379 for (it = self->transients; it; it = next) {
1380 next = g_slist_next(it);
1381 c = it->data;
1382 self->transients = g_slist_delete_link(self->transients, it);
1383 c->parents = g_slist_remove(c->parents, self);
1384 }
1385 for (it = self->parents; it; it = next) {
1386 next = g_slist_next(it);
1387 c = it->data;
1388 self->parents = g_slist_delete_link(self->parents, it);
1389 c->transients = g_slist_remove(c->transients, self);
1390 }
1391
1392 /** Re-add the client to the transient tree **/
1393
1394 /* If we're transient for a group then we need to add ourselves to all our
1395 parents */
1396 if (newgtran) {
1397 for (it = newgroup->members; it; it = g_slist_next(it)) {
1398 c = it->data;
1399 if (c != self &&
1400 !client_search_top_direct_parent(c)->transient_for_group &&
1401 client_normal(c))
1402 {
1403 c->transients = g_slist_prepend(c->transients, self);
1404 self->parents = g_slist_prepend(self->parents, c);
1405 }
1406 }
1407 }
1408
1409 /* If we are now transient for a single window we need to add ourselves to
1410 its children
1411
1412 WARNING: Cyclical transient-ness is possible if two windows are
1413 transient for eachother.
1414 */
1415 else if (newparent &&
1416 /* don't make ourself its child if it is already our child */
1417 !client_is_direct_child(self, newparent) &&
1418 client_normal(newparent))
1419 {
1420 newparent->transients = g_slist_prepend(newparent->transients, self);
1421 self->parents = g_slist_prepend(self->parents, newparent);
1422 }
1423
1424 /* Add any group transient windows to our children. But if we're transient
1425 for the group, then other group transients are not our children.
1426
1427 WARNING: Cyclical transient-ness is possible. For e.g. if:
1428 A is transient for the group
1429 B is transient for A
1430 C is transient for B
1431 A can't be transient for C or we have a cycle
1432 */
1433 if (!newgtran && newgroup &&
1434 (!newparent ||
1435 !client_search_top_direct_parent(newparent)->transient_for_group) &&
1436 client_normal(self))
1437 {
1438 for (it = newgroup->members; it; it = g_slist_next(it)) {
1439 c = it->data;
1440 if (c != self && c->transient_for_group &&
1441 /* Don't make it our child if it is already our parent */
1442 !client_is_direct_child(c, self))
1443 {
1444 self->transients = g_slist_prepend(self->transients, c);
1445 c->parents = g_slist_prepend(c->parents, self);
1446 }
1447 }
1448 }
1449
1450 /** If we change our group transient-ness, our children change their
1451 effective group transient-ness, which affects how they relate to other
1452 group windows **/
1453
1454 for (it = self->transients; it; it = g_slist_next(it)) {
1455 c = it->data;
1456 if (!c->transient_for_group)
1457 client_update_transient_tree(c, c->group, c->group,
1458 c->transient_for_group,
1459 c->transient_for_group,
1460 client_direct_parent(c),
1461 client_direct_parent(c));
1462 }
1463 }
1464
1465 static void client_get_mwm_hints(ObClient *self)
1466 {
1467 guint num;
1468 guint32 *hints;
1469
1470 self->mwmhints.flags = 0; /* default to none */
1471
1472 if (PROP_GETA32(self->window, motif_wm_hints, motif_wm_hints,
1473 &hints, &num)) {
1474 if (num >= OB_MWM_ELEMENTS) {
1475 self->mwmhints.flags = hints[0];
1476 self->mwmhints.functions = hints[1];
1477 self->mwmhints.decorations = hints[2];
1478 }
1479 g_free(hints);
1480 }
1481 }
1482
1483 void client_get_type_and_transientness(ObClient *self)
1484 {
1485 guint num, i;
1486 guint32 *val;
1487 Window t;
1488
1489 self->type = -1;
1490 self->transient = FALSE;
1491
1492 if (PROP_GETA32(self->window, net_wm_window_type, atom, &val, &num)) {
1493 /* use the first value that we know about in the array */
1494 for (i = 0; i < num; ++i) {
1495 if (val[i] == prop_atoms.net_wm_window_type_desktop)
1496 self->type = OB_CLIENT_TYPE_DESKTOP;
1497 else if (val[i] == prop_atoms.net_wm_window_type_dock)
1498 self->type = OB_CLIENT_TYPE_DOCK;
1499 else if (val[i] == prop_atoms.net_wm_window_type_toolbar)
1500 self->type = OB_CLIENT_TYPE_TOOLBAR;
1501 else if (val[i] == prop_atoms.net_wm_window_type_menu)
1502 self->type = OB_CLIENT_TYPE_MENU;
1503 else if (val[i] == prop_atoms.net_wm_window_type_utility)
1504 self->type = OB_CLIENT_TYPE_UTILITY;
1505 else if (val[i] == prop_atoms.net_wm_window_type_splash)
1506 self->type = OB_CLIENT_TYPE_SPLASH;
1507 else if (val[i] == prop_atoms.net_wm_window_type_dialog)
1508 self->type = OB_CLIENT_TYPE_DIALOG;
1509 else if (val[i] == prop_atoms.net_wm_window_type_normal)
1510 self->type = OB_CLIENT_TYPE_NORMAL;
1511 else if (val[i] == prop_atoms.kde_net_wm_window_type_override) {
1512 /* prevent this window from getting any decor or
1513 functionality */
1514 self->mwmhints.flags &= (OB_MWM_FLAG_FUNCTIONS |
1515 OB_MWM_FLAG_DECORATIONS);
1516 self->mwmhints.decorations = 0;
1517 self->mwmhints.functions = 0;
1518 }
1519 if (self->type != (ObClientType) -1)
1520 break; /* grab the first legit type */
1521 }
1522 g_free(val);
1523 }
1524
1525 if (XGetTransientForHint(ob_display, self->window, &t))
1526 self->transient = TRUE;
1527
1528 if (self->type == (ObClientType) -1) {
1529 /*the window type hint was not set, which means we either classify
1530 ourself as a normal window or a dialog, depending on if we are a
1531 transient. */
1532 if (self->transient)
1533 self->type = OB_CLIENT_TYPE_DIALOG;
1534 else
1535 self->type = OB_CLIENT_TYPE_NORMAL;
1536 }
1537
1538 /* then, based on our type, we can update our transientness.. */
1539 if (self->type == OB_CLIENT_TYPE_DIALOG ||
1540 self->type == OB_CLIENT_TYPE_TOOLBAR ||
1541 self->type == OB_CLIENT_TYPE_MENU ||
1542 self->type == OB_CLIENT_TYPE_UTILITY)
1543 {
1544 self->transient = TRUE;
1545 }
1546 }
1547
1548 void client_update_protocols(ObClient *self)
1549 {
1550 guint32 *proto;
1551 guint num_return, i;
1552
1553 self->focus_notify = FALSE;
1554 self->delete_window = FALSE;
1555
1556 if (PROP_GETA32(self->window, wm_protocols, atom, &proto, &num_return)) {
1557 for (i = 0; i < num_return; ++i) {
1558 if (proto[i] == prop_atoms.wm_delete_window)
1559 /* this means we can request the window to close */
1560 self->delete_window = TRUE;
1561 else if (proto[i] == prop_atoms.wm_take_focus)
1562 /* if this protocol is requested, then the window will be
1563 notified whenever we want it to receive focus */
1564 self->focus_notify = TRUE;
1565 else if (proto[i] == prop_atoms.net_wm_ping)
1566 /* if this protocol is requested, then the window will allow
1567 pings to determine if it is still alive */
1568 self->ping = TRUE;
1569 #ifdef SYNC
1570 else if (proto[i] == prop_atoms.net_wm_sync_request)
1571 /* if this protocol is requested, then resizing the
1572 window will be synchronized between the frame and the
1573 client */
1574 self->sync_request = TRUE;
1575 #endif
1576 }
1577 g_free(proto);
1578 }
1579 }
1580
1581 #ifdef SYNC
1582 void client_update_sync_request_counter(ObClient *self)
1583 {
1584 guint32 i;
1585
1586 if (PROP_GET32(self->window, net_wm_sync_request_counter, cardinal, &i)) {
1587 self->sync_counter = i;
1588 } else
1589 self->sync_counter = None;
1590 }
1591 #endif
1592
1593 static void client_get_colormap(ObClient *self)
1594 {
1595 XWindowAttributes wa;
1596
1597 if (XGetWindowAttributes(ob_display, self->window, &wa))
1598 client_update_colormap(self, wa.colormap);
1599 }
1600
1601 void client_update_colormap(ObClient *self, Colormap colormap)
1602 {
1603 if (colormap == self->colormap) return;
1604
1605 ob_debug("Setting client %s colormap: 0x%x\n", self->title, colormap);
1606
1607 if (client_focused(self)) {
1608 screen_install_colormap(self, FALSE); /* uninstall old one */
1609 self->colormap = colormap;
1610 screen_install_colormap(self, TRUE); /* install new one */
1611 } else
1612 self->colormap = colormap;
1613 }
1614
1615 void client_update_normal_hints(ObClient *self)
1616 {
1617 XSizeHints size;
1618 glong ret;
1619
1620 /* defaults */
1621 self->min_ratio = 0.0f;
1622 self->max_ratio = 0.0f;
1623 SIZE_SET(self->size_inc, 1, 1);
1624 SIZE_SET(self->base_size, -1, -1);
1625 SIZE_SET(self->min_size, 0, 0);
1626 SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
1627
1628 /* get the hints from the window */
1629 if (XGetWMNormalHints(ob_display, self->window, &size, &ret)) {
1630 /* normal windows can't request placement! har har
1631 if (!client_normal(self))
1632 */
1633 self->positioned = (size.flags & (PPosition|USPosition));
1634 self->sized = (size.flags & (PSize|USSize));
1635
1636 if (size.flags & PWinGravity)
1637 self->gravity = size.win_gravity;
1638
1639 if (size.flags & PAspect) {
1640 if (size.min_aspect.y)
1641 self->min_ratio =
1642 (gfloat) size.min_aspect.x / size.min_aspect.y;
1643 if (size.max_aspect.y)
1644 self->max_ratio =
1645 (gfloat) size.max_aspect.x / size.max_aspect.y;
1646 }
1647
1648 if (size.flags & PMinSize)
1649 SIZE_SET(self->min_size, size.min_width, size.min_height);
1650
1651 if (size.flags & PMaxSize)
1652 SIZE_SET(self->max_size, size.max_width, size.max_height);
1653
1654 if (size.flags & PBaseSize)
1655 SIZE_SET(self->base_size, size.base_width, size.base_height);
1656
1657 if (size.flags & PResizeInc && size.width_inc && size.height_inc)
1658 SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
1659
1660 ob_debug("Normal hints: min size (%d %d) max size (%d %d)\n "
1661 "size inc (%d %d) base size (%d %d)\n",
1662 self->min_size.width, self->min_size.height,
1663 self->max_size.width, self->max_size.height,
1664 self->size_inc.width, self->size_inc.height,
1665 self->base_size.width, self->base_size.height);
1666 }
1667 else
1668 ob_debug("Normal hints: not set\n");
1669 }
1670
1671 void client_setup_decor_and_functions(ObClient *self, gboolean reconfig)
1672 {
1673 /* start with everything (cept fullscreen) */
1674 self->decorations =
1675 (OB_FRAME_DECOR_TITLEBAR |
1676 OB_FRAME_DECOR_HANDLE |
1677 OB_FRAME_DECOR_GRIPS |
1678 OB_FRAME_DECOR_BORDER |
1679 OB_FRAME_DECOR_ICON |
1680 OB_FRAME_DECOR_ALLDESKTOPS |
1681 OB_FRAME_DECOR_ICONIFY |
1682 OB_FRAME_DECOR_MAXIMIZE |
1683 OB_FRAME_DECOR_SHADE |
1684 OB_FRAME_DECOR_CLOSE);
1685 self->functions =
1686 (OB_CLIENT_FUNC_RESIZE |
1687 OB_CLIENT_FUNC_MOVE |
1688 OB_CLIENT_FUNC_ICONIFY |
1689 OB_CLIENT_FUNC_MAXIMIZE |
1690 OB_CLIENT_FUNC_SHADE |
1691 OB_CLIENT_FUNC_CLOSE |
1692 OB_CLIENT_FUNC_BELOW |
1693 OB_CLIENT_FUNC_ABOVE |
1694 OB_CLIENT_FUNC_UNDECORATE);
1695
1696 if (!(self->min_size.width < self->max_size.width ||
1697 self->min_size.height < self->max_size.height))
1698 self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1699
1700 switch (self->type) {
1701 case OB_CLIENT_TYPE_NORMAL:
1702 /* normal windows retain all of the possible decorations and
1703 functionality, and can be fullscreen */
1704 self->functions |= OB_CLIENT_FUNC_FULLSCREEN;
1705 break;
1706
1707 case OB_CLIENT_TYPE_DIALOG:
1708 /* sometimes apps make dialog windows fullscreen for some reason (for
1709 e.g. kpdf does this..) */
1710 self->functions |= OB_CLIENT_FUNC_FULLSCREEN;
1711 break;
1712
1713 case OB_CLIENT_TYPE_UTILITY:
1714 /* these windows don't have anything added or removed by default */
1715 break;
1716
1717 case OB_CLIENT_TYPE_MENU:
1718 case OB_CLIENT_TYPE_TOOLBAR:
1719 /* these windows can't iconify or maximize */
1720 self->decorations &= ~(OB_FRAME_DECOR_ICONIFY |
1721 OB_FRAME_DECOR_MAXIMIZE);
1722 self->functions &= ~(OB_CLIENT_FUNC_ICONIFY |
1723 OB_CLIENT_FUNC_MAXIMIZE);
1724 break;
1725
1726 case OB_CLIENT_TYPE_SPLASH:
1727 /* these don't get get any decorations, and the only thing you can
1728 do with them is move them */
1729 self->decorations = 0;
1730 self->functions = OB_CLIENT_FUNC_MOVE;
1731 break;
1732
1733 case OB_CLIENT_TYPE_DESKTOP:
1734 /* these windows are not manipulated by the window manager */
1735 self->decorations = 0;
1736 self->functions = 0;
1737 break;
1738
1739 case OB_CLIENT_TYPE_DOCK:
1740 /* these windows are not manipulated by the window manager, but they
1741 can set below layer which has a special meaning */
1742 self->decorations = 0;
1743 self->functions = OB_CLIENT_FUNC_BELOW;
1744 break;
1745 }
1746
1747 /* Mwm Hints are applied subtractively to what has already been chosen for
1748 decor and functionality */
1749 if (self->mwmhints.flags & OB_MWM_FLAG_DECORATIONS) {
1750 if (! (self->mwmhints.decorations & OB_MWM_DECOR_ALL)) {
1751 if (! ((self->mwmhints.decorations & OB_MWM_DECOR_HANDLE) ||
1752 (self->mwmhints.decorations & OB_MWM_DECOR_TITLE)))
1753 {
1754 /* if the mwm hints request no handle or title, then all
1755 decorations are disabled, but keep the border if that's
1756 specified */
1757 if (self->mwmhints.decorations & OB_MWM_DECOR_BORDER)
1758 self->decorations = OB_FRAME_DECOR_BORDER;
1759 else
1760 self->decorations = 0;
1761 }
1762 }
1763 }
1764
1765 if (self->mwmhints.flags & OB_MWM_FLAG_FUNCTIONS) {
1766 if (! (self->mwmhints.functions & OB_MWM_FUNC_ALL)) {
1767 if (! (self->mwmhints.functions & OB_MWM_FUNC_RESIZE))
1768 self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1769 if (! (self->mwmhints.functions & OB_MWM_FUNC_MOVE))
1770 self->functions &= ~OB_CLIENT_FUNC_MOVE;
1771 /* dont let mwm hints kill any buttons
1772 if (! (self->mwmhints.functions & OB_MWM_FUNC_ICONIFY))
1773 self->functions &= ~OB_CLIENT_FUNC_ICONIFY;
1774 if (! (self->mwmhints.functions & OB_MWM_FUNC_MAXIMIZE))
1775 self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1776 */
1777 /* dont let mwm hints kill the close button
1778 if (! (self->mwmhints.functions & MwmFunc_Close))
1779 self->functions &= ~OB_CLIENT_FUNC_CLOSE; */
1780 }
1781 }
1782
1783 if (!(self->functions & OB_CLIENT_FUNC_SHADE))
1784 self->decorations &= ~OB_FRAME_DECOR_SHADE;
1785 if (!(self->functions & OB_CLIENT_FUNC_ICONIFY))
1786 self->decorations &= ~OB_FRAME_DECOR_ICONIFY;
1787 if (!(self->functions & OB_CLIENT_FUNC_RESIZE))
1788 self->decorations &= ~(OB_FRAME_DECOR_GRIPS | OB_FRAME_DECOR_HANDLE);
1789
1790 /* can't maximize without moving/resizing */
1791 if (!((self->functions & OB_CLIENT_FUNC_MAXIMIZE) &&
1792 (self->functions & OB_CLIENT_FUNC_MOVE) &&
1793 (self->functions & OB_CLIENT_FUNC_RESIZE))) {
1794 self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1795 self->decorations &= ~OB_FRAME_DECOR_MAXIMIZE;
1796 }
1797
1798 if (self->max_horz && self->max_vert) {
1799 /* you can't resize fully maximized windows */
1800 self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1801 /* kill the handle on fully maxed windows */
1802 self->decorations &= ~(OB_FRAME_DECOR_HANDLE | OB_FRAME_DECOR_GRIPS);
1803 }
1804
1805 /* If there are no decorations to remove, don't allow the user to try
1806 toggle the state */
1807 if (self->decorations == 0)
1808 self->functions &= ~OB_CLIENT_FUNC_UNDECORATE;
1809
1810 /* finally, the user can have requested no decorations, which overrides
1811 everything (but doesnt give it a border if it doesnt have one) */
1812 if (self->undecorated)
1813 self->decorations = 0;
1814
1815 /* if we don't have a titlebar, then we cannot shade! */
1816 if (!(self->decorations & OB_FRAME_DECOR_TITLEBAR))
1817 self->functions &= ~OB_CLIENT_FUNC_SHADE;
1818
1819 /* now we need to check against rules for the client's current state */
1820 if (self->fullscreen) {
1821 self->functions &= (OB_CLIENT_FUNC_CLOSE |
1822 OB_CLIENT_FUNC_FULLSCREEN |
1823 OB_CLIENT_FUNC_ICONIFY);
1824 self->decorations = 0;
1825 }
1826
1827 client_change_allowed_actions(self);
1828
1829 if (reconfig)
1830 /* force reconfigure to make sure decorations are updated */
1831 client_reconfigure(self, TRUE);
1832 }
1833
1834 static void client_change_allowed_actions(ObClient *self)
1835 {
1836 gulong actions[12];
1837 gint num = 0;
1838
1839 /* desktop windows are kept on all desktops */
1840 if (self->type != OB_CLIENT_TYPE_DESKTOP)
1841 actions[num++] = prop_atoms.net_wm_action_change_desktop;
1842
1843 if (self->functions & OB_CLIENT_FUNC_SHADE)
1844 actions[num++] = prop_atoms.net_wm_action_shade;
1845 if (self->functions & OB_CLIENT_FUNC_CLOSE)
1846 actions[num++] = prop_atoms.net_wm_action_close;
1847 if (self->functions & OB_CLIENT_FUNC_MOVE)
1848 actions[num++] = prop_atoms.net_wm_action_move;
1849 if (self->functions & OB_CLIENT_FUNC_ICONIFY)
1850 actions[num++] = prop_atoms.net_wm_action_minimize;
1851 if (self->functions & OB_CLIENT_FUNC_RESIZE)
1852 actions[num++] = prop_atoms.net_wm_action_resize;
1853 if (self->functions & OB_CLIENT_FUNC_FULLSCREEN)
1854 actions[num++] = prop_atoms.net_wm_action_fullscreen;
1855 if (self->functions & OB_CLIENT_FUNC_MAXIMIZE) {
1856 actions[num++] = prop_atoms.net_wm_action_maximize_horz;
1857 actions[num++] = prop_atoms.net_wm_action_maximize_vert;
1858 }
1859 if (self->functions & OB_CLIENT_FUNC_ABOVE)
1860 actions[num++] = prop_atoms.net_wm_action_above;
1861 if (self->functions & OB_CLIENT_FUNC_BELOW)
1862 actions[num++] = prop_atoms.net_wm_action_below;
1863 if (self->functions & OB_CLIENT_FUNC_UNDECORATE)
1864 actions[num++] = prop_atoms.ob_wm_action_undecorate;
1865
1866 PROP_SETA32(self->window, net_wm_allowed_actions, atom, actions, num);
1867
1868 /* make sure the window isn't breaking any rules now
1869
1870 don't check ICONIFY here. just cuz a window can't iconify doesnt mean
1871 it can't be iconified with its parent
1872 */
1873
1874 if (!(self->functions & OB_CLIENT_FUNC_SHADE) && self->shaded) {
1875 if (self->frame) client_shade(self, FALSE);
1876 else self->shaded = FALSE;
1877 }
1878 if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) && self->fullscreen) {
1879 if (self->frame) client_fullscreen(self, FALSE);
1880 else self->fullscreen = FALSE;
1881 }
1882 if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE) && (self->max_horz ||
1883 self->max_vert)) {
1884 if (self->frame) client_maximize(self, FALSE, 0);
1885 else self->max_vert = self->max_horz = FALSE;
1886 }
1887 }
1888
1889 void client_update_wmhints(ObClient *self)
1890 {
1891 XWMHints *hints;
1892
1893 /* assume a window takes input if it doesn't specify */
1894 self->can_focus = TRUE;
1895
1896 if ((hints = XGetWMHints(ob_display, self->window)) != NULL) {
1897 gboolean ur;
1898
1899 if (hints->flags & InputHint)
1900 self->can_focus = hints->input;
1901
1902 /* only do this when first managing the window *AND* when we aren't
1903 starting up! */
1904 if (ob_state() != OB_STATE_STARTING && self->frame == NULL)
1905 if (hints->flags & StateHint)
1906 self->iconic = hints->initial_state == IconicState;
1907
1908 ur = self->urgent;
1909 self->urgent = (hints->flags & XUrgencyHint);
1910 if (self->urgent && !ur)
1911 client_hilite(self, TRUE);
1912 else if (!self->urgent && ur && self->demands_attention)
1913 client_hilite(self, FALSE);
1914
1915 if (!(hints->flags & WindowGroupHint))
1916 hints->window_group = None;
1917
1918 /* did the group state change? */
1919 if (hints->window_group !=
1920 (self->group ? self->group->leader : None))
1921 {
1922 ObGroup *oldgroup = self->group;
1923
1924 /* remove from the old group if there was one */
1925 if (self->group) {
1926 group_remove(self->group, self);
1927 self->group = NULL;
1928 }
1929
1930 /* add ourself to the group if we have one */
1931 if (hints->window_group != None) {
1932 self->group = group_add(hints->window_group, self);
1933 }
1934
1935 /* Put ourselves into the new group's transient tree, and remove
1936 ourselves from the old group's */
1937 client_update_transient_tree(self, oldgroup, self->group,
1938 self->transient_for_group,
1939 self->transient_for_group,
1940 client_direct_parent(self),
1941 client_direct_parent(self));
1942
1943 /* Lastly, being in a group, or not, can change if the window is
1944 transient for anything.
1945
1946 The logic for this is:
1947 self->transient = TRUE always if the window wants to be
1948 transient for something, even if transient_for was NULL because
1949 it wasn't in a group before.
1950
1951 If parents was NULL and oldgroup was NULL we can assume
1952 that when we add the new group, it will become transient for
1953 something.
1954
1955 If transient_for_group is TRUE, then it must have already
1956 had a group. If it is getting a new group, the above call to
1957 client_update_transient_tree has already taken care of
1958 everything ! If it is losing all group status then it will
1959 no longer be transient for anything and that needs to be
1960 updated.
1961 */
1962 if (self->transient &&
1963 ((self->parents == NULL && oldgroup == NULL) ||
1964 (self->transient_for_group && !self->group)))
1965 client_update_transient_for(self);
1966 }
1967
1968 /* the WM_HINTS can contain an icon */
1969 if (hints->flags & IconPixmapHint)
1970 client_update_icons(self);
1971
1972 XFree(hints);
1973 }
1974 }
1975
1976 void client_update_title(ObClient *self)
1977 {
1978 gchar *data = NULL;
1979 gchar *visible = NULL;
1980
1981 g_free(self->title);
1982 g_free(self->original_title);
1983
1984 /* try netwm */
1985 if (!PROP_GETS(self->window, net_wm_name, utf8, &data)) {
1986 /* try old x stuff */
1987 if (!(PROP_GETS(self->window, wm_name, locale, &data)
1988 || PROP_GETS(self->window, wm_name, utf8, &data))) {
1989 if (self->transient) {
1990 /*
1991 GNOME alert windows are not given titles:
1992 http://developer.gnome.org/projects/gup/hig/draft_hig_new/windows-alert.html
1993 */
1994 data = g_strdup("");
1995 } else
1996 data = g_strdup(_("Unnamed Window"));
1997 }
1998 }
1999 self->original_title = g_strdup(data);
2000
2001 if (self->client_machine) {
2002 visible = g_strdup_printf("%s (%s)", data, self->client_machine);
2003 g_free(data);
2004 } else
2005 visible = data;
2006
2007 if (self->not_responding) {
2008 data = visible;
2009 if (self->kill_level > 0)
2010 visible = g_strdup_printf("%s - [%s]", data, _("Killing..."));
2011 else
2012 visible = g_strdup_printf("%s - [%s]", data, _("Not Responding"));
2013 g_free(data);
2014 }
2015
2016 PROP_SETS(self->window, net_wm_visible_name, visible);
2017 self->title = visible;
2018
2019 if (self->frame)
2020 frame_adjust_title(self->frame);
2021
2022 /* update the icon title */
2023 data = NULL;
2024 g_free(self->icon_title);
2025
2026 /* try netwm */
2027 if (!PROP_GETS(self->window, net_wm_icon_name, utf8, &data))
2028 /* try old x stuff */
2029 if (!(PROP_GETS(self->window, wm_icon_name, locale, &data) ||
2030 PROP_GETS(self->window, wm_icon_name, utf8, &data)))
2031 data = g_strdup(self->title);
2032
2033 if (self->client_machine) {
2034 visible = g_strdup_printf("%s (%s)", data, self->client_machine);
2035 g_free(data);
2036 } else
2037 visible = data;
2038
2039 if (self->not_responding) {
2040 data = visible;
2041 if (self->kill_level > 0)
2042 visible = g_strdup_printf("%s - [%s]", data, _("Killing..."));
2043 else
2044 visible = g_strdup_printf("%s - [%s]", data, _("Not Responding"));
2045 g_free(data);
2046 }
2047
2048 PROP_SETS(self->window, net_wm_visible_icon_name, visible);
2049 self->icon_title = visible;
2050 }
2051
2052 void client_update_strut(ObClient *self)
2053 {
2054 guint num;
2055 guint32 *data;
2056 gboolean got = FALSE;
2057 StrutPartial strut;
2058
2059 if (PROP_GETA32(self->window, net_wm_strut_partial, cardinal,
2060 &data, &num)) {
2061 if (num == 12) {
2062 got = TRUE;
2063 STRUT_PARTIAL_SET(strut,
2064 data[0], data[2], data[1], data[3],
2065 data[4], data[5], data[8], data[9],
2066 data[6], data[7], data[10], data[11]);
2067 }
2068 g_free(data);
2069 }
2070
2071 if (!got &&
2072 PROP_GETA32(self->window, net_wm_strut, cardinal, &data, &num)) {
2073 if (num == 4) {
2074 Rect *a;
2075
2076 got = TRUE;
2077
2078 /* use the screen's width/height */
2079 a = screen_physical_area_all_monitors();
2080
2081 STRUT_PARTIAL_SET(strut,
2082 data[0], data[2], data[1], data[3],
2083 a->y, a->y + a->height - 1,
2084 a->x, a->x + a->width - 1,
2085 a->y, a->y + a->height - 1,
2086 a->x, a->x + a->width - 1);
2087 g_free(a);
2088 }
2089 g_free(data);
2090 }
2091
2092 if (!got)
2093 STRUT_PARTIAL_SET(strut, 0, 0, 0, 0,
2094 0, 0, 0, 0, 0, 0, 0, 0);
2095
2096 if (!STRUT_EQUAL(strut, self->strut)) {
2097 self->strut = strut;
2098
2099 /* updating here is pointless while we're being mapped cuz we're not in
2100 the client list yet */
2101 if (self->frame)
2102 screen_update_areas();
2103 }
2104 }
2105
2106 void client_update_icons(ObClient *self)
2107 {
2108 guint num;
2109 guint32 *data;
2110 guint w, h, i, j;
2111 guint num_seen; /* number of icons present */
2112 RrImage *img;
2113
2114 img = NULL;
2115
2116 /* grab the server, because we might be setting the window's icon and
2117 we don't want them to set it in between and we overwrite their own
2118 icon */
2119 grab_server(TRUE);
2120
2121 if (PROP_GETA32(self->window, net_wm_icon, cardinal, &data, &num)) {
2122 /* figure out how many valid icons are in here */
2123 i = 0;
2124 num_seen = 0;
2125 while (i + 2 < num) { /* +2 is to make sure there is a w and h */
2126 w = data[i++];
2127 h = data[i++];
2128 /* watch for the data being too small for the specified size,
2129 or for zero sized icons. */
2130 if (i + w*h > num || w == 0 || h == 0) break;
2131
2132 /* convert it to the right bit order for ObRender */
2133 for (j = 0; j < w*h; ++j)
2134 data[i+j] =
2135 (((data[i+j] >> 24) & 0xff) << RrDefaultAlphaOffset) +
2136 (((data[i+j] >> 16) & 0xff) << RrDefaultRedOffset) +
2137 (((data[i+j] >> 8) & 0xff) << RrDefaultGreenOffset) +
2138 (((data[i+j] >> 0) & 0xff) << RrDefaultBlueOffset);
2139
2140 /* is it in the cache? */
2141 img = RrImageCacheFind(ob_rr_icons, &data[i], w, h);
2142 if (img) RrImageRef(img); /* own it */
2143
2144 i += w*h;
2145 ++num_seen;
2146
2147 /* don't bother looping anymore if we already found it in the cache
2148 since we'll just use that! */
2149 if (img) break;
2150 }
2151
2152 /* if it's not in the cache yet, then add it to the cache now.
2153 we have already converted it to the correct bit order above */
2154 if (!img && num_seen > 0) {
2155 img = RrImageNew(ob_rr_icons);
2156 i = 0;
2157 for (j = 0; j < num_seen; ++j) {
2158 w = data[i++];
2159 h = data[i++];
2160 RrImageAddPicture(img, &data[i], w, h);
2161 i += w*h;
2162 }
2163 }
2164
2165 g_free(data);
2166 }
2167
2168 /* if we didn't find an image from the NET_WM_ICON stuff, then try the
2169 legacy X hints */
2170 if (!img) {
2171 XWMHints *hints;
2172
2173 if ((hints = XGetWMHints(ob_display, self->window))) {
2174 if (hints->flags & IconPixmapHint) {
2175 gboolean xicon;
2176 xerror_set_ignore(TRUE);
2177 xicon = RrPixmapToRGBA(ob_rr_inst,
2178 hints->icon_pixmap,
2179 (hints->flags & IconMaskHint ?
2180 hints->icon_mask : None),
2181 (gint*)&w, (gint*)&h, &data);
2182 xerror_set_ignore(FALSE);
2183
2184
2185 if (xicon) {
2186 if (w > 0 && h > 0) {
2187 /* is this icon in the cache yet? */
2188 img = RrImageCacheFind(ob_rr_icons, data, w, h);
2189 if (img) RrImageRef(img); /* own it */
2190
2191 /* if not, then add it */
2192 if (!img) {
2193 img = RrImageNew(ob_rr_icons);
2194 RrImageAddPicture(img, data, w, h);
2195 }
2196 }
2197
2198 g_free(data);
2199 }
2200 }
2201 XFree(hints);
2202 }
2203 }
2204
2205 /* set the client's icons to be whatever we found */
2206 RrImageUnref(self->icon_set);
2207 self->icon_set = img;
2208
2209 /* if the client has no icon at all, then we set a default icon onto it.
2210 but, if it has parents, then one of them will have an icon already
2211 */
2212 if (!self->icon_set && !self->parents) {
2213 RrPixel32 *icon = ob_rr_theme->def_win_icon;
2214 gulong *ldata; /* use a long here to satisfy OBT_PROP_SETA32 */
2215
2216 w = ob_rr_theme->def_win_icon_w;
2217 h = ob_rr_theme->def_win_icon_h;
2218 ldata = g_new(gulong, w*h+2);
2219 ldata[0] = w;
2220 ldata[1] = h;
2221 for (i = 0; i < w*h; ++i)
2222 ldata[i+2] = (((icon[i] >> RrDefaultAlphaOffset) & 0xff) << 24) +
2223 (((icon[i] >> RrDefaultRedOffset) & 0xff) << 16) +
2224 (((icon[i] >> RrDefaultGreenOffset) & 0xff) << 8) +
2225 (((icon[i] >> RrDefaultBlueOffset) & 0xff) << 0);
2226 PROP_SETA32(self->window, net_wm_icon, cardinal, ldata, w*h+2);
2227 g_free(ldata);
2228 } else if (self->frame)
2229 /* don't draw the icon empty if we're just setting one now anyways,
2230 we'll get the property change any second */
2231 frame_adjust_icon(self->frame);
2232
2233 grab_server(FALSE);
2234 }
2235
2236 void client_update_icon_geometry(ObClient *self)
2237 {
2238 guint num;
2239 guint32 *data;
2240
2241 RECT_SET(self->icon_geometry, 0, 0, 0, 0);
2242
2243 if (PROP_GETA32(self->window, net_wm_icon_geometry, cardinal, &data, &num))
2244 {
2245 if (num == 4)
2246 /* don't let them set it with an area < 0 */
2247 RECT_SET(self->icon_geometry, data[0], data[1],
2248 MAX(data[2],0), MAX(data[3],0));
2249 g_free(data);
2250 }
2251 }
2252
2253 static void client_get_session_ids(ObClient *self)
2254 {
2255 guint32 leader;
2256 gboolean got;
2257 gchar *s;
2258 gchar **ss;
2259
2260 if (!PROP_GET32(self->window, wm_client_leader, window, &leader))
2261 leader = None;
2262
2263 /* get the SM_CLIENT_ID */
2264 got = FALSE;
2265 if (leader)
2266 got = PROP_GETS(leader, sm_client_id, locale, &self->sm_client_id);
2267 if (!got)
2268 PROP_GETS(self->window, sm_client_id, locale, &self->sm_client_id);
2269
2270 /* get the WM_CLASS (name and class). make them "" if they are not
2271 provided */
2272 got = FALSE;
2273 if (leader)
2274 got = PROP_GETSS(leader, wm_class, locale, &ss);
2275 if (!got)
2276 got = PROP_GETSS(self->window, wm_class, locale, &ss);
2277
2278 if (got) {
2279 if (ss[0]) {
2280 self->name = g_strdup(ss[0]);
2281 if (ss[1])
2282 self->class = g_strdup(ss[1]);
2283 }
2284 g_strfreev(ss);
2285 }
2286
2287 if (self->name == NULL) self->name = g_strdup("");
2288 if (self->class == NULL) self->class = g_strdup("");
2289
2290 /* get the WM_WINDOW_ROLE. make it "" if it is not provided */
2291 got = FALSE;
2292 if (leader)
2293 got = PROP_GETS(leader, wm_window_role, locale, &s);
2294 if (!got)
2295 got = PROP_GETS(self->window, wm_window_role, locale, &s);
2296
2297 if (got)
2298 self->role = s;
2299 else
2300 self->role = g_strdup("");
2301
2302 /* get the WM_COMMAND */
2303 got = FALSE;
2304
2305 if (leader)
2306 got = PROP_GETSS(leader, wm_command, locale, &ss);
2307 if (!got)
2308 got = PROP_GETSS(self->window, wm_command, locale, &ss);
2309
2310 if (got) {
2311 /* merge/mash them all together */
2312 gchar *merge = NULL;
2313 gint i;
2314
2315 for (i = 0; ss[i]; ++i) {
2316 gchar *tmp = merge;
2317 if (merge)
2318 merge = g_strconcat(merge, ss[i], NULL);
2319 else
2320 merge = g_strconcat(ss[i], NULL);
2321 g_free(tmp);
2322 }
2323 g_strfreev(ss);
2324
2325 self->wm_command = merge;
2326 }
2327
2328 /* get the WM_CLIENT_MACHINE */
2329 got = FALSE;
2330 if (leader)
2331 got = PROP_GETS(leader, wm_client_machine, locale, &s);
2332 if (!got)
2333 got = PROP_GETS(self->window, wm_client_machine, locale, &s);
2334
2335 if (got) {
2336 gchar localhost[128];
2337 guint32 pid;
2338
2339 gethostname(localhost, 127);
2340 localhost[127] = '\0';
2341 if (strcmp(localhost, s) != 0)
2342 self->client_machine = s;
2343 else
2344 g_free(s);
2345
2346 /* see if it has the PID set too (the PID requires that the
2347 WM_CLIENT_MACHINE be set) */
2348 if (PROP_GET32(self->window, net_wm_pid, cardinal, &pid))
2349 self->pid = pid;
2350 }
2351 }
2352
2353 static void client_change_wm_state(ObClient *self)
2354 {
2355 gulong state[2];
2356 glong old;
2357
2358 old = self->wmstate;
2359
2360 if (self->shaded || self->iconic ||
2361 (self->desktop != DESKTOP_ALL && self->desktop != screen_desktop))
2362 {
2363 self->wmstate = IconicState;
2364 } else
2365 self->wmstate = NormalState;
2366
2367 if (old != self->wmstate) {
2368 PROP_MSG(self->window, kde_wm_change_state,
2369 self->wmstate, 1, 0, 0);
2370
2371 state[0] = self->wmstate;
2372 state[1] = None;
2373 PROP_SETA32(self->window, wm_state, wm_state, state, 2);
2374 }
2375 }
2376
2377 static void client_change_state(ObClient *self)
2378 {
2379 gulong netstate[12];
2380 guint num;
2381
2382 num = 0;
2383 if (self->modal)
2384 netstate[num++] = prop_atoms.net_wm_state_modal;
2385 if (self->shaded)
2386 netstate[num++] = prop_atoms.net_wm_state_shaded;
2387 if (self->iconic)
2388 netstate[num++] = prop_atoms.net_wm_state_hidden;
2389 if (self->skip_taskbar)
2390 netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
2391 if (self->skip_pager)
2392 netstate[num++] = prop_atoms.net_wm_state_skip_pager;
2393 if (self->fullscreen)
2394 netstate[num++] = prop_atoms.net_wm_state_fullscreen;
2395 if (self->max_vert)
2396 netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
2397 if (self->max_horz)
2398 netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
2399 if (self->above)
2400 netstate[num++] = prop_atoms.net_wm_state_above;
2401 if (self->below)
2402 netstate[num++] = prop_atoms.net_wm_state_below;
2403 if (self->demands_attention)
2404 netstate[num++] = prop_atoms.net_wm_state_demands_attention;
2405 if (self->undecorated)
2406 netstate[num++] = prop_atoms.ob_wm_state_undecorated;
2407 PROP_SETA32(self->window, net_wm_state, atom, netstate, num);
2408
2409 if (self->frame)
2410 frame_adjust_state(self->frame);
2411 }
2412
2413 ObClient *client_search_focus_tree(ObClient *self)
2414 {
2415 GSList *it;
2416 ObClient *ret;
2417
2418 for (it = self->transients; it; it = g_slist_next(it)) {
2419 if (client_focused(it->data)) return it->data;
2420 if ((ret = client_search_focus_tree(it->data))) return ret;
2421 }
2422 return NULL;
2423 }
2424
2425 ObClient *client_search_focus_tree_full(ObClient *self)
2426 {
2427 if (self->parents) {
2428 GSList *it;
2429
2430 for (it = self->parents; it; it = g_slist_next(it)) {
2431 ObClient *c = it->data;
2432 if ((c = client_search_focus_tree_full(it->data))) return c;
2433 }
2434
2435 return NULL;
2436 }
2437 else {
2438 /* this function checks the whole tree, the client_search_focus_tree
2439 does not, so we need to check this window */
2440 if (client_focused(self))
2441 return self;
2442 return client_search_focus_tree(self);
2443 }
2444 }
2445
2446 ObClient *client_search_focus_group_full(ObClient *self)
2447 {
2448 GSList *it;
2449
2450 if (self->group) {
2451 for (it = self->group->members; it; it = g_slist_next(it)) {
2452 ObClient *c = it->data;
2453
2454 if (client_focused(c)) return c;
2455 if ((c = client_search_focus_tree(it->data))) return c;
2456 }
2457 } else
2458 if (client_focused(self)) return self;
2459 return NULL;
2460 }
2461
2462 gboolean client_has_parent(ObClient *self)
2463 {
2464 return self->parents != NULL;
2465 }
2466
2467 static ObStackingLayer calc_layer(ObClient *self)
2468 {
2469 ObStackingLayer l;
2470 Rect *monitor;
2471
2472 monitor = screen_physical_area_monitor(client_monitor(self));
2473
2474 if (self->type == OB_CLIENT_TYPE_DESKTOP)
2475 l = OB_STACKING_LAYER_DESKTOP;
2476 else if (self->type == OB_CLIENT_TYPE_DOCK) {
2477 if (self->below) l = OB_STACKING_LAYER_NORMAL;
2478 else l = OB_STACKING_LAYER_ABOVE;
2479 }
2480 else if ((self->fullscreen ||
2481 /* No decorations and fills the monitor = oldskool fullscreen.
2482 But not for maximized windows.
2483 */
2484 (self->decorations == 0 &&
2485 !(self->max_horz && self->max_vert) &&
2486 RECT_EQUAL(self->area, *monitor))) &&
2487 /* you are fullscreen while you or your children are focused.. */
2488 (client_focused(self) || client_search_focus_tree(self) ||
2489 /* you can be fullscreen if you're on another desktop */
2490 (self->desktop != screen_desktop &&
2491 self->desktop != DESKTOP_ALL) ||
2492 /* and you can also be fullscreen if the focused client is on
2493 another monitor, or nothing else is focused */
2494 (!focus_client ||
2495 client_monitor(focus_client) != client_monitor(self))))
2496 l = OB_STACKING_LAYER_FULLSCREEN;
2497 else if (self->above) l = OB_STACKING_LAYER_ABOVE;
2498 else if (self->below) l = OB_STACKING_LAYER_BELOW;
2499 else l = OB_STACKING_LAYER_NORMAL;
2500
2501 g_free(monitor);
2502
2503 return l;
2504 }
2505
2506 static void client_calc_layer_recursive(ObClient *self, ObClient *orig,
2507 ObStackingLayer min)
2508 {
2509 ObStackingLayer old, own;
2510 GSList *it;
2511
2512 old = self->layer;
2513 own = calc_layer(self);
2514 self->layer = MAX(own, min);
2515
2516 if (self->layer != old) {
2517 stacking_remove(CLIENT_AS_WINDOW(self));
2518 stacking_add_nonintrusive(CLIENT_AS_WINDOW(self));
2519 }
2520
2521 /* we've been restacked */
2522 self->visited = TRUE;
2523
2524 for (it = self->transients; it; it = g_slist_next(it))
2525 client_calc_layer_recursive(it->data, orig,
2526 self->layer);
2527 }
2528
2529 static void client_calc_layer_internal(ObClient *self)
2530 {
2531 GSList *sit;
2532
2533 /* transients take on the layer of their parents */
2534 sit = client_search_all_top_parents(self);
2535
2536 for (; sit; sit = g_slist_next(sit))
2537 client_calc_layer_recursive(sit->data, self, 0);
2538 }
2539
2540 void client_calc_layer(ObClient *self)
2541 {
2542 GList *it;
2543
2544 /* skip over stuff above fullscreen layer */
2545 for (it = stacking_list; it; it = g_list_next(it))
2546 if (window_layer(it->data) <= OB_STACKING_LAYER_FULLSCREEN) break;
2547
2548 /* find the windows in the fullscreen layer, and mark them not-visited */
2549 for (; it; it = g_list_next(it)) {
2550 if (window_layer(it->data) < OB_STACKING_LAYER_FULLSCREEN) break;
2551 else if (WINDOW_IS_CLIENT(it->data))
2552 WINDOW_AS_CLIENT(it->data)->visited = FALSE;
2553 }
2554
2555 client_calc_layer_internal(self);
2556
2557 /* skip over stuff above fullscreen layer */
2558 for (it = stacking_list; it; it = g_list_next(it))
2559 if (window_layer(it->data) <= OB_STACKING_LAYER_FULLSCREEN) break;
2560
2561 /* now recalc any windows in the fullscreen layer which have not
2562 had their layer recalced already */
2563 for (; it; it = g_list_next(it)) {
2564 if (window_layer(it->data) < OB_STACKING_LAYER_FULLSCREEN) break;
2565 else if (WINDOW_IS_CLIENT(it->data) &&
2566 !WINDOW_AS_CLIENT(it->data)->visited)
2567 client_calc_layer_internal(it->data);
2568 }
2569 }
2570
2571 gboolean client_should_show(ObClient *self)
2572 {
2573 if (self->iconic)
2574 return FALSE;
2575 if (client_normal(self) && screen_showing_desktop)
2576 return FALSE;
2577 if (self->desktop == screen_desktop || self->desktop == DESKTOP_ALL)
2578 return TRUE;
2579
2580 return FALSE;
2581 }
2582
2583 gboolean client_show(ObClient *self)
2584 {
2585 gboolean show = FALSE;
2586
2587 if (client_should_show(self)) {
2588 /* replay pending pointer event before showing the window, in case it
2589 should be going to something under the window */
2590 mouse_replay_pointer();
2591
2592 frame_show(self->frame);
2593 show = TRUE;
2594
2595 /* According to the ICCCM (sec 4.1.3.1) when a window is not visible,
2596 it needs to be in IconicState. This includes when it is on another
2597 desktop!
2598 */
2599 client_change_wm_state(self);
2600 }
2601 return show;
2602 }
2603
2604 gboolean client_hide(ObClient *self)
2605 {
2606 gboolean hide = FALSE;
2607
2608 if (!client_should_show(self)) {
2609 if (self == focus_client) {
2610 event_cancel_all_key_grabs();
2611 }
2612
2613 /* We don't need to ignore enter events here.
2614 The window can hide/iconify in 3 different ways:
2615 1 - through an x message. in this case we ignore all enter events
2616 caused by responding to the x message (unless underMouse)
2617 2 - by a keyboard action. in this case we ignore all enter events
2618 caused by the action
2619 3 - by a mouse action. in this case they are doing stuff with the
2620 mouse and focus _should_ move.
2621
2622 Also in action_end, we simulate an enter event that can't be ignored
2623 so trying to ignore them is futile in case 3 anyways
2624 */
2625
2626 /* replay pending pointer event before hiding the window, in case it
2627 should be going to the window */
2628 mouse_replay_pointer();
2629
2630 frame_hide(self->frame);
2631 hide = TRUE;
2632
2633 /* According to the ICCCM (sec 4.1.3.1) when a window is not visible,
2634 it needs to be in IconicState. This includes when it is on another
2635 desktop!
2636 */
2637 client_change_wm_state(self);
2638 }
2639 return hide;
2640 }
2641
2642 void client_showhide(ObClient *self)
2643 {
2644 if (!client_show(self))
2645 client_hide(self);
2646 }
2647
2648 gboolean client_normal(ObClient *self) {
2649 return ! (self->type == OB_CLIENT_TYPE_DESKTOP ||
2650 self->type == OB_CLIENT_TYPE_DOCK ||
2651 self->type == OB_CLIENT_TYPE_SPLASH);
2652 }
2653
2654 gboolean client_helper(ObClient *self)
2655 {
2656 return (self->type == OB_CLIENT_TYPE_UTILITY ||
2657 self->type == OB_CLIENT_TYPE_MENU ||
2658 self->type == OB_CLIENT_TYPE_TOOLBAR);
2659 }
2660
2661 gboolean client_mouse_focusable(ObClient *self)
2662 {
2663 return !(self->type == OB_CLIENT_TYPE_MENU ||
2664 self->type == OB_CLIENT_TYPE_TOOLBAR ||
2665 self->type == OB_CLIENT_TYPE_SPLASH ||
2666 self->type == OB_CLIENT_TYPE_DOCK);
2667 }
2668
2669 gboolean client_enter_focusable(ObClient *self)
2670 {
2671 /* you can focus desktops but it shouldn't on enter */
2672 return (client_mouse_focusable(self) &&
2673 self->type != OB_CLIENT_TYPE_DESKTOP);
2674 }
2675
2676
2677 static void client_apply_startup_state(ObClient *self,
2678 gint x, gint y, gint w, gint h)
2679 {
2680 /* save the states that we are going to apply */
2681 gboolean iconic = self->iconic;
2682 gboolean fullscreen = self->fullscreen;
2683 gboolean undecorated = self->undecorated;
2684 gboolean shaded = self->shaded;
2685 gboolean demands_attention = self->demands_attention;
2686 gboolean max_horz = self->max_horz;
2687 gboolean max_vert = self->max_vert;
2688 Rect oldarea;
2689 gint l;
2690
2691 /* turn them all off in the client, so they won't affect the window
2692 being placed */
2693 self->iconic = self->fullscreen = self->undecorated = self->shaded =
2694 self->demands_attention = self->max_horz = self->max_vert = FALSE;
2695
2696 /* move the client to its placed position, or it it's already there,
2697 generate a ConfigureNotify telling the client where it is.
2698
2699 do this after adjusting the frame. otherwise it gets all weird and
2700 clients don't work right
2701
2702 do this before applying the states so they have the correct
2703 pre-max/pre-fullscreen values
2704 */
2705 client_try_configure(self, &x, &y, &w, &h, &l, &l, FALSE);
2706 ob_debug("placed window 0x%x at %d, %d with size %d x %d\n",
2707 self->window, x, y, w, h);
2708 /* save the area, and make it where it should be for the premax stuff */
2709 oldarea = self->area;
2710 RECT_SET(self->area, x, y, w, h);
2711
2712 /* apply the states. these are in a carefully crafted order.. */
2713
2714 if (iconic)
2715 client_iconify(self, TRUE, FALSE, TRUE);
2716 if (fullscreen)
2717 client_fullscreen(self, TRUE);
2718 if (undecorated)
2719 client_set_undecorated(self, TRUE);
2720 if (shaded)
2721 client_shade(self, TRUE);
2722 if (demands_attention)
2723 client_hilite(self, TRUE);
2724
2725 if (max_vert && max_horz)
2726 client_maximize(self, TRUE, 0);
2727 else if (max_vert)
2728 client_maximize(self, TRUE, 2);
2729 else if (max_horz)
2730 client_maximize(self, TRUE, 1);
2731
2732 /* if the window hasn't been configured yet, then do so now, in fact the
2733 x,y,w,h may _not_ be the same as the area rect, which can end up
2734 meaning that the client isn't properly moved/resized by the fullscreen
2735 function
2736 pho can cause this because it maps at size of the screen but not 0,0
2737 so openbox moves it on screen to 0,0 (thus x,y=0,0 and area.x,y don't).
2738 then fullscreen'ing makes it go to 0,0 which it thinks it already is at
2739 cuz thats where the pre-fullscreen will be. however the actual area is
2740 not, so this needs to be called even if we have fullscreened/maxed
2741 */
2742 self->area = oldarea;
2743 client_configure(self, x, y, w, h, FALSE, TRUE, FALSE);
2744
2745 /* set the desktop hint, to make sure that it always exists */
2746 PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
2747
2748 /* nothing to do for the other states:
2749 skip_taskbar
2750 skip_pager
2751 modal
2752 above
2753 below
2754 */
2755 }
2756
2757 void client_gravity_resize_w(ObClient *self, gint *x, gint oldw, gint neww)
2758 {
2759 /* these should be the current values. this is for when you're not moving,
2760 just resizing */
2761 g_assert(*x == self->area.x);
2762 g_assert(oldw == self->area.width);
2763
2764 /* horizontal */
2765 switch (self->gravity) {
2766 default:
2767 case NorthWestGravity:
2768 case WestGravity:
2769 case SouthWestGravity:
2770 case StaticGravity:
2771 case ForgetGravity:
2772 break;
2773 case NorthGravity:
2774 case CenterGravity:
2775 case SouthGravity:
2776 *x -= (neww - oldw) / 2;
2777 break;
2778 case NorthEastGravity:
2779 case EastGravity:
2780 case SouthEastGravity:
2781 *x -= neww - oldw;
2782 break;
2783 }
2784 }
2785
2786 void client_gravity_resize_h(ObClient *self, gint *y, gint oldh, gint newh)
2787 {
2788 /* these should be the current values. this is for when you're not moving,
2789 just resizing */
2790 g_assert(*y == self->area.y);
2791 g_assert(oldh == self->area.height);
2792
2793 /* vertical */
2794 switch (self->gravity) {
2795 default:
2796 case NorthWestGravity:
2797 case NorthGravity:
2798 case NorthEastGravity:
2799 case StaticGravity:
2800 case ForgetGravity:
2801 break;
2802 case WestGravity:
2803 case CenterGravity:
2804 case EastGravity:
2805 *y -= (newh - oldh) / 2;
2806 break;
2807 case SouthWestGravity:
2808 case SouthGravity:
2809 case SouthEastGravity:
2810 *y -= newh - oldh;
2811 break;
2812 }
2813 }
2814
2815 void client_try_configure(ObClient *self, gint *x, gint *y, gint *w, gint *h,
2816 gint *logicalw, gint *logicalh,
2817 gboolean user)
2818 {
2819 Rect desired = {*x, *y, *w, *h};
2820 frame_rect_to_frame(self->frame, &desired);
2821
2822 /* make the frame recalculate its dimensions n shit without changing
2823 anything visible for real, this way the constraints below can work with
2824 the updated frame dimensions. */
2825 frame_adjust_area(self->frame, FALSE, TRUE, TRUE);
2826
2827 /* gets the frame's position */
2828 frame_client_gravity(self->frame, x, y);
2829
2830 /* these positions are frame positions, not client positions */
2831
2832 /* set the size and position if fullscreen */
2833 if (self->fullscreen) {
2834 Rect *a;
2835 guint i;
2836
2837 i = screen_find_monitor(&desired);
2838 a = screen_physical_area_monitor(i);
2839
2840 *x = a->x;
2841 *y = a->y;
2842 *w = a->width;
2843 *h = a->height;
2844
2845 user = FALSE; /* ignore if the client can't be moved/resized when it
2846 is fullscreening */
2847
2848 g_free(a);
2849 } else if (self->max_horz || self->max_vert) {
2850 Rect *a;
2851 guint i;
2852
2853 /* use all possible struts when maximizing to the full screen */
2854 i = screen_find_monitor(&desired);
2855 a = screen_area(self->desktop, i,
2856 (self->max_horz && self->max_vert ? NULL : &desired));
2857
2858 /* set the size and position if maximized */
2859 if (self->max_horz) {
2860 *x = a->x;
2861 *w = a->width - self->frame->size.left - self->frame->size.right;
2862 }
2863 if (self->max_vert) {
2864 *y = a->y;
2865 *h = a->height - self->frame->size.top - self->frame->size.bottom;
2866 }
2867
2868 user = FALSE; /* ignore if the client can't be moved/resized when it
2869 is maximizing */
2870
2871 g_free(a);
2872 }
2873
2874 /* gets the client's position */
2875 frame_frame_gravity(self->frame, x, y);
2876
2877 /* work within the preferred sizes given by the window, these may have
2878 changed rather than it's requested width and height, so always run
2879 through this code */
2880 {
2881 gint basew, baseh, minw, minh;
2882 gint incw, inch;
2883 gfloat minratio, maxratio;
2884
2885 incw = self->fullscreen || self->max_horz ? 1 : self->size_inc.width;
2886 inch = self->fullscreen || self->max_vert ? 1 : self->size_inc.height;
2887 minratio = self->fullscreen || (self->max_horz && self->max_vert) ?
2888 0 : self->min_ratio;
2889 maxratio = self->fullscreen || (self->max_horz && self->max_vert) ?
2890 0 : self->max_ratio;
2891
2892 /* base size is substituted with min size if not specified */
2893 if (self->base_size.width >= 0 || self->base_size.height >= 0) {
2894 basew = self->base_size.width;
2895 baseh = self->base_size.height;
2896 } else {
2897 basew = self->min_size.width;
2898 baseh = self->min_size.height;
2899 }
2900 /* min size is substituted with base size if not specified */
2901 if (self->min_size.width || self->min_size.height) {
2902 minw = self->min_size.width;
2903 minh = self->min_size.height;
2904 } else {
2905 minw = self->base_size.width;
2906 minh = self->base_size.height;
2907 }
2908
2909 /* This comment is no longer true */
2910 /* if this is a user-requested resize, then check against min/max
2911 sizes */
2912
2913 /* smaller than min size or bigger than max size? */
2914 if (*w > self->max_size.width) *w = self->max_size.width;
2915 if (*w < minw) *w = minw;
2916 if (*h > self->max_size.height) *h = self->max_size.height;
2917 if (*h < minh) *h = minh;
2918
2919 *w -= basew;
2920 *h -= baseh;
2921
2922 /* keep to the increments */
2923 *w /= incw;
2924 *h /= inch;
2925
2926 /* you cannot resize to nothing */
2927 if (basew + *w < 1) *w = 1 - basew;
2928 if (baseh + *h < 1) *h = 1 - baseh;
2929
2930 /* save the logical size */
2931 *logicalw = incw > 1 ? *w : *w + basew;
2932 *logicalh = inch > 1 ? *h : *h + baseh;
2933
2934 *w *= incw;
2935 *h *= inch;
2936
2937 *w += basew;
2938 *h += baseh;
2939
2940 /* adjust the height to match the width for the aspect ratios.
2941 for this, min size is not substituted for base size ever. */
2942 *w -= self->base_size.width;
2943 *h -= self->base_size.height;
2944
2945 if (minratio)
2946 if (*h * minratio > *w) {
2947 *h = (gint)(*w / minratio);
2948
2949 /* you cannot resize to nothing */
2950 if (*h < 1) {
2951 *h = 1;
2952 *w = (gint)(*h * minratio);
2953 }
2954 }
2955 if (maxratio)
2956 if (*h * maxratio < *w) {
2957 *h = (gint)(*w / maxratio);
2958
2959 /* you cannot resize to nothing */
2960 if (*h < 1) {
2961 *h = 1;
2962 *w = (gint)(*h * minratio);
2963 }
2964 }
2965
2966 *w += self->base_size.width;
2967 *h += self->base_size.height;
2968 }
2969
2970 /* these override the above states! if you cant move you can't move! */
2971 if (user) {
2972 if (!(self->functions & OB_CLIENT_FUNC_MOVE)) {
2973 *x = self->area.x;
2974 *y = self->area.y;
2975 }
2976 if (!(self->functions & OB_CLIENT_FUNC_RESIZE)) {
2977 *w = self->area.width;
2978 *h = self->area.height;
2979 }
2980 }
2981
2982 g_assert(*w > 0);
2983 g_assert(*h > 0);
2984 }
2985
2986
2987 void client_configure(ObClient *self, gint x, gint y, gint w, gint h,
2988 gboolean user, gboolean final, gboolean force_reply)
2989 {
2990 Rect oldframe;
2991 gint oldw, oldh;
2992 gboolean send_resize_client;
2993 gboolean moved = FALSE, resized = FALSE, rootmoved = FALSE;
2994 gboolean fmoved, fresized;
2995 guint fdecor = self->frame->decorations;
2996 gboolean fhorz = self->frame->max_horz;
2997 gboolean fvert = self->frame->max_vert;
2998 gint logicalw, logicalh;
2999
3000 /* find the new x, y, width, and height (and logical size) */
3001 client_try_configure(self, &x, &y, &w, &h, &logicalw, &logicalh, user);
3002
3003 /* set the logical size if things changed */
3004 if (!(w == self->area.width && h == self->area.height))
3005 SIZE_SET(self->logical_size, logicalw, logicalh);
3006
3007 /* figure out if we moved or resized or what */
3008 moved = (x != self->area.x || y != self->area.y);
3009 resized = (w != self->area.width || h != self->area.height);
3010
3011 oldw = self->area.width;
3012 oldh = self->area.height;
3013 oldframe = self->frame->area;
3014 RECT_SET(self->area, x, y, w, h);
3015
3016 /* for app-requested resizes, always resize if 'resized' is true.
3017 for user-requested ones, only resize if final is true, or when
3018 resizing in redraw mode */
3019 send_resize_client = ((!user && resized) ||
3020 (user && (final ||
3021 (resized && config_resize_redraw))));
3022
3023 /* if the client is enlarging, then resize the client before the frame */
3024 if (send_resize_client && (w > oldw || h > oldh)) {
3025 XMoveResizeWindow(ob_display, self->window,
3026 self->frame->size.left, self->frame->size.top,
3027 MAX(w, oldw), MAX(h, oldh));
3028 frame_adjust_client_area(self->frame);
3029 }
3030
3031 /* find the frame's dimensions and move/resize it */
3032 fmoved = moved;
3033 fresized = resized;
3034
3035 /* if decorations changed, then readjust everything for the frame */
3036 if (self->decorations != fdecor ||
3037 self->max_horz != fhorz || self->max_vert != fvert)
3038 {
3039 fmoved = fresized = TRUE;
3040 }
3041
3042 /* adjust the frame */
3043 if (fmoved || fresized) {
3044 gulong ignore_start;
3045 if (!user)
3046 ignore_start = event_start_ignore_all_enters();
3047
3048 /* replay pending pointer event before move the window, in case it
3049 would change what window gets the event */
3050 mouse_replay_pointer();
3051
3052 frame_adjust_area(self->frame, fmoved, fresized, FALSE);
3053
3054 if (!user)
3055 event_end_ignore_all_enters(ignore_start);
3056 }
3057
3058 if (!user || final) {
3059 gint oldrx = self->root_pos.x;
3060 gint oldry = self->root_pos.y;
3061 /* we have reset the client to 0 border width, so don't include
3062 it in these coords */
3063 POINT_SET(self->root_pos,
3064 self->frame->area.x + self->frame->size.left -
3065 self->border_width,
3066 self->frame->area.y + self->frame->size.top -
3067 self->border_width);
3068 if (self->root_pos.x != oldrx || self->root_pos.y != oldry)
3069 rootmoved = TRUE;
3070 }
3071
3072 /* This is kinda tricky and should not be changed.. let me explain!
3073
3074 When user = FALSE, then the request is coming from the application
3075 itself, and we are more strict about when to send a synthetic
3076 ConfigureNotify. We strictly follow the rules of the ICCCM sec 4.1.5
3077 in this case (if force_reply is true)
3078
3079 When user = TRUE, then the request is coming from "us", like when we
3080 maximize a window or something. In this case we are more lenient. We
3081 used to follow the same rules as above, but _Java_ Swing can't handle
3082 this. So just to appease Swing, when user = TRUE, we always send
3083 a synthetic ConfigureNotify to give the window its root coordinates.
3084 */
3085 if ((!user && !resized && (rootmoved || force_reply)) ||
3086 (user && final && rootmoved))
3087 {
3088 XEvent event;
3089
3090 event.type = ConfigureNotify;
3091 event.xconfigure.display = ob_display;
3092 event.xconfigure.event = self->window;
3093 event.xconfigure.window = self->window;
3094
3095 ob_debug("Sending ConfigureNotify to %s for %d,%d %dx%d\n",
3096 self->title, self->root_pos.x, self->root_pos.y, w, h);
3097
3098 /* root window real coords */
3099 event.xconfigure.x = self->root_pos.x;
3100 event.xconfigure.y = self->root_pos.y;
3101 event.xconfigure.width = w;
3102 event.xconfigure.height = h;
3103 event.xconfigure.border_width = self->border_width;
3104 event.xconfigure.above = None;
3105 event.xconfigure.override_redirect = FALSE;
3106 XSendEvent(event.xconfigure.display, event.xconfigure.window,
3107 FALSE, StructureNotifyMask, &event);
3108 }
3109
3110 /* if the client is shrinking, then resize the frame before the client.
3111
3112 both of these resize sections may run, because the top one only resizes
3113 in the direction that is growing
3114 */
3115 if (send_resize_client && (w <= oldw || h <= oldh)) {
3116 frame_adjust_client_area(self->frame);
3117 XMoveResizeWindow(ob_display, self->window,
3118 self->frame->size.left, self->frame->size.top, w, h);
3119 }
3120
3121 XFlush(ob_display);
3122
3123 /* if it moved between monitors, then this can affect the stacking
3124 layer of this window or others - for fullscreen windows */
3125 if (screen_find_monitor(&self->frame->area) !=
3126 screen_find_monitor(&oldframe))
3127 {
3128 client_calc_layer(self);
3129 }
3130 }
3131
3132 void client_fullscreen(ObClient *self, gboolean fs)
3133 {
3134 gint x, y, w, h;
3135
3136 if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) || /* can't */
3137 self->fullscreen == fs) return; /* already done */
3138
3139 self->fullscreen = fs;
3140 client_change_state(self); /* change the state hints on the client */
3141
3142 if (fs) {
3143 self->pre_fullscreen_area = self->area;
3144 /* if the window is maximized, its area isn't all that meaningful.
3145 save its premax area instead. */
3146 if (self->max_horz) {
3147 self->pre_fullscreen_area.x = self->pre_max_area.x;
3148 self->pre_fullscreen_area.width = self->pre_max_area.width;
3149 }
3150 if (self->max_vert) {
3151 self->pre_fullscreen_area.y = self->pre_max_area.y;
3152 self->pre_fullscreen_area.height = self->pre_max_area.height;
3153 }
3154
3155 /* these will help configure_full figure out where to fullscreen
3156 the window */
3157 x = self->area.x;
3158 y = self->area.y;
3159 w = self->area.width;
3160 h = self->area.height;
3161 } else {
3162 g_assert(self->pre_fullscreen_area.width > 0 &&
3163 self->pre_fullscreen_area.height > 0);
3164
3165 x = self->pre_fullscreen_area.x;
3166 y = self->pre_fullscreen_area.y;
3167 w = self->pre_fullscreen_area.width;
3168 h = self->pre_fullscreen_area.height;
3169 RECT_SET(self->pre_fullscreen_area, 0, 0, 0, 0);
3170 }
3171
3172 ob_debug("Window %s going fullscreen (%d)\n",
3173 self->title, self->fullscreen);
3174
3175 client_setup_decor_and_functions(self, FALSE);
3176 client_move_resize(self, x, y, w, h);
3177
3178 /* and adjust our layer/stacking. do this after resizing the window,
3179 and applying decorations, because windows which fill the screen are
3180 considered "fullscreen" and it affects their layer */
3181 client_calc_layer(self);
3182
3183 if (fs) {
3184 /* try focus us when we go into fullscreen mode */
3185 client_focus(self);
3186 }
3187 }
3188
3189 static void client_iconify_recursive(ObClient *self,
3190 gboolean iconic, gboolean curdesk,
3191 gboolean hide_animation)
3192 {
3193 GSList *it;
3194 gboolean changed = FALSE;
3195
3196 if (self->iconic != iconic) {
3197 ob_debug("%sconifying window: 0x%lx\n", (iconic ? "I" : "Uni"),
3198 self->window);
3199
3200 if (iconic) {
3201 /* don't let non-normal windows iconify along with their parents
3202 or whatever */
3203 if (client_normal(self)) {
3204 self->iconic = iconic;
3205
3206 /* update the focus lists.. iconic windows go to the bottom of
3207 the list */
3208 focus_order_to_bottom(self);
3209
3210 changed = TRUE;
3211 }
3212 } else {
3213 self->iconic = iconic;
3214
3215 if (curdesk && self->desktop != screen_desktop &&
3216 self->desktop != DESKTOP_ALL)
3217 client_set_desktop(self, screen_desktop, FALSE, FALSE);
3218
3219 /* this puts it after the current focused window */
3220 focus_order_remove(self);
3221 focus_order_add_new(self);
3222
3223 changed = TRUE;
3224 }
3225 }
3226
3227 if (changed) {
3228 client_change_state(self);
3229 if (config_animate_iconify && !hide_animation)
3230 frame_begin_iconify_animation(self->frame, iconic);
3231 /* do this after starting the animation so it doesn't flash */
3232 client_showhide(self);
3233 }
3234
3235 /* iconify all direct transients, and deiconify all transients
3236 (non-direct too) */
3237 for (it = self->transients; it; it = g_slist_next(it))
3238 if (it->data != self)
3239 if (client_is_direct_child(self, it->data) || !iconic)
3240 client_iconify_recursive(it->data, iconic, curdesk,
3241 hide_animation);
3242 }
3243
3244 void client_iconify(ObClient *self, gboolean iconic, gboolean curdesk,
3245 gboolean hide_animation)
3246 {
3247 if (self->functions & OB_CLIENT_FUNC_ICONIFY || !iconic) {
3248 /* move up the transient chain as far as possible first */
3249 self = client_search_top_direct_parent(self);
3250 client_iconify_recursive(self, iconic, curdesk, hide_animation);
3251 }
3252 }
3253
3254 void client_maximize(ObClient *self, gboolean max, gint dir)
3255 {
3256 gint x, y, w, h;
3257
3258 g_assert(dir == 0 || dir == 1 || dir == 2);
3259 if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE) && max) return;/* can't */
3260
3261 /* check if already done */
3262 if (max) {
3263 if (dir == 0 && self->max_horz && self->max_vert) return;
3264 if (dir == 1 && self->max_horz) return;
3265 if (dir == 2 && self->max_vert) return;
3266 } else {
3267 if (dir == 0 && !self->max_horz && !self->max_vert) return;
3268 if (dir == 1 && !self->max_horz) return;
3269 if (dir == 2 && !self->max_vert) return;
3270 }
3271
3272 /* these will help configure_full figure out which screen to fill with
3273 the window */
3274 x = self->area.x;
3275 y = self->area.y;
3276 w = self->area.width;
3277 h = self->area.height;
3278
3279 if (max) {
3280 if ((dir == 0 || dir == 1) && !self->max_horz) { /* horz */
3281 RECT_SET(self->pre_max_area,
3282 self->area.x, self->pre_max_area.y,
3283 self->area.width, self->pre_max_area.height);
3284 }
3285 if ((dir == 0 || dir == 2) && !self->max_vert) { /* vert */
3286 RECT_SET(self->pre_max_area,
3287 self->pre_max_area.x, self->area.y,
3288 self->pre_max_area.width, self->area.height);
3289 }
3290 } else {
3291 if ((dir == 0 || dir == 1) && self->max_horz) { /* horz */
3292 g_assert(self->pre_max_area.width > 0);
3293
3294 x = self->pre_max_area.x;
3295 w = self->pre_max_area.width;
3296
3297 RECT_SET(self->pre_max_area, 0, self->pre_max_area.y,
3298 0, self->pre_max_area.height);
3299 }
3300 if ((dir == 0 || dir == 2) && self->max_vert) { /* vert */
3301 g_assert(self->pre_max_area.height > 0);
3302
3303 y = self->pre_max_area.y;
3304 h = self->pre_max_area.height;
3305
3306 RECT_SET(self->pre_max_area, self->pre_max_area.x, 0,
3307 self->pre_max_area.width, 0);
3308 }
3309 }
3310
3311 if (dir == 0 || dir == 1) /* horz */
3312 self->max_horz = max;
3313 if (dir == 0 || dir == 2) /* vert */
3314 self->max_vert = max;
3315
3316 client_change_state(self); /* change the state hints on the client */
3317
3318 client_setup_decor_and_functions(self, FALSE);
3319 client_move_resize(self, x, y, w, h);
3320 }
3321
3322 void client_shade(ObClient *self, gboolean shade)
3323 {
3324 if ((!(self->functions & OB_CLIENT_FUNC_SHADE) &&
3325 shade) || /* can't shade */
3326 self->shaded == shade) return; /* already done */
3327
3328 self->shaded = shade;
3329 client_change_state(self);
3330 client_change_wm_state(self); /* the window is being hidden/shown */
3331 /* resize the frame to just the titlebar */
3332 frame_adjust_area(self->frame, FALSE, TRUE, FALSE);
3333 }
3334
3335 static void client_ping_event(ObClient *self, gboolean dead)
3336 {
3337 if (self->not_responding != dead) {
3338 self->not_responding = dead;
3339 client_update_title(self);
3340
3341 if (dead)
3342 /* the client isn't responding, so ask to kill it */
3343 client_prompt_kill(self);
3344 else {
3345 /* it came back to life ! */
3346
3347 if (self->kill_prompt) {
3348 prompt_unref(self->kill_prompt);
3349 self->kill_prompt = NULL;
3350 }
3351
3352 self->kill_level = 0;
3353 }
3354 }
3355 }
3356
3357 void client_close(ObClient *self)
3358 {
3359 if (!(self->functions & OB_CLIENT_FUNC_CLOSE)) return;
3360
3361 /* if closing an internal obprompt, that is just cancelling it */
3362 if (self->prompt) {
3363 prompt_cancel(self->prompt);
3364 return;
3365 }
3366
3367 /* in the case that the client provides no means to requesting that it
3368 close, we just kill it */
3369 if (!self->delete_window)
3370 /* don't use client_kill(), we should only kill based on PID in
3371 response to a lack of PING replies */
3372 XKillClient(ob_display, self->window);
3373 else {
3374 /* request the client to close with WM_DELETE_WINDOW */
3375 PROP_MSG_TO(self->window, self->window, wm_protocols,
3376 prop_atoms.wm_delete_window, event_curtime, 0, 0, 0,
3377 NoEventMask);
3378
3379 /* we're trying to close the window, so see if it is responding. if it
3380 is not, then we will let them kill the window */
3381 if (self->ping)
3382 ping_start(self, client_ping_event);
3383
3384 /* if we already know the window isn't responding (maybe they clicked
3385 no in the kill dialog but it hasn't come back to life), then show
3386 the kill dialog */
3387 if (self->not_responding)
3388 client_prompt_kill(self);
3389 }
3390 }
3391
3392 #define OB_KILL_RESULT_NO 0
3393 #define OB_KILL_RESULT_YES 1
3394
3395 static void client_kill_requested(ObPrompt *p, gint result, gpointer data)
3396 {
3397 ObClient *self = data;
3398
3399 if (result == OB_KILL_RESULT_YES)
3400 client_kill(self);
3401
3402 prompt_unref(self->kill_prompt);
3403 self->kill_prompt = NULL;
3404 }
3405
3406 static void client_prompt_kill(ObClient *self)
3407 {
3408 /* check if we're already prompting */
3409 if (!self->kill_prompt) {
3410 ObPromptAnswer answers[] = {
3411 { 0, OB_KILL_RESULT_NO },
3412 { 0, OB_KILL_RESULT_YES }
3413 };
3414 gchar *m;
3415 const gchar *y, *title;
3416
3417 title = self->original_title;
3418 if (title[0] == '\0') {
3419 /* empty string, so use its parent */
3420 ObClient *p = client_search_top_direct_parent(self);
3421 if (p) title = p->original_title;
3422 }
3423
3424 if (client_on_localhost(self)) {
3425 const gchar *sig;
3426
3427 if (self->kill_level == 0)
3428 sig = "terminate";
3429 else
3430 sig = "kill";
3431
3432 m = g_strdup_printf
3433 (_("The window \"%s\" does not seem to be responding. Do you want to force it to exit by sending the %s signal?"),
3434 title, sig);
3435 y = _("End Process");
3436 }
3437 else {
3438 m = g_strdup_printf
3439 (_("The window \"%s\" does not seem to be responding. Do you want to disconnect it from the X server?"),
3440 title);
3441 y = _("Disconnect");
3442 }
3443 /* set the dialog buttons' text */
3444 answers[0].text = _("Cancel"); /* "no" */
3445 answers[1].text = y; /* "yes" */
3446
3447 self->kill_prompt = prompt_new(m, answers,
3448 sizeof(answers)/sizeof(answers[0]),
3449 OB_KILL_RESULT_NO, /* default = no */
3450 OB_KILL_RESULT_NO, /* cancel = no */
3451 client_kill_requested, self);
3452 g_free(m);
3453 }
3454
3455 prompt_show(self->kill_prompt, self, TRUE);
3456 }
3457
3458 void client_kill(ObClient *self)
3459 {
3460 /* don't kill our own windows */
3461 if (self->prompt) return;
3462
3463 if (client_on_localhost(self) && self->pid) {
3464 /* running on the local host */
3465 if (self->kill_level == 0) {
3466 ob_debug("killing window 0x%x with pid %lu, with SIGTERM",
3467 self->window, self->pid);
3468 kill(self->pid, SIGTERM);
3469 ++self->kill_level;
3470
3471 /* show that we're trying to kill it */
3472 client_update_title(self);
3473 }
3474 else {
3475 ob_debug("killing window 0x%x with pid %lu, with SIGKILL\n",
3476 self->window, self->pid);
3477 kill(self->pid, SIGKILL); /* kill -9 */
3478 }
3479 }
3480 else {
3481 /* running on a remote host */
3482 XKillClient(ob_display, self->window);
3483 }
3484 }
3485
3486 void client_hilite(ObClient *self, gboolean hilite)
3487 {
3488 if (self->demands_attention == hilite)
3489 return; /* no change */
3490
3491 /* don't allow focused windows to hilite */
3492 self->demands_attention = hilite && !client_focused(self);
3493 if (self->frame != NULL) { /* if we're mapping, just set the state */
3494 if (self->demands_attention)
3495 frame_flash_start(self->frame);
3496 else
3497 frame_flash_stop(self->frame);
3498 client_change_state(self);
3499 }
3500 }
3501
3502 static void client_set_desktop_recursive(ObClient *self,
3503 guint target,
3504 gboolean donthide,
3505 gboolean dontraise)
3506 {
3507 guint old;
3508 GSList *it;
3509
3510 if (target != self->desktop && self->type != OB_CLIENT_TYPE_DESKTOP) {
3511
3512 ob_debug("Setting desktop %u\n", target+1);
3513
3514 g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
3515
3516 old = self->desktop;
3517 self->desktop = target;
3518 PROP_SET32(self->window, net_wm_desktop, cardinal, target);
3519 /* the frame can display the current desktop state */
3520 frame_adjust_state(self->frame);
3521 /* 'move' the window to the new desktop */
3522 if (!donthide)
3523 client_hide(self);
3524 client_show(self);
3525 /* raise if it was not already on the desktop */
3526 if (old != DESKTOP_ALL && !dontraise)
3527 stacking_raise(CLIENT_AS_WINDOW(self));
3528 if (STRUT_EXISTS(self->strut))
3529 screen_update_areas();
3530 else
3531 /* the new desktop's geometry may be different, so we may need to
3532 resize, for example if we are maximized */
3533 client_reconfigure(self, FALSE);
3534 }
3535
3536 /* move all transients */
3537 for (it = self->transients; it; it = g_slist_next(it))
3538 if (it->data != self)
3539 if (client_is_direct_child(self, it->data))
3540 client_set_desktop_recursive(it->data, target,
3541 donthide, dontraise);
3542 }
3543
3544 void client_set_desktop(ObClient *self, guint target,
3545 gboolean donthide, gboolean dontraise)
3546 {
3547 self = client_search_top_direct_parent(self);
3548 client_set_desktop_recursive(self, target, donthide, dontraise);
3549 }
3550
3551 gboolean client_is_direct_child(ObClient *parent, ObClient *child)
3552 {
3553 while (child != parent && (child = client_direct_parent(child)));
3554 return child == parent;
3555 }
3556
3557 ObClient *client_search_modal_child(ObClient *self)
3558 {
3559 GSList *it;
3560 ObClient *ret;
3561
3562 for (it = self->transients; it; it = g_slist_next(it)) {
3563 ObClient *c = it->data;
3564 if ((ret = client_search_modal_child(c))) return ret;
3565 if (c->modal) return c;
3566 }
3567 return NULL;
3568 }
3569
3570 gboolean client_validate(ObClient *self)
3571 {
3572 XEvent e;
3573
3574 XSync(ob_display, FALSE); /* get all events on the server */
3575
3576 if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
3577 XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
3578 XPutBackEvent(ob_display, &e);
3579 return FALSE;
3580 }
3581
3582 return TRUE;
3583 }
3584
3585 void client_set_wm_state(ObClient *self, glong state)
3586 {
3587 if (state == self->wmstate) return; /* no change */
3588
3589 switch (state) {
3590 case IconicState:
3591 client_iconify(self, TRUE, TRUE, FALSE);
3592 break;
3593 case NormalState:
3594 client_iconify(self, FALSE, TRUE, FALSE);
3595 break;
3596 }
3597 }
3598
3599 void client_set_state(ObClient *self, Atom action, glong data1, glong data2)
3600 {
3601 gboolean shaded = self->shaded;
3602 gboolean fullscreen = self->fullscreen;
3603 gboolean undecorated = self->undecorated;
3604 gboolean max_horz = self->max_horz;
3605 gboolean max_vert = self->max_vert;
3606 gboolean modal = self->modal;
3607 gboolean iconic = self->iconic;
3608 gboolean demands_attention = self->demands_attention;
3609 gboolean above = self->above;
3610 gboolean below = self->below;
3611 gint i;
3612
3613 if (!(action == prop_atoms.net_wm_state_add ||
3614 action == prop_atoms.net_wm_state_remove ||
3615 action == prop_atoms.net_wm_state_toggle))
3616 /* an invalid action was passed to the client message, ignore it */
3617 return;
3618
3619 for (i = 0; i < 2; ++i) {
3620 Atom state = i == 0 ? data1 : data2;
3621
3622 if (!state) continue;
3623
3624 /* if toggling, then pick whether we're adding or removing */
3625 if (action == prop_atoms.net_wm_state_toggle) {
3626 if (state == prop_atoms.net_wm_state_modal)
3627 action = modal ? prop_atoms.net_wm_state_remove :
3628 prop_atoms.net_wm_state_add;
3629 else if (state == prop_atoms.net_wm_state_maximized_vert)
3630 action = self->max_vert ? prop_atoms.net_wm_state_remove :
3631 prop_atoms.net_wm_state_add;
3632 else if (state == prop_atoms.net_wm_state_maximized_horz)
3633 action = self->max_horz ? prop_atoms.net_wm_state_remove :
3634 prop_atoms.net_wm_state_add;
3635 else if (state == prop_atoms.net_wm_state_shaded)
3636 action = shaded ? prop_atoms.net_wm_state_remove :
3637 prop_atoms.net_wm_state_add;
3638 else if (state == prop_atoms.net_wm_state_skip_taskbar)
3639 action = self->skip_taskbar ?
3640 prop_atoms.net_wm_state_remove :
3641 prop_atoms.net_wm_state_add;
3642 else if (state == prop_atoms.net_wm_state_skip_pager)
3643 action = self->skip_pager ?
3644 prop_atoms.net_wm_state_remove :
3645 prop_atoms.net_wm_state_add;
3646 else if (state == prop_atoms.net_wm_state_hidden)
3647 action = self->iconic ?
3648 prop_atoms.net_wm_state_remove :
3649 prop_atoms.net_wm_state_add;
3650 else if (state == prop_atoms.net_wm_state_fullscreen)
3651 action = fullscreen ?
3652 prop_atoms.net_wm_state_remove :
3653 prop_atoms.net_wm_state_add;
3654 else if (state == prop_atoms.net_wm_state_above)
3655 action = self->above ? prop_atoms.net_wm_state_remove :
3656 prop_atoms.net_wm_state_add;
3657 else if (state == prop_atoms.net_wm_state_below)
3658 action = self->below ? prop_atoms.net_wm_state_remove :
3659 prop_atoms.net_wm_state_add;
3660 else if (state == prop_atoms.net_wm_state_demands_attention)
3661 action = self->demands_attention ?
3662 prop_atoms.net_wm_state_remove :
3663 prop_atoms.net_wm_state_add;
3664 else if (state == prop_atoms.ob_wm_state_undecorated)
3665 action = undecorated ? prop_atoms.net_wm_state_remove :
3666 prop_atoms.net_wm_state_add;
3667 }
3668
3669 if (action == prop_atoms.net_wm_state_add) {
3670 if (state == prop_atoms.net_wm_state_modal) {
3671 modal = TRUE;
3672 } else if (state == prop_atoms.net_wm_state_maximized_vert) {
3673 max_vert = TRUE;
3674 } else if (state == prop_atoms.net_wm_state_maximized_horz) {
3675 max_horz = TRUE;
3676 } else if (state == prop_atoms.net_wm_state_shaded) {
3677 shaded = TRUE;
3678 } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
3679 self->skip_taskbar = TRUE;
3680 } else if (state == prop_atoms.net_wm_state_skip_pager) {
3681 self->skip_pager = TRUE;
3682 } else if (state == prop_atoms.net_wm_state_hidden) {
3683 iconic = TRUE;
3684 } else if (state == prop_atoms.net_wm_state_fullscreen) {
3685 fullscreen = TRUE;
3686 } else if (state == prop_atoms.net_wm_state_above) {
3687 above = TRUE;
3688 below = FALSE;
3689 } else if (state == prop_atoms.net_wm_state_below) {
3690 above = FALSE;
3691 below = TRUE;
3692 } else if (state == prop_atoms.net_wm_state_demands_attention) {
3693 demands_attention = TRUE;
3694 } else if (state == prop_atoms.ob_wm_state_undecorated) {
3695 undecorated = TRUE;
3696 }
3697
3698 } else { /* action == prop_atoms.net_wm_state_remove */
3699 if (state == prop_atoms.net_wm_state_modal) {
3700 modal = FALSE;
3701 } else if (state == prop_atoms.net_wm_state_maximized_vert) {
3702 max_vert = FALSE;
3703 } else if (state == prop_atoms.net_wm_state_maximized_horz) {
3704 max_horz = FALSE;
3705 } else if (state == prop_atoms.net_wm_state_shaded) {
3706 shaded = FALSE;
3707 } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
3708 self->skip_taskbar = FALSE;
3709 } else if (state == prop_atoms.net_wm_state_skip_pager) {
3710 self->skip_pager = FALSE;
3711 } else if (state == prop_atoms.net_wm_state_hidden) {
3712 iconic = FALSE;
3713 } else if (state == prop_atoms.net_wm_state_fullscreen) {
3714 fullscreen = FALSE;
3715 } else if (state == prop_atoms.net_wm_state_above) {
3716 above = FALSE;
3717 } else if (state == prop_atoms.net_wm_state_below) {
3718 below = FALSE;
3719 } else if (state == prop_atoms.net_wm_state_demands_attention) {
3720 demands_attention = FALSE;
3721 } else if (state == prop_atoms.ob_wm_state_undecorated) {
3722 undecorated = FALSE;
3723 }
3724 }
3725 }
3726
3727 if (max_horz != self->max_horz || max_vert != self->max_vert) {
3728 if (max_horz != self->max_horz && max_vert != self->max_vert) {
3729 /* toggling both */
3730 if (max_horz == max_vert) { /* both going the same way */
3731 client_maximize(self, max_horz, 0);
3732 } else {
3733 client_maximize(self, max_horz, 1);
3734 client_maximize(self, max_vert, 2);
3735 }
3736 } else {
3737 /* toggling one */
3738 if (max_horz != self->max_horz)
3739 client_maximize(self, max_horz, 1);
3740 else
3741 client_maximize(self, max_vert, 2);
3742 }
3743 }
3744 /* change fullscreen state before shading, as it will affect if the window
3745 can shade or not */
3746 if (fullscreen != self->fullscreen)
3747 client_fullscreen(self, fullscreen);
3748 if (shaded != self->shaded)
3749 client_shade(self, shaded);
3750 if (undecorated != self->undecorated)
3751 client_set_undecorated(self, undecorated);
3752 if (above != self->above || below != self->below) {
3753 self->above = above;
3754 self->below = below;
3755 client_calc_layer(self);
3756 }
3757
3758 if (modal != self->modal) {
3759 self->modal = modal;
3760 /* when a window changes modality, then its stacking order with its
3761 transients needs to change */
3762 stacking_raise(CLIENT_AS_WINDOW(self));
3763
3764 /* it also may get focused. if something is focused that shouldn't
3765 be focused anymore, then move the focus */
3766 if (focus_client && client_focus_target(focus_client) != focus_client)
3767 client_focus(focus_client);
3768 }
3769
3770 if (iconic != self->iconic)
3771 client_iconify(self, iconic, FALSE, FALSE);
3772
3773 if (demands_attention != self->demands_attention)
3774 client_hilite(self, demands_attention);
3775
3776 client_change_state(self); /* change the hint to reflect these changes */
3777 }
3778
3779 ObClient *client_focus_target(ObClient *self)
3780 {
3781 ObClient *child = NULL;
3782
3783 child = client_search_modal_child(self);
3784 if (child) return child;
3785 return self;
3786 }
3787
3788 gboolean client_can_focus(ObClient *self)
3789 {
3790 /* choose the correct target */
3791 self = client_focus_target(self);
3792
3793 if (!self->frame->visible)
3794 return FALSE;
3795
3796 if (!(self->can_focus || self->focus_notify))
3797 return FALSE;
3798
3799 return TRUE;
3800 }
3801
3802 gboolean client_focus(ObClient *self)
3803 {
3804 /* we might not focus this window, so if we have modal children which would
3805 be focused instead, bring them to this desktop */
3806 client_bring_modal_windows(self);
3807
3808 /* choose the correct target */
3809 self = client_focus_target(self);
3810
3811 if (!client_can_focus(self)) {
3812 ob_debug_type(OB_DEBUG_FOCUS,
3813 "Client %s can't be focused\n", self->title);
3814 return FALSE;
3815 }
3816
3817 ob_debug_type(OB_DEBUG_FOCUS,
3818 "Focusing client \"%s\" (0x%x) at time %u\n",
3819 self->title, self->window, event_curtime);
3820
3821 /* if using focus_delay, stop the timer now so that focus doesn't
3822 go moving on us */
3823 event_halt_focus_delay();
3824
3825 event_cancel_all_key_grabs();
3826
3827 xerror_set_ignore(TRUE);
3828 xerror_occured = FALSE;
3829
3830 if (self->can_focus) {
3831 /* This can cause a BadMatch error with CurrentTime, or if an app
3832 passed in a bad time for _NET_WM_ACTIVE_WINDOW. */
3833 XSetInputFocus(ob_display, self->window, RevertToPointerRoot,
3834 event_curtime);
3835 }
3836
3837 if (self->focus_notify) {
3838 XEvent ce;
3839 ce.xclient.type = ClientMessage;
3840 ce.xclient.message_type = prop_atoms.wm_protocols;
3841 ce.xclient.display = ob_display;
3842 ce.xclient.window = self->window;
3843 ce.xclient.format = 32;
3844 ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
3845 ce.xclient.data.l[1] = event_curtime;
3846 ce.xclient.data.l[2] = 0l;
3847 ce.xclient.data.l[3] = 0l;
3848 ce.xclient.data.l[4] = 0l;
3849 XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
3850 }
3851
3852 xerror_set_ignore(FALSE);
3853
3854 ob_debug_type(OB_DEBUG_FOCUS, "Error focusing? %d\n", xerror_occured);
3855 return !xerror_occured;
3856 }
3857
3858 static void client_present(ObClient *self, gboolean here, gboolean raise,
3859 gboolean unshade)
3860 {
3861 if (client_normal(self) && screen_showing_desktop)
3862 screen_show_desktop(FALSE, self);
3863 if (self->iconic)
3864 client_iconify(self, FALSE, here, FALSE);
3865 if (self->desktop != DESKTOP_ALL &&
3866 self->desktop != screen_desktop)
3867 {
3868 if (here)
3869 client_set_desktop(self, screen_desktop, FALSE, TRUE);
3870 else
3871 screen_set_desktop(self->desktop, FALSE);
3872 } else if (!self->frame->visible)
3873 /* if its not visible for other reasons, then don't mess
3874 with it */
3875 return;
3876 if (self->shaded && unshade)
3877 client_shade(self, FALSE);
3878 if (raise)
3879 stacking_raise(CLIENT_AS_WINDOW(self));
3880
3881 client_focus(self);
3882 }
3883
3884 /* this function exists to map to the client_activate message in the ewmh,
3885 the user arg is unused because nobody uses it correctly anyway. */
3886 void client_activate(ObClient *self, gboolean here, gboolean raise,
3887 gboolean unshade, gboolean user)
3888 {
3889 client_present(self, here, raise, unshade);
3890 }
3891
3892 static void client_bring_windows_recursive(ObClient *self,
3893 guint desktop,
3894 gboolean helpers,
3895 gboolean modals,
3896 gboolean iconic)
3897 {
3898 GSList *it;
3899
3900 for (it = self->transients; it; it = g_slist_next(it))
3901 client_bring_windows_recursive(it->data, desktop,
3902 helpers, modals, iconic);
3903
3904 if (((helpers && client_helper(self)) ||
3905 (modals && self->modal)) &&
3906 ((self->desktop != desktop && self->desktop != DESKTOP_ALL) ||
3907 (iconic && self->iconic)))
3908 {
3909 if (iconic && self->iconic)
3910 client_iconify(self, FALSE, TRUE, FALSE);
3911 else
3912 client_set_desktop(self, desktop, FALSE, FALSE);
3913 }
3914 }
3915
3916 void client_bring_helper_windows(ObClient *self)
3917 {
3918 client_bring_windows_recursive(self, self->desktop, TRUE, FALSE, FALSE);
3919 }
3920
3921 void client_bring_modal_windows(ObClient *self)
3922 {
3923 client_bring_windows_recursive(self, self->desktop, FALSE, TRUE, TRUE);
3924 }
3925
3926 gboolean client_focused(ObClient *self)
3927 {
3928 return self == focus_client;
3929 }
3930
3931 RrImage* client_icon(ObClient *self)
3932 {
3933 RrImage *ret = NULL;
3934
3935 if (self->icon_set)
3936 ret = self->icon_set;
3937 else if (self->parents) {
3938 GSList *it;
3939 for (it = self->parents; it && !ret; it = g_slist_next(it))
3940 ret = client_icon(it->data);
3941 }
3942 if (!ret)
3943 ret = client_default_icon;
3944 return ret;
3945 }
3946
3947 void client_set_layer(ObClient *self, gint layer)
3948 {
3949 if (layer < 0) {
3950 self->below = TRUE;
3951 self->above = FALSE;
3952 } else if (layer == 0) {
3953 self->below = self->above = FALSE;
3954 } else {
3955 self->below = FALSE;
3956 self->above = TRUE;
3957 }
3958 client_calc_layer(self);
3959 client_change_state(self); /* reflect this in the state hints */
3960 }
3961
3962 void client_set_undecorated(ObClient *self, gboolean undecorated)
3963 {
3964 if (self->undecorated != undecorated &&
3965 /* don't let it undecorate if the function is missing, but let
3966 it redecorate */
3967 (self->functions & OB_CLIENT_FUNC_UNDECORATE || !undecorated))
3968 {
3969 self->undecorated = undecorated;
3970 client_setup_decor_and_functions(self, TRUE);
3971 client_change_state(self); /* reflect this in the state hints */
3972 }
3973 }
3974
3975 guint client_monitor(ObClient *self)
3976 {
3977 return screen_find_monitor(&self->frame->area);
3978 }
3979
3980 ObClient *client_direct_parent(ObClient *self)
3981 {
3982 if (!self->parents) return NULL;
3983 if (self->transient_for_group) return NULL;
3984 return self->parents->data;
3985 }
3986
3987 ObClient *client_search_top_direct_parent(ObClient *self)
3988 {
3989 ObClient *p;
3990 while ((p = client_direct_parent(self))) self = p;
3991 return self;
3992 }
3993
3994 static GSList *client_search_all_top_parents_internal(ObClient *self,
3995 gboolean bylayer,
3996 ObStackingLayer layer)
3997 {
3998 GSList *ret;
3999 ObClient *p;
4000
4001 /* move up the direct transient chain as far as possible */
4002 while ((p = client_direct_parent(self)) &&
4003 (!bylayer || p->layer == layer))
4004 self = p;
4005
4006 if (!self->parents)
4007 ret = g_slist_prepend(NULL, self);
4008 else
4009 ret = g_slist_copy(self->parents);
4010
4011 return ret;
4012 }
4013
4014 GSList *client_search_all_top_parents(ObClient *self)
4015 {
4016 return client_search_all_top_parents_internal(self, FALSE, 0);
4017 }
4018
4019 GSList *client_search_all_top_parents_layer(ObClient *self)
4020 {
4021 return client_search_all_top_parents_internal(self, TRUE, self->layer);
4022 }
4023
4024 ObClient *client_search_focus_parent(ObClient *self)
4025 {
4026 GSList *it;
4027
4028 for (it = self->parents; it; it = g_slist_next(it))
4029 if (client_focused(it->data)) return it->data;
4030
4031 return NULL;
4032 }
4033
4034 ObClient *client_search_parent(ObClient *self, ObClient *search)
4035 {
4036 GSList *it;
4037
4038 for (it = self->parents; it; it = g_slist_next(it))
4039 if (it->data == search) return search;
4040
4041 return NULL;
4042 }
4043
4044 ObClient *client_search_transient(ObClient *self, ObClient *search)
4045 {
4046 GSList *sit;
4047
4048 for (sit = self->transients; sit; sit = g_slist_next(sit)) {
4049 if (sit->data == search)
4050 return search;
4051 if (client_search_transient(sit->data, search))
4052 return search;
4053 }
4054 return NULL;
4055 }
4056
4057 static void detect_edge(Rect area, ObDirection dir,
4058 gint my_head, gint my_size,
4059 gint my_edge_start, gint my_edge_size,
4060 gint *dest, gboolean *near_edge)
4061 {
4062 gint edge_start, edge_size, head, tail;
4063 gboolean skip_head = FALSE, skip_tail = FALSE;
4064
4065 switch (dir) {
4066 case OB_DIRECTION_NORTH:
4067 case OB_DIRECTION_SOUTH:
4068 edge_start = area.x;
4069 edge_size = area.width;
4070 break;
4071 case OB_DIRECTION_EAST:
4072 case OB_DIRECTION_WEST:
4073 edge_start = area.y;
4074 edge_size = area.height;
4075 break;
4076 default:
4077 g_assert_not_reached();
4078 }
4079
4080 /* do we collide with this window? */
4081 if (!RANGES_INTERSECT(my_edge_start, my_edge_size,
4082 edge_start, edge_size))
4083 return;
4084
4085 switch (dir) {
4086 case OB_DIRECTION_NORTH:
4087 head = RECT_BOTTOM(area);
4088 tail = RECT_TOP(area);
4089 break;
4090 case OB_DIRECTION_SOUTH:
4091 head = RECT_TOP(area);
4092 tail = RECT_BOTTOM(area);
4093 break;
4094 case OB_DIRECTION_WEST:
4095 head = RECT_RIGHT(area);
4096 tail = RECT_LEFT(area);
4097 break;
4098 case OB_DIRECTION_EAST:
4099 head = RECT_LEFT(area);
4100 tail = RECT_RIGHT(area);
4101 break;
4102 default:
4103 g_assert_not_reached();
4104 }
4105 switch (dir) {
4106 case OB_DIRECTION_NORTH:
4107 case OB_DIRECTION_WEST:
4108 /* check if our window is past the head of this window */
4109 if (my_head <= head + 1)
4110 skip_head = TRUE;
4111 /* check if our window's tail is past the tail of this window */
4112 if (my_head + my_size - 1 <= tail)
4113 skip_tail = TRUE;
4114 /* check if the head of this window is closer than the previously
4115 chosen edge (take into account that the previously chosen
4116 edge might have been a tail, not a head) */
4117 if (head + (*near_edge ? 0 : my_size) <= *dest)
4118 skip_head = TRUE;
4119 /* check if the tail of this window is closer than the previously
4120 chosen edge (take into account that the previously chosen
4121 edge might have been a head, not a tail) */
4122 if (tail - (!*near_edge ? 0 : my_size) <= *dest)
4123 skip_tail = TRUE;
4124 break;
4125 case OB_DIRECTION_SOUTH:
4126 case OB_DIRECTION_EAST:
4127 /* check if our window is past the head of this window */
4128 if (my_head >= head - 1)
4129 skip_head = TRUE;
4130 /* check if our window's tail is past the tail of this window */
4131 if (my_head - my_size + 1 >= tail)
4132 skip_tail = TRUE;
4133 /* check if the head of this window is closer than the previously
4134 chosen edge (take into account that the previously chosen
4135 edge might have been a tail, not a head) */
4136 if (head - (*near_edge ? 0 : my_size) >= *dest)
4137 skip_head = TRUE;
4138 /* check if the tail of this window is closer than the previously
4139 chosen edge (take into account that the previously chosen
4140 edge might have been a head, not a tail) */
4141 if (tail + (!*near_edge ? 0 : my_size) >= *dest)
4142 skip_tail = TRUE;
4143 break;
4144 default:
4145 g_assert_not_reached();
4146 }
4147
4148 ob_debug("my head %d size %d\n", my_head, my_size);
4149 ob_debug("head %d tail %d dest %d\n", head, tail, *dest);
4150 if (!skip_head) {
4151 ob_debug("using near edge %d\n", head);
4152 *dest = head;
4153 *near_edge = TRUE;
4154 }
4155 else if (!skip_tail) {
4156 ob_debug("using far edge %d\n", tail);
4157 *dest = tail;
4158 *near_edge = FALSE;
4159 }
4160 }
4161
4162 void client_find_edge_directional(ObClient *self, ObDirection dir,
4163 gint my_head, gint my_size,
4164 gint my_edge_start, gint my_edge_size,
4165 gint *dest, gboolean *near_edge)
4166 {
4167 GList *it;
4168 Rect *a, *mon;
4169 Rect dock_area;
4170 gint edge;
4171
4172 a = screen_area(self->desktop, SCREEN_AREA_ALL_MONITORS,
4173 &self->frame->area);
4174 mon = screen_area(self->desktop, SCREEN_AREA_ONE_MONITOR,
4175 &self->frame->area);
4176
4177 switch (dir) {
4178 case OB_DIRECTION_NORTH:
4179 if (my_head >= RECT_TOP(*mon) + 1)
4180 edge = RECT_TOP(*mon) - 1;
4181 else
4182 edge = RECT_TOP(*a) - 1;
4183 break;
4184 case OB_DIRECTION_SOUTH:
4185 if (my_head <= RECT_BOTTOM(*mon) - 1)
4186 edge = RECT_BOTTOM(*mon) + 1;
4187 else
4188 edge = RECT_BOTTOM(*a) + 1;
4189 break;
4190 case OB_DIRECTION_EAST:
4191 if (my_head <= RECT_RIGHT(*mon) - 1)
4192 edge = RECT_RIGHT(*mon) + 1;
4193 else
4194 edge = RECT_RIGHT(*a) + 1;
4195 break;
4196 case OB_DIRECTION_WEST:
4197 if (my_head >= RECT_LEFT(*mon) + 1)
4198 edge = RECT_LEFT(*mon) - 1;
4199 else
4200 edge = RECT_LEFT(*a) - 1;
4201 break;
4202 default:
4203 g_assert_not_reached();
4204 }
4205 /* default to the far edge, then narrow it down */
4206 *dest = edge;
4207 *near_edge = TRUE;
4208
4209 for (it = client_list; it; it = g_list_next(it)) {
4210 ObClient *cur = it->data;
4211
4212 /* skip windows to not bump into */
4213 if (cur == self)
4214 continue;
4215 if (cur->iconic)
4216 continue;
4217 if (self->desktop != cur->desktop && cur->desktop != DESKTOP_ALL &&
4218 cur->desktop != screen_desktop)
4219 continue;
4220
4221 ob_debug("trying window %s\n", cur->title);
4222
4223 detect_edge(cur->frame->area, dir, my_head, my_size, my_edge_start,
4224 my_edge_size, dest, near_edge);
4225 }
4226 dock_get_area(&dock_area);
4227 detect_edge(dock_area, dir, my_head, my_size, my_edge_start,
4228 my_edge_size, dest, near_edge);
4229 g_free(a);
4230 g_free(mon);
4231 }
4232
4233 void client_find_move_directional(ObClient *self, ObDirection dir,
4234 gint *x, gint *y)
4235 {
4236 gint head, size;
4237 gint e, e_start, e_size;
4238 gboolean near;
4239
4240 switch (dir) {
4241 case OB_DIRECTION_EAST:
4242 head = RECT_RIGHT(self->frame->area);
4243 size = self->frame->area.width;
4244 e_start = RECT_TOP(self->frame->area);
4245 e_size = self->frame->area.height;
4246 break;
4247 case OB_DIRECTION_WEST:
4248 head = RECT_LEFT(self->frame->area);
4249 size = self->frame->area.width;
4250 e_start = RECT_TOP(self->frame->area);
4251 e_size = self->frame->area.height;
4252 break;
4253 case OB_DIRECTION_NORTH:
4254 head = RECT_TOP(self->frame->area);
4255 size = self->frame->area.height;
4256 e_start = RECT_LEFT(self->frame->area);
4257 e_size = self->frame->area.width;
4258 break;
4259 case OB_DIRECTION_SOUTH:
4260 head = RECT_BOTTOM(self->frame->area);
4261 size = self->frame->area.height;
4262 e_start = RECT_LEFT(self->frame->area);
4263 e_size = self->frame->area.width;
4264 break;
4265 default:
4266 g_assert_not_reached();
4267 }
4268
4269 client_find_edge_directional(self, dir, head, size,
4270 e_start, e_size, &e, &near);
4271 *x = self->frame->area.x;
4272 *y = self->frame->area.y;
4273 switch (dir) {
4274 case OB_DIRECTION_EAST:
4275 if (near) e -= self->frame->area.width;
4276 else e++;
4277 *x = e;
4278 break;
4279 case OB_DIRECTION_WEST:
4280 if (near) e++;
4281 else e -= self->frame->area.width;
4282 *x = e;
4283 break;
4284 case OB_DIRECTION_NORTH:
4285 if (near) e++;
4286 else e -= self->frame->area.height;
4287 *y = e;
4288 break;
4289 case OB_DIRECTION_SOUTH:
4290 if (near) e -= self->frame->area.height;
4291 else e++;
4292 *y = e;
4293 break;
4294 default:
4295 g_assert_not_reached();
4296 }
4297 frame_frame_gravity(self->frame, x, y);
4298 }
4299
4300 void client_find_resize_directional(ObClient *self, ObDirection side,
4301 gboolean grow,
4302 gint *x, gint *y, gint *w, gint *h)
4303 {
4304 gint head;
4305 gint e, e_start, e_size, delta;
4306 gboolean near;
4307 ObDirection dir;
4308
4309 switch (side) {
4310 case OB_DIRECTION_EAST:
4311 head = RECT_RIGHT(self->frame->area) +
4312 (self->size_inc.width - 1) * (grow ? 1 : 0);
4313 e_start = RECT_TOP(self->frame->area);
4314 e_size = self->frame->area.height;
4315 dir = grow ? OB_DIRECTION_EAST : OB_DIRECTION_WEST;
4316 break;
4317 case OB_DIRECTION_WEST:
4318 head = RECT_LEFT(self->frame->area) -
4319 (self->size_inc.width - 1) * (grow ? 1 : 0);
4320 e_start = RECT_TOP(self->frame->area);
4321 e_size = self->frame->area.height;
4322 dir = grow ? OB_DIRECTION_WEST : OB_DIRECTION_EAST;
4323 break;
4324 case OB_DIRECTION_NORTH:
4325 head = RECT_TOP(self->frame->area) -
4326 (self->size_inc.height - 1) * (grow ? 1 : 0);
4327 e_start = RECT_LEFT(self->frame->area);
4328 e_size = self->frame->area.width;
4329 dir = grow ? OB_DIRECTION_NORTH : OB_DIRECTION_SOUTH;
4330 break;
4331 case OB_DIRECTION_SOUTH:
4332 head = RECT_BOTTOM(self->frame->area) +
4333 (self->size_inc.height - 1) * (grow ? 1 : 0);
4334 e_start = RECT_LEFT(self->frame->area);
4335 e_size = self->frame->area.width;
4336 dir = grow ? OB_DIRECTION_SOUTH : OB_DIRECTION_NORTH;
4337 break;
4338 default:
4339 g_assert_not_reached();
4340 }
4341
4342 ob_debug("head %d dir %d\n", head, dir);
4343 client_find_edge_directional(self, dir, head, 1,
4344 e_start, e_size, &e, &near);
4345 ob_debug("edge %d\n", e);
4346 *x = self->frame->area.x;
4347 *y = self->frame->area.y;
4348 *w = self->frame->area.width;
4349 *h = self->frame->area.height;
4350 switch (side) {
4351 case OB_DIRECTION_EAST:
4352 if (grow == near) --e;
4353 delta = e - RECT_RIGHT(self->frame->area);
4354 *w += delta;
4355 break;
4356 case OB_DIRECTION_WEST:
4357 if (grow == near) ++e;
4358 delta = RECT_LEFT(self->frame->area) - e;
4359 *x -= delta;
4360 *w += delta;
4361 break;
4362 case OB_DIRECTION_NORTH:
4363 if (grow == near) ++e;
4364 delta = RECT_TOP(self->frame->area) - e;
4365 *y -= delta;
4366 *h += delta;
4367 break;
4368 case OB_DIRECTION_SOUTH:
4369 if (grow == near) --e;
4370 delta = e - RECT_BOTTOM(self->frame->area);
4371 *h += delta;
4372 break;
4373 default:
4374 g_assert_not_reached();
4375 }
4376 frame_frame_gravity(self->frame, x, y);
4377 *w -= self->frame->size.left + self->frame->size.right;
4378 *h -= self->frame->size.top + self->frame->size.bottom;
4379 }
4380
4381 ObClient* client_under_pointer(void)
4382 {
4383 gint x, y;
4384 GList *it;
4385 ObClient *ret = NULL;
4386
4387 if (screen_pointer_pos(&x, &y)) {
4388 for (it = stacking_list; it; it = g_list_next(it)) {
4389 if (WINDOW_IS_CLIENT(it->data)) {
4390 ObClient *c = WINDOW_AS_CLIENT(it->data);
4391 if (c->frame->visible &&
4392 /* check the desktop, this is done during desktop
4393 switching and windows are shown/hidden status is not
4394 reliable */
4395 (c->desktop == screen_desktop ||
4396 c->desktop == DESKTOP_ALL) &&
4397 /* ignore all animating windows */
4398 !frame_iconify_animating(c->frame) &&
4399 RECT_CONTAINS(c->frame->area, x, y))
4400 {
4401 ret = c;
4402 break;
4403 }
4404 }
4405 }
4406 }
4407 return ret;
4408 }
4409
4410 gboolean client_has_group_siblings(ObClient *self)
4411 {
4412 return self->group && self->group->members->next;
4413 }
4414
4415 /*! Returns TRUE if the client is running on the same machine as Openbox */
4416 gboolean client_on_localhost(ObClient *self)
4417 {
4418 return self->client_machine == NULL;
4419 }
This page took 0.239987 seconds and 4 git commands to generate.