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
|
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <X11/Xlib.h>
#include <sys/types.h>
#include <X11/XKBlib.h>
/*
* maximum number of arguemnts we will try and call when
* launching the usder defined program
*/
#define ARG_LIMIT 20
#define BUFF_SIZE 512
char *program = "sh -c /home/daniel_j/programming/bash/beep.sh";
Display *display;
XEvent xevent;
XkbEvent *xkbevent;
int xkbeventcode; /* stores the event code for the xkb extension */
int
forkexecute()
{
pid_t pid = fork();
if (pid == 0)
{
// child process, we don't want to ignore signals
signal(SIGCHLD, SIG_DFL);
/*
* we don't want std{out,err} to be associated with the terminal,
* but we also don't want to close it to avoid the file descriptors
* being re-used potentially leading to problems, so reopen them to /dev/null
*/
freopen("/dev/null", "w", stdout);
freopen("/dev/null", "w", stderr);
char *args[ARG_LIMIT];
char *buff = malloc(BUFF_SIZE);
if (buff == NULL)
{
perror("malloc");
return -1;
}
/*
* the program we're calling might have arguments,
* so we tokenise the string and add each part to an array
* that we will use in execvp
*/
strncpy(buff, program, BUFF_SIZE-1);
char *t = strtok(buff, " ");
int z = 0;
while (t != NULL && z < ARG_LIMIT-1) // save a position for NULL
{
args[z] = t;
t = strtok(NULL, " ");
z++;
}
args[z+1] = (char *)0;
execvp(args[0], args);
_exit(1);
}
else if (pid == -1)
{
perror("fork");
return -1;
}
return 1;
}
void
bellevent(void)
{
/*
* a bell event was caught, execute the user defined program
*/
int ret = forkexecute();
if (ret != 1)
fprintf(stderr, "forkexecute() failed");
}
int
main(void)
{
// ignore child processes
signal(SIGCHLD, SIG_IGN);
// ignore return values for now
display = XkbOpenDisplay(NULL, &xkbeventcode, NULL, NULL, NULL, NULL);
if (!display)
{
fprintf(stderr, "Cannot open display.\n");
exit(1);
}
XkbSelectEvents(display, XkbUseCoreKbd, XkbBellNotifyMask, XkbBellNotifyMask);
while (1)
{
XNextEvent(display, &xevent);
if (xevent.type == xkbeventcode)
{
xkbevent = (XkbEvent *)&xevent;
if (xkbevent->any.xkb_type == XkbBellNotify)
{
bellevent();
}
}
}
return 0;
}
|