all repos — openbox @ 5b6f3c6f7a0e4f1efe3c90865226f02ffa03ca9e

openbox fork - make it a bit more like ryudo

openbox/actions/desktop.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
#include "openbox/actions.h"
#include "openbox/screen.h"
#include "openbox/client.h"
#include "openbox/openbox.h"
#include "obt/keyboard.h"

typedef enum {
    LAST,
    RELATIVE,
    ABSOLUTE
} SwitchType;

typedef struct {
    SwitchType type;
    union {
        struct {
            guint desktop;
        } abs;

        struct {
            gboolean linear;
            gboolean wrap;
            ObDirection dir;
        } rel;
    } u;
    gboolean send;
    gboolean follow;
    gboolean interactive;
} Options;

static gpointer setup_go_func(xmlNodePtr node,
                              ObActionsIPreFunc *pre,
                              ObActionsIInputFunc *input,
                              ObActionsICancelFunc *cancel,
                              ObActionsIPostFunc *post);
static gpointer setup_send_func(xmlNodePtr node,
                                ObActionsIPreFunc *pre,
                                ObActionsIInputFunc *input,
                                ObActionsICancelFunc *cancel,
                                ObActionsIPostFunc *post);
static gboolean run_func(ObActionsData *data, gpointer options);

static gboolean i_pre_func(guint state, gpointer options);
static gboolean i_input_func(guint initial_state,
                             XEvent *e,
                             ObtIC *ic,
                             gpointer options,
                             gboolean *used);
static void i_post_func(gpointer options);

/* 3.4-compatibility */
static gpointer setup_go_last_func(xmlNodePtr node);
static gpointer setup_send_last_func(xmlNodePtr node);
static gpointer setup_go_abs_func(xmlNodePtr node);
static gpointer setup_send_abs_func(xmlNodePtr node);
static gpointer setup_go_next_func(xmlNodePtr node,
                                   ObActionsIPreFunc *pre,
                                   ObActionsIInputFunc *input,
                                   ObActionsICancelFunc *cancel,
                                   ObActionsIPostFunc *post);
static gpointer setup_send_next_func(xmlNodePtr node,
                                     ObActionsIPreFunc *pre,
                                     ObActionsIInputFunc *input,
                                     ObActionsICancelFunc *cancel,
                                     ObActionsIPostFunc *post);
static gpointer setup_go_prev_func(xmlNodePtr node,
                                   ObActionsIPreFunc *pre,
                                   ObActionsIInputFunc *input,
                                   ObActionsICancelFunc *cancel,
                                   ObActionsIPostFunc *post);
static gpointer setup_send_prev_func(xmlNodePtr node,
                                     ObActionsIPreFunc *pre,
                                     ObActionsIInputFunc *input,
                                     ObActionsICancelFunc *cancel,
                                     ObActionsIPostFunc *post);
static gpointer setup_go_left_func(xmlNodePtr node,
                                   ObActionsIPreFunc *pre,
                                   ObActionsIInputFunc *input,
                                   ObActionsICancelFunc *cancel,
                                   ObActionsIPostFunc *post);
static gpointer setup_send_left_func(xmlNodePtr node,
                                     ObActionsIPreFunc *pre,
                                     ObActionsIInputFunc *input,
                                     ObActionsICancelFunc *cancel,
                                     ObActionsIPostFunc *post);
static gpointer setup_go_right_func(xmlNodePtr node,
                                    ObActionsIPreFunc *pre,
                                    ObActionsIInputFunc *input,
                                    ObActionsICancelFunc *cancel,
                                    ObActionsIPostFunc *post);
static gpointer setup_send_right_func(xmlNodePtr node,
                                      ObActionsIPreFunc *pre,
                                      ObActionsIInputFunc *input,
                                      ObActionsICancelFunc *cancel,
                                      ObActionsIPostFunc *post);
static gpointer setup_go_up_func(xmlNodePtr node,
                                 ObActionsIPreFunc *pre,
                                 ObActionsIInputFunc *input,
                                 ObActionsICancelFunc *cancel,
                                 ObActionsIPostFunc *post);
