[go: up one dir, main page]

Menu

[f59a1b]: / client.c  Maximize  Restore  History

Download this file

1450 lines (1252 with data), 31.1 kB

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
/*
* Copyright 2010 Johan Veenhuizen
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "list.h"
#include "wind.h"
struct client {
struct listener listener;
List winstack;
Window window;
Colormap colormap;
struct geometry geometry;
XWMHints *wmhints;
XSizeHints *wmnormalhints;
Atom *wmprotocols;
int wmprotocolscount;
Window wmtransientfor;
// WM_NAME property in current locale encoding
char *wmname;
// _NET_WM_NAME property in UTF-8 encoding
char *netwmname;
// Application id of this client
XID app;
struct frame *frame;
Desk desk;
/*
* If this counter is zero when an UnmapNotify event
* is received, the client is considered withdrawn.
*/
int ignoreunmapcount;
Bool ismapped;
Bool hasfocus;
Bool pendingfocus;
Bool isfull;
Bool isdock;
Bool skiptaskbar;
Bool isundecorated;
Bool followdesk;
Bool initialized;
};
static void cmap(struct client *);
static void cunmap(struct client *);
static void cpop(struct client *);
static void cpush(struct client *);
static void cfocusapp(struct client *, Time);
static void reloadwmtransientfor(struct client *);
static void reloadwmhints(struct client *);
static void reloadwmnormalhints(struct client *);
static void reloadwmname(struct client *);
static void reloadwmprotocols(struct client *);
static void cupdatedesk(struct client *);
static void buttonpress(struct client *, XButtonEvent *);
static void keypress(struct client *, XKeyEvent *);
static void keypress_delete(struct client *, unsigned, Time);
static void keypress_pushapp(struct client *, unsigned, Time);
static void keypress_fullscreen(struct client *, unsigned, Time);
static void keypress_sticky(struct client *, unsigned, Time);
static void focusin(struct client *, XFocusChangeEvent *);
static void focusout(struct client *, XFocusChangeEvent *);
static void configurerequest(struct client *, XConfigureRequestEvent *);
static void propertynotify(struct client *, XPropertyEvent *);
static void maprequest(struct client *, XMapRequestEvent *);
static void unmapnotify(struct client *, XUnmapEvent *);
static void destroynotify(struct client *, XDestroyWindowEvent *);
static void clientmessage(struct client *, XClientMessageEvent *);
static void colormapnotify(struct client *, XColormapEvent *);
static void event(void *, XEvent *);
static void cinstallcolormaps(struct client *);
static void crelease(struct client *, int);
static void getclientstack(struct client ***, int *);
static void csendwmproto(struct client *, Atom, Time);
static void creframe(struct client *);
static Bool cisframed(struct client *);
static struct client *getfronttask(void);
static Bool expectsfocus(struct client *);
static void cunmanage(struct client *);
static void cwithdraw(struct client *);
static void smartpos(struct client *);
static void randpos(struct geometry *);
static Bool samedesk(struct client *, struct client *);
static Bool overlap(struct geometry, struct geometry);
static void move(struct client *, int, int);
static LIST_DEFINE(winstack);
// Current desk
static Desk curdesk = 0;
// Number of desks
static Desk ndesk = 1;
// True if restacking needed
static Bool needrestack = False;
// Dummy window for window stacking
static Window stacktop = None;
static struct {
KeySym keysym;
unsigned modifiers;
void (*function)(struct client *, unsigned, Time);
} keymap[] = {
{ XK_BackSpace, Mod1Mask, keypress_delete },
{ XK_Escape, Mod1Mask, keypress_pushapp },
{ XK_Return, Mod1Mask, keypress_fullscreen },
{ XK_space, Mod1Mask, keypress_sticky },
};
void setndesk(Desk val)
{
if (val == 0 || val >= 0xffffffffUL)
return;
Desk oldval = ndesk;
ndesk = val;
if (val >= oldval)
ewmh_notifyndesk(val);
if (curdesk >= val)
gotodesk(val - 1);
struct client **v;
int n;
getclientstack(&v, &n);
for (int i = n - 1; i >= 0; i--)
if (v[i]->desk != DESK_ALL && v[i]->desk >= val)
csetdesk(v[i], val - 1);
free(v);
if (val < oldval)
ewmh_notifyndesk(val);
}
void gotodesk(Desk d)
{
if (d == curdesk || d >= ndesk || d == DESK_ALL)
return;
curdesk = d;
/*
* Minimize the number of window exposures by first mapping
* windows from the new desk top-down, and then unmapping
* the windows from the old desk bottom-up.
*/
struct client **v;
int n;
getclientstack(&v, &n);
for (int i = n - 1; i >= 0; i--)
if (cisvisible(v[i]))
cmap(v[i]);
for (int i = 0; i < n; i++)
if (v[i]->followdesk && v[i]->desk != DESK_ALL)
csetdesk(v[i], curdesk);
else if (!cisvisible(v[i]))
cunmap(v[i]);
free(v);
ewmh_notifycurdesk(curdesk);
}
void csetappdesk(struct client *c, Desk d)
{
struct client **v;
int n;
getclientstack(&v, &n);
for (int i = 0; i < n; i++)
if (v[i]->app == c->app)
csetdesk(v[i], d);
free(v);
}
void csetdesk(struct client *c, Desk d)
{
if (d >= ndesk && d != DESK_ALL)
d = ndesk - 1;
c->desk = d;
ewmh_notifyclientdesktop(c->window, d);
if (cisvisible(c))
cmap(c);
else
cunmap(c);
// May have become sticky
if (c->frame != NULL)
fupdate(c->frame);
}
Desk cgetdesk(struct client *c)
{
return c->desk;
}
void csetdock(struct client *c, Bool isdock)
{
c->isdock = isdock;
creframe(c);
}
void csetfull(struct client *c, Bool enabled)
{
if (enabled && !c->isfull) {
if (c->frame != NULL) {
fdestroy(c->frame);
c->frame = NULL;
}
c->isfull = True;
XMoveResizeWindow(dpy, c->window,
-c->geometry.borderwidth,
-c->geometry.borderwidth,
DisplayWidth(dpy, scr),
DisplayHeight(dpy, scr));
} else if (!enabled && c->isfull) {
assert(c->frame == NULL);
c->isfull = False;
XMoveResizeWindow(dpy, c->window,
c->geometry.x, c->geometry.y,
c->geometry.width, c->geometry.height);
if (cisframed(c))
c->frame = fcreate(c);
}
ewmh_notifyfull(c->window, enabled);
}
void csetundecorated(struct client *c, Bool enabled)
{
c->isundecorated = enabled;
creframe(c);
}
void csetappfollowdesk(struct client *c, Bool enabled)
{
struct client **v;
int n;
getclientstack(&v, &n);
for (int i = 0; i < n; i++)
if (v[i]->app == c->app)
v[i]->followdesk = enabled;
free(v);
}
/*
* Return client window stack, from bottom (first) to top (last).
* Caller deallocates using free(3).
*/
void getwindowstack(Window **vp, size_t *np)
{
List *lp;
size_t n = 0;
LIST_FOREACH(lp, &winstack)
n++;
Window *v = xmalloc(n * sizeof v[0]);
size_t i = 0;
LIST_FOREACH(lp, &winstack) {
struct client *c = LIST_ITEM(lp, struct client, winstack);
v[i++] = c->window;
}
*vp = v;
*np = n;
}
void cpushapp(struct client *c)
{
struct client **v;
int n;
getclientstack(&v, &n);
for (int i = n - 1; i >= 0; i--)
if (v[i]->app == c->app)
cpush(v[i]);
free(v);
}
void cpopapp(struct client *c)
{
struct client **v;
int n;
getclientstack(&v, &n);
for (int i = 0; i < n; i++)
if (v[i]->app == c->app)
cpop(v[i]);
if (c->wmtransientfor != None) {
for (int i = 0; i < n; i++)
if (v[i]->window == c->wmtransientfor) {
cpop(v[i]);
break;
}
for (int i = 0; i < n; i++)
if (v[i]->wmtransientfor == c->wmtransientfor)
cpop(v[i]);
cpop(c);
} else {
cpop(c);
for (int i = 0; i < n; i++)
if (v[i]->wmtransientfor == c->window)
cpop(v[i]);
}
free(v);
}
/*
* Return client stack, from bottom (first) to top (last).
* Caller deallocates using free(3).
*/
static void getclientstack(struct client ***vp, int *np)
{
int n = 0;
List *lp;
LIST_FOREACH(lp, &winstack)
n++;
struct client **v = xmalloc(n * sizeof *v);
struct client **p = v;
LIST_FOREACH(lp, &winstack)
*p++ = LIST_ITEM(lp, struct client, winstack);
*vp = v;
*np = n;
}
static void cpop(struct client *c)
{
if (LIST_TAIL(&winstack) != &c->winstack) {
LIST_REMOVE(&c->winstack);
LIST_INSERT_TAIL(&winstack, &c->winstack);
needrestack = True;
}
}
static void cpush(struct client *c)
{
if (LIST_HEAD(&winstack) != &c->winstack) {
LIST_REMOVE(&c->winstack);
LIST_INSERT_HEAD(&winstack, &c->winstack);
needrestack = True;
}
}
void restack(void)
{
if (!needrestack)
return;
int n = 1;
List *lp;
LIST_FOREACH(lp, &winstack)
n++;
Window *v = xmalloc(n * sizeof *v);
int i = 0;
assert(stacktop != None);
v[i++] = stacktop;
LIST_FOREACH_REV(lp, &winstack) {
struct client *c = LIST_ITEM(lp, struct client, winstack);
v[i++] = c->frame == NULL ? c->window : fgetwin(c->frame);
}
assert(i == n);
XRestackWindows(dpy, v, n);
free(v);
needrestack = False;
ewmh_notifyrestack();
}
static void reloadwmtransientfor(struct client *c)
{
c->wmtransientfor = None;
XGetTransientForHint(dpy, c->window, &c->wmtransientfor);
if (c->wmtransientfor != None) {
c->app = c->wmtransientfor;
XWMHints *h = XGetWMHints(dpy, c->wmtransientfor);
if (h != NULL) {
if (h->flags & WindowGroupHint)
c->app = h->window_group;
XFree(h);
}
cupdatedesk(c);
}
}
static void reloadwmhints(struct client *c)
{
if (c->wmhints != NULL)
XFree(c->wmhints);
c->wmhints = XGetWMHints(dpy, c->window);
if (c->wmtransientfor == None) {
c->app = c->window;
if (c->wmhints != NULL) {
if (c->wmhints->flags & WindowGroupHint)
c->app = c->wmhints->window_group;
}
cupdatedesk(c);
}
}
static void reloadwmnormalhints(struct client *c)
{
if (c->wmnormalhints == NULL)
c->wmnormalhints = XAllocSizeHints();
if (c->wmnormalhints != NULL) {
c->wmnormalhints->flags = 0;
long dummy;
XGetWMNormalHints(dpy, c->window, c->wmnormalhints, &dummy);
}
}
static void reloadwmname(struct client *c)
{
free(c->wmname);
c->wmname = NULL;
XTextProperty p;
if (XGetWMName(dpy, c->window, &p) != 0) {
c->wmname = decodetextproperty(&p);
if (p.value != NULL)
XFree(p.value);
}
if (c->frame != NULL)
fupdate(c->frame);
}
static void reloadwmprotocols(struct client *c)
{
if (c->wmprotocols != NULL) {
XFree(c->wmprotocols);
c->wmprotocols = NULL;
}
c->wmprotocolscount = 0;
XGetWMProtocols(dpy, c->window,
&c->wmprotocols, &c->wmprotocolscount);
}
static void cupdatedesk(struct client *c)
{
Desk d = c->desk;
struct client **v;
int n;
getclientstack(&v, &n);
if (c->wmtransientfor != None) {
for (int i = n - 1; i >= 0; i--)
if (v[i]->window == c->wmtransientfor) {
d = v[i]->desk;
break;
}
} else if (c->app != c->window) {
for (int i = n - 1; i >= 0; i--)
if (v[i]->app == c->app && v[i] != c) {
d = v[i]->desk;
break;
}
}
free(v);
if (d != c->desk)
csetdesk(c, d);
}
static void buttonpress(struct client *c, XButtonEvent *e)
{
cpopapp(c);
cfocus(c, e->time);
ungrabbutton(AnyButton, AnyModifier, c->window);
XAllowEvents(dpy, ReplayPointer, CurrentTime);
}
static void keypress(struct client *c, XKeyEvent *e)
{
for (int i = 0; i < NELEM(keymap); i++)
if (XKeysymToKeycode(dpy, keymap[i].keysym) == e->keycode)
keymap[i].function(c, e->state, e->time);
}
static void keypress_delete(struct client *c, unsigned state, Time time)
{
if (!c->isdock)
cdelete(c, time);
}
static void keypress_pushapp(struct client *c, unsigned state, Time time)
{
cpushapp(c);
refocus(time);
}
static void keypress_fullscreen(struct client *c, unsigned state, Time time)
{
if (!c->isdock)
csetfull(c, !c->isfull);
}
static void keypress_sticky(struct client *c, unsigned state, Time time)
{
if (c->isdock)
return;
if (cgetdesk(c) == DESK_ALL)
csetappdesk(c, curdesk);
else {
csetappdesk(c, DESK_ALL);
// Make sure we are still on top when switching desks.
cpopapp(c);
}
}
static void focusin(struct client *c, XFocusChangeEvent *e)
{
// Ignore ungrab and pointer events to prevent confusion.
if (e->mode == NotifyUngrab || e->detail == NotifyPointerRoot ||
e->detail == NotifyPointer)
return;
// This should not happen.
if (c->hasfocus || !c->ismapped)
return;
cinstallcolormaps(c);
c->pendingfocus = False;
c->hasfocus = True;
if (c->frame != NULL)
fupdate(c->frame);
ewmh_notifyfocus(None, c->window);
}
static void focusout(struct client *c, XFocusChangeEvent *e)
{
if (e->mode == NotifyGrab)
return;
if (e->detail == NotifyPointerRoot ||
e->detail == NotifyPointer ||
e->detail == NotifyInferior)
return;
if (!c->hasfocus)
return;
c->hasfocus = False;
grabbutton(AnyButton, AnyModifier, c->window, True,
ButtonPressMask, GrabModeSync, GrabModeAsync,
None, None);
if (c->frame != NULL)
fupdate(c->frame);
ewmh_notifyfocus(c->window, None);
}
static void configurerequest(struct client *c, XConfigureRequestEvent *e)
{
if (c->frame != NULL) {
/*
* If this happens, we are processing an event that
* was sent before we created the frame. We need to
* redirect the event manually. Note that this should
* only happen immediately after creating a frame.
*
* XMMS is one program that triggers this particularly
* often, and so is the "Save As" dialog of Firefox.
*/
redirect((XEvent *)e, fgetwin(c->frame));
return;
}
if (c->isfull) {
// Deny fullscreen windows to reconfigure themselves.
csendconf(c);
return;
}
unsigned long mask = e->value_mask &
(CWX | CWY | CWWidth | CWHeight | CWBorderWidth);
if (mask & CWX)
c->geometry.x = e->x;
if (mask & CWY)
c->geometry.y = e->y;
if (mask & CWWidth)
c->geometry.width = e->width;
if (mask & CWHeight)
c->geometry.height = e->height;
if (mask & CWBorderWidth)
c->geometry.borderwidth = e->border_width;
XConfigureWindow(dpy, c->window, mask,
&(XWindowChanges){
.x = c->geometry.x,
.y = c->geometry.y,
.width = c->geometry.width,
.height = c->geometry.height,
.border_width = c->geometry.borderwidth });
}
static void propertynotify(struct client *c, XPropertyEvent *e)
{
switch (e->atom) {
case XA_WM_NAME:
reloadwmname(c);
break;
case XA_WM_HINTS:
reloadwmhints(c);
break;
case XA_WM_NORMAL_HINTS:
reloadwmnormalhints(c);
break;
case XA_WM_TRANSIENT_FOR:
reloadwmtransientfor(c);
break;
default:
if (e->atom == WM_PROTOCOLS)
reloadwmprotocols(c);
break;
}
ewmh_propertynotify(c, e);
mwm_propertynotify(c, e);
}
/*
* We don't listen to this event ourselves, but get it redirected to
* us from the root listener and from the frame listener.
*/
static void maprequest(struct client *c, XMapRequestEvent *e)
{
ewmh_maprequest(c);
cpopapp(c);
if (cisvisible(c)) {
cmap(c);
cfocus(c, CurrentTime);
}
}
static void unmapnotify(struct client *c, XUnmapEvent *e)
{
if (c->ignoreunmapcount > 0)
c->ignoreunmapcount--;
else
cwithdraw(c);
}
static void destroynotify(struct client *c, XDestroyWindowEvent *e)
{
cwithdraw(c);
}
static void clientmessage(struct client *c, XClientMessageEvent *e)
{
if (e->message_type == WM_CHANGE_STATE && e->format == 32 &&
e->data.l[0] == IconicState) {
/*
* Wind doesn't allow hidden windows, so just push it.
*/
cpushapp(c);
if (chasfocus(c))
refocus(CurrentTime);
}
ewmh_clientmessage(c, e);
}
static void colormapnotify(struct client *c, XColormapEvent *e)
{
if (e->new) {
c->colormap = e->colormap;
if (c->hasfocus)
cinstallcolormaps(c);
}
}
static void event(void *self, XEvent *e)
{
switch (e->type) {
case ButtonPress:
buttonpress(self, &e->xbutton);
break;
case KeyPress:
keypress(self, &e->xkey);
break;
case FocusIn:
focusin(self, &e->xfocus);
break;
case FocusOut:
focusout(self, &e->xfocus);
break;
case ConfigureRequest:
configurerequest(self, &e->xconfigurerequest);
break;
case PropertyNotify:
propertynotify(self, &e->xproperty);
break;
case MapRequest:
// We get this redirected to us from the root listener
// and from the frame listener.
maprequest(self, &e->xmaprequest);
break;
case UnmapNotify:
unmapnotify(self, &e->xunmap);
break;
case DestroyNotify:
destroynotify(self, &e->xdestroywindow);
break;
case ClientMessage:
clientmessage(self, &e->xclient);
break;
case ColormapNotify:
colormapnotify(self, &e->xcolormap);
break;
}
}
static void cinstallcolormaps(struct client *c)
{
XInstallColormap(dpy, c->colormap == None ?
DefaultColormap(dpy, scr) : c->colormap);
}
/*
* Returns true if the window is, or should be, visible.
*/
Bool cisvisible(struct client *c)
{
return c->desk == curdesk || c->desk == DESK_ALL;
}
/*
* Returns true if the window expects keyboard input focus.
* A window that does not specify an input hint is considered
* to expect focus.
*/
static Bool expectsfocus(struct client *c)
{
return c->wmhints == NULL ||
!(c->wmhints->flags & InputHint) ||
c->wmhints->input;
}
/*
* Returns true if the window is a task, i.e. if it appears in taskbars.
*/
Bool cistask(struct client *c)
{
return !c->skiptaskbar && c->wmtransientfor == None;
}
struct client *manage(Window window)
{
XWindowAttributes attr;
if (!XGetWindowAttributes(dpy, window, &attr))
return NULL;
if (attr.override_redirect)
return NULL;
long wmstate = getwmstate(window);
if (wmstate == WithdrawnState) {
XWMHints *h = XGetWMHints(dpy, window);
if (h == NULL)
wmstate = NormalState;
else {
if (h->flags & StateHint)
wmstate = h->initial_state;
else
wmstate = NormalState;
XFree(h);
}
}
if (wmstate == WithdrawnState)
return NULL;
struct client *c = xmalloc(sizeof *c);
LIST_INIT(&c->winstack);
LIST_INSERT_TAIL(&winstack, &c->winstack);
needrestack = True;
c->desk = curdesk;
c->frame = NULL;
c->wmname = NULL;
c->netwmname = NULL;
c->wmhints = NULL;
c->wmnormalhints = NULL;
c->wmprotocols = NULL;
c->wmprotocolscount = 0;
c->wmtransientfor = None;
c->window = window;
c->app = window;
c->colormap = attr.colormap;
c->ignoreunmapcount = 0;
c->hasfocus = False;
c->pendingfocus = False;
c->isfull = False;
c->isdock = False;
c->skiptaskbar = False;
c->isundecorated = False;
c->followdesk = False;
c->initialized = False;
csetgeom(c, (struct geometry){
attr.x,
attr.y,
attr.width,
attr.height,
attr.border_width });
XAddToSaveSet(dpy, c->window);
/*
* Everything initialized to default values. Register
* for events and THEN (re)load all attributes and
* properties. This avoids losing update events.
*/
c->listener.function = event;
c->listener.pointer = c;
setlistener(c->window, &c->listener);
XSelectInput(dpy, c->window,
StructureNotifyMask |
PropertyChangeMask |
ColormapChangeMask |
FocusChangeMask);
XSync(dpy, False);
/*
* Done registering for events. What we read now is
* safe to use, since any updates will be notified
* to our event listener.
*/
c->ismapped = ismapped(c->window);
if (!XGetWindowAttributes(dpy, c->window, &attr)) {
// The window disappeared.
crelease(c, True);
return NULL;
}
csetgeom(c, (struct geometry){
attr.x,
attr.y,
attr.width,
attr.height,
attr.border_width });
c->colormap = attr.colormap;
reloadwmname(c);
reloadwmhints(c);
reloadwmnormalhints(c);
reloadwmprotocols(c);
reloadwmtransientfor(c);
/*
* Let the hints create the frame, if there should be one.
*/
ewmh_manage(c);
mwm_manage(c);
grabbutton(AnyButton, AnyModifier, c->window, True,
ButtonPressMask, GrabModeSync, GrabModeAsync,
None, None);
for (int i = 0; i < NELEM(keymap); i++)
grabkey(XKeysymToKeycode(dpy, keymap[i].keysym),
keymap[i].modifiers, c->window, True,
GrabModeAsync, GrabModeAsync);
if (c->geometry.width == DisplayWidth(dpy, scr) &&
c->geometry.height == DisplayHeight(dpy, scr))
csetfull(c, True);
if (!cisframed(c))
ewmh_notifyframeextents(c->window, (struct extents){
.top = 0,
.bottom = 0,
.left = 0,
.right = 0 });
XSizeHints *h = c->wmnormalhints;
if (runlevel != RL_STARTUP && (h == NULL ||
(h->flags & (USPosition | PPosition)) == 0))
smartpos(c);
/*
* Make sure WM_STATE is always initiated. We can't trust
* the first call to cmap/cunmap.
*/
setwmstate(c->window, cisvisible(c) ? NormalState : IconicState);
c->initialized = True;
if (wmstate == IconicState && runlevel == RL_NORMAL) {
// Closest thing to iconic state
cpush(c);
if (cisvisible(c))
cmap(c);
} else {
cpopapp(c);
if (cisvisible(c)) {
cmap(c);
if (runlevel == RL_NORMAL)
cfocus(c, CurrentTime);
}
}
return c;
}
void manageall(void)
{
assert(stacktop == None);
stacktop = XCreateWindow(dpy, root, 0, 0, 100, 100, 0, CopyFromParent,
InputOnly, CopyFromParent, 0, NULL);
Window r, p, *stack;
unsigned n;
if (XQueryTree(dpy, root, &r, &p, &stack, &n) != 0) {
for (int i = 0; i < n; i++) {
if (ismapped(stack[i]))
manage(stack[i]);
}
if (stack != NULL)
XFree(stack);
}
restack();
}
static void cmap(struct client *c)
{
assert(c->desk == curdesk || c->desk == DESK_ALL);
// Prevent premature mapping
if (!c->initialized)
return;
if (!c->ismapped) {
/*
* Make sure stacking order is correct before
* mapping the window.
*/
restack();
if (c->frame != NULL) {
Window f = fgetwin(c->frame);
XMapSubwindows(dpy, f);
XMapWindow(dpy, f);
} else
XMapWindow(dpy, c->window);
c->ismapped = True;
setwmstate(c->window, NormalState);
}
}
static void cunmap(struct client *c)
{
if (c->ismapped) {
c->ismapped = False;
c->ignoreunmapcount++;
if (c->frame != NULL) {
Window f = fgetwin(c->frame);
XUnmapWindow(dpy, f);
XUnmapSubwindows(dpy, f);
} else
XUnmapWindow(dpy, c->window);
setwmstate(c->window, IconicState);
}
}
static void crelease(struct client *c, Bool clientrequested)
{
// Unset this or fdestroy() will refocus the window.
c->hasfocus = False;
if (c->frame != NULL) {
fdestroy(c->frame);
c->frame = NULL;
}
LIST_REMOVE(&c->winstack);
needrestack = True;
ungrabkey(AnyKey, AnyModifier, c->window);
XSelectInput(dpy, c->window, 0);
setlistener(c->window, NULL);
if (!clientrequested)
XMapWindow(dpy, c->window);
XRemoveFromSaveSet(dpy, c->window);
/*
* If we recently tried to focus this window (pendingfocus)
* and no other window currently has the focus, we need to
* find a new focus window.
*/
if (c->pendingfocus && getfocus() == NULL)
refocus(CurrentTime);
if (c->wmnormalhints != NULL)
XFree(c->wmnormalhints);
if (c->wmhints != NULL)
XFree(c->wmhints);
if (c->wmprotocols != NULL)
XFree(c->wmprotocols);
free(c->wmname);
free(c->netwmname);
free(c);
}
static void cwithdraw(struct client *c)
{
ewmh_withdraw(c);
Window w = c->window;
crelease(c, True);
setwmstate(w, WithdrawnState);
}
static void cunmanage(struct client *c)
{
ewmh_unmanage(c);
Window w = c->window;
crelease(c, False);
setwmstate(w, NormalState);
}
void cdelete(struct client *c, Time time)
{
/*
* Don't check if it supports delete. Most windows do,
* and half of the ones that don't announce support
* just forgot to announce it.
*/
csendwmproto(c, WM_DELETE_WINDOW, time);
}
void unmanageall(void)
{
struct client **v;
int n;
getclientstack(&v, &n);
for (int i = n - 1; i >= 0; i--)
cunmanage(v[i]);
free(v);
if (stacktop != None) {
XDestroyWindow(dpy, stacktop);
stacktop = None;
}
}
struct client *getfocus(void)
{
List *lp;
LIST_FOREACH_REV(lp, &winstack) {
struct client *c = LIST_ITEM(lp, struct client, winstack);
if (c->hasfocus) {
assert(c->desk == curdesk || c->desk == DESK_ALL);
assert(c->ismapped);
return c;
}
}
return NULL;
}
static struct client *getfronttask(void)
{
List *lp;
LIST_FOREACH_REV(lp, &winstack) {
struct client *c = LIST_ITEM(lp, struct client, winstack);
if (cisvisible(c) && cistask(c))
return c;
}
return NULL;
}
static void cfocusapp(struct client *c, Time time)
{
struct client *topmost = NULL;
struct client *focus = NULL;
// Find a window of the application that expects focus.
List *lp;
LIST_FOREACH_REV(lp, &winstack) {
struct client *x = LIST_ITEM(
lp, struct client, winstack);
if (x->app == c->app && cisvisible(x)) {
if (topmost == NULL)
topmost = x;
if (focus == NULL && expectsfocus(x))
focus = x;
}
}
if (focus == NULL)
focus = topmost;
assert(focus != NULL);
cfocus(focus, time);
}
void cfocus(struct client *c, Time time)
{
if (c->hasfocus || !cisvisible(c))
return;
/*
* ICCCM says we MUST NOT send CurrentTime in WM_TAKE_FOCUS
* messages, so if we are given CurrentTime we set focus
* explicitly.
*/
if (time != CurrentTime && !expectsfocus(c) &&
chaswmproto(c, WM_TAKE_FOCUS))
csendwmproto(c, WM_TAKE_FOCUS, time);
else
XSetInputFocus(dpy, c->window, RevertToPointerRoot, time);
c->pendingfocus = True;
}
/*
* Focus the front window and return it.
*/
struct client *refocus(Time time)
{
struct client *c = getfronttask();
if (c != NULL)
cfocusapp(c, time);
return c;
}
int namewidth(struct font *font, struct client *c)
{
if (c->netwmname != NULL)
return fttextwidth_utf8(font, c->netwmname);
else if (c->wmname != NULL)
return fttextwidth(font, c->wmname);
else
return 0;
}
void drawname(Drawable d, struct font *font, struct fontcolor *color,
int x, int y, struct client *c)
{
if (c->netwmname != NULL)
ftdrawstring_utf8(d, font, color, x, y, c->netwmname);
else if (c->wmname != NULL)
ftdrawstring(d, font, color, x, y, c->wmname);
}
Bool chaswmproto(struct client *c, Atom protocol)
{
for (int i = 0; i < c->wmprotocolscount; i++)
if (c->wmprotocols[i] == protocol)
return True;
return False;
}
static void csendwmproto(struct client *c, Atom protocol, Time time)
{
XEvent e;
memset(&e, 0, sizeof e);
e.xclient.type = ClientMessage;
e.xclient.window = c->window;
e.xclient.message_type = WM_PROTOCOLS;
e.xclient.format = 32;
e.xclient.data.l[0] = protocol;
e.xclient.data.l[1] = time;
XSendEvent(dpy, c->window, False, 0L, &e);
}
struct geometry cgetgeom(struct client *c)
{
if (c->isfull)
return (struct geometry){
.x = -c->geometry.borderwidth,
.y = -c->geometry.borderwidth,
.width = DisplayWidth(dpy, scr),
.height = DisplayHeight(dpy, scr),
.borderwidth = c->geometry.borderwidth };
else
return c->geometry;
}
void csetgeom(struct client *c, struct geometry g)
{
c->geometry = g;
}
void adjustclientsize(struct client *c, int width, int height,
int *rwidth, int *rheight)
{
width = MAX(1, width);
height = MAX(1, height);
XSizeHints *h = c->wmnormalhints;
if (h == NULL) {
*rwidth = width;
*rheight = height;
return;
}
int basewidth;
int baseheight;
if (h->flags & PBaseSize) {
basewidth = h->base_width;
baseheight = h->base_height;
} else if (h->flags & PMinSize) {
basewidth = h->min_width;
baseheight = h->min_height;
} else {
basewidth = 0;
baseheight = 0;
}
// Cannot be less than the base (or minimum)
width = MAX(basewidth, width);
height = MAX(baseheight, height);
if (h->flags & PResizeInc) {
if (h->width_inc != 0)
width -= (width - basewidth) % h->width_inc;
if (h->height_inc != 0)
height -= (height - baseheight) % h->height_inc;
}
if (h->flags & PMaxSize) {
width = MIN(h->max_width, width);
height = MIN(h->max_height, height);
}
width = MAX(1, width);
height = MAX(1, height);
*rwidth = width;
*rheight = height;
}
void csendconf(struct client *c)
{
struct geometry g = cgetgeom(c);
XSendEvent(dpy, c->window, False, StructureNotifyMask,
(XEvent *)&(XConfigureEvent){
.type = ConfigureNotify,
.event = c->window,
.window = c->window,
.x = g.x,
.y = g.y,
.width = g.width,
.height = g.height,
.border_width = g.borderwidth,
.above = None,
.override_redirect = False });
}
Bool chasfocus(struct client *c)
{
return c->hasfocus;
}
Window cgetwin(struct client *c)
{
return c->window;
}
void csetnetwmname(struct client *c, const char *name)
{
free(c->netwmname);
c->netwmname = (name == NULL) ? NULL : xstrdup(name);
if (c->frame != NULL)
fupdate(c->frame);
}
Bool cgetwmnormalhints(struct client *c, XSizeHints *hints)
{
if (c->wmnormalhints == NULL)
return False;
else {
*hints = *c->wmnormalhints;
return True;
}
}
void cignoreunmap(struct client *c)
{
assert(c->ismapped);
c->ignoreunmapcount++;
}
Bool cismapped(struct client *c)
{
return c->ismapped;
}
static void creframe(struct client *c)
{
if (cisframed(c)) {
if (c->frame == NULL)
c->frame = fcreate(c);
} else if (c->frame != NULL) {
fdestroy(c->frame);
c->frame = NULL;
}
}
static Bool cisframed(struct client *c)
{
return !c->isfull && !c->isdock && !c->isundecorated;
}
void csetskiptaskbar(struct client *c, Bool skiptaskbar)
{
c->skiptaskbar = skiptaskbar;
}
/*
* Find a good location for the specified client and move it there.
*/
static void smartpos(struct client *c)
{
struct geometry g = c->frame == NULL ?
cgetgeom(c) : fgetgeom(c->frame);
struct client **v;
int n;
getclientstack(&v, &n);
// Exclude the window itself, and clients on other desks
for (int i = 0; i < n; i++)
if (v[i] == c || !samedesk(v[i], c))
v[i--] = v[--n];
/*
* Examine a number of random locations and pick the least bad.
* A "bad" location is defined as a location that makes the
* window overlap other windows, the more the worse. This is
* in no way optimal, but it is reasonably fast and avoids
* really bad locations.
*/
unsigned min = ~0;
struct geometry best = g;
for (int k = 0; min != 0 && k < 100; k++) {
randpos(&g);
unsigned badness = 0;
for (int i = 0; i < n; i++) {
struct geometry g2 = v[i]->frame == NULL ?
cgetgeom(v[i]) : fgetgeom(v[i]->frame);
if (overlap(g, g2))
badness++;
}
if (badness < min) {
min = badness;
best = g;
}
}
move(c, best.x, best.y);
free(v);
}
/*
* Find a random location for the specified geometry.
*/
static void randpos(struct geometry *g)
{
int maxx = DisplayWidth(dpy, scr) - (g->width + 2 * g->borderwidth);
int maxy = DisplayHeight(dpy, scr) - (g->height + 2 * g->borderwidth);
g->x = maxx > 0 ? rand() % maxx : 0;
g->y = maxy > 0 ? rand() % maxy : 0;
}
/*
* Return true if, and only if, the two clients are visible on the same desk.
*/
static Bool samedesk(struct client *c1, struct client *c2)
{
return c1->desk == c2->desk ||
c1->desk == DESK_ALL || c2->desk == DESK_ALL;
}
/*
* Return true if, and only if, the two geometries overlap.
*/
static Bool overlap(struct geometry g1, struct geometry g2)
{
int x1 = g1.x;
int x2 = g2.x;
int x3 = g1.x + g1.width + 2 * g1.borderwidth;
int x4 = g2.x + g2.width + 2 * g2.borderwidth;
int y1 = g1.y;
int y2 = g2.y;
int y3 = g1.y + g1.height + 2 * g1.borderwidth;
int y4 = g2.y + g2.height + 2 * g2.borderwidth;
return x1 < x4 && x2 < x3 && y1 < y4 && y2 < y3;
}
/*
* XXX: We move a window by simulating a ConfigureRequest from
* the client.
*/
static void move(struct client *c, int x, int y)
{
Window parent = c->frame == NULL ? root : fgetwin(c->frame);
XEvent e;
e.xconfigurerequest = (XConfigureRequestEvent){
.type = ConfigureRequest,
.serial = 0,
.send_event = True,
.display = dpy,
.parent = parent,
.window = c->window,
.x = x,
.y = y,
.value_mask = CWX | CWY };
redirect(&e, parent);
}