示例#1
0
WearerPtr Wearer::create(DescriptionPtr dsc)
{
  /* REMEBER TO UPDATE CLONE, WHEN ADDING NEW ELEMENTS */  
  WearerPtr w = nullptr;
  WearerDescriptionPtr wearerDsc = std::dynamic_pointer_cast<WearerDescription>(dsc);

  if ( wearerDsc != nullptr )
  {
    w.reset( new Wearer );
    wearerDsc->eqItems->maxSize = wearerDsc->itemSlots.size();

    std::for_each(wearerDsc->itemSlots.begin(), wearerDsc->itemSlots.end(), [&](ItemSlotType slot)
    {
      w->_itemSlots[slot] = nullptr;
    });

    w->_equippedItems = Container::create(wearerDsc->eqItems);
    if ( w->_equippedItems )
    {
      assignItemsToSlots( w );
    }

  }else throw creation_error("Wrong wearer description!");

  return w;
}
// create a UInput device and return the file descriptor as a handle
JNIEXPORT jint JNICALL Java_com_endpoint_lg_evdev_writer_UinputDevice_createDevice(JNIEnv *env, jclass cls, jstring uinput_path, jstring dev_name, jint id_vendor, jint id_product, jint version, jintArray abs_min, jintArray abs_max) {
  struct uinput_user_dev uidev;
  int status;

  // open the uinput node at the provided path
  const char *nv_uinput_path = env->GetStringUTFChars(uinput_path, 0);

  int fd = open(nv_uinput_path, O_WRONLY | O_NONBLOCK);
  if (fd < 0) {
    #ifdef DEBUG
    perror("opening the uinput node");
    #endif
    return -1;
  }

  env->ReleaseStringUTFChars(uinput_path, nv_uinput_path);

  /*** specify allowed event types and codes ***/

  // EV_KEY
  status = ioctl(fd, UI_SET_EVBIT, EV_KEY);
  if (status != 0)
    return creation_error("error activating EV_KEY type");

  status = allow_all_codes(fd, UI_SET_KEYBIT, KEY_MAX);
  if (status != 0)
    return creation_error("error activating EV_KEY codes");

  // EV_REL
  status = ioctl(fd, UI_SET_EVBIT, EV_REL);
  if (status != 0)
    return creation_error("error activating EV_REL type");

  allow_all_codes(fd, UI_SET_RELBIT, REL_MAX);
  if (status != 0)
    return creation_error("error activating EV_REL codes");

  // EV_ABS
  status = ioctl(fd, UI_SET_EVBIT, EV_ABS);
  if (status != 0)
    return creation_error("error activating EV_ABS type");

  allow_all_codes(fd, UI_SET_ABSBIT, ABS_MAX);
  if (status != 0)
    return creation_error("error activating EV_ABS codes");

  // EV_SYN
  status = ioctl(fd, UI_SET_EVBIT, EV_SYN);
  if (status != 0)
    return creation_error("error activating EV_SYN type");

  // initialize the user device struct
  memset(&uidev, 0, sizeof(uidev));

  // set the device name -- grab the string from JVM
  const char *nv_dev_name = env->GetStringUTFChars(dev_name, 0);

  strncpy(uidev.name, nv_dev_name, UINPUT_MAX_NAME_SIZE);

  env->ReleaseStringUTFChars(dev_name, nv_dev_name);

  // set more device attributes
  uidev.id.bustype = BUS_USB; // XXX: this is a big assumption
  uidev.id.vendor  = (__u16)id_vendor;
  uidev.id.product = (__u16)id_product;
  uidev.id.version = (__u16)version;

  // set ABS min/max values
  copy_native_int_array(env, uidev.absmin, abs_min, ABS_CNT);
  copy_native_int_array(env, uidev.absmax, abs_max, ABS_CNT);

  // write our device information
  status = write(fd, &uidev, sizeof(uidev));
  if (status != sizeof(uidev))
    return creation_error("error writing uinput_user_dev");

  // create the device
  status = ioctl(fd, UI_DEV_CREATE);
  if (status != 0)
    return creation_error("error on ioctl UI_DEV_CREATE");

  #ifdef DEBUG
  fprintf(
    stderr,
    "created device: %s vendor: %d product: %d version: %d\n",
    uidev.name, uidev.id.vendor, uidev.id.product, uidev.id.version
  );
  #endif

  // fd is now a handle for the user device
  return fd;
}