00001 /* $Id: win32wndhandle.cpp,v 1.10 2001/10/17 15:16:58 phaller Exp $ */ 00002 /* 00003 * Win32 Handle Management Code for OS/2 00004 * 00005 * 00006 * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl) 00007 * 00008 * 00009 * TODO: The table should be dynamically increased when necessary 00010 * This is just a quick and dirty implementation 00011 * TODO: Probably need to clean up the table when app closes 00012 * (also store PID and remove all handles with PID of terminating app) 00013 * 00014 * Project Odin Software License can be found in LICENSE.TXT 00015 * 00016 */ 00017 00018 #include <os2win.h> 00019 #include <vmutex.h> 00020 #include "win32wndhandle.h" 00021 00022 #define DBG_LOCALLOG DBG_win32wndhandle 00023 #include "dbglocal.h" 00024 00025 //****************************************************************************** 00026 00027 //NOTE: This must be in the local data segment -> if a shared semaphore was 00028 // created by a different process, the handle returned by DosOpenMutexSem 00029 // will be returned in hGlobalTableMutex 00030 HMTX hGlobalTableMutex = 0; 00031 00032 //Global DLL Data 00033 #pragma data_seg(_GLOBALDATA) 00034 ULONG WindowHandleTable[MAX_WINDOW_HANDLES] = {0}; 00035 VMutex tableMutex(VMUTEX_SHARED, &hGlobalTableMutex); 00036 ULONG lastIndex = 0; 00037 #pragma data_seg() 00038 00039 //****************************************************************************** 00040 //****************************************************************************** 00041 BOOL HwAllocateWindowHandle(HWND *hwnd, DWORD dwUserData) 00042 { 00043 tableMutex.enter(VMUTEX_WAIT_FOREVER, &hGlobalTableMutex); 00044 00045 //find next free handle 00046 if(lastIndex >= MAX_WINDOW_HANDLES-1) { 00047 lastIndex = 0; 00048 } 00049 for(int i=lastIndex;i<MAX_WINDOW_HANDLES;i++) { 00050 if(WindowHandleTable[i] == 0) { 00051 lastIndex = i; 00052 break; 00053 } 00054 } 00055 if(i == MAX_WINDOW_HANDLES) { 00056 //oops, out of handles 00057 tableMutex.leave(&hGlobalTableMutex); 00058 dprintf(("ERROR: USER32: HwAllocateWindowHandle OUT OF WINDOW HANDLES!!")); 00059 DebugInt3(); 00060 return FALSE; 00061 } 00062 *hwnd = lastIndex; 00063 *hwnd |= WNDHANDLE_MAGIC_HIGHWORD; 00064 WindowHandleTable[lastIndex] = dwUserData; 00065 00066 lastIndex++; 00067 tableMutex.leave(&hGlobalTableMutex); 00068 return TRUE; 00069 } 00070 //****************************************************************************** 00071 //****************************************************************************** 00072 void HwFreeWindowHandle(HWND hwnd) 00073 { 00074 hwnd &= WNDHANDLE_MAGIC_MASK; 00075 if(hwnd < MAX_WINDOW_HANDLES) { 00076 tableMutex.enter(VMUTEX_WAIT_FOREVER, &hGlobalTableMutex); 00077 WindowHandleTable[hwnd] = 0; 00078 tableMutex.leave(&hGlobalTableMutex); 00079 } 00080 } 00081 //****************************************************************************** 00082 //****************************************************************************** 00083 /* 2001-10-17 PH 00084 * Note: this function is repeated as "inline macro" in win32wbase.cpp. 00085 * Changes here must be reflected there, tool. 00086 */ 00087 BOOL HwGetWindowHandleData(HWND hwnd, DWORD *pdwUserData) 00088 { 00089 if((hwnd & 0xFFFF0000) != WNDHANDLE_MAGIC_HIGHWORD) { 00090 return FALSE; //unknown window (PM?) 00091 } 00092 hwnd &= WNDHANDLE_MAGIC_MASK; 00093 if(hwnd < MAX_WINDOW_HANDLES) { 00094 *pdwUserData = WindowHandleTable[hwnd]; 00095 return TRUE; 00096 } 00097 *pdwUserData = 0; 00098 return FALSE; 00099 } 00100 //****************************************************************************** 00101 //******************************************************************************