static gpointer setup_send_up_func(xmlNodePtr node,
                                   ObActionsIPreFunc *pre,
                                   ObActionsIInputFunc *input,
                                   ObActionsICancelFunc *cancel,
                                   ObActionsIPostFunc *post);
static gpointer setup_go_down_func(xmlNodePtr node,
                                   ObActionsIPreFunc *pre,
                                   ObActionsIInputFunc *input,
                                   ObActionsICancelFunc *cancel,
                                   ObActionsIPostFunc *post);
static gpointer setup_send_down_func(xmlNodePtr node,
                                     ObActionsIPreFunc *pre,
                                     ObActionsIInputFunc *input,
                                     ObActionsICancelFunc *cancel,
                                     ObActionsIPostFunc *post);
 
void action_desktop_startup(void)
{
    actions_register_i("GoToDesktop", setup_go_func, g_free, run_func);
    actions_register_i("SendToDesktop", setup_send_func, g_free, run_func);
    /* 3.4-compatibility */
    actions_register("DesktopLast", setup_go_last_func, g_free, run_func);
    actions_register("SendToDesktopLast", setup_send_last_func,
                     g_free, run_func);
    actions_register("Desktop", setup_go_abs_func, g_free, run_func);
    actions_register("SendToDesktop", setup_send_abs_func, g_free, run_func);
    actions_register_i("DesktopNext", setup_go_next_func, g_free, run_func);
    actions_register_i("SendToDesktopNext", setup_send_next_func,
                       g_free, run_func);
    actions_register_i("DesktopPrevious", setup_go_prev_func,
                       g_free, run_func);
    actions_register_i("SendToDesktopPrevious", setup_send_prev_func,
                       g_free, run_func);
    actions_register_i("DesktopLeft", setup_go_left_func, g_free, run_func);
    actions_register_i("SendToDesktopLeft", setup_send_left_func,
                       g_free, run_func);
    actions_register_i("DesktopRight", setup_go_right_func, g_free, run_func);
    actions_register_i("SendToDesktopRight", setup_send_right_func,
                       g_free, run_func);
    actions_register_i("DesktopUp", setup_go_up_func, g_free, run_func);
    actions_register_i("SendToDesktopUp", setup_send_up_func,
                       g_free, run_func);
    actions_register_i("DesktopDown", setup_go_down_func, g_free, run_func);
    actions_register_i("SendToDesktopDown", setup_send_down_func,
                       g_free, run_func);
}

static gpointer setup_func(xmlNodePtr node,
                           ObActionsIPreFunc *pre,
                           ObActionsIInputFunc *input,
                           ObActionsICancelFunc *cancel,
                           ObActionsIPostFunc *post)
{
    xmlNodePtr n;
    Options *o;

    o = g_new0(Options, 1);
    /* don't go anywhere if there are no options given */
    o->type = ABSOLUTE;
    o->u.abs.desktop = screen_desktop;
    /* wrap by default - it's handy! */
    o->u.rel.wrap = TRUE;

    if ((n = obt_xml_find_node(node, "to"))) {
        gchar *s = obt_xml_node_string(n);
        if (!g_ascii_strcasecmp(s, "last"))
            o->type = LAST;
        else if (!g_ascii_strcasecmp(s, "next")) {
            o->type = RELATIVE;
            o->u.rel.linear = TRUE;
            o->u.rel.dir = OB_DIRECTION_EAST;
        }
        else if (!g_ascii_strcasecmp(s, "previous")) {
            o->type = RELATIVE;
            o->u.rel.linear = TRUE;
            o->u.rel.dir = OB_DIRECTION_WEST;
        }
        else if (!g_ascii_strcasecmp(s, "north") ||
                 !g_ascii_strcasecmp(s, "up")) {
            o->type = RELATIVE;
            o->u.rel.dir = OB_DIRECTION_NORTH;
        }
        else if (!g_ascii_strcasecmp(s, "south") ||
                 !g_ascii_strcasecmp(s, "down")) {
            o->type = RELATIVE;
            o->u.rel.dir = OB_DIRECTION_SOUTH;
        }
        else if (!g_ascii_strcasecmp(s, "west") ||
                 !g_ascii_strcasecmp(s, "left")) {
            o->type = RELATIVE;
            o->u.rel.dir = OB_DIRECTION_WEST;
        }
        else if (!g_ascii_strcasecmp(s, "east") ||
                 !g_ascii_strcasecmp(s, "right")) {
            o->type = RELATIVE;
            o->u.rel.dir = OB_DIRECTION_EAST;
        }
        else {
            o->type = ABSOLUTE;
            o->u.abs.desktop = atoi(s) - 1;
        }
        g_free(s);
    }

    if ((n = obt_xml_find_node(node, "wrap")))
        o->u.rel.wrap = obt_xml_node_bool(n);

    return o;
}


