changeset 3771:8cc36a399a12 gsoc2008_manymouse

comments added and improved code look(windows part)
author Szymon Wilczek <kazeuser@gmail.com>
date Sat, 02 Aug 2008 14:02:28 +0000
parents 81b649bad6d2
children 9087a84cba51
files src/events/SDL_mouse.c 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 5 files changed, 131 insertions(+), 87 deletions(-) [+]
line wrap: on
line diff
--- a/src/events/SDL_mouse.c	Thu Jul 31 14:41:48 2008 +0000
+++ b/src/events/SDL_mouse.c	Sat Aug 02 14:02:28 2008 +0000
@@ -33,8 +33,8 @@
 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 @@
         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_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 @@
     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 @@
 	{
 		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 @@
         return 0;
     }
 
-    /* Update internal mouse state */
+    /* Update internal mouse coordinates */
     if (mouse->relative_mode==SDL_FALSE) {
         mouse->x = x;
         mouse->y = y;
--- a/src/video/win32/SDL_win32events.c	Thu Jul 31 14:41:48 2008 +0000
+++ b/src/video/win32/SDL_win32events.c	Sat Aug 02 14:02:28 2008 +0000
@@ -64,7 +64,7 @@
 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 @@
     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 @@
 		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 @@
             return (0);
         }
         break;
