comments added and improved code look(windows part) gsoc2008_manymouse
authorSzymon Wilczek
Sat, 02 Aug 2008 14:02:28 +0000
branchgsoc2008_manymouse
changeset 37718cc36a399a12
parent 3770 81b649bad6d2
child 3772 9087a84cba51
comments added and improved code look(windows part)
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
     1.1 --- a/src/events/SDL_mouse.c	Thu Jul 31 14:41:48 2008 +0000
     1.2 +++ b/src/events/SDL_mouse.c	Sat Aug 02 14:02:28 2008 +0000
     1.3 @@ -33,8 +33,8 @@
     1.4  static SDL_Mouse **SDL_mice=NULL;
     1.5  static int *SDL_IdIndex=NULL;
     1.6  static int SDL_highestId=-1;
     1.7 -static int last_x, last_y;
     1.8 -int x_max, y_max;
     1.9 +static int last_x, last_y;/*the last reported x and y coordinates by the system cursor*/
    1.10 +int x_max, y_max; /*current window width and height*/
    1.11  /* Public functions */
    1.12  int
    1.13  SDL_MouseInit(void)
    1.14 @@ -77,6 +77,8 @@
    1.15          return -1;
    1.16      }
    1.17      *SDL_mice[index] = *mouse;
    1.18 +
    1.19 +	/*we're setting the mouse properties*/
    1.20  	length=0;
    1.21  	length=SDL_strlen(name);
    1.22      SDL_mice[index]->name=SDL_malloc((length+1)*sizeof(char));
    1.23 @@ -90,7 +92,11 @@
    1.24          SDL_CreateCursor(default_cdata, default_cmask, DEFAULT_CWIDTH,
    1.25                           DEFAULT_CHEIGHT, DEFAULT_CHOTX, DEFAULT_CHOTY);
    1.26      SDL_SetCursor(SDL_mice[index]->def_cursor);
    1.27 +	/*we're assuming that all mouses are in the computer sensing zone*/
    1.28      SDL_mice[index]->proximity=SDL_TRUE;
    1.29 +	/*we're assuming that all mouses are working in the absolute position mode
    1.30 +	thanx to that, the users that don't want to use many mouses don't have to
    1.31 +	worry about anything*/
    1.32      SDL_mice[index]->relative_mode=SDL_FALSE;
    1.33      SDL_SelectMouse(selected_mouse);
    1.34  
    1.35 @@ -361,6 +367,8 @@
    1.36      int posted;
    1.37      int xrel;
    1.38      int yrel;
    1.39 +	/*while using the relative mode and many windows, we have to be sure,
    1.40 +	that the pointers find themselves inside the windows*/
    1.41  	if(x>x_max)
    1.42  	{
    1.43  		x=x_max;
    1.44 @@ -369,23 +377,23 @@
    1.45  	{
    1.46  		y=y_max;
    1.47  	}
    1.48 +
    1.49      if (!mouse || mouse->flush_motion) {
    1.50          return 0;
    1.51      }
    1.52 +
    1.53 +	/*if the mouse is out of proximity we don't to want to have any motion from it*/
    1.54      if(mouse->proximity==SDL_FALSE)
    1.55      {
    1.56          last_x=x;
    1.57          last_y=y;
    1.58          return 0;
    1.59      }
    1.60 -    if (mouse->relative_mode==SDL_TRUE && mouse->proximity==SDL_TRUE) {
    1.61 -        /* Push the cursor around */
    1.62 -        xrel = x - last_x;
    1.63 -        yrel = y - last_y;
    1.64 -    } else {
    1.65 -        xrel = x - last_x;
    1.66 -        yrel = y - last_y;
    1.67 -    }
    1.68 +
    1.69 +	/*the relative motion is calculated regarding the system cursor last position*/
    1.70 +
    1.71 +    xrel = x - last_x;
    1.72 +    yrel = y - last_y;
    1.73  
    1.74      /* Drop events that don't change state */
    1.75      if (!xrel && !yrel) {
    1.76 @@ -395,7 +403,7 @@
    1.77          return 0;
    1.78      }
    1.79  
    1.80 -    /* Update internal mouse state */
    1.81 +    /* Update internal mouse coordinates */
    1.82      if (mouse->relative_mode==SDL_FALSE) {
    1.83          mouse->x = x;
    1.84          mouse->y = y;
     2.1 --- a/src/video/win32/SDL_win32events.c	Thu Jul 31 14:41:48 2008 +0000
     2.2 +++ b/src/video/win32/SDL_win32events.c	Sat Aug 02 14:02:28 2008 +0000
     2.3 @@ -64,7 +64,7 @@
     2.4  extern int total_mice;
     2.5  extern int tablet;
     2.6  
     2.7 -int pressure=0;
     2.8 +int pressure=0;/*the pressure reported by the tablet*/
     2.9  
    2.10  static WPARAM
    2.11  RemapVKEY(WPARAM wParam, LPARAM lParam)
    2.12 @@ -138,6 +138,7 @@
    2.13      switch (msg) {
    2.14  		case WT_PACKET:
    2.15  		{
    2.16 +			/*if we receive such data we need to update the pressure*/
    2.17  			if (WTPacket((HCTX)lParam, wParam, &packet))
    2.18  			{
    2.19  				pressure=(int)packet.pkNormalPressure;
    2.20 @@ -146,10 +147,12 @@
    2.21  		break;
    2.22  	case WT_PROXIMITY:
    2.23  		{
    2.24 +			/*checking where the proximity message showed up*/
    2.25  			int h_context=LOWORD(lParam);
    2.26  			LPPOINT point;
    2.27  			GetCursorPos(&point);
    2.28  			ScreenToClient(hwnd, &point);
    2.29 +			/*are we in proximity or out of proximity*/
    2.30  			if(h_context==0)
    2.31  			{
    2.32  				SDL_SendProximity(tablet, (int)(&point->x),(int)(&point->y), SDL_PROXIMITYOUT);
    2.33 @@ -206,7 +209,7 @@
    2.34              return (0);
    2.35          }
    2.36          break;
    2.37 -	case WM_INPUT:
    2.38 +	case WM_INPUT:/*mouse events*/
    2.39  		{
    2.40  			LPBYTE lpb;
    2.41  			int w, h;
    2.42 @@ -214,16 +217,19 @@
    2.43  			int index;
    2.44  			int i;
    2.45  			int size=0;
    2.46 -            SDL_Mouse *mouse;
    2.47  			const RAWMOUSE *raw_mouse=NULL;
    2.48  			LPPOINT point;
    2.49  			USHORT flags;
    2.50 +
    2.51 +			/*we're collecting data from the mouse*/
    2.52  			GetRawInputData((HRAWINPUT) lParam, RID_INPUT, NULL, &size, sizeof (RAWINPUTHEADER));
    2.53  			lpb = SDL_malloc(size*sizeof(LPBYTE));
    2.54  			GetRawInputData((HRAWINPUT) lParam, RID_INPUT, lpb, &size, sizeof (RAWINPUTHEADER));
    2.55  			raw = (RAWINPUT *) lpb;
    2.56  			header = &raw->header;
    2.57  			flags=raw->data.mouse.usButtonFlags;
    2.58 +
    2.59 +			/*we're checking which mouse generated the event*/
    2.60  			for(i=0;i<total_mice;++i)
    2.61  			{
    2.62  				if(mice[i]==header->hDevice)
    2.63 @@ -232,11 +238,13 @@
    2.64  					break;
    2.65  				}
    2.66  			}
    2.67 -            mouse = SDL_GetMouse(index);
    2.68 +          
    2.69  			GetCursorPos(&point);
    2.70  			ScreenToClient(hwnd, &point);
    2.71  			SDL_GetWindowSize(data->windowID, &w, &h);
    2.72 -			SDL_UpdateCoordinates(w,h);
    2.73 +			SDL_UpdateCoordinates(w,h);/*we're updating the current window size*/
    2.74 +
    2.75 +			/*if the message was sent by a tablet we have to send also pressure*/
    2.76  			if(i==tablet)
    2.77  			{
    2.78  				SDL_SendMouseMotion(index,0,(int)(&point->x),(int)(&point->y),pressure);
    2.79 @@ -245,6 +253,7 @@
    2.80  			{
    2.81  				SDL_SendMouseMotion(index,0,(int)(&point->x),(int)(&point->y),0);
    2.82  			}
    2.83 +			/*we're sending mouse buttons messages to check up if sth changed*/
    2.84  			if(flags & RI_MOUSE_BUTTON_1_DOWN)
    2.85  			{
    2.86  				SDL_SendMouseButton(index,SDL_PRESSED,SDL_BUTTON_LEFT);
     3.1 --- a/src/video/win32/SDL_win32mouse.c	Thu Jul 31 14:41:48 2008 +0000
     3.2 +++ b/src/video/win32/SDL_win32mouse.c	Sat Aug 02 14:02:28 2008 +0000
     3.3 @@ -20,6 +20,8 @@
     3.4      slouken@libsdl.org
     3.5  */
     3.6  
     3.7 +/*we need to define it, so that raw input is included*/
     3.8 +
     3.9  #if (_WIN32_WINNT < 0x0501)
    3.10  #undef _WIN32_WINNT
    3.11  #define _WIN32_WINNT 0x0501
    3.12 @@ -51,10 +53,12 @@
    3.13  	int i;
    3.14  	int tmp=0;
    3.15  	char* buffer=NULL;
    3.16 -	char* tab="wacom";
    3.17 +	char* tab="wacom";/*since windows does't give us handles to tablets, we have to detect a tablet by it's name*/
    3.18  
    3.19      SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    3.20  
    3.21 +	/*we're checking for the number of rawinput devices*/
    3.22 +
    3.23  	if(GetRawInputDeviceList(NULL,&devCount,sizeof(RAWINPUTDEVICELIST)))
    3.24  	{
    3.25  		return;
    3.26 @@ -64,10 +68,14 @@
    3.27  		deviceList = SDL_malloc(sizeof(RAWINPUTDEVICELIST)*devCount);
    3.28  	}
    3.29  
    3.30 +	/*we're getting the raw input device list*/
    3.31 +
    3.32  	GetRawInputDeviceList(deviceList,&devCount,sizeof(RAWINPUTDEVICELIST));
    3.33  
    3.34  	mice = SDL_malloc(devCount*sizeof(HANDLE));
    3.35  
    3.36 +	/*we're getting the details of the devices*/
    3.37 +
    3.38  	for(i=0;i<devCount;++i)
    3.39  	{
    3.40  		int j;
    3.41 @@ -83,7 +91,7 @@
    3.42  		DWORD out=256*sizeof(char);
    3.43  		SDL_Mouse mouse;
    3.44  		int l;
    3.45 -		if(deviceList[i].dwType!=RIM_TYPEMOUSE)
    3.46 +		if(deviceList[i].dwType!=RIM_TYPEMOUSE) /*if a device isn't a mouse type we don't want it*/
    3.47  		{
    3.48  			continue;
    3.49  		}
    3.50 @@ -91,9 +99,13 @@
    3.51  		{
    3.52  			continue;
    3.53  		}
    3.54 +
    3.55  		buffer = SDL_malloc((tmp+1)*sizeof(char));
    3.56  		key_name = SDL_malloc(tmp + sizeof(reg_key_root)*sizeof(char));
    3.57  
    3.58 +		/*we're getting the device registry path and polishing it to get it's name,
    3.59 +		surely there must be an easier way, but we haven't found it yet*/
    3.60 +
    3.61  		if(GetRawInputDeviceInfoA(deviceList[i].hDevice, RIDI_DEVICENAME, buffer, &tmp)<0)
    3.62  		{
    3.63  			continue;
    3.64 @@ -120,6 +132,8 @@
    3.65  	SDL_memcpy(key_name, reg_key_root, SDL_strlen (reg_key_root));
    3.66      SDL_memcpy(key_name + (SDL_strlen (reg_key_root)), buffer, j + 1);
    3.67      
    3.68 +	/*we're opening the registry key to get the mouse name*/
    3.69 +
    3.70  	rc = RegOpenKeyExA(HKEY_LOCAL_MACHINE, key_name, 0, KEY_READ, &hkey);
    3.71      
    3.72  	if (rc != ERROR_SUCCESS)
    3.73 @@ -129,46 +143,50 @@
    3.74  
    3.75      rc = RegQueryValueExA(hkey, "DeviceDesc", NULL, &regtype, device_name, &out);
    3.76      RegCloseKey(hkey);
    3.77 +
    3.78      if (rc != ERROR_SUCCESS)
    3.79  	{
    3.80          SDL_memcpy(device_name, default_device_name, SDL_strlen(default_device_name));
    3.81 -    } 
    3.82 -		mice[index]=deviceList[i].hDevice;
    3.83 -		SDL_zero(mouse);
    3.84 -		SDL_SetIndexId(index,index);
    3.85 -		l=SDL_strlen(device_name);
    3.86 -		if(tablet==-1)
    3.87 +    }
    3.88 +	/*we're saving the handle to the device*/
    3.89 +	mice[index]=deviceList[i].hDevice;
    3.90 +	SDL_zero(mouse);
    3.91 +	SDL_SetIndexId(index,index);
    3.92 +	l=SDL_strlen(device_name);
    3.93 +	/*we're checking if the device isn't by any chance a tablet*/
    3.94 +	if(tablet==-1)
    3.95 +	{
    3.96 +		for(j=0;j<l-5;++j)
    3.97  		{
    3.98 -			for(j=0;j<l-5;++j)
    3.99 +			for(k=0;k<5;++k)
   3.100  			{
   3.101 -				for(k=0;k<5;++k)
   3.102 +				if(tab[k]!=SDL_tolower((unsigned char)device_name[j+k]))
   3.103  				{
   3.104 -					if(tab[k]!=SDL_tolower((unsigned char)device_name[j+k]))
   3.105 -					{
   3.106 -						break;
   3.107 -					}
   3.108 -				}
   3.109 -				if(k==5)
   3.110 -				{
   3.111 -					tablet=index;
   3.112  					break;
   3.113  				}
   3.114  			}
   3.115 -		}
   3.116 -		if(tablet==index)
   3.117 -		{
   3.118 -			AXIS pressure;
   3.119 -			WTInfo(WTI_DEVICES,DVC_NPRESSURE, &pressure);
   3.120 -			data->mouse = SDL_AddMouse(&mouse, index,device_name,pressure.axMax,pressure.axMin);
   3.121 +			if(k==5)
   3.122 +			{
   3.123 +				tablet=index;
   3.124 +				break;
   3.125 +			}
   3.126  		}
   3.127 -		else
   3.128 -		{
   3.129 -			data->mouse = SDL_AddMouse(&mouse, index,device_name,0,0);
   3.130 -		}
   3.131 -		++index;
   3.132 +	}
   3.133 +	/*if it's a tablet, let's read it's maximum and minimum pressure*/
   3.134 +	if(tablet==index)
   3.135 +	{
   3.136 +		AXIS pressure;
   3.137 +		WTInfo(WTI_DEVICES,DVC_NPRESSURE, &pressure);
   3.138 +		data->mouse = SDL_AddMouse(&mouse, index,device_name,pressure.axMax,pressure.axMin);
   3.139 +	}
   3.140 +	else
   3.141 +	{
   3.142 +		data->mouse = SDL_AddMouse(&mouse, index,device_name,0,0);
   3.143 +	}
   3.144 +	++index;
   3.145  
   3.146 -		SDL_free(buffer);
   3.147 -		SDL_free(key_name);
   3.148 +	SDL_free(buffer);
   3.149 +	SDL_free(key_name);
   3.150  	}
   3.151  	total_mice=index;
   3.152  	SDL_free(deviceList);
   3.153 @@ -179,10 +197,8 @@
   3.154  {
   3.155  	int i;
   3.156      SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   3.157 -	for(i=0;i<total_mice;++i)
   3.158 -	{
   3.159 -		SDL_DelMouse(i);
   3.160 -	}
   3.161 +	/*let's delete all of the mouses*/
   3.162 +	SDL_MouseQuit();
   3.163  }
   3.164  
   3.165  /* vi: set ts=4 sw=4 expandtab: */
     4.1 --- a/src/video/win32/SDL_win32video.c	Thu Jul 31 14:41:48 2008 +0000
     4.2 +++ b/src/video/win32/SDL_win32video.c	Sat Aug 02 14:02:28 2008 +0000
     4.3 @@ -37,10 +37,10 @@
     4.4  static int WIN_VideoInit(_THIS);
     4.5  static void WIN_VideoQuit(_THIS);
     4.6  
     4.7 -int total_mice =0;
     4.8 -HANDLE* mice = NULL;
     4.9 -HCTX* g_hCtx = NULL;
    4.10 -int tablet=-1;
    4.11 +int total_mice = 0; /*total mouse count*/
    4.12 +HANDLE* mice = NULL; /*the handles to the detected mice*/
    4.13 +HCTX* g_hCtx = NULL; /*handles to tablet contexts*/
    4.14 +int tablet=-1; /*we're assuming that there is no tablet*/
    4.15  
    4.16  /* WIN32 driver bootstrap functions */
    4.17  
     5.1 --- a/src/video/win32/SDL_win32window.c	Thu Jul 31 14:41:48 2008 +0000
     5.2 +++ b/src/video/win32/SDL_win32window.c	Sat Aug 02 14:02:28 2008 +0000
     5.3 @@ -20,6 +20,8 @@
     5.4      slouken@libsdl.org
     5.5  */
     5.6  
     5.7 +/*we need to define it, so that raw input is included*/
     5.8 +
     5.9  #if (_WIN32_WINNT < 0x0501)
    5.10  #undef _WIN32_WINNT
    5.11  #define _WIN32_WINNT 0x0501
    5.12 @@ -36,13 +38,14 @@
    5.13  #include "SDL_syswm.h"
    5.14  
    5.15  #include <wintab.h>
    5.16 +/*we're telling wintab that we want to receive movement, button events and pressure information in packets*/
    5.17  #define PACKETDATA ( PK_X | PK_Y | PK_BUTTONS | PK_NORMAL_PRESSURE)
    5.18  #define PACKETMODE 0
    5.19  #include <pktdef.h>
    5.20  
    5.21 -extern HCTX* g_hCtx;
    5.22 +extern HCTX* g_hCtx; /*the table of tablet event contexts, each windows has to have it's own tablet context*/
    5.23  
    5.24 -int highestId=0;
    5.25 +int highestId=0; /*the highest id of the tablet context*/
    5.26  
    5.27  static int
    5.28  SetupWindowData(_THIS, SDL_Window * window, HWND hwnd, SDL_bool created)
    5.29 @@ -198,46 +201,53 @@
    5.30          CreateWindow(SDL_Appname, TEXT(""), style, x, y, w, h, NULL, NULL,
    5.31                       SDL_Instance, NULL);
    5.32  	
    5.33 -	WTInfo(WTI_DEFSYSCTX, 0, &lc);
    5.34 -	lc.lcPktData = PACKETDATA;
    5.35 -	lc.lcPktMode = PACKETMODE;
    5.36 -	lc.lcOptions |= CXO_MESSAGES;
    5.37 -	lc.lcOptions |= CXO_SYSTEM;
    5.38 -	lc.lcMoveMask = PACKETDATA;
    5.39 -	lc.lcBtnDnMask=lc.lcBtnUpMask = PACKETDATA;
    5.40 +	/*we're configuring the tablet data. See Wintab reference for more info*/
    5.41 +	
    5.42 +	if(WTInfo(WTI_DEFSYSCTX, 0, &lc)!=0)
    5.43 +	{
    5.44 +	
    5.45 +		lc.lcPktData = PACKETDATA;
    5.46 +		lc.lcPktMode = PACKETMODE;
    5.47 +		lc.lcOptions |= CXO_MESSAGES;
    5.48 +		lc.lcOptions |= CXO_SYSTEM;
    5.49 +		lc.lcMoveMask = PACKETDATA;
    5.50 +		lc.lcBtnDnMask=lc.lcBtnUpMask = PACKETDATA;
    5.51  
    5.52 -	WTInfo(WTI_DEVICES,DVC_X,&TabX);
    5.53 -	WTInfo(WTI_DEVICES,DVC_Y,&TabY);
    5.54 +		WTInfo(WTI_DEVICES,DVC_X,&TabX);
    5.55 +		WTInfo(WTI_DEVICES,DVC_Y,&TabY);
    5.56  	
    5.57 -	lc.lcInOrgX = 0;
    5.58 -	lc.lcInOrgY = 0;
    5.59 +		lc.lcInOrgX = 0;
    5.60 +		lc.lcInOrgY = 0;
    5.61  	
    5.62 -	lc.lcInExtX = TabX.axMax;
    5.63 -	lc.lcInExtY = TabY.axMax;
    5.64 +		lc.lcInExtX = TabX.axMax;
    5.65 +		lc.lcInExtY = TabY.axMax;
    5.66  
    5.67 -	lc.lcOutOrgX = 0;
    5.68 -	lc.lcOutOrgY = 0;
    5.69 +		lc.lcOutOrgX = 0;
    5.70 +		lc.lcOutOrgY = 0;
    5.71 +
    5.72 +		lc.lcOutExtX = GetSystemMetrics(SM_CXSCREEN);
    5.73 +		lc.lcOutExtY = -GetSystemMetrics(SM_CYSCREEN);
    5.74  
    5.75 -	lc.lcOutExtX = GetSystemMetrics(SM_CXSCREEN);
    5.76 -	lc.lcOutExtY = -GetSystemMetrics(SM_CYSCREEN);
    5.77 -	if(window->id>highestId)
    5.78 -	{
    5.79 -		HCTX* tmp_hctx;
    5.80 -		highestId=window->id;
    5.81 -		tmp_hctx= (HCTX*)SDL_realloc(g_hCtx,(highestId+1)*sizeof(HCTX));
    5.82 -		if (!tmp_hctx) {
    5.83 -            SDL_OutOfMemory();
    5.84 -            return -1;
    5.85 -        }
    5.86 -		g_hCtx=tmp_hctx;
    5.87 +		if(window->id>highestId)
    5.88 +		{
    5.89 +			HCTX* tmp_hctx;
    5.90 +			highestId=window->id;
    5.91 +			tmp_hctx= (HCTX*)SDL_realloc(g_hCtx,(highestId+1)*sizeof(HCTX));
    5.92 +			if (!tmp_hctx) {
    5.93 +				SDL_OutOfMemory();
    5.94 +				return -1;
    5.95 +			}
    5.96 +			g_hCtx=tmp_hctx;
    5.97 +		}
    5.98 +
    5.99 +		g_hCtx[window->id] = WTOpen(hwnd, &lc, TRUE);
   5.100  	}
   5.101  
   5.102 -	g_hCtx[window->id] = WTOpen(hwnd, &lc, TRUE);
   5.103 +	/*we're telling the window, we want it to report raw input events from mice*/
   5.104  
   5.105  	Rid.usUsagePage = 0x01; 
   5.106  	Rid.usUsage = 0x02; 
   5.107 -	//Rid.usUsage = MOUSE_MOVE_ABSOLUTE;
   5.108 -	Rid.dwFlags = RIDEV_INPUTSINK;   // adds HID mouse and also ignores legacy mouse messages
   5.109 +	Rid.dwFlags = RIDEV_INPUTSINK;  
   5.110  	Rid.hwndTarget = hwnd;
   5.111  
   5.112  	RegisterRawInputDevices(&Rid, 1, sizeof(Rid));
   5.113 @@ -456,6 +466,7 @@
   5.114              DestroyWindow(data->hwnd);
   5.115          }
   5.116          SDL_free(data);
   5.117 +		/*lets close the tablet context for the destoryed window*/
   5.118  		WTClose(g_hCtx[window->id]);
   5.119      }
   5.120  }