-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui_x.c
667 lines (572 loc) · 18.6 KB
/
ui_x.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
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/* wmbright -- a brightness control using randr.
* Copyright (C) 2000, 2001
* Daniel Richard G. <skunk@mit.edu>,
* timecop <timecop@japan.co.jp>
* Copyright (C) 2019
* Johannes Holmberg, johannes@update.uu.se
*
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/extensions/shape.h>
#include <X11/xpm.h>
#include <X11/cursorfont.h>
#include <X11/extensions/Xrandr.h>
#include "include/master.xpm"
#include "include/led-on.xpm"
#include "include/led-off.xpm"
#include "include/common.h"
#include "include/misc.h"
#include "include/brightness.h"
#include "include/ui_x.h"
#include "include/config.h"
#ifndef PI
#define PI M_PI
#endif
#define LED_POS_RADIUS 13
#define KNOB_CENTER_X 41
#define KNOB_CENTER_Y 39
#define LED_WIDTH 6
#define LED_HEIGHT 6
struct osd {
RRCrtc crtc;
Window win;
GC gc;
int width;
int x;
int y;
bool mapped;
bool on;
int bar;
};
struct dockapp {
int width;
int height;
Pixmap pixmap;
Pixmap mask;
GC gc;
int ctlength;
int osd_count;
struct osd *osd;
};
static Pixmap led_on_pixmap;
static Pixmap led_on_mask;
static Pixmap led_off_pixmap;
static Pixmap led_off_mask;
#define copy_xpm_area(x, y, w, h, dx, dy) \
XCopyArea(display, dockapp.pixmap, dockapp.pixmap, dockapp.gc, \
x, y, w, h, dx, dy)
/* local prototypes */
static Cursor create_null_cursor(Display *x_display);
/* ui stuff */
static void draw_leds(void);
static void draw_percent(void);
static void draw_knob(float level);
/* global variables */
static struct dockapp dockapp;
static Display *display;
static Window win;
static Window iconwin;
static Cursor hand_cursor;
static Cursor null_cursor;
static Cursor norm_cursor;
static Cursor bar_cursor;
/* public methods */
void dockapp_init(Display *x_display)
{
display = x_display;
}
void redraw_window(void)
{
XCopyArea(display, dockapp.pixmap, iconwin, dockapp.gc,
0, 0, dockapp.width, dockapp.height, 0, 0);
XCopyArea(display, dockapp.pixmap, win, dockapp.gc,
0, 0, dockapp.width, dockapp.height, 0, 0);
}
void ui_update(void)
{
draw_leds();
draw_knob(brightness_get_level(-1));
redraw_window();
}
void knob_turn(float delta)
{
brightness_set_level_rel(delta);
draw_knob(brightness_get_level(-1));
redraw_window();
}
int blit_string(const char *text)
{
register int i;
register int c;
register int k;
k = 0;
copy_xpm_area(0, 87, 256, 9, 0, 96);
for (i = 0; text[i] || i > 31; i++) {
c = toupper(text[i]);
if (c == '-') {
copy_xpm_area(60, 67, 6, 8, k, 96);
k += 6;
}
if (c == ' ') {
copy_xpm_area(66, 67, 6, 8, k, 96);
k += 6;
}
if (c == '.') {
copy_xpm_area(72, 67, 6, 8, k, 96);
k += 6;
}
if (c >= 'A' && c <= 'Z') { /* letter */
c = c - 'A';
copy_xpm_area(c * 6, 77, 6, 8, k, 96);
k += 6;
} else if (c >= '0' && c <= '9') { /* number */
c = c - '0';
copy_xpm_area(c * 6, 67, 6, 8, k, 96);
k += 6;
}
}
dockapp.ctlength = k;
return k;
}
void scroll_text(int x, int y, int width, int chars, bool reset)
{
static int wait;
static int pos;
static int stop;
/* no text scrolling at all */
if (!config.scrolltext || (chars * 7 <= width)) {
if (!reset)
return;
copy_xpm_area(0, 96, 58, 9, x, y);
redraw_window();
return;
}
if (reset) {
wait = 10;
pos = 0;
stop = 0;
copy_xpm_area(0, 87, width, 9, x, y);
}
if (stop) {
return;
}
if (pos < -(dockapp.ctlength)) {
pos = width;
wait = 30;
return;
}
if (wait > 0) {
wait--;
} else {
pos -= 2;
if (pos == 0 || pos == 1) {
wait = 10;
}
}
if (pos > 0) {
copy_xpm_area(0, 87, pos, 9, x, y); /* clear */
copy_xpm_area(0, 96, width - pos, 9, x + pos, y);
} else { /* don't need to clear, already in text */
copy_xpm_area(abs(pos), 96, width, 9, x, y);
}
redraw_window();
return;
}
void new_window(char *name, int width, int height)
{
XpmAttributes attr;
Pixel fg, bg;
XGCValues gcval;
XSizeHints sizehints;
XClassHint classhint;
XWMHints wmhints;
XTextProperty wname;
dockapp.width = width;
dockapp.height = height;
sizehints.flags = USSize | USPosition;
sizehints.x = 0;
sizehints.y = 0;
sizehints.width = width;
sizehints.height = height;
fg = BlackPixel(display, DefaultScreen(display));
bg = WhitePixel(display, DefaultScreen(display));
win = XCreateSimpleWindow(display, DefaultRootWindow(display),
sizehints.x, sizehints.y,
sizehints.width, sizehints.height, 1, fg, bg);
iconwin = XCreateSimpleWindow(display, win, sizehints.x, sizehints.y,
sizehints.width, sizehints.height, 1, fg, bg);
XSetWMNormalHints(display, win, &sizehints);
classhint.res_name = name;
classhint.res_class = name;
XSetClassHint(display, win, &classhint);
#define INPUT_MASK \
ButtonPressMask \
| ExposureMask \
| ButtonReleaseMask \
| PointerMotionMask \
| LeaveWindowMask \
| StructureNotifyMask
XSelectInput(display, win, INPUT_MASK);
XSelectInput(display, iconwin, INPUT_MASK);
#undef INPUT_MASK
XStringListToTextProperty(&name, 1, &wname);
XSetWMName(display, win, &wname);
gcval.foreground = fg;
gcval.background = bg;
gcval.graphics_exposures = 0;
dockapp.gc =
XCreateGC(display, win, GCForeground | GCBackground | GCGraphicsExposures, &gcval);
attr.exactColors = 0;
attr.alloc_close_colors = 1;
attr.closeness = 30000;
attr.valuemask = (XpmExactColors | XpmAllocCloseColors | XpmCloseness);
if ((XpmCreatePixmapFromData(display, DefaultRootWindow(display),
master_xpm, &dockapp.pixmap, &dockapp.mask,
&attr) != XpmSuccess) ||
(XpmCreatePixmapFromData(display, DefaultRootWindow(display),
led_on_xpm, &led_on_pixmap, &led_on_mask,
&attr) != XpmSuccess) ||
(XpmCreatePixmapFromData(display, DefaultRootWindow(display),
led_off_xpm, &led_off_pixmap, &led_off_mask,
&attr) != XpmSuccess)) {
fputs("Cannot allocate colors for the dockapp pixmaps!\n", stderr);
exit(EXIT_FAILURE);
}
XShapeCombineMask(display, win, ShapeBounding, 0, 0, dockapp.mask, ShapeSet);
XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, dockapp.mask, ShapeSet);
wmhints.initial_state = WithdrawnState;
wmhints.icon_window = iconwin;
wmhints.icon_x = sizehints.x;
wmhints.icon_y = sizehints.y;
wmhints.window_group = win;
wmhints.flags =
StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
XSetWMHints(display, win, &wmhints);
hand_cursor = XCreateFontCursor(display, XC_hand2);
norm_cursor = XCreateFontCursor(display, XC_left_ptr);
bar_cursor = XCreateFontCursor(display, XC_sb_up_arrow);
null_cursor = create_null_cursor(display);
XMapWindow(display, win);
}
void destroy_osd()
{
for (int i = 0; i < dockapp.osd_count; i++) {
XFreeGC(display, dockapp.osd[i].gc);
XDestroyWindow(display, dockapp.osd[i].win);
}
}
void new_osd(int height)
{
Window osdwin;
Pixel fg = WhitePixel(display, DefaultScreen(display));
Pixel bg = BlackPixel(display, DefaultScreen(display));
XGCValues gcval;
GC gc;
XSizeHints sizehints;
XSetWindowAttributes xattributes;
int win_layer = 6;
XFontStruct *fs = NULL;
int width;
int x;
int y;
XRRScreenResources *screen = XRRGetScreenResources(display, DefaultRootWindow(display));
dockapp.osd_count = brightness_get_monitor_count();
dockapp.osd = (struct osd *)malloc(sizeof(struct osd) * dockapp.osd_count);
for (int i = 0; i < dockapp.osd_count; i++) {
dockapp.osd[i].crtc = screen->crtcs[i];
dockapp.osd[i].gc = 0;
dockapp.osd[i].win = 0;
}
/* -sony-fixed-medium-r-normal--24-170-100-100-c-120-iso8859-1
* -misc-fixed-medium-r-normal--36-*-75-75-c-*-iso8859-* */
/* try our cool scaled 36pt fixed font */
fs = XLoadQueryFont(display,
"-misc-fixed-medium-r-normal--36-*-75-75-c-*-iso8859-*");
if (fs == NULL) {
/* they don't have it! */
/* try our next preferred font (100dpi sony) */
fprintf(stderr, "Trying alternate font\n");
fs = XLoadQueryFont(display,
"-sony-fixed-medium-r-normal--24-*-100-100-c-*-iso8859-*");
/* they don't have the sony font either */
if (fs == NULL) {
fprintf(stderr, "Trying \"fixed\" font\n");
fs = XLoadQueryFont(display,
"fixed");
/* if they don't have the fixed font, we've got different kind
* of problems */
if (fs == NULL) {
fprintf(stderr, "Your X server is probably broken\n");
exit(1);
}
}
}
sizehints.flags = USSize | USPosition;
for (int i = 0; i < dockapp.osd_count; i++) {
dockapp.osd[i].mapped = false;
struct dimensions dim = brightness_get_dimensions(i+1);
if (dim.width == 0) {
dockapp.osd[i].on = false;
continue;
}
width = dim.width - 200;
x = dim.x + 100;
y = dim.y + dim.height - 120;
sizehints.x = x;
sizehints.y = y;
sizehints.width = width;
sizehints.height = height;
xattributes.save_under = True;
xattributes.override_redirect = True;
xattributes.cursor = None;
osdwin = XCreateSimpleWindow(display, DefaultRootWindow(display),
sizehints.x, sizehints.y, width, height,
0, fg, bg);
XSetWMNormalHints(display, osdwin, &sizehints);
XChangeWindowAttributes(display, osdwin, CWSaveUnder | CWOverrideRedirect,
&xattributes);
char name[5] = "osdx";
name[3] = '0' + i;
XStoreName(display, osdwin, name);
XSelectInput(display, osdwin, ExposureMask);
XChangeProperty(display, osdwin, XInternAtom(display, "_WIN_LAYER", False),
XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&win_layer, 1);
gcval.foreground = get_color(display, config.osd_color);
gcval.background = bg;
gcval.graphics_exposures = 0;
gc = XCreateGC(display, osdwin,
GCForeground | GCBackground | GCGraphicsExposures,
&gcval);
XSetFont(display, gc, fs->fid);
dockapp.osd[i].win = osdwin;
dockapp.osd[i].gc = gc;
dockapp.osd[i].width = width;
dockapp.osd[i].x = x;
dockapp.osd[i].y = y;
}
}
void update_osd_by_number(int osd, bool up)
{
int foo;
if (dockapp.osd[osd].on) {
float level = brightness_get_level(osd + 1);
int bar = dockapp.osd[osd].bar;
foo = (dockapp.osd[osd].width - 20) * level / 20.0;
if (up) {
for (int j = 1; j <= foo; j++)
XFillRectangle(display, dockapp.osd[osd].win, dockapp.osd[osd].gc,
j * 20, 30, 5, 25);
} else if (foo < bar) {
XClearArea(display, dockapp.osd[osd].win, ((foo+1) * 20), 30,
((bar-foo) * 20), 25, 1);
} else if (foo > bar) {
for (int j = (bar > 0 ? bar : 1); j <= foo; j++)
XFillRectangle(display, dockapp.osd[osd].win, dockapp.osd[osd].gc,
j * 20, 30, 5, 25);
}
dockapp.osd[osd].bar = foo;
}
}
void update_osd(bool up)
{
if (config.osd) {
if (brightness_get_current_monitor() == 0) { // Update all of them!
for (int i = 0; i < dockapp.osd_count; i++) {
if (dockapp.osd[i].on) {
update_osd_by_number(i, up);
}
}
} else {
update_osd_by_number(brightness_get_current_monitor()-1, up);
}
}
}
void unmap_osd(void)
{
if (config.osd) {
for (int i = 0; i < dockapp.osd_count; i++) {
if (dockapp.osd[i].mapped) {
XUnmapWindow(display, dockapp.osd[i].win);
XFlush(display);
dockapp.osd[i].mapped = false;
}
}
}
}
void map_osd_by_number(int osd) {
XMapRaised(display, dockapp.osd[osd].win);
XDrawString(display, dockapp.osd[osd].win, dockapp.osd[osd].gc, 1, 25,
brightness_get_method_name(osd+1), strlen(brightness_get_method_name(osd+1)));
update_osd(true);
XFlush(display);
dockapp.osd[osd].mapped = true;
}
void map_osd(void)
{
if (config.osd) {
if (brightness_get_current_monitor() == 0) { // Map all of them!
for (int i = 0; i < dockapp.osd_count; i++) {
if (dockapp.osd[i].on) {
map_osd_by_number(i);
}
}
} else {
map_osd_by_number(brightness_get_current_monitor()-1);
}
}
}
bool osd_mapped(void)
{
if (brightness_get_current_monitor() == 0) {
for (int i = 0; i < dockapp.osd_count; i++) {
if (dockapp.osd[i].mapped) {
return true;
}
}
return false;
} else {
return dockapp.osd[brightness_get_current_monitor()-1].mapped;
}
}
void set_cursor(int type)
{
static int oldtype;
if (oldtype == type)
return;
switch (type) {
case NULL_CURSOR:
XDefineCursor(display, win, null_cursor);
XDefineCursor(display, iconwin, null_cursor);
break;
case NORMAL_CURSOR:
XDefineCursor(display, win, norm_cursor);
XDefineCursor(display, iconwin, norm_cursor);
break;
case HAND_CURSOR:
XDefineCursor(display, win, hand_cursor);
XDefineCursor(display, iconwin, hand_cursor);
break;
case BAR_CURSOR:
XDefineCursor(display, win, bar_cursor);
XDefineCursor(display, iconwin, bar_cursor);
break;
}
oldtype = type;
}
static void draw_leds(void)
{
enum method method = brightness_get_method();
if (brightness_has_method(BACKLIGHT)) /* backlight exists */
if (method == BACKLIGHT)
copy_xpm_area(65, 0, 12, 7, 4, 42); /* BL lit */
else
copy_xpm_area(65, 7, 12, 7, 4, 42); /* BL not lit */
else /* backlight not available */
copy_xpm_area(65, 14, 12, 7, 4, 42); /* BL dark */
if (brightness_has_method(GAMMA)) /* backlight exists */
if (method == GAMMA)
copy_xpm_area(77, 0, 12, 7, 4, 33); /* BL lit */
else
copy_xpm_area(77, 7, 12, 7, 4, 33); /* BL not lit */
else /* backlight not available */
copy_xpm_area(77, 14, 12, 7, 4, 33); /* BL dark */
}
static void draw_percent(void)
{
int level = brightness_get_percent();
copy_xpm_area(0, 87, 15, 9, 44, 4); /* clear percentage */
if (level < 100) {
if (level >= 10)
copy_xpm_area((level / 10) * 6, 67, 6, 9, 47, 4);
copy_xpm_area((level % 10) * 6, 67, 6, 9, 53, 4);
} else {
copy_xpm_area(9, 67, 3, 9, 44, 4);
copy_xpm_area(0, 67, 6, 9, 47, 4);
copy_xpm_area(0, 67, 6, 9, 53, 4);
}
}
static void draw_knob(float level)
{
float bearing, led_x, led_y;
int led_topleft_x, led_topleft_y;
Pixmap led_pixmap;
bearing = (1.25 * PI) - (1.5 * PI) * level;
led_x = KNOB_CENTER_X + LED_POS_RADIUS * cos(bearing);
led_y = KNOB_CENTER_Y - LED_POS_RADIUS * sin(bearing);
led_topleft_x = (int)(led_x - (LED_WIDTH / 2.0) + 0.5);
led_topleft_y = (int)(led_y - (LED_HEIGHT / 2.0) + 0.5);
/* clear previous knob picture */
copy_xpm_area(101, 0, 42, 42, 20, 18);
led_pixmap = led_on_pixmap;
XCopyArea(display, led_pixmap, dockapp.pixmap, dockapp.gc,
0, 0, LED_WIDTH, LED_HEIGHT, led_topleft_x, led_topleft_y);
draw_percent();
}
static Cursor create_null_cursor(Display *x_display)
{
Pixmap cursor_mask;
XGCValues gcval;
GC gc;
XColor dummy_color;
Cursor cursor;
cursor_mask = XCreatePixmap(x_display, DefaultRootWindow(x_display), 1, 1, 1);
gcval.function = GXclear;
gc = XCreateGC(x_display, cursor_mask, GCFunction, &gcval);
XFillRectangle(x_display, cursor_mask, gc, 0, 0, 1, 1);
dummy_color.pixel = 0;
dummy_color.red = 0;
dummy_color.flags = 04;
cursor = XCreatePixmapCursor(x_display, cursor_mask, cursor_mask,
&dummy_color, &dummy_color, 0, 0);
XFreePixmap(x_display, cursor_mask);
XFreeGC(x_display, gc);
return cursor;
}
unsigned long get_color(Display *display, char *color_name)
{
XColor color;
XWindowAttributes winattr;
Status status;
XGetWindowAttributes(display, RootWindow(display, DefaultScreen(display)), &winattr);
status = XParseColor(display, winattr.colormap, color_name, &color);
if (status == 0) {
fprintf(stderr, "wmbright:warning: Could not get color \"%s\" for OSD, falling back to default\n", color_name);
if (color_name != default_osd_color)
status = XParseColor(display, winattr.colormap, default_osd_color, &color);
if (status == 0)
return WhitePixel(display, DefaultScreen(display));
}
color.flags = DoRed | DoGreen | DoBlue;
XAllocColor(display, winattr.colormap, &color);
return color.pixel;
}
void ui_rrnotify()
{
destroy_osd();
new_osd(60);
ui_update();
}