src/joystick/bsd/SDL_sysjoystick.c
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
     1.1 --- a/src/joystick/bsd/SDL_sysjoystick.c	Mon May 29 03:53:21 2006 +0000
     1.2 +++ b/src/joystick/bsd/SDL_sysjoystick.c	Mon May 29 04:04:35 2006 +0000
     1.3 @@ -135,8 +135,8 @@
     1.4  static char *joynames[MAX_JOYS];
     1.5  static char *joydevnames[MAX_JOYS];
     1.6  
     1.7 -static int report_alloc (struct report *, struct report_desc *, int);
     1.8 -static void report_free (struct report *);
     1.9 +static int report_alloc(struct report *, struct report_desc *, int);
    1.10 +static void report_free(struct report *);
    1.11  
    1.12  #ifdef USBHID_UCR_DATA
    1.13  #define REP_BUF_DATA(rep) ((rep)->buf->ucr_data)
    1.14 @@ -145,49 +145,49 @@
    1.15  #endif
    1.16  
    1.17  int
    1.18 -SDL_SYS_JoystickInit (void)
    1.19 +SDL_SYS_JoystickInit(void)
    1.20  {
    1.21      char s[16];
    1.22      int i, fd;
    1.23  
    1.24      SDL_numjoysticks = 0;
    1.25  
    1.26 -    SDL_memset (joynames, 0, sizeof (joynames));
    1.27 -    SDL_memset (joydevnames, 0, sizeof (joydevnames));
    1.28 +    SDL_memset(joynames, 0, sizeof(joynames));
    1.29 +    SDL_memset(joydevnames, 0, sizeof(joydevnames));
    1.30  
    1.31      for (i = 0; i < MAX_UHID_JOYS; i++) {
    1.32          SDL_Joystick nj;
    1.33  
    1.34 -        SDL_snprintf (s, SDL_arraysize (s), "/dev/uhid%d", i);
    1.35 +        SDL_snprintf(s, SDL_arraysize(s), "/dev/uhid%d", i);
    1.36  
    1.37          nj.index = SDL_numjoysticks;
    1.38 -        joynames[nj.index] = strdup (s);
    1.39 +        joynames[nj.index] = strdup(s);
    1.40  
    1.41 -        if (SDL_SYS_JoystickOpen (&nj) == 0) {
    1.42 -            SDL_SYS_JoystickClose (&nj);
    1.43 +        if (SDL_SYS_JoystickOpen(&nj) == 0) {
    1.44 +            SDL_SYS_JoystickClose(&nj);
    1.45              SDL_numjoysticks++;
    1.46          } else {
    1.47 -            SDL_free (joynames[nj.index]);
    1.48 +            SDL_free(joynames[nj.index]);
    1.49              joynames[nj.index] = NULL;
    1.50          }
    1.51      }
    1.52      for (i = 0; i < MAX_JOY_JOYS; i++) {
    1.53 -        SDL_snprintf (s, SDL_arraysize (s), "/dev/joy%d", i);
    1.54 -        fd = open (s, O_RDONLY);
    1.55 +        SDL_snprintf(s, SDL_arraysize(s), "/dev/joy%d", i);
    1.56 +        fd = open(s, O_RDONLY);
    1.57          if (fd != -1) {
    1.58 -            joynames[SDL_numjoysticks++] = strdup (s);
    1.59 -            close (fd);
    1.60 +            joynames[SDL_numjoysticks++] = strdup(s);
    1.61 +            close(fd);
    1.62          }
    1.63      }
    1.64  
    1.65      /* Read the default USB HID usage table. */
    1.66 -    hid_init (NULL);
    1.67 +    hid_init(NULL);
    1.68  
    1.69      return (SDL_numjoysticks);
    1.70  }
    1.71  
    1.72  const char *
    1.73 -SDL_SYS_JoystickName (int index)
    1.74 +SDL_SYS_JoystickName(int index)
    1.75  {
    1.76      if (joydevnames[index] != NULL) {
    1.77          return (joydevnames[index]);
    1.78 @@ -196,7 +196,7 @@
    1.79  }
    1.80  
    1.81  static int
    1.82 -usage_to_joyaxe (unsigned usage)
    1.83 +usage_to_joyaxe(unsigned usage)
    1.84  {
    1.85      int joyaxe;
    1.86      switch (usage) {
    1.87 @@ -231,7 +231,7 @@
    1.88  }
    1.89  
    1.90  static unsigned
    1.91 -hatval_to_sdl (Sint32 hatval)
    1.92 +hatval_to_sdl(Sint32 hatval)
    1.93  {
    1.94      static const unsigned hat_dir_map[8] = {
    1.95          SDL_HAT_UP, SDL_HAT_RIGHTUP, SDL_HAT_RIGHT, SDL_HAT_RIGHTDOWN,
    1.96 @@ -247,7 +247,7 @@
    1.97  
    1.98  
    1.99  int
   1.100 -SDL_SYS_JoystickOpen (SDL_Joystick * joy)
   1.101 +SDL_SYS_JoystickOpen(SDL_Joystick * joy)
   1.102  {
   1.103      char *path = joynames[joy->index];
   1.104      struct joystick_hwdata *hw;
   1.105 @@ -257,29 +257,29 @@
   1.106      int fd;
   1.107      int i;
   1.108  
   1.109 -    fd = open (path, O_RDONLY);
   1.110 +    fd = open(path, O_RDONLY);
   1.111      if (fd == -1) {
   1.112 -        SDL_SetError ("%s: %s", path, strerror (errno));
   1.113 +        SDL_SetError("%s: %s", path, strerror(errno));
   1.114          return (-1);
   1.115      }
   1.116  
   1.117      hw = (struct joystick_hwdata *)
   1.118 -        SDL_malloc (sizeof (struct joystick_hwdata));
   1.119 +        SDL_malloc(sizeof(struct joystick_hwdata));
   1.120      if (hw == NULL) {
   1.121 -        SDL_OutOfMemory ();
   1.122 -        close (fd);
   1.123 +        SDL_OutOfMemory();
   1.124 +        close(fd);
   1.125          return (-1);
   1.126      }
   1.127      joy->hwdata = hw;
   1.128      hw->fd = fd;
   1.129 -    hw->path = strdup (path);
   1.130 -    if (!SDL_strncmp (path, "/dev/joy", 8)) {
   1.131 +    hw->path = strdup(path);
   1.132 +    if (!SDL_strncmp(path, "/dev/joy", 8)) {
   1.133          hw->type = BSDJOY_JOY;
   1.134          joy->naxes = 2;
   1.135          joy->nbuttons = 2;
   1.136          joy->nhats = 0;
   1.137          joy->nballs = 0;
   1.138 -        joydevnames[joy->index] = strdup ("Gameport joystick");
   1.139 +        joydevnames[joy->index] = strdup("Gameport joystick");
   1.140          goto usbend;
   1.141      } else {
   1.142          hw->type = BSDJOY_UHID;
   1.143 @@ -290,32 +290,32 @@
   1.144          for (ax = 0; ax < JOYAXE_count; ax++)
   1.145              hw->axis_map[ax] = -1;
   1.146      }
   1.147 -    hw->repdesc = hid_get_report_desc (fd);
   1.148 +    hw->repdesc = hid_get_report_desc(fd);
   1.149      if (hw->repdesc == NULL) {
   1.150 -        SDL_SetError ("%s: USB_GET_REPORT_DESC: %s", hw->path,
   1.151 -                      strerror (errno));
   1.152 +        SDL_SetError("%s: USB_GET_REPORT_DESC: %s", hw->path,
   1.153 +                     strerror(errno));
   1.154          goto usberr;
   1.155      }
   1.156  
   1.157      rep = &hw->inreport;
   1.158 -    if (ioctl (fd, USB_GET_REPORT_ID, &rep->rid) < 0) {
   1.159 +    if (ioctl(fd, USB_GET_REPORT_ID, &rep->rid) < 0) {
   1.160          rep->rid = -1;          /* XXX */
   1.161      }
   1.162 -    if (report_alloc (rep, hw->repdesc, REPORT_INPUT) < 0) {
   1.163 +    if (report_alloc(rep, hw->repdesc, REPORT_INPUT) < 0) {
   1.164          goto usberr;
   1.165      }
   1.166      if (rep->size <= 0) {
   1.167 -        SDL_SetError ("%s: Input report descriptor has invalid length",
   1.168 -                      hw->path);
   1.169 +        SDL_SetError("%s: Input report descriptor has invalid length",
   1.170 +                     hw->path);
   1.171          goto usberr;
   1.172      }
   1.173  #if defined(USBHID_NEW) || (defined(__FREEBSD__) && __FreeBSD_version >= 500111)
   1.174 -    hdata = hid_start_parse (hw->repdesc, 1 << hid_input, rep->rid);
   1.175 +    hdata = hid_start_parse(hw->repdesc, 1 << hid_input, rep->rid);
   1.176  #else
   1.177 -    hdata = hid_start_parse (hw->repdesc, 1 << hid_input);
   1.178 +    hdata = hid_start_parse(hw->repdesc, 1 << hid_input);
   1.179  #endif
   1.180      if (hdata == NULL) {
   1.181 -        SDL_SetError ("%s: Cannot start HID parser", hw->path);
   1.182 +        SDL_SetError("%s: Cannot start HID parser", hw->path);
   1.183          goto usberr;
   1.184      }
   1.185      joy->naxes = 0;
   1.186 @@ -325,31 +325,31 @@
   1.187      for (i = 0; i < JOYAXE_count; i++)
   1.188          hw->axis_map[i] = -1;
   1.189  
   1.190 -    while (hid_get_item (hdata, &hitem) > 0) {
   1.191 +    while (hid_get_item(hdata, &hitem) > 0) {
   1.192          char *sp;
   1.193          const char *s;
   1.194  
   1.195          switch (hitem.kind) {
   1.196          case hid_collection:
   1.197 -            switch (HID_PAGE (hitem.usage)) {
   1.198 +            switch (HID_PAGE(hitem.usage)) {
   1.199              case HUP_GENERIC_DESKTOP:
   1.200 -                switch (HID_USAGE (hitem.usage)) {
   1.201 +                switch (HID_USAGE(hitem.usage)) {
   1.202                  case HUG_JOYSTICK:
   1.203                  case HUG_GAME_PAD:
   1.204 -                    s = hid_usage_in_page (hitem.usage);
   1.205 -                    sp = SDL_malloc (SDL_strlen (s) + 5);
   1.206 -                    SDL_snprintf (sp, SDL_strlen (s) + 5, "%s (%d)",
   1.207 -                                  s, joy->index);
   1.208 +                    s = hid_usage_in_page(hitem.usage);
   1.209 +                    sp = SDL_malloc(SDL_strlen(s) + 5);
   1.210 +                    SDL_snprintf(sp, SDL_strlen(s) + 5, "%s (%d)",
   1.211 +                                 s, joy->index);
   1.212                      joydevnames[joy->index] = sp;
   1.213                  }
   1.214              }
   1.215              break;
   1.216          case hid_input:
   1.217 -            switch (HID_PAGE (hitem.usage)) {
   1.218 +            switch (HID_PAGE(hitem.usage)) {
   1.219              case HUP_GENERIC_DESKTOP:
   1.220                  {
   1.221 -                    unsigned usage = HID_USAGE (hitem.usage);
   1.222 -                    int joyaxe = usage_to_joyaxe (usage);
   1.223 +                    unsigned usage = HID_USAGE(hitem.usage);
   1.224 +                    int joyaxe = usage_to_joyaxe(usage);
   1.225                      if (joyaxe >= 0) {
   1.226                          hw->axis_map[joyaxe] = 1;
   1.227                      } else if (usage == HUG_HAT_SWITCH) {
   1.228 @@ -368,25 +368,25 @@
   1.229              break;
   1.230          }
   1.231      }
   1.232 -    hid_end_parse (hdata);
   1.233 +    hid_end_parse(hdata);
   1.234      for (i = 0; i < JOYAXE_count; i++)
   1.235          if (hw->axis_map[i] > 0)
   1.236              hw->axis_map[i] = joy->naxes++;
   1.237  
   1.238    usbend:
   1.239      /* The poll blocks the event thread. */
   1.240 -    fcntl (fd, F_SETFL, O_NONBLOCK);
   1.241 +    fcntl(fd, F_SETFL, O_NONBLOCK);
   1.242  
   1.243      return (0);
   1.244    usberr:
   1.245 -    close (hw->fd);
   1.246 -    SDL_free (hw->path);
   1.247 -    SDL_free (hw);
   1.248 +    close(hw->fd);
   1.249 +    SDL_free(hw->path);
   1.250 +    SDL_free(hw);
   1.251      return (-1);
   1.252  }
   1.253  
   1.254  void
   1.255 -SDL_SYS_JoystickUpdate (SDL_Joystick * joy)
   1.256 +SDL_SYS_JoystickUpdate(SDL_Joystick * joy)
   1.257  {
   1.258      struct hid_item hitem;
   1.259      struct hid_data *hdata;
   1.260 @@ -399,10 +399,10 @@
   1.261      static int x, y, xmin = 0xffff, ymin = 0xffff, xmax = 0, ymax = 0;
   1.262  
   1.263      if (joy->hwdata->type == BSDJOY_JOY) {
   1.264 -        if (read (joy->hwdata->fd, &gameport, sizeof gameport) !=
   1.265 +        if (read(joy->hwdata->fd, &gameport, sizeof gameport) !=
   1.266              sizeof gameport)
   1.267              return;
   1.268 -        if (abs (x - gameport.x) > 8) {
   1.269 +        if (abs(x - gameport.x) > 8) {
   1.270              x = gameport.x;
   1.271              if (x < xmin) {
   1.272                  xmin = x;
   1.273 @@ -417,9 +417,9 @@
   1.274              v = (Sint32) x;
   1.275              v -= (xmax + xmin + 1) / 2;
   1.276              v *= 32768 / ((xmax - xmin + 1) / 2);
   1.277 -            SDL_PrivateJoystickAxis (joy, 0, v);
   1.278 +            SDL_PrivateJoystickAxis(joy, 0, v);
   1.279          }
   1.280 -        if (abs (y - gameport.y) > 8) {
   1.281 +        if (abs(y - gameport.y) > 8) {
   1.282              y = gameport.y;
   1.283              if (y < ymin) {
   1.284                  ymin = y;
   1.285 @@ -434,13 +434,13 @@
   1.286              v = (Sint32) y;
   1.287              v -= (ymax + ymin + 1) / 2;
   1.288              v *= 32768 / ((ymax - ymin + 1) / 2);
   1.289 -            SDL_PrivateJoystickAxis (joy, 1, v);
   1.290 +            SDL_PrivateJoystickAxis(joy, 1, v);
   1.291          }
   1.292          if (gameport.b1 != joy->buttons[0]) {
   1.293 -            SDL_PrivateJoystickButton (joy, 0, gameport.b1);
   1.294 +            SDL_PrivateJoystickButton(joy, 0, gameport.b1);
   1.295          }
   1.296          if (gameport.b2 != joy->buttons[1]) {
   1.297 -            SDL_PrivateJoystickButton (joy, 1, gameport.b2);
   1.298 +            SDL_PrivateJoystickButton(joy, 1, gameport.b2);
   1.299          }
   1.300          return;
   1.301      }
   1.302 @@ -448,53 +448,51 @@
   1.303  
   1.304      rep = &joy->hwdata->inreport;
   1.305  
   1.306 -    if (read (joy->hwdata->fd, REP_BUF_DATA (rep), rep->size) != rep->size) {
   1.307 +    if (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) != rep->size) {
   1.308          return;
   1.309      }
   1.310  #if defined(USBHID_NEW) || (defined(__FREEBSD__) && __FreeBSD_version >= 500111)
   1.311 -    hdata = hid_start_parse (joy->hwdata->repdesc, 1 << hid_input, rep->rid);
   1.312 +    hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input, rep->rid);
   1.313  #else
   1.314 -    hdata = hid_start_parse (joy->hwdata->repdesc, 1 << hid_input);
   1.315 +    hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input);
   1.316  #endif
   1.317      if (hdata == NULL) {
   1.318 -        fprintf (stderr, "%s: Cannot start HID parser\n", joy->hwdata->path);
   1.319 +        fprintf(stderr, "%s: Cannot start HID parser\n", joy->hwdata->path);
   1.320          return;
   1.321      }
   1.322  
   1.323 -    for (nbutton = 0; hid_get_item (hdata, &hitem) > 0;) {
   1.324 +    for (nbutton = 0; hid_get_item(hdata, &hitem) > 0;) {
   1.325          switch (hitem.kind) {
   1.326          case hid_input:
   1.327 -            switch (HID_PAGE (hitem.usage)) {
   1.328 +            switch (HID_PAGE(hitem.usage)) {
   1.329              case HUP_GENERIC_DESKTOP:
   1.330                  {
   1.331 -                    unsigned usage = HID_USAGE (hitem.usage);
   1.332 -                    int joyaxe = usage_to_joyaxe (usage);
   1.333 +                    unsigned usage = HID_USAGE(hitem.usage);
   1.334 +                    int joyaxe = usage_to_joyaxe(usage);
   1.335                      if (joyaxe >= 0) {
   1.336                          naxe = joy->hwdata->axis_map[joyaxe];
   1.337                          /* scaleaxe */
   1.338 -                        v = (Sint32) hid_get_data (REP_BUF_DATA (rep),
   1.339 -                                                   &hitem);
   1.340 +                        v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
   1.341                          v -= (hitem.logical_maximum +
   1.342                                hitem.logical_minimum + 1) / 2;
   1.343                          v *= 32768 /
   1.344                              ((hitem.logical_maximum -
   1.345                                hitem.logical_minimum + 1) / 2);
   1.346                          if (v != joy->axes[naxe]) {
   1.347 -                            SDL_PrivateJoystickAxis (joy, naxe, v);
   1.348 +                            SDL_PrivateJoystickAxis(joy, naxe, v);
   1.349                          }
   1.350                      } else if (usage == HUG_HAT_SWITCH) {
   1.351 -                        v = (Sint32) hid_get_data (REP_BUF_DATA (rep),
   1.352 -                                                   &hitem);
   1.353 -                        SDL_PrivateJoystickHat (joy, 0,
   1.354 -                                                hatval_to_sdl (v) -
   1.355 -                                                hitem.logical_minimum);
   1.356 +                        v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
   1.357 +                        SDL_PrivateJoystickHat(joy, 0,
   1.358 +                                               hatval_to_sdl(v) -
   1.359 +                                               hitem.logical_minimum);
   1.360                      }
   1.361                      break;
   1.362                  }
   1.363              case HUP_BUTTON:
   1.364 -                v = (Sint32) hid_get_data (REP_BUF_DATA (rep), &hitem);
   1.365 +                v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
   1.366                  if (joy->buttons[nbutton] != v) {
   1.367 -                    SDL_PrivateJoystickButton (joy, nbutton, v);
   1.368 +                    SDL_PrivateJoystickButton(joy, nbutton, v);
   1.369                  }
   1.370                  nbutton++;
   1.371                  break;
   1.372 @@ -506,77 +504,77 @@
   1.373              break;
   1.374          }
   1.375      }
   1.376 -    hid_end_parse (hdata);
   1.377 +    hid_end_parse(hdata);
   1.378  
   1.379      return;
   1.380  }
   1.381  
   1.382  /* Function to close a joystick after use */
   1.383  void
   1.384 -SDL_SYS_JoystickClose (SDL_Joystick * joy)
   1.385 +SDL_SYS_JoystickClose(SDL_Joystick * joy)
   1.386  {
   1.387 -    if (SDL_strncmp (joy->hwdata->path, "/dev/joy", 8)) {
   1.388 -        report_free (&joy->hwdata->inreport);
   1.389 -        hid_dispose_report_desc (joy->hwdata->repdesc);
   1.390 +    if (SDL_strncmp(joy->hwdata->path, "/dev/joy", 8)) {
   1.391 +        report_free(&joy->hwdata->inreport);
   1.392 +        hid_dispose_report_desc(joy->hwdata->repdesc);
   1.393      }
   1.394 -    close (joy->hwdata->fd);
   1.395 -    SDL_free (joy->hwdata->path);
   1.396 -    SDL_free (joy->hwdata);
   1.397 +    close(joy->hwdata->fd);
   1.398 +    SDL_free(joy->hwdata->path);
   1.399 +    SDL_free(joy->hwdata);
   1.400  
   1.401      return;
   1.402  }
   1.403  
   1.404  void
   1.405 -SDL_SYS_JoystickQuit (void)
   1.406 +SDL_SYS_JoystickQuit(void)
   1.407  {
   1.408      int i;
   1.409  
   1.410      for (i = 0; i < MAX_JOYS; i++) {
   1.411          if (joynames[i] != NULL)
   1.412 -            SDL_free (joynames[i]);
   1.413 +            SDL_free(joynames[i]);
   1.414          if (joydevnames[i] != NULL)
   1.415 -            SDL_free (joydevnames[i]);
   1.416 +            SDL_free(joydevnames[i]);
   1.417      }
   1.418  
   1.419      return;
   1.420  }
   1.421  
   1.422  static int
   1.423 -report_alloc (struct report *r, struct report_desc *rd, int repind)
   1.424 +report_alloc(struct report *r, struct report_desc *rd, int repind)
   1.425  {
   1.426      int len;
   1.427  
   1.428  #ifdef __DragonFly__
   1.429 -    len = hid_report_size (rd, r->rid, repinfo[repind].kind);
   1.430 +    len = hid_report_size(rd, r->rid, repinfo[repind].kind);
   1.431  #elif __FREEBSD__
   1.432  # if (__FreeBSD_version >= 460000)
   1.433  #  if (__FreeBSD_version <= 500111)
   1.434 -    len = hid_report_size (rd, r->rid, repinfo[repind].kind);
   1.435 +    len = hid_report_size(rd, r->rid, repinfo[repind].kind);
   1.436  #  else
   1.437 -    len = hid_report_size (rd, repinfo[repind].kind, r->rid);
   1.438 +    len = hid_report_size(rd, repinfo[repind].kind, r->rid);
   1.439  #  endif
   1.440  # else
   1.441 -    len = hid_report_size (rd, repinfo[repind].kind, &r->rid);
   1.442 +    len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
   1.443  # endif
   1.444  #else
   1.445  # ifdef USBHID_NEW
   1.446 -    len = hid_report_size (rd, repinfo[repind].kind, r->rid);
   1.447 +    len = hid_report_size(rd, repinfo[repind].kind, r->rid);
   1.448  # else
   1.449 -    len = hid_report_size (rd, repinfo[repind].kind, &r->rid);
   1.450 +    len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
   1.451  # endif
   1.452  #endif
   1.453  
   1.454      if (len < 0) {
   1.455 -        SDL_SetError ("Negative HID report size");
   1.456 +        SDL_SetError("Negative HID report size");
   1.457          return (-1);
   1.458      }
   1.459      r->size = len;
   1.460  
   1.461      if (r->size > 0) {
   1.462 -        r->buf = SDL_malloc (sizeof (*r->buf) - sizeof (REP_BUF_DATA (r)) +
   1.463 -                             r->size);
   1.464 +        r->buf = SDL_malloc(sizeof(*r->buf) - sizeof(REP_BUF_DATA(r)) +
   1.465 +                            r->size);
   1.466          if (r->buf == NULL) {
   1.467 -            SDL_OutOfMemory ();
   1.468 +            SDL_OutOfMemory();
   1.469              return (-1);
   1.470          }
   1.471      } else {
   1.472 @@ -588,10 +586,10 @@
   1.473  }
   1.474  
   1.475  static void
   1.476 -report_free (struct report *r)
   1.477 +report_free(struct report *r)
   1.478  {
   1.479      if (r->buf != NULL) {
   1.480 -        SDL_free (r->buf);
   1.481 +        SDL_free(r->buf);
   1.482      }
   1.483      r->status = SREPORT_UNINIT;
   1.484  }