File: aedesk.c

package info (click to toggle)
aewm 1.3.12-3
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 356 kB
  • sloc: ansic: 2,557; makefile: 77; sh: 20
file content (69 lines) | stat: -rw-r--r-- 1,764 bytes parent folder | download | duplicates (3)
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
/* aewm - Copyright 1998-2007 Decklin Foster <decklin@red-bean.com>.
 * This program is free software; please see LICENSE for details. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include "common.h"
#include "atom.h"

#define UMOD(x, y) ((((long)(x) % (long)(y)) + (y)) % (y))

Display *dpy;
Window root;
Atom net_cur_desk, net_num_desks;

static unsigned long parse_desk(char *spec);

int main(int argc, char **argv)
{
    unsigned long desk;
    int i;

    if (argc < 2) {
        fprintf(stderr, "usage: aedesk [+-]<integer>|-n <integer>\n");
        exit(2);
    }

    dpy = XOpenDisplay(NULL);
    root = DefaultRootWindow(dpy);
    net_cur_desk = XInternAtom(dpy, "_NET_CURRENT_DESKTOP", False);
    net_num_desks = XInternAtom(dpy, "_NET_NUMBER_OF_DESKTOPS", False);

    if (!dpy) {
        fprintf(stderr, "aedesk: can't open display %s\n", getenv("DISPLAY"));
        exit(1);
    }

    for (i = 1; i < argc; i++) {
        if ARG("setn", "n", 1) {
            desk = atol(argv[++i]);
            set_atoms(root, net_num_desks, XA_CARDINAL, &desk, 1);
        } else {
            desk = parse_desk(argv[i]);
            send_xmessage(root, net_cur_desk, desk, SubstructureNotifyMask);
        }
    }

    XCloseDisplay(dpy);
    return 0;
}

unsigned long parse_desk(char *spec)
{
    unsigned long ndesks, cur_desk;

    if (strchr("+-", spec[0])) {
        if (get_atoms(root, net_cur_desk, XA_CARDINAL, 0, &cur_desk, 1, NULL)
                && get_atoms(root, net_num_desks, XA_CARDINAL, 0, &ndesks, 1,
                NULL)) {
            return UMOD(cur_desk + atol(spec), ndesks);
        } else {
            return 0;
        }
    } else {
        return atol(spec);
    }
}