Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
comments added and improved code look(windows part)
Browse files Browse the repository at this point in the history
  • Loading branch information
Szymon Wilczek committed Aug 2, 2008
1 parent 8032bc1 commit a110d66
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 90 deletions.
30 changes: 19 additions & 11 deletions src/events/SDL_mouse.c
Expand Up @@ -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)
Expand Down Expand Up @@ -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));
Expand All @@ -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);

Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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;
Expand Down
19 changes: 14 additions & 5 deletions src/video/win32/SDL_win32events.c
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -206,24 +209,27 @@ 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;
const RAWINPUTHEADER *header;
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)
Expand All @@ -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);
Expand All @@ -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);
Expand Down
86 changes: 51 additions & 35 deletions src/video/win32/SDL_win32mouse.c
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;i<devCount;++i)
{
int j;
Expand All @@ -83,17 +91,21 @@ WIN_InitMouse(_THIS)
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;
}
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));

/*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;
Expand All @@ -120,6 +132,8 @@ WIN_InitMouse(_THIS)
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)
Expand All @@ -129,46 +143,50 @@ WIN_InitMouse(_THIS)

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]))
{
break;
}
}
if(k==5)
if(tab[k]!=SDL_tolower((unsigned char)device_name[j+k]))
{
tablet=index;
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);
}
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);
Expand All @@ -179,10 +197,8 @@ WIN_QuitMouse(_THIS)
{
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: */
Expand Down
8 changes: 4 additions & 4 deletions src/video/win32/SDL_win32video.c
Expand Up @@ -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 */

Expand Down

0 comments on commit a110d66

Please sign in to comment.