int Android_AddJoystick(int device_id, const char *name, const char *desc, SDL_bool is_accelerometer, int nbuttons, int naxes, int nhats, int nballs) { SDL_JoystickGUID guid; SDL_joylist_item *item; if(JoystickByDeviceId(device_id) != NULL || name == NULL) { return -1; } /* the GUID is just the first 16 chars of the name for now */ SDL_zero( guid ); SDL_memcpy( &guid, desc, SDL_min( sizeof(guid), SDL_strlen( desc) ) ); item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item)); if (item == NULL) { return -1; } SDL_zerop(item); item->guid = guid; item->device_id = device_id; item->name = SDL_strdup(name); if ( item->name == NULL ) { SDL_free(item); return -1; } item->is_accelerometer = is_accelerometer; if (nbuttons > -1) { item->nbuttons = nbuttons; } else { item->nbuttons = ANDROID_MAX_NBUTTONS; } item->naxes = naxes; item->nhats = nhats; item->nballs = nballs; item->device_instance = instance_counter++; if (SDL_joylist_tail == NULL) { SDL_joylist = SDL_joylist_tail = item; } else { SDL_joylist_tail->next = item; SDL_joylist_tail = item; } /* Need to increment the joystick count before we post the event */ ++numjoysticks; SDL_PrivateJoystickAdded(numjoysticks - 1); #ifdef DEBUG_JOYSTICK SDL_Log("Added joystick %s with device_id %d", name, device_id); #endif return numjoysticks; }
static SDL_bool SteamControllerConnectedCallback(const char *name, SDL_JoystickGUID guid, int *device_instance) { SDL_joylist_item *item; item = (SDL_joylist_item *) SDL_calloc(1, sizeof (SDL_joylist_item)); if (item == NULL) { return SDL_FALSE; } item->path = SDL_strdup(""); item->name = SDL_strdup(name); item->guid = guid; item->m_bSteamController = SDL_TRUE; if ((item->path == NULL) || (item->name == NULL)) { SDL_free(item->path); SDL_free(item->name); SDL_free(item); return SDL_FALSE; } *device_instance = item->device_instance = instance_counter++; if (SDL_joylist_tail == NULL) { SDL_joylist = SDL_joylist_tail = item; } else { SDL_joylist_tail->next = item; SDL_joylist_tail = item; } /* Need to increment the joystick count before we post the event */ ++numjoysticks; SDL_PrivateJoystickAdded(numjoysticks - 1); return SDL_TRUE; }
/* !!! FIXME: I would love to dump this code and use libudev instead. */ static int MaybeAddDevice(const char *path) { struct stat sb; int fd = -1; int isstick = 0; char namebuf[128]; SDL_JoystickGUID guid; SDL_joylist_item *item; if (path == NULL) { return -1; } if (stat(path, &sb) == -1) { return -1; } /* Check to make sure it's not already in list. */ for (item = SDL_joylist; item != NULL; item = item->next) { if (sb.st_rdev == item->devnum) { return -1; /* already have this one */ } } fd = open(path, O_RDONLY, 0); if (fd < 0) { return -1; } #ifdef DEBUG_INPUT_EVENTS printf("Checking %s\n", path); #endif isstick = IsJoystick(fd, namebuf, sizeof (namebuf), &guid); close(fd); if (!isstick) { return -1; } item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item)); if (item == NULL) { return -1; } SDL_zerop(item); item->devnum = sb.st_rdev; item->path = SDL_strdup(path); item->name = SDL_strdup(namebuf); item->guid = guid; if ( (item->path == NULL) || (item->name == NULL) ) { SDL_free(item->path); SDL_free(item->name); SDL_free(item); return -1; } item->device_instance = instance_counter++; if (SDL_joylist_tail == NULL) { SDL_joylist = SDL_joylist_tail = item; } else { SDL_joylist_tail->next = item; SDL_joylist_tail = item; } /* Need to increment the joystick count before we post the event */ ++numjoysticks; SDL_PrivateJoystickAdded(numjoysticks - 1); return numjoysticks; }
static void JoystickDeviceWasAddedCallback(void *ctx, IOReturn res, void *sender, IOHIDDeviceRef ioHIDDeviceObject) { recDevice *device; int device_index = 0; io_service_t ioservice; if (res != kIOReturnSuccess) { return; } if (JoystickAlreadyKnown(ioHIDDeviceObject)) { return; /* IOKit sent us a duplicate. */ } device = (recDevice *) SDL_calloc(1, sizeof(recDevice)); if (!device) { SDL_OutOfMemory(); return; } if (!GetDeviceInfo(ioHIDDeviceObject, device)) { SDL_free(device); return; /* not a device we care about, probably. */ } if (SDL_IsGameControllerNameAndGUID(device->product, device->guid) && SDL_ShouldIgnoreGameController(device->product, device->guid)) { SDL_free(device); return; } /* Get notified when this device is disconnected. */ IOHIDDeviceRegisterRemovalCallback(ioHIDDeviceObject, JoystickDeviceWasRemovedCallback, device); IOHIDDeviceScheduleWithRunLoop(ioHIDDeviceObject, CFRunLoopGetCurrent(), SDL_JOYSTICK_RUNLOOP_MODE); /* Allocate an instance ID for this device */ device->instance_id = ++s_joystick_instance_id; /* We have to do some storage of the io_service_t for SDL_HapticOpenFromJoystick */ ioservice = IOHIDDeviceGetService(ioHIDDeviceObject); #if SDL_HAPTIC_IOKIT if ((ioservice) && (FFIsForceFeedback(ioservice) == FF_OK)) { device->ffservice = ioservice; MacHaptic_MaybeAddDevice(ioservice); } #endif /* Add device to the end of the list */ if ( !gpDeviceList ) { gpDeviceList = device; } else { recDevice *curdevice; curdevice = gpDeviceList; while ( curdevice->pNext ) { ++device_index; curdevice = curdevice->pNext; } curdevice->pNext = device; ++device_index; /* bump by one since we counted by pNext. */ } SDL_PrivateJoystickAdded(device_index); }
static EM_BOOL Emscripten_JoyStickConnected(int eventType, const EmscriptenGamepadEvent *gamepadEvent, void *userData) { int i; SDL_joylist_item *item; if (JoystickByIndex(gamepadEvent->index) != NULL) { return 1; } item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item)); if (item == NULL) { return 1; } SDL_zerop(item); item->index = gamepadEvent->index; item->name = SDL_strdup(gamepadEvent->id); if ( item->name == NULL ) { SDL_free(item); return 1; } item->mapping = SDL_strdup(gamepadEvent->mapping); if ( item->mapping == NULL ) { SDL_free(item->name); SDL_free(item); return 1; } item->naxes = gamepadEvent->numAxes; item->nbuttons = gamepadEvent->numButtons; item->device_instance = instance_counter++; item->timestamp = gamepadEvent->timestamp; for( i = 0; i < item->naxes; i++) { item->axis[i] = gamepadEvent->axis[i]; } for( i = 0; i < item->nbuttons; i++) { item->analogButton[i] = gamepadEvent->analogButton[i]; item->digitalButton[i] = gamepadEvent->digitalButton[i]; } if (SDL_joylist_tail == NULL) { SDL_joylist = SDL_joylist_tail = item; } else { SDL_joylist_tail->next = item; SDL_joylist_tail = item; } ++numjoysticks; SDL_PrivateJoystickAdded(numjoysticks - 1); #ifdef DEBUG_JOYSTICK SDL_Log("Number of joysticks is %d", numjoysticks); #endif #ifdef DEBUG_JOYSTICK SDL_Log("Added joystick with index %d", item->index); #endif return 1; }