Example #1
0
int set_led_state(struct wiimoteglue_state* state, struct wii_device *dev, bool leds[]) {
  /*This code will have to change
   *if there is ever a wiimote with
   *more than 4 LEDs.
   */

  if (dev == NULL || leds == NULL)
    return -1;
  if (dev->xwii == NULL)
    return -2; /*this controller is closed, but otherwise okay.*/
  if (state->set_leds == 0 || dev->type == BALANCE)
    return 0; /*do nothing*/
  int i;
  int ret;
  int okay = 0;

  for (i = 1; i <= 4; i++) {
    if (leds[i-1] < 0)
      return -1;
    /*if anything seems fishy, bail out.
     *We really don't want to end up with
     *a device connected but with all 4
     *leds off.
     */
  }

  for (i = 1; i <= 4; i++) {
    ret = xwii_iface_set_led(dev->xwii,XWII_LED(i),leds[i-1]);
    if (ret < 0) {
      okay = -1;
    }
  }
  return okay;
}
Example #2
0
int run_once(struct xwii_iface *iface)
{
	struct xwii_event event;
	int ret = 0;
	struct vt_stat vtstat;
	int vt = 0;
	int i;
	int console = 0;
	char *device = "/dev/console";
	//char *device = "/dev/tty0";
	
    console = open(device, O_RDWR);
	if (console < 0) {
        fprintf(stderr, "/dev/console problems. Do you have enough permission ?\n");
        exit(3);
    }
    if ((ioctl(console, VT_GETSTATE, &vtstat) < 0) || vt == -1) {
        fprintf(stderr, "/dev/console problems. Do you have enough permission ?\n");
        exit(3);
    }
    close(console);
    vt = vtstat.v_active;
    printf("Start vt %i.\n",vt);
    if (vt > 10)
        vt = 0;
    if (vt < 0) {
        printf("Error getting vt.\n");
        return -1;
    }
    for (i=0; i<4; i++) {
	    printf("Setting led %i to %i.\n", i+1, ledmap[vt][i]);
    	ret = xwii_iface_set_led(iface, i+1, ledmap[vt][i]);
    	if (ret) {
    	    printf("Error setting led %i to %i.\n", i+1, ledmap[vt][i]);
    	    return ret;
    	}
    }

	while (true) {
		ret = xwii_iface_poll(iface, &event);
		if (ret == -EAGAIN) {
			usleep(50000);
		} else if (ret) {
			printf("Error: Read failed with err:%d\n", ret);
			return ret;
		} else if (event.type == XWII_EVENT_KEY && event.v.key.state) {
            console = open(device, O_RDWR); //got some errors when leaving console open over switches...
	        if (console < 0) {
                fprintf(stderr, "/dev/console problems. Do you have enough permission ?\n");
                exit(3);
            }
            if ((ioctl(console, VT_GETSTATE, &vtstat) < 0) || vt == -1) {
                fprintf(stderr, "/dev/console problems. Do you have enough permission ?\n");
                exit(3);
            } else
                printf("old-vt %i.   ",vt);
            vt = vtstat.v_active;
            switch (event.v.key.code) {
		    case XWII_KEY_TWO:
		        vt += 2;
		    case XWII_KEY_ONE:
		        vt -= 1;
		        if (vt < 1)
		            vt = 1;
		        if (vt > 10)
		            vt = 10;
                if (ioctl(console, VT_ACTIVATE, vt) != 0) {
                    fprintf(stderr, "/dev/console problems. Do you have enough permission ?\n");
                    exit(3);
                } else
                    printf("Switched to vt %i.\n",vt);
                for (i=0; i<4; i++) {
                	ret = xwii_iface_set_led(iface, i+1, ledmap[vt][i]);
                	if (ret) {
                	    printf("Error setting led %i to %i.\n", i+1, ledmap[vt][i]);
                	    return ret;
                	}
                }
			    break;
			default:
			    continue;
			}
			close(console);
		}
	}
	return ret;
}