all repos — openbox @ c4a5149016c98346bae4ce594d37065bd8f3dcf0

openbox fork - make it a bit more like ryudo

tools/slit/slit.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
#include "render/render.h"
#include "render/theme.h"

#include <X11/Xlib.h>
#include <stdlib.h>
#include <glib.h>
#ifdef HAVE_SIGNAL_H
#  include <signal.h>
#endif
#ifdef HAVE_SYS_SELECT_H
#  include <sys/select.h>
#endif

#define TITLE_EVENT_MASK (ButtonPressMask | ButtonReleaseMask | \
                          ButtonMotionMask)
#define ROOT_EVENT_MASK (PropertyChangeMask | StructureNotifyMask | \
                         SubstructureNotifyMask)
#define SLITAPP_EVENT_MASK (StructureNotifyMask)

Display *ob_display;
Window ob_root;
int ob_screen;

static struct Slit {
    Window frame;
    Window title;

    /* user-requested position stuff */
    int gravity;
    int user_x, user_y;

    /* actual position (when not auto-hidden) */
    int x, y;
    int w, h;

    gboolean horz;

    Appearance *a_frame;
    Appearance *a_title;

    GList *slit_apps;
} *slit;
static int nslits;

struct SlitApp {
    Window icon_win;
    Window win;
    int x;
    int y;
    int w;
    int h;
};

static Atom atom_atom;
static Atom atom_card;
static Atom atom_theme;
static Atom atom_type;
static Atom atom_type_dock;
static Atom atom_desktop;
static Atom atom_state;
static Atom atom_strut;

static gboolean quit = FALSE;
static gboolean reconfig = FALSE;

void slit_read_theme();
void slit_configure();
void event_handle(XEvent *e);
void slit_add_existing();
void slit_add_app(Window win);
void slit_remove_app(struct Slit *slit, struct SlitApp *app,gboolean reparent);

void sighandler(int signal)
{
    if (signal == SIGUSR1) 
        reconfig =TRUE;
    else
        quit = TRUE;
}

int xerrorhandler(Display *d, XErrorEvent *e)
{
    char errtxt[128];
    XGetErrorText(d, e->error_code, errtxt, 127);
    g_error("X Error: %s", errtxt);
    return 0;
}

