-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwmbright.c
398 lines (357 loc) · 11.5 KB
/
wmbright.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
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
/* wmbright -- a brightness control using randr.
* Copyright (C) 2000, 2001 [email protected]
* Mixer code in version 3.0 based on mixer api library by
* Daniel Richard G. <[email protected]>, which in turn was based on
* the mixer code in WMix 2.x releases.
* Copyright (C) 2019 Johannes Holmberg, [email protected]
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <string.h>
#include <getopt.h>
#include <unistd.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/Xrandr.h>
#include "include/common.h"
#include "include/misc.h"
#include "include/ui_x.h"
#include "include/mmkeys.h"
#include "include/config.h"
#include "include/brightness.h"
static Display *display;
static bool button_pressed = false;
static bool slider_pressed = false;
static double prev_button_press_time = 0.0;
static float display_height;
static float display_width;
static int mouse_drag_home_x;
static int mouse_drag_home_y;
static int idle_loop;
static int msg_length;
/* local stuff */
static void signal_catch(int sig);
static void button_press_event(XButtonEvent *event);
static void button_release_event(XButtonEvent *event);
static int key_press_event(XKeyEvent *event);
static void motion_event(XMotionEvent *event);
int main(int argc, char **argv)
{
XEvent event;
int rr_event_base, rr_error_base;
bool need_reinit = false;
config_init();
parse_cli_options(argc, argv);
config_read();
XInitThreads();
display = XOpenDisplay(config.display_name);
if (display == NULL) {
const char *name;
if (config.display_name) {
name = config.display_name;
} else {
name = getenv("DISPLAY");
if (name == NULL) {
fprintf(stderr, "wbright:error: Unable to open display, variable $DISPLAY not set\n");
return EXIT_FAILURE;
}
}
fprintf(stderr, "wmbright:error: Unable to open display \"%s\"\n", name);
return EXIT_FAILURE;
}
if (!XRRQueryExtension(display, &rr_event_base, &rr_error_base)) {
fprintf(stderr, "wmbright:error: randr extension not found\n");
return EXIT_FAILURE;
}
int rr_mask = RROutputChangeNotifyMask; //RRScreenChangeNotifyMask;
XRRSelectInput(display,
RootWindow(display, DefaultScreen(display)),
rr_mask);
brightness_init(display, config.verbose, (const char **)config.exclude_output);
display_width = (float)DisplayWidth(display, DefaultScreen(display)) / 4.0;
display_height = (float)DisplayHeight(display, DefaultScreen(display)) / 2.0;
dockapp_init(display);
new_window("wmbright", 64, 64);
new_osd(60);
if (config.mmkeys)
mmkey_install(display);
config_release();
blit_string(brightness_get_monitor_name());
msg_length = strlen(brightness_get_monitor_name());
scroll_text(3, 4, 35, msg_length, true);
ui_update();
/* add click regions */
add_region(1, 20, 18, 42, 42); /* knob */
add_region(2, 3, 41, 14, 9); /* backlight indicator */
add_region(3, 3, 32, 14, 9); /* gamma indicator */
add_region(8, 3, 50, 7, 10); /* previous channel */
add_region(9, 10, 50, 7, 10); /* next channel */
add_region(10, 3, 4, 58, 11); /* re-scroll current channel name */
/* setup up/down signal handler */
create_pid_file();
signal(SIGUSR1, (void *) signal_catch);
signal(SIGUSR2, (void *) signal_catch);
while (true) {
if (button_pressed || slider_pressed || (XPending(display) > 0)) {
XNextEvent(display, &event);
switch (event.type) {
case KeyPress:
if (key_press_event(&event.xkey))
idle_loop = 0;
break;
case Expose:
redraw_window();
break;
case ButtonPress:
button_press_event(&event.xbutton);
idle_loop = 0;
break;
case ButtonRelease:
button_release_event(&event.xbutton);
idle_loop = 0;
break;
case MotionNotify:
/* process cursor change, or drag events */
motion_event(&event.xmotion);
idle_loop = 0;
break;
case LeaveNotify:
/* go back to standard cursor */
if ((!button_pressed) && (!slider_pressed))
set_cursor(NORMAL_CURSOR);
break;
case DestroyNotify:
XCloseDisplay(display);
return EXIT_SUCCESS;
default:
if (event.type == rr_event_base + RRNotify) {
XRRNotifyEvent *notify = (XRRNotifyEvent *)&event;
if (notify->subtype == RRNotify_OutputChange) {
if (config.verbose)
printf("Outputs changed, reconfiguring.\n");
need_reinit = true;
}
XRRUpdateConfiguration(&event);
}
break;
}
} else {
if (need_reinit) {
need_reinit = false;
brightness_reinit();
ui_rrnotify();
continue;
}
usleep(100000);
//brightness_tick();
scroll_text(3, 4, 35, msg_length, false);
/* get rid of OSD after a few seconds of idle */
if ((idle_loop++ > 15) && osd_mapped() && !button_pressed) {
unmap_osd();
idle_loop = -10000;
}
if (brightness_is_changed())
ui_update();
}
}
return EXIT_SUCCESS;
}
static void signal_catch(int sig)
{
switch (sig) {
case SIGUSR1:
printf("sigusr1\n");
brightness_set_level_rel(config.scrollstep);
if (!osd_mapped())
map_osd();
if (osd_mapped())
update_osd(false);
ui_update();
idle_loop = 0;
break;
case SIGUSR2:
printf("sigusr2\n");
brightness_set_level_rel(-config.scrollstep);
if (!osd_mapped())
map_osd();
if (osd_mapped())
update_osd(false);
ui_update();
idle_loop = 0;
break;
}
}
static void button_press_event(XButtonEvent *event)
{
double button_press_time = get_current_time();
int x = event->x;
int y = event->y;
//bool double_click = false;
/* handle wheel scrolling to adjust level */
if (config.mousewheel) {
if (event->button == config.wheel_button_up) {
brightness_ready();
brightness_set_level_rel(config.scrollstep);
brightness_unready();
if (!osd_mapped())
map_osd();
if (osd_mapped())
update_osd(false);
ui_update();
idle_loop = 0;
return;
}
if (event->button == config.wheel_button_down) {
brightness_ready();
brightness_set_level_rel(-config.scrollstep);
brightness_unready();
if (!osd_mapped())
map_osd();
if (osd_mapped())
update_osd(false);
ui_update();
idle_loop = 0;
return;
}
}
if ((button_press_time - prev_button_press_time) <= 0.5) {
//double_click = true;
prev_button_press_time = 0.0;
} else
prev_button_press_time = button_press_time;
switch (check_region(x, y)) {
case 1: /* on knob */
brightness_ready();
button_pressed = true;
slider_pressed = false;
mouse_drag_home_x = x;
mouse_drag_home_y = y;
break;
case 2: /* backlight indicator */
if (brightness_set_method(BACKLIGHT)) {
unmap_osd();
map_osd();
ui_update();
idle_loop = 0;
}
break;
case 3: /* gamma indicator */
if (brightness_set_method(GAMMA)) {
unmap_osd();
map_osd();
ui_update();
idle_loop = 0;
}
break;
case 8: /* previous monitor */
brightness_set_monitor_rel(-1);
blit_string(brightness_get_monitor_name());
msg_length = strlen(brightness_get_monitor_name());
scroll_text(3, 4, 35, msg_length, true);
unmap_osd();
map_osd();
ui_update();
idle_loop = 0;
break;
case 9: /* next monitor */
brightness_set_monitor_rel(1);
blit_string(brightness_get_monitor_name());
msg_length = strlen(brightness_get_monitor_name());
scroll_text(3, 4, 35, msg_length, true);
unmap_osd();
map_osd();
ui_update();
break;
case 10:
scroll_text(3, 4, 35, msg_length, true);
break;
default:
//printf("unknown region pressed\n");
break;
}
}
static int key_press_event(XKeyEvent *event)
{
if (event->keycode == mmkeys.brightness_up) {
brightness_set_level_rel(config.scrollstep);
if (!osd_mapped())
map_osd();
if (osd_mapped())
update_osd(false);
ui_update();
idle_loop = 0;
return 1;
}
if (event->keycode == mmkeys.brightness_down) {
brightness_set_level_rel(-config.scrollstep);
if (!osd_mapped())
map_osd();
if (osd_mapped())
update_osd(false);
ui_update();
idle_loop = 0;
return 1;
}
/* Ignore other keys */
return 0;
}
static void button_release_event(XButtonEvent *event)
{
int x = event->x;
int y = event->y;
int region;
region = check_region(x, y);
if (region == 1)
set_cursor(HAND_CURSOR);
if (button_pressed) {
brightness_unready();
}
button_pressed = false;
slider_pressed = false;
}
static void motion_event(XMotionEvent *event)
{
int x = event->x;
int y = event->y;
int region;
if ((x == mouse_drag_home_x) && (y == mouse_drag_home_y))
return;
region = check_region(x, y);
if (button_pressed) {
if (y != mouse_drag_home_y) {
float delta;
set_cursor(NULL_CURSOR);
delta = (float)(mouse_drag_home_y - y) / display_height;
knob_turn(delta);
if (!osd_mapped())
map_osd();
if (osd_mapped())
update_osd(false);
idle_loop = 0;
}
XWarpPointer(display, None, event->window, x, y, 0, 0,
mouse_drag_home_x, mouse_drag_home_y);
return;
}
if (region == 1)
set_cursor(HAND_CURSOR);
else
set_cursor(NORMAL_CURSOR);
}