Changeset 142

Show
Ignore:
Timestamp:
08/14/07 20:28:42 (1 year ago)
Author:
dsmith
Message:

wminput daemon mode

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/ChangeLog

    r138 r142  
     12007-08-14 L. Donnie Smith <cwiid@abstrakraft.org> 
     2        wminput 
     3        * added {c,py}_wiimote_deinit functions 
     4        * added daemon, quiet, and reconnect options 
     5 
    162007-07-29 L. Donnie Smith <cwiid@abstrakraft.org> 
    27        wminput 
  • trunk/wminput/c_plugin.c

    r125 r142  
    1616 * 
    1717 *  ChangeLog: 
     18 *  2007-08-14 L. Donnie Smith <cwiid@abstrakraft.org> 
     19 *  * added c_wiimote_deinit 
     20 * 
    1821 *  2007-06-05 L. Donnie Smith <cwiid@abstrakraft.org> 
    1922 *  * Initial ChangeLog 
     
    5053 
    5154        return 0; 
     55} 
     56 
     57void c_wiimote_deinit() 
     58{ 
     59        return; 
    5260} 
    5361 
  • trunk/wminput/c_plugin.h

    r125 r142  
    1616 * 
    1717 *  ChangeLog: 
     18 *  2007-08-14 L. Donnie Smith <cwiid@abstrakraft.org> 
     19 *  * added c_wiimote_deinit 
     20 * 
    1821 *  2007-06-05 L. Donnie Smith <cwiid@abstrakraft.org> 
    1922 *  * Initial ChangeLog 
     
    2528int c_init(void); 
    2629int c_wiimote(cwiid_wiimote_t *wiimote); 
     30void c_wiimote_deinit(void); 
    2731void c_deinit(void); 
    2832int c_plugin_open(struct plugin *plugin, char *dir); 
  • trunk/wminput/main.c

    r139 r142  
    1616 * 
    1717 *  ChangeLog: 
     18 *  2007-08-14 L. Donnie Smith <cwiid@abstrakraft.org> 
     19 *  * added daemon, quiet, and reconnect options 
     20 * 
    1821 *  2007-07-29 L. Donnie Smith <cwiid@abstrakraft.org> 
    1922 *  * fixed wait forever logic 
     
    109112        printf("\t-v, --version\t\toutput version information and exit.\n"); 
    110113        printf("\t-c, --config [file]\tChoose config file to use.\n"); 
     114        printf("\t-d, --daemon\t\tImplies -q, -r, and -w.\n"); 
     115        printf("\t-q, --quiet\t\tReduce output to errors\n"); 
     116        printf("\t-r, --reconnect [wait]\t\tAutomatically try reconnect after wiimote disconnect.\n"); 
    111117        printf("\t-w, --wait\t\tWait indefinitely for wiimote to connect.\n"); 
    112118} 
     
    114120int main(int argc, char *argv[]) 
    115121{ 
    116         char wait_forever = 0
     122        char wait_forever = 0, quiet = 0, reconnect = 0, reconnect_wait = 0
    117123        char *config_search_dirs[3], *plugin_search_dirs[3]; 
    118124        char *config_filename = DEFAULT_CONFIG_FILE; 
     
    122128        int c, i; 
    123129        char *str_addr; 
    124         bdaddr_t bdaddr
     130        bdaddr_t bdaddr, current_bdaddr
    125131        sigset_t sigset; 
    126132        int signum, ret=0; 
     
    136142                static struct option long_options[] = { 
    137143                        {"help", 0, 0, 'h'}, 
     144                        {"version", 0, 0, 'v'}, 
     145                        {"config", 1, 0, 'c'}, 
     146                        {"daemon", 0, 0, 'd'}, 
     147                        {"quiet", 0, 0, 'q'}, 
     148                        {"reconnect", 2, 0, 'r'}, 
    138149                        {"wait", 0, 0, 'w'}, 
    139                         {"config", 1, 0, 'c'}, 
    140                         {"version", 0, 0, 'v'}, 
    141150                        {0, 0, 0, 0} 
    142151                }; 
    143152 
    144                 c = getopt_long (argc, argv, "hwc:v", long_options, &option_index); 
     153                c = getopt_long (argc, argv, "hvc:dqr::w", long_options, &option_index); 
    145154 
    146155                if (c == -1) { 
     
    153162                        return 0; 
    154163                        break; 
    155                 case 'w': 
    156                         wait_forever = 1; 
    157                         break; 
    158                 case 'c': 
    159                         config_filename = optarg; 
    160                         break; 
    161164                case 'v': 
    162165                        printf("CWiid Version %s\n", PACKAGE_VERSION); 
    163166                        return 0; 
     167                        break; 
     168                case 'c': 
     169                        config_filename = optarg; 
     170                        break; 
     171                case 'd': 
     172                        wait_forever = 1; 
     173                        quiet = 1; 
     174                        reconnect = 1; 
     175                        break; 
     176                case 'q': 
     177                        quiet = 1; 
     178                        break; 
     179                case 'r': 
     180                        reconnect = 1; 
     181                        if (optarg) { 
     182                                reconnect_wait = strtol(optarg, &tmp, 10); 
     183                                if (*tmp != '\0') { 
     184                                        wminput_err("bad reconnect wait time"); 
     185                                        return -1; 
     186                                } 
     187                        } 
     188                        break; 
     189                case 'w': 
     190                        wait_forever = 1; 
    164191                        break; 
    165192                case '?': 
     
    231258        } 
    232259 
    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 */ 
    318260        sigemptyset(&sigset); 
    319261        sigaddset(&sigset, SIGTERM); 
    320262        sigaddset(&sigset, SIGINT); 
    321263        sigaddset(&sigset, SIGUSR1); 
    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(&current_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(&current_bdaddr, BDADDR_ANY)) { 
     274                                if (cwiid_find_wiimote(&current_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(&current_bdaddr, CWIID_FLAG_MESG_IFC))); 
     283                } 
     284                else { 
     285                        if ((wiimote = cwiid_open(&current_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); 
    341388 
    342389        if (conf_unload(&conf)) { 
     
    344391        } 
    345392 
     393        c_deinit(); 
    346394#ifdef HAVE_PYTHON 
    347395        py_deinit(); 
    348396#endif 
     397 
     398        if (!quiet) { 
     399                printf("Exiting.\n"); 
     400        } 
    349401 
    350402        return ret; 
  • trunk/wminput/py_plugin.c

    r133 r142  
    1616 * 
    1717 *  ChangeLog: 
     18 *  2007-08-14 L. Donnie Smith <cwiid@abstrakraft.org> 
     19 *  * added py_wiimote_deinit 
     20 * 
    1821 *  2007-06-28 L. Donnie Smith <cwiid@abstrakraft.org> 
    1922 *  * supress error for nonexistent python plugins 
     
    9093        int i; 
    9194 
    92         Py_Initialize(); 
     95        Py_InitializeEx(0); 
    9396 
    9497        if (!(PyCWiidModule = PyImport_ImportModule("cwiid"))) { 
     
    191194} 
    192195 
     196void py_wiimote_deinit() 
     197{ 
     198        Py_DECREF(PyWiimote); 
     199} 
     200 
    193201void py_deinit(void) 
    194202{ 
    195         Py_DECREF(PyWiimote); 
    196203        Py_DECREF(PyCWiidModule); 
    197204        Py_DECREF(PyPath); 
  • trunk/wminput/py_plugin.h

    r125 r142  
    1616 * 
    1717 *  ChangeLog: 
     18 *  2007-08-14 L. Donnie Smith <cwiid@abstrakraft.org> 
     19 *  * added py_wiimote_deinit 
     20 * 
    1821 *  2007-06-05 L. Donnie Smith <cwiid@abstrakraft.org> 
    1922 *  * refactored to isolate plugin logic 
     
    2831int py_init(void); 
    2932int py_wiimote(cwiid_wiimote_t *wiimote); 
     33void py_wiimote_deinit(void); 
    3034void py_deinit(void); 
    3135int py_plugin_open(struct plugin *plugin, char *dir);