Ticket #15: daemon.patch
| File daemon.patch, 9.5 kB (added by dsmith, 5 years ago) |
|---|
-
wminput/c_plugin.c
51 51 return 0; 52 52 } 53 53 54 void c_wiimote_deinit() 55 { 56 return; 57 } 58 54 59 void c_deinit(void) 55 60 { 56 61 return; -
wminput/c_plugin.h
24 24 25 25 int c_init(void); 26 26 int c_wiimote(cwiid_wiimote_t *wiimote); 27 void c_wiimote_deinit(void); 27 28 void c_deinit(void); 28 29 int c_plugin_open(struct plugin *plugin, char *dir); 29 30 void c_plugin_close(struct plugin *plugin); -
wminput/py_plugin.c
190 190 return 0; 191 191 } 192 192 193 void py_wiimote_deinit() 194 { 195 Py_DECREF(PyWiimote); 196 } 197 193 198 void py_deinit(void) 194 199 { 195 Py_DECREF(PyWiimote);196 200 Py_DECREF(PyCWiidModule); 197 201 Py_DECREF(PyPath); 198 202 Py_DECREF(PySysModule); -
wminput/py_plugin.h
27 27 28 28 int py_init(void); 29 29 int py_wiimote(cwiid_wiimote_t *wiimote); 30 void py_wiimote_deinit(void); 30 31 void py_deinit(void); 31 32 int py_plugin_open(struct plugin *plugin, char *dir); 32 33 void py_plugin_close(struct plugin *plugin); -
wminput/main.c
108 108 printf("\t-h, --help\t\tPrints this output.\n"); 109 109 printf("\t-v, --version\t\toutput version information and exit.\n"); 110 110 printf("\t-c, --config [file]\tChoose config file to use.\n"); 111 printf("\t-d, --daemon\t\tImplies -q, -r, and -w.\n"); 112 printf("\t-q, --quiet\t\tReduce output to errors\n"); 113 printf("\t-r, --reconnect [wait]\t\tAutomatically try reconnect after wiimote disconnect.\n"); 111 114 printf("\t-w, --wait\t\tWait indefinitely for wiimote to connect.\n"); 112 115 } 113 116 114 117 int main(int argc, char *argv[]) 115 118 { 116 char wait_forever = 0 ;119 char wait_forever = 0, quiet = 0, reconnect = 0, reconnect_wait = 0; 117 120 char *config_search_dirs[3], *plugin_search_dirs[3]; 118 121 char *config_filename = DEFAULT_CONFIG_FILE; 119 122 char home_config_dir[HOME_DIR_LEN]; … … 121 124 char *tmp; 122 125 int c, i; 123 126 char *str_addr; 124 bdaddr_t bdaddr ;127 bdaddr_t bdaddr, current_bdaddr; 125 128 sigset_t sigset; 126 129 int signum, ret=0; 127 130 struct uinput_listen_data uinput_listen_data; … … 135 138 136 139 static struct option long_options[] = { 137 140 {"help", 0, 0, 'h'}, 141 {"version", 0, 0, 'v'}, 142 {"config", 1, 0, 'c'}, 143 {"daemon", 0, 0, 'd'}, 144 {"quiet", 0, 0, 'q'}, 145 {"reconnect", 2, 0, 'r'}, 138 146 {"wait", 0, 0, 'w'}, 139 {"config", 1, 0, 'c'},140 {"version", 0, 0, 'v'},141 147 {0, 0, 0, 0} 142 148 }; 143 149 144 c = getopt_long (argc, argv, "h wc:v", long_options, &option_index);150 c = getopt_long (argc, argv, "hvc:dqr::w", long_options, &option_index); 145 151 146 152 if (c == -1) { 147 153 break; … … 152 158 print_usage(); 153 159 return 0; 154 160 break; 155 case 'w': 156 wait_forever = 1; 161 case 'v': 162 printf("CWiid Version %s\n", PACKAGE_VERSION); 163 return 0; 157 164 break; 158 165 case 'c': 159 166 config_filename = optarg; 160 167 break; 161 case 'v': 162 printf("CWiid Version %s\n", PACKAGE_VERSION); 163 return 0; 168 case 'd': 169 wait_forever = 1; 170 quiet = 1; 171 reconnect = 1; 164 172 break; 173 case 'q': 174 quiet = 1; 175 break; 176 case 'r': 177 reconnect = 1; 178 if (optarg) { 179 reconnect_wait = strtol(optarg, &tmp, 10); 180 if (*tmp != '\0') { 181 wminput_err("bad reconnect wait time"); 182 return -1; 183 } 184 } 185 break; 186 case 'w': 187 wait_forever = 1; 188 break; 165 189 case '?': 166 190 printf("Try `wminput --help` for more information\n"); 167 191 return 1; … … 230 254 bdaddr = *BDADDR_ANY; 231 255 } 232 256 233 /* Wiimote Connect */ 234 printf("Put Wiimote in discoverable mode now (press 1+2)...\n"); 235 if (wait_forever) { 236 if (!bacmp(&bdaddr, BDADDR_ANY)) { 237 if (cwiid_find_wiimote(&bdaddr, -1)) { 238 wminput_err("error finding wiimote"); 257 sigemptyset(&sigset); 258 sigaddset(&sigset, SIGTERM); 259 sigaddset(&sigset, SIGINT); 260 sigaddset(&sigset, SIGUSR1); 261 262 do { 263 bacpy(¤t_bdaddr, &bdaddr); 264 265 /* Wiimote Connect */ 266 if (!quiet) { 267 printf("Put Wiimote in discoverable mode now (press 1+2)...\n"); 268 } 269 if (wait_forever) { 270 if (!bacmp(¤t_bdaddr, BDADDR_ANY)) { 271 if (cwiid_find_wiimote(¤t_bdaddr, -1)) { 272 wminput_err("error finding wiimote"); 273 conf_unload(&conf); 274 return -1; 275 } 276 } 277 /* TODO: avoid continuously calling cwiid_open */ 278 /* TODO: kill error messages on failed cwiid_open calls */ 279 while (!(wiimote = cwiid_open(¤t_bdaddr, CWIID_FLAG_MESG_IFC))); 280 } 281 else { 282 if ((wiimote = cwiid_open(¤t_bdaddr, CWIID_FLAG_MESG_IFC)) == NULL) { 283 wminput_err("unable to connect"); 239 284 conf_unload(&conf); 240 285 return -1; 241 286 } 242 287 } 243 /* TODO: avoid continuously calling cwiid_open */ 244 /* TODO: kill error messages on failed cwiid_open calls */ 245 while (!(wiimote = cwiid_open(&bdaddr, CWIID_FLAG_MESG_IFC))); 246 } 247 else { 248 if ((wiimote = cwiid_open(&bdaddr, CWIID_FLAG_MESG_IFC)) == NULL) { 249 wminput_err("unable to connect"); 288 if (cwiid_set_mesg_callback(wiimote, &cwiid_callback)) { 289 wminput_err("error setting callback"); 250 290 conf_unload(&conf); 251 291 return -1; 252 292 } 253 }254 if (cwiid_set_mesg_callback(wiimote, &cwiid_callback)) {255 wminput_err("error setting callback");256 conf_unload(&conf);257 return -1;258 }259 293 260 if (c_wiimote(wiimote)) {261 conf_unload(&conf);262 return -1;263 }294 if (c_wiimote(wiimote)) { 295 conf_unload(&conf); 296 return -1; 297 } 264 298 #ifdef HAVE_PYTHON 265 if (py_wiimote(wiimote)) {266 conf_unload(&conf);267 return -1;268 }299 if (py_wiimote(wiimote)) { 300 conf_unload(&conf); 301 return -1; 302 } 269 303 #endif 270 304 271 /* init plugins */272 for (i=0; (i < CONF_MAX_PLUGINS) && conf.plugins[i].name; i++) {273 switch (conf.plugins[i].type) {274 case PLUGIN_C:275 if (c_plugin_init(&conf.plugins[i], i)) {276 wminput_err("error on %s init", conf.plugins[i].name);277 conf_unload(&conf);278 cwiid_close(wiimote);279 return -1;280 }281 break;305 /* init plugins */ 306 for (i=0; (i < CONF_MAX_PLUGINS) && conf.plugins[i].name; i++) { 307 switch (conf.plugins[i].type) { 308 case PLUGIN_C: 309 if (c_plugin_init(&conf.plugins[i], i)) { 310 wminput_err("error on %s init", conf.plugins[i].name); 311 conf_unload(&conf); 312 cwiid_close(wiimote); 313 return -1; 314 } 315 break; 282 316 #ifdef HAVE_PYTHON 283 case PLUGIN_PYTHON: 284 if (py_plugin_init(&conf.plugins[i], i)) { 285 wminput_err("error %s init", conf.plugins[i].name); 286 conf_unload(&conf); 287 cwiid_close(wiimote); 288 return -1; 317 case PLUGIN_PYTHON: 318 if (py_plugin_init(&conf.plugins[i], i)) { 319 wminput_err("error %s init", conf.plugins[i].name); 320 conf_unload(&conf); 321 cwiid_close(wiimote); 322 return -1; 323 } 324 break; 325 #endif 289 326 } 290 break;291 #endif292 327 } 293 }294 328 295 if (wminput_set_report_mode()) {296 conf_unload(&conf);297 cwiid_close(wiimote);298 return -1;299 }329 if (wminput_set_report_mode()) { 330 conf_unload(&conf); 331 cwiid_close(wiimote); 332 return -1; 333 } 300 334 301 uinput_listen_data.wiimote = wiimote;302 uinput_listen_data.conf = &conf;303 if (pthread_create(&uinput_listen_thread, NULL,304 (void *(*)(void *))uinput_listen,305 &uinput_listen_data)) {306 wminput_err("error starting uinput listen thread");307 conf_unload(&conf);308 cwiid_close(wiimote);309 return -1;310 }335 uinput_listen_data.wiimote = wiimote; 336 uinput_listen_data.conf = &conf; 337 if (pthread_create(&uinput_listen_thread, NULL, 338 (void *(*)(void *))uinput_listen, 339 &uinput_listen_data)) { 340 wminput_err("error starting uinput listen thread"); 341 conf_unload(&conf); 342 cwiid_close(wiimote); 343 return -1; 344 } 311 345 346 if (!quiet) { 347 printf("Ready.\n"); 348 } 312 349 313 printf("Ready.\n");350 init = 0; 314 351 315 init = 0; 352 /* wait */ 353 sigprocmask(SIG_BLOCK, &sigset, NULL); 354 sigwait(&sigset, &signum); 355 sigprocmask(SIG_UNBLOCK, &sigset, NULL); 316 356 317 /* wait */ 318 sigemptyset(&sigset); 319 sigaddset(&sigset, SIGTERM); 320 sigaddset(&sigset, SIGINT); 321 sigaddset(&sigset, SIGUSR1); 322 sigprocmask(SIG_BLOCK, &sigset, NULL); 323 sigwait(&sigset, &signum); 357 if ((signum == SIGTERM) || (signum == SIGINT)) { 358 reconnect = 0; 359 } 324 360 325 printf("Exiting.\n"); 361 if (pthread_cancel(uinput_listen_thread)) { 362 wminput_err("Error canceling uinput listen thread"); 363 ret = -1; 364 } 365 else if (pthread_join(uinput_listen_thread, NULL)) { 366 wminput_err("Error joining uinput listen thread"); 367 ret = -1; 368 } 326 369 327 if (pthread_cancel(uinput_listen_thread)) { 328 wminput_err("Error canceling uinput listen thread"); 329 ret = -1; 330 } 331 else if (pthread_join(uinput_listen_thread, NULL)) { 332 wminput_err("Error joining uinput listen thread"); 333 ret = -1; 334 } 370 c_wiimote_deinit(); 371 #ifdef HAVE_PYTHON 372 py_wiimote_deinit(); 373 #endif 335 374 336 /* disconnect */337 if (cwiid_close(wiimote)) {338 wminput_err("Error on wiimote disconnect");339 ret = -1;340 }375 /* disconnect */ 376 if (cwiid_close(wiimote)) { 377 wminput_err("Error on wiimote disconnect"); 378 ret = -1; 379 } 341 380 381 if (reconnect && reconnect_wait) { 382 sleep(reconnect_wait); 383 } 384 } while (reconnect); 385 342 386 if (conf_unload(&conf)) { 343 387 ret = -1; 344 388 } 345 389 390 c_deinit(); 346 391 #ifdef HAVE_PYTHON 347 392 py_deinit(); 348 393 #endif 349 394 395 if (!quiet) { 396 printf("Exiting.\n"); 397 } 398 350 399 return ret; 351 400 } 352 401
