| 1 | /* $Id: win32wndhandle.cpp,v 1.14 2002-06-13 14:02:50 sandervl Exp $ */ | 
|---|
| 2 | /* | 
|---|
| 3 | * Win32 Handle Management Code for OS/2 | 
|---|
| 4 | * | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl) | 
|---|
| 7 | * | 
|---|
| 8 | * | 
|---|
| 9 | * TODO: The table should be dynamically increased when necessary | 
|---|
| 10 | *       This is just a quick and dirty implementation | 
|---|
| 11 | * TODO: Probably need to clean up the table when app closes | 
|---|
| 12 | *       (also store PID and remove all handles with PID of terminating app) | 
|---|
| 13 | * | 
|---|
| 14 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 15 | * | 
|---|
| 16 | */ | 
|---|
| 17 |  | 
|---|
| 18 | #include <os2win.h> | 
|---|
| 19 | #include <vmutex.h> | 
|---|
| 20 | #include "win32wndhandle.h" | 
|---|
| 21 | #include <custombuild.h> | 
|---|
| 22 |  | 
|---|
| 23 | #define DBG_LOCALLOG    DBG_win32wndhandle | 
|---|
| 24 | #include "dbglocal.h" | 
|---|
| 25 |  | 
|---|
| 26 | //****************************************************************************** | 
|---|
| 27 |  | 
|---|
| 28 | //Global DLL Data | 
|---|
| 29 | #pragma data_seg(_GLOBALDATA) | 
|---|
| 30 | ULONG                WindowHandleTable[MAX_WINDOW_HANDLES] = {0}; | 
|---|
| 31 | CRITICAL_SECTION_OS2 globalwhandlecritsect = {0}; | 
|---|
| 32 | ULONG                lastIndex = 0; | 
|---|
| 33 | #pragma data_seg() | 
|---|
| 34 |  | 
|---|
| 35 | static char *pszWndHandleSemName = NULL; | 
|---|
| 36 |  | 
|---|
| 37 | //****************************************************************************** | 
|---|
| 38 | //****************************************************************************** | 
|---|
| 39 | void WIN32API SetCustomWndHandleSemName(LPSTR pszSemName) | 
|---|
| 40 | { | 
|---|
| 41 | pszWndHandleSemName = pszSemName; | 
|---|
| 42 | } | 
|---|
| 43 | //****************************************************************************** | 
|---|
| 44 | //****************************************************************************** | 
|---|
| 45 | void InitializeWindowHandles() | 
|---|
| 46 | { | 
|---|
| 47 | if(globalwhandlecritsect.hmtxLock == 0) { | 
|---|
| 48 | DosInitializeCriticalSection(&globalwhandlecritsect, (pszWndHandleSemName) ? pszWndHandleSemName : WINHANDLE_CRITSECTION_NAME); | 
|---|
| 49 | } | 
|---|
| 50 | else { | 
|---|
| 51 | dprintf(("InitializeWindowHandles -> access shared critical section")); | 
|---|
| 52 | DosAccessCriticalSection(&globalwhandlecritsect, WINHANDLE_CRITSECTION_NAME); | 
|---|
| 53 | } | 
|---|
| 54 | } | 
|---|
| 55 | //****************************************************************************** | 
|---|
| 56 | //****************************************************************************** | 
|---|
| 57 | BOOL HwAllocateWindowHandle(HWND *hwnd, DWORD dwUserData) | 
|---|
| 58 | { | 
|---|
| 59 | DosEnterCriticalSection(&globalwhandlecritsect); | 
|---|
| 60 |  | 
|---|
| 61 | //find next free handle | 
|---|
| 62 | if(lastIndex >= MAX_WINDOW_HANDLES-1) { | 
|---|
| 63 | lastIndex = 0; | 
|---|
| 64 | } | 
|---|
| 65 | for(int i=lastIndex;i<MAX_WINDOW_HANDLES;i++) { | 
|---|
| 66 | if(WindowHandleTable[i] == 0) { | 
|---|
| 67 | lastIndex = i; | 
|---|
| 68 | break; | 
|---|
| 69 | } | 
|---|
| 70 | } | 
|---|
| 71 | if(i == MAX_WINDOW_HANDLES) { | 
|---|
| 72 | //oops, out of handles | 
|---|
| 73 | DosLeaveCriticalSection(&globalwhandlecritsect); | 
|---|
| 74 | dprintf(("ERROR: USER32: HwAllocateWindowHandle OUT OF WINDOW HANDLES!!")); | 
|---|
| 75 | DebugInt3(); | 
|---|
| 76 | return FALSE; | 
|---|
| 77 | } | 
|---|
| 78 | *hwnd  = lastIndex+1; //we skip handle 0x68000000 | 
|---|
| 79 | *hwnd |= WNDHANDLE_MAGIC_HIGHWORD; | 
|---|
| 80 | WindowHandleTable[lastIndex] = dwUserData; | 
|---|
| 81 |  | 
|---|
| 82 | lastIndex++; | 
|---|
| 83 | DosLeaveCriticalSection(&globalwhandlecritsect); | 
|---|
| 84 | dprintf2(("HwAllocateWindowHandle %x", *hwnd)); | 
|---|
| 85 | return TRUE; | 
|---|
| 86 | } | 
|---|
| 87 | //****************************************************************************** | 
|---|
| 88 | //****************************************************************************** | 
|---|
| 89 | void HwFreeWindowHandle(HWND hwnd) | 
|---|
| 90 | { | 
|---|
| 91 | hwnd = hwnd - 1; //we skip handle 0x68000000 | 
|---|
| 92 | hwnd &= WNDHANDLE_MAGIC_MASK; | 
|---|
| 93 | if(hwnd < MAX_WINDOW_HANDLES) { | 
|---|
| 94 | DosEnterCriticalSection(&globalwhandlecritsect); | 
|---|
| 95 | WindowHandleTable[hwnd] = 0; | 
|---|
| 96 | DosLeaveCriticalSection(&globalwhandlecritsect); | 
|---|
| 97 | } | 
|---|
| 98 | dprintf2(("HwFreeWindowHandle %x", hwnd)); | 
|---|
| 99 | } | 
|---|
| 100 | //****************************************************************************** | 
|---|
| 101 | //****************************************************************************** | 
|---|
| 102 | BOOL HwGetWindowHandleData(HWND hwnd, DWORD *pdwUserData) | 
|---|
| 103 | { | 
|---|
| 104 | hwnd = hwnd - 1; //we skip handle 0x68000000 | 
|---|
| 105 | if((hwnd & 0xFFFF0000) != WNDHANDLE_MAGIC_HIGHWORD) { | 
|---|
| 106 | return FALSE; //unknown window (PM?) | 
|---|
| 107 | } | 
|---|
| 108 | hwnd &= WNDHANDLE_MAGIC_MASK; | 
|---|
| 109 | if(hwnd < MAX_WINDOW_HANDLES) { | 
|---|
| 110 | *pdwUserData = WindowHandleTable[hwnd]; | 
|---|
| 111 | return TRUE; | 
|---|
| 112 | } | 
|---|
| 113 | *pdwUserData = 0; | 
|---|
| 114 | return FALSE; | 
|---|
| 115 | } | 
|---|
| 116 | //****************************************************************************** | 
|---|
| 117 | //****************************************************************************** | 
|---|