-
Notifications
You must be signed in to change notification settings - Fork 0
/
soundkeys.c
93 lines (77 loc) · 1.65 KB
/
soundkeys.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
/* soundkeys.c */
/*
* Fed up with the sound keys not working, so attempting to fix it.
*/
#include <stdio.h>
#include <X11/Xlib.h>
#include <stdlib.h>
#include <X11/XF86keysym.h>
#ifndef DEFAULT_VOLUME
#define DEFAULT_VOLUME (0x80);
#endif
void handlekeypress(Display *dpy, XEvent *ev)
{
static int vol = DEFAULT_VOLUME;
static int muted = 0;
char cmd[128];
KeySym ks;
int svol;
ks = XKeycodeToKeysym(dpy, ev->xkey.keycode, 0);
if (muted) {
svol = vol;
muted = 0;
} else {
switch (ks) {
case XF86XK_AudioMute:
svol = 0;
muted = 1;
break;
case XF86XK_AudioLowerVolume:
svol = vol -= 10;
break;
case XF86XK_AudioRaiseVolume:
svol = vol += 10;
break;
default:
return;
}
}
snprintf(cmd, sizeof(cmd), "amixer set PCM %d", svol);
if (vol < 0) {
vol = 0;
return;
} else if (vol > 255) {
vol = 255;
return;
}
system(cmd);
}
int main(void)
{
Display *display;
Window root;
XEvent ev;
KeyCode kc;
display = XOpenDisplay(getenv("DISPLAY"));
if (!display)
exit(1);
root = RootWindow(display, 0);
XSelectInput(display, root, KeyPressMask);
kc = XKeysymToKeycode(display, XF86XK_AudioLowerVolume);
XGrabKey(display, kc, AnyModifier, root, True, /* lower */
GrabModeAsync, GrabModeAsync);
kc = XKeysymToKeycode(display, XF86XK_AudioMute);
XGrabKey(display, kc, AnyModifier, root, True, /* mute */
GrabModeAsync, GrabModeAsync);
kc = XKeysymToKeycode(display, XF86XK_AudioRaiseVolume);
XGrabKey(display, kc, AnyModifier, root, True, /* raise */
GrabModeAsync, GrabModeAsync);
for (;;) {
XNextEvent(display, &ev);
switch (ev.type) {
case KeyPress:
handlekeypress(display, &ev);
break;
}
}
}