Exemplo n.º 1
0
/* ljoy_process_new_data: [fdwatch thread]
 *
 *  Process new data arriving in the joystick's fd.
 */
static void ljoy_process_new_data(void *data)
{
    ALLEGRO_JOYSTICK_LINUX *joy = data;
    ALLEGRO_EVENT_SOURCE *es = al_get_joystick_event_source();

    if (!es) {
        // Joystick driver not fully initialized
        return;
    }

    _al_event_source_lock(es);
    {
        struct input_event input_events[32];
        int bytes, nr, i;

        while ((bytes = read(joy->fd, &input_events, sizeof input_events)) > 0) {
            nr = bytes / sizeof(struct input_event);

            for (i = 0; i < nr; i++) {
                int type = input_events[i].type;
                int code = input_events[i].code;
                int value = input_events[i].value;

                if (type == EV_KEY) {
                    int number = map_button_number(joy, code);
                    if (number >= 0) {
                        if (value)
                            joy->joystate.button[number] = 32767;
                        else
                            joy->joystate.button[number] = 0;

                        ljoy_generate_button_event(joy, number,
                                                   (value
                                                    ? ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN
                                                    : ALLEGRO_EVENT_JOYSTICK_BUTTON_UP));
                    }
                }
                else if (type == EV_ABS) {
                    int number = code;
                    if (number < TOTAL_JOYSTICK_AXES) {
                        const AXIS_MAPPING *map = &joy->axis_mapping[number];
                        int stick = map->stick;
                        int axis = map->axis;
                        float pos = norm_pos(map, value);

                        joy->joystate.stick[stick].axis[axis] = pos;
                        ljoy_generate_axis_event(joy, stick, axis, pos);
                    }
                }
            }
        }
    }
    _al_event_source_unlock(es);
}
Exemplo n.º 2
0
/* ljoy_process_new_data: [fdwatch thread]
 *
 *  Process new data arriving in the joystick's fd.
 */
static void ljoy_process_new_data(void *data)
{
   ALLEGRO_JOYSTICK_LINUX *joy = data;

   _al_event_source_lock(&joy->parent.es);
   {
      struct js_event js_events[32];
      int bytes, nr, i;

      while ((bytes = read(joy->fd, &js_events, sizeof js_events)) > 0) {

         nr = bytes / sizeof(struct js_event);

         for (i = 0; i < nr; i++) {

            int type   = js_events[i].type;
            int number = js_events[i].number;
            int value  = js_events[i].value;

            if (type & JS_EVENT_BUTTON) {
               if (number < _AL_MAX_JOYSTICK_BUTTONS) {
                  if (value)
                     joy->joystate.button[number] = 32767;
                  else
                     joy->joystate.button[number] = 0;

                  ljoy_generate_button_event(joy, number,
                                             (value
                                              ? ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN
                                              : ALLEGRO_EVENT_JOYSTICK_BUTTON_UP));
               }
            }
            else if (type & JS_EVENT_AXIS) {
               if (number < TOTAL_JOYSTICK_AXES) {
                  int stick = joy->axis_mapping[number].stick;
                  int axis  = joy->axis_mapping[number].axis;
                  float pos = value / 32767.0;

                  joy->joystate.stick[stick].axis[axis] = pos;

                  ljoy_generate_axis_event(joy, stick, axis, pos);
               }
            }
         }
      }
   }
   _al_event_source_unlock(&joy->parent.es);
}