all repos — ryudo @ db9b136766e5a3b24ca87a9a2f3810c668ba4bf9

the floatiling window manager that flows; fork of rio from plan9port

event.c (raw)

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
/*
 * Copyright (c) 2019 Derek Stevens, 2005 Rus Cox, 1994-1996 David Hogan
 * see README for licence details
 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <X11/X.h>
#include <X11/Xos.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/extensions/shape.h>
#include <X11/extensions/Xrandr.h>
#include "config.h"
#include "dat.h"
#include "fns.h"
#include "patchlevel.h"

#ifndef MICROSLEEP_DELAY
#define MICROSLEEP_DELAY 100000
#endif

void mainloop(int shape_event) {
  static XEvent ev;
  static Client* c;
  static int monitor;

  for (;;) {
    getevent(&ev);
#ifdef DEBUG_EV
    if (debug) {
      ShowEvent(&ev);
      printf("\n");
    }
#endif
    switch (ev.type) {
      default:
#ifdef SHAPE
        if (shape && ev.type == shape_event)
          shapenotify((XShapeEvent*)&ev);
        else
#endif
          fprintf(stderr, "ryudo: unknown ev.type %d\n", ev.type);
        break;
      /* XRANDR related events; idk what the define for this is */
      case 89:
        if (XRRUpdateConfiguration(&ev)) {
          fetchmonitorinfo();
          monitor = getmonitorbyclient(c);
          if (monitor < 0 || monitor >= nmonitors) {
            break;
          }
          for (c = clients; c; c = c->next) {
            monitor = getmonitorbyclient(c);
            wrangle(c, monitorinfo[monitor]);
          }
        }
        break;
      case KeyPress:
        keypress(&ev.xkey);
        break;
      case KeyRelease:
        keyrelease(&ev.xkey);
        break;
      case ButtonPress:
        button(&ev.xbutton);
        XAllowEvents(dpy, ReplayPointer, ev.xbutton.time);
        break;
      case ButtonRelease:
        XAllowEvents(dpy, ReplayPointer, ev.xbutton.time);
        break;
      case MapRequest:
        mapreq(&ev.xmaprequest);
        break;
      case ConfigureRequest:
        configurereq(&ev.xconfigurerequest);
        break;
      case CirculateRequest:
        circulatereq(&ev.xcirculaterequest);
        break;
      case UnmapNotify:
        unmap(&ev.xunmap);
        break;
      case CreateNotify:
        newwindow(&ev.xcreatewindow);
        break;
      case DestroyNotify:
        destroy(ev.xdestroywindow.window);
        break;
      case ClientMessage:
        clientmesg(&ev.xclient);
        break;
      case ColormapNotify:
        cmap(&ev.xcolormap);
        break;
      case PropertyNotify:
        property(&ev.xproperty);
        break;
      case SelectionClear:
        fprintf(stderr, "ryudo: SelectionClear (this should not happen)\n");
        break;
      case SelectionNotify:
        fprintf(stderr, "ryudo: SelectionNotify (this should not happen)\n");
        break;
      case SelectionRequest:
        fprintf(stderr, "ryudo: SelectionRequest (this should not happen)\n");
        break;
      case EnterNotify:
        enter(&ev.xcrossing);
        break;
      case LeaveNotify:
        leave(&ev.xcrossing);
        break;
      case ReparentNotify:
        reparent(&ev.xreparent);
        break;
      case FocusIn:
        focusin(&ev.xfocus);
        break;
      case MotionNotify:
        motionnotify(&ev.xmotion);
        break;
      case Expose:
      case NoExpose:
      case FocusOut:
      case ConfigureNotify:
      case MapNotify:
      case MappingNotify:
      case GraphicsExpose:
        /* not interested */
        trace("ignore", 0, &ev);
        break;
    }
  }
}