int main()
{
    int i;
    guint desk = 0xffffffff;
    XEvent e;
    XSetWindowAttributes attrib;
    struct sigaction action;
    sigset_t sigset;
    int xfd;
    fd_set selset;

    /* set up signal handler */
    sigemptyset(&sigset);
    action.sa_handler = sighandler;
    action.sa_mask = sigset;
    action.sa_flags = SA_NOCLDSTOP;
    sigaction(SIGUSR1, &action, (struct sigaction *) NULL);
    sigaction(SIGPIPE, &action, (struct sigaction *) NULL);
    sigaction(SIGSEGV, &action, (struct sigaction *) NULL);
    sigaction(SIGFPE, &action, (struct sigaction *) NULL);
    sigaction(SIGTERM, &action, (struct sigaction *) NULL);
    sigaction(SIGINT, &action, (struct sigaction *) NULL);
    sigaction(SIGHUP, &action, (struct sigaction *) NULL);

    ob_display = XOpenDisplay(NULL);
    ob_screen = DefaultScreen(ob_display);
    ob_root = RootWindow(ob_display, ob_screen);

   XSetErrorHandler(xerrorhandler);

    render_startup();
    theme_startup();

    atom_atom = XInternAtom(ob_display, "ATOM", False);
    atom_card = XInternAtom(ob_display, "CARDINAL", False);
    atom_theme = XInternAtom(ob_display, "_OPENBOX_THEME", False);
    atom_type = XInternAtom(ob_display, "_NET_WM_WINDOW_TYPE", False);
    atom_type_dock = XInternAtom(ob_display, "_NET_WM_WINDOW_TYPE_DOCK",False);
    atom_desktop =XInternAtom(ob_display, "_NET_WM_DESKTOP", False);
    atom_state = XInternAtom(ob_display, "WM_STATE", False);
    atom_strut = XInternAtom(ob_display, "_NET_WM_STRUT", False);

    nslits = 1;
    slit = g_new0(struct Slit, nslits);

    for (i = 0; i < nslits; ++i) {
        slit[i].horz = TRUE;

        slit[i].frame = XCreateWindow(ob_display, ob_root, 0, 0, 1, 1, 0,
                                      render_depth, InputOutput, render_visual,
                                      0, NULL);
        attrib.event_mask = TITLE_EVENT_MASK;
        slit[i].title = XCreateWindow(ob_display, slit[i].frame, 0, 0, 1, 1, 0,
                                      render_depth, InputOutput, render_visual,
                                      CWEventMask, &attrib);
        XMapWindow(ob_display, slit[i].title);

        XChangeProperty(ob_display, slit[i].frame, atom_type, atom_atom,
                        32, PropModeReplace, (guchar*)&atom_type_dock, 1);

        XChangeProperty(ob_display, slit[i].frame, atom_desktop, atom_card,
                        32, PropModeReplace, (guchar*)&desk, 1);
    }

    slit_read_theme();

    XSelectInput(ob_display, ob_root, ROOT_EVENT_MASK);

    slit_add_existing();

    xfd = ConnectionNumber(ob_display);
    FD_ZERO(&selset);
    FD_SET(xfd, &selset);
    while (!quit) {
        gboolean hadevent = FALSE;
        while (XPending(ob_display)) {
            XNextEvent(ob_display, &e);
            event_handle(&e);
            hadevent = TRUE;
        }
        if (!hadevent) {
            if (reconfig)
                slit_read_theme();

            if (!quit)
                select(xfd + 1, &selset, NULL, NULL, NULL);
        }
    }

    for (i = 0; i < nslits; ++i) {
        while (slit[i].slit_apps)
            slit_remove_app(&slit[i], slit[i].slit_apps->data, TRUE);

        XDestroyWindow(ob_display, slit[i].title);
        XDestroyWindow(ob_display, slit[i].frame);

        appearance_free(slit[i].a_frame);
        appearance_free(slit[i].a_title);
    }

    theme_shutdown();
    render_shutdown();
    XCloseDisplay(ob_display);
    return 0;
}

Window find_client(Window win)
{
  Window r, *children;
  unsigned int n, i;
  Atom ret_type;
  int ret_format;
  unsigned long ret_items, ret_bytesleft;
  unsigned long *prop_return;

  XQueryTree(ob_display, win, &r, &r, &children, &n);
  for (i = 0; i < n; ++i) {
    Window w = find_client(children[i]);
    if (w) return w;
  }

  /* try me */
  XGetWindowProperty(ob_display, win, atom_state, 0, 1,
		     False, atom_state, &ret_type, &ret_format,
		     &ret_items, &ret_bytesleft,
		     (unsigned char**) &prop_return); 
  if (ret_type == None || ret_items < 1)
    return None;
  return win; /* found it! */
}

