Main Page   Data Structures   File List   Data Fields   Globals  

lapi.c

Go to the documentation of this file.
00001 #define MAXLENGTH 1000
00002 
00003 #include <windows.h>
00004 
00005 #include "guid.h"
00006 #include "lapi.h"
00007 
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 
00011 #include <setupapi.h>
00012 
00013 typedef struct {
00014       HANDLE handle;
00015       HANDLE suspend;
00016       char *devicename;
00017 } MouseData;
00018 
00019 callback theCallback = NULL;
00020 MouseData *mice = NULL;
00021 int maxmouse = 1;
00022 int maxused = 0;
00023 HANDLE hMutex = NULL;
00024 
00025 void __cdecl lRegisterCallback(callback c) {
00026       if (hMutex == NULL) {
00027             hMutex = CreateMutex(NULL, FALSE, NULL);
00028       }
00029       theCallback = c;
00030 }
00031 
00032 void __cdecl lUnRegisterCallback(void) {
00033       theCallback = NULL;
00034       CloseHandle(hMutex); hMutex = NULL;
00035 }
00036 
00037 void ApcCallback(PVOID NormalContext, PVOID SystemArgument1, PVOID SystemArgument2) {
00038       if (theCallback) {
00039             while (WaitForSingleObject(hMutex, INFINITE) != WAIT_OBJECT_0) ;
00040             theCallback((int) NormalContext,
00041                         (signed int) (signed short int) ((((unsigned int) SystemArgument1) & 0xffff0000) >> 16),
00042                         (signed int) (signed short int) (((unsigned int ) SystemArgument1) & 0xffff),
00043                         (unsigned int) ((((unsigned int) SystemArgument2) & 0xffff0000) >> 16),
00044                         (int) (((unsigned int) SystemArgument2) & 0xffff));
00045             ReleaseMutex(hMutex);
00046       }
00047 }
00048 
00049 int __cdecl lGetMice(int count) {
00050       int number = 0;
00051       HANDLE new = INVALID_HANDLE_VALUE;
00052       HDEVINFO hardwareDeviceInfo;
00053       SP_INTERFACE_DEVICE_DATA deviceInfoData;
00054       BOOLEAN done = FALSE;
00055       ULONG i = 0;
00056 
00057       PSP_INTERFACE_DEVICE_DETAIL_DATA functionClassDeviceData = NULL;
00058       ULONG predictedLength = 0;
00059       ULONG requiredLength = 0;
00060 
00061       hardwareDeviceInfo = SetupDiGetClassDevs(
00062                   (LPGUID) &GUID_CLASS_MOUSE_CPNTOOLS,
00063                   NULL, // Define no enumerator (global)
00064                   NULL, // Define no
00065                   (DIGCF_PRESENT | // Only Devices present
00066                    DIGCF_DEVICEINTERFACE)); // Function class devices
00067 
00068       deviceInfoData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);
00069 
00070       while ((number < count) || (count == 0)) {
00071             if (SetupDiEnumDeviceInterfaces(
00072                               hardwareDeviceInfo,
00073                               0, // Don't care about specific PDOs
00074                               (LPGUID) &GUID_CLASS_MOUSE_CPNTOOLS,
00075                               i,
00076                               &deviceInfoData)) {
00077                   SetupDiGetInterfaceDeviceDetail(
00078                               hardwareDeviceInfo,
00079                               &deviceInfoData,
00080                               NULL, // probe, no output buffer,
00081                               0, // probe, output buffer of length 0
00082                               &requiredLength,
00083                               NULL); // not interested in specific dev-node
00084 
00085                   functionClassDeviceData = (PSP_INTERFACE_DEVICE_DETAIL_DATA) malloc(requiredLength);
00086                   functionClassDeviceData->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
00087                   predictedLength = requiredLength;
00088 
00089                   if (SetupDiGetInterfaceDeviceDetail(
00090                                     hardwareDeviceInfo,
00091                                     &deviceInfoData,
00092                                     functionClassDeviceData,
00093                                     predictedLength,
00094                                     &requiredLength,
00095                                     NULL)) {
00096                         char c[MAXLENGTH];
00097                         sprintf(c, "%s\\execute\\get\\%u\\%u", 
00098                                     functionClassDeviceData->DevicePath,
00099                                     maxused + 1,
00100                                     &ApcCallback);
00101 
00102                         new = CreateFile(
00103                                     c,
00104                                     0,
00105                                     FILE_SHARE_READ | FILE_SHARE_WRITE, // Don't want to share access
00106                                     NULL, // no SECURITY_ATTRIBUTES structure
00107                                     OPEN_EXISTING, // No special create flags
00108                                     0, // No special attributes
00109                                     NULL); // No template file
00110 
00111                         sprintf(c, "%s", functionClassDeviceData->DevicePath);
00112 
00113                         if (new != INVALID_HANDLE_VALUE) {
00114                               maxused++;
00115                               number++;
00116                               if (maxused >= maxmouse) {
00117                                     maxmouse *= 2;
00118                                     mice = (MouseData *) realloc(mice, maxmouse * sizeof(MouseData));
00119                               }
00120                               mice[maxused].handle = new;
00121                               mice[maxused].suspend = NULL;
00122                               mice[maxused].devicename = (char *) malloc((strlen(functionClassDeviceData->DevicePath) + 1) * sizeof(char));
00123                               strcpy(mice[maxused].devicename, functionClassDeviceData->DevicePath);
00124                         }
00125                   }
00126 
00127                   free(functionClassDeviceData);
00128             } else if (ERROR_NO_MORE_ITEMS == GetLastError())
00129                   break;
00130             i++;
00131       }
00132 
00133       SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
00134       return number;
00135 }
00136 
00137 int __cdecl lHasMouse(int number) {
00138       if ((number <= maxused) && (number > 0)) {
00139             return mice[number].handle != 0;
00140       }
00141       return 0;
00142 }
00143 
00144 void __cdecl lUnGetMouse(number) {
00145       if (lHasMouse(number)) {
00146             lUnSuspendMouse(number);
00147             CloseHandle(mice[number].handle);
00148             mice[number].handle = NULL;
00149             free(mice[number].devicename);
00150             mice[number].devicename = NULL;
00151             while ((maxused > 0) && (lHasMouse(maxused))) maxused--;
00152       }
00153 }
00154 
00155 void __cdecl lUnGetAllMice() {
00156       int i;
00157       for (i = 0; i < maxused; ++i)
00158             lUnGetMouse(i);
00159 }
00160 
00161 void __cdecl lSuspendMouse(number) {
00162       if (lHasMouse(number)) {
00163             if (mice[number].suspend == NULL) {
00164                   char c[MAXLENGTH];
00165                   sprintf(c, "%s\\execute\\suspend", mice[number].devicename);
00166                   mice[number].suspend = CreateFile(
00167                               c, // Filename
00168                               0, // Access
00169                               FILE_SHARE_READ | FILE_SHARE_WRITE, // Share
00170                               NULL, // No SECURITY_ATTRIBUTES
00171                               OPEN_EXISTING,
00172                               0, // No special attributes
00173                               NULL); // No template file
00174             }
00175       }
00176 }
00177 
00178 void __cdecl lUnSuspendMouse(number) {
00179       if (lHasMouse(number)) {
00180             if (mice[number].suspend) {
00181                   CloseHandle(mice[number].suspend);
00182                   mice[number].suspend = NULL;
00183             }
00184       }
00185 }

Generated on Mon Jan 12 21:47:21 2004 for CPNMouse by doxygen1.2.18