Release 1.5.29.
[wine] / dlls / wintab32 / wintab32.c
1 /*
2  * WinTab32 library
3  *
4  * Copyright 2003 CodeWeavers, Aric Stewart
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "winerror.h"
28 #define NOFIX32
29 #include "wintab.h"
30 #include "wintab_internal.h"
31 #include "wine/gdi_driver.h"
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(wintab32);
35
36 HWND hwndDefault = NULL;
37 static const WCHAR
38   WC_TABLETCLASSNAME[] = {'W','i','n','e','T','a','b','l','e','t','C','l','a','s','s',0};
39 static CRITICAL_SECTION_DEBUG csTablet_debug =
40 {
41     0, 0, &csTablet,
42     { &csTablet_debug.ProcessLocksList, &csTablet_debug.ProcessLocksList },
43       0, 0, { (DWORD_PTR)(__FILE__ ": csTablet") }
44 };
45 CRITICAL_SECTION csTablet = { &csTablet_debug, -1, 0, 0, 0, 0 };
46
47 int  (CDECL *pLoadTabletInfo)(HWND hwnddefault) = NULL;
48 int  (CDECL *pGetCurrentPacket)(LPWTPACKET packet) = NULL;
49 int  (CDECL *pAttachEventQueueToTablet)(HWND hOwner) = NULL;
50 UINT (CDECL *pWTInfoW)(UINT wCategory, UINT nIndex, LPVOID lpOutput) = NULL;
51
52 static LRESULT WINAPI TABLET_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
53                                           LPARAM lParam);
54
55 static VOID TABLET_Register(void)
56 {
57     WNDCLASSW wndClass;
58     ZeroMemory(&wndClass, sizeof(WNDCLASSW));
59     wndClass.style = CS_GLOBALCLASS;
60     wndClass.lpfnWndProc = TABLET_WindowProc;
61     wndClass.cbClsExtra = 0;
62     wndClass.cbWndExtra = 0;
63     wndClass.hCursor = NULL;
64     wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW +1);
65     wndClass.lpszClassName = WC_TABLETCLASSNAME;
66     RegisterClassW(&wndClass);
67 }
68
69 static VOID TABLET_Unregister(void)
70 {
71     UnregisterClassW(WC_TABLETCLASSNAME, NULL);
72 }
73
74 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpReserved)
75 {
76     static const WCHAR name[] = {'T','a','b','l','e','t',0};
77
78     TRACE("%p, %x, %p\n",hInstDLL,fdwReason,lpReserved);
79     switch (fdwReason)
80     {
81         case DLL_PROCESS_ATTACH:
82             TRACE("Initialization\n");
83             DisableThreadLibraryCalls(hInstDLL);
84             TABLET_Register();
85             hwndDefault = CreateWindowW(WC_TABLETCLASSNAME, name,
86                                         WS_POPUPWINDOW,0,0,0,0,0,0,hInstDLL,0);
87             if (hwndDefault)
88             {
89                 HDC hdc = GetDC( hwndDefault );
90                 HMODULE module = __wine_get_driver_module( hdc );
91
92                 pLoadTabletInfo = (void *)GetProcAddress(module, "LoadTabletInfo");
93                 pAttachEventQueueToTablet = (void *)GetProcAddress(module, "AttachEventQueueToTablet");
94                 pGetCurrentPacket = (void *)GetProcAddress(module, "GetCurrentPacket");
95                 pWTInfoW = (void *)GetProcAddress(module, "WTInfoW");
96                 ReleaseDC( hwndDefault, hdc );
97             }
98             else
99                 return FALSE;
100             break;
101         case DLL_PROCESS_DETACH:
102             TRACE("Detaching\n");
103             if (hwndDefault)
104             {
105                 DestroyWindow(hwndDefault);
106                 hwndDefault = 0;
107             }
108             TABLET_Unregister();
109             DeleteCriticalSection(&csTablet);
110             break;
111     }
112     return TRUE;
113 }
114
115
116 /*
117  * The window proc for the default TABLET window
118  */
119 static LRESULT WINAPI TABLET_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
120                                           LPARAM lParam)
121 {
122     TRACE("Incoming Message 0x%x  (0x%08x, 0x%08x)\n", uMsg, (UINT)wParam,
123            (UINT)lParam);
124
125     switch(uMsg)
126     {
127         case WM_NCCREATE:
128             return TRUE;
129
130         case WT_PACKET:
131             {
132                 WTPACKET packet;
133                 LPOPENCONTEXT handler;
134                 if (pGetCurrentPacket)
135                 {
136                     pGetCurrentPacket(&packet);
137                     handler = AddPacketToContextQueue(&packet,(HWND)lParam);
138                     if (handler && handler->context.lcOptions & CXO_MESSAGES)
139                        TABLET_PostTabletMessage(handler, _WT_PACKET(handler->context.lcMsgBase),
140                                    (WPARAM)packet.pkSerialNumber,
141                                    (LPARAM)handler->handle, FALSE);
142                 }
143                 break;
144             }
145         case WT_PROXIMITY:
146             {
147                 WTPACKET packet;
148                 LPOPENCONTEXT handler;
149                 if (pGetCurrentPacket)
150                 {
151                     pGetCurrentPacket(&packet);
152                     handler = AddPacketToContextQueue(&packet,(HWND)wParam);
153                     if (handler)
154                         TABLET_PostTabletMessage(handler, WT_PROXIMITY,
155                                                 (WPARAM)handler->handle, lParam, TRUE);
156                 }
157                 break;
158             }
159     }
160     return 0;
161 }