void event_handle(XEvent *e)
{
    int i;
    Window win;
    static guint button = 0;
    int sw, sh;
    int xpos, ypos;
    int x, y, g;

    switch (e->type) {
    case ButtonPress:
        if (!button) {
            button = e->xbutton.button;
        }
        break;
    case ButtonRelease:
        if (button == e->xbutton.button)
            button = 0;
        break;
    case MotionNotify:
        if (button == 1) {
            for (i = 0; i < nslits; ++i)
                if (slit[i].title == e->xmotion.window) {
                    /* pick a corner and move it */
                    sw = WidthOfScreen(ScreenOfDisplay(ob_display, ob_screen));
                    sh = HeightOfScreen(ScreenOfDisplay(ob_display,ob_screen));

                    if (e->xmotion.x_root < sw / 3) /* left edge */
                        xpos = 0;
                    else if (e->xmotion.x_root < sw / 3 * 2) /* middle */
                        xpos = 1;
                    else /* right edge */
                        xpos = 2;
                    if (e->xmotion.y_root < sh / 3) /* top edge */
                        ypos = 0;
                    else if (e->xmotion.y_root < sh / 3 * 2) /* middle */
                        ypos = 1;
                    else /* bottom edge */
                        ypos = 2;

                    if (xpos == 1 && ypos == 1)
                        return; /* cant go in middle middle */

                    if (xpos == 0) {
                        if (ypos == 0) {
                            x = 0;
                            y = 0;
                            g = NorthWestGravity;
                        } else if (ypos == 1) {
                            x = 0;
                            y = sh / 2;
                            g = WestGravity;
                        } else {
                            x = 0;
                            y = sh;
                            g = SouthWestGravity;
                        }
                    } else if (xpos == 1) {
                        if (ypos == 0) {
                            x = sw / 2;
                            y = 0;
                            g = NorthGravity;
                        } else {
                            x = sw / 2;
                            y = sh;
                            g = SouthGravity;
                        }
                    } else {
                        if (ypos == 0) {
                            x = sw;
                            y = 0;
                            g = NorthEastGravity;
                        } else if (ypos == 1) {
                            x = sw;
                            y = sh / 2;
                            g = EastGravity;
                        } else {
                            x = sw;
                            y = sh;
                            g = SouthEastGravity;
                        }
                    }
                    if (x != slit[i].x || y != slit[i].y ||
                        g != slit[i].gravity) {
                        slit[i].user_x = x;
                        slit[i].user_y = y;
                        slit[i].gravity = g;
                        slit_configure();
                    }
                }
        }
        break;
    case PropertyNotify:
        g_message("PropertyNotify on 0x%lx", e->xproperty.window);
        if (e->xproperty.window == ob_root) {
            if (e->xproperty.atom == atom_theme)
                slit_read_theme();
        }
        break;
    case ConfigureNotify:
        g_message("ConfigureNotify on 0x%lx", e->xconfigure.window);
        if (e->xconfigure.window == ob_root) {
            slit_configure();
            return;
        }

        /* an owned slitapp? */
        for (i = 0; i < nslits; ++i) {
            GList *it;

            for (it = slit[i].slit_apps; it; it = it->next) {
                struct SlitApp *app = it->data;
                if (e->xconfigure.window == app->icon_win) {
                    if (app->w != e->xconfigure.width ||
                        app->h != e->xconfigure.height) {
                        g_message("w %d h %d w %d h %d",
                                  app->w, e->xconfigure.width,
                                  app->h, e->xconfigure.height);
                        app->w = e->xconfigure.width;
                        app->h = e->xconfigure.height;
                        slit_configure();
                    }
                    return;
                }
            }
        }
        break;
    case MapNotify:
        g_message("MapNotify on 0x%lx", e->xmap.window);

        win = find_client(e->xmap.window);
        if (!win) return;

        for (i = 0; i < nslits; ++i)
            if (win == slit[i].frame)
                return;

        slit_add_app(win);
        break;
    case UnmapNotify:
        g_message("UnmapNotify on 0x%lx", e->xunmap.window);
        for (i = 0; i < nslits; ++i) {
            GList *it;

            for (it = slit[i].slit_apps; it; it = it->next) {
                struct SlitApp *app = it->data;
                if (e->xunmap.window == app->icon_win) {
                    gboolean r;
                    XEvent e;

                    r = !XCheckTypedWindowEvent(ob_display, app->icon_win,
                                                DestroyNotify, &e);
                    if (r) {
                        if (XCheckTypedWindowEvent(ob_display, app->icon_win,
                                                   ReparentNotify, &e)) {
                            XPutBackEvent(ob_display, &e);
                            r = FALSE;
                        }
                    }
                    slit_remove_app(&slit[i], app, r);
                    break;
                }
            }
        }
        break;
    case ReparentNotify:
        g_message("ReparentNotify on 0x%lx", e->xdestroywindow.window);
        for (i = 0; i < nslits; ++i) {
            GList *it;

            for (it = slit[i].slit_apps; it; it = it->next) {
                struct SlitApp *app = it->data;
                if (e->xdestroywindow.window == app->icon_win) {
                    slit_remove_app(&slit[i], app, FALSE);
                    break;
                }
            }
        }
    case DestroyNotify:
        g_message("DestroyNotify on 0x%lx", e->xdestroywindow.window);
        for (i = 0; i < nslits; ++i) {
            GList *it;

            for (it = slit[i].slit_apps; it; it = it->next) {
                struct SlitApp *app = it->data;
                if (e->xdestroywindow.window == app->icon_win) {
                    slit_remove_app(&slit[i], app, FALSE);
                    break;
                }
            }
        }
        break;
    }
}

