Checking for stylus position change

by no2chem 29. April 2009 17:46

Cobaltikus asked if it was possible to detect stylus position change without the registry. After carefully inspecting keyboard driver, it appears that the registry is the best way. You would use code that looks like this:

VOID StylusThread()
{
	HANDLE	hEvents[2];
	HKEY	hKey;
	DWORD	dwSize = sizeof(DWORD);
	DWORD	dwStylusPosition = NULL;

	RegOpenKeyEx(HKEY_CURRENT_USER, _T("ControlPanel\\Keybd"), NULL, NULL, &hKey);
	
	hEvents[0] = CeFindFirstRegChange(hKey, FALSE, REG_NOTIFY_CHANGE_LAST_SET);
	hEvents[1] = CreateEvent(NULL, FALSE, FALSE, _T("NueSIPStylus\\KillSIPThread"));
	
	ResetEvent(hEvents[1]);

	while (TRUE)
	{
		if (WaitForMultipleObjects(2, hEvents, FALSE, INFINITE) == WAIT_OBJECT_0)
		{
			DWORD dwPrevious = dwStylusPosition; //save previous position
			RegQueryValueEx(hKey, _T("StylusOutStatus"), NULL, NULL, (LPBYTE) &dwStylusPosition, &dwSize);
			if (dwPrevious != dwStylusPosition)
			{
				//only change if StylusOutStatus was value that changed
				doSIPChange(dwStylusPosition);
				//don't forget to reset the event
				CeFindNextRegChange(hEvents[0]);
			}
		}

		else
		{
			//Wait failed, or kill event set
			break;
		}
	}

	//Clean up
	CeFindCloseRegChange(hEvents[0]);
	CloseHandle(hEvents[1]);
	RegCloseKey(hKey);
}

Okay, maybe that was a shameless test of if the prettify code plugin is working correctly. And I apologize for the lack of commented code (Actually, this code segment has way more comments than I typically do….)

Also, contrary to what I posted earlier, it is possible to query the current position of the stylus, although there is no nice API to do it. For those who know how to query GPIOs on the device, you want GPIO 0x25, at least on the RAPH800/DIAM500 (CDMA) devices, htc-linux.org doesn’t specify for GSM devices…

On the RAPH100 (GSM), it looks like there is no interrupt, and that the stylus thread waits for a named event - “StylusEvent”, and queries “KSC1:”, the MicroP controller on the position of the stylus. Fortunately, both drivers write the registry “StylusOutStatus” value, so as long as you query that, you’ll be okay.

nueSIPStylus will query the GPIO on CDMA devices and set the stylus position in the registry at boot, remedying the “take stylus out, soft reset, and put stylus back in before OS boots” problem, at least on CDMA devices.

Tags: ,

nueTools | Development

Disclaimer
Windows Mobile is a registered trademark of Microsoft Corporation in the United States and other countries.

Wei Enterprises is not affiliated in any way with Microsoft, HTC, Sprint, or any other wireless carrier/phone manufacturer otherwise mentioned on this site.

Copyright 2012 2009 Wei Enterprises