forked from jordansissel/xdotool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_windowsize.c
137 lines (116 loc) · 3.68 KB
/
cmd_windowsize.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
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
#include "xdo_cmd.h"
#include <string.h>
int cmd_windowsize(context_t *context) {
int ret = 0;
unsigned int width, height;
int is_width_percent = 0, is_height_percent = 0;
int c;
int opsync = 0;
int use_hints = 0;
enum { opt_unused, opt_help, opt_usehints, opt_sync };
struct option longopts[] = {
{ "usehints", 0, NULL, opt_usehints },
{ "help", no_argument, NULL, opt_help },
{ "sync", no_argument, NULL, opt_sync },
{ 0, 0, 0, 0 },
};
int size_flags = 0;
char *cmd = *context->argv;
int option_index;
static const char *usage =
"Usage: %s [--sync] [--usehints] [window=%1] width height\n"
HELP_SEE_WINDOW_STACK
"--usehints - Use window sizing hints (like font size in terminals)\n"
"--sync - only exit once the window has resized\n";
while ((c = getopt_long_only(context->argc, context->argv, "+uh",
longopts, &option_index)) != -1) {
switch (c) {
case 'h':
case opt_help:
printf(usage, cmd);
consume_args(context, context->argc);
return EXIT_SUCCESS;
case 'u':
case opt_usehints:
use_hints = 1;
break;
case opt_sync:
opsync = 1;
break;
default:
fprintf(stderr, usage, cmd);
return EXIT_FAILURE;
}
}
consume_args(context, optind);
const char *window_arg = "%1";
if (!window_get_arg(context, 2, 0, &window_arg)) {
fprintf(stderr, "Invalid argument count, got %d, expected %d\n",
3, context->argc);
fprintf(stderr, usage, cmd);
return EXIT_FAILURE;
}
/* Use percentage if given a percent. */
if (strchr(context->argv[0], '%')) {
is_width_percent = 1;
}
if (strchr(context->argv[1], '%')) {
is_height_percent = 1;
}
if (use_hints) {
if (!is_height_percent) {
size_flags |= SIZE_USEHINTS_Y;
}
if (!is_width_percent) {
size_flags |= SIZE_USEHINTS_X;
}
}
width = (unsigned int)strtoul(context->argv[0], NULL, 0);
height = (unsigned int)strtoul(context->argv[1], NULL, 0);
consume_args(context, 2);
XWindowAttributes wattr;
unsigned int original_w, original_h;
unsigned int root_w, root_h; /* for percent */
window_each(context, window_arg, {
if (is_width_percent || is_height_percent) {
Window root = 0;
XGetWindowAttributes(context->xdo->xdpy, window, &wattr);
root = wattr.root;
xdo_get_window_size(context->xdo, root, &root_w, &root_h);
if (is_width_percent) {
width = (root_w * width / 100);
}
if (is_height_percent) {
height = (root_h * height / 100);
}
}
if (opsync) {
unsigned int w = width;
unsigned int h = height;
xdo_get_window_size(context->xdo, window, &original_w, &original_h);
if (size_flags & SIZE_USEHINTS_X) {
xdo_translate_window_with_sizehint(context->xdo, window, w, h, &w, NULL);
}
if (size_flags & SIZE_USEHINTS_Y) {
xdo_translate_window_with_sizehint(context->xdo, window, w, h, NULL, &h);
}
if (original_w == w && original_h == h) {
/* Skip, this window doesn't need to move. */
break;
}
}
ret = xdo_set_window_size(context->xdo, window, width, height, size_flags);
if (ret) {
fprintf(stderr, "xdo_set_window_size on window:%ld reported an error\n",
window);
return ret;
}
if (opsync) {
//xdo_wait_for_window_size(context->xdo, window, width, height, 0,
//SIZE_TO);
xdo_wait_for_window_size(context->xdo, window, original_w, original_h, 0,
SIZE_FROM);
}
}); /* window_each(...) */
return ret;
}