home *** CD-ROM | disk | FTP | other *** search
- /*
- File: CompoundClassTest.c
-
- Contains: Demo/Test code for HID API inside of CompositeClassDriver
-
- Version: xxx put version here xxx
-
- */
-
- #include <string.h>
- #include <CType.h>
- #include <StdLib.h>
- #include <CursorCtl.h>
- #include <CodeFragments.h>
- #include <stdio.h>
- #include <USB.h>
-
-
- void myKeyboardInterruptProc(UInt32 theDeviceClass, void * theData);
- void myMouseInterruptProc(UInt32 theDeviceClass, void * theData);
-
- USBHIDModuleDispatchTablePtr pTheKeyboardDispatchTable;
- USBHIDModuleDispatchTablePtr pTheMouseDispatchTable;
- USBDriverDescriptionPtr pTheUSBDriverDescription;
- HIDInterruptProcPtr saveInterruptProcPtr;
-
- /*
- Important note for USB Interrupt procs
- The following code is not VM safe. If this were for a
- real application, it's important to ensure that the interrupt proc code
- is not paged out - if the process is placed into the
- background and VM is enabled.
-
- */
- void myKeyboardInterruptProc(UInt32 theDeviceClass, void * theData)
- {
- DebugStr("\pk;g");
- if (saveInterruptProcPtr != nil)
- (*saveInterruptProcPtr)(theDeviceClass, theData);
- };
-
-
- void myMouseInterruptProc(UInt32 theDeviceClass, void * theData)
- {
- DebugStr("\pm;g");
- if (saveInterruptProcPtr != nil)
- (*saveInterruptProcPtr)(theDeviceClass, theData);
- };
-
- main()
- {
-
- OSStatus status;
-
- UInt16 hidDeviceClass = 0x0003;
- UInt16 hidDeviceSubClass = 0x0001;
- UInt16 keyboardDeviceProtocol = 0x0001;
- UInt16 mouseDeviceProtocol = 0x0002;
- UInt8 ledbits = 0x0000;
- UInt32 mousedpi;
- char stage;
-
- USBHIDDescriptorPtr pMyHIDDescriptor;
-
- CFragConnectionID keyboardConnID, mouseConnID, gamepadConnID;
- CFragSymbolClass symClass;
- USBDeviceRef kbdDevRef, mouseDevRef, gamepadDevRef;
- THz currentZone;
- HIDInterruptProcPtr saveInterruptProcPtr;
- UInt32 savedRefCon;
-
- currentZone = GetZone (); // save the current zone so that we can restore it later
-
- printf("Locate Keyboard HID Module and install an interrupt service routine\n");
-
- kbdDevRef = kNoDeviceRef; // initialize this input ref so that we can find the first keyboard device
- // if it exists.
- status = USBGetNextDeviceByClass(&kbdDevRef, &keyboardConnID, hidDeviceClass, hidDeviceSubClass, keyboardDeviceProtocol);
-
-
- if (status)
- {
- printf("!!Keyboard HID Module not found!!\n");
- printf(" USBGetDriverConnectionID Status: %d\n", status);
- }
- else
- {
- printf(" USB Keyboard connection ID: %d\n", keyboardConnID);
- SetZone (SystemZone ()); // need to set the zone to the system zone to call FindSymbol
- // for a symbol of a USB driver that is loaded into the
- // system zone.
- status = FindSymbol (keyboardConnID, "\pTheHIDModuleDispatchTable", (Ptr *)&pTheKeyboardDispatchTable, &symClass);
- SetZone (currentZone); // restore the zone
- if (status)
- {
- printf("!!HID Dispatch Table not found!!\n");
- printf(" FindSymbol Status: %d\n", status);
- }
- else
- {
- printf(" HID Dispatch Table @: %0p\n\n", pTheKeyboardDispatchTable);
-
- SetZone (SystemZone ()); // need to set the zone to the system zone to call FindSymbol
- // for a symbol of a USB driver that is loaded into the
- // system zone.
- status = FindSymbol (keyboardConnID, "\pTheUSBDriverDescription", (Ptr *)&pTheUSBDriverDescription, &symClass);
- SetZone (currentZone); // restore the zone
- if (!status)
- {
- switch(pTheUSBDriverDescription->usbDriverType.usbDriverVersion.stage)
- {
- case developStage:
- stage = 'd';
- break;
-
- case alphaStage:
- stage = 'a';
- break;
-
- case betaStage:
- stage = 'b';
- break;
-
- case finalStage:
- stage = 'f';
- break;
- };
- printf("*** Name: %s\n", pTheUSBDriverDescription->usbDriverType.nameInfoStr);
- printf("*** Version: %d.%x%c%x\n", pTheUSBDriverDescription->usbDriverType.usbDriverVersion.majorRev,
- pTheUSBDriverDescription->usbDriverType.usbDriverVersion.minorAndBugRev,
- stage,
- pTheUSBDriverDescription->usbDriverType.usbDriverVersion.nonRelRev);
- fflush(stdout);
- }
-
- // before we install an interrupt handler, we need to obtain the current handler setting
- // so that we can restore it later
- status = (*(pTheKeyboardDispatchTable->pUSBHIDGetDeviceInfo))(kHIDGetInterruptHandler, &saveInterruptProcPtr);
-
- if (status)
- printf("*** Keyboard Interrupt Handler not returned\n");
- else
- {
- // get the saved interrupt refcon
- status = (*(pTheKeyboardDispatchTable->pUSBHIDGetDeviceInfo))(kHIDGetInterruptRefcon, &savedRefCon);
- if (status)
- printf("*** Keyboard Interrupt refcon not returned\n");
- }
-
- if (!status)
- {
- // now install our specified interrupt proc
- status = (*(pTheKeyboardDispatchTable->pUSBHIDInstallInterrupt))(myKeyboardInterruptProc, 0);
- if (status)
- printf("*** Keyboard Interrupt could not be set\n");
- }
-
- if (!status)
- {
- printf("*** Keyboard Interrupt Handler successfully installed\n");
- printf("*** Setting LED bit #1\n", (int)ledbits);
- fflush(stdout);
- ledbits = 0x01;
- (*pTheKeyboardDispatchTable->pUSBHIDControlDevice) ( (UInt32)1, (void *)&ledbits );
-
- printf("*** Setting LED bit #2\n", (int)ledbits);
- fflush(stdout);
- ledbits = 0x02;
- (*pTheKeyboardDispatchTable->pUSBHIDControlDevice) ( (UInt32)1, (void *)&ledbits );
-
- printf("*** Setting LED bit #3\n", (int)ledbits);
- fflush(stdout);
- ledbits = 0x04;
- (*pTheKeyboardDispatchTable->pUSBHIDControlDevice) ( (UInt32)1, (void *)&ledbits );
-
- printf("*** Clearing LED bits\n", (int)ledbits);
- fflush(stdout);
- ledbits = 0x00;
- (*pTheKeyboardDispatchTable->pUSBHIDControlDevice) ( (UInt32)1, (void *)&ledbits );
-
- printf("*** Enable keyboard raw report mode\n");
- ledbits = 0x03;
- (*pTheKeyboardDispatchTable->pUSBHIDControlDevice) ( (UInt32)100, (void *)0);
- fflush(stdout);
-
- (*(pTheKeyboardDispatchTable->pUSBHIDInstallInterrupt))(saveInterruptProcPtr, savedRefCon);
- printf("*** Keyboard Interrupt Handler successfully restored\n");
- }
-
- }
- }
- fflush(stdout);
-
- printf("\n\n\n");
-
- printf("Locate Mouse HID Module and install an interrupt service routine\n");
-
- mouseDevRef = kNoDeviceRef; // initialize this input ref so that we can find the first usb mouse
- // if it exists.
- status = USBGetNextDeviceByClass (&mouseDevRef, &mouseConnID, hidDeviceClass, hidDeviceSubClass, mouseDeviceProtocol);
- if (status)
- {
- printf("!!Mouse HID Module not found!!\n");
- printf(" USBGetDriverConnectionID Status: %d\n", status);
- }
- else
- {
- printf(" USB Mouse connection ID: %d\n", mouseConnID);
- SetZone (SystemZone ()); // need to set the zone to the system zone to call FindSymbol
- // for a symbol of a USB driver that is loaded into the
- // system zone.
- status = FindSymbol (mouseConnID, "\pTheHIDModuleDispatchTable", (Ptr *)&pTheMouseDispatchTable, &symClass);
- SetZone (currentZone); // restore the zone
- if (status)
- {
- printf("!!HID Dispatch Table not found!!\n");
- printf(" FindSymbol Status: %d\n", status);
- }
- else
- {
- printf(" HID Dispatch Table @: %0p\n\n", pTheMouseDispatchTable);
-
- SetZone (SystemZone ()); // need to set the zone to the system zone to call FindSymbol
- // for a symbol of a USB driver that is loaded into the
- // system zone.
- status = FindSymbol (mouseConnID, "\pTheUSBDriverDescription", (Ptr *)&pTheUSBDriverDescription, &symClass);
- SetZone (currentZone); // restore the zone
- if (!status)
- {
- switch(pTheUSBDriverDescription->usbDriverType.usbDriverVersion.stage)
- {
- case developStage:
- stage = 'd';
- break;
-
- case alphaStage:
- stage = 'a';
- break;
-
- case betaStage:
- stage = 'b';
- break;
-
- case finalStage:
- stage = 'f';
- break;
- };
- printf("*** Name: %s\n", pTheUSBDriverDescription->usbDriverType.nameInfoStr);
- printf("*** Version: %d.%x%c%x\n", pTheUSBDriverDescription->usbDriverType.usbDriverVersion.majorRev,
- pTheUSBDriverDescription->usbDriverType.usbDriverVersion.minorAndBugRev,
- stage,
- pTheUSBDriverDescription->usbDriverType.usbDriverVersion.nonRelRev);
- fflush(stdout);
- }
-
- printf("*** Get DPI from the mouse...\n");
- status = (*pTheMouseDispatchTable->pUSBHIDGetDeviceInfo) ((UInt32)kHIDGetDeviceUnitsPerInch, (void *)&mousedpi );
- if (status == noErr)
- {
- printf("*** DPI ('fixed') = %ld (0x%lx)\n",mousedpi,mousedpi);
- printf("*** DPI ('int') = %ld (0x%lx)\n",(mousedpi >> 16), (mousedpi >> 16));
- }
- else
- printf("*** pUSBHIDGetDeviceInfo failed to mouse\n");
-
- fflush(stdout);
-
- // get the current interrupt handler
- status = (*(pTheMouseDispatchTable->pUSBHIDGetDeviceInfo))(kHIDGetInterruptHandler, &saveInterruptProcPtr);
-
- if (status)
- printf("*** mouse Interrupt Handler not returned\n");
- else
- {
- // get the saved interrupt refcon
- status = (*(pTheMouseDispatchTable->pUSBHIDGetDeviceInfo))(kHIDGetInterruptRefcon, &savedRefCon);
- if (status)
- printf("*** mouse Interrupt refcon not returned\n");
- }
-
- if (!status)
- {
- printf("*** Installing mouse interrupt handler\n");
- fflush(stdout);
- status = (*(pTheMouseDispatchTable->pUSBHIDInstallInterrupt))(myMouseInterruptProc, 0);
- if (!status)
- printf("*** mouse Interrupt could not be set\n");
- }
-
- if (!status)
- {
- printf("*** Mouse Interrupt Handler successfully installed\n");
- fflush(stdout);
-
- (*(pTheMouseDispatchTable->pUSBHIDInstallInterrupt))(saveInterruptProcPtr, savedRefCon);
- printf("*** mouse Interrupt Handler successfully restored\n");
- }
- }
- }
-
- fflush(stdout);
-
- }