Exemplo n.º 1
0
void Gamepad_processEvents() {
	unsigned int eventIndex;
	static bool inProcessEvents;
	
	if (!inited || inProcessEvents) {
		return;
	}
	
	inProcessEvents = true;
	pthread_mutex_lock(&eventQueueMutex);
	for (eventIndex = 0; eventIndex < eventCount; eventIndex++) {
		processQueuedEvent(eventQueue[eventIndex]);
		if (eventQueue[eventIndex].eventType == GAMEPAD_EVENT_DEVICE_REMOVED) {
			disposeDevice(eventQueue[eventIndex].eventData);
			
		} else if (eventQueue[eventIndex].eventType == GAMEPAD_EVENT_BUTTON_DOWN ||
		           eventQueue[eventIndex].eventType == GAMEPAD_EVENT_BUTTON_UP ||
		           eventQueue[eventIndex].eventType == GAMEPAD_EVENT_AXIS_MOVED) {
			free(eventQueue[eventIndex].eventData);
		}
	}
	eventCount = 0;
	pthread_mutex_unlock(&eventQueueMutex);
	inProcessEvents = false;
}
Exemplo n.º 2
0
void Gamepad_processEvents() {
	unsigned int deviceIndex;
	static bool inProcessEvents;
	JOYINFOEX info;
	MMRESULT result;
	struct Gamepad_device * device;
	struct Gamepad_devicePrivate * devicePrivate;
	
	if (!inited || inProcessEvents) {
		return;
	}
	
	inProcessEvents = true;
	for (deviceIndex = 0; deviceIndex < numDevices; deviceIndex++) {
		device = devices[deviceIndex];
		devicePrivate = device->privateData;
		
		info.dwSize = sizeof(info);
		info.dwFlags = JOY_RETURNALL;
		result = joyGetPosEx(devicePrivate->joystickID, &info);
		if (result == JOYERR_UNPLUGGED) {
			Gamepad_eventDispatcher()->dispatchEvent(Gamepad_eventDispatcher(), Atom_fromString(GAMEPAD_EVENT_DEVICE_REMOVED), device);
			
			disposeDevice(device);
			numDevices--;
			for (; deviceIndex < numDevices; deviceIndex++) {
				devices[deviceIndex] = devices[deviceIndex + 1];
			}
			
		} else if (result == JOYERR_NOERROR) {
			if (info.dwXpos != devicePrivate->lastState.dwXpos) {
				handleAxisChange(device, devicePrivate->xAxisIndex, info.dwXpos);
			}
			if (info.dwYpos != devicePrivate->lastState.dwYpos) {
				handleAxisChange(device, devicePrivate->yAxisIndex, info.dwYpos);
			}
			if (info.dwZpos != devicePrivate->lastState.dwZpos) {
				handleAxisChange(device, devicePrivate->zAxisIndex, info.dwZpos);
			}
			if (info.dwRpos != devicePrivate->lastState.dwRpos) {
				handleAxisChange(device, devicePrivate->rAxisIndex, info.dwRpos);
			}
			if (info.dwUpos != devicePrivate->lastState.dwUpos) {
				handleAxisChange(device, devicePrivate->uAxisIndex, info.dwUpos);
			}
			if (info.dwVpos != devicePrivate->lastState.dwVpos) {
				handleAxisChange(device, devicePrivate->vAxisIndex, info.dwVpos);
			}
			if (info.dwPOV != devicePrivate->lastState.dwPOV) {
				handlePOVChange(device, devicePrivate->lastState.dwPOV, info.dwPOV);
			}
			if (info.dwButtons != devicePrivate->lastState.dwButtons) {
				handleButtonChange(device, devicePrivate->lastState.dwButtons, info.dwButtons);
			}
			devicePrivate->lastState = info;
		}
	}
	inProcessEvents = false;
}
Exemplo n.º 3
0
void Gamepad_shutdown() {
	unsigned int deviceIndex;
	
	if (inited) {
		for (deviceIndex = 0; deviceIndex < numDevices; deviceIndex++) {
			disposeDevice(devices[deviceIndex]);
		}
		free(devices);
		devices = NULL;
		numDevices = 0;
		inited = false;
	}
}
Exemplo n.º 4
0
void Gamepad_shutdown() {
    if (inited) {
        unsigned int eventIndex;
        unsigned int devicesLeft;
        unsigned int gamepadIndex;

        do {
            pthread_mutex_lock(&devicesMutex);
            devicesLeft = numDevices;
            if (devicesLeft > 0) {
                pthread_t thread;

                thread = ((struct Gamepad_devicePrivate *) devices[0]->privateData)->thread;
                pthread_cancel(thread);
                pthread_join(thread, NULL);

                numDevices--;
                for (gamepadIndex = 0; gamepadIndex < numDevices; gamepadIndex++) {
                    devices[gamepadIndex] = devices[gamepadIndex + 1];
                }
            }
            pthread_mutex_unlock(&devicesMutex);
        } while (devicesLeft > 0);

        pthread_mutex_destroy(&devicesMutex);
        pthread_mutex_destroy(&eventQueueMutex);
        free(devices);
        devices = NULL;

        for (eventIndex = 0; eventIndex < eventCount; eventIndex++) {
            if (!strcmp(eventQueue[eventIndex].eventType, GAMEPAD_EVENT_DEVICE_REMOVED)) {
                disposeDevice((Gamepad_device*)eventQueue[eventIndex].eventData);
            }
        }

        eventQueueSize = 0;
        eventCount = 0;
        free(eventQueue);
        eventQueue = NULL;

        if (eventDispatcher != NULL) {
            eventDispatcher->dispose(eventDispatcher);
            free(eventDispatcher);
            eventDispatcher = NULL;
        }

        inited = false;
    }
}
Exemplo n.º 5
0
static void onDeviceRemoved(void * context, IOReturn result, void * sender, IOHIDDeviceRef device) {
	unsigned int deviceIndex;
	
	for (deviceIndex = 0; deviceIndex < numDevices; deviceIndex++) {
		if (((struct Gamepad_devicePrivate *) devices[deviceIndex]->privateData)->deviceRef == device) {
			Gamepad_eventDispatcher()->dispatchEvent(Gamepad_eventDispatcher(), GAMEPAD_EVENT_DEVICE_REMOVED, devices[deviceIndex]);
			
			disposeDevice(devices[deviceIndex]);
			numDevices--;
			for (; deviceIndex < numDevices; deviceIndex++) {
				devices[deviceIndex] = devices[deviceIndex + 1];
			}
			return;
		}
	}
}
Exemplo n.º 6
0
void Gamepad_processEvents() {
    unsigned int eventIndex;

    if (!inited) {
        return;
    }

    pthread_mutex_lock(&eventQueueMutex);
    for (eventIndex = 0; eventIndex < eventCount; eventIndex++) {
        eventQueue[eventIndex].dispatcher->dispatchEvent(eventQueue[eventIndex].dispatcher, eventQueue[eventIndex].eventType, eventQueue[eventIndex].eventData);
        if (!strcmp(eventQueue[eventIndex].eventType, GAMEPAD_EVENT_DEVICE_REMOVED)) {
            disposeDevice((Gamepad_device*)eventQueue[eventIndex].eventData);
        }
    }
    eventCount = 0;
    pthread_mutex_unlock(&eventQueueMutex);
}
Exemplo n.º 7
0
void Gamepad_shutdown() {
	if (hidManager != NULL) {
		unsigned int deviceIndex;
		
		IOHIDManagerUnscheduleFromRunLoop(hidManager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
		IOHIDManagerClose(hidManager, 0);
		CFRelease(hidManager);
		hidManager = NULL;
		
		for (deviceIndex = 0; deviceIndex < numDevices; deviceIndex++) {
			disposeDevice(devices[deviceIndex]);
		}
		free(devices);
		devices = NULL;
		numDevices = 0;
	}
}
Exemplo n.º 8
0
void Gamepad_shutdown() {
	unsigned int deviceIndex;
	
	if (inited) {
		for (deviceIndex = 0; deviceIndex < numDevices; deviceIndex++) {
			disposeDevice(devices[deviceIndex]);
		}
		free(devices);
		devices = NULL;
		numDevices = 0;
		if (eventDispatcher != NULL) {
			eventDispatcher->dispose(eventDispatcher);
			eventDispatcher = NULL;
		}
		inited = false;
	}
}
Exemplo n.º 9
0
static void onDeviceRemoved(void * context, IOReturn result, void * sender, IOHIDDeviceRef device) {
  unsigned int deviceIndex;
	
  for (deviceIndex = 0; deviceIndex < numDevices; deviceIndex++) {
    if (((struct Gamepad_devicePrivate *) devices[deviceIndex]->privateData)->deviceRef == device) {
      if (Gamepad_deviceRemoveCallback != NULL) {
        Gamepad_deviceRemoveCallback(devices[deviceIndex], Gamepad_deviceRemoveContext);
      }
			
      disposeDevice(devices[deviceIndex]);
      numDevices--;
      for (; deviceIndex < numDevices; deviceIndex++) {
        devices[deviceIndex] = devices[deviceIndex + 1];
      }
      return;
    }
  }
}