-
Notifications
You must be signed in to change notification settings - Fork 95
/
2bwm.c
3425 lines (2908 loc) · 85 KB
/
2bwm.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
/* 2bwm, a fast floating WM with the particularity of having 2 borders written
* over the XCB library and derived from mcwm written by Michael Cardell.
* Heavily modified version of http://www.hack.org/mc/hacks/mcwm/
* Copyright (c) 2010, 2011, 2012 Michael Cardell Widerkrantz, mc at the domain hack.org.
* Copyright (c) 2014, 2020 Patrick Louis, patrick at the domain psychology dot wtf.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <xcb/randr.h>
#include <xcb/xcb_keysyms.h>
#include <xcb/xcb_icccm.h>
#include <xcb/xcb_ewmh.h>
#include <xcb/xcb_xrm.h>
#include <X11/keysym.h>
#include "list.h"
#include "definitions.h"
#include "types.h"
///---Internal Constants---///
///---Globals---///
static xcb_generic_event_t *ev = NULL;
static void (*events[XCB_NO_OPERATION])(xcb_generic_event_t *e);
static unsigned int numlockmask = 0;
static bool is_sloppy = true; // by default use sloppy focus
int sigcode = 0; // Signal code. Non-zero if we've been interruped by a signal.
xcb_connection_t *conn = NULL; // Connection to X server.
xcb_ewmh_connection_t *ewmh = NULL; // Ewmh Connection.
xcb_screen_t *screen = NULL; // Our current screen.
int randrbase = 0; // Beginning of RANDR extension events.
static uint8_t curws = 0; // Current workspace.
struct client *focuswin = NULL; // Current focus window.
struct client *lastwin[WORKSPACES]; // Last focused window.
static xcb_drawable_t top_win=0; // Window always on top.
static xcb_drawable_t dock_win=0; // A single dock always on top.
static struct item *winlist = NULL; // Global list of all client windows.
static struct item *monlist = NULL; // List of all physical monitor outputs.
static struct item *wslist[WORKSPACES];
///---Global configuration.---///
static const char *atomnames[NB_ATOMS][1] = {
{"WM_DELETE_WINDOW"},
{"WM_CHANGE_STATE"}
};
xcb_atom_t ATOM[NB_ATOMS];
///---Functions prototypes---///
static void run(void);
static bool setup(int);
static void install_sig_handlers(void);
static void start(const Arg *);
static void mousemotion(const Arg *);
static void cursor_move(const Arg *);
static void changeworkspace(const Arg *);
static void changeworkspace_helper(const uint32_t);
static void focusnext(const Arg *);
static void focuslastwin(const Arg *);
static void focusnext_helper(bool);
static void sendtoworkspace(const Arg *);
static void sendtonextworkspace(const Arg *);
static void sendtoprevworkspace(const Arg *);
static void resizestep(const Arg *);
static void resizestep_aspect(const Arg *);
static void movestep(const Arg *);
static void maxvert_hor(const Arg *);
static void maxhalf(const Arg *);
static void teleport(const Arg *);
static void changescreen(const Arg *);
static void grabkeys(void);
static void twobwm_restart();
static void twobwm_exit();
static void centerpointer(xcb_drawable_t, struct client *);
static void always_on_top();
static bool setup_keyboard(void);
static bool setupscreen(void);
static int setuprandr(void);
static void arrangewindows(void);
static void prevworkspace();
static void nextworkspace();
static void getrandr(void);
static void raise_current_window(void);
static void raiseorlower();
static void setunfocus(void);
static void maximize(const Arg *);
static void fullscreen(const Arg *);
static void unmaxwin(struct client *);
static void maxwin(struct client *, uint8_t);
static void maximize_helper(struct client *,uint16_t, uint16_t, uint16_t, uint16_t);
static void hide();
static void clientmessage(xcb_generic_event_t *);
static void deletewin();
static void unkillable();
static void fix();
static void check_name(struct client *);
static void addtoclientlist(const xcb_drawable_t);
static void configurerequest(xcb_generic_event_t *);
static void buttonpress(xcb_generic_event_t *);
static void unmapnotify(xcb_generic_event_t *);
static void mapnotify(xcb_generic_event_t *);
static void destroynotify(xcb_generic_event_t *);
static void circulaterequest(xcb_generic_event_t *);
static void newwin(xcb_generic_event_t *);
static void handle_keypress(xcb_generic_event_t *);
static xcb_cursor_t Create_Font_Cursor(xcb_connection_t *, uint16_t);
static xcb_keycode_t *xcb_get_keycodes(xcb_keysym_t);
static xcb_screen_t *xcb_screen_of_display(xcb_connection_t *, int);
static struct client *setupwin(xcb_window_t);
static struct client create_back_win(void);
static void cleanup(void);
static uint32_t getwmdesktop(xcb_drawable_t);
static void addtoworkspace(struct client *, uint32_t);
static void grabbuttons(struct client *);
static void delfromworkspace(struct client *);
static void unkillablewindow(struct client *);
static void fixwindow(struct client *);
static uint32_t getcolor(const char *);
static void forgetclient(struct client *);
static void forgetwin(xcb_window_t);
static void fitonscreen(struct client *);
static void getoutputs(xcb_randr_output_t *,const int, xcb_timestamp_t);
static void arrbymon(struct monitor *);
static struct monitor *findmonitor(xcb_randr_output_t);
static struct monitor *findclones(xcb_randr_output_t, const int16_t, const int16_t);
static struct monitor *findmonbycoord(const int16_t, const int16_t);
static void delmonitor(struct monitor *);
static struct monitor *addmonitor(xcb_randr_output_t, const int16_t, const int16_t, const uint16_t, const uint16_t);
static void raisewindow(xcb_drawable_t);
static void movelim(struct client *);
static void movewindow(xcb_drawable_t, const int16_t, const int16_t);
static struct client *findclient(const xcb_drawable_t *);
static void setfocus(struct client *);
static void resizelim(struct client *);
static void resize(xcb_drawable_t, const uint16_t, const uint16_t);
static void moveresize(xcb_drawable_t, const uint16_t, const uint16_t,const uint16_t, const uint16_t);
static void mousemove(const int16_t,const int16_t);
static void mouseresize(struct client *,const int16_t,const int16_t);
static void setborders(struct client *,const bool);
static void unmax(struct client *);
static bool getpointer(const xcb_drawable_t *, int16_t *,int16_t *);
static bool getgeom(const xcb_drawable_t *, int16_t *, int16_t *, uint16_t *,uint16_t *, uint8_t *);
static void configwin(xcb_window_t, uint16_t,const struct winconf *);
static void sigcatch(const int);
static void ewmh_init(void);
static xcb_atom_t getatom(const char *);
static void getmonsize(int8_t, int16_t *, int16_t *, uint16_t *, uint16_t *,const struct client *);
static void noborder(int16_t *,struct client *, bool);
static void movepointerback(const int16_t, const int16_t, const struct client *);
static void snapwindow(struct client *);
#include "config.h"
///---Function bodies---///
void
fix()
{
fixwindow(focuswin);
}
void
unkillable()
{
unkillablewindow(focuswin);
}
void
delmonitor(struct monitor *mon)
{
freeitem(&monlist, NULL, mon->item);
}
void
raise_current_window(void)
{
raisewindow(focuswin->id);
}
void
focusnext(const Arg *arg)
{
focusnext_helper(arg->i > 0);
}
void
focuslastwin(const Arg *arg)
{
if (lastwin[curws] == NULL)
return;
raisewindow(lastwin[curws]->id);
setfocus(lastwin[curws]);
}
void
delfromworkspace(struct client *client)
{
if(client->ws < 0)
return;
delitem(&wslist[client->ws], client->wsitem);
client->wsitem = NULL;
client->ws = -1;
}
void
changeworkspace(const Arg *arg)
{
changeworkspace_helper(arg->i);
}
void
nextworkspace()
{
curws == WORKSPACES - 1 ? changeworkspace_helper(0)
:changeworkspace_helper(curws+1);
}
void
prevworkspace()
{
curws > 0 ? changeworkspace_helper(curws - 1)
: changeworkspace_helper(WORKSPACES-1);}
void
twobwm_exit()
{
exit(EXIT_SUCCESS);
}
void
saveorigsize(struct client *client)
{
client->origsize.x = client->x;
client->origsize.y = client->y;
client->origsize.width = client->width;
client->origsize.height = client->height;
}
void
centerpointer(xcb_drawable_t win, struct client *cl)
{
int16_t cur_x, cur_y;
cur_x = cur_y = 0;
switch(CURSOR_POSITION) {
case BOTTOM_RIGHT:
cur_x += cl->width;
case BOTTOM_LEFT:
cur_y += cl->height; break;
case TOP_RIGHT:
cur_x += cl->width;
case TOP_LEFT:
break;
default:
cur_x = cl->width / 2;
cur_y = cl->height / 2;
}
xcb_warp_pointer(conn, XCB_NONE, win, 0, 0, 0, 0, cur_x, cur_y);
}
void
updateclientlist(void)
{
uint32_t len,i;
xcb_window_t *children;
struct client *cl;
/* can only be called after the first window has been spawn */
xcb_query_tree_reply_t *reply = xcb_query_tree_reply(conn,
xcb_query_tree(conn, screen->root), 0);
xcb_delete_property(conn, screen->root, ewmh->_NET_CLIENT_LIST);
xcb_delete_property(conn, screen->root, ewmh->_NET_CLIENT_LIST_STACKING);
if (reply == NULL) {
addtoclientlist(0);
return;
}
len = xcb_query_tree_children_length(reply);
children = xcb_query_tree_children(reply);
for (i=0; i < len; i ++) {
cl = findclient(&children[i]);
if(cl != NULL)
addtoclientlist(cl->id);
}
free(reply);
}
/* get screen of display */
xcb_screen_t *
xcb_screen_of_display(xcb_connection_t *con, int screen)
{
xcb_screen_iterator_t iter;
iter = xcb_setup_roots_iterator(xcb_get_setup(con));
for (; iter.rem; --screen, xcb_screen_next(&iter))
if (screen == 0)
return iter.data;
return NULL;
}
void
movepointerback(const int16_t startx, const int16_t starty,
const struct client *client)
{
if (startx > (0 - conf.borderwidth - 1) && startx < (client->width
+ conf.borderwidth + 1) && starty > (0
- conf.borderwidth - 1) && starty
< (client->height + conf.borderwidth + 1))
xcb_warp_pointer(conn, XCB_NONE, client->id,0,0,0,0, startx,
starty);
}
/* Set keyboard focus to follow mouse pointer. Then exit. We don't need to
* bother mapping all windows we know about. They should all be in the X
* server's Save Set and should be mapped automagically. */
void
cleanup(void)
{
free(ev);
if(monlist)
delallitems(&monlist,NULL);
struct item *curr,*wsitem;
for(int i = 0; i < WORKSPACES; i++){
if(!wslist[i])
continue;
curr = wslist[i];
while(curr){
wsitem = curr;
curr = curr->next;
free(wsitem);
}
}
if(winlist){
delallitems(&winlist,NULL);
}
if (ewmh != NULL){
xcb_ewmh_connection_wipe(ewmh);
free(ewmh);
}
if(!conn){
return;
}
xcb_set_input_focus(conn, XCB_NONE,XCB_INPUT_FOCUS_POINTER_ROOT,
XCB_CURRENT_TIME);
xcb_flush(conn);
xcb_disconnect(conn);
}
/* Rearrange windows to fit new screen size. */
void
arrangewindows(void)
{
struct client *client;
struct item *item;
/* Go through all windows and resize them appropriately to
* fit the screen. */
for (item=winlist; item != NULL; item = item->next) {
client = item->data;
fitonscreen(client);
}
}
uint32_t getwmdesktop(xcb_drawable_t win)
{ // Get EWWM hint so we might know what workspace window win should be visible on.
// Returns either workspace, NET_WM_FIXED if this window should be
// visible on all workspaces or TWOBWM_NOWS if we didn't find any hints.
xcb_get_property_cookie_t cookie = xcb_get_property(conn, false, win, ewmh->_NET_WM_DESKTOP,
XCB_GET_PROPERTY_TYPE_ANY, 0, sizeof(uint32_t));
xcb_get_property_reply_t *reply = xcb_get_property_reply(conn, cookie, NULL);
if (NULL==reply || 0 == xcb_get_property_value_length(reply)) { /* 0 if we didn't find it. */
if(NULL!=reply) free(reply);
return TWOBWM_NOWS;
}
uint32_t wsp = *(uint32_t *)xcb_get_property_value(reply);
if(NULL!=reply)free(reply);
return wsp;
}
/* check if the window is unkillable, if yes return true */
bool
get_unkil_state(xcb_drawable_t win)
{
xcb_get_property_cookie_t cookie;
xcb_get_property_reply_t *reply;
uint8_t wsp;
cookie = xcb_get_property(conn, false, win, ewmh->_NET_WM_STATE_DEMANDS_ATTENTION,
XCB_GET_PROPERTY_TYPE_ANY, 0,sizeof(uint8_t));
reply = xcb_get_property_reply(conn, cookie, NULL);
if (reply == NULL || xcb_get_property_value_length(reply) == 0) {
if(reply != NULL )
free(reply);
return false;
}
wsp = *(uint8_t *) xcb_get_property_value(reply);
if (reply != NULL)
free(reply);
if (wsp == 1)
return true;
else
return false;
}
void
check_name(struct client *client)
{
xcb_get_property_reply_t *reply ;
unsigned int reply_len;
char *wm_name_window;
unsigned int i;
uint32_t values[1] = { 0 };
if (NULL == client)
return;
reply = xcb_get_property_reply(conn, xcb_get_property(conn, false,
client->id, getatom(LOOK_INTO) ,
XCB_GET_PROPERTY_TYPE_ANY, 0,60), NULL
);
if (reply == NULL || xcb_get_property_value_length(reply) == 0) {
if (NULL!=reply)
free(reply);
return;
}
reply_len = xcb_get_property_value_length(reply);
wm_name_window = malloc(sizeof(char*) * (reply_len+1));;
memcpy(wm_name_window, xcb_get_property_value(reply), reply_len);
wm_name_window[reply_len] = '\0';
if(NULL != reply)
free(reply);
for(i=0;i<sizeof(ignore_names)/sizeof(__typeof__(*ignore_names));i++)
if (strstr(wm_name_window, ignore_names[i]) !=NULL) {
client->ignore_borders = true;
xcb_configure_window(conn, client->id,
XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
break;
}
free(wm_name_window);
}
/* Add a window, specified by client, to workspace ws. */
void
addtoworkspace(struct client *client, uint32_t ws)
{
if (ws < 0 || ws > WORKSPACES) {
// this fixes pip mode for Chrome as it somewhat returns ws=4294967295
ws = curws;
}
struct item *item = additem(&wslist[ws]);
if (client == NULL)
return;
if (NULL == item)
return;
/* Remember our place in the workspace window list. */
client->wsitem = item;
client->ws = ws;
item->data = client;
/* Set window hint property so we can survive a crash. Like "fixed" */
if (!client->fixed)
xcb_change_property(conn, XCB_PROP_MODE_REPLACE, client->id,
ewmh->_NET_WM_DESKTOP, XCB_ATOM_CARDINAL, 32, 1,
&ws
);
}
static void addtoclientlist(const xcb_drawable_t id)
{
xcb_change_property(conn, XCB_PROP_MODE_APPEND , screen->root, ewmh->_NET_CLIENT_LIST, XCB_ATOM_WINDOW, 32, 1,&id);
xcb_change_property(conn, XCB_PROP_MODE_APPEND , screen->root, ewmh->_NET_CLIENT_LIST_STACKING, XCB_ATOM_WINDOW, 32, 1,&id);
}
/* Change current workspace to ws */
void
changeworkspace_helper(const uint32_t ws)
{
xcb_query_pointer_reply_t *pointer;
struct client *client;
struct item *item;
if (ws == curws)
return;
xcb_ewmh_set_current_desktop(ewmh, 0, ws);
/* Go through list of current ws.
* Unmap everything that isn't fixed. */
for (item=wslist[curws]; item != NULL;) {
client = item->data;
item = item->next;
setborders(client,false);
if (!client->fixed){
xcb_unmap_window(conn, client->id);
}else{
// correct order is delete first add later.
delfromworkspace(client);
addtoworkspace(client,ws);
}
}
for (item=wslist[ws]; item != NULL; item = item->next) {
client = item->data;
if (!client->fixed && !client->iconic)
xcb_map_window(conn, client->id);
}
curws = ws;
pointer = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
screen->root), 0);
if(pointer == NULL)
setfocus(NULL);
else {
setfocus(findclient(&pointer->child));
free(pointer);
}
}
void
always_on_top()
{
struct client *cl = NULL;
if(focuswin==NULL)
return;
if(top_win!=focuswin->id) {
if (0 != top_win) cl = findclient(&top_win);
top_win = focuswin->id;
if (NULL!=cl)
setborders(cl, false);
raisewindow(top_win);
} else
top_win = 0;
setborders(focuswin,true);
}
/* Fix or unfix a window client from all workspaces. If setcolour is */
void
fixwindow(struct client *client)
{
uint32_t ws,ww;
if (NULL == client)
return;
if (client->fixed) {
client->fixed = false;
xcb_change_property(conn, XCB_PROP_MODE_REPLACE, client->id,
ewmh->_NET_WM_DESKTOP, XCB_ATOM_CARDINAL, 32, 1,
&curws
);
} else {
/* Raise the window, if going to another desktop don't
* let the fixed window behind. */
raisewindow(client->id);
client->fixed = true;
ww = NET_WM_FIXED;
xcb_change_property(conn, XCB_PROP_MODE_REPLACE, client->id,
ewmh->_NET_WM_DESKTOP, XCB_ATOM_CARDINAL, 32, 1,
&ww
);
}
setborders(client,true);
}
/* Make unkillable or killable a window client. If setcolour is */
void
unkillablewindow(struct client *client)
{
if (NULL == client)
return;
if (client->unkillable) {
client->unkillable = false;
xcb_delete_property(conn, client->id, ewmh->_NET_WM_STATE_DEMANDS_ATTENTION);
} else {
raisewindow(client->id);
client->unkillable = true;
xcb_change_property(conn, XCB_PROP_MODE_REPLACE, client->id,
ewmh->_NET_WM_STATE_DEMANDS_ATTENTION, XCB_ATOM_CARDINAL, 8, 1,
&client->unkillable
);
}
setborders(client,true);
}
void
sendtoworkspace(const Arg *arg)
{
if (NULL == focuswin||focuswin->fixed||arg->i == curws)
return;
// correct order is delete first add later.
delfromworkspace(focuswin);
addtoworkspace(focuswin, arg->i);
xcb_unmap_window(conn, focuswin->id);
xcb_flush(conn);
}
void
sendtonextworkspace(const Arg *arg)
{
Arg arg2 = { .i=0 };
Arg arg3 = { .i=curws + 1 };
curws == WORKSPACES - 1 ? sendtoworkspace(&arg2):sendtoworkspace(&arg3);
}
void
sendtoprevworkspace(const Arg *arg)
{
Arg arg2 = {.i=curws-1};
Arg arg3 = {.i=WORKSPACES-1};
curws > 0 ? sendtoworkspace(&arg2) : sendtoworkspace(&arg3);
}
/* Get the pixel values of a named colour colstr. Returns pixel values. */
uint32_t
getcolor(const char *hex)
{
uint32_t rgb48;
char strgroups[7] = {
hex[1], hex[2], hex[3], hex[4], hex[5], hex[6], '\0'
};
rgb48 = strtol(strgroups, NULL, 16);
return rgb48 | 0xff000000;
}
/* Forget everything about client client. */
void
forgetclient(struct client *client)
{
uint32_t ws;
if (NULL == client)
return;
if (client->id == top_win)
top_win = 0;
if (client->id == dock_win)
dock_win = 0;
/* Delete client from the workspace list it belongs to. */
delfromworkspace(client);
/* Remove from global window list. */
freeitem(&winlist, NULL, client->winitem);
}
/* Forget everything about a client with client->id win. */
void
forgetwin(xcb_window_t win)
{
struct client *client;
struct item *item;
for (item = winlist; item != NULL; item = item->next) {
/* Find this window in the global window list. */
client = item->data;
if (win == client->id) {
/* Forget it and free allocated data, it might
* already be freed by handling an UnmapNotify. */
forgetclient(client);
return;
}
}
}
void
getmonsize(int8_t with_offsets, int16_t *mon_x, int16_t *mon_y,
uint16_t *mon_width, uint16_t *mon_height,
const struct client *client)
{
if (NULL == client || NULL == client->monitor) {
/* Window isn't attached to any monitor, so we use
* the root window size. */
*mon_x = *mon_y = 0;
*mon_width = screen->width_in_pixels;
*mon_height = screen->height_in_pixels;
} else {
*mon_x = client->monitor->x;
*mon_y = client->monitor->y;
*mon_width = client->monitor->width;
*mon_height = client->monitor->height;
}
if (with_offsets) {
*mon_x += offsets[0];
*mon_y += offsets[1];
*mon_width -= offsets[2];
*mon_height -= offsets[3];
}
}
void
noborder(int16_t *temp,struct client *client, bool set_unset)
{
if (client->ignore_borders) {
if (set_unset) {
*temp = conf.borderwidth;
conf.borderwidth = 0;
} else
conf.borderwidth = *temp;
}
}
void
maximize_helper(struct client *client,uint16_t mon_x, uint16_t mon_y,
uint16_t mon_width, uint16_t mon_height)
{
uint32_t values[4];
values[0] = 0;
saveorigsize(client);
xcb_configure_window(conn, client->id, XCB_CONFIG_WINDOW_BORDER_WIDTH,
values);
client->x = mon_x;
client->y = mon_y;
client->width = mon_width;
client->height = mon_height;
moveresize(client->id, client->x, client->y, client->width,
client->height);
client->maxed = true;
}
/* Fit client on physical screen, moving and resizing as necessary. */
void
fitonscreen(struct client *client)
{
int16_t mon_x, mon_y,temp=0;
uint16_t mon_width, mon_height;
bool willmove,willresize;
willmove = willresize = client->vertmaxed = client->hormaxed = false;
getmonsize(1, &mon_x, &mon_y, &mon_width, &mon_height,client);
if (client->maxed) {
client->maxed = false;
setborders(client,false);
} else {
/* not maxed but look as if it was maxed, then make it maxed */
if (client->width==mon_width && client->height==mon_height)
willresize = true;
else {
getmonsize(0, &mon_x, &mon_y, &mon_width, &mon_height,
client);
if (client->width == mon_width && client->height
== mon_height)
willresize = true;
}
if (willresize) {
client->x = mon_x;
client->y = mon_y;
client->width -= conf.borderwidth * 2;
client->height-= conf.borderwidth * 2;
maximize_helper(client, mon_x, mon_y, mon_width,
mon_height);
return;
} else {
getmonsize(1, &mon_x, &mon_y, &mon_width,
&mon_height,client);
}
}
if (client->x > mon_x + mon_width || client->y > mon_y + mon_height
|| client->x < mon_x||client->y < mon_y) {
willmove = true;
/* Is it outside the physical monitor? */
if (client->x > mon_x + mon_width)
client->x = mon_x + mon_width
- client->width-conf.borderwidth*2;
if (client->y > mon_y + mon_height)
client->y = mon_y + mon_height - client->height
- conf.borderwidth*2;
if (client->x < mon_x)
client->x = mon_x;
if (client->y < mon_y)
client->y = mon_y;
}
/* Is it smaller than it wants to be? */
if (0 != client->min_height && client->height < client->min_height) {
client->height = client->min_height;
willresize = true;
}
if (0 != client->min_width && client->width < client->min_width) {
client->width = client->min_width;
willresize = true;
}
noborder(&temp, client, true);
/* If the window is larger than our screen, just place it in
* the corner and resize. */
if (client->width + conf.borderwidth * 2 > mon_width) {
client->x = mon_x;
client->width = mon_width - conf.borderwidth * 2;;
willmove = willresize = true;
} else
if (client->x + client->width + conf.borderwidth * 2
> mon_x + mon_width) {
client->x = mon_x + mon_width - (client->width
+ conf.borderwidth * 2);
willmove = true;
}
if (client->height + conf.borderwidth * 2 > mon_height) {
client->y = mon_y;
client->height = mon_height - conf.borderwidth * 2;
willmove = willresize = true;
} else
if (client->y + client->height + conf.borderwidth*2 > mon_y
+ mon_height) {
client->y = mon_y + mon_height - (client->height
+ conf.borderwidth * 2);
willmove = true;
}
if (willmove)
movewindow(client->id, client->x, client->y);
if (willresize)
resize(client->id, client->width, client->height);
noborder( &temp, client, false);
}
/* Set position, geometry and attributes of a new window and show it on
* the screen.*/
void
newwin(xcb_generic_event_t *ev)
{
xcb_map_request_event_t *e = (xcb_map_request_event_t *) ev;
struct client *client;
long data[] = {
XCB_ICCCM_WM_STATE_NORMAL,
XCB_NONE
};
/* The window is trying to map itself on the current workspace,
* but since it's unmapped it probably belongs on another workspace.*/
if (NULL != findclient(&e->window))
return;
client = setupwin(e->window);
if (NULL == client)
return;
/* Add this window to the current workspace. */
addtoworkspace(client, curws);
/* If we don't have specific coord map it where the pointer is.*/
if (!client->usercoord) {
if (!getpointer(&screen->root, &client->x, &client->y))
client->x = client->y = 0;
client->x -= client->width/2; client->y -= client->height/2;
movewindow(client->id, client->x, client->y);
}
/* Find the physical output this window will be on if RANDR is active */
if (-1 != randrbase) {
client->monitor = findmonbycoord(client->x, client->y);
if (NULL == client->monitor && NULL != monlist)
/* Window coordinates are outside all physical monitors.
* Choose the first screen.*/
client->monitor = monlist->data;
}
fitonscreen(client);
/* Show window on screen. */
xcb_map_window(conn, client->id);
xcb_change_property(conn, XCB_PROP_MODE_REPLACE, client->id,
ewmh->_NET_WM_STATE, ewmh->_NET_WM_STATE, 32, 2, data);
centerpointer(e->window,client);
updateclientlist();
if (!client->maxed)
setborders(client,true);
// always focus new window
setfocus(client);
}
/* Set border colour, width and event mask for window. */
struct client *
setupwin(xcb_window_t win)
{
unsigned int i;
uint8_t result;
uint32_t values[2],ws;
xcb_atom_t a;
xcb_size_hints_t hints;
xcb_ewmh_get_atoms_reply_t win_type;
xcb_window_t prop;
struct item *item;
struct client *client;
xcb_get_property_cookie_t cookie;
if (xcb_ewmh_get_wm_window_type_reply(ewmh,
xcb_ewmh_get_wm_window_type(ewmh, win), &win_type, NULL) == 1) {
for (i = 0; i < win_type.atoms_len; i++) {
a = win_type.atoms[i];
if (a == ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR || a
== ewmh->_NET_WM_WINDOW_TYPE_DOCK || a
== ewmh->_NET_WM_WINDOW_TYPE_DESKTOP ) {
xcb_map_window(conn,win);
dock_win = win;
return NULL;
}
}
xcb_ewmh_get_atoms_reply_wipe(&win_type);
}
values[0] = XCB_EVENT_MASK_ENTER_WINDOW;
xcb_change_window_attributes(conn, win, XCB_CW_BACK_PIXEL,
&conf.empty_col);
xcb_change_window_attributes_checked(conn, win, XCB_CW_EVENT_MASK,
values);
/* Add this window to the X Save Set. */
xcb_change_save_set(conn, XCB_SET_MODE_INSERT, win);
/* Remember window and store a few things about it. */
item = additem(&winlist);
if (NULL == item)
return NULL;
client = malloc(sizeof(struct client));
if (NULL == client)
return NULL;
item->data = client;
client->id = win;
client->x = client->y = client->width = client->height
= client->min_width = client->min_height = client->base_width
= client->base_height = 0;
client->max_width = screen->width_in_pixels;
client->max_height = screen->height_in_pixels;
client->width_inc = client->height_inc = 1;
client->usercoord = client->vertmaxed = client->hormaxed
= client->maxed = client->unkillable= client->fixed
= client->ignore_borders= client->iconic= false;
client->monitor = NULL;
client->winitem = item;
/* Initialize workspace pointers. */
client->wsitem = NULL;
client->ws = -1;
/* Get window geometry. */
getgeom(&client->id, &client->x, &client->y, &client->width,
&client->height, &client->depth);
/* Get the window's incremental size step, if any.*/
xcb_icccm_get_wm_normal_hints_reply(conn,
xcb_icccm_get_wm_normal_hints_unchecked(conn, win),
&hints, NULL
);
/* The user specified the position coordinates.
* Remember that so we can use geometry later. */
if (hints.flags &XCB_ICCCM_SIZE_HINT_US_POSITION)
client->usercoord = true;
if (hints.flags &XCB_ICCCM_SIZE_HINT_P_MIN_SIZE) {