From a110d663a2d04cefef11469b627e69eecde49867 Mon Sep 17 00:00:00 2001 From: Szymon Wilczek Date: Sat, 2 Aug 2008 14:02:28 +0000 Subject: [PATCH] comments added and improved code look(windows part) --- src/events/SDL_mouse.c | 30 +++++++---- src/video/win32/SDL_win32events.c | 19 +++++-- src/video/win32/SDL_win32mouse.c | 86 ++++++++++++++++++------------- src/video/win32/SDL_win32video.c | 8 +-- src/video/win32/SDL_win32window.c | 81 ++++++++++++++++------------- 5 files changed, 134 insertions(+), 90 deletions(-) diff --git a/src/events/SDL_mouse.c b/src/events/SDL_mouse.c index 097ed5f5b..bccec67f4 100644 --- a/src/events/SDL_mouse.c +++ b/src/events/SDL_mouse.c @@ -33,8 +33,8 @@ static int SDL_current_mouse=-1; static SDL_Mouse **SDL_mice=NULL; static int *SDL_IdIndex=NULL; static int SDL_highestId=-1; -static int last_x, last_y; -int x_max, y_max; +static int last_x, last_y;/*the last reported x and y coordinates by the system cursor*/ +int x_max, y_max; /*current window width and height*/ /* Public functions */ int SDL_MouseInit(void) @@ -77,6 +77,8 @@ SDL_AddMouse(const SDL_Mouse * mouse, int index, char* name,int pressure_max,int return -1; } *SDL_mice[index] = *mouse; + + /*we're setting the mouse properties*/ length=0; length=SDL_strlen(name); SDL_mice[index]->name=SDL_malloc((length+1)*sizeof(char)); @@ -90,7 +92,11 @@ SDL_AddMouse(const SDL_Mouse * mouse, int index, char* name,int pressure_max,int SDL_CreateCursor(default_cdata, default_cmask, DEFAULT_CWIDTH, DEFAULT_CHEIGHT, DEFAULT_CHOTX, DEFAULT_CHOTY); SDL_SetCursor(SDL_mice[index]->def_cursor); + /*we're assuming that all mouses are in the computer sensing zone*/ SDL_mice[index]->proximity=SDL_TRUE; + /*we're assuming that all mouses are working in the absolute position mode + thanx to that, the users that don't want to use many mouses don't have to + worry about anything*/ SDL_mice[index]->relative_mode=SDL_FALSE; SDL_SelectMouse(selected_mouse); @@ -361,6 +367,8 @@ SDL_SendMouseMotion(int id, int relative, int x, int y,int z) int posted; int xrel; int yrel; + /*while using the relative mode and many windows, we have to be sure, + that the pointers find themselves inside the windows*/ if(x>x_max) { x=x_max; @@ -369,23 +377,23 @@ SDL_SendMouseMotion(int id, int relative, int x, int y,int z) { y=y_max; } + if (!mouse || mouse->flush_motion) { return 0; } + + /*if the mouse is out of proximity we don't to want to have any motion from it*/ if(mouse->proximity==SDL_FALSE) { last_x=x; last_y=y; return 0; } - if (mouse->relative_mode==SDL_TRUE && mouse->proximity==SDL_TRUE) { - /* Push the cursor around */ - xrel = x - last_x; - yrel = y - last_y; - } else { - xrel = x - last_x; - yrel = y - last_y; - } + + /*the relative motion is calculated regarding the system cursor last position*/ + + xrel = x - last_x; + yrel = y - last_y; /* Drop events that don't change state */ if (!xrel && !yrel) { @@ -395,7 +403,7 @@ SDL_SendMouseMotion(int id, int relative, int x, int y,int z) return 0; } - /* Update internal mouse state */ + /* Update internal mouse coordinates */ if (mouse->relative_mode==SDL_FALSE) { mouse->x = x; mouse->y = y; diff --git a/src/video/win32/SDL_win32events.c b/src/video/win32/SDL_win32events.c index 41dd0a252..df4ebe2a2 100644 --- a/src/video/win32/SDL_win32events.c +++ b/src/video/win32/SDL_win32events.c @@ -64,7 +64,7 @@ extern HANDLE* mice; extern int total_mice; extern int tablet; -int pressure=0; +int pressure=0;/*the pressure reported by the tablet*/ static WPARAM RemapVKEY(WPARAM wParam, LPARAM lParam) @@ -138,6 +138,7 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) switch (msg) { case WT_PACKET: { + /*if we receive such data we need to update the pressure*/ if (WTPacket((HCTX)lParam, wParam, &packet)) { pressure=(int)packet.pkNormalPressure; @@ -146,10 +147,12 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) break; case WT_PROXIMITY: { + /*checking where the proximity message showed up*/ int h_context=LOWORD(lParam); LPPOINT point; GetCursorPos(&point); ScreenToClient(hwnd, &point); + /*are we in proximity or out of proximity*/ if(h_context==0) { SDL_SendProximity(tablet, (int)(&point->x),(int)(&point->y), SDL_PROXIMITYOUT); @@ -206,7 +209,7 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) return (0); } break; - case WM_INPUT: + case WM_INPUT:/*mouse events*/ { LPBYTE lpb; int w, h; @@ -214,16 +217,19 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) int index; int i; int size=0; - SDL_Mouse *mouse; const RAWMOUSE *raw_mouse=NULL; LPPOINT point; USHORT flags; + + /*we're collecting data from the mouse*/ GetRawInputData((HRAWINPUT) lParam, RID_INPUT, NULL, &size, sizeof (RAWINPUTHEADER)); lpb = SDL_malloc(size*sizeof(LPBYTE)); GetRawInputData((HRAWINPUT) lParam, RID_INPUT, lpb, &size, sizeof (RAWINPUTHEADER)); raw = (RAWINPUT *) lpb; header = &raw->header; flags=raw->data.mouse.usButtonFlags; + + /*we're checking which mouse generated the event*/ for(i=0;ihDevice) @@ -232,11 +238,13 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) break; } } - mouse = SDL_GetMouse(index); + GetCursorPos(&point); ScreenToClient(hwnd, &point); SDL_GetWindowSize(data->windowID, &w, &h); - SDL_UpdateCoordinates(w,h); + SDL_UpdateCoordinates(w,h);/*we're updating the current window size*/ + + /*if the message was sent by a tablet we have to send also pressure*/ if(i==tablet) { SDL_SendMouseMotion(index,0,(int)(&point->x),(int)(&point->y),pressure); @@ -245,6 +253,7 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { SDL_SendMouseMotion(index,0,(int)(&point->x),(int)(&point->y),0); } + /*we're sending mouse buttons messages to check up if sth changed*/ if(flags & RI_MOUSE_BUTTON_1_DOWN) { SDL_SendMouseButton(index,SDL_PRESSED,SDL_BUTTON_LEFT); diff --git a/src/video/win32/SDL_win32mouse.c b/src/video/win32/SDL_win32mouse.c index fabe10b5e..115a78993 100644 --- a/src/video/win32/SDL_win32mouse.c +++ b/src/video/win32/SDL_win32mouse.c @@ -20,6 +20,8 @@ slouken@libsdl.org */ +/*we need to define it, so that raw input is included*/ + #if (_WIN32_WINNT < 0x0501) #undef _WIN32_WINNT #define _WIN32_WINNT 0x0501 @@ -51,10 +53,12 @@ WIN_InitMouse(_THIS) int i; int tmp=0; char* buffer=NULL; - char* tab="wacom"; + char* tab="wacom";/*since windows does't give us handles to tablets, we have to detect a tablet by it's name*/ SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; + /*we're checking for the number of rawinput devices*/ + if(GetRawInputDeviceList(NULL,&devCount,sizeof(RAWINPUTDEVICELIST))) { return; @@ -64,10 +68,14 @@ WIN_InitMouse(_THIS) deviceList = SDL_malloc(sizeof(RAWINPUTDEVICELIST)*devCount); } + /*we're getting the raw input device list*/ + GetRawInputDeviceList(deviceList,&devCount,sizeof(RAWINPUTDEVICELIST)); mice = SDL_malloc(devCount*sizeof(HANDLE)); + /*we're getting the details of the devices*/ + for(i=0;imouse = SDL_AddMouse(&mouse, index,device_name,pressure.axMax,pressure.axMin); - } - else - { - data->mouse = SDL_AddMouse(&mouse, index,device_name,0,0); - } - ++index; + } + /*if it's a tablet, let's read it's maximum and minimum pressure*/ + if(tablet==index) + { + AXIS pressure; + WTInfo(WTI_DEVICES,DVC_NPRESSURE, &pressure); + data->mouse = SDL_AddMouse(&mouse, index,device_name,pressure.axMax,pressure.axMin); + } + else + { + data->mouse = SDL_AddMouse(&mouse, index,device_name,0,0); + } + ++index; - SDL_free(buffer); - SDL_free(key_name); + SDL_free(buffer); + SDL_free(key_name); } total_mice=index; SDL_free(deviceList); @@ -179,10 +197,8 @@ WIN_QuitMouse(_THIS) { int i; SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; - for(i=0;i +/*we're telling wintab that we want to receive movement, button events and pressure information in packets*/ #define PACKETDATA ( PK_X | PK_Y | PK_BUTTONS | PK_NORMAL_PRESSURE) #define PACKETMODE 0 #include -extern HCTX* g_hCtx; +extern HCTX* g_hCtx; /*the table of tablet event contexts, each windows has to have it's own tablet context*/ -int highestId=0; +int highestId=0; /*the highest id of the tablet context*/ static int SetupWindowData(_THIS, SDL_Window * window, HWND hwnd, SDL_bool created) @@ -198,46 +201,53 @@ WIN_CreateWindow(_THIS, SDL_Window * window) CreateWindow(SDL_Appname, TEXT(""), style, x, y, w, h, NULL, NULL, SDL_Instance, NULL); - WTInfo(WTI_DEFSYSCTX, 0, &lc); - lc.lcPktData = PACKETDATA; - lc.lcPktMode = PACKETMODE; - lc.lcOptions |= CXO_MESSAGES; - lc.lcOptions |= CXO_SYSTEM; - lc.lcMoveMask = PACKETDATA; - lc.lcBtnDnMask=lc.lcBtnUpMask = PACKETDATA; - - WTInfo(WTI_DEVICES,DVC_X,&TabX); - WTInfo(WTI_DEVICES,DVC_Y,&TabY); - - lc.lcInOrgX = 0; - lc.lcInOrgY = 0; + /*we're configuring the tablet data. See Wintab reference for more info*/ - lc.lcInExtX = TabX.axMax; - lc.lcInExtY = TabY.axMax; - - lc.lcOutOrgX = 0; - lc.lcOutOrgY = 0; - - lc.lcOutExtX = GetSystemMetrics(SM_CXSCREEN); - lc.lcOutExtY = -GetSystemMetrics(SM_CYSCREEN); - if(window->id>highestId) + if(WTInfo(WTI_DEFSYSCTX, 0, &lc)!=0) { - HCTX* tmp_hctx; - highestId=window->id; - tmp_hctx= (HCTX*)SDL_realloc(g_hCtx,(highestId+1)*sizeof(HCTX)); - if (!tmp_hctx) { - SDL_OutOfMemory(); - return -1; - } - g_hCtx=tmp_hctx; + + lc.lcPktData = PACKETDATA; + lc.lcPktMode = PACKETMODE; + lc.lcOptions |= CXO_MESSAGES; + lc.lcOptions |= CXO_SYSTEM; + lc.lcMoveMask = PACKETDATA; + lc.lcBtnDnMask=lc.lcBtnUpMask = PACKETDATA; + + WTInfo(WTI_DEVICES,DVC_X,&TabX); + WTInfo(WTI_DEVICES,DVC_Y,&TabY); + + lc.lcInOrgX = 0; + lc.lcInOrgY = 0; + + lc.lcInExtX = TabX.axMax; + lc.lcInExtY = TabY.axMax; + + lc.lcOutOrgX = 0; + lc.lcOutOrgY = 0; + + lc.lcOutExtX = GetSystemMetrics(SM_CXSCREEN); + lc.lcOutExtY = -GetSystemMetrics(SM_CYSCREEN); + + if(window->id>highestId) + { + HCTX* tmp_hctx; + highestId=window->id; + tmp_hctx= (HCTX*)SDL_realloc(g_hCtx,(highestId+1)*sizeof(HCTX)); + if (!tmp_hctx) { + SDL_OutOfMemory(); + return -1; + } + g_hCtx=tmp_hctx; + } + + g_hCtx[window->id] = WTOpen(hwnd, &lc, TRUE); } - g_hCtx[window->id] = WTOpen(hwnd, &lc, TRUE); + /*we're telling the window, we want it to report raw input events from mice*/ Rid.usUsagePage = 0x01; Rid.usUsage = 0x02; - //Rid.usUsage = MOUSE_MOVE_ABSOLUTE; - Rid.dwFlags = RIDEV_INPUTSINK; // adds HID mouse and also ignores legacy mouse messages + Rid.dwFlags = RIDEV_INPUTSINK; Rid.hwndTarget = hwnd; RegisterRawInputDevices(&Rid, 1, sizeof(Rid)); @@ -456,6 +466,7 @@ WIN_DestroyWindow(_THIS, SDL_Window * window) DestroyWindow(data->hwnd); } SDL_free(data); + /*lets close the tablet context for the destoryed window*/ WTClose(g_hCtx[window->id]); } }