static gpointer setup_go_func(xmlNodePtr node,
                              ObActionsIPreFunc *pre,
                              ObActionsIInputFunc *input,
                              ObActionsICancelFunc *cancel,
                              ObActionsIPostFunc *post)
{
    Options *o;

    o = setup_func(node, pre, input, cancel, post);
    if (o->type == RELATIVE) {
        o->interactive = TRUE;
        *pre = i_pre_func;
        *input = i_input_func;
        *post = i_post_func;
    }

    return o;
}

static gpointer setup_send_func(xmlNodePtr node,
                                ObActionsIPreFunc *pre,
                                ObActionsIInputFunc *input,
                                ObActionsICancelFunc *cancel,
                                ObActionsIPostFunc *post)
{
    xmlNodePtr n;
    Options *o;

    o = setup_func(node, pre, input, cancel, post);
    o->send = TRUE;
    o->follow = TRUE;

    if ((n = obt_xml_find_node(node, "follow")))
        o->follow = obt_xml_node_bool(n);

    if (o->type == RELATIVE && o->follow) {
        o->interactive = TRUE;
        *pre = i_pre_func;
        *input = i_input_func;
        *post = i_post_func;
    }

    return o;
}

/* Always return FALSE because its not interactive */
static gboolean run_func(ObActionsData *data, gpointer options)
{
    Options *o = options;
    guint d;

    switch (o->type) {
    case LAST:
        d = screen_last_desktop;
        break;
    case ABSOLUTE:
        d = o->u.abs.desktop;
        break;
    case RELATIVE:
        d = screen_find_desktop(screen_desktop,
                                o->u.rel.dir, o->u.rel.wrap, o->u.rel.linear);
        break;
    default:
        g_assert_not_reached();
    }

    if (d < screen_num_desktops && d != screen_desktop) {
        gboolean go = TRUE;

        actions_client_move(data, TRUE);
        if (o->send && data->client && client_normal(data->client)) {
            client_set_desktop(data->client, d, o->follow, FALSE);
            go = o->follow;
        }

        if (go) {
            screen_set_desktop(d, TRUE);
            if (data->client)
                client_bring_helper_windows(data->client);
        }

        actions_client_move(data, FALSE);
    }

    return o->interactive;
}

static gboolean i_input_func(guint initial_state,
                             XEvent *e,
                             ObtIC *ic,
                             gpointer options,
                             gboolean *used)
{
    guint mods;

    mods = obt_keyboard_only_modmasks(e->xkey.state);
    if (e->type == KeyRelease) {
        /* remove from the state the mask of the modifier key being
           released, if it is a modifier key being released that is */
        mods &= ~obt_keyboard_keycode_to_modmask(e->xkey.keycode);
    }

    if (e->type == KeyPress) {
        /* Escape cancels no matter what */
        if (ob_keycode_match(e->xkey.keycode, OB_KEY_ESCAPE)) {
            return FALSE;
        }

        /* There were no modifiers and they pressed enter */
        else if (ob_keycode_match(e->xkey.keycode, OB_KEY_RETURN) &&
                 !initial_state)
        {
            return FALSE;
        }
    }
    /* They released the modifiers */
    else if (e->type == KeyRelease && initial_state && !(mods & initial_state))
    {
        return FALSE;
    }

    return TRUE;
}

