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