-	case WM_INPUT:
+	case WM_INPUT:/*mouse events*/
 		{
 			LPBYTE lpb;
 			int w, h;
@@ -214,16 +217,19 @@
 			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;i<total_mice;++i)
 			{
 				if(mice[i]==header->hDevice)
@@ -232,11 +238,13 @@
 					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 @@
 			{
 				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);
--- a/src/video/win32/SDL_win32mouse.c	Thu Jul 31 14:41:48 2008 +0000
+++ b/src/video/win32/SDL_win32mouse.c	Sat Aug 02 14:02:28 2008 +0000
@@ -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 @@
 	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 @@
 		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;i<devCount;++i)
 	{
 		int j;
@@ -83,7 +91,7 @@
 		DWORD out=256*sizeof(char);
 		SDL_Mouse mouse;
 		int l;
-		if(deviceList[i].dwType!=RIM_TYPEMOUSE)
+		if(deviceList[i].dwType!=RIM_TYPEMOUSE) /*if a device isn't a mouse type we don't want it*/
 		{
 			continue;
 		}
@@ -91,9 +99,13 @@
 		{
 			continue;
 		}
+
 		buffer = SDL_malloc((tmp+1)*sizeof(char));
 		key_name = SDL_malloc(tmp + sizeof(reg_key_root)*sizeof(char));
 
+		/*we're getting the device registry path and polishing it to get it's name,
+		surely there must be an easier way, but we haven't found it yet*/
+
 		if(GetRawInputDeviceInfoA(deviceList[i].hDevice, RIDI_DEVICENAME, buffer, &tmp)<0)
 		{
 			continue;
@@ -120,6 +132,8 @@
 	SDL_memcpy(key_name, reg_key_root, SDL_strlen (reg_key_root));
     SDL_memcpy(key_name + (SDL_strlen (reg_key_root)), buffer, j + 1);
     
+	/*we're opening the registry key to get the mouse name*/
+
 	rc = RegOpenKeyExA(HKEY_LOCAL_MACHINE, key_name, 0, KEY_READ, &hkey);
     
 	if (rc != ERROR_SUCCESS)
@@ -129,46 +143,50 @@
 
     rc = RegQueryValueExA(hkey, "DeviceDesc", NULL, &regtype, device_name, &out);
     RegCloseKey(hkey);
+
     if (rc != ERROR_SUCCESS)
 	{
         SDL_memcpy(device_name, default_device_name, SDL_strlen(default_device_name));
-    } 
-		mice[index]=deviceList[i].hDevice;
-		SDL_zero(mouse);
-		SDL_SetIndexId(index,index);
-		l=SDL_strlen(device_name);
-		if(tablet==-1)
+    }
+	/*we're saving the handle to the device*/
+	mice[index]=deviceList[i].hDevice;
+	SDL_zero(mouse);
+	SDL_SetIndexId(index,index);
+	l=SDL_strlen(device_name);
+	/*we're checking if the device isn't by any chance a tablet*/
+	if(tablet==-1)
+	{
+		for(j=0;j<l-5;++j)
 		{
-			for(j=0;j<l-5;++j)
+			for(k=0;k<5;++k)
 			{
-				for(k=0;k<5;++k)
+				if(tab[k]!=SDL_tolower((unsigned char)device_name[j+k]))
 				{
-					if(tab[k]!=SDL_tolower((unsigned char)device_name[j+k]))
-					{
-						break;
-					}
-				}
-				if(k==5)
-				{
-					tablet=index;
 					break;
 				}
 			}
-		}
-		if(tablet==index)
-		{
-			AXIS pressure;
-			WTInfo(WTI_DEVICES,DVC_NPRESSURE, &pressure);
-			data->mouse = SDL_AddMouse(&mouse, index,device_name,pressure.axMax,pressure.axMin);
+			if(k==5)
+			{
+				tablet=index;
+				break;
+			}
 		}
-		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 @@
 {
 	int i;
     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
-	for(i=0;i<total_mice;++i)
-	{
-		SDL_DelMouse(i);
-	}
+	/*let's delete all of the mouses*/
+	SDL_MouseQuit();
 }
 
 /* vi: set ts=4 sw=4 expandtab: */
--- a/src/video/win32/SDL_win32video.c	Thu Jul 31 14:41:48 2008 +0000
+++ b/src/video/win32/SDL_win32video.c	Sat Aug 02 14:02:28 2008 +0000
@@ -37,10 +37,10 @@
 static int WIN_VideoInit(_THIS);
 static void WIN_VideoQuit(_THIS);
 
-int total_mice =0;
-HANDLE* mice = NULL;
-HCTX* g_hCtx = NULL;
-int tablet=-1;
+int total_mice = 0; /*total mouse count*/
+HANDLE* mice = NULL; /*the handles to the detected mice*/
+HCTX* g_hCtx = NULL; /*handles to tablet contexts*/
+int tablet=-1; /*we're assuming that there is no tablet*/
 
 /* WIN32 driver bootstrap functions */
 
--- a/src/video/win32/SDL_win32window.c	Thu Jul 31 14:41:48 2008 +0000
+++ b/src/video/win32/SDL_win32window.c	Sat Aug 02 14:02:28 2008 +0000
@@ -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
@@ -36,13 +38,14 @@
 #include "SDL_syswm.h"
 
 #include <wintab.h>
+/*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 <pktdef.h>
 
-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 @@
         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;
+	/*we're configuring the tablet data. See Wintab reference for more info*/
+	
+	if(WTInfo(WTI_DEFSYSCTX, 0, &lc)!=0)
+	{
+	
+		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);
+		WTInfo(WTI_DEVICES,DVC_X,&TabX);
+		WTInfo(WTI_DEVICES,DVC_Y,&TabY);
 	
-	lc.lcInOrgX = 0;
-	lc.lcInOrgY = 0;
+		lc.lcInOrgX = 0;
+		lc.lcInOrgY = 0;
 	
-	lc.lcInExtX = TabX.axMax;
-	lc.lcInExtY = TabY.axMax;
+		lc.lcInExtX = TabX.axMax;
+		lc.lcInExtY = TabY.axMax;
 
-	lc.lcOutOrgX = 0;
-	lc.lcOutOrgY = 0;
+		lc.lcOutOrgX = 0;
+		lc.lcOutOrgY = 0;
+
+		lc.lcOutExtX = GetSystemMetrics(SM_CXSCREEN);
+		lc.lcOutExtY = -GetSystemMetrics(SM_CYSCREEN);
 
-	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;
+		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 @@
             DestroyWindow(data->hwnd);
         }
         SDL_free(data);
+		/*lets close the tablet context for the destoryed window*/
 		WTClose(g_hCtx[window->id]);
     }
 }