Beispiel #1
0
static void dev_destroy(struct wiimote_dev *dev) {
	if (dev->root) {
		xwii_iface_unref(dev->iface);
		free(dev->root);
		dev->root = NULL;
	}
	if (dev->uinput > 0) {
		ioctl(dev->uinput, UI_DEV_DESTROY);
		close(dev->uinput);
	}
	printf("deassociated from device %s\n", dev->device);
}
Beispiel #2
0
int main(int argc, char **argv)
{
	int ret = 0;
	char *path = NULL;
	struct xwii_iface *iface;

	if (argc > 2 || (argc == 2 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0))) {
	    printf("This is %s version %s.\n\n", PACKAGE, VERSION);
		printf("Usage: %s [-h | --help] [-o | --once]\n", argv[0]);
		printf("\t-h\t--help\tShow this information.\n");
		printf("\t-o\t--once\tFind one device and do not try to rediscover.\n");
		return 1;
	} 
	

	while (true) {
	    struct xwii_monitor *mon = xwii_monitor_new(false, false);
	    if (!mon) {
		    printf("Cannot create monitor\n");
		    return 2;
	    }
    	path = xwii_monitor_poll(mon);
    	xwii_monitor_unref(mon);
		usleep(100000);
       	if (path != NULL) {
		    ret = xwii_iface_new(&iface, path);
		    if (ret) {
			    printf("Error newing '%s' err:%d\n", path, ret);
		    } else {
                ret = xwii_iface_open(iface, XWII_IFACE_CORE |
					                     XWII_IFACE_WRITABLE);
	            if (ret) {
		            printf("Error opening '%s' err:%d\n", path, ret);
	            } else {
		            ret = run_once(iface);
		            if (ret) {
            		    printf("Event-Loop exit with status %d\n", ret);
            		}
            	}
		    }
		    free(path);
		    xwii_iface_unref(iface);
		}
		if (argc >= 2 && (strcmp(argv[1], "-o") == 0 || strcmp(argv[1], "--once") == 0))
		    break;
	}


	return ret;
}
int close_wii_device(struct wiimoteglue_state* state, struct wii_device *dev) {
  if (dev->xwii == NULL) {
    return 0; //Already closed.
  }
  printf("Controller %s (%s) has been closed.\n",dev->id,dev->bluetooth_addr);

  
  close(dev->fd);

  xwii_iface_unref(dev->xwii);
  dev->xwii = NULL;
  if (dev->slot != NULL) {
    printf("(It was assigned slot %s)\n",dev->slot->slot_name);
    remove_device_from_slot(dev);
  }

  return 0;
}
int open_wii_device(struct wiimoteglue_state *state, struct wii_device* dev) {
  if (dev->xwii != NULL) {
    return 0; //Already open.
  }
  
  if (dev->udev == NULL) {
    return -1; //Not even connected.
  }
  
  int i;
  struct xwii_iface *wiidev;
  char* syspath = udev_device_get_syspath(dev->udev);
  if (syspath == NULL)
    return -1;
  xwii_iface_new(&wiidev,syspath);
  xwii_iface_watch(wiidev,true);
  xwii_iface_open(wiidev,  XWII_IFACE_WRITABLE | (XWII_IFACE_ALL ^ XWII_IFACE_ACCEL ^ XWII_IFACE_IR ^ XWII_IFACE_MOTION_PLUS));
  if (!(xwii_iface_available(wiidev) & (XWII_IFACE_CORE | XWII_IFACE_PRO_CONTROLLER | XWII_IFACE_BALANCE_BOARD))) {
    printf("Tried to open a non-wiimote device...\n");
    printf("Or we didn't have permission on the event devices?\n");
    printf("You might need to add a udev rule to give permission.\n");
    xwii_iface_unref(wiidev);
    return -1;
  }

  dev->xwii = wiidev;
  dev->ifaces = xwii_iface_opened(wiidev);

  if (state->ignore_pro && (dev->ifaces & XWII_IFACE_PRO_CONTROLLER)) {
    xwii_iface_unref(wiidev);
    dev->xwii = NULL;
    return 0;
  }



  dev->type = REMOTE;
  if (dev->ifaces & XWII_IFACE_BALANCE_BOARD) {
    dev->type = BALANCE;
  }
  if (dev->ifaces & XWII_IFACE_PRO_CONTROLLER) {
    dev->type = PRO;
  }

  dev->fd = xwii_iface_get_fd(wiidev);
  wiimoteglue_epoll_watch_wiimote(state->epfd, dev);



  compute_device_map(state,dev);

  if (dev->map->accel_active) {
    xwii_iface_open(wiidev,XWII_IFACE_ACCEL);
  } else {
    xwii_iface_close(wiidev,XWII_IFACE_ACCEL);
  }

  if (dev->map->IR_count) {
    xwii_iface_open(wiidev,XWII_IFACE_IR);
  } else {
    xwii_iface_close(wiidev,XWII_IFACE_IR);
  }
  
  /*LEDs only checked after opening,
   *and we want to store the state
   *only on the very initial opening.*/
  if (dev->original_leds[0] == -2)
    store_led_state(state,dev);

  return 0;

}