src/joystick/bsd/SDL_sysjoystick.c
changeset 7733 957d7137867c
parent 7719 31b5f9ff36ca
child 8093 b43765095a6f
     1.1 --- a/src/joystick/bsd/SDL_sysjoystick.c	Fri Sep 06 20:45:08 2013 -0700
     1.2 +++ b/src/joystick/bsd/SDL_sysjoystick.c	Fri Sep 06 20:54:14 2013 -0700
     1.3 @@ -446,48 +446,47 @@
     1.4      static int x, y, xmin = 0xffff, ymin = 0xffff, xmax = 0, ymax = 0;
     1.5  
     1.6      if (joy->hwdata->type == BSDJOY_JOY) {
     1.7 -        if (read(joy->hwdata->fd, &gameport, sizeof gameport) !=
     1.8 -            sizeof gameport)
     1.9 -            return;
    1.10 -        if (abs(x - gameport.x) > 8) {
    1.11 -            x = gameport.x;
    1.12 -            if (x < xmin) {
    1.13 -                xmin = x;
    1.14 +        while (read(joy->hwdata->fd, &gameport, sizeof gameport) == sizeof gameport) {
    1.15 +            if (abs(x - gameport.x) > 8) {
    1.16 +                x = gameport.x;
    1.17 +                if (x < xmin) {
    1.18 +                    xmin = x;
    1.19 +                }
    1.20 +                if (x > xmax) {
    1.21 +                    xmax = x;
    1.22 +                }
    1.23 +                if (xmin == xmax) {
    1.24 +                    xmin--;
    1.25 +                    xmax++;
    1.26 +                }
    1.27 +                v = (Sint32) x;
    1.28 +                v -= (xmax + xmin + 1) / 2;
    1.29 +                v *= 32768 / ((xmax - xmin + 1) / 2);
    1.30 +                SDL_PrivateJoystickAxis(joy, 0, v);
    1.31              }
    1.32 -            if (x > xmax) {
    1.33 -                xmax = x;
    1.34 +            if (abs(y - gameport.y) > 8) {
    1.35 +                y = gameport.y;
    1.36 +                if (y < ymin) {
    1.37 +                    ymin = y;
    1.38 +                }
    1.39 +                if (y > ymax) {
    1.40 +                    ymax = y;
    1.41 +                }
    1.42 +                if (ymin == ymax) {
    1.43 +                    ymin--;
    1.44 +                    ymax++;
    1.45 +                }
    1.46 +                v = (Sint32) y;
    1.47 +                v -= (ymax + ymin + 1) / 2;
    1.48 +                v *= 32768 / ((ymax - ymin + 1) / 2);
    1.49 +                SDL_PrivateJoystickAxis(joy, 1, v);
    1.50              }
    1.51 -            if (xmin == xmax) {
    1.52 -                xmin--;
    1.53 -                xmax++;
    1.54 +            if (gameport.b1 != joy->buttons[0]) {
    1.55 +                SDL_PrivateJoystickButton(joy, 0, gameport.b1);
    1.56              }
    1.57 -            v = (Sint32) x;
    1.58 -            v -= (xmax + xmin + 1) / 2;
    1.59 -            v *= 32768 / ((xmax - xmin + 1) / 2);
    1.60 -            SDL_PrivateJoystickAxis(joy, 0, v);
    1.61 -        }
    1.62 -        if (abs(y - gameport.y) > 8) {
    1.63 -            y = gameport.y;
    1.64 -            if (y < ymin) {
    1.65 -                ymin = y;
    1.66 +            if (gameport.b2 != joy->buttons[1]) {
    1.67 +                SDL_PrivateJoystickButton(joy, 1, gameport.b2);
    1.68              }
    1.69 -            if (y > ymax) {
    1.70 -                ymax = y;
    1.71 -            }
    1.72 -            if (ymin == ymax) {
    1.73 -                ymin--;
    1.74 -                ymax++;
    1.75 -            }
    1.76 -            v = (Sint32) y;
    1.77 -            v -= (ymax + ymin + 1) / 2;
    1.78 -            v *= 32768 / ((ymax - ymin + 1) / 2);
    1.79 -            SDL_PrivateJoystickAxis(joy, 1, v);
    1.80 -        }
    1.81 -        if (gameport.b1 != joy->buttons[0]) {
    1.82 -            SDL_PrivateJoystickButton(joy, 0, gameport.b1);
    1.83 -        }
    1.84 -        if (gameport.b2 != joy->buttons[1]) {
    1.85 -            SDL_PrivateJoystickButton(joy, 1, gameport.b2);
    1.86          }
    1.87          return;
    1.88      }
    1.89 @@ -495,65 +494,62 @@
    1.90  
    1.91      rep = &joy->hwdata->inreport;
    1.92  
    1.93 -    if (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) != rep->size) {
    1.94 -        return;
    1.95 -    }
    1.96 +    while (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) == rep->size) {
    1.97  #if defined(USBHID_NEW) || (defined(__FREEBSD__) && __FreeBSD_kernel_version >= 500111) || defined(__FreeBSD_kernel__)
    1.98 -    hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input, rep->rid);
    1.99 +        hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input, rep->rid);
   1.100  #else
   1.101 -    hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input);
   1.102 +        hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input);
   1.103  #endif
   1.104 -    if (hdata == NULL) {
   1.105 -        fprintf(stderr, "%s: Cannot start HID parser\n", joy->hwdata->path);
   1.106 -        return;
   1.107 -    }
   1.108 +        if (hdata == NULL) {
   1.109 +            /*fprintf(stderr, "%s: Cannot start HID parser\n", joy->hwdata->path);*/
   1.110 +            continue;
   1.111 +        }
   1.112  
   1.113 -    for (nbutton = 0; hid_get_item(hdata, &hitem) > 0;) {
   1.114 -        switch (hitem.kind) {
   1.115 -        case hid_input:
   1.116 -            switch (HID_PAGE(hitem.usage)) {
   1.117 -            case HUP_GENERIC_DESKTOP:
   1.118 -                {
   1.119 -                    unsigned usage = HID_USAGE(hitem.usage);
   1.120 -                    int joyaxe = usage_to_joyaxe(usage);
   1.121 -                    if (joyaxe >= 0) {
   1.122 -                        naxe = joy->hwdata->axis_map[joyaxe];
   1.123 -                        /* scaleaxe */
   1.124 -                        v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
   1.125 -                        v -= (hitem.logical_maximum +
   1.126 -                              hitem.logical_minimum + 1) / 2;
   1.127 -                        v *= 32768 /
   1.128 -                            ((hitem.logical_maximum -
   1.129 -                              hitem.logical_minimum + 1) / 2);
   1.130 -                        if (v != joy->axes[naxe]) {
   1.131 -                            SDL_PrivateJoystickAxis(joy, naxe, v);
   1.132 +        for (nbutton = 0; hid_get_item(hdata, &hitem) > 0;) {
   1.133 +            switch (hitem.kind) {
   1.134 +            case hid_input:
   1.135 +                switch (HID_PAGE(hitem.usage)) {
   1.136 +                case HUP_GENERIC_DESKTOP:
   1.137 +                    {
   1.138 +                        unsigned usage = HID_USAGE(hitem.usage);
   1.139 +                        int joyaxe = usage_to_joyaxe(usage);
   1.140 +                        if (joyaxe >= 0) {
   1.141 +                            naxe = joy->hwdata->axis_map[joyaxe];
   1.142 +                            /* scaleaxe */
   1.143 +                            v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
   1.144 +                            v -= (hitem.logical_maximum +
   1.145 +                                  hitem.logical_minimum + 1) / 2;
   1.146 +                            v *= 32768 /
   1.147 +                                ((hitem.logical_maximum -
   1.148 +                                  hitem.logical_minimum + 1) / 2);
   1.149 +                            if (v != joy->axes[naxe]) {
   1.150 +                                SDL_PrivateJoystickAxis(joy, naxe, v);
   1.151 +                            }
   1.152 +                        } else if (usage == HUG_HAT_SWITCH) {
   1.153 +                            v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
   1.154 +                            SDL_PrivateJoystickHat(joy, 0,
   1.155 +                                                   hatval_to_sdl(v) -
   1.156 +                                                   hitem.logical_minimum);
   1.157                          }
   1.158 -                    } else if (usage == HUG_HAT_SWITCH) {
   1.159 -                        v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
   1.160 -                        SDL_PrivateJoystickHat(joy, 0,
   1.161 -                                               hatval_to_sdl(v) -
   1.162 -                                               hitem.logical_minimum);
   1.163 +                        break;
   1.164                      }
   1.165 +                case HUP_BUTTON:
   1.166 +                    v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
   1.167 +                    if (joy->buttons[nbutton] != v) {
   1.168 +                        SDL_PrivateJoystickButton(joy, nbutton, v);
   1.169 +                    }
   1.170 +                    nbutton++;
   1.171                      break;
   1.172 +                default:
   1.173 +                    continue;
   1.174                  }
   1.175 -            case HUP_BUTTON:
   1.176 -                v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
   1.177 -                if (joy->buttons[nbutton] != v) {
   1.178 -                    SDL_PrivateJoystickButton(joy, nbutton, v);
   1.179 -                }
   1.180 -                nbutton++;
   1.181                  break;
   1.182              default:
   1.183 -                continue;
   1.184 +                break;
   1.185              }
   1.186 -            break;
   1.187 -        default:
   1.188 -            break;
   1.189          }
   1.190 +        hid_end_parse(hdata);
   1.191      }
   1.192 -    hid_end_parse(hdata);
   1.193 -
   1.194 -    return;
   1.195  }
   1.196  
   1.197  /* Function to close a joystick after use */