summaryrefslogtreecommitdiffstats
path: root/execonkp/main.c
blob: fa1bae30751ae2c4c922bcdb805f7d37e054e106 (plain)
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
/*
 *   Helper to run programs on X11 keyboard events.
 *
 *   Copyright (C) 2016 Michael Buesch <m@bues.ch>
 *
 *   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.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>

#include <X11/Xlib.h>
#include <X11/extensions/XInput2.h>

#define PFX	"execonkp: "


static void handle_event(char * const * run_prog)
{
	printf(PFX "Key event detected. Running %s...\n", run_prog[0]);

	execv(run_prog[0], run_prog);
	fprintf(stderr, PFX "execv(\"%s\") failed: %s\n",
		run_prog[0], strerror(errno));
	exit(1);
}

static void signal_handler(int signal)
{
	printf(PFX "terminated\n");
	exit(0);
}

static int install_sighandler(int signal, void (*handler)(int))
{
	struct sigaction act;

	memset(&act, 0, sizeof(act));
	sigemptyset(&act.sa_mask);
	act.sa_flags = 0;
	act.sa_handler = handler;

	return sigaction(signal, &act, NULL);
}

static void usage(void)
{
	printf("Usage: execonkp [OPTIONS] COMMAND [ARGS]\n");
	printf("\n");
	printf("Options:\n");
	printf("  -p|--on-press     Run the command on key press.\n");
	printf("  -r|--on-release   Run the command on key release.\n");
	printf("\n");
	printf("By default only -p is set.\n");
	printf("Setting -r clears -p unless -p is also specified afterwards.\n");
}

int main(int argc, char **argv)
{
	Display *display;
	Window window;
	XIEventMask evmask;
	unsigned char evmask_bits[(XI_LASTEVENT + 8) / 8] = { 0, };
	int res, tmp0, tmp1, xi_opcode;
	sigset_t sigset;
	char * const * run_prog;
	int on_press = 1, on_release = 0;

	argc--;
	argv++;
	while (argc >= 1 && argv[0][0] == '-') {
		if (strcmp(argv[0], "-p") == 0 ||
		    strcmp(argv[0], "--on-press") == 0) {
			on_press = 1;
		} else if (strcmp(argv[0], "-r") == 0 ||
		    strcmp(argv[0], "--on-release") == 0) {
			on_press = 0;
			on_release = 1;
		} else {
			usage();
			return 1;
		}
		argc--;
		argv++;
	}
	if (argc < 1) {
		usage();
		return 1;
	}
	run_prog = argv;

	display = XOpenDisplay(NULL);
	if (!display) {
		/* Fallback to the first display. */
		display = XOpenDisplay(":0");
		if (!display) {
			fprintf(stderr, PFX "Failed to open DISPLAY\n");
			return 1;
		}
	}
	window = DefaultRootWindow(display);

	res = XQueryExtension(display, "XInputExtension", &xi_opcode, &tmp0, &tmp1);
	if (!res) {
		fprintf(stderr, PFX "X Input extension not available.\n");
		return 1;
	}

	sigemptyset(&sigset);
	res = sigprocmask(SIG_SETMASK, &sigset, NULL);
	res |= install_sighandler(SIGINT, signal_handler);
	res |= install_sighandler(SIGTERM, signal_handler);
	if (res) {
		fprintf(stderr, PFX "Failed to setup signal handlers\n");
		return 1;
	}

	if (on_press)
		XISetMask(evmask_bits, XI_KeyPress);
	if (on_release)
		XISetMask(evmask_bits, XI_KeyRelease);
	memset(&evmask, 0, sizeof(evmask));
	evmask.deviceid = XIAllDevices;
	evmask.mask_len = sizeof(evmask_bits);
	evmask.mask = evmask_bits;

	res = XISelectEvents(display, window, &evmask, 1);
	if (res != Success) {
		fprintf(stderr, PFX "Failed to set event mask (%d)\n", res);
		return 1;
	}

	printf(PFX "Waiting for keyboard event...\n");
	while (1) {
		XEvent ev;
		XGenericEventCookie *cookie = &ev.xcookie;

		XNextEvent(display, &ev);
		if (XGetEventData(display, cookie) &&
		    cookie->type == GenericEvent &&
		    cookie->extension == xi_opcode) {
			handle_event(run_prog);
		}
		XFreeEventData(display, cookie);
	}
}
bues.ch cgit interface