winedbg: Don't dereference possibly NULL thread pointer.
[wine/zf.git] / dlls / wintab32 / wintab32.c
blobd56706b4223d0961346377c5e902f289d3da8084
1 /*
2 * WinTab32 library
4 * Copyright 2003 CodeWeavers, Aric Stewart
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.
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.
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
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winreg.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "winerror.h"
29 #define NOFIX32
30 #include "wintab.h"
31 #include "wintab_internal.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(wintab32);
36 HWND hwndDefault = NULL;
37 static CRITICAL_SECTION_DEBUG csTablet_debug =
39 0, 0, &csTablet,
40 { &csTablet_debug.ProcessLocksList, &csTablet_debug.ProcessLocksList },
41 0, 0, { (DWORD_PTR)(__FILE__ ": csTablet") }
43 CRITICAL_SECTION csTablet = { &csTablet_debug, -1, 0, 0, 0, 0 };
45 int (CDECL *pLoadTabletInfo)(HWND hwnddefault) = NULL;
46 int (CDECL *pGetCurrentPacket)(LPWTPACKET packet) = NULL;
47 int (CDECL *pAttachEventQueueToTablet)(HWND hOwner) = NULL;
48 UINT (CDECL *pWTInfoW)(UINT wCategory, UINT nIndex, LPVOID lpOutput) = NULL;
50 static LRESULT WINAPI TABLET_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
51 LPARAM lParam);
53 static VOID TABLET_Register(void)
55 WNDCLASSW wndClass;
56 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
57 wndClass.style = CS_GLOBALCLASS;
58 wndClass.lpfnWndProc = TABLET_WindowProc;
59 wndClass.cbClsExtra = 0;
60 wndClass.cbWndExtra = 0;
61 wndClass.hCursor = NULL;
62 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW +1);
63 wndClass.lpszClassName = L"WineTabletClass";
64 RegisterClassW(&wndClass);
67 static VOID TABLET_Unregister(void)
69 UnregisterClassW(L"WineTabletClass", NULL);
72 static HMODULE load_graphics_driver(void)
74 static const WCHAR key_pathW[] = L"System\\CurrentControlSet\\Control\\Video\\{";
75 static const WCHAR displayW[] = L"}\\0000";
77 HMODULE ret = 0;
78 HKEY hkey;
79 DWORD size;
80 WCHAR path[MAX_PATH];
81 WCHAR key[ARRAY_SIZE(key_pathW) + ARRAY_SIZE(displayW) + 40];
82 UINT guid_atom = HandleToULong( GetPropW( GetDesktopWindow(), L"__wine_display_device_guid" ));
84 if (!guid_atom) return 0;
85 memcpy( key, key_pathW, sizeof(key_pathW) );
86 if (!GlobalGetAtomNameW( guid_atom, key + lstrlenW(key), 40 )) return 0;
87 lstrcatW( key, displayW );
88 if (RegOpenKeyW( HKEY_LOCAL_MACHINE, key, &hkey )) return 0;
89 size = sizeof(path);
90 if (!RegQueryValueExW( hkey, L"GraphicsDriver", NULL, NULL, (BYTE *)path, &size )) ret = LoadLibraryW( path );
91 RegCloseKey( hkey );
92 TRACE( "%s %p\n", debugstr_w(path), ret );
93 return ret;
96 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpReserved)
98 TRACE("%p, %x, %p\n",hInstDLL,fdwReason,lpReserved);
99 switch (fdwReason)
101 case DLL_PROCESS_ATTACH:
102 TRACE("Initialization\n");
103 DisableThreadLibraryCalls(hInstDLL);
104 TABLET_Register();
105 hwndDefault = CreateWindowW(L"WineTabletClass", L"Tablet",
106 WS_POPUPWINDOW,0,0,0,0,0,0,hInstDLL,0);
107 if (hwndDefault)
109 HMODULE module = load_graphics_driver();
110 pLoadTabletInfo = (void *)GetProcAddress(module, "LoadTabletInfo");
111 pAttachEventQueueToTablet = (void *)GetProcAddress(module, "AttachEventQueueToTablet");
112 pGetCurrentPacket = (void *)GetProcAddress(module, "GetCurrentPacket");
113 pWTInfoW = (void *)GetProcAddress(module, "WTInfoW");
115 else
116 return FALSE;
117 break;
118 case DLL_PROCESS_DETACH:
119 if (lpReserved) break;
120 TRACE("Detaching\n");
121 if (hwndDefault) DestroyWindow(hwndDefault);
122 TABLET_Unregister();
123 DeleteCriticalSection(&csTablet);
124 break;
126 return TRUE;
131 * The window proc for the default TABLET window
133 static LRESULT WINAPI TABLET_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
134 LPARAM lParam)
136 TRACE("Incoming Message 0x%x (0x%08x, 0x%08x)\n", uMsg, (UINT)wParam,
137 (UINT)lParam);
139 switch(uMsg)
141 case WM_NCCREATE:
142 return TRUE;
144 case WT_PACKET:
146 WTPACKET packet;
147 LPOPENCONTEXT handler;
148 if (pGetCurrentPacket)
150 pGetCurrentPacket(&packet);
151 handler = AddPacketToContextQueue(&packet,(HWND)lParam);
152 if (handler && handler->context.lcOptions & CXO_MESSAGES)
153 TABLET_PostTabletMessage(handler, _WT_PACKET(handler->context.lcMsgBase),
154 (WPARAM)packet.pkSerialNumber,
155 (LPARAM)handler->handle, FALSE);
157 break;
159 case WT_PROXIMITY:
161 WTPACKET packet;
162 LPOPENCONTEXT handler;
163 if (pGetCurrentPacket)
165 pGetCurrentPacket(&packet);
166 handler = AddPacketToContextQueue(&packet,(HWND)wParam);
167 if (handler)
168 TABLET_PostTabletMessage(handler, WT_PROXIMITY,
169 (WPARAM)handler->handle, lParam, TRUE);
171 break;
174 return 0;