-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmcwm.c
4513 lines (3844 loc) · 116 KB
/
mcwm.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
/*
* mcwm, a small window manager for the X Window System using the X
* protocol C Binding libraries.
*
* For 'user' configurable stuff, see config.h.
*
* MC, mc at the domain hack.org
* http://hack.org/mc/
*
* Copyright (c) 2010, 2011, 2012, 2013, 2014 Michael Cardell
* Widerkrantz, mc at the domain hack.org.
*
* Permission to use, copy, modify, and/or 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 <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#include <getopt.h>
#include <string.h>
#include <signal.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/select.h>
#include <xcb/xcb.h>
#include <xcb/randr.h>
#include <xcb/xcb_keysyms.h>
#include <xcb/xcb_atom.h>
#include <xcb/xcb_icccm.h>
#include <X11/keysym.h>
#include <xcb/xproto.h>
#include <xcb/xcb_util.h>
#ifdef DEBUG
#include "events.h"
#endif
#include "list.h"
/* Check here for user configurable parts: */
#include "config.h"
#ifdef DMALLOC
#include "dmalloc.h"
#endif
#ifdef DEBUG
#define PDEBUG(Args...) \
do { fprintf(stderr, "mcwm: "); fprintf(stderr, ##Args); } while(0)
#define D(x) x
#else
#define PDEBUG(Args...)
#define D(x)
#endif
/* Internal Constants. */
/* We're currently moving a window with the mouse. */
#define MCWM_MOVE 2
/* We're currently resizing a window with the mouse. */
#define MCWM_RESIZE 3
/*
* We're currently tabbing around the window list, looking for a new
* window to focus on.
*/
#define MCWM_TABBING 4
/* Number of workspaces. */
#define WORKSPACES 10
/* Value in WM hint which means this window is fixed on all workspaces. */
#define NET_WM_FIXED 0xffffffff
/* This means we didn't get any window hint at all. */
#define MCWM_NOWS 0xfffffffe
/* Types. */
/* All our key shortcuts. */
typedef enum {
KEY_F,
KEY_H,
KEY_J,
KEY_K,
KEY_L,
KEY_M,
KEY_R,
KEY_RET,
KEY_X,
KEY_TAB,
KEY_BACKTAB,
KEY_1,
KEY_2,
KEY_3,
KEY_4,
KEY_5,
KEY_6,
KEY_7,
KEY_8,
KEY_9,
KEY_0,
KEY_Y,
KEY_U,
KEY_B,
KEY_N,
KEY_END,
KEY_PREVSCR,
KEY_NEXTSCR,
KEY_ICONIFY,
KEY_PREVWS,
KEY_NEXTWS,
KEY_MAX
} key_enum_t;
struct monitor
{
xcb_randr_output_t id;
char *name;
int16_t x; /* X and Y. */
int16_t y;
uint16_t width; /* Width in pixels. */
uint16_t height; /* Height in pixels. */
struct item *item; /* Pointer to our place in output list. */
};
struct sizepos
{
int16_t x;
int16_t y;
uint16_t width;
uint16_t height;
};
/* Everything we know about a window. */
struct client
{
xcb_drawable_t id; /* ID of this window. */
bool usercoord; /* X,Y was set by -geom. */
int16_t x; /* X coordinate. */
int16_t y; /* Y coordinate. */
uint16_t width; /* Width in pixels. */
uint16_t height; /* Height in pixels. */
struct sizepos origsize; /* Original size if we're currently maxed. */
uint16_t min_width, min_height; /* Hints from application. */
uint16_t max_width, max_height;
int32_t width_inc, height_inc;
int32_t base_width, base_height;
bool vertmaxed; /* Vertically maximized? */
bool maxed; /* Totally maximized? */
bool fixed; /* Visible on all workspaces? */
struct monitor *monitor; /* The physical output this window is on. */
struct item *winitem; /* Pointer to our place in global windows list. */
struct item *wsitem[WORKSPACES]; /* Pointer to our place in every
* workspace window list. */
};
/* Window configuration data. */
struct winconf
{
int16_t x;
int16_t y;
uint16_t width;
uint16_t height;
uint8_t stackmode;
xcb_window_t sibling;
uint16_t borderwidth;
};
/* Globals */
int sigcode; /* Signal code. Non-zero if we've been
* interruped by a signal. */
xcb_connection_t *conn; /* Connection to X server. */
xcb_screen_t *screen; /* Our current screen. */
int randrbase; /* Beginning of RANDR extension events. */
uint32_t curws = 0; /* Current workspace. */
struct client *focuswin; /* Current focus window. */
struct client *lastfocuswin; /* Last focused window. NOTE! Only
* used to communicate between
* start and end of tabbing
* mode. */
struct item *winlist = NULL; /* Global list of all client windows. */
struct item *monlist = NULL; /* List of all physical monitor outputs. */
int mode = 0; /* Internal mode, such as move or resize */
/*
* Workspace list: Every workspace has a list of all visible
* windows.
*/
struct item *wslist[WORKSPACES] =
{
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
/* Shortcut key type and initializiation. */
struct keys
{
xcb_keysym_t keysym;
xcb_keycode_t keycode;
} keys[KEY_MAX] =
{
{ USERKEY_FIX, 0 },
{ USERKEY_MOVE_LEFT, 0 },
{ USERKEY_MOVE_DOWN, 0 },
{ USERKEY_MOVE_UP, 0 },
{ USERKEY_MOVE_RIGHT, 0 },
{ USERKEY_MAXVERT, 0 },
{ USERKEY_RAISE, 0 },
{ USERKEY_TERMINAL, 0 },
{ USERKEY_MAX, 0 },
{ USERKEY_CHANGE, 0 },
{ USERKEY_BACKCHANGE, 0 },
{ USERKEY_WS1, 0 },
{ USERKEY_WS2, 0 },
{ USERKEY_WS3, 0 },
{ USERKEY_WS4, 0 },
{ USERKEY_WS5, 0 },
{ USERKEY_WS6, 0 },
{ USERKEY_WS7, 0 },
{ USERKEY_WS8, 0 },
{ USERKEY_WS9, 0 },
{ USERKEY_WS10, 0 },
{ USERKEY_TOPLEFT, 0 },
{ USERKEY_TOPRIGHT, 0 },
{ USERKEY_BOTLEFT, 0 },
{ USERKEY_BOTRIGHT, 0 },
{ USERKEY_DELETE, 0 },
{ USERKEY_PREVSCREEN, 0 },
{ USERKEY_NEXTSCREEN, 0 },
{ USERKEY_ICONIFY, 0 },
{ USERKEY_PREVWS, 0 },
{ USERKEY_NEXTWS, 0 },
};
/* All keycodes generating our MODKEY mask. */
struct modkeycodes
{
xcb_keycode_t *keycodes;
unsigned len;
} modkeys =
{
NULL,
0
};
/* Global configuration. */
struct conf
{
int borderwidth; /* Do we draw borders? If so, how large? */
int snapmargin; /* Do we have snap margin? If so, how large? */
char *terminal; /* Path to terminal to start. */
uint32_t focuscol; /* Focused border colour. */
uint32_t unfocuscol; /* Unfocused border colour. */
uint32_t fixedcol; /* Fixed windows border colour. */
bool allowicons; /* Allow windows to be unmapped. */
} conf;
xcb_atom_t atom_desktop; /*
* EWMH _NET_WM_DESKTOP hint that says
* what workspace a window should be
* on.
*/
xcb_atom_t wm_delete_window; /* WM_DELETE_WINDOW event to close windows. */
xcb_atom_t wm_change_state;
xcb_atom_t wm_state;
xcb_atom_t wm_protocols; /* WM_PROTOCOLS. */
/* Functions declerations. */
static void finishtabbing(void);
static struct modkeycodes getmodkeys(xcb_mod_mask_t modmask);
static void cleanup(int code);
static void arrangewindows(void);
static void setwmdesktop(xcb_drawable_t win, uint32_t ws);
static int32_t getwmdesktop(xcb_drawable_t win);
static void addtoworkspace(struct client *client, uint32_t ws);
static void delfromworkspace(struct client *client, uint32_t ws);
static void changeworkspace(uint32_t ws);
static void fixwindow(struct client *client, bool setcolour);
static uint32_t getcolor(const char *colstr);
static void forgetclient(struct client *client);
static void forgetwin(xcb_window_t win);
static void fitonscreen(struct client *client);
static void newwin(xcb_window_t win);
static struct client *setupwin(xcb_window_t win);
static xcb_keycode_t keysymtokeycode(xcb_keysym_t keysym,
xcb_key_symbols_t *keysyms);
static int setupkeys(void);
static int setupscreen(void);
static int setuprandr(void);
static void getrandr(void);
static void getoutputs(xcb_randr_output_t *outputs, int len,
xcb_timestamp_t timestamp);
void arrbymon(struct monitor *monitor);
static struct monitor *findmonitor(xcb_randr_output_t id);
static struct monitor *findclones(xcb_randr_output_t id, int16_t x, int16_t y);
static struct monitor *findmonbycoord(int16_t x, int16_t y);
static void delmonitor(struct monitor *mon);
static struct monitor *addmonitor(xcb_randr_output_t id, char *name,
uint32_t x, uint32_t y, uint16_t width,
uint16_t height);
static void raisewindow(xcb_drawable_t win);
static void raiseorlower(struct client *client);
static void movelim(struct client *client);
static void movewindow(xcb_drawable_t win, uint16_t x, uint16_t y);
static struct client *findclient(xcb_drawable_t win);
static void focusnext(bool reverse);
static void setunfocus(xcb_drawable_t win);
static void setfocus(struct client *client);
static int start(char *program);
static void resizelim(struct client *client);
static void moveresize(xcb_drawable_t win, uint16_t x, uint16_t y,
uint16_t width, uint16_t height);
static void resize(xcb_drawable_t win, uint16_t width, uint16_t height);
static void resizestep(struct client *client, char direction);
static void snapwindow(struct client *client, int snap_mode);
static void mousemove(struct client *client, int rel_x, int rel_y);
static void mouseresize(struct client *client, int rel_x, int rel_y);
static void movestep(struct client *client, char direction);
static void setborders(struct client *client, int width);
static void unmax(struct client *client);
static void maximize(struct client *client);
static void maxvert(struct client *client);
static void hide(struct client *client);
static bool getpointer(xcb_drawable_t win, int16_t *x, int16_t *y);
static bool getgeom(xcb_drawable_t win, int16_t *x, int16_t *y, uint16_t *width,
uint16_t *height);
static void topleft(void);
static void topright(void);
static void botleft(void);
static void botright(void);
static void deletewin(void);
static void prevscreen(void);
static void nextscreen(void);
static void handle_keypress(xcb_key_press_event_t *ev);
static void configwin(xcb_window_t win, uint16_t mask, struct winconf wc);
static void configurerequest(xcb_configure_request_event_t *e);
static void events(void);
static void printhelp(void);
static void sigcatch(int sig);
static xcb_atom_t getatom(char *atom_name);
/* Function bodies. */
/*
* MODKEY was released after tabbing around the
* workspace window ring. This means this mode is
* finished and we have found a new focus window.
*
* We need to move first the window we used to focus
* on to the head of the window list and then move the
* new focus to the head of the list as well. The list
* should always start with the window we're focusing
* on.
*/
void finishtabbing(void)
{
mode = 0;
if (NULL != lastfocuswin)
{
movetohead(&wslist[curws], lastfocuswin->wsitem[curws]);
lastfocuswin = NULL;
}
movetohead(&wslist[curws], focuswin->wsitem[curws]);
}
/*
* Find out what keycode modmask is bound to. Returns a struct. If the
* len in the struct is 0 something went wrong.
*/
struct modkeycodes getmodkeys(xcb_mod_mask_t modmask)
{
xcb_get_modifier_mapping_cookie_t cookie;
xcb_get_modifier_mapping_reply_t *reply;
xcb_keycode_t *modmap;
struct modkeycodes keycodes = {
NULL,
0
};
int mask;
unsigned i;
const xcb_mod_mask_t masks[8] = { XCB_MOD_MASK_SHIFT,
XCB_MOD_MASK_LOCK,
XCB_MOD_MASK_CONTROL,
XCB_MOD_MASK_1,
XCB_MOD_MASK_2,
XCB_MOD_MASK_3,
XCB_MOD_MASK_4,
XCB_MOD_MASK_5 };
cookie = xcb_get_modifier_mapping_unchecked(conn);
if ((reply = xcb_get_modifier_mapping_reply(conn, cookie, NULL)) == NULL)
{
return keycodes;
}
if (NULL == (keycodes.keycodes = calloc(reply->keycodes_per_modifier,
sizeof (xcb_keycode_t))))
{
PDEBUG("Out of memory.\n");
return keycodes;
}
modmap = xcb_get_modifier_mapping_keycodes(reply);
/*
* The modmap now contains keycodes.
*
* The number of keycodes in the list is 8 *
* keycodes_per_modifier. The keycodes are divided into eight
* sets, with each set containing keycodes_per_modifier elements.
*
* Each set corresponds to a modifier in masks[] in the order
* specified above.
*
* The keycodes_per_modifier value is chosen arbitrarily by the
* server. Zeroes are used to fill in unused elements within each
* set.
*/
for (mask = 0; mask < 8; mask ++)
{
if (masks[mask] == modmask)
{
for (i = 0; i < reply->keycodes_per_modifier; i ++)
{
if (0 != modmap[mask * reply->keycodes_per_modifier + i])
{
keycodes.keycodes[i]
= modmap[mask * reply->keycodes_per_modifier + i];
keycodes.len ++;
}
}
PDEBUG("Got %d keycodes.\n", keycodes.len);
}
}
free(reply);
return keycodes;
}
/*
* 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(int code)
{
xcb_set_input_focus(conn, XCB_NONE,
XCB_INPUT_FOCUS_POINTER_ROOT,
XCB_CURRENT_TIME);
xcb_flush(conn);
xcb_disconnect(conn);
exit(code);
}
/*
* Rearrange windows to fit new screen size.
*/
void arrangewindows(void)
{
struct item *item;
struct client *client;
/*
* Go through all windows. If they don't fit on the new screen,
* move them around and resize them as necessary.
*/
for (item = winlist; item != NULL; item = item->next)
{
client = item->data;
fitonscreen(client);
}
}
/* Set the EWMH hint that window win belongs on workspace ws. */
void setwmdesktop(xcb_drawable_t win, uint32_t ws)
{
PDEBUG("Changing _NET_WM_DESKTOP on window %d to %d\n", win, ws);
xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
atom_desktop, XCB_ATOM_CARDINAL, 32, 1,
&ws);
}
/*
* 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 MCWM_NOWS if we didn't find any hints.
*/
int32_t getwmdesktop(xcb_drawable_t win)
{
xcb_get_property_reply_t *reply;
xcb_get_property_cookie_t cookie;
uint32_t *wsp;
uint32_t ws;
cookie = xcb_get_property(conn, false, win, atom_desktop,
XCB_GET_PROPERTY_TYPE_ANY, 0,
sizeof (int32_t));
reply = xcb_get_property_reply(conn, cookie, NULL);
if (NULL == reply)
{
fprintf(stderr, "mcwm: Couldn't get properties for win %d\n", win);
return MCWM_NOWS;
}
/* Length is 0 if we didn't find it. */
if (0 == xcb_get_property_value_length(reply))
{
PDEBUG("_NET_WM_DESKTOP reply was 0 length.\n");
goto bad;
}
wsp = xcb_get_property_value(reply);
ws = *wsp;
PDEBUG("got _NET_WM_DESKTOP: %d stored at %p.\n", ws, (void *)wsp);
free(reply);
return ws;
bad:
free(reply);
return MCWM_NOWS;
}
/* Add a window, specified by client, to workspace ws. */
void addtoworkspace(struct client *client, uint32_t ws)
{
struct item *item;
item = additem(&wslist[ws]);
if (NULL == item)
{
PDEBUG("addtoworkspace: Out of memory.\n");
return;
}
/* Remember our place in the workspace window list. */
client->wsitem[ws] = item;
/* Remember the data. */
item->data = client;
/*
* Set window hint property so we can survive a crash.
*
* Fixed windows have their own special WM hint. We don't want to
* mess with that.
*/
if (!client->fixed)
{
setwmdesktop(client->id, ws);
}
}
/* Delete window client from workspace ws. */
void delfromworkspace(struct client *client, uint32_t ws)
{
delitem(&wslist[ws], client->wsitem[ws]);
/* Reset our place in the workspace window list. */
client->wsitem[ws] = NULL;
}
/* Change current workspace to ws. */
void changeworkspace(uint32_t ws)
{
struct item *item;
struct client *client;
if (ws == curws)
{
PDEBUG("Changing to same workspace!\n");
return;
}
PDEBUG("Changing from workspace #%d to #%d\n", curws, ws);
/*
* We lose our focus if the window we focus isn't fixed. An
* EnterNotify event will set focus later.
*/
if (NULL != focuswin && !focuswin->fixed)
{
setunfocus(focuswin->id);
focuswin = NULL;
}
/* Go through list of current ws. Unmap everything that isn't fixed. */
for (item = wslist[curws]; item != NULL; item = item->next)
{
client = item->data;
PDEBUG("changeworkspace. unmap phase. ws #%d, client-fixed: %d\n",
curws, client->fixed);
if (!client->fixed)
{
/*
* This is an ordinary window. Just unmap it. Note that
* this will generate an unnecessary UnmapNotify event
* which we will try to handle later.
*/
xcb_unmap_window(conn, client->id);
}
}
/* Go through list of new ws. Map everything that isn't fixed. */
for (item = wslist[ws]; item != NULL; item = item->next)
{
client = item->data;
PDEBUG("changeworkspace. map phase. ws #%d, client-fixed: %d\n",
ws, client->fixed);
/* Fixed windows are already mapped. Map everything else. */
if (!client->fixed)
{
xcb_map_window(conn, client->id);
}
}
xcb_flush(conn);
curws = ws;
}
/*
* Fix or unfix a window client from all workspaces. If setcolour is
* set, also change back to ordinary focus colour when unfixing.
*/
void fixwindow(struct client *client, bool setcolour)
{
uint32_t values[1];
uint32_t ws;
if (NULL == client)
{
return;
}
if (client->fixed)
{
client->fixed = false;
setwmdesktop(client->id, curws);
if (setcolour)
{
/* Set border color to ordinary focus colour. */
values[0] = conf.focuscol;
xcb_change_window_attributes(conn, client->id, XCB_CW_BORDER_PIXEL,
values);
}
/* Delete from all workspace lists except current. */
for (ws = 0; ws < WORKSPACES; ws ++)
{
if (ws != curws)
{
delfromworkspace(client, ws);
}
}
}
else
{
/*
* First raise the window. If we're going to another desktop
* we don't want this fixed window to be occluded behind
* something else.
*/
raisewindow(client->id);
client->fixed = true;
setwmdesktop(client->id, NET_WM_FIXED);
/* Add window to all workspace lists. */
for (ws = 0; ws < WORKSPACES; ws ++)
{
if (ws != curws)
{
addtoworkspace(client, ws);
}
}
if (setcolour)
{
/* Set border color to fixed colour. */
values[0] = conf.fixedcol;
xcb_change_window_attributes(conn, client->id, XCB_CW_BORDER_PIXEL,
values);
}
}
xcb_flush(conn);
}
/*
* Get the pixel values of a named colour colstr.
*
* Returns pixel values.
* */
uint32_t getcolor(const char *colstr)
{
xcb_alloc_named_color_reply_t *col_reply;
xcb_colormap_t colormap;
xcb_generic_error_t *error;
xcb_alloc_named_color_cookie_t colcookie;
colormap = screen->default_colormap;
colcookie = xcb_alloc_named_color(conn, colormap, strlen(colstr), colstr);
col_reply = xcb_alloc_named_color_reply(conn, colcookie, &error);
if (NULL != error)
{
fprintf(stderr, "mcwm: Couldn't get pixel value for colour %s. "
"Exiting.\n", colstr);
xcb_disconnect(conn);
exit(1);
}
return col_reply->pixel;
}
/* Forget everything about client client. */
void forgetclient(struct client *client)
{
uint32_t ws;
if (NULL == client)
{
PDEBUG("forgetclient: client was NULL\n");
return;
}
/*
* Delete this client from whatever workspace lists it belongs to.
* Note that it's OK to be on several workspaces at once even if
* you're not fixed.
*/
for (ws = 0; ws < WORKSPACES; ws ++)
{
if (NULL != client->wsitem[ws])
{
delfromworkspace(client, ws);
}
}
/* 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 item *item;
struct client *client;
uint32_t ws;
/* Find this window in the global window list. */
for (item = winlist; item != NULL; item = item->next)
{
client = item->data;
/*
* Forget about it completely and free allocated data.
*
* Note that it might already be freed by handling an
* UnmapNotify, so it isn't necessarily an error if we don't
* find it.
*/
PDEBUG("Win %d == client ID %d\n", win, client->id);
if (win == client->id)
{
/* Found it. */
PDEBUG("Found it. Forgetting...\n");
/*
* Delete window from whatever workspace lists it belonged
* to. Note that it's OK to be on several workspaces at
* once.
*/
for (ws = 0; ws < WORKSPACES; ws ++)
{
PDEBUG("Looking in ws #%d.\n", ws);
if (NULL == client->wsitem[ws])
{
PDEBUG(" but it wasn't there.\n");
}
else
{
PDEBUG(" found it here. Deleting!\n");
delfromworkspace(client, ws);
}
}
free(item->data);
delitem(&winlist, item);
return;
}
}
}
/*
* Fit client on physical screen, moving and resizing as necessary.
*/
void fitonscreen(struct client *client)
{
int16_t mon_x;
int16_t mon_y;
uint16_t mon_width;
uint16_t mon_height;
bool willmove = false;
bool willresize = false;
client->vertmaxed = false;
if (client->maxed)
{
client->maxed = false;
setborders(client, conf.borderwidth);
}
if (NULL == client->monitor)
{
/*
* This window isn't attached to any physical monitor. This
* probably means there is no RANDR, so we use the root window
* size.
*/
mon_x = 0;
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;
}
PDEBUG("Is window outside monitor?\n");
PDEBUG("x: %d between %d and %d?\n", client->x, mon_x, mon_x + mon_width);
PDEBUG("y: %d between %d and %d?\n", client->y, mon_y, mon_y + mon_height);
/* Is it outside the physical monitor? */
if (client->x > mon_x + mon_width)
{
client->x = mon_x + mon_width - client->width;
willmove = true;
}
if (client->y > mon_y + mon_height)
{
client->y = mon_y + mon_height - client->height;
willmove = true;
}
if (client->x < mon_x)
{
client->x = mon_x;
willmove = true;
}
if (client->y < mon_y)
{
client->y = mon_y;
willmove = true;
}
/* 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;
}
/*
* 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 = true;
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 = true;
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)
{
PDEBUG("Moving to %d,%d.\n", client->x, client->y);
movewindow(client->id, client->x, client->y);
}
if (willresize)
{
PDEBUG("Resizing to %d x %d.\n", client->width, client->height);
resize(client->id, client->width, client->height);
}
}
/*
* Set position, geometry and attributes of a new window and show it
* on the screen.
*/
void newwin(xcb_window_t win)
{
struct client *client;
if (NULL != findclient(win))
{
/*
* We know this window from before. It's trying to map itself
* on the current workspace, but since it's unmapped it
* probably belongs on another workspace. We don't like that.
* Silently ignore.
*/
return;
}
/*
* Set up stuff, like borders, add the window to the client list,
* et cetera.
*/
client = setupwin(win);