aboutsummaryrefslogtreecommitdiff
path: root/menu.c
blob: cefa8785b8382ef8c0b541aa612d004477d15fcf (plain)
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
/*
 * Pop-up menus.
 */

/* Copyright (c) 1994-1996 David Hogan, see README for licence details */
#define _SVID_SOURCE 1 /* putenv in glibc */
#define _DEFAULT_SOURCE 1
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "config.h"
#ifdef VIRTNOTIFY
#include <libnotify/notify.h>
#endif
#include "dat.h"
#include "fns.h"

Client* hiddenc[MAXHIDDEN];

int numhidden;

int virt;
int reversehide = 1;

Client* currents[NUMVIRTUALS] = {NULL, NULL, NULL, NULL};

char* b2items[NUMVIRTUALS + 1] = VIRTUALLIST;

Menu b2menu = {b2items};

char* b3items[B3FIXED + MAXHIDDEN + 1] = {
  "New",
  "Reshape",
  "Move",
#if defined(SHOWMAX) && defined(SHOWSTICK)
  "Maximize",
  "Stick",
#else
#ifdef SHOWMAX
  "Maximize",
#endif
#ifdef SHOWSTICK
  "Stick",
#endif
#endif
  "Delete",
  "Hide",
  0};

enum {
  New,
  Reshape,
  Move,
#if defined(SHOWMAX) && defined(SHOWSTICK)
  Maximize,
  Stick,
#else
#ifdef SHOWMAX
  Maximize,
#endif
#ifdef SHOWSTICK
  Stick,
#endif
#endif
  Delete,
  Hide
};

Menu b3menu = {b3items};

Menu egg = {version};

void button(XButtonEvent* e) {
  int n, shift;
  Client* c;
  Window dw;
  ScreenInfo* s;

  static XWindowAttributes ra;
  XGetWindowAttributes(dpy, DefaultRootWindow(dpy), &ra);

  curtime = e->time;
  s = getscreen(e->root);
  if (s == 0)
    return;
  c = getclient(e->window, 0);
  if (c) {
    if (debug)
      fprintf(
        stderr,
        "but: e x=%d y=%d c x=%d y=%d dx=%d dy=%d BORDR %d\n",
        e->x,
        e->y,
        c->x,
        c->y,
        c->dx,
        c->dy,
        BORDER);
    if (borderorient(c, e->x, e->y) != BorderUnknown) {
      switch (e->button) {
        case Button1:
        case Button2:
          reshape(c, e->button, pull, e);
          return;
        case Button3:
          move(c, Button3);
          return;
        default:
          return;
      }
    }
    e->x += c->x - BORDER;
    e->y += c->y - BORDER;
  } else if (e->window != e->root) {
    if (debug)
      fprintf(stderr, "but no client: e x=%d y=%d\n", e->x, e->y);
    XTranslateCoordinates(
      dpy,
      e->window,
      s->root,
      e->x,
      e->y,
      &e->x,
      &e->y,
      &dw);
  }
  switch (e->button) {
    case Button1:
      fflush(stdout);
      if (c) {
        if (ffm)
          XRaiseWindow(dpy, c->window);
        XMapRaised(dpy, c->parent);
        top(c);
        active(c);
      }
      return;
    case Button2:
      if (c) {
        XMapRaised(dpy, c->parent);
        active(c);
        XAllowEvents(dpy, ReplayPointer, curtime);
      } else if (
        (e->state & (ShiftMask | ControlMask)) == (ShiftMask | ControlMask)) {
        menuhit(e, &egg);
      } else if (numvirtuals > 1 && (n = menuhit(e, &b2menu)) > -1)
        button2(n);
      return;
    case Button3:
      break;
    case Button4:
      /* scroll up changes to previous virtual screen, wraps around */
      if (!c && e->type == ButtonPress) {
        if (numvirtuals > 1) {
          if (virt > 0) {
            switch_to(virt - 1);
          } else {
            switch_to(numvirtuals - 1);
          }
        }
      }
      return;
    case Button5:
      /* scroll down changes to next virtual screen, wraps around */
      if (!c && e->type == ButtonPress) {
        if (numvirtuals > 1) {
          if (virt < numvirtuals - 1) {
            switch_to(virt + 1);
          } else {
            switch_to(0);
          }
        }
      }
      return;
    default:
      return;
  }

  if (current && current->screen == s)
    cmapnofocus(s);
  switch (n = menuhit(e, &b3menu)) {
    case New:
      spawn(s);
      break;
    case Reshape:
      reshape(selectwin(1, 0, s), Button3, sweep, 0);
      break;
    case Move:
      move(selectwin(0, 0, s), Button3);
      break;
    case Delete:
      shift = 0;
      c = selectwin(1, &shift, s);
      delete (c, shift);
      break;
    case Hide:
      hide(selectwin(1, 0, s));
      break;
#ifdef SHOWMAX
    case Maximize:
      c = selectwin(1, 0, s);
#ifdef AUTOSTICK
      if (isautostick(c))
        break;
#endif
      quickreshape(
        c,
        -BORDER,
        -BORDER,
        ra.width + 2 * BORDER,
        ra.height + 2 * BORDER);
      active(c);
      top(c);
      break;
#endif
#ifdef SHOWSTICK
    case Stick:
      stick(selectwin(1, 0, s));
      break;
#endif
    default: /* unhide window */
      unhide(n - B3FIXED, 1);
      break;
    case -1: /* nothing */
      break;
  }
  if (current && current->screen == s)
    cmapfocus(current);
}

