changeset 3768:1b87a8beab9d gsoc2008_manymouse

Project part1
author Szymon Wilczek <kazeuser@gmail.com>
date Wed, 30 Jul 2008 16:09:24 +0000
parents abc8acb8e3d7
children fe32943f86ec
files src/video/win32/SDL_win32events.c src/video/win32/SDL_win32mouse.c src/video/win32/SDL_win32video.c src/video/win32/SDL_win32window.c
diffstat 4 files changed, 293 insertions(+), 173 deletions(-) [+]
line wrap: on
line diff
--- a/src/video/win32/SDL_win32events.c	Wed Jul 23 16:12:43 2008 +0000
+++ b/src/video/win32/SDL_win32events.c	Wed Jul 30 16:09:24 2008 +0000
@@ -19,6 +19,12 @@
     Sam Lantinga
     slouken@libsdl.org
 */
+
+#if (_WIN32_WINNT < 0x0501)
+#undef _WIN32_WINNT
+#define _WIN32_WINNT 0x0501
+#endif
+
 #include "SDL_config.h"
 
 #include "SDL_win32video.h"
@@ -53,11 +59,11 @@
 #define GET_XBUTTON_WPARAM(w) (HIWORD(w))
 #endif
 
-#define pi 1.0
+extern HCTX* g_hCtx;
+extern HANDLE* mice;
+extern int total_mice;
 
-int first=1;
-
-LOGCONTEXT lc;
+int pressure=0;
 
 static WPARAM
 RemapVKEY(WPARAM wParam, LPARAM lParam)
