forked from IcebergThings/weston
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathwindow-manager.c
3472 lines (2974 loc) · 98.6 KB
/
window-manager.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
/*
* Copyright © 2011 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "config.h"
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <limits.h>
#include <assert.h>
#include <X11/Xcursor/Xcursor.h>
#include <linux/input.h>
#include <libweston/libweston.h>
#include "xwayland.h"
#include "xwayland-internal-interface.h"
#include "shared/cairo-util.h"
#include "hash.h"
#include "shared/helpers.h"
struct wm_size_hints {
uint32_t flags;
int32_t x, y;
int32_t width, height; /* should set so old wm's don't mess up */
int32_t min_width, min_height;
int32_t max_width, max_height;
int32_t width_inc, height_inc;
struct {
int32_t x;
int32_t y;
} min_aspect, max_aspect;
int32_t base_width, base_height;
int32_t win_gravity;
};
#define USPosition (1L << 0)
#define USSize (1L << 1)
#define PPosition (1L << 2)
#define PSize (1L << 3)
#define PMinSize (1L << 4)
#define PMaxSize (1L << 5)
#define PResizeInc (1L << 6)
#define PAspect (1L << 7)
#define PBaseSize (1L << 8)
#define PWinGravity (1L << 9)
struct motif_wm_hints {
uint32_t flags;
uint32_t functions;
uint32_t decorations;
int32_t input_mode;
uint32_t status;
};
#define MWM_HINTS_FUNCTIONS (1L << 0)
#define MWM_HINTS_DECORATIONS (1L << 1)
#define MWM_HINTS_INPUT_MODE (1L << 2)
#define MWM_HINTS_STATUS (1L << 3)
#define MWM_FUNC_ALL (1L << 0)
#define MWM_FUNC_RESIZE (1L << 1)
#define MWM_FUNC_MOVE (1L << 2)
#define MWM_FUNC_MINIMIZE (1L << 3)
#define MWM_FUNC_MAXIMIZE (1L << 4)
#define MWM_FUNC_CLOSE (1L << 5)
#define MWM_DECOR_ALL (1L << 0)
#define MWM_DECOR_BORDER (1L << 1)
#define MWM_DECOR_RESIZEH (1L << 2)
#define MWM_DECOR_TITLE (1L << 3)
#define MWM_DECOR_MENU (1L << 4)
#define MWM_DECOR_MINIMIZE (1L << 5)
#define MWM_DECOR_MAXIMIZE (1L << 6)
#define MWM_DECOR_EVERYTHING \
(MWM_DECOR_BORDER | MWM_DECOR_RESIZEH | MWM_DECOR_TITLE | \
MWM_DECOR_MENU | MWM_DECOR_MINIMIZE | MWM_DECOR_MAXIMIZE)
#define MWM_INPUT_MODELESS 0
#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
#define MWM_INPUT_SYSTEM_MODAL 2
#define MWM_INPUT_FULL_APPLICATION_MODAL 3
#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
#define MWM_TEAROFF_WINDOW (1L<<0)
#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
#define _NET_WM_MOVERESIZE_SIZE_TOP 1
#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
struct weston_output_weak_ref {
struct weston_output *output;
struct wl_listener destroy_listener;
};
struct weston_wm_window {
struct weston_wm *wm;
xcb_window_t id;
xcb_window_t frame_id;
struct frame *frame;
cairo_surface_t *cairo_surface;
uint32_t surface_id;
struct weston_surface *surface;
struct weston_desktop_xwayland_surface *shsurf;
struct wl_listener surface_destroy_listener;
struct wl_event_source *repaint_source;
struct wl_event_source *configure_source;
int properties_dirty;
int pid;
char *machine;
char *class;
char *name;
struct weston_wm_window *transient_for;
uint32_t protocols;
xcb_atom_t type;
int width, height;
int x;
int y;
bool pos_dirty;
int frame_x;
int frame_y;
int map_request_x;
int map_request_y;
struct weston_output_weak_ref legacy_fullscreen_output;
int saved_width, saved_height;
int decorate;
uint32_t last_button_time;
int did_double;
int override_redirect;
int fullscreen;
int has_alpha;
int delete_window;
int maximized_vert;
int maximized_horz;
int take_focus;
int no_shadow;
struct wm_size_hints size_hints;
struct motif_wm_hints motif_hints;
struct wl_list link;
int decor_top;
int decor_bottom;
int decor_left;
int decor_right;
};
static int
our_resource(struct weston_wm *wm, uint32_t id);
static void
weston_wm_window_set_allow_commits(struct weston_wm_window *window, bool allow);
static struct weston_wm_window *
get_wm_window(struct weston_surface *surface);
static void
weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window);
static void
weston_wm_window_schedule_repaint(struct weston_wm_window *window);
static int
legacy_fullscreen(struct weston_wm *wm,
struct weston_wm_window *window,
struct weston_output **output_ret);
static void
xserver_map_shell_surface(struct weston_wm_window *window,
struct weston_surface *surface);
static bool
wm_debug_is_enabled(struct weston_wm *wm)
{
return weston_log_scope_is_enabled(wm->server->wm_debug);
}
static void __attribute__ ((format (printf, 2, 3)))
wm_printf(struct weston_wm *wm, const char *fmt, ...)
{
va_list ap;
char timestr[128];
if (wm_debug_is_enabled(wm))
weston_log_scope_printf(wm->server->wm_debug, "%s ",
weston_log_scope_timestamp(wm->server->wm_debug,
timestr, sizeof timestr));
va_start(ap, fmt);
weston_log_scope_vprintf(wm->server->wm_debug, fmt, ap);
va_end(ap);
}
static void
weston_output_weak_ref_init(struct weston_output_weak_ref *ref)
{
ref->output = NULL;
}
static void
weston_output_weak_ref_clear(struct weston_output_weak_ref *ref)
{
if (!ref->output)
return;
wl_list_remove(&ref->destroy_listener.link);
ref->output = NULL;
}
static void
weston_output_weak_ref_handle_destroy(struct wl_listener *listener, void *data)
{
struct weston_output_weak_ref *ref;
ref = wl_container_of(listener, ref, destroy_listener);
assert(ref->output == data);
weston_output_weak_ref_clear(ref);
}
static void
weston_output_weak_ref_set(struct weston_output_weak_ref *ref,
struct weston_output *output)
{
weston_output_weak_ref_clear(ref);
if (!output)
return;
ref->destroy_listener.notify = weston_output_weak_ref_handle_destroy;
wl_signal_add(&output->destroy_signal, &ref->destroy_listener);
ref->output = output;
}
static bool __attribute__ ((warn_unused_result))
wm_lookup_window(struct weston_wm *wm, xcb_window_t hash,
struct weston_wm_window **window)
{
*window = hash_table_lookup(wm->window_hash, hash);
if (*window)
return true;
return false;
}
const char *
get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
{
xcb_get_atom_name_cookie_t cookie;
xcb_get_atom_name_reply_t *reply;
xcb_generic_error_t *e;
static char buffer[64];
if (atom == XCB_ATOM_NONE)
return "None";
cookie = xcb_get_atom_name (c, atom);
reply = xcb_get_atom_name_reply (c, cookie, &e);
if (reply) {
snprintf(buffer, sizeof buffer, "%.*s",
xcb_get_atom_name_name_length (reply),
xcb_get_atom_name_name (reply));
} else {
snprintf(buffer, sizeof buffer, "(atom %u)", atom);
}
free(reply);
return buffer;
}
static xcb_cursor_t
xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
{
xcb_connection_t *c = wm->conn;
xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
xcb_screen_t *screen = s.data;
xcb_gcontext_t gc;
xcb_pixmap_t pix;
xcb_render_picture_t pic;
xcb_cursor_t cursor;
int stride = img->width * 4;
pix = xcb_generate_id(c);
xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
pic = xcb_generate_id(c);
xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
gc = xcb_generate_id(c);
xcb_create_gc(c, gc, pix, 0, 0);
xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
img->width, img->height, 0, 0, 0, 32,
stride * img->height, (uint8_t *) img->pixels);
xcb_free_gc(c, gc);
cursor = xcb_generate_id(c);
xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
xcb_render_free_picture(c, pic);
xcb_free_pixmap(c, pix);
return cursor;
}
static xcb_cursor_t
xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
{
/* TODO: treat animated cursors as well */
if (images->nimage != 1)
return -1;
return xcb_cursor_image_load_cursor(wm, images->images[0]);
}
static xcb_cursor_t
xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
{
xcb_cursor_t cursor;
XcursorImages *images;
char *v = NULL;
char *theme = NULL;
int size = 0;
if (!file)
return 0;
v = getenv ("XCURSOR_SIZE");
if (v)
size = atoi(v);
if (!size)
size = 32;
theme = getenv("XCURSOR_THEME");
images = XcursorLibraryLoadImages (file, theme, size);
if (!images)
return -1;
cursor = xcb_cursor_images_load_cursor (wm, images);
XcursorImagesDestroy (images);
return cursor;
}
static unsigned
dump_cardinal_array_elem(FILE *fp, unsigned format,
void *arr, unsigned len, unsigned ind)
{
const char *comma;
/* If more than 16 elements, print 0-14, ..., last */
if (ind > 14 && ind < len - 1) {
fprintf(fp, ", ...");
return len - 1;
}
comma = ind ? ", " : "";
switch (format) {
case 32:
fprintf(fp, "%s%" PRIu32, comma, ((uint32_t *)arr)[ind]);
break;
case 16:
fprintf(fp, "%s%" PRIu16, comma, ((uint16_t *)arr)[ind]);
break;
case 8:
fprintf(fp, "%s%" PRIu8, comma, ((uint8_t *)arr)[ind]);
break;
default:
fprintf(fp, "%s???", comma);
}
return ind + 1;
}
static void
dump_cardinal_array(FILE *fp, xcb_get_property_reply_t *reply)
{
unsigned i = 0;
void *arr;
assert(reply->type == XCB_ATOM_CARDINAL);
arr = xcb_get_property_value(reply);
fprintf(fp, "[");
while (i < reply->value_len)
i = dump_cardinal_array_elem(fp, reply->format,
arr, reply->value_len, i);
fprintf(fp, "]");
}
void
dump_property(FILE *fp, struct weston_wm *wm,
xcb_atom_t property, xcb_get_property_reply_t *reply)
{
int32_t *incr_value;
const char *text_value, *name;
xcb_atom_t *atom_value;
xcb_window_t *window_value;
int width, len;
uint32_t i;
width = fprintf(fp, "%s: ", get_atom_name(wm->conn, property));
if (reply == NULL) {
fprintf(fp, "(no reply)\n");
return;
}
width += fprintf(fp, "%s/%d, length %d (value_len %d): ",
get_atom_name(wm->conn, reply->type),
reply->format,
xcb_get_property_value_length(reply),
reply->value_len);
if (reply->type == wm->atom.incr) {
incr_value = xcb_get_property_value(reply);
fprintf(fp, "%d\n", *incr_value);
} else if (reply->type == wm->atom.utf8_string ||
reply->type == wm->atom.string) {
text_value = xcb_get_property_value(reply);
if (reply->value_len > 40)
len = 40;
else
len = reply->value_len;
fprintf(fp, "\"%.*s\"\n", len, text_value);
} else if (reply->type == XCB_ATOM_ATOM) {
atom_value = xcb_get_property_value(reply);
for (i = 0; i < reply->value_len; i++) {
name = get_atom_name(wm->conn, atom_value[i]);
if (width + strlen(name) + 2 > 78) {
fprintf(fp, "\n ");
width = 4;
} else if (i > 0) {
width += fprintf(fp, ", ");
}
width += fprintf(fp, "%s", name);
}
fprintf(fp, "\n");
} else if (reply->type == XCB_ATOM_CARDINAL) {
dump_cardinal_array(fp, reply);
fprintf(fp, "\n");
} else if (reply->type == XCB_ATOM_WINDOW && reply->format == 32) {
window_value = xcb_get_property_value(reply);
fprintf(fp, "win %u\n", *window_value);
} else {
fprintf(fp, "huh?\n");
}
}
static void
read_and_dump_property(FILE *fp, struct weston_wm *wm,
xcb_window_t window, xcb_atom_t property)
{
xcb_get_property_reply_t *reply;
xcb_get_property_cookie_t cookie;
cookie = xcb_get_property(wm->conn, 0, window,
property, XCB_ATOM_ANY, 0, 2048);
reply = xcb_get_property_reply(wm->conn, cookie, NULL);
dump_property(fp, wm, property, reply);
free(reply);
}
static char *
window_type_atom_to_string(struct weston_wm *wm, xcb_atom_t atom)
{
if (atom == wm->atom.net_wm_window_type_normal)
return "_NET_WM_WINDOW_TYPE_NORMAL";
else if (atom == wm->atom.net_wm_window_type_desktop)
return "_NET_WM_WINDOW_TYPE_DESKTOP";
else if (atom == wm->atom.net_wm_window_type_dock)
return "_NET_WM_WINDOW_TYPE_DOCK";
else if (atom == wm->atom.net_wm_window_type_toolbar)
return "_NET_WM_WINDOW_TYPE_TOOLBAR";
else if (atom == wm->atom.net_wm_window_type_menu)
return "_NET_WM_WINDOW_TYPE_MENU";
else if (atom == wm->atom.net_wm_window_type_utility)
return "_WM_WINDOW_TYPE_UTILITY";
else if (atom == wm->atom.net_wm_window_type_splash)
return "_NET_WM_WINDOW_TYPE_SPLASH";
else if (atom == wm->atom.net_wm_window_type_dialog)
return "_NET_WM_WINDOW_TYPE_DIALOG";
else if (atom == wm->atom.net_wm_window_type_dropdown)
return "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU";
else if (atom == wm->atom.net_wm_window_type_popup)
return "_NET_WM_WINDOW_TYPE_POPUP_MENU";
else if (atom == wm->atom.net_wm_window_type_tooltip)
return "_NET_WM_WINDOW_TYPE_TOOLTIP";
else if (atom == wm->atom.net_wm_window_type_notification)
return "_NET_WM_WINDOW_TYPE_NOTIFICATION";
else if (atom == wm->atom.net_wm_window_type_combo)
return "_NET_WM_WINDOW_TYPE_COMBO";
else if (atom == wm->atom.net_wm_window_type_dnd)
return "_NET_WM_WINDOW_TYPE_DND";
else if (atom == wm->atom.kde_net_wm_window_type_override)
return "_KDE_NET_WM_WINDOW_TYPE_OVERRIDE";
else
return "UNKNOWN";
}
/* We reuse some predefined, but otherwise useles atoms
* as local type placeholders that never touch the X11 server,
* to make weston_wm_window_read_properties() less exceptional.
*/
#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
#define TYPE_WM_WINDOW_TYPE XCB_ATOM_CUT_BUFFER4
static void
weston_wm_window_read_properties(struct weston_wm_window *window)
{
struct weston_wm *wm = window->wm;
#define F(field) (&window->field)
const struct {
xcb_atom_t atom;
xcb_atom_t type;
void *ptr;
} props[] = {
{ XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
{ XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
{ XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
{ wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, NULL },
{ wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, NULL },
{ wm->atom.net_wm_state, TYPE_NET_WM_STATE, NULL },
{ wm->atom.net_wm_window_type, TYPE_WM_WINDOW_TYPE, F(type) },
{ wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
{ wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
{ wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, NULL },
{ wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
};
#undef F
xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
xcb_get_property_reply_t *reply;
void *p;
uint32_t *xid;
xcb_atom_t *atom;
uint32_t i;
char name[1024];
if (!window->properties_dirty)
return;
window->properties_dirty = 0;
for (i = 0; i < ARRAY_LENGTH(props); i++)
cookie[i] = xcb_get_property(wm->conn,
0, /* delete */
window->id,
props[i].atom,
XCB_ATOM_ANY, 0, 2048);
window->decorate = window->override_redirect ? 0 : MWM_DECOR_EVERYTHING;
window->size_hints.flags = 0;
window->motif_hints.flags = 0;
window->delete_window = 0;
window->take_focus = 0;
for (i = 0; i < ARRAY_LENGTH(props); i++) {
reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
if (!reply)
/* Bad window, typically */
continue;
if (reply->type == XCB_ATOM_NONE) {
/* No such property */
free(reply);
continue;
}
p = props[i].ptr;
switch (props[i].type) {
case XCB_ATOM_WM_CLIENT_MACHINE:
case XCB_ATOM_STRING:
/* FIXME: We're using this for both string and
utf8_string */
if (*(char **) p)
free(*(char **) p);
*(char **) p =
strndup(xcb_get_property_value(reply),
xcb_get_property_value_length(reply));
break;
case XCB_ATOM_WINDOW:
xid = xcb_get_property_value(reply);
if (!wm_lookup_window(wm, *xid, p))
weston_log("XCB_ATOM_WINDOW contains window"
" id not found in hash table.\n");
break;
case XCB_ATOM_CARDINAL:
case XCB_ATOM_ATOM:
atom = xcb_get_property_value(reply);
*(xcb_atom_t *) p = *atom;
break;
case TYPE_WM_WINDOW_TYPE:
atom = xcb_get_property_value(reply);
/* pick first one as type */
*(xcb_atom_t *) p = *atom;
/* scan all atoms */
for (i = 0; i < reply->value_len; i++) {
/* while there is a lot of discussion on this KDE property, but
commonly mentioned there should be no window decoration at all
including window shadow for _KDE_NET_WM_WINDOW_TYPE_OVERRIDE. */
if (atom[i] == wm->atom.kde_net_wm_window_type_override) {
window->no_shadow = 1;
window->decorate = 0;
}
wm_printf(wm, "wm_window_read_properties (window %d) window type: %s\n",
window->id, window_type_atom_to_string(wm, atom[i]));
}
break;
case TYPE_WM_PROTOCOLS:
atom = xcb_get_property_value(reply);
for (i = 0; i < reply->value_len; i++)
if (atom[i] == wm->atom.wm_delete_window) {
window->delete_window = 1;
} else if (atom[i] == wm->atom.wm_take_focus) {
window->take_focus = 1;
}
break;
case TYPE_WM_NORMAL_HINTS:
memcpy(&window->size_hints,
xcb_get_property_value(reply),
sizeof window->size_hints);
break;
case TYPE_NET_WM_STATE:
window->fullscreen = 0;
atom = xcb_get_property_value(reply);
for (i = 0; i < reply->value_len; i++) {
if (atom[i] == wm->atom.net_wm_state_fullscreen)
window->fullscreen = 1;
if (atom[i] == wm->atom.net_wm_state_maximized_vert)
window->maximized_vert = 1;
if (atom[i] == wm->atom.net_wm_state_maximized_horz)
window->maximized_horz = 1;
}
break;
case TYPE_MOTIF_WM_HINTS:
memcpy(&window->motif_hints,
xcb_get_property_value(reply),
sizeof window->motif_hints);
if (window->motif_hints.flags & MWM_HINTS_DECORATIONS) {
if (window->motif_hints.decorations & MWM_DECOR_ALL)
/* MWM_DECOR_ALL means all except the other values listed. */
window->decorate =
MWM_DECOR_EVERYTHING & (~window->motif_hints.decorations);
else
window->decorate =
window->motif_hints.decorations;
}
break;
default:
break;
}
free(reply);
}
if (window->pid > 0) {
gethostname(name, sizeof(name));
for (i = 0; i < sizeof(name); i++) {
if (name[i] == '\0')
break;
}
if (i == sizeof(name))
name[0] = '\0'; /* ignore stupid hostnames */
/* this is only one heuristic to guess the PID of a client is
* valid, assuming it's compliant with icccm and ewmh.
* Non-compliants and remote applications of course fail. */
if (!window->machine || strcmp(window->machine, name))
window->pid = 0;
}
}
#undef TYPE_WM_PROTOCOLS
#undef TYPE_MOTIF_WM_HINTS
#undef TYPE_NET_WM_STATE
#undef TYPE_WM_NORMAL_HINTS
static void
weston_wm_window_get_frame_size(struct weston_wm_window *window,
int *width, int *height)
{
struct theme *t = window->wm->theme;
if (window->fullscreen) {
*width = window->width;
*height = window->height;
} else if (window->decorate && window->frame) {
*width = frame_width(window->frame);
*height = frame_height(window->frame);
} else {
*width = window->width + t->margin * 2;
*height = window->height + t->margin * 2;
}
}
static void
weston_wm_window_get_child_position(struct weston_wm_window *window,
int *x, int *y)
{
struct theme *t = window->wm->theme;
if (window->fullscreen) {
*x = 0;
*y = 0;
} else if (window->decorate && window->frame) {
frame_interior(window->frame, x, y, NULL, NULL);
} else {
*x = t->margin;
*y = t->margin;
}
}
static void
weston_wm_window_send_configure_notify(struct weston_wm_window *window)
{
xcb_configure_notify_event_t configure_notify;
struct weston_wm *wm = window->wm;
bool is_our_resource = our_resource(wm, window->id);
int x, y;
weston_wm_window_get_child_position(window, &x, &y);
configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
configure_notify.pad0 = 0;
configure_notify.event = window->id;
configure_notify.window = window->id;
configure_notify.above_sibling = XCB_WINDOW_NONE;
configure_notify.x = x;
configure_notify.y = y;
configure_notify.width = window->width;
configure_notify.height = window->height;
configure_notify.border_width = 0;
configure_notify.override_redirect = 0;
configure_notify.pad1 = 0;
xcb_send_event(wm->conn, 0, window->id,
XCB_EVENT_MASK_STRUCTURE_NOTIFY,
(char *) &configure_notify);
wm_printf(wm, "XWM: send_configure_notify (window %d) %d,%d @ %dx%d%s\n",
window->id, x, y, window->width, window->height,
is_our_resource ? ", ours" : "");
}
static void
weston_wm_window_send_event_configure_notify_with_position(struct weston_wm_window *window, int x, int y)
{
xcb_configure_notify_event_t configure_notify;
struct weston_wm *wm = window->wm;
bool is_our_resource = our_resource(wm, window->id);
configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
configure_notify.pad0 = 0;
configure_notify.event = window->id;
configure_notify.window = window->id;
configure_notify.above_sibling = XCB_NONE;
configure_notify.x = x;
configure_notify.y = y;
configure_notify.width = window->width;
configure_notify.height = window->height;
configure_notify.border_width = 0;
configure_notify.override_redirect = window->override_redirect;
configure_notify.pad1 = 0;
xcb_send_event(wm->conn, 0, window->id,
XCB_EVENT_MASK_STRUCTURE_NOTIFY,
(char *) &configure_notify);
wm_printf(wm, "XWM: send_event_configure_notify_window_position (window %d) %d,%d @ %dx%d%s\n",
window->id, x, y, window->width, window->height,
is_our_resource ? ", ours" : "");
}
static void
weston_wm_configure_window(struct weston_wm *wm, xcb_window_t window_id,
uint16_t mask, const uint32_t *values)
{
static const struct {
xcb_config_window_t bitmask;
const char *name;
} names[] = {
{ XCB_CONFIG_WINDOW_X, "x" },
{ XCB_CONFIG_WINDOW_Y, "y" },
{ XCB_CONFIG_WINDOW_WIDTH, "width" },
{ XCB_CONFIG_WINDOW_HEIGHT, "height" },
{ XCB_CONFIG_WINDOW_BORDER_WIDTH, "border_width" },
{ XCB_CONFIG_WINDOW_SIBLING, "sibling" },
{ XCB_CONFIG_WINDOW_STACK_MODE, "stack_mode" },
};
char *buf = NULL;
size_t sz = 0;
FILE *fp;
unsigned i, v;
bool is_our_resource = our_resource(wm, window_id);
xcb_configure_window(wm->conn, window_id, mask, values);
if (!wm_debug_is_enabled(wm))
return;
fp = open_memstream(&buf, &sz);
if (!fp)
return;
fprintf(fp, "XWM: configure window %d:", window_id);
for (i = 0, v = 0; i < ARRAY_LENGTH(names); i++) {
if (mask & names[i].bitmask)
fprintf(fp, " %s=%d", names[i].name, values[v++]);
}
fprintf(fp, "%s", is_our_resource ? ", ours" : "");
fclose(fp);
wm_printf(wm, "%s\n", buf);
free(buf);
}
static void
weston_wm_window_configure_frame(struct weston_wm_window *window)
{
uint16_t mask;
uint32_t values[2];
int width, height;
if (!window->frame_id)
return;
weston_wm_window_get_frame_size(window, &width, &height);
values[0] = width;
values[1] = height;
mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
weston_wm_configure_window(window->wm, window->frame_id, mask, values);
}
static void
weston_wm_window_configure_frame_with_position(struct weston_wm_window *window, int x, int y)
{
uint16_t mask;
uint32_t values[4];
int width, height;
if (!window->frame_id)
return;
weston_wm_window_get_frame_size(window, &width, &height);
values[0] = x; // x = position of frame, not child
values[1] = y; // y = position of frame, not child
values[2] = width;
values[3] = height;
mask = XCB_CONFIG_WINDOW_X |
XCB_CONFIG_WINDOW_Y |
XCB_CONFIG_WINDOW_WIDTH |
XCB_CONFIG_WINDOW_HEIGHT;
weston_wm_configure_window(window->wm, window->frame_id, mask, values);
}
static void
weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
{
xcb_configure_request_event_t *configure_request =
(xcb_configure_request_event_t *) event;
struct weston_wm_window *window;
uint32_t values[16];
uint16_t mask = 0;
int x, y, configure_x, configure_y;
int i = 0;
bool is_our_resource = our_resource(wm, configure_request->window);
bool configure_frame_position = false;
wm_printf(wm, "XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d mask 0x%x%s\n",
configure_request->window,
configure_request->x, configure_request->y,
configure_request->width, configure_request->height,
configure_request->value_mask,
is_our_resource ? ", ours" : "");
if (!wm_lookup_window(wm, configure_request->window, &window))
return;
wm_printf(wm, "XCB_CONFIGURE_REQUEST (window %d) frame %d%s%s\n",
configure_request->window,
window->frame_id,
window->fullscreen ? ", fullscreen" : "",
window->override_redirect ? ", override" : "");
if (window->fullscreen) {
weston_wm_window_send_configure_notify(window);
return;
}
if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
window->width = configure_request->width;
if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
window->height = configure_request->height;
if (configure_request->value_mask & XCB_CONFIG_WINDOW_X)
configure_x = configure_request->x;
else
configure_x = window->x;
if (configure_request->value_mask & XCB_CONFIG_WINDOW_Y)
configure_y = configure_request->y;
else
configure_y = window->y;
if (window->frame) {
weston_wm_window_set_allow_commits(window, false);
frame_resize_inside(window->frame, window->width, window->height);
}
weston_wm_window_get_child_position(window, &x, &y);
/* don't send x/y when frame (parent window) is not created yet,
unless this is frame itself. Since only after frame is created,
the app's window position will become relative to parent (frame). */
if (window->frame || window->override_redirect) {
/* window is app's window has frame as parent, or override. */
values[i++] = x; // relative from frame.
values[i++] = y; // relative from frame.
mask |= XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
/* and if configure has differnt position than current position,
move its frame (if it has frame), so app contents will move, too */
if (window->frame && ((configure_x != window->x) || (configure_y != window->y)))
configure_frame_position = true;
}
values[i++] = window->width;
values[i++] = window->height;
values[i++] = 0;
mask |= XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
XCB_CONFIG_WINDOW_BORDER_WIDTH;
if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
values[i++] = configure_request->sibling;
mask |= XCB_CONFIG_WINDOW_SIBLING;
}
if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
values[i++] = configure_request->stack_mode;
mask |= XCB_CONFIG_WINDOW_STACK_MODE;
}
weston_wm_configure_window(wm, window->id, mask, values);
if (window->frame == NULL && !window->override_redirect) {
/* must not mapped yet */
assert(!window->shsurf);
/* if frame is not created yet, or not override window,
save window position, so it's captured at map,
weston_wm_handle_map_request() for map_request_x/y.
This over-writes the position given at create_notify. */
window->x = configure_x;
window->y = configure_y;
} else if (configure_frame_position) {
weston_wm_window_configure_frame_with_position(window,
configure_x - x,
configure_y - y);
} else {
weston_wm_window_configure_frame(window);
}