void spawn(ScreenInfo* s) {
  /*
   * ugly dance to cause sweeping for terminals.
   * the very next window created will require sweeping.
   * hope it's created by the program we're about to
   * exec!
   */
  isNew = 1;
  /*
   * ugly dance to avoid leaving zombies. Could use SIGCHLD,
   * but it's not very portable.
   */
  if (fork() == 0) {
    if (fork() == 0) {
      close(ConnectionNumber(dpy));
      if (s->display[0] != '\0')
        putenv(s->display);
      signal(SIGINT, SIG_DFL);
      signal(SIGTERM, SIG_DFL);
      signal(SIGHUP, SIG_DFL);
      if (termprog != NULL) {
        execl(shell, shell, "-c", termprog, (char*)0);
        fprintf(stderr, "ryudo: exec %s", shell);
        perror(" failed");
      }
      execlp("urxvt", "urxvt", (char*)0);
      execlp("9term", "9term", scrolling ? "-ws" : "-w", (char*)0);
      execlp("xterm", "xterm", "-ut", (char*)0);
      perror("ryudo: exec urxvt/9term/xterm failed");
      exit(1);
    }
    exit(0);
  }
  wait((int*)0);
}

void reshape(
  Client* c, int but, int (*fn)(Client*, int, XButtonEvent*), XButtonEvent* e) {
  int odx, ody;

  if (c == 0)
    return;
  odx = c->dx;
  ody = c->dy;
  if (fn(c, but, e) == 0)
    return;
  active(c);
  top(c);
  XRaiseWindow(dpy, c->parent);
  XMoveResizeWindow(
    dpy,
    c->parent,
    c->x - BORDER,
    c->y - BORDER,
    c->dx + 2 * BORDER,
    c->dy + 2 * BORDER);
  if (c->dx == odx && c->dy == ody)
    sendconfig(c);
  else
    XMoveResizeWindow(dpy, c->window, BORDER, BORDER, c->dx, c->dy);
}

void move(Client* c, int but) {
  if (c == 0)
    return;
  if (drag(c, but) == 0)
    return;
  active(c);
  top(c);
  XRaiseWindow(dpy, c->parent);
  XMoveWindow(dpy, c->parent, c->x - BORDER, c->y - BORDER);
  sendconfig(c);
}

void delete (Client* c, int shift) {
  // int v;
  if (c == 0)
    return;
  // v = c->virt;
  if ((c->proto & Pdelete) && !shift)
    sendcmessage(c->window, wm_protocols, wm_delete, 0, 0);
  else
    XKillClient(dpy, c->window); /* let event clean up */
  ensureactive();
}

