-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaemon.c
201 lines (174 loc) · 4.41 KB
/
daemon.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
/* Copyright 2014-2015 Drew Thoreson
*
* 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 3 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, see <http://www.gnu.org/licenses/>
*/
#define _XOPEN_SOURCE 500
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <getopt.h>
#include "gpio.h"
#include "snes.h"
#include "uinput.h"
#include "config.h"
static int uinput_fd = -1;
static bool foreground = false;
/* Die, printing an error message */
static void derror(const char *msg)
{
perror(msg);
exit(EXIT_FAILURE);
}
static void sigkill_handler(int signo)
{
for (unsigned i = 0; i < NR_CONTROLLERS; i++)
gpio_write(controller[i].clock, GPIO_LOW);
gpio_fini();
uinput_fini(uinput_fd);
exit(EXIT_SUCCESS);
}
static void init_all(void)
{
struct sigaction kill_action;
if (!gpio_init())
derror("gpio_init");
if ((uinput_fd = uinput_init()) < 0)
derror("uinput_init");
// clean exit on SIGINT/SIGTERM
sigemptyset(&kill_action.sa_mask);
kill_action.sa_handler = sigkill_handler;
kill_action.sa_flags = 0;
if (sigaction(SIGINT, &kill_action, NULL) < 0)
derror("sigaction");
if (sigaction(SIGTERM, &kill_action, NULL) < 0)
derror("sigaction");
}
static void daemonize(void)
{
pid_t pid;
if ((pid = fork()) < 0)
derror("fork");
if (pid > 0)
_exit(EXIT_SUCCESS);
if (setsid() < 0)
derror("setsid");
if ((pid = fork()) < 0)
derror("fork");
if (pid > 0)
_exit(EXIT_SUCCESS);
if (chdir("/") < 0)
derror("chdir");
umask(0);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
static void usage(int status)
{
#define uputs(str) fprintf(stderr, str "\n")
uputs("Usage: usnes [option]...");
uputs("Options:");
uputs("\t-f,--foreground Run the driver as a foreground process");
uputs("\t-h,--help Display this message and exit");
exit(status);
}
static void parse_opts(int argc, char *argv[])
{
for (;;) {
static struct option long_options[] = {
{ "foreground", no_argument, 0, 'f' },
{ "help", no_argument, 0, 'h' },
{ 0, 0, 0, 0 }
};
int options_index = 0;
int c = getopt_long(argc, argv, "fh", long_options, &options_index);
if (c < 0)
break;
switch (c) {
case 'f':
foreground = true;
break;
case 'h':
usage(EXIT_SUCCESS);
case '?':
break;
default:
usage(EXIT_FAILURE);
}
}
}
static snes_controller_t handle[NR_CONTROLLERS];
static snes_state_t prev_state[NR_CONTROLLERS];
static snes_state_t state[NR_CONTROLLERS];
static int init_controllers(void)
{
bool shared_clock = true;
bool shared_latch = true;
for (unsigned i = 0; i < NR_CONTROLLERS; i++) {
if (controller[i].clock != controller[0].clock)
shared_clock = false;
if (controller[i].latch != controller[0].latch)
shared_latch = false;
handle[i] = snes_open(
controller[i].clock,
controller[i].latch,
controller[i].data);
prev_state[i] = state[i] = snes_state_empty();
}
return (shared_clock ? SNES_SHARED_CLOCK : 0)
| (shared_latch ? SNES_SHARED_LATCH : 0);
}
static void do_event(unsigned long key, int value)
{
if (uinput_key_event(uinput_fd, key, value) < 0)
perror("key_event");
if (uinput_syn_event(uinput_fd) < 0)
perror("syn_event");
}
static void handle_events(void)
{
for (unsigned b = 0; b < SNES_NR_BUTTONS; b++) {
for (unsigned c = 0; c < NR_CONTROLLERS; c++) {
if (snes_state_changed(prev_state[c], state[c], b)) {
do_event(controller[c].keymap[b],
snes_button_pressed(state[c], b));
}
}
}
}
int main(int argc, char *argv[])
{
int flags;
parse_opts(argc, argv);
printf("Starting usnes... ");
fflush(stdout);
init_all();
printf("OK\n");
if (!foreground)
daemonize();
flags = init_controllers();
for (;;) {
// poll
snes_read_multi(NR_CONTROLLERS, handle, state, flags);
handle_events();
// wait
usleep(POLL_MS*1000);
for (unsigned i = 0; i < NR_CONTROLLERS; i++)
prev_state[i] = state[i];
}
}