void configurereq(XConfigureRequestEvent* e) {
  XWindowChanges wc;
  Client* c;
  XRRMonitorInfo monitor;
  int m;
  /* we don't set curtime as nothing here uses it */
  c = getclient(e->window, 0);
  trace("configurereq", c, e);

  e->value_mask &= ~CWSibling;

  if (c) {
    m = getmonitorbyclient(c);
    monitor = monitorinfo[m];

    if (e->value_mask & CWX)
      c->x = e->x;
    if (e->value_mask & CWY)
      c->y = e->y;
    if (e->value_mask & CWWidth)
      c->dx = e->width;
    if (e->value_mask & CWHeight)
      c->dy = e->height;
    if (
      e->value_mask & CWBorderWidth && e->width >= monitor.width &&
      e->height >= monitor.height)
      c->border = 0;
    else
      c->border = BORDER;

    if (e->value_mask & CWStackMode) {
      if (e->detail == Above)
        top(c);
      else
        e->value_mask &= ~CWStackMode;
    }
    e->value_mask |= CWX | CWY | CWHeight | CWWidth;

    if (c->parent != c->screen->root && c->window == e->window) {
      wc.x = c->x - c->border;
      wc.y = c->y - c->border;
      wc.width = c->dx + c->border + c->border;
      wc.height = c->dy + c->border + c->border;
      wc.sibling = None;
      wc.border_width = c->border;
      wc.stack_mode = e->detail;
      XConfigureWindow(dpy, c->parent, e->value_mask, &wc);

      if (e->value_mask & CWStackMode) {
        if (c->virt > 0 && c->virt != virt)
          switch_to(c->virt);
        if (hidden(c)) {
          unhidec(c, 1);
        }
        top(c);
        active(c);
      }
    }
  }

  if (c && c->parent != c->screen->root) {
    wc.x = c->border;
    wc.y = c->border;
  } else {
    wc.x = c->x;
    wc.y = c->y;
  }
  wc.width = c->dx;
  wc.height = c->dy;
  wc.border_width = 0;
  wc.sibling = None;
  wc.stack_mode = Above;
  e->value_mask &= ~CWStackMode;
  e->value_mask |= CWBorderWidth;
  XConfigureWindow(dpy, c->window, e->value_mask, &wc);
}

void mapreq(XMapRequestEvent* e) {
  Client* c;
  int i;
  XRRMonitorInfo monitor;

  monitor = monitorinfo[getmonitorbymouse()];

  curtime = CurrentTime;
  c = getclient(e->window, 0);
  trace("mapreq", c, e);

  if (c == 0 || c->window != e->window) {
    /* workaround for stupid NCDware */
    fprintf(
      stderr,
      "ryudo: bad mapreq c %p w %x, rescanning\n",
      (void*)c,
      (int)e->window);
    for (i = 0; i < num_screens; i++)
      scanwins(&screens[i]);
    c = getclient(e->window, 0);
    if (c == 0 || c->window != e->window) {
      fprintf(stderr, "ryudo: window not found after rescan\n");
      return;
    }
  }

  if (kbLaunch) {
    usleep(MICROSLEEP_DELAY);
#ifdef CENTERVMAX
    centerclient(c, monitor, 1);
#else
    centerclient(c, monitor, 0);
#endif
    kbLaunch = 0;
  }
  switch (c->state) {
    case WithdrawnState:
      if (c->parent == c->screen->root) {
        if (!manage(c, 0))
          return;
        break;
      }
      XReparentWindow(dpy, c->window, c->parent, BORDER, BORDER);
      XAddToSaveSet(dpy, c->window);
      /* fall through... */
    case NormalState:
      XMapWindow(dpy, c->window);
      XMapRaised(dpy, c->parent);
      top(c);
      setstate(c, NormalState);
      if (c->trans != None && current && c->trans == current->window)
        active(c);
      break;
    case IconicState:
      unhidec(c, 1);
      break;
  }
}

void unmap(XUnmapEvent* e) {
  Client *c, *revertc;
  int m;

  curtime = CurrentTime;
  c = getclient(e->window, 0);

  if (c) {
    m = getmonitorbyclient(c);
    switch (c->state) {
      case IconicState:
        if (e->send_event) {
          unhidec(c, 0);
          withdraw(c);
        }
        break;
      case NormalState:
        if (c == current) {
          nofocus();
          if (revertc = getclient(getrevert(c), 0)) {
            top(revertc);
            active(revertc);
          } else {
            shuffleonmonitor(m);
          }
        }
        if (!c->reparenting)
          withdraw(c);
        break;
    }
    c->reparenting = 0;
  }
}

void circulatereq(XCirculateRequestEvent* e) {
  fprintf(stderr, "It must be the warlock Krill!\n"); /* ☺ */
}

void newwindow(XCreateWindowEvent* e) {
  Client* c;
  ScreenInfo* s;

  /* we don't set curtime as nothing here uses it */
  if (e->override_redirect)
    return;
  c = getclient(e->window, 1);
  if (c && c->window == e->window && (s = getscreen(e->parent))) {
    c->x = e->x;
    c->y = e->y;
    c->dx = e->width;
    c->dy = e->height;
    c->border = e->border_width;
    c->screen = s;
    if (c->parent == None)
      c->parent = c->screen->root;
  }
#if defined(OPACITY) && defined(TRANSPARENTLIST)
  if (istransparent(c)) {
    /* We need to set the atom on both the window and its parent */
    const Atom alpha_atom = XInternAtom(dpy, "_NET_WM_WINDOW_OPACITY", 0);
    unsigned long opacity = (unsigned long)OPACITY * 0x1010101;
    XChangeProperty(
      dpy,
      c->window,
      alpha_atom,
      XA_CARDINAL,
      32,
      PropModeReplace,
      (unsigned char*)&opacity,
      1);
    XChangeProperty(
      dpy,
      c->parent,
      alpha_atom,
      XA_CARDINAL,
      32,
      PropModeReplace,
      (unsigned char*)&opacity,
      1);
  }
#endif
}

