-
Notifications
You must be signed in to change notification settings - Fork 590
/
client.c
2518 lines (2237 loc) · 83.7 KB
/
client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* client.c - client management
*
* Copyright © 2007-2009 Julien Danjou <julien@danjou.info>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include <xcb/xcb_atom.h>
#include <xcb/shape.h>
#include <cairo-xcb.h>
#include "objects/tag.h"
#include "ewmh.h"
#include "screen.h"
#include "systray.h"
#include "property.h"
#include "spawn.h"
#include "luaa.h"
#include "xwindow.h"
#include "common/atoms.h"
#include "common/xutil.h"
static area_t titlebar_get_area(client_t *c, client_titlebar_t bar);
static drawable_t *titlebar_get_drawable(lua_State *L, client_t *c, int cl_idx, client_titlebar_t bar);
/** Collect a client.
* \param L The Lua VM state.
* \return The number of element pushed on stack.
*/
static void
client_wipe(client_t *c)
{
key_array_wipe(&c->keys);
xcb_icccm_get_wm_protocols_reply_wipe(&c->protocols);
p_delete(&c->machine);
p_delete(&c->class);
p_delete(&c->instance);
p_delete(&c->icon_name);
p_delete(&c->alt_icon_name);
p_delete(&c->name);
p_delete(&c->alt_name);
p_delete(&c->startup_id);
if(c->icon)
cairo_surface_destroy(c->icon);
}
/** Change the clients urgency flag.
* \param L The Lua VM state.
* \param cidx The client index on the stack.
* \param urgent The new flag state.
*/
void
client_set_urgent(lua_State *L, int cidx, bool urgent)
{
client_t *c = luaA_checkudata(L, cidx, &client_class);
if(c->urgent != urgent)
{
xcb_get_property_cookie_t hints =
xcb_icccm_get_wm_hints_unchecked(globalconf.connection, c->window);
c->urgent = urgent;
/* update ICCCM hints */
xcb_icccm_wm_hints_t wmh;
xcb_icccm_get_wm_hints_reply(globalconf.connection, hints, &wmh, NULL);
if(urgent)
wmh.flags |= XCB_ICCCM_WM_HINT_X_URGENCY;
else
wmh.flags &= ~XCB_ICCCM_WM_HINT_X_URGENCY;
xcb_icccm_set_wm_hints(globalconf.connection, c->window, &wmh);
luaA_object_emit_signal(L, cidx, "property::urgent", 0);
}
}
#define DO_CLIENT_SET_PROPERTY(prop) \
void \
client_set_##prop(lua_State *L, int cidx, fieldtypeof(client_t, prop) value) \
{ \
client_t *c = luaA_checkudata(L, cidx, &client_class); \
if(c->prop != value) \
{ \
c->prop = value; \
luaA_object_emit_signal(L, cidx, "property::" #prop, 0); \
} \
}
DO_CLIENT_SET_PROPERTY(group_window)
DO_CLIENT_SET_PROPERTY(type)
DO_CLIENT_SET_PROPERTY(transient_for)
DO_CLIENT_SET_PROPERTY(pid)
DO_CLIENT_SET_PROPERTY(skip_taskbar)
#undef DO_CLIENT_SET_PROPERTY
#define DO_CLIENT_SET_STRING_PROPERTY2(prop, signal) \
void \
client_set_##prop(lua_State *L, int cidx, char *value) \
{ \
client_t *c = luaA_checkudata(L, cidx, &client_class); \
if (A_STREQ(c->prop, value)) \
{ \
p_delete(&value); \
return; \
} \
p_delete(&c->prop); \
c->prop = value; \
luaA_object_emit_signal(L, cidx, "property::" #signal, 0); \
}
#define DO_CLIENT_SET_STRING_PROPERTY(prop) \
DO_CLIENT_SET_STRING_PROPERTY2(prop, prop)
DO_CLIENT_SET_STRING_PROPERTY(name)
DO_CLIENT_SET_STRING_PROPERTY2(alt_name, name)
DO_CLIENT_SET_STRING_PROPERTY(icon_name)
DO_CLIENT_SET_STRING_PROPERTY2(alt_icon_name, icon_name)
DO_CLIENT_SET_STRING_PROPERTY(role)
DO_CLIENT_SET_STRING_PROPERTY(machine)
#undef DO_CLIENT_SET_STRING_PROPERTY
void
client_set_class_instance(lua_State *L, int cidx, const char *class, const char *instance)
{
client_t *c = luaA_checkudata(L, cidx, &client_class);
p_delete(&c->class);
p_delete(&c->instance);
c->class = a_strdup(class);
luaA_object_emit_signal(L, cidx, "property::class", 0);
c->instance = a_strdup(instance);
luaA_object_emit_signal(L, cidx, "property::instance", 0);
}
/** Returns true if a client is tagged
* with one of the tags of the specified screen.
* \param c The client to check.
* \param screen Virtual screen.
* \return true if the client is visible, false otherwise.
*/
bool
client_maybevisible(client_t *c)
{
if(c->sticky)
return true;
foreach(tag, globalconf.tags)
if(tag_get_selected(*tag) && is_client_tagged(c, *tag))
return true;
return false;
}
/** Get a client by its window.
* \param w The client window to find.
* \return A client pointer if found, NULL otherwise.
*/
client_t *
client_getbywin(xcb_window_t w)
{
foreach(c, globalconf.clients)
if((*c)->window == w)
return *c;
return NULL;
}
/** Get a client by its frame window.
* \param w The client window to find.
* \return A client pointer if found, NULL otherwise.
*/
client_t *
client_getbyframewin(xcb_window_t w)
{
foreach(c, globalconf.clients)
if((*c)->frame_window == w)
return *c;
return NULL;
}
/** Unfocus a client (internal).
* \param c The client.
*/
static void
client_unfocus_internal(client_t *c)
{
globalconf.focus.client = NULL;
luaA_object_push(globalconf.L, c);
luaA_object_emit_signal(globalconf.L, -1, "unfocus", 0);
lua_pop(globalconf.L, 1);
}
/** Unfocus a client.
* \param c The client.
*/
static void
client_unfocus(client_t *c)
{
client_unfocus_internal(c);
globalconf.focus.need_update = true;
}
/** Check if client supports atom a protocol in WM_PROTOCOL.
* \param c The client.
* \param atom The protocol atom to check for.
* \return True if client has the atom in protocol, false otherwise.
*/
bool
client_hasproto(client_t *c, xcb_atom_t atom)
{
for(uint32_t i = 0; i < c->protocols.atoms_len; i++)
if(c->protocols.atoms[i] == atom)
return true;
return false;
}
/** Prepare banning a client by running all needed lua events.
* \param c The client.
*/
void client_ban_unfocus(client_t *c)
{
/* Wait until the last moment to take away the focus from the window. */
if(globalconf.focus.client == c)
client_unfocus(c);
}
/** Ban client and move it out of the viewport.
* \param c The client.
*/
void
client_ban(client_t *c)
{
if(!c->isbanned)
{
xcb_unmap_window(globalconf.connection, c->frame_window);
c->isbanned = true;
client_ban_unfocus(c);
}
}
/** This is part of The Bob Marley Algorithm: we ignore enter and leave window
* in certain cases, like map/unmap or move, so we don't get spurious events.
*/
void
client_ignore_enterleave_events(void)
{
foreach(c, globalconf.clients)
{
xcb_change_window_attributes(globalconf.connection,
(*c)->window,
XCB_CW_EVENT_MASK,
(const uint32_t []) { CLIENT_SELECT_INPUT_EVENT_MASK & ~(XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW) });
xcb_change_window_attributes(globalconf.connection,
(*c)->frame_window,
XCB_CW_EVENT_MASK,
(const uint32_t []) { FRAME_SELECT_INPUT_EVENT_MASK & ~(XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW) });
}
}
void
client_restore_enterleave_events(void)
{
foreach(c, globalconf.clients)
{
xcb_change_window_attributes(globalconf.connection,
(*c)->window,
XCB_CW_EVENT_MASK,
(const uint32_t []) { CLIENT_SELECT_INPUT_EVENT_MASK });
xcb_change_window_attributes(globalconf.connection,
(*c)->frame_window,
XCB_CW_EVENT_MASK,
(const uint32_t []) { FRAME_SELECT_INPUT_EVENT_MASK });
}
}
/** Record that a client got focus.
* \param c The client.
*/
void
client_focus_update(client_t *c)
{
if(!client_maybevisible(c))
return;
if(globalconf.focus.client)
{
if (globalconf.focus.client == c)
/* Already focused */
return;
/* When we are called due to a FocusIn event (=old focused client
* already unfocused), we don't want to cause a SetInputFocus,
* because the client which has focus now could be using globally
* active input model (or 'no input').
*/
client_unfocus_internal(globalconf.focus.client);
}
globalconf.focus.client = c;
/* according to EWMH, we have to remove the urgent state from a client */
luaA_object_push(globalconf.L, c);
client_set_urgent(globalconf.L, -1, false);
luaA_object_emit_signal(globalconf.L, -1, "focus", 0);
lua_pop(globalconf.L, 1);
}
/** Give focus to client, or to first client if client is NULL.
* \param c The client.
*/
void
client_focus(client_t *c)
{
/* We have to set focus on first client */
if(!c && globalconf.clients.len && !(c = globalconf.clients.tab[0]))
return;
if(!client_maybevisible(c) || c == globalconf.focus.client)
return;
client_focus_update(c);
globalconf.focus.need_update = true;
}
void
client_focus_refresh(void)
{
client_t *c = globalconf.focus.client;
xcb_window_t win = globalconf.screen->root;
if(!globalconf.focus.need_update)
return;
globalconf.focus.need_update = false;
if(c)
{
/* Make sure this window is unbanned and e.g. not minimized */
client_unban(c);
/* Sets focus on window - using xcb_set_input_focus or WM_TAKE_FOCUS */
if(!c->nofocus)
win = c->window;
else
/* Focus the root window to make sure the previously focused client
* doesn't get any input in case WM_TAKE_FOCUS gets ignored.
*/
win = globalconf.screen->root;
if(client_hasproto(c, WM_TAKE_FOCUS))
xwindow_takefocus(c->window);
}
/* If nothing has the focused or the currently focused client doesn't want
* us to focus it, this sets the focus to the root window. This makes sure
* the previously focused client actually gets unfocused. Alternatively, the
* new client gets the input focus.
*/
xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_PARENT,
win, globalconf.timestamp);
}
static void
client_update_properties(client_t *c)
{
/* get all hints */
xcb_get_property_cookie_t wm_normal_hints = property_get_wm_normal_hints(c);
xcb_get_property_cookie_t wm_hints = property_get_wm_hints(c);
xcb_get_property_cookie_t wm_transient_for = property_get_wm_transient_for(c);
xcb_get_property_cookie_t wm_client_leader = property_get_wm_client_leader(c);
xcb_get_property_cookie_t wm_client_machine = property_get_wm_client_machine(c);
xcb_get_property_cookie_t wm_window_role = property_get_wm_window_role(c);
xcb_get_property_cookie_t net_wm_pid = property_get_net_wm_pid(c);
xcb_get_property_cookie_t net_wm_icon = property_get_net_wm_icon(c);
xcb_get_property_cookie_t wm_name = property_get_wm_name(c);
xcb_get_property_cookie_t net_wm_name = property_get_net_wm_name(c);
xcb_get_property_cookie_t wm_icon_name = property_get_wm_icon_name(c);
xcb_get_property_cookie_t net_wm_icon_name = property_get_net_wm_icon_name(c);
xcb_get_property_cookie_t wm_class = property_get_wm_class(c);
xcb_get_property_cookie_t wm_protocols = property_get_wm_protocols(c);
xcb_get_property_cookie_t opacity = xwindow_get_opacity_unchecked(c->window);
/* update strut */
ewmh_process_client_strut(c);
/* Now process all replies */
property_update_wm_normal_hints(c, wm_normal_hints);
property_update_wm_hints(c, wm_hints);
property_update_wm_transient_for(c, wm_transient_for);
property_update_wm_client_leader(c, wm_client_leader);
property_update_wm_client_machine(c, wm_client_machine);
property_update_wm_window_role(c, wm_window_role);
property_update_net_wm_pid(c, net_wm_pid);
property_update_net_wm_icon(c, net_wm_icon);
property_update_wm_name(c, wm_name);
property_update_net_wm_name(c, net_wm_name);
property_update_wm_icon_name(c, wm_icon_name);
property_update_net_wm_icon_name(c, net_wm_icon_name);
property_update_wm_class(c, wm_class);
property_update_wm_protocols(c, wm_protocols);
window_set_opacity(globalconf.L, -1, xwindow_get_opacity_from_cookie(opacity));
}
/** Manage a new client.
* \param w The window.
* \param wgeom Window geometry.
* \param startup True if we are managing at startup time.
*/
void
client_manage(xcb_window_t w, xcb_get_geometry_reply_t *wgeom, bool startup)
{
const uint32_t select_input_val[] = { CLIENT_SELECT_INPUT_EVENT_MASK };
if(systray_iskdedockapp(w))
{
systray_request_handle(w, NULL);
return;
}
/* If this is a new client that just has been launched, then request its
* startup id. */
xcb_get_property_cookie_t startup_id_q = { 0 };
if(!startup)
startup_id_q = xcb_get_property(globalconf.connection, false, w,
_NET_STARTUP_ID, XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
/* Make sure the window is automatically mapped if awesome exits or dies. */
xcb_change_save_set(globalconf.connection, XCB_SET_MODE_INSERT, w);
if (globalconf.have_shape)
xcb_shape_select_input(globalconf.connection, w, 1);
client_t *c = client_new(globalconf.L);
xcb_screen_t *s = globalconf.screen;
/* consider the window banned */
c->isbanned = true;
/* Store window */
c->window = w;
c->frame_window = xcb_generate_id(globalconf.connection);
xcb_create_window(globalconf.connection, globalconf.default_depth, c->frame_window, s->root,
wgeom->x, wgeom->y, wgeom->width, wgeom->height,
wgeom->border_width, XCB_COPY_FROM_PARENT, globalconf.visual->visual_id,
XCB_CW_BORDER_PIXEL | XCB_CW_BIT_GRAVITY | XCB_CW_WIN_GRAVITY
| XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK | XCB_CW_COLORMAP,
(const uint32_t [])
{
globalconf.screen->black_pixel,
XCB_GRAVITY_NORTH_WEST,
XCB_GRAVITY_NORTH_WEST,
1,
FRAME_SELECT_INPUT_EVENT_MASK,
globalconf.default_cmap
});
if (startup)
{
/* The client is already mapped, thus we must be sure that we don't send
* ourselves an UnmapNotify due to the xcb_reparent_window().
*
* Grab the server to make sure we don't lose any events.
*/
uint32_t no_event[] = { 0 };
xcb_grab_server(globalconf.connection);
xcb_change_window_attributes(globalconf.connection,
globalconf.screen->root,
XCB_CW_EVENT_MASK,
no_event);
}
xcb_reparent_window(globalconf.connection, w, c->frame_window, 0, 0);
xcb_map_window(globalconf.connection, w);
if (startup)
{
xcb_change_window_attributes(globalconf.connection,
globalconf.screen->root,
XCB_CW_EVENT_MASK,
ROOT_WINDOW_EVENT_MASK);
xcb_ungrab_server(globalconf.connection);
}
/* Do this now so that we don't get any events for the above
* (Else, reparent could cause an UnmapNotify) */
xcb_change_window_attributes(globalconf.connection, w, XCB_CW_EVENT_MASK, select_input_val);
luaA_object_emit_signal(globalconf.L, -1, "property::window", 0);
/* The frame window gets the border, not the real client window */
xcb_configure_window(globalconf.connection, w,
XCB_CONFIG_WINDOW_BORDER_WIDTH,
(uint32_t[]) { 0 });
/* Move this window to the bottom of the stack. Without this we would force
* other windows which will be above this one to redraw themselves because
* this window occludes them for a tiny moment. The next stack_refresh()
* will fix this up and move the window to its correct place. */
xcb_configure_window(globalconf.connection, c->frame_window,
XCB_CONFIG_WINDOW_STACK_MODE,
(uint32_t[]) { XCB_STACK_MODE_BELOW});
/* Duplicate client and push it in client list */
lua_pushvalue(globalconf.L, -1);
client_array_push(&globalconf.clients, luaA_object_ref(globalconf.L, -1));
/* Set the right screen */
screen_client_moveto(c, screen_getbycoord(wgeom->x, wgeom->y), false);
/* Store initial geometry and emits signals so we inform that geometry have
* been set. */
#define HANDLE_GEOM(attr) \
c->geometry.attr = wgeom->attr; \
luaA_object_emit_signal(globalconf.L, -1, "property::" #attr, 0);
HANDLE_GEOM(x)
HANDLE_GEOM(y)
HANDLE_GEOM(width)
HANDLE_GEOM(height)
#undef HANDLE_GEOM
luaA_object_emit_signal(globalconf.L, -1, "property::geometry", 0);
/* Set border width */
window_set_border_width(globalconf.L, -1, wgeom->border_width);
/* we honor size hints by default */
c->size_hints_honor = true;
luaA_object_emit_signal(globalconf.L, -1, "property::size_hints_honor", 0);
/* update all properties */
client_update_properties(c);
/* Then check clients hints */
ewmh_client_check_hints(c);
/* Push client in stack */
stack_client_push(c);
/* Put the window in normal state. */
xwindow_set_state(c->window, XCB_ICCCM_WM_STATE_NORMAL);
if(!startup)
{
/* Request our response */
xcb_get_property_reply_t *reply =
xcb_get_property_reply(globalconf.connection, startup_id_q, NULL);
/* Say spawn that a client has been started, with startup id as argument */
char *startup_id = xutil_get_text_property_from_reply(reply);
c->startup_id = startup_id;
p_delete(&reply);
spawn_start_notify(c, startup_id);
}
luaA_class_emit_signal(globalconf.L, &client_class, "list", 0);
/* client is still on top of the stack; push startup value,
* and emit signals with one arg */
lua_pushboolean(globalconf.L, startup);
luaA_object_emit_signal(globalconf.L, -2, "manage", 1);
/* pop client */
lua_pop(globalconf.L, 1);
}
static void
client_remove_titlebar_geometry(client_t *c, area_t *geometry)
{
geometry->x += c->titlebar[CLIENT_TITLEBAR_LEFT].size;
geometry->y += c->titlebar[CLIENT_TITLEBAR_TOP].size;
geometry->width -= c->titlebar[CLIENT_TITLEBAR_LEFT].size;
geometry->width -= c->titlebar[CLIENT_TITLEBAR_RIGHT].size;
geometry->height -= c->titlebar[CLIENT_TITLEBAR_TOP].size;
geometry->height -= c->titlebar[CLIENT_TITLEBAR_BOTTOM].size;
}
static void
client_add_titlebar_geometry(client_t *c, area_t *geometry)
{
geometry->x -= c->titlebar[CLIENT_TITLEBAR_LEFT].size;
geometry->y -= c->titlebar[CLIENT_TITLEBAR_TOP].size;
geometry->width += c->titlebar[CLIENT_TITLEBAR_LEFT].size;
geometry->width += c->titlebar[CLIENT_TITLEBAR_RIGHT].size;
geometry->height += c->titlebar[CLIENT_TITLEBAR_TOP].size;
geometry->height += c->titlebar[CLIENT_TITLEBAR_BOTTOM].size;
}
/** Send a synthetic configure event to a window.
*/
void
client_send_configure(client_t *c)
{
area_t geometry = c->geometry;
if (!c->fullscreen)
client_remove_titlebar_geometry(c, &geometry);
xwindow_configure(c->window, geometry, c->border_width);
}
/** Apply size hints to the client's new geometry.
*/
static area_t
client_apply_size_hints(client_t *c, area_t geometry)
{
int32_t minw = 0, minh = 0;
int32_t basew = 0, baseh = 0, real_basew = 0, real_baseh = 0;
if (c->fullscreen)
return geometry;
/* Size hints are applied to the window without any decoration */
client_remove_titlebar_geometry(c, &geometry);
if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_SIZE)
{
basew = c->size_hints.base_width;
baseh = c->size_hints.base_height;
real_basew = basew;
real_baseh = baseh;
}
else if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
{
/* base size is substituted with min size if not specified */
basew = c->size_hints.min_width;
baseh = c->size_hints.min_height;
}
if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
{
minw = c->size_hints.min_width;
minh = c->size_hints.min_height;
}
else if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_SIZE)
{
/* min size is substituted with base size if not specified */
minw = c->size_hints.base_width;
minh = c->size_hints.base_height;
}
/* Handle the size aspect ratio */
if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_ASPECT
&& c->size_hints.min_aspect_den > 0
&& c->size_hints.max_aspect_den > 0
&& geometry.height > real_baseh
&& geometry.width > real_basew)
{
/* ICCCM mandates:
* If a base size is provided along with the aspect ratio fields, the base size should be subtracted from the
* window size prior to checking that the aspect ratio falls in range. If a base size is not provided, nothing
* should be subtracted from the window size. (The minimum size is not to be used in place of the base size for
* this purpose.)
*/
double dx = geometry.width - real_basew;
double dy = geometry.height - real_baseh;
double ratio = dx / dy;
double min = c->size_hints.min_aspect_num / (double) c->size_hints.min_aspect_den;
double max = c->size_hints.max_aspect_num / (double) c->size_hints.max_aspect_den;
if(max > 0 && min > 0 && ratio > 0)
{
if(ratio < min)
{
/* dx is lower than allowed, make dy lower to compensate this (+ 0.5 to force proper rounding). */
dy = dx / min + 0.5;
geometry.width = dx + real_basew;
geometry.height = dy + real_baseh;
} else if(ratio > max)
{
/* dx is too high, lower it (+0.5 for proper rounding) */
dx = dy * max + 0.5;
geometry.width = dx + real_basew;
geometry.height = dy + real_baseh;
}
}
}
/* Handle the minimum size */
geometry.width = MAX(geometry.width, minw);
geometry.height = MAX(geometry.height, minh);
/* Handle the maximum size */
if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
{
if(c->size_hints.max_width)
geometry.width = MIN(geometry.width, c->size_hints.max_width);
if(c->size_hints.max_height)
geometry.height = MIN(geometry.height, c->size_hints.max_height);
}
/* Handle the size increment */
if(c->size_hints.flags & (XCB_ICCCM_SIZE_HINT_P_RESIZE_INC | XCB_ICCCM_SIZE_HINT_BASE_SIZE)
&& c->size_hints.width_inc && c->size_hints.height_inc)
{
uint16_t t1 = geometry.width, t2 = geometry.height;
unsigned_subtract(t1, basew);
unsigned_subtract(t2, baseh);
geometry.width -= t1 % c->size_hints.width_inc;
geometry.height -= t2 % c->size_hints.height_inc;
}
client_add_titlebar_geometry(c, &geometry);
return geometry;
}
static void
client_resize_do(client_t *c, area_t geometry, bool force_notice, bool honor_hints)
{
bool send_notice = force_notice;
bool hide_titlebars = c->fullscreen;
screen_t *new_screen = screen_getbycoord(geometry.x, geometry.y);
if (honor_hints)
geometry = client_apply_size_hints(c, geometry);
if(c->geometry.width == geometry.width
&& c->geometry.height == geometry.height)
send_notice = true;
/* Also store geometry including border */
area_t old_geometry = c->geometry;
c->geometry = geometry;
/* Ignore all spurious enter/leave notify events */
client_ignore_enterleave_events();
/* Configure the client for its new size */
area_t real_geometry = geometry;
if (!hide_titlebars)
{
real_geometry.x = c->titlebar[CLIENT_TITLEBAR_LEFT].size;
real_geometry.y = c->titlebar[CLIENT_TITLEBAR_TOP].size;
real_geometry.width -= c->titlebar[CLIENT_TITLEBAR_LEFT].size;
real_geometry.width -= c->titlebar[CLIENT_TITLEBAR_RIGHT].size;
real_geometry.height -= c->titlebar[CLIENT_TITLEBAR_TOP].size;
real_geometry.height -= c->titlebar[CLIENT_TITLEBAR_BOTTOM].size;
} else {
real_geometry.x = 0;
real_geometry.y = 0;
}
xcb_configure_window(globalconf.connection, c->frame_window,
XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
(uint32_t[]) { geometry.x, geometry.y, geometry.width, geometry.height });
xcb_configure_window(globalconf.connection, c->window,
XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
(uint32_t[]) { real_geometry.x, real_geometry.y, real_geometry.width, real_geometry.height });
if(send_notice)
/* We are moving without changing the size, see ICCCM 4.2.3 */
client_send_configure(c);
client_restore_enterleave_events();
luaA_object_push(globalconf.L, c);
luaA_object_emit_signal(globalconf.L, -1, "property::geometry", 0);
if (old_geometry.x != geometry.x)
luaA_object_emit_signal(globalconf.L, -1, "property::x", 0);
if (old_geometry.y != geometry.y)
luaA_object_emit_signal(globalconf.L, -1, "property::y", 0);
if (old_geometry.width != geometry.width)
luaA_object_emit_signal(globalconf.L, -1, "property::width", 0);
if (old_geometry.height != geometry.height)
luaA_object_emit_signal(globalconf.L, -1, "property::height", 0);
lua_pop(globalconf.L, 1);
screen_client_moveto(c, new_screen, false);
/* Update all titlebars */
for (client_titlebar_t bar = CLIENT_TITLEBAR_TOP; bar < CLIENT_TITLEBAR_COUNT; bar++) {
if (c->titlebar[bar].drawable == NULL && c->titlebar[bar].size == 0)
continue;
luaA_object_push(globalconf.L, c);
drawable_t *drawable = titlebar_get_drawable(globalconf.L, c, -1, bar);
luaA_object_push_item(globalconf.L, -1, drawable);
area_t area = titlebar_get_area(c, bar);
/* Convert to global coordinates */
area.x += geometry.x;
area.y += geometry.y;
if (hide_titlebars)
area.width = area.height = 0;
drawable_set_geometry(drawable, -1, area);
/* Pop the client and the drawable */
lua_pop(globalconf.L, 2);
}
}
/** Resize client window.
* The sizes given as parameters are with borders!
* \param c Client to resize.
* \param geometry New window geometry.
* \param honor_hints Use size hints.
* \return true if an actual resize occurred.
*/
bool
client_resize(client_t *c, area_t geometry, bool honor_hints)
{
area_t area;
/* offscreen appearance fixes */
area = display_area_get();
if(geometry.x > area.width)
geometry.x = area.width - geometry.width;
if(geometry.y > area.height)
geometry.y = area.height - geometry.height;
if(geometry.x + geometry.width < 0)
geometry.x = 0;
if(geometry.y + geometry.height < 0)
geometry.y = 0;
if(geometry.width < c->titlebar[CLIENT_TITLEBAR_LEFT].size + c->titlebar[CLIENT_TITLEBAR_RIGHT].size)
return false;
if(geometry.height < c->titlebar[CLIENT_TITLEBAR_TOP].size + c->titlebar[CLIENT_TITLEBAR_BOTTOM].size)
return false;
if(geometry.width == 0 || geometry.height == 0)
return false;
if(c->geometry.x != geometry.x
|| c->geometry.y != geometry.y
|| c->geometry.width != geometry.width
|| c->geometry.height != geometry.height)
{
client_resize_do(c, geometry, false, honor_hints);
return true;
}
return false;
}
/** Set a client minimized, or not.
* \param L The Lua VM state.
* \param cidx The client index.
* \param s Set or not the client minimized.
*/
void
client_set_minimized(lua_State *L, int cidx, bool s)
{
client_t *c = luaA_checkudata(L, cidx, &client_class);
if(c->minimized != s)
{
c->minimized = s;
banning_need_update();
if(s)
{
/* ICCCM: To transition from ICONIC to NORMAL state, the client
* should just map the window. Thus, iconic clients need to be
* unmapped, else the MapWindow request doesn't have any effect.
*/
xwindow_set_state(c->window, XCB_ICCCM_WM_STATE_ICONIC);
uint32_t no_event[] = { 0 };
const uint32_t select_input_val[] = { CLIENT_SELECT_INPUT_EVENT_MASK };
xcb_grab_server(globalconf.connection);
xcb_change_window_attributes(globalconf.connection,
globalconf.screen->root,
XCB_CW_EVENT_MASK,
no_event);
xcb_change_window_attributes(globalconf.connection,
c->window,
XCB_CW_EVENT_MASK,
no_event);
xcb_unmap_window(globalconf.connection, c->window);
xcb_change_window_attributes(globalconf.connection,
globalconf.screen->root,
XCB_CW_EVENT_MASK,
ROOT_WINDOW_EVENT_MASK);
xcb_change_window_attributes(globalconf.connection,
c->window,
XCB_CW_EVENT_MASK,
select_input_val);
xcb_ungrab_server(globalconf.connection);
}
else
{
xwindow_set_state(c->window, XCB_ICCCM_WM_STATE_NORMAL);
xcb_map_window(globalconf.connection, c->window);
}
if(strut_has_value(&c->strut))
screen_emit_signal(globalconf.L, c->screen, "property::workarea", 0);
luaA_object_emit_signal(L, cidx, "property::minimized", 0);
}
}
/** Set a client hidden, or not.
* \param L The Lua VM state.
* \param cidx The client index.
* \param s Set or not the client hidden.
*/
static void
client_set_hidden(lua_State *L, int cidx, bool s)
{
client_t *c = luaA_checkudata(L, cidx, &client_class);
if(c->hidden != s)
{
c->hidden = s;
banning_need_update();
if(strut_has_value(&c->strut))
screen_emit_signal(globalconf.L, c->screen, "property::workarea", 0);
luaA_object_emit_signal(L, cidx, "property::hidden", 0);
}
}
/** Set a client sticky, or not.
* \param L The Lua VM state.
* \param cidx The client index.
* \param s Set or not the client sticky.
*/
void
client_set_sticky(lua_State *L, int cidx, bool s)
{
client_t *c = luaA_checkudata(L, cidx, &client_class);
if(c->sticky != s)
{
c->sticky = s;
banning_need_update();
luaA_object_emit_signal(L, cidx, "property::sticky", 0);
}
}
/** Set a client fullscreen, or not.
* \param L The Lua VM state.
* \param cidx The client index.
* \param s Set or not the client fullscreen.
*/
void
client_set_fullscreen(lua_State *L, int cidx, bool s)
{
client_t *c = luaA_checkudata(L, cidx, &client_class);
if(c->fullscreen != s)
{
/* become fullscreen! */
if(s)
{
/* remove any max state */
client_set_maximized(L, cidx, false);
/* You can only be part of one of the special layers. */
client_set_below(L, cidx, false);
client_set_above(L, cidx, false);
client_set_ontop(L, cidx, false);
}
int abs_cidx = luaA_absindex(L, cidx); \
lua_pushboolean(L, s);
c->fullscreen = s;
luaA_object_emit_signal(L, abs_cidx, "request::fullscreen", 1);
luaA_object_emit_signal(L, abs_cidx, "property::fullscreen", 0);
/* Force a client resize, so that titlebars get shown/hidden */
client_resize_do(c, c->geometry, true, false);
stack_windows();
}
}
/** Get a clients maximized state (horizontally and vertically).
* \param c The client.
* \return The maximized state.
*/
static int
client_get_maximized(client_t *c)
{
return c->maximized_horizontal && c->maximized_vertical;
}
/** Set a client horizontally|vertically maximized.
* \param L The Lua VM state.
* \param cidx The client index.
* \param s The maximized status.
*/
#define DO_FUNCTION_CLIENT_MAXIMIZED(type) \
void \
client_set_maximized_##type(lua_State *L, int cidx, bool s) \
{ \
client_t *c = luaA_checkudata(L, cidx, &client_class); \
if(c->maximized_##type != s) \
{ \
int abs_cidx = luaA_absindex(L, cidx); \
if(s) \
client_set_fullscreen(L, abs_cidx, false); \
lua_pushboolean(L, s); \
int max_before = client_get_maximized(c); \