void slit_add_existing()
{
    unsigned int i, nchild;
    int j;
    Window w, *children;
    XWindowAttributes attrib;

    XQueryTree(ob_display, ob_root, &w, &w, &children, &nchild);

    for (i = 0; i < nchild; ++i) {
        for (j = 0; j < nslits; ++j)
            if (children[i] == slit[j].frame)
                continue;
        if (children[i] == None)
            continue;
        if ((children[i] = find_client(children[i])) == None)
            continue;
	if (XGetWindowAttributes(ob_display, children[i], &attrib)) {
	    if (attrib.override_redirect) continue;

            slit_add_app(children[i]);
	}
    }
    XFree(children);
}

void slit_add_app(Window win)
{
    int i;
    XWMHints *h;
    XWindowAttributes attrib;
    struct Slit *s;

    s = &slit[0];

    if ((h = XGetWMHints(ob_display, win))) {
        if (h->flags & StateHint && h->initial_state == WithdrawnState) {
            struct SlitApp *app = g_new(struct SlitApp, 1);

            app->win = win;
            app->icon_win = (h->flags & IconWindowHint) ?
                h->icon_window : win;

            XFree(h);

            for (i = 0; i < nslits; ++i) {
                GList *it;
                for (it = slit[i].slit_apps; it; it = it->next)
                    if (app->icon_win ==
                        ((struct SlitApp*)it->data)->icon_win)
                        /* already managed! */
                        return;
            }

            if (XGetWindowAttributes(ob_display, app->icon_win, &attrib)) {
                app->w = attrib.width;
                app->h = attrib.height;
            } else {
                g_free(app);
                app = NULL;
            }

            if (app) {
                s->slit_apps = g_list_append(s->slit_apps, app);
                slit_configure();
                XReparentWindow(ob_display, app->icon_win,
                                s->frame, app->x, app->y);
/*                if (app->win != app->icon_win)
                  XUnmapWindow(ob_display, app->win);*/
                XSync(ob_display, False);
                XSelectInput(ob_display, app->icon_win,
                             SLITAPP_EVENT_MASK);
            }
            g_message("Managed: 0x%lx", app->icon_win);
        } else
            XFree(h);
    }
}

void slit_remove_app(struct Slit *slit, struct SlitApp *app, gboolean reparent)
{

    XSelectInput(ob_display, app->icon_win, NoEventMask);
    XSync(ob_display, False);
    if (reparent) {
        g_message("reparenting");
/*        if (app->win != app->icon_win)
          XMapWindow(ob_display, app->win);*/
        XReparentWindow(ob_display, app->icon_win, ob_root, 0, 0);
    }

    g_free(app);
    slit->slit_apps = g_list_remove(slit->slit_apps, app);
    slit_configure();
}

