1 | /* $Id: win32wndhandle.cpp,v 1.20 2004-03-18 13:18:33 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 | * TODO: a linear list might not be the optimal implementation. Look
|
---|
14 | * into using some sort of tree (not important)
|
---|
15 | *
|
---|
16 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
17 | *
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include <os2win.h>
|
---|
21 | #include <vmutex.h>
|
---|
22 | #include "win32wndhandle.h"
|
---|
23 | #include <custombuild.h>
|
---|
24 |
|
---|
25 | #define DBG_LOCALLOG DBG_win32wndhandle
|
---|
26 | #include "dbglocal.h"
|
---|
27 |
|
---|
28 | //******************************************************************************
|
---|
29 |
|
---|
30 | //
|
---|
31 | // Global DLL Data (keep it in sync with globaldata.asm!)
|
---|
32 | //
|
---|
33 | extern ULONG WindowHandleTable[MAX_WINDOW_HANDLES]; // = {0}
|
---|
34 | extern CRITICAL_SECTION_OS2 globalwhandlecritsect; // = {0}
|
---|
35 | extern ULONG lastIndex; // = 0
|
---|
36 |
|
---|
37 | //******************************************************************************
|
---|
38 | //******************************************************************************
|
---|
39 |
|
---|
40 | static const char *pszWndHandleSemName = WINHANDLE_CRITSECTION_NAME;
|
---|
41 |
|
---|
42 | //******************************************************************************
|
---|
43 | //******************************************************************************
|
---|
44 | void WIN32API SetCustomWndHandleSemName(LPSTR pszSemName)
|
---|
45 | {
|
---|
46 | pszWndHandleSemName = pszSemName;
|
---|
47 | }
|
---|
48 | //******************************************************************************
|
---|
49 | //******************************************************************************
|
---|
50 | void InitializeWindowHandles()
|
---|
51 | {
|
---|
52 | if(globalwhandlecritsect.hevLock == 0) {
|
---|
53 | dprintf(("InitializeWindowHandles %x -> create shared critical section", &globalwhandlecritsect));
|
---|
54 | }
|
---|
55 | else {
|
---|
56 | dprintf(("InitializeWindowHandles %x -> access shared critical section", &globalwhandlecritsect));
|
---|
57 | }
|
---|
58 | DosAccessCriticalSection(&globalwhandlecritsect, pszWndHandleSemName);
|
---|
59 | }
|
---|
60 | //******************************************************************************
|
---|
61 | //******************************************************************************
|
---|
62 | void FinalizeWindowHandles()
|
---|
63 | {
|
---|
64 | DosDeleteCriticalSection(&globalwhandlecritsect);
|
---|
65 | }
|
---|
66 | //******************************************************************************
|
---|
67 | //******************************************************************************
|
---|
68 | BOOL HwAllocateWindowHandle(HWND *hwnd, DWORD dwUserData)
|
---|
69 | {
|
---|
70 | DosEnterCriticalSection(&globalwhandlecritsect);
|
---|
71 |
|
---|
72 | //find next free handle
|
---|
73 | if(lastIndex >= MAX_WINDOW_HANDLES-1) {
|
---|
74 | lastIndex = 0;
|
---|
75 | }
|
---|
76 | int i;
|
---|
77 | for(i=lastIndex;i<MAX_WINDOW_HANDLES;i++) {
|
---|
78 | if(WindowHandleTable[i] == 0) {
|
---|
79 | lastIndex = i;
|
---|
80 | break;
|
---|
81 | }
|
---|
82 | }
|
---|
83 | if(i == MAX_WINDOW_HANDLES) {
|
---|
84 | //oops, out of handles
|
---|
85 | DosLeaveCriticalSection(&globalwhandlecritsect);
|
---|
86 | dprintf(("ERROR: USER32: HwAllocateWindowHandle OUT OF WINDOW HANDLES!!"));
|
---|
87 | DebugInt3();
|
---|
88 | return FALSE;
|
---|
89 | }
|
---|
90 | *hwnd = lastIndex+1; //we skip handle 0x68000000
|
---|
91 | *hwnd |= WNDHANDLE_MAGIC_HIGHWORD;
|
---|
92 | WindowHandleTable[lastIndex] = dwUserData;
|
---|
93 |
|
---|
94 | lastIndex++;
|
---|
95 | DosLeaveCriticalSection(&globalwhandlecritsect);
|
---|
96 | dprintf2(("HwAllocateWindowHandle %x", *hwnd));
|
---|
97 | return TRUE;
|
---|
98 | }
|
---|
99 | //******************************************************************************
|
---|
100 | //******************************************************************************
|
---|
101 | void HwFreeWindowHandle(HWND hwnd)
|
---|
102 | {
|
---|
103 | hwnd = hwnd - 1; //we skip handle 0x68000000
|
---|
104 | hwnd &= WNDHANDLE_MAGIC_MASK;
|
---|
105 | if(hwnd < MAX_WINDOW_HANDLES) {
|
---|
106 | DosEnterCriticalSection(&globalwhandlecritsect);
|
---|
107 | WindowHandleTable[hwnd] = 0;
|
---|
108 | DosLeaveCriticalSection(&globalwhandlecritsect);
|
---|
109 | }
|
---|
110 | dprintf2(("HwFreeWindowHandle %x", hwnd));
|
---|
111 | }
|
---|
112 | //******************************************************************************
|
---|
113 | //******************************************************************************
|
---|
114 | BOOL HwGetWindowHandleData(HWND hwnd, DWORD *pdwUserData)
|
---|
115 | {
|
---|
116 | hwnd = hwnd - 1; //we skip handle 0x68000000
|
---|
117 | if((hwnd & 0xFFFF0000) != WNDHANDLE_MAGIC_HIGHWORD) {
|
---|
118 | return FALSE; //unknown window (PM?)
|
---|
119 | }
|
---|
120 | hwnd &= WNDHANDLE_MAGIC_MASK;
|
---|
121 | if(hwnd < MAX_WINDOW_HANDLES) {
|
---|
122 | *pdwUserData = WindowHandleTable[hwnd];
|
---|
123 | return TRUE;
|
---|
124 | }
|
---|
125 | *pdwUserData = 0;
|
---|
126 | return FALSE;
|
---|
127 | }
|
---|
128 | //******************************************************************************
|
---|
129 | //******************************************************************************
|
---|