void hide(Client* c) {
  if (c == 0 || numhidden == MAXHIDDEN)
    return;
  if (hidden(c)) {
    fprintf(stderr, "ryudo: already hidden: %s\n", c->label);
    return;
  }
  XUnmapWindow(dpy, c->parent);
  XUnmapWindow(dpy, c->window);
  setstate(c, IconicState);
  if (c == current)
    nofocus();
  if (reversehide) {
    memmove(hiddenc + 1, hiddenc, numhidden * sizeof hiddenc[0]);
    memmove(
      b3items + B3FIXED + 1,
      b3items + B3FIXED,
      numhidden * sizeof b3items[0]);
    hiddenc[0] = c;
    b3items[B3FIXED] = c->label;
  } else {
    hiddenc[numhidden] = c;
    b3items[B3FIXED + numhidden] = c->label;
  }
  numhidden++;
  b3items[B3FIXED + numhidden] = 0;
  ensureactive();
}

void unhide(int n, int map) {
  Client* c;
  int i;

  if (n >= numhidden) {
    fprintf(stderr, "ryudo: unhide: n %d numhidden %d\n", n, numhidden);
    return;
  }
  c = hiddenc[n];
  if (!hidden(c)) {
    fprintf(
      stderr,
      "ryudo: unhide: not hidden: %s(0x%x)\n",
      c->label,
      (int)c->window);
    return;
  }
  c->virt = virt;

  if (map) {
    XMapWindow(dpy, c->window);
    XMapRaised(dpy, c->parent);
    setstate(c, NormalState);
    active(c);
    top(c);
  }

  numhidden--;
  for (i = n; i < numhidden; i++) {
    hiddenc[i] = hiddenc[i + 1];
    b3items[B3FIXED + i] = b3items[B3FIXED + i + 1];
  }
  b3items[B3FIXED + numhidden] = 0;
}

void unhidec(Client* c, int map) {
  int i;

  for (i = 0; i < numhidden; i++)
    if (c == hiddenc[i]) {
      unhide(i, map);
      return;
    }
  fprintf(
    stderr,
    "ryudo: unhidec: not hidden: %s(0x%x)\n",
    c->label,
    (int)c->window);
}

void stick(Client* c) {
  if (numvirtuals > 1 && c->virt >= 0)
    c->virt = -1;
  else
    c->virt = virt;
}

void renamec(Client* c, char* name) {
  int i;

  if (name == 0)
    name = "???";
  c->label = name;
  if (!hidden(c))
    return;
  for (i = 0; i < numhidden; i++)
    if (c == hiddenc[i]) {
      b3items[B3FIXED + i] = name;
      return;
    }
}

void button2(int n) {
  switch_to(n);
  if (current)
    cmapfocus(current);
}

void switch_to_c(int n, Client* c) {
  if (c == 0)
    return;

  if (c->next)
    switch_to_c(n, c->next);

  if (c->parent == DefaultRootWindow(dpy))
    return;

#ifdef AUTOSTICK
  if (c->virt >= 0 && isautostick(c)) {
    stick(c);
  }
#endif

  if (c->virt != virt && c->state == NormalState && c->virt >= 0) {
    XUnmapWindow(dpy, c->parent);
    XUnmapWindow(dpy, c->window);
    setstate(c, IconicState);
    if (c == current)
      nofocus();
  } else if (c->virt == virt && c->state == IconicState) {
    int i;

    for (i = 0; i < numhidden; i++)
      if (c == hiddenc[i])
        break;

    if (i == numhidden) {
      XMapWindow(dpy, c->window);
      XMapWindow(dpy, c->parent);
      setstate(c, NormalState);
      if (currents[virt] == c)
        active(c);
    }
  }
}

void switch_to(int n) {
#ifdef VIRTNOTIFY
  static char virtmsg[32];
  NotifyNotification* notification =
    notify_notification_new(VIRTHEADER, NULL, NULL);
#endif
  if (n == virt)
    return;
  currents[virt] = current;
  virt = n;

  /* redundant when called from a menu switch
   * but needed for scroll-button switches
   */
  b2menu.lasthit = n;

  switch_to_c(n, clients);
  current = currents[virt];
  ensureactive();
  top(current);
#ifdef VIRTNOTIFY
  sprintf(virtmsg, VIRTMSG, b2items[virt]);
  notify_notification_update(notification, VIRTHEADER, virtmsg, NULL);
  notify_notification_set_category(notification, "virtual");
  notify_notification_show(notification, NULL);
  notify_notification_close(notification, NULL);
#endif
}

void initb2menu(int n) { b2items[n] = 0; }