| 1 | #define INCL_WIN | 
|---|
| 2 | #define INCL_DOS | 
|---|
| 3 | #define INCL_VIO | 
|---|
| 4 | #define INCL_SUB | 
|---|
| 5 | #define INCL_DOSERRORS | 
|---|
| 6 | #define INCL_WINSWITCHLIST | 
|---|
| 7 | #include <os2.h> | 
|---|
| 8 | #include "DosQuerySysState2.h" | 
|---|
| 9 | #include <stdio.h> | 
|---|
| 10 | #include <stdlib.h> | 
|---|
| 11 |  | 
|---|
| 12 | #include "vio32Private.h" | 
|---|
| 13 | #include "vio32Utils.h" | 
|---|
| 14 | #include "vio32SharedData.h" | 
|---|
| 15 | #include "vio32Client.h" | 
|---|
| 16 |  | 
|---|
| 17 |  | 
|---|
| 18 | APIRET EXPENTRY vio32Open(){ | 
|---|
| 19 | return kbd32ClientInstance.Open(); | 
|---|
| 20 | } | 
|---|
| 21 |  | 
|---|
| 22 | APIRET EXPENTRY vio32Close(){ | 
|---|
| 23 | return kbd32ClientInstance.Close(); | 
|---|
| 24 | } | 
|---|
| 25 |  | 
|---|
| 26 | APIRET EXPENTRY kbd32GetKey(KBD32KEYINFO *key,ULONG msecTimeout){ | 
|---|
| 27 | return kbd32ClientInstance.GetKeyStroke(key,msecTimeout); | 
|---|
| 28 | } | 
|---|
| 29 |  | 
|---|
| 30 | APIRET EXPENTRY vio32GetMouse(VIO32MOUINFO *mou,ULONG msecTimeout){ | 
|---|
| 31 | return kbd32ClientInstance.GetMouse(mou,msecTimeout); | 
|---|
| 32 | } | 
|---|
| 33 |  | 
|---|
| 34 | APIRET EXPENTRY vio32GetEvent(VIO32EVENTINFO *event,ULONG *eventType,ULONG msecTimeout){ | 
|---|
| 35 | return kbd32ClientInstance.GetEvent(event,eventType,msecTimeout); | 
|---|
| 36 | } | 
|---|
| 37 |  | 
|---|
| 38 | APIRET EXPENTRY vio32PeekEvent(VIO32EVENTINFO *event,ULONG *eventType,ULONG position){ | 
|---|
| 39 | return kbd32ClientInstance.PeekEvent(event,eventType,position); | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 | APIRET EXPENTRY vio32FlushEventQ(){ | 
|---|
| 43 | return kbd32ClientInstance.FlushEventQ(); | 
|---|
| 44 | } | 
|---|
| 45 |  | 
|---|
| 46 | APIRET EXPENTRY vio32GetEventQSize(ULONG *size,ULONG msecTimeout){ | 
|---|
| 47 | return kbd32ClientInstance.GetEventQSize(size,msecTimeout); | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | APIRET EXPENTRY vio32WriteEvent(VIO32EVENTINFO *event, ULONG *eventType){ | 
|---|
| 51 | return kbd32ClientInstance.WriteEvent(event,eventType); | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | APIRET EXPENTRY kbd32GetKeyStatus(VIO32STATUSINFO *status){ | 
|---|
| 55 | return kbd32ClientInstance.GetKeyStatus(status); | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | APIRET EXPENTRY kbd32SetMode(ULONG mode){ | 
|---|
| 59 | return kbd32ClientInstance.SetMode(mode); | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | APIRET EXPENTRY kbd32GetMode(ULONG *mode){ | 
|---|
| 63 | return kbd32ClientInstance.GetMode(mode); | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | APIRET kbd32AddFilter(vio32Filter *filter,ULONG *where){ | 
|---|
| 67 | ULONG defaultPos=VIO32_PRE_FIRST; | 
|---|
| 68 | if (where == NULL) where = &defaultPos; | 
|---|
| 69 | return kbd32ClientInstance.AddFilter(filter,*where); | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | APIRET kbd32RemoveFilter(VOID *filterData){ | 
|---|
| 73 | return kbd32ClientInstance.RemoveFilter(filterData); | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | // hook stuff | 
|---|
| 77 | HMODULE module  = 0; | 
|---|
| 78 | PFN     pfn1     = NULL; | 
|---|
| 79 | PFN     pfn2     = NULL; | 
|---|
| 80 |  | 
|---|
| 81 | BOOL EXPENTRY InstallHook(){ | 
|---|
| 82 | // already done! | 
|---|
| 83 | if (module != 0) return FALSE; | 
|---|
| 84 |  | 
|---|
| 85 | // already done! | 
|---|
| 86 | if (pfn1 != NULL) return FALSE; | 
|---|
| 87 |  | 
|---|
| 88 | // already done! | 
|---|
| 89 | if (pfn2 != NULL) return FALSE; | 
|---|
| 90 |  | 
|---|
| 91 | // now morph into PM on this thread in acse it didn't happen before | 
|---|
| 92 | PPIB pib; | 
|---|
| 93 | PTIB tib; | 
|---|
| 94 | DosGetInfoBlocks(&tib, &pib); | 
|---|
| 95 | if (pib->pib_ultype == 2) pib->pib_ultype = 3; | 
|---|
| 96 |  | 
|---|
| 97 | // this code can be run only from a VIO app | 
|---|
| 98 | if (pib->pib_ultype != 3) return FALSE; | 
|---|
| 99 |  | 
|---|
| 100 | // get anchor block | 
|---|
| 101 | HAB hab = WinInitialize(0); | 
|---|
| 102 |  | 
|---|
| 103 | // now get the message Queue | 
|---|
| 104 | HMQ queue = WinQueryWindowULong(VIOWindowClient(), QWL_HMQ); | 
|---|
| 105 |  | 
|---|
| 106 | // now get my own module handle | 
|---|
| 107 | char buffer[256]; | 
|---|
| 108 | APIRET rc = DosLoadModule(buffer,256,"vio32.dll",&module); | 
|---|
| 109 | if (rc != 0){ | 
|---|
| 110 | printf("DosLoadModule rc is %i\n", rc); | 
|---|
| 111 | return FALSE; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | // get function pointer | 
|---|
| 115 | rc = DosQueryProcAddr(module,1,NULL,&pfn1); | 
|---|
| 116 | if (rc != 0){ | 
|---|
| 117 | printf("DosQueryProcAddr(2) rc is %i\n", rc); | 
|---|
| 118 | return FALSE; | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | BOOL ret = WinSetHook(hab,queue,HK_INPUT,pfn1,module); | 
|---|
| 122 | if (!ret){ | 
|---|
| 123 | printf("WinSetHook(1) failed\n"); | 
|---|
| 124 | return FALSE; | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | // get function pointer | 
|---|
| 128 | rc = DosQueryProcAddr(module,2,NULL,&pfn2); | 
|---|
| 129 | if (rc != 0){ | 
|---|
| 130 | printf("DosQueryProcAddr(2) rc is %i\n", rc); | 
|---|
| 131 | return FALSE; | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | ret = WinSetHook(hab,queue,HK_SENDMSG,pfn2,module); | 
|---|
| 135 | if (!ret){ | 
|---|
| 136 | printf("WinSetHook(2) failed\n"); | 
|---|
| 137 | return FALSE; | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | return (rc == 0); | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | BOOL EXPENTRY DeInstallHook(){ | 
|---|
| 144 | // never installed | 
|---|
| 145 | if (module == 0) return FALSE; | 
|---|
| 146 |  | 
|---|
| 147 | // never installed | 
|---|
| 148 | if (pfn1 == NULL) return FALSE; | 
|---|
| 149 |  | 
|---|
| 150 | // never installed | 
|---|
| 151 | if (pfn2 == NULL) return FALSE; | 
|---|
| 152 |  | 
|---|
| 153 | // now morph into PM on this thread in acse it didn't happen before | 
|---|
| 154 | PPIB pib; | 
|---|
| 155 | PTIB tib; | 
|---|
| 156 | DosGetInfoBlocks(&tib, &pib); | 
|---|
| 157 | if (pib->pib_ultype == 2) pib->pib_ultype = 3; | 
|---|
| 158 |  | 
|---|
| 159 | // this code can be run only from a VIO app | 
|---|
| 160 | if (pib->pib_ultype != 3) return FALSE; | 
|---|
| 161 |  | 
|---|
| 162 | // get anchor block | 
|---|
| 163 | HAB hab = WinInitialize(0); | 
|---|
| 164 |  | 
|---|
| 165 | // now get the message Queue | 
|---|
| 166 | HMQ queue = WinQueryWindowULong(VIOWindowClient(), QWL_HMQ); | 
|---|
| 167 |  | 
|---|
| 168 | // finally remove hook | 
|---|
| 169 | BOOL ret = WinReleaseHook(hab,queue,HK_INPUT,pfn1,module); | 
|---|
| 170 |  | 
|---|
| 171 | // remove hook 2 | 
|---|
| 172 | ret &= WinReleaseHook(hab,queue,HK_SENDMSG,pfn2,module); | 
|---|
| 173 |  | 
|---|
| 174 | // deallocate the DLL | 
|---|
| 175 | DosFreeModule(module); | 
|---|
| 176 | module = NULL; | 
|---|
| 177 | pfn1   = NULL; | 
|---|
| 178 | pfn2   = NULL; | 
|---|
| 179 |  | 
|---|
| 180 | return ret; | 
|---|
| 181 | } | 
|---|
| 182 |  | 
|---|
| 183 |  | 
|---|
| 184 | BOOL  EXPENTRY InputHook(HAB hab, PQMSG pQmsg, ULONG fs){ | 
|---|
| 185 |  | 
|---|
| 186 | // take only mouse and kbd messages | 
|---|
| 187 | //    if ((pQmsg->msg < WM_MOUSEMOVE) || (pQmsg->msg > WM_CHAR)) return FALSE; | 
|---|
| 188 | if (pQmsg->msg != WM_CHAR) return FALSE; | 
|---|
| 189 |  | 
|---|
| 190 | // now grab mutex sem | 
|---|
| 191 | APIRET ret; | 
|---|
| 192 | HMTX   hmtx = 0; | 
|---|
| 193 | BOOL   locked = FALSE; | 
|---|
| 194 | ret = DosOpenMutexSem((PSZ)MUTEX_NAME,&hmtx); | 
|---|
| 195 | if (ret == 0){ | 
|---|
| 196 | ret = DosRequestMutexSem(hmtx,SEM_INDEFINITE_WAIT); | 
|---|
| 197 | if (ret == 0) locked = TRUE; | 
|---|
| 198 | } | 
|---|
| 199 |  | 
|---|
| 200 | // grab shared mem | 
|---|
| 201 | kbd32SharedData *SharedData = NULL; | 
|---|
| 202 | BOOL haveMem = FALSE; | 
|---|
| 203 | if (ret == 0){ | 
|---|
| 204 | ret = DosGetNamedSharedMem((PPVOID)&SharedData,(PSZ)SHMEM_NAME,PAG_READ | PAG_WRITE); | 
|---|
| 205 | // ok I have the pointer to the memory I am ok! let's release the memory | 
|---|
| 206 | if (ret == 0) haveMem = TRUE; | 
|---|
| 207 | } | 
|---|
| 208 |  | 
|---|
| 209 | // now send the key stroke | 
|---|
| 210 | BOOL dataUsed = FALSE; | 
|---|
| 211 | if (ret == 0){ | 
|---|
| 212 | // find if the application is registered | 
|---|
| 213 | PID pid; | 
|---|
| 214 | int index; | 
|---|
| 215 | HWND hwndFrame; | 
|---|
| 216 | if (SharedData->searchByHwndClient(pQmsg->hwnd,hwndFrame,pid,index)){ | 
|---|
| 217 | // get mode of operation of given app | 
|---|
| 218 | ULONG mode = SharedData->getMode(index); | 
|---|
| 219 |  | 
|---|
| 220 | // treat specific WM_CHAR msg | 
|---|
| 221 | if (pQmsg->msg == WM_CHAR){ | 
|---|
| 222 | // trasnform qmsg in the specific msg | 
|---|
| 223 | CHRMSG *wmchar = (CHRMSG *)&pQmsg->mp1; | 
|---|
| 224 | // on special modes abort | 
|---|
| 225 | if (((mode & KBD32MODE_DISABLEBREAK)==0) && (wmchar->scancode == 110)) ret = 1; | 
|---|
| 226 | } | 
|---|
| 227 | //            else // mouse move | 
|---|
| 228 | //            if (pQmsg->msg == WM_MOUSEMOVE){ | 
|---|
| 229 | //                if ((mode & KBD32MODE_ENABLEMMOVE)==0) ret = 1; | 
|---|
| 230 | //            } else // mouse clicks | 
|---|
| 231 | //                if ((mode & KBD32MODE_ENABLEMOUSE)==0) ret = 1; | 
|---|
| 232 |  | 
|---|
| 233 |  | 
|---|
| 234 | HQUEUE keyQ = 0; | 
|---|
| 235 |  | 
|---|
| 236 | // get the associated Q | 
|---|
| 237 | if (ret == 0){ | 
|---|
| 238 | PID pid_; | 
|---|
| 239 | char qName[64]; | 
|---|
| 240 | sprintf(qName,PRIMARY_QUEUE_NAME,pid); | 
|---|
| 241 | ret = DosOpenQueue(&pid_,&keyQ,qName); | 
|---|
| 242 | } | 
|---|
| 243 |  | 
|---|
| 244 | // now check how many entries are busy | 
|---|
| 245 | ULONG entries = 0; | 
|---|
| 246 | if (ret == 0) ret = DosQueryQueue(keyQ, &entries); | 
|---|
| 247 |  | 
|---|
| 248 | // if too many are used bleep | 
|---|
| 249 | if ((ret == 0) && (entries >= MAX_PRIMARY_Q_LEN)){ | 
|---|
| 250 | ret = 1; | 
|---|
| 251 | DosBeep(1000,20); | 
|---|
| 252 | } | 
|---|
| 253 | // if all is good send data | 
|---|
| 254 | if (ret == 0){ | 
|---|
| 255 | // allocate a Q object. | 
|---|
| 256 | VIO32EVENTINFO *key = SharedData->getBuffer(index); | 
|---|
| 257 | // copy data | 
|---|
| 258 | key->pmMsg.mp1 = pQmsg->mp1; | 
|---|
| 259 | key->pmMsg.mp2 = pQmsg->mp2; | 
|---|
| 260 | // send the data | 
|---|
| 261 | ret = DosWriteQueue(keyQ,pQmsg->msg,sizeof(VIO32EVENTINFO),key,0); | 
|---|
| 262 | } | 
|---|
| 263 | // now mark the buffer as used | 
|---|
| 264 | if (ret == 0){ | 
|---|
| 265 | SharedData->advanceBuffer(index); | 
|---|
| 266 | dataUsed = TRUE; | 
|---|
| 267 | } | 
|---|
| 268 | // close Q | 
|---|
| 269 | if (keyQ != 0) DosCloseQueue(keyQ); | 
|---|
| 270 | } | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 | // release memory | 
|---|
| 274 | if (haveMem) DosFreeMem((PVOID)SharedData); | 
|---|
| 275 | haveMem = FALSE; | 
|---|
| 276 |  | 
|---|
| 277 | // relase mutex sem | 
|---|
| 278 | if (locked) DosReleaseMutexSem(hmtx); | 
|---|
| 279 | locked = FALSE; | 
|---|
| 280 |  | 
|---|
| 281 | // let click do focus change | 
|---|
| 282 | if ((pQmsg->msg >= 0x71) && (pQmsg->msg <= 0x79)) return FALSE; | 
|---|
| 283 | return dataUsed; | 
|---|
| 284 | } | 
|---|
| 285 |  | 
|---|
| 286 | VOID EXPENTRY SendMsgHook(HAB hab,PSMHSTRUCT psmh,BOOL fInterTask){ | 
|---|
| 287 | if (fInterTask) return ; | 
|---|
| 288 |  | 
|---|
| 289 | // filter messages | 
|---|
| 290 | BOOL toBeProcessed = FALSE; | 
|---|
| 291 | if (psmh->msg == WM_FOCUSCHANGE) toBeProcessed = TRUE; | 
|---|
| 292 | if (psmh->msg == WM_SIZE)        toBeProcessed = TRUE; | 
|---|
| 293 | if (psmh->msg == WM_MOVE)        toBeProcessed = TRUE; | 
|---|
| 294 |  | 
|---|
| 295 | if (!toBeProcessed) return; | 
|---|
| 296 |  | 
|---|
| 297 | // now grab mutex sem | 
|---|
| 298 | APIRET ret; | 
|---|
| 299 | HMTX   hmtx = 0; | 
|---|
| 300 | BOOL   locked = FALSE; | 
|---|
| 301 | ret = DosOpenMutexSem((PSZ)MUTEX_NAME,&hmtx); | 
|---|
| 302 | if (ret == 0){ | 
|---|
| 303 | ret = DosRequestMutexSem(hmtx,SEM_INDEFINITE_WAIT); | 
|---|
| 304 | if (ret == 0) locked = TRUE; | 
|---|
| 305 | } | 
|---|
| 306 |  | 
|---|
| 307 | // grab shared mem | 
|---|
| 308 | kbd32SharedData *SharedData = NULL; | 
|---|
| 309 | BOOL haveMem = FALSE; | 
|---|
| 310 | if (ret == 0){ | 
|---|
| 311 | ret = DosGetNamedSharedMem((PPVOID)&SharedData,(PSZ)SHMEM_NAME,PAG_READ | PAG_WRITE); | 
|---|
| 312 | // ok I have the pointer to the memory I am ok! let's release the memory | 
|---|
| 313 | if (ret == 0) haveMem = TRUE; | 
|---|
| 314 | } | 
|---|
| 315 |  | 
|---|
| 316 | // now send the key stroke | 
|---|
| 317 | BOOL dataUsed = FALSE; | 
|---|
| 318 | if (ret == 0){ | 
|---|
| 319 | // find if the application is registered | 
|---|
| 320 | PID pid; | 
|---|
| 321 | int index; | 
|---|
| 322 | HWND hwndClient; | 
|---|
| 323 | if (SharedData->searchByHwndFrame(psmh->hwnd,hwndClient,pid,index)){ | 
|---|
| 324 |  | 
|---|
| 325 | HQUEUE keyQ = 0; | 
|---|
| 326 |  | 
|---|
| 327 | // get the associated Q | 
|---|
| 328 | if (ret == 0){ | 
|---|
| 329 | PID pid_; | 
|---|
| 330 | char qName[64]; | 
|---|
| 331 | sprintf(qName,PRIMARY_QUEUE_NAME,pid); | 
|---|
| 332 | ret = DosOpenQueue(&pid_,&keyQ,qName); | 
|---|
| 333 | } | 
|---|
| 334 |  | 
|---|
| 335 | // now check how many entries are busy | 
|---|
| 336 | ULONG entries = 0; | 
|---|
| 337 | if (ret == 0) ret = DosQueryQueue(keyQ, &entries); | 
|---|
| 338 |  | 
|---|
| 339 | // if too many are used bleep | 
|---|
| 340 | if ((ret == 0) && (entries >= MAX_PRIMARY_Q_LEN)){ | 
|---|
| 341 | ret = 1; | 
|---|
| 342 | DosBeep(1000,20); | 
|---|
| 343 | } | 
|---|
| 344 | // if all is good send data | 
|---|
| 345 | if (ret == 0){ | 
|---|
| 346 | // allocate a Q object. | 
|---|
| 347 | VIO32EVENTINFO *key = SharedData->getBuffer(index); | 
|---|
| 348 | // copy content | 
|---|
| 349 | key->pmMsg.mp1 = psmh->mp1; | 
|---|
| 350 | key->pmMsg.mp2 = psmh->mp2; | 
|---|
| 351 | // send the data | 
|---|
| 352 | ret = DosWriteQueue(keyQ,psmh->msg,sizeof(VIO32EVENTINFO),key,0); | 
|---|
| 353 | } | 
|---|
| 354 | // now mark the buffer as used | 
|---|
| 355 | if (ret == 0){ | 
|---|
| 356 | SharedData->advanceBuffer(index); | 
|---|
| 357 | dataUsed = TRUE; | 
|---|
| 358 | } | 
|---|
| 359 | // close Q | 
|---|
| 360 | if (keyQ != 0) DosCloseQueue(keyQ); | 
|---|
| 361 | } | 
|---|
| 362 | } | 
|---|
| 363 |  | 
|---|
| 364 | // release memory | 
|---|
| 365 | if (haveMem) DosFreeMem((PVOID)SharedData); | 
|---|
| 366 | haveMem = FALSE; | 
|---|
| 367 |  | 
|---|
| 368 | // relase mutex sem | 
|---|
| 369 | if (locked) DosReleaseMutexSem(hmtx); | 
|---|
| 370 | locked = FALSE; | 
|---|
| 371 |  | 
|---|
| 372 |  | 
|---|
| 373 | return ; | 
|---|
| 374 | } | 
|---|
| 375 |  | 
|---|
| 376 |  | 
|---|
| 377 |  | 
|---|
| 378 |  | 
|---|