void destroy(Window w) {
  int i;
  Client *c, *revertc;
  Window revert;

  curtime = CurrentTime;
  c = getclient(w, 0);
  if (c == 0)
    return;

  revert = getrevert(c);

  if (numvirtuals > 1)
    for (i = 0; i < numvirtuals; i++)
      if (currents[i] == c)
        currents[i] = 0;

  rmclient(c);

  if (!current && (revertc = getclient(revert, 0))) {
    top(revertc);
    active(revertc);
  }

  /* flush any errors generated by the window's sudden demise */
  ignore_badwindow = 1;
  XSync(dpy, False);
  ignore_badwindow = 0;
}

void clientmesg(XClientMessageEvent* e) {
  Client* c;

  curtime = CurrentTime;
  if (e->message_type == exit_rio) {
    cleanup();
    exit(0);
  }
  if (e->message_type == restart_rio) {
    fprintf(stderr, "*** ryudo restarting ***\n");
    cleanup();
    execvp(myargv[0], myargv);
    perror("ryudo: exec failed");
    exit(1);
  }

  if (e->message_type == wm_protocols) {
    return;
  }
  if (e->message_type == wm_change_state) {
    c = getclient(e->window, 0);
    if (e->format == 32 && e->data.l[0] == IconicState && c != 0) {
      if (normal(c))
        hide(c);
    } else
      fprintf(
        stderr,
        "ryudo: WM_CHANGE_STATE: format %d data %d w 0x%x\n",
        (int)e->format,
        (int)e->data.l[0],
        (int)e->window);
    return;
  }
  if (e->message_type == wm_state) {
    //		c = getclient(e->window, 0);
    //		if(e->format == 32 && e->data.l[1] == wm_state_fullscreen){
    //		}else
    fprintf(
      stderr,
      "ryudo: WM_STATE: format %d data %d %d w 0x%x\n",
      (int)e->format,
      (int)e->data.l[0],
      (int)e->data.l[1],
      (int)e->window);
    return;
  }
  fprintf(
    stderr,
    "ryudo: strange ClientMessage, type 0x%x window 0x%x\n",
    (int)e->message_type,
    (int)e->window);
}

void cmap(XColormapEvent* e) {
  Client* c;
  int i;

  /* we don't set curtime as nothing here uses it */
  if (e->new) {
    c = getclient(e->window, 0);
    if (c) {
      c->cmap = e->colormap;
      if (c == current)
        cmapfocus(c);
    } else
      for (c = clients; c; c = c->next) {
        for (i = 0; i < c->ncmapwins; i++)
          if (c->cmapwins[i] == e->window) {
            c->wmcmaps[i] = e->colormap;
            if (c == current)
              cmapfocus(c);
            return;
          }
      }
  }
}

void property(XPropertyEvent* e) {
  Atom a;
  int delete;
  Client* c;
  long msize;

  /* we don't set curtime as nothing here uses it */
  a = e->atom;
  delete = (e->state == PropertyDelete);
  c = getclient(e->window, 0);
  if (c == 0)
    return;

  switch (a) {
    case XA_WM_ICON_NAME:
      if (c->iconname != 0)
        XFree((char*)c->iconname);
      c->iconname = delete ? 0 : getprop(c->window, a);
      setlabel(c);
      renamec(c, c->label);
      return;
    case XA_WM_NAME:
      if (c->name != 0)
        XFree((char*)c->name);
      c->name = delete ? 0 : getprop(c->window, a);
      setlabel(c);
      renamec(c, c->label);
      return;
    case XA_WM_TRANSIENT_FOR:
      gettrans(c);
      return;
    case XA_WM_HINTS:
    case XA_WM_SIZE_HINTS:
    case XA_WM_ZOOM_HINTS:
      /* placeholders to not forget.  ignore for now.  -Axel */
      return;
    case XA_WM_NORMAL_HINTS:
      if (
        XGetWMNormalHints(dpy, c->window, &c->size, &msize) == 0 ||
        c->size.flags == 0)
        c->size.flags = PSize; /* not specified - punt */
      return;
  }
  if (a == _rio_hold_mode) {
    c->hold = getiprop(c->window, _rio_hold_mode);
    if (c == current)
      draw_border(c, 1);
  } else if (a == wm_colormaps) {
    getcmaps(c);
    if (c == current)
      cmapfocus(c);
  }
}

