about summary refs log tree commit diff stats
path: root/dwm.c
diff options
context:
space:
mode:
authorAnselm R Garbe <anselm@garbe.us>2009-06-27 18:39:03 +0100
committerAnselm R Garbe <anselm@garbe.us>2009-06-27 18:39:03 +0100
commit176408afa895adf133a437038466ddc74e326a0c (patch)
tree02b5350da8e7e76033217534efefed6f1d04f81d /dwm.c
parent64674c395b89f8d9640163cdcf9c8f4e25ba0e9c (diff)
downloaddwm-176408afa895adf133a437038466ddc74e326a0c.tar.gz
fixed several issues with focus handling via mouse, also added sending clients to the right monitor they belong to after mouse moves/resizals
Diffstat (limited to 'dwm.c')
-rw-r--r--dwm.c122
1 files changed, 79 insertions, 43 deletions
diff --git a/dwm.c b/dwm.c
index 096895c..9779654 100644
--- a/dwm.c
+++ b/dwm.c
@@ -166,7 +166,7 @@ static void detach(Client *c);
 static void detachstack(Client *c);
 static void die(const char *errstr, ...);
 static void drawbar(Monitor *m);
-static void drawbars();
+static void drawbars(void);
 static void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
 static void drawtext(const char *text, unsigned long col[ColLast], Bool invert);
 static void enternotify(XEvent *e);
@@ -176,6 +176,9 @@ static void focusin(XEvent *e);
 static void focusstack(const Arg *arg);
 static Client *getclient(Window w);
 static unsigned long getcolor(const char *colstr);
+static Monitor *getmonitor(Window w);
+static Monitor *getmonitorxy(int x, int y);
+static Bool getrootpointer(int *x, int *y);
 static long getstate(Window w);
 static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
 static void grabbuttons(Client *c, Bool focused);
@@ -197,6 +200,7 @@ static void resizemouse(const Arg *arg);
 static void restack(Monitor *m);
 static void run(void);
 static void scan(void);
+static void sendmon(Client *c, Monitor *m);
 static void setclientstate(Client *c, long state);
 static void setlayout(const Arg *arg);
 static void setmfact(const Arg *arg);
