-
Notifications
You must be signed in to change notification settings - Fork 321
/
cmd_set_desktop.c
64 lines (51 loc) · 1.55 KB
/
cmd_set_desktop.c
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
#include "xdo_cmd.h"
int cmd_set_desktop(context_t *context) {
char *cmd = *context->argv;
long desktop;
int relative = False;
int c;
enum { opt_unused, opt_help, opt_relative };
static struct option longopts[] = {
{ "help", no_argument, NULL, opt_help },
{ "relative", no_argument, NULL, opt_relative },
{ 0, 0, 0, 0 },
};
static const char *usage =
"Usage: %s desktop\n"
"--relative - Move relative to the current desktop. Negative values OK\n";
int option_index;
while ((c = getopt_long_only(context->argc, context->argv, "+h",
longopts, &option_index)) != -1) {
switch (c) {
case 'h':
case opt_help:
printf(usage, cmd);
consume_args(context, context->argc);
return EXIT_SUCCESS;
break;
case opt_relative:
relative = True;
break;
default:
fprintf(stderr, usage, cmd);
return EXIT_FAILURE;
}
}
consume_args(context, optind);
if (context->argc < 1) {
fprintf(stderr, usage, cmd);
return 1;
}
desktop = strtol(context->argv[0], NULL, 0);
consume_args(context, 1);
if (relative == True) {
long cur_desktop = 0, ndesktops = 0;
xdo_get_current_desktop(context->xdo, &cur_desktop);
xdo_get_number_of_desktops(context->xdo, &ndesktops);
desktop = (desktop + cur_desktop) % ndesktops;
/* negative mod doesn't result in a positive number. Fix that. */
if (desktop < 0)
desktop += ndesktops;
}
return xdo_set_current_desktop(context->xdo, desktop);
}