void reparent(XReparentEvent* e) {
  Client* c;
  XWindowAttributes attr;
  ScreenInfo* s;

  /* we don't set curtime as nothing here uses it */
  if (!getscreen(e->event) || e->override_redirect)
    return;
  if ((s = getscreen(e->parent)) != 0) {
    c = getclient(e->window, 1);
    if (c != 0 && (c->dx == 0 || c->dy == 0)) {
      /* flush any errors */
      ignore_badwindow = 1;
      XGetWindowAttributes(dpy, c->window, &attr);
      XSync(dpy, False);
      ignore_badwindow = 0;

      c->x = attr.x;
      c->y = attr.y;
      c->dx = attr.width;
      c->dy = attr.height;
      c->border = attr.border_width;
      c->screen = s;
      if (c->parent == None)
        c->parent = c->screen->root;
    }
  } else {
    c = getclient(e->window, 0);
    if (c != 0 && (c->parent == c->screen->root || withdrawn(c)))
      rmclient(c);
  }
}

#ifdef SHAPE
void shapenotify(XShapeEvent* e) {
  Client* c;

  /* we don't set curtime as nothing here uses it */
  c = getclient(e->window, 0);
  if (c == 0)
    return;

  setshape(c);
}
#endif

void enter(XCrossingEvent* e) {
  Client* c;

  curtime = e->time;
  if (!ffm)
    if (e->mode != NotifyGrab || e->detail != NotifyNonlinearVirtual)
      return;
  c = getclient(e->window, 0);
  if (c != 0 && c != current) {
    /* someone grabbed the pointer; make them current */
    if (!ffm)
      XMapRaised(dpy, c->parent);
    top(c);
    active(c);
  }
}

void leave(XCrossingEvent* e) {
  Client* c;

  c = getclient(e->window, 0);
  if (c)
    XUndefineCursor(dpy, c->parent);
  /* 	XDefineCursor(dpy, c->parent, c->screen->arrow); */
}

void focusin(XFocusChangeEvent* e) {
  Client* c;

  curtime = CurrentTime;
  if (e->detail != NotifyNonlinearVirtual)
    return;
  c = getclient(e->window, 0);
  if (c != 0 && c->window == e->window && c != current) {
#ifdef AUTOSTICK
    if (!isautostick(c)) {
#endif
      /* someone grabbed keyboard or seized focus; make them current */
      XMapRaised(dpy, c->parent);
      top(c);
      active(c);
#ifdef AUTOSTICK
    }
#endif
  }
}

BorderOrient borderorient(Client* c, int x, int y) {
  if (x <= BORDER) {
    if (y <= CORNER) {
      if (debug)
        fprintf(stderr, "topleft\n");
      return BorderWNW;
    }
    if (y >= (c->dy + 2 * BORDER) - CORNER) {
      if (debug)
        fprintf(stderr, "botleft\n");
      return BorderWSW;
    }
    if (y > CORNER && y < (c->dy + 2 * BORDER) - CORNER) {
      if (debug)
        fprintf(stderr, "left\n");
      return BorderW;
    }
  } else if (x <= CORNER) {
    if (y <= BORDER) {
      if (debug)
        fprintf(stderr, "topleft\n");
      return BorderNNW;
    }
    if (y >= (c->dy + BORDER)) {
      if (debug)
        fprintf(stderr, "botleft\n");
      return BorderSSW;
    }
  } else if (x >= (c->dx + BORDER)) {
    if (y <= CORNER) {
      if (debug)
        fprintf(stderr, "topright\n");
      return BorderENE;
    }
    if (y >= (c->dy + 2 * BORDER) - CORNER) {
      if (debug)
        fprintf(stderr, "botright\n");
      return BorderESE;
    }
    if (y > CORNER && y < (c->dy + 2 * BORDER) - CORNER) {
      if (debug)
        fprintf(stderr, "right\n");
      return BorderE;
    }
  } else if (x >= (c->dx + 2 * BORDER) - CORNER) {
    if (y <= BORDER) {
      if (debug)
        fprintf(stderr, "topright\n");
      return BorderNNE;
    }
    if (y >= (c->dy + BORDER)) {
      if (debug)
        fprintf(stderr, "botright\n");
      return BorderSSE;
    }
  } else if (x > CORNER && x < (c->dx + 2 * BORDER) - CORNER) {
    if (y <= BORDER) {
      if (debug)
        fprintf(stderr, "top\n");
      return BorderN;
    }
    if (y >= (c->dy + BORDER)) {
      if (debug)
        fprintf(stderr, "bot\n");
      return BorderS;
    }
  }
  return BorderUnknown;
}

void motionnotify(XMotionEvent* e) {
  Client* c;
  BorderOrient bl;

  c = getclient(e->window, 0);
  if (c) {
    bl = borderorient(c, e->x, e->y);
    if (bl == BorderUnknown)
      XUndefineCursor(dpy, c->parent);
    else
      XDefineCursor(dpy, c->parent, c->screen->bordcurs[bl]);
  }
}