static gboolean i_pre_func(guint initial_state, gpointer options)
{
    if (!initial_state) {
        Options *o = options;
        o->interactive = FALSE;
        return FALSE;
    }
    else {
        screen_show_desktop_popup(screen_desktop, TRUE);
        return TRUE;
    }
}

static void i_post_func(gpointer options)
{
    screen_hide_desktop_popup();
}

/* 3.4-compatilibity */
static gpointer setup_follow(xmlNodePtr node)
{
    xmlNodePtr n;
    Options *o = g_new0(Options, 1);
    o->send = TRUE;
    o->follow = TRUE;
    if ((n = obt_xml_find_node(node, "follow")))
        o->follow = obt_xml_node_bool(n);
    return o;
}

static gpointer setup_go_last_func(xmlNodePtr node)
{
    Options *o = g_new0(Options, 1);
    o->type = LAST;
    return o;
}

static gpointer setup_send_last_func(xmlNodePtr node)
{
    Options *o = setup_follow(node);
    o->type = LAST;
    return o;
}

static gpointer setup_go_abs_func(xmlNodePtr node)
{
    xmlNodePtr n;
    Options *o = g_new0(Options, 1);
    o->type = ABSOLUTE;
    if ((n = obt_xml_find_node(node, "desktop")))
        o->u.abs.desktop = obt_xml_node_int(n) - 1;
    else
        o->u.abs.desktop = screen_desktop;
    return o;
}

static gpointer setup_send_abs_func(xmlNodePtr node)
{
    xmlNodePtr n;
    Options *o = setup_follow(node);
    o->type = ABSOLUTE;
    if ((n = obt_xml_find_node(node, "desktop")))
        o->u.abs.desktop = obt_xml_node_int(n) - 1;
    else
        o->u.abs.desktop = screen_desktop;
    return o;
}

static void setup_rel(Options *o, xmlNodePtr node, gboolean lin,
                      ObDirection dir,
                      ObActionsIPreFunc *pre,
                      ObActionsIInputFunc *input,
                      ObActionsIPostFunc *post)
{
    xmlNodePtr n;

    o->type = RELATIVE;
    o->u.rel.linear = lin;
    o->u.rel.dir = dir;
    o->u.rel.wrap = TRUE;

    if ((n = obt_xml_find_node(node, "wrap")))
        o->u.rel.wrap = obt_xml_node_bool(n);

    if (input) {
        o->interactive = TRUE;
        *pre = i_pre_func;
        *input = i_input_func;
        *post = i_post_func;
    }
}

static gpointer setup_go_next_func(xmlNodePtr node,
                                   ObActionsIPreFunc *pre,
                                   ObActionsIInputFunc *input,
                                   ObActionsICancelFunc *cancel,
                                   ObActionsIPostFunc *post)
{
    Options *o = g_new0(Options, 1);
    setup_rel(o, node, TRUE, OB_DIRECTION_EAST, pre, input, post);
    return o;
}

static gpointer setup_send_next_func(xmlNodePtr node,
                                     ObActionsIPreFunc *pre,
                                     ObActionsIInputFunc *input,
                                     ObActionsICancelFunc *cancel,
                                     ObActionsIPostFunc *post)
{
    Options *o = setup_follow(node);
    setup_rel(o, node, TRUE, OB_DIRECTION_EAST,
              pre, (o->follow ? input : NULL), post);
    return o;
}

static gpointer setup_go_prev_func(xmlNodePtr node,
                                   ObActionsIPreFunc *pre,
                                   ObActionsIInputFunc *input,
                                   ObActionsICancelFunc *cancel,
                                   ObActionsIPostFunc *post)
{
    Options *o = g_new0(Options, 1);
    setup_rel(o, node, TRUE, OB_DIRECTION_WEST, pre, input, post);
    return o;
}