@@ -397,15 +401,11 @@ buttonpress(XEvent *e) {
 
 	click = ClkRootWin;
 	/* focus monitor if necessary */
-	for(m = mons; m; m = m->next)
-		if(ev->window == m->barwin) {
-			if(m != selmon) {
-				unfocus(selmon->stack);
-				selmon = m;
-				focus(NULL);
-			}
-			break;
-		}
+	if((m = getmonitor(ev->window)) && m != selmon) {
+		unfocus(selmon->sel);
+		selmon = m;
+		focus(NULL);
+	}
 	if(ev->window == selmon->barwin && ev->x >= selmon->btx) {
 		i = 0;
 		x = selmon->btx;
@@ -683,7 +683,7 @@ drawbar(Monitor *m) {
 }
 
 void
-drawbars() {
+drawbars(void) {
 	Monitor *m;
 
 	for(m = mons; m; m = m->next)
@@ -742,10 +742,15 @@ drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
 void
 enternotify(XEvent *e) {
 	Client *c;
+	Monitor *m;
 	XCrossingEvent *ev = &e->xcrossing;
 
 	if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
 		return;
+	if((m = getmonitor(ev->window)) && m != selmon) {
+		unfocus(selmon->sel);
+		selmon = m;
+	}
 	if((c = getclient(ev->window)))
 		focus(c);
 	else
@@ -757,12 +762,8 @@ expose(XEvent *e) {
 	Monitor *m;
 	XExposeEvent *ev = &e->xexpose;
 
-	if(ev->count == 0)
-		for(m = mons; m; m = m->next)
-			if(ev->window == m->barwin) {
-				drawbar(m);
-				break;
-			}
+	if(ev->count == 0 && (m = getmonitor(ev->window)))
+		drawbar(m);
 }
 
 void
@@ -809,7 +810,6 @@ focusmon(const Arg *arg) {
 			unfocus(selmon->sel);
 			selmon = m;
 			focus(NULL);
-			drawbars();
 			break;
 		}
 }
@@ -863,6 +863,40 @@ getcolor(const char *colstr) {
 	return color.pixel;
 }
 
+Monitor *
+getmonitor(Window w) {
+	int x, y;
+	Client *c;
+	Monitor *m;
+
+	if(w == root && getrootpointer(&x, &y))
+		return getmonitorxy(x, y);
+	for(m = mons; m; m = m->next)
+		if(w == m->barwin)
+			return m;
+	if((c = getclient(w)))
+		return c->mon;
+	return NULL;
+}
+
+Monitor *
+getmonitorxy(int x, int y) {
+	Monitor *m;
+
+	for(m = mons; m; m = m->next)
+		if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh))
+			return m;
+	return NULL;
+}
+
+Bool
+getrootpointer(int *x, int *y) {
+	int di;
+	unsigned int dui;
+	Window dummy;
+	return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
+}
+
 long
 getstate(Window w) {
 	int format, status;
@@ -1124,10 +1158,9 @@ monocle(Monitor *m) {
 
 void
 movemouse(const Arg *arg) {
-	int x, y, ocx, ocy, di, nx, ny;
-	unsigned int dui;
+	int x, y, ocx, ocy, nx, ny;
 	Client *c;
-	Window dummy;
+	Monitor *m;
 	XEvent ev;
 
 	if(!(c = selmon->sel))
@@ -1138,7 +1171,8 @@ movemouse(const Arg *arg) {
 	if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
 	None, cursor[CurMove], CurrentTime) != GrabSuccess)
 		return;
-	XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui);
+	if(!getrootpointer(&x, &y))
+		return;
 	do {
 		XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
 		switch (ev.type) {
@@ -1171,6 +1205,8 @@ movemouse(const Arg *arg) {
 	}
 	while(ev.type != ButtonRelease);
 	XUngrabPointer(dpy, CurrentTime);
+	if((m = getmonitorxy(c->x + c->w / 2, c->y + c->h / 2)) != selmon)
+		sendmon(c, m);
 }
 
 Client *
@@ -1239,6 +1275,7 @@ resizemouse(const Arg *arg) {
 	int ocx, ocy;
 	int nw, nh;
 	Client *c;
+	Monitor *m;
 	XEvent ev;
 
 	if(!(c = selmon->sel))
@@ -1277,6 +1314,8 @@ resizemouse(const Arg *arg) {
 	XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
 	XUngrabPointer(dpy, CurrentTime);
 	while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
+	if((m = getmonitorxy(c->x + c->w / 2, c->y + c->h / 2)) != selmon)
+		sendmon(c, m);
 }
 
 void
@@ -1342,6 +1381,20 @@ scan(void) {
 }
 
 void
+sendmon(Client *c, Monitor *m) {
+	if(c->mon == m)
+		return;
+	detach(c);
+	detachstack(c);
+	c->mon = m;
+	c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
+	attach(c);
+	attachstack(c);
+	focus(NULL);
+	arrange();
+}
+
+void
 setclientstate(Client *c, long state) {
 	long data[] = {state, None};
 
@@ -1497,14 +1550,7 @@ tagmon(const Arg *arg) {
 		return;
 	for(i = 0, m = mons; m; m = m->next, i++)
 		if(i == arg->ui) {
-			detach(c);
-			detachstack(c);
-			c->mon = m;
-			c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
-			attach(c);
-			attachstack(c);
-			focus(NULL);
-			arrange();
+			sendmon(c, m);
 			break;
 		}
 }
@@ -1676,11 +1722,9 @@ updatebarpos(Monitor *m) {
 
 void
 updategeom(void) {
-	int i, di, n = 1, x, y;
-	unsigned int dui;
+	int i, n = 1;
 	Client *c;
 	Monitor *newmons = NULL, *m, *tm;
-	Window dummy;
 
 #ifdef XINULATOR
 	n = 2;
@@ -1763,17 +1807,9 @@ updategeom(void) {
 		}
 
 	/* select focused monitor */
-	selmon = newmons;
-	if(XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui)) 
-		for(m = newmons; m; m = m->next)
-			if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh)) {
-				selmon = m;
-				break;
-			}
-
-	/* final assignment of new monitors */
 	cleanupmons();
 	mons = newmons;
+	selmon = getmonitor(root);
 }
 
 void
@@ -1848,7 +1884,7 @@ updatetitle(Client *c) {
 }
 
 void
-updatestatus() {
+updatestatus(void) {
 	if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
 		strcpy(stext, "dwm-"VERSION);
 	drawbar(selmon);
s/commit/src/LYDownload.c?id=57bfc74ff4ec4c2980b2330f2badc54a8990842d'>57bfc74f ^
fb4db89a ^


d326f24d ^
fb4db89a ^
e087f6d4

57bfc74f ^
d326f24d ^
57bfc74f ^
8c68f693 ^
2d161b7d ^


d326f24d ^
2d161b7d ^


8c68f693 ^
2d161b7d ^
86b4d41a ^
d326f24d ^
57bfc74f ^
2d161b7d ^
d3f9d547 ^
e087f6d4

fb4db89a ^
d326f24d ^

fb4db89a ^

86b4d41a ^
fb4db89a ^
a2a1ab1e ^
d3f9d547 ^
383b3f2d ^
83824f14 ^
ffc5ca31 ^





83824f14 ^
86b4d41a ^
83824f14 ^
d326f24d ^
0cb11571 ^
e087f6d4
a2a1ab1e ^
d326f24d ^
a2a1ab1e ^





e087f6d4

8c68f693 ^
e087f6d4
57bfc74f ^
d326f24d ^
57bfc74f ^
fb4db89a ^
e087f6d4
86b4d41a ^
57bfc74f ^
d326f24d ^
57bfc74f ^
e087f6d4
d1349dd6 ^
e087f6d4

e087f6d4
57bfc74f ^
d326f24d ^
57bfc74f ^
e087f6d4
d326f24d ^
e087f6d4
d326f24d ^
e087f6d4

d326f24d ^
57bfc74f ^
d326f24d ^
e087f6d4

57bfc74f ^
e087f6d4

d326f24d ^
e087f6d4



d326f24d ^
e087f6d4




387b9cbf ^
d326f24d ^
e087f6d4




d326f24d ^


e087f6d4
86b4d41a ^
e087f6d4
86b4d41a ^
e087f6d4



86b4d41a ^
e087f6d4

d326f24d ^
e087f6d4



d326f24d ^
e087f6d4




387b9cbf ^
d326f24d ^
e087f6d4




d326f24d ^


e087f6d4
86b4d41a ^
e087f6d4
86b4d41a ^
e087f6d4





d326f24d ^
e087f6d4



08fc6e5c ^

57bfc74f ^
0cb11571 ^
08fc6e5c ^






57bfc74f ^
08fc6e5c ^
e087f6d4
57bfc74f ^
d326f24d ^
57bfc74f ^
08fc6e5c ^










e087f6d4

57bfc74f ^
d326f24d ^
57bfc74f ^
95586cc2 ^
d3f9d547 ^
d326f24d ^
e087f6d4





d3f9d547 ^
e087f6d4

d326f24d ^
e087f6d4
95586cc2 ^
e087f6d4

d326f24d ^
e087f6d4
95586cc2 ^
a2e94617 ^
e087f6d4

d326f24d ^

e087f6d4


b63d287c ^
e087f6d4
d3f9d547 ^
a2e94617 ^
b63d287c ^
e087f6d4



d326f24d ^
e087f6d4

d326f24d ^
e087f6d4
d326f24d ^
e087f6d4

d326f24d ^
e087f6d4
86b4d41a ^
e087f6d4
d326f24d ^
e087f6d4
08fc6e5c ^
e087f6d4
d326f24d ^

e087f6d4

d326f24d ^
e087f6d4
86b4d41a ^

e087f6d4



d326f24d ^
e087f6d4



d326f24d ^
e087f6d4




d326f24d ^

e087f6d4




d326f24d ^



e087f6d4










d326f24d ^
e087f6d4



d326f24d ^
e087f6d4




d326f24d ^

e087f6d4




d326f24d ^



e087f6d4














86b4d41a ^
d3f9d547 ^
86b4d41a ^
e087f6d4
86b4d41a ^
e087f6d4
86b4d41a ^
57bfc74f ^
d326f24d ^

57bfc74f ^
0cb11571 ^
57bfc74f ^

e087f6d4



d326f24d ^




e087f6d4
87434eaa ^

a735f5ad ^
87434eaa ^

e087f6d4
86b4d41a ^
d3f9d547 ^
e087f6d4
86b4d41a ^
e087f6d4
95586cc2 ^
86b4d41a ^
08fc6e5c ^

86b4d41a ^

e087f6d4



86b4d41a ^
d326f24d ^

86b4d41a ^
d326f24d ^
e087f6d4










d326f24d ^
d3f9d547 ^
e087f6d4


d326f24d ^
d3f9d547 ^
e087f6d4

86b4d41a ^
e087f6d4

6bbc5d0b ^


b52ca53f ^
6bbc5d0b ^


d326f24d ^
6bbc5d0b ^



d326f24d ^

387b9cbf ^
d326f24d ^
e087f6d4
d326f24d ^
e087f6d4
83824f14 ^
d3f9d547 ^
fb4db89a ^
e087f6d4
8c68f693 ^
e087f6d4

57bfc74f ^
d326f24d ^
57bfc74f ^
e087f6d4


533c7482 ^
d326f24d ^
50f9f94b ^
d97a6513 ^
d3f9d547 ^
e087f6d4
fb4db89a ^
57bfc74f ^

d326f24d ^
51f21bae ^
d3f9d547 ^
5816641f ^
d3f9d547 ^

2d161b7d ^
d3f9d547 ^
d97a6513 ^
5d4274ca ^
d3f9d547 ^
2d161b7d ^
d3f9d547 ^
5d4274ca ^
d3f9d547 ^




387b9cbf ^
a2a1ab1e ^
57bfc74f ^
d326f24d ^
57bfc74f ^
86b4d41a ^
e087f6d4
6bbc5d0b ^












d326f24d ^
50f9f94b ^



6bbc5d0b ^
50f9f94b ^

6bbc5d0b ^
50f9f94b ^


6bbc5d0b ^

d3f9d547 ^





e087f6d4
57bfc74f ^
86b4d41a ^
d326f24d ^
57bfc74f ^
d326f24d ^






e087f6d4

e087f6d4
d3f9d547 ^



f61041c1 ^
e087f6d4
57bfc74f ^
d326f24d ^
57bfc74f ^
e087f6d4

d326f24d ^
e087f6d4
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