Example #1
0
C_RESULT open_wiimote(void)
{
  C_RESULT res = C_OK;

  printf("\nSearching for Wiimote - press both '1' and '2' on your Wiimote !!\n");

  if(cwiid_find_wiimote(&bdaddr, WIIMOTE_FIND_TIMEOUT) == 0)
  {
    if( ! (wiimote = cwiid_open(&bdaddr, 0)) )
      PRINT("Unable to connect to wiimote\n");
    else
      res = C_OK;
  }

  if(SUCCEED(res))
    if(cwiid_set_rpt_mode(wiimote, CWIID_RPT_BTN|CWIID_RPT_ACC))
      res = C_FAIL;

  if(SUCCEED(res))
    if (cwiid_get_acc_cal(wiimote, CWIID_EXT_NONE, &wm_cal))
      res = C_FAIL;

  cwiid_set_led(wiimote,1);

  printf(" ... Wiimote found.\n");

  return res;
}
Example #2
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;
	const char *dev_name = NULL;
	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'},
			{"name", 1, 0, 'n'},
			{0, 0, 0, 0}
		};

		c = getopt_long (argc, argv, "hvc:dqr::wn:", 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 'n':
			dev_name = optarg;
			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, dev_name)) {
		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;
}
Example #3
0
cwiid_wiimote_t *cwiid_open_timeout(bdaddr_t *bdaddr, int flags, int timeout)
{
	struct sockaddr_l2 remote_addr;
	int ctl_socket = -1, int_socket = -1;
	struct wiimote *wiimote = NULL;
	bdaddr_t any_bdaddr;

	/* Treat a null bdaddr as BDADDR_ANY */
	if (bdaddr == NULL) {
		any_bdaddr = *BDADDR_ANY;
		bdaddr = &any_bdaddr;
	}

	/* If BDADDR_ANY is given, find available wiimote */
	if (bacmp(bdaddr, BDADDR_ANY) == 0) {
		if (cwiid_find_wiimote(bdaddr, timeout)) {
			goto ERR_HND;
		}
		sleep(1);
	}

	/* Connect to Wiimote */
	/* Control Channel */
	memset(&remote_addr, 0, sizeof remote_addr);
	remote_addr.l2_family = AF_BLUETOOTH;
	bacpy( &remote_addr.l2_bdaddr, bdaddr );
	remote_addr.l2_psm = htobs(CTL_PSM);
	if ((ctl_socket =
	  socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)) == -1) {
		cwiid_err(NULL, "Socket creation error (control socket): %s", strerror(errno));
		goto ERR_HND;
	}
	if (connect(ctl_socket, (struct sockaddr *)&remote_addr,
		        sizeof remote_addr)) {
		cwiid_err(NULL, "Socket connect error (control socket): %s", strerror(errno));
		goto ERR_HND;
	}

	/* Interrupt Channel */
	remote_addr.l2_psm = htobs(INT_PSM);
	if ((int_socket =
	  socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)) == -1) {
		cwiid_err(NULL, "Socket creation error (interrupt socket): %s", strerror(errno));
		goto ERR_HND;
	}
	if (connect(int_socket, (struct sockaddr *)&remote_addr,
		        sizeof remote_addr)) {
		cwiid_err(NULL, "Socket connect error (interrupt socket): %s", strerror(errno));
		goto ERR_HND;
	}

	if ((wiimote = cwiid_new(ctl_socket, int_socket, flags)) == NULL) {
		/* Raises its own error */
		goto ERR_HND;
	}

	return wiimote;

ERR_HND:
	/* Close Sockets */
	if (ctl_socket != -1) {
		if (close(ctl_socket)) {
			cwiid_err(NULL, "Socket close error (control socket): %s", strerror(errno));
		}
	}
	if (int_socket != -1) {
		if (close(int_socket)) {
			cwiid_err(NULL, "Socket close error (interrupt socket): %s", strerror(errno));
		}
	}
	return NULL;
}