static gpointer setup_send_prev_func(xmlNodePtr node,
                                     ObActionsIPreFunc *pre,
                                     ObActionsIInputFunc *input,
                                     ObActionsICancelFunc *cancel,
                                     ObActionsIPostFunc *post)
{
    Options *o = setup_follow(node);
    setup_rel(o, node, TRUE, OB_DIRECTION_WEST,
              pre, (o->follow ? input : NULL), post);
    return o;
}

static gpointer setup_go_left_func(xmlNodePtr node,
                                   ObActionsIPreFunc *pre,
                                   ObActionsIInputFunc *input,
                                   ObActionsICancelFunc *cancel,
                                   ObActionsIPostFunc *post)
{
    Options *o = g_new0(Options, 1);
    setup_rel(o, node, FALSE, OB_DIRECTION_WEST, pre, input, post);
    return o;
}

static gpointer setup_send_left_func(xmlNodePtr node,
                                     ObActionsIPreFunc *pre,
                                     ObActionsIInputFunc *input,
                                     ObActionsICancelFunc *cancel,
                                     ObActionsIPostFunc *post)
{
    Options *o = setup_follow(node);
    setup_rel(o, node, FALSE, OB_DIRECTION_WEST,
              pre, (o->follow ? input : NULL), post);
    return o;
}

static gpointer setup_go_right_func(xmlNodePtr node,
                                    ObActionsIPreFunc *pre,
                                    ObActionsIInputFunc *input,
                                    ObActionsICancelFunc *cancel,
                                    ObActionsIPostFunc *post)
{
    Options *o = g_new0(Options, 1);
    setup_rel(o, node, FALSE, OB_DIRECTION_EAST, pre, input, post);
    return o;
}

static gpointer setup_send_right_func(xmlNodePtr node,
                                      ObActionsIPreFunc *pre,
                                      ObActionsIInputFunc *input,
                                      ObActionsICancelFunc *cancel,
                                      ObActionsIPostFunc *post)
{
    Options *o = setup_follow(node);
    setup_rel(o, node, FALSE, OB_DIRECTION_EAST,
              pre, (o->follow ? input : NULL), post);
    return o;
}

static gpointer setup_go_up_func(xmlNodePtr node,
                                 ObActionsIPreFunc *pre,
                                 ObActionsIInputFunc *input,
                                 ObActionsICancelFunc *cancel,
                                 ObActionsIPostFunc *post)
{
    Options *o = g_new0(Options, 1);
    setup_rel(o, node, FALSE, OB_DIRECTION_NORTH, pre, input, post);
    return o;
}

static gpointer setup_send_up_func(xmlNodePtr node,
                                   ObActionsIPreFunc *pre,
                                   ObActionsIInputFunc *input,
                                   ObActionsICancelFunc *cancel,
                                   ObActionsIPostFunc *post)
{
    Options *o = setup_follow(node);
    setup_rel(o, node, FALSE, OB_DIRECTION_NORTH,
              pre, (o->follow ? input : NULL), post);
    return o;
}

static gpointer setup_go_down_func(xmlNodePtr node,
                                   ObActionsIPreFunc *pre,
                                   ObActionsIInputFunc *input,
                                   ObActionsICancelFunc *cancel,
                                   ObActionsIPostFunc *post)
{
    Options *o = g_new0(Options, 1);
    setup_rel(o, node, FALSE, OB_DIRECTION_SOUTH, pre, input, post);
    return o;
}

static gpointer setup_send_down_func(xmlNodePtr node,
                                     ObActionsIPreFunc *pre,
                                     ObActionsIInputFunc *input,
                                     ObActionsICancelFunc *cancel,
                                     ObActionsIPostFunc *post)
{
    Options *o = setup_follow(node);
    setup_rel(o, node, FALSE, OB_DIRECTION_SOUTH,
              pre, (o->follow ? input : NULL), post);
    return o;
}