@@ -94,7 +100,7 @@
 WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
 {
     SDL_WindowData *data;
-
+	RAWINPUT* raw;
 	PACKET packet;
 	
 	/* Send a SDL_SYSWMEVENT if the application wants them */
@@ -129,11 +135,11 @@
 #endif
 
     switch (msg) {
-	case WT_PACKET:
+		case WT_PACKET:
 		{
 			if (WTPacket((HCTX)lParam, wParam, &packet))
 			{
-				SDL_SendMouseMotion(0,0,(int)packet.pkX,(int)packet.pkY,(int)packet.pkNormalPressure);
+				pressure=(int)packet.pkNormalPressure;
 			}
 		}
 		break;
@@ -201,13 +207,30 @@
 			SDL_SendMouseMotion(0,1,1,0,0);
 			SDL_SendMouseMotion(0,1,-1,0,0);
 		}
-    case WM_MOUSEMOVE:
+    /*case WM_MOUSEMOVE:
         {
+			LPBYTE lpb;
+			const RAWINPUTHEADER *header;
             int index;
+			int i;
+			int size=0;
             SDL_Mouse *mouse;
             int x, y;
 
-            index = data->videodata->mouse;
+            //index = data->videodata->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;
+			for(i=0;i<total_mice;++i)
+			{
+				if(mice[i]==header->hDevice)
+				{
+					index=i;
+					break;
+				}
+			}
             mouse = SDL_GetMouse(index);
 
             if (mouse->focus != data->windowID) {
@@ -222,10 +245,14 @@
             }
 
             /* mouse has moved within the window */
-            x = LOWORD(lParam);
-            y = HIWORD(lParam);
+            //x = LOWORD(lParam);
+            //y = HIWORD(lParam);
 			//printf("index: %d\n",index);
-            if (mouse->relative_mode) {
+			/*if (WTPacketsPeek(g_hCtx[data->windowID],1,&packet))
+				{
+					pressure=(int)packet.pkNormalPressure;
+				}*/
+            /*if (mouse->relative_mode) {
                 int w, h;
                 POINT center;
                 SDL_GetWindowSize(data->windowID, &w, &h);
@@ -236,14 +263,72 @@
                 if (x || y) {
                     ClientToScreen(hwnd, &center);
                     SetCursorPos(center.x, center.y);
-					SDL_SendMouseMotion(index, 1, x, y,0);
+					SDL_SendMouseMotion(index, 1, x, y,pressure);
                 }
             } else {
-					SDL_SendMouseMotion(index, 0, x, y,0);
+					SDL_SendMouseMotion(index, 0, x, y,pressure);
             }
         }
-        return (0);
-
+        return (0);*/
+	case WM_INPUT:
+		{
+			LPBYTE lpb;
+			int w, h;
+			const RAWINPUTHEADER *header;
+			int index;
+			int i;
+			int size=0;
+            SDL_Mouse *mouse;
+			const RAWMOUSE *raw_mouse=NULL;
+			LPPOINT point;
+			USHORT flags;
+			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;
+			//raw_mouse=&raw->data.mouse;
+			flags=raw->data.mouse.usButtonFlags;
+			for(i=0;i<total_mice;++i)
+			{
+				if(mice[i]==header->hDevice)
+				{
+					index=i;
+					break;
+				}
+			}
+            mouse = SDL_GetMouse(index);
+			GetCursorPos(&point);
+			ScreenToClient(hwnd, &point);
+			SDL_GetWindowSize(data->windowID, &w, &h);
+			SDL_UpdateCoordinates(w,h);
+			SDL_SendMouseMotion(index,0,(int)(&point->x),(int)(&point->y),pressure);
+			if(flags & RI_MOUSE_BUTTON_1_DOWN)
+			{
+				SDL_SendMouseButton(index,SDL_PRESSED,SDL_BUTTON_LEFT);
+			}
+			else if(flags & RI_MOUSE_BUTTON_1_UP)
+			{
+				SDL_SendMouseButton(index,SDL_RELEASED,SDL_BUTTON_LEFT);
+			}
+			if(flags & RI_MOUSE_BUTTON_2_DOWN)
+			{
+				SDL_SendMouseButton(index,SDL_PRESSED,SDL_BUTTON_MIDDLE);
+			}
+			else if(flags & RI_MOUSE_BUTTON_2_UP)
+			{
+				SDL_SendMouseButton(index,SDL_RELEASED,SDL_BUTTON_MIDDLE);
+			}
+			if(flags & RI_MOUSE_BUTTON_3_DOWN)
+			{
+				SDL_SendMouseButton(index,SDL_PRESSED,SDL_BUTTON_RIGHT);
+			}
+			else if(flags & RI_MOUSE_BUTTON_3_UP)
+			{
+				SDL_SendMouseButton(index,SDL_RELEASED,SDL_BUTTON_RIGHT);
+			}
+		}
+		return(0);
     case WM_MOUSELEAVE:
         {
             int index;
@@ -258,108 +343,9 @@
         }
         return (0);
 
-    case WM_LBUTTONDOWN:
-    case WM_LBUTTONUP:
-    case WM_MBUTTONDOWN:
-    case WM_MBUTTONUP:
-    case WM_RBUTTONDOWN:
-    case WM_RBUTTONUP:
-    case WM_XBUTTONDOWN:
-    case WM_XBUTTONUP:
-        {
-            int xbuttonval = 0;
-            int index;
-            SDL_Mouse *mouse;
-            Uint8 button, state;
-
-            /* DJM:
-               We want the SDL window to take focus so that
-               it acts like a normal windows "component"
-               (e.g. gains keyboard focus on a mouse click).
-             */
-            SetFocus(hwnd);
-
-            index = data->videodata->mouse;
-            mouse = SDL_GetMouse(index);
+    
 
-            /* Figure out which button to use */
-            switch (msg) {
-            case WM_LBUTTONDOWN:
-                button = SDL_BUTTON_LEFT;
-                state = SDL_PRESSED;
-                break;
-            case WM_LBUTTONUP:
-                button = SDL_BUTTON_LEFT;
-                state = SDL_RELEASED;
-                break;
-            case WM_MBUTTONDOWN:
-                button = SDL_BUTTON_MIDDLE;
-                state = SDL_PRESSED;
-                break;
-            case WM_MBUTTONUP:
-                button = SDL_BUTTON_MIDDLE;
-                state = SDL_RELEASED;
-                break;
-            case WM_RBUTTONDOWN:
-                button = SDL_BUTTON_RIGHT;
-                state = SDL_PRESSED;
-                break;
-            case WM_RBUTTONUP:
-                button = SDL_BUTTON_RIGHT;
-                state = SDL_RELEASED;
-                break;
-            case WM_XBUTTONDOWN:
-                xbuttonval = GET_XBUTTON_WPARAM(wParam);
-                button = SDL_BUTTON_X1 + xbuttonval - 1;
-                state = SDL_PRESSED;
-                break;
-            case WM_XBUTTONUP:
-                xbuttonval = GET_XBUTTON_WPARAM(wParam);
-                button = SDL_BUTTON_X1 + xbuttonval - 1;
-                state = SDL_RELEASED;
-                break;
-            default:
-                /* Eh? Unknown button? */
-                return (0);
-            }
-            if (state == SDL_PRESSED) {
-                /* Grab mouse so we get up events */
-                if (++data->mouse_pressed > 0) {
-                    SetCapture(hwnd);
-                }
-            } else {
-                /* Release mouse after all up events */
-                if (--data->mouse_pressed <= 0) {
-                    ReleaseCapture();
-                    data->mouse_pressed = 0;
-                }
-            }
-
-            if (!mouse->relative_mode) {
-                int x, y;
-                x = LOWORD(lParam);
-                y = HIWORD(lParam);
-                SDL_SendMouseMotion(index, 0, x, y,0);
-            }
-            SDL_SendMouseButton(index, state, button);
-
-            /*
-             * MSDN says:
-             *  "Unlike the WM_LBUTTONUP, WM_MBUTTONUP, and WM_RBUTTONUP
-             *   messages, an application should return TRUE from [an
-             *   XBUTTON message] if it processes it. Doing so will allow
-             *   software that simulates this message on Microsoft Windows
-             *   systems earlier than Windows 2000 to determine whether
-             *   the window procedure processed the message or called
-             *   DefWindowProc to process it.
-             */
-            if (xbuttonval > 0) {
-                return (TRUE);
-            }
-        }
-        return (0);
-
-    case WM_MOUSEWHEEL:
+ /*   case WM_MOUSEWHEEL:
         {
             int index;
             int motion = (short) HIWORD(wParam);
@@ -367,7 +353,7 @@
             index = data->videodata->mouse;
             SDL_SendMouseWheel(index, 0, motion);
         }
-        return (0);
+        return (0);*/
 
     case WM_SYSKEYDOWN:
     case WM_KEYDOWN:
@@ -663,16 +649,11 @@
 {
     MSG msg;
 
-    while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
+    while (PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE)) {
         TranslateMessage(&msg);
-        DispatchMessage(&msg);
+        DispatchMessageA(&msg);
 
     }
-	/*while (GetMessage(&msg, NULL, 0, 0)) {
-        TranslateMessage(&msg);
-        DispatchMessage(&msg);
-    }*/
-	//WTClose(g_hCtx);
 }
 
 static int app_registered = 0;
--- a/src/video/win32/SDL_win32mouse.c	Wed Jul 23 16:12:43 2008 +0000
+++ b/src/video/win32/SDL_win32mouse.c	Wed Jul 30 16:09:24 2008 +0000
@@ -1,46 +1,165 @@
-/*
-    SDL - Simple DirectMedia Layer
-    Copyright (C) 1997-2006 Sam Lantinga
-
-    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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
-
-    Sam Lantinga
-    slouken@libsdl.org
-*/
-#include "SDL_config.h"
-
-#include "SDL_win32video.h"
-
-#include "../../events/SDL_mouse_c.h"
-
-void
-WIN_InitMouse(_THIS)
-{
-    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
-    SDL_Mouse mouse;
-
-    SDL_zero(mouse);
-    data->mouse = SDL_AddMouse(&mouse, -1);
-}
-
-void
-WIN_QuitMouse(_THIS)
-{
-    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
-
-    SDL_DelMouse(data->mouse);
-}
-
-/* vi: set ts=4 sw=4 expandtab: */
+/*
+    SDL - Simple DirectMedia Layer
+    Copyright (C) 1997-2006 Sam Lantinga
+
+    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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+    Sam Lantinga
+    slouken@libsdl.org
+*/
+
+#if (_WIN32_WINNT < 0x0501)
+#undef _WIN32_WINNT
+#define _WIN32_WINNT 0x0501
+#endif
+
+#include "SDL_config.h"
+
+#include "SDL_win32video.h"
+
+#include "../../events/SDL_mouse_c.h"
+
+extern int total_mice;
+
+extern HANDLE* mice;
+
+extern int total_mice;
+
+RAWINPUTDEVICE *Rid=NULL;
+
+void
+WIN_InitMouse(_THIS)
+{
+	int index=0;
+	RAWINPUTDEVICELIST *deviceList=NULL;
+	int devCount=0;
+	int i;
+	int tmp=0;
+	char* buffer=NULL;
+
+    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
+
+	if(GetRawInputDeviceList(NULL,&devCount,sizeof(RAWINPUTDEVICELIST)))
+	{
+		return;
+	}
+	else
+	{
+		deviceList = SDL_malloc(sizeof(RAWINPUTDEVICELIST)*devCount);
+	}
+
+	GetRawInputDeviceList(deviceList,&devCount,sizeof(RAWINPUTDEVICELIST));
+
+	mice = SDL_malloc(devCount*sizeof(HANDLE));
+
+	for(i=0;i<devCount;++i)
+	{
+		int j;
+		char *default_device_name="Pointing device xx";
+		const char *reg_key_root = "System\\CurrentControlSet\\Enum\\";
+		char *device_name=SDL_malloc(256*sizeof(char));
+		char *key_name=NULL;
+		char *tmp_name=NULL;
+		LONG rc = 0;
+		HKEY hkey;
+		DWORD regtype = REG_SZ;
+		DWORD out=256*sizeof(char);
+		SDL_Mouse mouse;
+		if(deviceList[i].dwType!=RIM_TYPEMOUSE)
+		{
+			continue;
+		}
+		if(GetRawInputDeviceInfoA(deviceList[i].hDevice, RIDI_DEVICENAME, NULL, &tmp)<0)
+		{
+			continue;
+		}
+		buffer = SDL_malloc((tmp+1)*sizeof(char));
+		key_name = SDL_malloc(tmp + sizeof(reg_key_root)*sizeof(char));
+
+		if(GetRawInputDeviceInfoA(deviceList[i].hDevice, RIDI_DEVICENAME, buffer, &tmp)<0)
+		{
+			continue;
+		}
+
+		buffer+=4;
+		tmp-=4;
+
+		tmp_name=buffer;
+		for(j=0;j<tmp;++j)
+		{
+			if(*tmp_name=='#')
+			{
+				*tmp_name='\\';
+			}
+			else if(*tmp_name=='{')
+			{
+				break;
+			}
+			++tmp_name;
+		}
+		*tmp_name='\0';
+
+	SDL_memcpy(key_name, reg_key_root, SDL_strlen (reg_key_root));
+    SDL_memcpy(key_name + (SDL_strlen (reg_key_root)), buffer, j + 1);
+    
+	rc = RegOpenKeyExA(HKEY_LOCAL_MACHINE, key_name, 0, KEY_READ, &hkey);
+    
+	if (rc != ERROR_SUCCESS)
+        return;
+
+    rc = RegQueryValueExA(hkey, "DeviceDesc", NULL, &regtype, device_name, &out);
+    RegCloseKey(hkey);
+    if (rc != ERROR_SUCCESS)
+	{
+		return;
+        //SDL_memcpy(device_name, default_device_name, SDL_strlen(default_device_name));
+    } 
+    //device_name[254] = '\0';
+
+		mice[index]=deviceList[i].hDevice;
+		SDL_zero(mouse);
+		SDL_SetIndexId(index,index);
+		data->mouse = SDL_AddMouse(&mouse, index,device_name,0,0);
+		//data->mouse = SDL_AddMouse(&mouse, index,key_name,0,0);
+		++index;
+
+		SDL_free(buffer);
+		SDL_free(key_name);
+	}
+	Rid = SDL_malloc(sizeof(RAWINPUTDEVICE));
+	/*Rid[0].usUsagePage = 0x01; 
+	Rid[0].usUsage = 0x02; 
+	Rid[0].dwFlags = RIDEV_INPUTSINK;   // adds HID mouse and also ignores legacy mouse messages
+	Rid[0].hwndTarget = NULL;
+
+	RegisterRawInputDevices(Rid, 1, sizeof(Rid[0]));*/
+
+	total_mice=index;
+	SDL_free(deviceList);
+}
+
+void
+WIN_QuitMouse(_THIS)
+{
+	int i;
+    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
+	for(i=0;i<total_mice;++i)
+	{
+		SDL_DelMouse(i);
+	}
+	SDL_free(Rid);
+}
+
+/* vi: set ts=4 sw=4 expandtab: */
--- a/src/video/win32/SDL_win32video.c	Wed Jul 23 16:12:43 2008 +0000
+++ b/src/video/win32/SDL_win32video.c	Wed Jul 30 16:09:24 2008 +0000
@@ -37,7 +37,10 @@
 static int WIN_VideoInit(_THIS);
 static void WIN_VideoQuit(_THIS);
 
+int total_mice =0;
+HANDLE* mice = NULL;
 HCTX* g_hCtx = NULL;
+//int highestId=0;
 
 /* WIN32 driver bootstrap functions */
 
--- a/src/video/win32/SDL_win32window.c	Wed Jul 23 16:12:43 2008 +0000
+++ b/src/video/win32/SDL_win32window.c	Wed Jul 30 16:09:24 2008 +0000
@@ -19,6 +19,12 @@
     Sam Lantinga
     slouken@libsdl.org
 */
+
+#if (_WIN32_WINNT < 0x0501)
+#undef _WIN32_WINNT
+#define _WIN32_WINNT 0x0501
+#endif
+
 #include "SDL_config.h"
 
 #include "../SDL_sysvideo.h"
@@ -140,6 +146,7 @@
 int
 WIN_CreateWindow(_THIS, SDL_Window * window)
 {
+	RAWINPUTDEVICE Rid;
 	AXIS TabX,TabY;
 	LOGCONTEXT lc;
     HWND hwnd;
@@ -191,11 +198,11 @@
         CreateWindow(SDL_Appname, TEXT(""), style, x, y, w, h, NULL, NULL,
                      SDL_Instance, NULL);
 	
-	WTInfo(WTI_DEFCONTEXT, 0, &lc);
-
+	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;
 
@@ -226,6 +233,16 @@
 	}
 
 	g_hCtx[window->id] = WTOpen(hwnd, &lc, TRUE);
+
+	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.hwndTarget = hwnd;
+
+	RegisterRawInputDevices(&Rid, 1, sizeof(Rid));
+		
+
     WIN_PumpEvents(_this);
 
     if (!hwnd) {