📄 linux.c
字号:
/* DirectInput Joystick device * * Copyright 1998 Marcus Meissner * Copyright 1998,1999 Lionel Ulmer * Copyright 2000-2001 TransGaming Technologies Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#include "config.h"#include "wine/port.h"#ifdef HAVE_LINUX_22_JOYSTICK_API#include <stdarg.h>#include <stdio.h>#include <string.h>#include <time.h>#ifdef HAVE_UNISTD_H# include <unistd.h>#endif#ifdef HAVE_SYS_TIME_H# include <sys/time.h>#endif#include <sys/fcntl.h>#ifdef HAVE_SYS_IOCTL_H# include <sys/ioctl.h>#endif#include <errno.h>#ifdef HAVE_SYS_ERRNO_H# include <sys/errno.h>#endif#ifdef HAVE_LINUX_JOYSTICK_H# include <linux/joystick.h>#endif#define JOYDEV "/dev/js0"#include "wine/debug.h"#include "wine/unicode.h"#include "windef.h"#include "winbase.h"#include "winerror.h"#include "dinput.h"#include "dinput_private.h"#include "device_private.h"WINE_DEFAULT_DEBUG_CHANNEL(dinput);/* Wine joystick driver object instances */#define WINE_JOYSTICK_AXIS_BASE 0#define WINE_JOYSTICK_BUTTON_BASE 8typedef struct JoystickImpl JoystickImpl;static ICOM_VTABLE(IDirectInputDevice8A) JoystickAvt;static ICOM_VTABLE(IDirectInputDevice8W) JoystickWvt;struct JoystickImpl{ LPVOID lpVtbl; DWORD ref; GUID guid; /* The 'parent' DInput */ IDirectInputImpl *dinput; /* joystick private */ int joyfd; LPDIDATAFORMAT df; HANDLE hEvent; LONG lMin,lMax,deadzone; LPDIDEVICEOBJECTDATA data_queue; int queue_head, queue_tail, queue_len; DIJOYSTATE js;};static GUID DInput_Wine_Joystick_GUID = { /* 9e573ed9-7734-11d2-8d4a-23903fb6bdf7 */ 0x9e573ed9, 0x7734, 0x11d2, {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}};static BOOL joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, int version){ int fd = -1; if (dwFlags & DIEDFL_FORCEFEEDBACK) return FALSE; if ((dwDevType==0) || (GET_DIDEVICE_TYPE(dwDevType)==DIDEVTYPE_JOYSTICK)) { /* check whether we have a joystick */ if ((fd = open(JOYDEV,O_RDONLY) != -1) || (errno!=ENODEV && errno!=ENOENT)) { TRACE("Enumerating the linux Joystick device\n"); /* Return joystick */ lpddi->guidInstance = GUID_Joystick; lpddi->guidProduct = DInput_Wine_Joystick_GUID; /* we only support traditional joysticks for now */ lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL<<8); strcpy(lpddi->tszInstanceName, "Joystick"); /* ioctl JSIOCGNAME(len) */ strcpy(lpddi->tszProductName, "Wine Joystick"); lpddi->guidFFDriver = GUID_NULL; if (fd != -1) close(fd); return TRUE; } } return FALSE;}static BOOL joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, int version){ int fd = -1; if (dwFlags & DIEDFL_FORCEFEEDBACK) return FALSE; if ((dwDevType==0) || (GET_DIDEVICE_TYPE(dwDevType)==DIDEVTYPE_JOYSTICK)) { /* check whether we have a joystick */ if ((fd = open(JOYDEV,O_RDONLY) != -1) || (errno!=ENODEV && errno!=ENOENT)) { TRACE("Enumerating the linux Joystick device\n"); /* Return joystick */ lpddi->guidInstance = GUID_Joystick; lpddi->guidProduct = DInput_Wine_Joystick_GUID; /* we only support traditional joysticks for now */ lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL<<8); MultiByteToWideChar(CP_ACP, 0, "Joystick", -1, lpddi->tszInstanceName, MAX_PATH); /* ioctl JSIOCGNAME(len) */ MultiByteToWideChar(CP_ACP, 0, "Wine Joystick", -1, lpddi->tszProductName, MAX_PATH); lpddi->guidFFDriver = GUID_NULL; if (fd != -1) close(fd); return TRUE; } } return FALSE;}static JoystickImpl *alloc_device(REFGUID rguid, LPVOID jvt, IDirectInputImpl *dinput){ JoystickImpl* newDevice; newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl)); newDevice->lpVtbl = jvt; newDevice->ref = 1; newDevice->joyfd = -1; newDevice->lMin = -32768; newDevice->lMax = +32767; newDevice->dinput = dinput; memcpy(&(newDevice->guid),rguid,sizeof(*rguid)); return newDevice;}static HRESULT joydev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev){ if ((IsEqualGUID(&GUID_Joystick,rguid)) || (IsEqualGUID(&DInput_Wine_Joystick_GUID,rguid))) { if ((riid == NULL) || IsEqualGUID(&IID_IDirectInputDeviceA,riid) || IsEqualGUID(&IID_IDirectInputDevice2A,riid) || IsEqualGUID(&IID_IDirectInputDevice7A,riid) || IsEqualGUID(&IID_IDirectInputDevice8A,riid)) { *pdev=(IDirectInputDeviceA*) alloc_device(rguid, &JoystickAvt, dinput); TRACE("Creating a Joystick device (%p)\n", *pdev); return DI_OK; } else return DIERR_NOINTERFACE; } return DIERR_DEVICENOTREG;}static HRESULT joydev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev){ if ((IsEqualGUID(&GUID_Joystick,rguid)) || (IsEqualGUID(&DInput_Wine_Joystick_GUID,rguid))) { if ((riid == NULL) || IsEqualGUID(&IID_IDirectInputDeviceW,riid) || IsEqualGUID(&IID_IDirectInputDevice2W,riid) || IsEqualGUID(&IID_IDirectInputDevice7W,riid) || IsEqualGUID(&IID_IDirectInputDevice8W,riid)) { *pdev = (IDirectInputDeviceW*) alloc_device(rguid, &JoystickWvt, dinput); TRACE("Creating a Joystick device (%p)\n", *pdev); return DI_OK; } else return DIERR_NOINTERFACE; } return DIERR_DEVICENOTREG;}static dinput_device joydev = { 10, joydev_enum_deviceA, joydev_enum_deviceW, joydev_create_deviceA, joydev_create_deviceW};DECL_GLOBAL_CONSTRUCTOR(joydev_register) { dinput_register_device(&joydev); }/****************************************************************************** * Joystick */static ULONG WINAPI JoystickAImpl_Release(LPDIRECTINPUTDEVICE8A iface){ ICOM_THIS(JoystickImpl,iface); This->ref--; if (This->ref) return This->ref; /* Free the data queue */ if (This->data_queue != NULL) HeapFree(GetProcessHeap(),0,This->data_queue); /* Free the DataFormat */ HeapFree(GetProcessHeap(), 0, This->df); HeapFree(GetProcessHeap(),0,This); return 0;}/****************************************************************************** * SetDataFormat : the application can choose the format of the data * the device driver sends back with GetDeviceState. */static HRESULT WINAPI JoystickAImpl_SetDataFormat( LPDIRECTINPUTDEVICE8A iface,LPCDIDATAFORMAT df){ ICOM_THIS(JoystickImpl,iface); int i; TRACE("(this=%p,%p)\n",This,df); TRACE("(df.dwSize=%ld)\n",df->dwSize); TRACE("(df.dwObjsize=%ld)\n",df->dwObjSize); TRACE("(df.dwFlags=0x%08lx)\n",df->dwFlags); TRACE("(df.dwDataSize=%ld)\n",df->dwDataSize); TRACE("(df.dwNumObjs=%ld)\n",df->dwNumObjs); for (i=0;i<df->dwNumObjs;i++) { TRACE("df.rgodf[%d].guid %s (%p)\n",i,debugstr_guid(df->rgodf[i].pguid), df->rgodf[i].pguid); TRACE("df.rgodf[%d].dwOfs %ld\n",i,df->rgodf[i].dwOfs); TRACE("dwType 0x%02x,dwInstance %d\n",DIDFT_GETTYPE(df->rgodf[i].dwType),DIDFT_GETINSTANCE(df->rgodf[i].dwType)); TRACE("df.rgodf[%d].dwFlags 0x%08lx\n",i,df->rgodf[i].dwFlags); } /* Store the new data format */ This->df = HeapAlloc(GetProcessHeap(),0,df->dwSize); memcpy(This->df, df, df->dwSize); This->df->rgodf = HeapAlloc(GetProcessHeap(),0,df->dwNumObjs*df->dwObjSize); memcpy(This->df->rgodf,df->rgodf,df->dwNumObjs*df->dwObjSize); return 0;}/****************************************************************************** * Acquire : gets exclusive control of the joystick */static HRESULT WINAPI JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface){ ICOM_THIS(JoystickImpl,iface); TRACE("(this=%p)\n",This); if (This->joyfd!=-1) return 0; This->joyfd=open(JOYDEV,O_RDONLY); if (This->joyfd==-1) return DIERR_NOTFOUND; return 0;}/****************************************************************************** * Unacquire : frees the joystick */static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface){ ICOM_THIS(JoystickImpl,iface); TRACE("(this=%p)\n",This); if (This->joyfd!=-1) { close(This->joyfd); This->joyfd = -1; } return DI_OK;}#define map_axis(val) ((val+32768)*(This->lMax-This->lMin)/65536+This->lMin)static void joy_polldev(JoystickImpl *This) { struct timeval tv; fd_set readfds; struct js_event jse; if (This->joyfd==-1) return; while (1) { memset(&tv,0,sizeof(tv)); FD_ZERO(&readfds);FD_SET(This->joyfd,&readfds); if (1>select(This->joyfd+1,&readfds,NULL,NULL,&tv)) return; /* we have one event, so we can read */ if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) { return; } TRACE("js_event: type 0x%x, number %d, value %d\n",jse.type,jse.number,jse.value); if (jse.type & JS_EVENT_BUTTON) { GEN_EVENT(DIJOFS_BUTTON(jse.number),jse.value?0x80:0x00,jse.time,(This->dinput->evsequence)++); This->js.rgbButtons[jse.number] = jse.value?0x80:0x00; } if (jse.type & JS_EVENT_AXIS) { switch (jse.number) { case 0: GEN_EVENT(jse.number*4,jse.value,jse.time,(This->dinput->evsequence)++); This->js.lX = map_axis(jse.value); break; case 1: GEN_EVENT(jse.number*4,jse.value,jse.time,(This->dinput->evsequence)++); This->js.lY = map_axis(jse.value); break; case 2: GEN_EVENT(jse.number*4,jse.value,jse.time,(This->dinput->evsequence)++); This->js.lZ = map_axis(jse.value); break; default: FIXME("more than 3 axes (%d) not handled!\n",jse.number); break; } } }}/****************************************************************************** * GetDeviceState : returns the "state" of the joystick. * */static HRESULT WINAPI JoystickAImpl_GetDeviceState( LPDIRECTINPUTDEVICE8A iface,DWORD len,LPVOID ptr
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -