| 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"); |
|---|
| 239 | | conf_unload(&conf); |
|---|
| 240 | | return -1; |
|---|
| 241 | | } |
|---|
| 242 | | } |
|---|
| 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"); |
|---|
| 250 | | conf_unload(&conf); |
|---|
| 251 | | return -1; |
|---|
| 252 | | } |
|---|
| 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 | | |
|---|
| 260 | | if (c_wiimote(wiimote)) { |
|---|
| 261 | | conf_unload(&conf); |
|---|
| 262 | | return -1; |
|---|
| 263 | | } |
|---|
| 264 | | #ifdef HAVE_PYTHON |
|---|
| 265 | | if (py_wiimote(wiimote)) { |
|---|
| 266 | | conf_unload(&conf); |
|---|
| 267 | | return -1; |
|---|
| 268 | | } |
|---|
| 269 | | #endif |
|---|
| 270 | | |
|---|
| 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; |
|---|
| 282 | | #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; |
|---|
| 289 | | } |
|---|
| 290 | | break; |
|---|
| 291 | | #endif |
|---|
| 292 | | } |
|---|
| 293 | | } |
|---|
| 294 | | |
|---|
| 295 | | if (wminput_set_report_mode()) { |
|---|
| 296 | | conf_unload(&conf); |
|---|
| 297 | | cwiid_close(wiimote); |
|---|
| 298 | | return -1; |
|---|
| 299 | | } |
|---|
| 300 | | |
|---|
| 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 | | } |
|---|
| 311 | | |
|---|
| 312 | | |
|---|
| 313 | | printf("Ready.\n"); |
|---|
| 314 | | |
|---|
| 315 | | init = 0; |
|---|
| 316 | | |
|---|
| 317 | | /* wait */ |
|---|
| 322 | | sigprocmask(SIG_BLOCK, &sigset, NULL); |
|---|
| 323 | | sigwait(&sigset, &signum); |
|---|
| 324 | | |
|---|
| 325 | | printf("Exiting.\n"); |
|---|
| 326 | | |
|---|
| 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 | | } |
|---|
| 335 | | |
|---|
| 336 | | /* disconnect */ |
|---|
| 337 | | if (cwiid_close(wiimote)) { |
|---|
| 338 | | wminput_err("Error on wiimote disconnect"); |
|---|
| 339 | | ret = -1; |
|---|
| 340 | | } |
|---|
| | 264 | |
|---|
| | 265 | do { |
|---|
| | 266 | bacpy(¤t_bdaddr, &bdaddr); |
|---|
| | 267 | |
|---|
| | 268 | /* Wiimote Connect */ |
|---|
| | 269 | if (!quiet) { |
|---|
| | 270 | printf("Put Wiimote in discoverable mode now (press 1+2)...\n"); |
|---|
| | 271 | } |
|---|
| | 272 | if (wait_forever) { |
|---|
| | 273 | if (!bacmp(¤t_bdaddr, BDADDR_ANY)) { |
|---|
| | 274 | if (cwiid_find_wiimote(¤t_bdaddr, -1)) { |
|---|
| | 275 | wminput_err("error finding wiimote"); |
|---|
| | 276 | conf_unload(&conf); |
|---|
| | 277 | return -1; |
|---|
| | 278 | } |
|---|
| | 279 | } |
|---|
| | 280 | /* TODO: avoid continuously calling cwiid_open */ |
|---|
| | 281 | /* TODO: kill error messages on failed cwiid_open calls */ |
|---|
| | 282 | while (!(wiimote = cwiid_open(¤t_bdaddr, CWIID_FLAG_MESG_IFC))); |
|---|
| | 283 | } |
|---|
| | 284 | else { |
|---|
| | 285 | if ((wiimote = cwiid_open(¤t_bdaddr, CWIID_FLAG_MESG_IFC)) == NULL) { |
|---|
| | 286 | wminput_err("unable to connect"); |
|---|
| | 287 | conf_unload(&conf); |
|---|
| | 288 | return -1; |
|---|
| | 289 | } |
|---|
| | 290 | } |
|---|
| | 291 | if (cwiid_set_mesg_callback(wiimote, &cwiid_callback)) { |
|---|
| | 292 | wminput_err("error setting callback"); |
|---|
| | 293 | conf_unload(&conf); |
|---|
| | 294 | return -1; |
|---|
| | 295 | } |
|---|
| | 296 | |
|---|
| | 297 | if (c_wiimote(wiimote)) { |
|---|
| | 298 | conf_unload(&conf); |
|---|
| | 299 | return -1; |
|---|
| | 300 | } |
|---|
| | 301 | #ifdef HAVE_PYTHON |
|---|
| | 302 | if (py_wiimote(wiimote)) { |
|---|
| | 303 | conf_unload(&conf); |
|---|
| | 304 | return -1; |
|---|
| | 305 | } |
|---|
| | 306 | #endif |
|---|
| | 307 | |
|---|
| | 308 | /* init plugins */ |
|---|
| | 309 | for (i=0; (i < CONF_MAX_PLUGINS) && conf.plugins[i].name; i++) { |
|---|
| | 310 | switch (conf.plugins[i].type) { |
|---|
| | 311 | case PLUGIN_C: |
|---|
| | 312 | if (c_plugin_init(&conf.plugins[i], i)) { |
|---|
| | 313 | wminput_err("error on %s init", conf.plugins[i].name); |
|---|
| | 314 | conf_unload(&conf); |
|---|
| | 315 | cwiid_close(wiimote); |
|---|
| | 316 | return -1; |
|---|
| | 317 | } |
|---|
| | 318 | break; |
|---|
| | 319 | #ifdef HAVE_PYTHON |
|---|
| | 320 | case PLUGIN_PYTHON: |
|---|
| | 321 | if (py_plugin_init(&conf.plugins[i], i)) { |
|---|
| | 322 | wminput_err("error %s init", conf.plugins[i].name); |
|---|
| | 323 | conf_unload(&conf); |
|---|
| | 324 | cwiid_close(wiimote); |
|---|
| | 325 | return -1; |
|---|
| | 326 | } |
|---|
| | 327 | break; |
|---|
| | 328 | #endif |
|---|
| | 329 | } |
|---|
| | 330 | } |
|---|
| | 331 | |
|---|
| | 332 | if (wminput_set_report_mode()) { |
|---|
| | 333 | conf_unload(&conf); |
|---|
| | 334 | cwiid_close(wiimote); |
|---|
| | 335 | return -1; |
|---|
| | 336 | } |
|---|
| | 337 | |
|---|
| | 338 | uinput_listen_data.wiimote = wiimote; |
|---|
| | 339 | uinput_listen_data.conf = &conf; |
|---|
| | 340 | if (pthread_create(&uinput_listen_thread, NULL, |
|---|
| | 341 | (void *(*)(void *))uinput_listen, |
|---|
| | 342 | &uinput_listen_data)) { |
|---|
| | 343 | wminput_err("error starting uinput listen thread"); |
|---|
| | 344 | conf_unload(&conf); |
|---|
| | 345 | cwiid_close(wiimote); |
|---|
| | 346 | return -1; |
|---|
| | 347 | } |
|---|
| | 348 | |
|---|
| | 349 | if (!quiet) { |
|---|
| | 350 | printf("Ready.\n"); |
|---|
| | 351 | } |
|---|
| | 352 | |
|---|
| | 353 | init = 0; |
|---|
| | 354 | |
|---|
| | 355 | /* wait */ |
|---|
| | 356 | sigprocmask(SIG_BLOCK, &sigset, NULL); |
|---|
| | 357 | sigwait(&sigset, &signum); |
|---|
| | 358 | sigprocmask(SIG_UNBLOCK, &sigset, NULL); |
|---|
| | 359 | |
|---|
| | 360 | if ((signum == SIGTERM) || (signum == SIGINT)) { |
|---|
| | 361 | reconnect = 0; |
|---|
| | 362 | } |
|---|
| | 363 | |
|---|
| | 364 | if (pthread_cancel(uinput_listen_thread)) { |
|---|
| | 365 | wminput_err("Error canceling uinput listen thread"); |
|---|
| | 366 | ret = -1; |
|---|
| | 367 | } |
|---|
| | 368 | else if (pthread_join(uinput_listen_thread, NULL)) { |
|---|
| | 369 | wminput_err("Error joining uinput listen thread"); |
|---|
| | 370 | ret = -1; |
|---|
| | 371 | } |
|---|
| | 372 | |
|---|
| | 373 | c_wiimote_deinit(); |
|---|
| | 374 | #ifdef HAVE_PYTHON |
|---|
| | 375 | py_wiimote_deinit(); |
|---|
| | 376 | #endif |
|---|
| | 377 | |
|---|
| | 378 | /* disconnect */ |
|---|
| | 379 | if (cwiid_close(wiimote)) { |
|---|
| | 380 | wminput_err("Error on wiimote disconnect"); |
|---|
| | 381 | ret = -1; |
|---|
| | 382 | } |
|---|
| | 383 | |
|---|
| | 384 | if (reconnect && reconnect_wait) { |
|---|
| | 385 | sleep(reconnect_wait); |
|---|
| | 386 | } |
|---|
| | 387 | } while (reconnect); |
|---|