Ejemplo n.º 1
0
int main(int argc, char *argv[])
{
	char wait_forever = 0, quiet = 0, reconnect = 0, reconnect_wait = 0;
	char *config_search_dirs[3], *plugin_search_dirs[3];
	char *config_filename = DEFAULT_CONFIG_FILE;
	char home_config_dir[HOME_DIR_LEN];
	char home_plugin_dir[HOME_DIR_LEN];
	char *tmp;
	int c, i;
	char *str_addr;
	bdaddr_t bdaddr, current_bdaddr;
	sigset_t sigset;
	int signum, ret=0;
	struct uinput_listen_data uinput_listen_data;
	pthread_t uinput_listen_thread;

	init = 1;

	/* Parse Options */
	while (1) {
		int option_index = 0;

		static struct option long_options[] = {
			{"help", 0, 0, 'h'},
			{"version", 0, 0, 'v'},
			{"config", 1, 0, 'c'},
			{"daemon", 0, 0, 'd'},
			{"quiet", 0, 0, 'q'},
			{"reconnect", 2, 0, 'r'},
			{"wait", 0, 0, 'w'},
			{0, 0, 0, 0}
		};

		c = getopt_long (argc, argv, "hvc:dqr::w", long_options, &option_index);

		if (c == -1) {
			break;
		}

		switch (c) {
		case 'h':
			print_usage();
			return 0;
			break;
		case 'v':
			printf("CWiid Version %s\n", PACKAGE_VERSION);
			return 0;
			break;
		case 'c':
			config_filename = optarg;
			break;
		case 'd':
			wait_forever = 1;
			quiet = 1;
			reconnect = 1;
			break;
		case 'q':
			quiet = 1;
			break;
		case 'r':
			reconnect = 1;
			if (optarg) {
				reconnect_wait = strtol(optarg, &tmp, 10);
				if (*tmp != '\0') {
					wminput_err("bad reconnect wait time");
					return -1;
				}
			}
			break;
		case 'w':
			wait_forever = 1;
			break;
		case '?':
			printf("Try `wminput --help` for more information\n");
			return 1;
			break;
		default:
			return -1;
			break;
		}
	}

	if (c_init()) {
		return -1;
	}

#ifdef HAVE_PYTHON
	if (py_init()) {
		return -1;
	}
#endif

	/* Load Config */
	/* Setup search directory arrays */
	if ((tmp = getenv("HOME")) == NULL) {
		wminput_err("Unable to find home directory");
		config_search_dirs[0] = WMINPUT_CONFIG_DIR;
		plugin_search_dirs[0] = CWIID_PLUGINS_DIR;
		config_search_dirs[1] = plugin_search_dirs[1] = NULL;
	}
	else {
		snprintf(home_config_dir, HOME_DIR_LEN, "%s/.cwiid/wminput", tmp);
		snprintf(home_plugin_dir, HOME_DIR_LEN, "%s/.cwiid/plugins", tmp);
		config_search_dirs[0] = home_config_dir;
		plugin_search_dirs[0] = home_plugin_dir;
		config_search_dirs[1] = WMINPUT_CONFIG_DIR;
		plugin_search_dirs[1] = CWIID_PLUGINS_DIR;
		config_search_dirs[2] = plugin_search_dirs[2] = NULL;
	}

	if (conf_load(&conf, config_filename, config_search_dirs,
	  plugin_search_dirs)) {
		return -1;
	}

	/* Determine BDADDR */
	/* priority: command-line option, environment variable, BDADDR_ANY */
	if (optind < argc) {
		if (str2ba(argv[optind], &bdaddr)) {
			wminput_err("invalid bdaddr");
			bdaddr = *BDADDR_ANY;
		}
		optind++;
		if (optind < argc) {
			wminput_err("invalid command-line");
			print_usage();
			conf_unload(&conf);
			return -1;
		}
	}
	else if ((str_addr = getenv(WIIMOTE_BDADDR)) != NULL) {
		if (str2ba(str_addr, &bdaddr)) {
			wminput_err("invalid address in %s", WIIMOTE_BDADDR);
			bdaddr = *BDADDR_ANY;
		}
	}
	else {
		bdaddr = *BDADDR_ANY;
	}

	sigemptyset(&sigset);
	sigaddset(&sigset, SIGTERM);
	sigaddset(&sigset, SIGINT);
	sigaddset(&sigset, SIGUSR1);

	do {
		bacpy(&current_bdaddr, &bdaddr);

		/* Wiimote Connect */
		if (!quiet) {
			printf("Put Wiimote in discoverable mode now (press 1+2)...\n");
		}
		if (wait_forever) {
			if (!bacmp(&current_bdaddr, BDADDR_ANY)) {
				if (cwiid_find_wiimote(&current_bdaddr, -1)) {
					wminput_err("error finding wiimote");
					conf_unload(&conf);
					return -1;
				}
			}
			/* TODO: avoid continuously calling cwiid_open */
			cwiid_set_err(cwiid_err_connect);
			while (!(wiimote = cwiid_open(&current_bdaddr, CWIID_FLAG_MESG_IFC)));
			cwiid_set_err(cwiid_err_default);
		}
		else {
			if ((wiimote = cwiid_open(&current_bdaddr, CWIID_FLAG_MESG_IFC)) == NULL) {
				wminput_err("unable to connect");
				conf_unload(&conf);
				return -1;
			}
		}
		if (cwiid_set_mesg_callback(wiimote, &cwiid_callback)) {
			wminput_err("error setting callback");
			conf_unload(&conf);
			return -1;
		}

		if (c_wiimote(wiimote)) {
			conf_unload(&conf);
			return -1;
		}
#ifdef HAVE_PYTHON
		if (py_wiimote(wiimote)) {
			conf_unload(&conf);
			return -1;
		}
#endif

		/* init plugins */
		for (i=0; (i < CONF_MAX_PLUGINS) && conf.plugins[i].name; i++) {
			switch (conf.plugins[i].type) {
			case PLUGIN_C:
				if (c_plugin_init(&conf.plugins[i], i)) {
					wminput_err("error on %s init", conf.plugins[i].name);
					conf_unload(&conf);
					cwiid_close(wiimote);
					return -1;
				}
				break;
#ifdef HAVE_PYTHON
			case PLUGIN_PYTHON:
				if (py_plugin_init(&conf.plugins[i], i)) {
					wminput_err("error %s init", conf.plugins[i].name);
					conf_unload(&conf);
					cwiid_close(wiimote);
					return -1;
				}
				break;
#endif
			}
		}

		if (wminput_set_report_mode()) {
			conf_unload(&conf);
			cwiid_close(wiimote);
			return -1;
		}

		uinput_listen_data.wiimote = wiimote;
		uinput_listen_data.conf = &conf;
		if (pthread_create(&uinput_listen_thread, NULL,
		                   (void *(*)(void *))uinput_listen,
		                   &uinput_listen_data)) {
			wminput_err("error starting uinput listen thread");
			conf_unload(&conf);
			cwiid_close(wiimote);
			return -1;
		}

		if (!quiet) {
			printf("Ready.\n");
		}

		init = 0;

		/* wait */
		sigprocmask(SIG_BLOCK, &sigset, NULL);
		sigwait(&sigset, &signum);
		sigprocmask(SIG_UNBLOCK, &sigset, NULL);

		if ((signum == SIGTERM) || (signum == SIGINT)) {
			reconnect = 0;
		}

		if (pthread_cancel(uinput_listen_thread)) {
			wminput_err("Error canceling uinput listen thread");
			ret = -1;
		}
		else if (pthread_join(uinput_listen_thread, NULL)) {
			wminput_err("Error joining uinput listen thread");
			ret = -1;
		}

		c_wiimote_deinit();
#ifdef HAVE_PYTHON
		py_wiimote_deinit();
#endif

		/* disconnect */
		if (cwiid_close(wiimote)) {
			wminput_err("Error on wiimote disconnect");
			ret = -1;
		}

		if (reconnect && reconnect_wait) {
			sleep(reconnect_wait);
		}
	} while (reconnect);

	if (conf_unload(&conf)) {
		ret = -1;
	}

	c_deinit();
#ifdef HAVE_PYTHON
	py_deinit();
#endif

	if (!quiet) {
		printf("Exiting.\n");
	}

	return ret;
}
Ejemplo n.º 2
0
int main(int argc, char *argv[]) 
{
  if (argc != 2) {
    printf("Usage: %s 3, where 3 is the number of gestures to recognize\n", argv[0]);
    exit(1);
  }
  n_gestures = atoi(argv[1]);
  hmms = malloc(n_gestures * sizeof(HmmStateRef));

  int n_states = n_gestures+3;
  int n_obs = 17;

  for(int i = 0; i < n_gestures; i++) {
    hmms[i] = hmm_new(n_states, n_obs);
  }

  cwiid_wiimote_t *wiimote;	/* wiimote handle */
  //struct cwiid_state state;	/* wiimote state */
  bdaddr_t bdaddr;	/* bluetooth device address */
  //unsigned char mesg = 0;
  //unsigned char led_state = 0;
  unsigned char rpt_mode = 0;
  //unsigned char rumble = 0;

  /* Make stdout unbuffered, which is useful for piping the output of
   * this program into a timestamping utility, such as tai64n(1) */
  setvbuf(stdout, NULL, _IOLBF, 0);

  cwiid_set_err(err);

#if WE_ACTUALLY_CARE_ABOUT_BLUETOOTH
  /* Connect to address given on command-line, if present */
  if (argc > 1) {
    str2ba(argv[1], &bdaddr);
  }
  else {
#endif
    bdaddr = *BDADDR_ANY;
#if WE_ACTUALLY_CARE_ABOUT_BLUETOOTH
  }
#endif

  printf("Put Wiimote in discoverable mode now (press 1+2)...\n");
  if (!(wiimote = cwiid_open(&bdaddr, 0))) {
    fprintf(stderr, "Unable to connect to wiimote\n");
    exit(1);
  }
  if (cwiid_set_mesg_callback(wiimote, cwiid_callback)) {
    fprintf(stderr, "Unable to set message callback\n");
    exit(1);
  }

  toggle_bit(rpt_mode, CWIID_RPT_ACC);
  toggle_bit(rpt_mode, CWIID_RPT_BTN);
  set_rpt_mode(wiimote, rpt_mode);


  if (cwiid_enable(wiimote, CWIID_FLAG_MESG_IFC)) {
    fprintf(stderr, "Unable to set message flag\n");
    exit(1);
  }


  while(1) {
    sleep(1);
  }
}
Ejemplo n.º 3
0
void BluetoothProcess::run()
{
        cwiid_wiimote_t *wiimote;	/* wiimote handle */
        struct cwiid_state state;	/* wiimote state */
        bdaddr_t bdaddr;	/* bluetooth device address */
        unsigned char mesg = 0;
        unsigned char led_state = 0;
        unsigned char rpt_mode = 0;
        unsigned char rumble = 0;
        eexit = 0;

/*        f = false;    //表示没记录到红外值
        record_x = 0;
        record_y = 0;
*/
        cwiid_set_err(err);

        bdaddr = *BDADDR_ANY;

        /* Connect to the wiimote */
        printf("Put Wiimote in discoverable mode now (press 1+2)...\n");
        if (!(wiimote = cwiid_open(&bdaddr, 0))) {
                fprintf(stderr, "Unable to connect to wiimote\n");
                return;
        }
        if (cwiid_set_mesg_callback(wiimote, cwiid_callback)) {
                fprintf(stderr, "Unable to set message callback\n");
        }

        printf("Note: To demonstrate the new API interfaces, wmdemo no longer "
               "enables messages by default.\n"
               "Output can be gathered through the new state-based interface (s), "
               "or by enabling the messages interface (m).\n");

        /* Menu */
        printf("%s", MENU);

/*	while (!exit) {
                switch (getchar()) {
                case '1':
                        toggle_bit(led_state, CWIID_LED1_ON);
                        set_led_state(wiimote, led_state);
                        break;
                case '2':
                        toggle_bit(led_state, CWIID_LED2_ON);
                        set_led_state(wiimote, led_state);
                        break;
                case '3':
                        toggle_bit(led_state, CWIID_LED3_ON);
                        set_led_state(wiimote, led_state);
                        break;
                case '4':
                        toggle_bit(led_state, CWIID_LED4_ON);
                        set_led_state(wiimote, led_state);
                        break;
                case '5':
                        toggle_bit(rumble, 1);
                        if (cwiid_set_rumble(wiimote, rumble)) {
                                fprintf(stderr, "Error setting rumble\n");
                        }
                        break;
                case 'a':
                        toggle_bit(rpt_mode, CWIID_RPT_ACC);
                        set_rpt_mode(wiimote, rpt_mode);
                        break;
                case 'b':
                        toggle_bit(rpt_mode, CWIID_RPT_BTN);
                        set_rpt_mode(wiimote, rpt_mode);
                        break;
                case 'c':
                        cwiid_enable(wiimote, CWIID_FLAG_MOTIONPLUS);
                        break;
                case 'e':
                        /* CWIID_RPT_EXT is actually
                         * CWIID_RPT_NUNCHUK | CWIID_RPT_CLASSIC | CWIID_RPT_BALANCE */
/*			toggle_bit(rpt_mode, CWIID_RPT_EXT);
                        set_rpt_mode(wiimote, rpt_mode);
                        break;
                case 'i':
                        /* libwiimote picks the highest quality IR mode available with the
                         * other options selected (not including as-yet-undeciphered
                         * interleaved mode */
/*			break;
                case 'm':
                        if (!mesg) {
                                if (cwiid_enable(wiimote, CWIID_FLAG_MESG_IFC)) {
                                        fprintf(stderr, "Error enabling messages\n");
                                }
                                else {
                                        mesg = 1;
                                }
                        }
                        else {
                                if (cwiid_disable(wiimote, CWIID_FLAG_MESG_IFC)) {
                                        fprintf(stderr, "Error disabling message\n");
                                }
                                else {
                                        mesg = 0;
                                }
                        }
                        break;
                case 'p':
                        printf("%s", MENU);
                        break;
                case 'r':
                        if (cwiid_request_status(wiimote)) {
                                fprintf(stderr, "Error requesting status message\n");
                        }
                        break;
                case 's':
                        if (cwiid_get_state(wiimote, &state)) {
                                fprintf(stderr, "Error getting state\n");
                        }
                        print_state(&state);
                        break;
                case 't':
                        toggle_bit(rpt_mode, CWIID_RPT_STATUS);
                        set_rpt_mode(wiimote, rpt_mode);
                        break;
                case 'x':
                        exit = -1;
                        break;
                case '\n':
                        break;
                default:
                        fprintf(stderr, "invalid option\n");
                }
        }
*/
        while ( !eexit )
        {
                        cwiid_enable(wiimote, CWIID_FLAG_MESG_IFC);
                        toggle_bit(rpt_mode, CWIID_RPT_IR);
                        set_rpt_mode(wiimote, rpt_mode);
        }
        if (cwiid_close(wiimote)) {
                fprintf(stderr, "Error on wiimote disconnect\n");
                return;
        }

}
Ejemplo n.º 4
0
int main(int argc, char *argv[])
{
	cwiid_wiimote_t *wiimote;	/* wiimote handle */
	struct cwiid_state state;	/* wiimote state */
	bdaddr_t bdaddr;	/* bluetooth device address */
	unsigned char mesg = 0;
	unsigned char led_state = 0;
	unsigned char rpt_mode = 0;
	unsigned char rumble = 0;
	int exit = 0;

	deviceState.numButtons = NUM_BUTTONS;
	deviceState.numAxis = NUM_AXIS;
	deviceState.numSensors = NUM_SENSORS;
	deviceState.axisValues = NULL;
	deviceState.buttonValues = NULL;
	deviceState.sensorValues = new SensorData[NUM_SENSORS];

	cwiid_set_err(err);

	if (argc < 2) {
		fprintf(stderr, "Usage: %s HOSTNAME\n\n", argv[0]);
		return 1;
	} // if

// TODO: parse data from config file!!!
	/* Connect to address given on command-line, if present */
	if (argc > 2) {
		str2ba(argv[2], &bdaddr);
	}
	else {
		bdaddr = *BDADDR_ANY;
	}

	/* Connect to the wiimote */
	printf("Put Wiimote in discoverable mode now (press 1+2)...\n");
	if (!(wiimote = cwiid_open(&bdaddr, 0))) {
		fprintf(stderr, "Unable to connect to wiimote\n");
		return -1;
	}
	if (cwiid_set_mesg_callback(wiimote, cwiid_callback)) {
		fprintf(stderr, "Unable to set message callback\n");
	}

	toggle_bit(led_state, CWIID_LED1_ON);
	set_led_state(wiimote, led_state);
	if (cwiid_get_state(wiimote, &state)) {
		fprintf(stderr, "Error getting state\n");
	}
	print_state(&state);
	toggle_bit(rpt_mode, CWIID_RPT_IR);
	toggle_bit(rpt_mode, CWIID_RPT_BTN);
//	toggle_bit(rpt_mode, CWIID_RPT_ACC);
	set_rpt_mode(wiimote, rpt_mode);
	if (cwiid_enable(wiimote, CWIID_FLAG_MESG_IFC)) {
	    fprintf(stderr, "Error enabling messages\n");
	} else {
	    mesg = 1;
	}

	if (initNetwork(argv[1], CLIENTPORT)) {
		fprintf(stderr, "Error at network initialization\n");
		return -1;
	} // if

	bool running = true;

	while (running) {sleep(1);}

	if (cwiid_close(wiimote)) {
		fprintf(stderr, "Error on wiimote disconnect\n");
		return -1;
	}

	return 0;
}