Ticket #15: daemon.patch

File daemon.patch, 9.5 kB (added by dsmith, 5 years ago)
  • wminput/c_plugin.c

     
    5151        return 0; 
    5252} 
    5353 
     54void c_wiimote_deinit() 
     55{ 
     56        return; 
     57} 
     58 
    5459void c_deinit(void) 
    5560{ 
    5661        return; 
  • wminput/c_plugin.h

     
    2424 
    2525int c_init(void); 
    2626int c_wiimote(cwiid_wiimote_t *wiimote); 
     27void c_wiimote_deinit(void); 
    2728void c_deinit(void); 
    2829int c_plugin_open(struct plugin *plugin, char *dir); 
    2930void c_plugin_close(struct plugin *plugin); 
  • wminput/py_plugin.c

     
    190190        return 0; 
    191191} 
    192192 
     193void py_wiimote_deinit() 
     194{ 
     195        Py_DECREF(PyWiimote); 
     196} 
     197 
    193198void py_deinit(void) 
    194199{ 
    195         Py_DECREF(PyWiimote); 
    196200        Py_DECREF(PyCWiidModule); 
    197201        Py_DECREF(PyPath); 
    198202        Py_DECREF(PySysModule); 
  • wminput/py_plugin.h

     
    2727 
    2828int py_init(void); 
    2929int py_wiimote(cwiid_wiimote_t *wiimote); 
     30void py_wiimote_deinit(void); 
    3031void py_deinit(void); 
    3132int py_plugin_open(struct plugin *plugin, char *dir); 
    3233void py_plugin_close(struct plugin *plugin); 
  • wminput/main.c

     
    108108        printf("\t-h, --help\t\tPrints this output.\n"); 
    109109        printf("\t-v, --version\t\toutput version information and exit.\n"); 
    110110        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"); 
    111114        printf("\t-w, --wait\t\tWait indefinitely for wiimote to connect.\n"); 
    112115} 
    113116 
    114117int main(int argc, char *argv[]) 
    115118{ 
    116         char wait_forever = 0; 
     119        char wait_forever = 0, quiet = 0, reconnect = 0, reconnect_wait = 0; 
    117120        char *config_search_dirs[3], *plugin_search_dirs[3]; 
    118121        char *config_filename = DEFAULT_CONFIG_FILE; 
    119122        char home_config_dir[HOME_DIR_LEN]; 
     
    121124        char *tmp; 
    122125        int c, i; 
    123126        char *str_addr; 
    124         bdaddr_t bdaddr; 
     127        bdaddr_t bdaddr, current_bdaddr; 
    125128        sigset_t sigset; 
    126129        int signum, ret=0; 
    127130        struct uinput_listen_data uinput_listen_data; 
     
    135138 
    136139                static struct option long_options[] = { 
    137140                        {"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'}, 
    138146                        {"wait", 0, 0, 'w'}, 
    139                         {"config", 1, 0, 'c'}, 
    140                         {"version", 0, 0, 'v'}, 
    141147                        {0, 0, 0, 0} 
    142148                }; 
    143149 
    144                 c = getopt_long (argc, argv, "hwc:v", long_options, &option_index); 
     150                c = getopt_long (argc, argv, "hvc:dqr::w", long_options, &option_index); 
    145151 
    146152                if (c == -1) { 
    147153                        break; 
     
    152158                        print_usage(); 
    153159                        return 0; 
    154160                        break; 
    155                 case 'w': 
    156                         wait_forever = 1; 
     161                case 'v': 
     162                        printf("CWiid Version %s\n", PACKAGE_VERSION); 
     163                        return 0; 
    157164                        break; 
    158165                case 'c': 
    159166                        config_filename = optarg; 
    160167                        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; 
    164172                        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; 
    165189                case '?': 
    166190                        printf("Try `wminput --help` for more information\n"); 
    167191                        return 1; 
     
    230254                bdaddr = *BDADDR_ANY; 
    231255        } 
    232256 
    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(&current_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(&current_bdaddr, BDADDR_ANY)) { 
     271                                if (cwiid_find_wiimote(&current_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(&current_bdaddr, CWIID_FLAG_MESG_IFC))); 
     280                } 
     281                else { 
     282                        if ((wiimote = cwiid_open(&current_bdaddr, CWIID_FLAG_MESG_IFC)) == NULL) { 
     283                                wminput_err("unable to connect"); 
    239284                                conf_unload(&conf); 
    240285                                return -1; 
    241286                        } 
    242287                } 
    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"); 
    250290                        conf_unload(&conf); 
    251291                        return -1; 
    252292                } 
    253         } 
    254         if (cwiid_set_mesg_callback(wiimote, &cwiid_callback)) { 
    255                 wminput_err("error setting callback"); 
    256                 conf_unload(&conf); 
    257                 return -1; 
    258         } 
    259293 
    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                } 
    264298#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                } 
    269303#endif 
    270304 
    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; 
    282316#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 
    289326                        } 
    290                         break; 
    291 #endif 
    292327                } 
    293         } 
    294328 
    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                } 
    300334 
    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                } 
    311345 
     346                if (!quiet) { 
     347                        printf("Ready.\n"); 
     348                } 
    312349 
    313         printf("Ready.\n"); 
     350                init = 0; 
    314351 
    315         init = 0; 
     352                /* wait */ 
     353                sigprocmask(SIG_BLOCK, &sigset, NULL); 
     354                sigwait(&sigset, &signum); 
     355                sigprocmask(SIG_UNBLOCK, &sigset, NULL); 
    316356 
    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                } 
    324360 
    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                } 
    326369 
    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 
    335374 
    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                } 
    341380 
     381                if (reconnect && reconnect_wait) { 
     382                        sleep(reconnect_wait); 
     383                } 
     384        } while (reconnect); 
     385 
    342386        if (conf_unload(&conf)) { 
    343387                ret = -1; 
    344388        } 
    345389 
     390        c_deinit(); 
    346391#ifdef HAVE_PYTHON 
    347392        py_deinit(); 
    348393#endif 
    349394 
     395        if (!quiet) { 
     396                printf("Exiting.\n"); 
     397        } 
     398 
    350399        return ret; 
    351400} 
    352401