void slit_read_theme()
{
    XTextProperty prop;
    int i;
    char *theme = NULL;

    if (XGetTextProperty(ob_display, ob_root, &prop,
                         XInternAtom(ob_display, "_OPENBOX_THEME", False))) {
        theme = theme_load((char*)prop.value);
        XFree(prop.value);
    } else
        theme = theme_load(NULL);
 
    g_free(theme);
    if (!theme) exit(EXIT_FAILURE);

    for (i = 0; i < nslits; ++i) {
        appearance_free(slit[i].a_frame);
        appearance_free(slit[i].a_title);

        slit[i].a_frame = appearance_copy(theme_a_unfocused_title);
        slit[i].a_title = appearance_copy(theme_a_unfocused_title);

        XSetWindowBorder(ob_display, slit[i].frame, theme_b_color->pixel);
        XSetWindowBorderWidth(ob_display, slit[i].frame, theme_bwidth);
        XSetWindowBorder(ob_display, slit[i].frame, BlackPixel(ob_display, ob_screen));
        XSetWindowBorderWidth(ob_display, slit[i].frame, 30);
    }

    slit_configure();
}

void slit_configure()
{
    int i;
    int titleh;
    GList *it;
    int spot;

    titleh = 4;

    for (i = 0; i < nslits; ++i) {
        if (slit[i].horz) {
            slit[i].w = titleh;
            slit[i].h = 0;
        } else {
            slit[i].w = 0;
            slit[i].h = titleh;
        }
        spot = titleh;

        for (it = slit[i].slit_apps; it; it = it->next) {
            struct SlitApp *app = it->data;
            if (slit[i].horz) {
                g_message("%d", spot);
                app->x = spot;
                app->y = 0;
                slit[i].w += app->w;
                slit[i].h = MAX(slit[i].h, app->h);
                spot += app->w;
            } else {
                app->x = 0;
                app->y = spot;
                slit[i].w = MAX(slit[i].h, app->w);
                slit[i].h += app->h;
                spot += app->h;
            }

            XMoveWindow(ob_display, app->icon_win, app->x, app->y);
        }

        /* calculate position */
        slit[i].x = slit[i].user_x;
        slit[i].y = slit[i].user_y;

        switch(slit[i].gravity) {
        case NorthGravity:
        case CenterGravity:
        case SouthGravity:
            slit[i].x -= slit[i].w / 2;
            break;
        case NorthEastGravity:
        case EastGravity:
        case SouthEastGravity:
            slit[i].x -= slit[i].w;
            break;
        }
        switch(slit[i].gravity) {
        case WestGravity:
        case CenterGravity:
        case EastGravity:
            slit[i].y -= slit[i].h / 2;
            break;
        case SouthWestGravity:
        case SouthGravity:
        case SouthEastGravity:
            slit[i].y -= slit[i].h;
            break;
        }

        if (slit[i].w > 0 && slit[i].h > 0) {
            RECT_SET(slit[i].a_frame->area, 0, 0, slit[i].w, slit[i].h);
            XMoveResizeWindow(ob_display, slit[i].frame,
                              slit[i].x - theme_bwidth,
                              slit[i].y - theme_bwidth,
                              slit[i].w, slit[i].h);

            if (slit[i].horz) {
                RECT_SET(slit[i].a_title->area, 0, 0, titleh, slit[i].h);
                XMoveResizeWindow(ob_display, slit[i].title, 0, 0,
                                  titleh, slit[i].h);
            } else {
                RECT_SET(slit[i].a_title->area, 0, 0, slit[i].w, titleh);
                XMoveResizeWindow(ob_display, slit[i].title, 0, 0,
                                  slit[i].w, titleh);
            }

            paint(slit[i].frame, slit[i].a_frame);
            paint(slit[i].title, slit[i].a_title);
            XMapWindow(ob_display, slit[i].frame);
        } else
            XUnmapWindow(ob_display, slit[i].frame);
    }
}