Moved palette functions to user_main.c and removed
[wine/multimedia.git] / dlls / user / user_main.c
blob70d2a0370111ec927b13c2ecadaad17ebc2f9e67
1 /*
2 * USER initialization code
4 * Copyright 2000 Alexandre Julliard
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <stdarg.h>
22 #include <string.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "winreg.h"
28 #include "wine/winbase16.h"
29 #include "wine/winuser16.h"
31 #include "controls.h"
32 #include "cursoricon.h"
33 #include "message.h"
34 #include "user.h"
35 #include "win.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(graphics);
40 USER_DRIVER USER_Driver;
42 WORD USER_HeapSel = 0; /* USER heap selector */
43 HMODULE user32_module = 0;
45 static HPALETTE (WINAPI *pfnGDISelectPalette)( HDC hdc, HPALETTE hpal, WORD bkgnd );
46 static UINT (WINAPI *pfnGDIRealizePalette)( HDC hdc );
47 static HPALETTE hPrimaryPalette;
49 static HMODULE graphics_driver;
50 static DWORD exiting_thread_id;
52 extern void WDML_NotifyThreadDetach(void);
54 #define GET_USER_FUNC(name) USER_Driver.p##name = (void*)GetProcAddress( graphics_driver, #name )
56 /* load the graphics driver */
57 static BOOL load_driver(void)
59 char buffer[MAX_PATH], *name, *next;
60 HKEY hkey;
62 strcpy( buffer, "x11drv,ttydrv" ); /* default value */
63 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Wine", &hkey ))
65 DWORD type, count = sizeof(buffer);
66 RegQueryValueExA( hkey, "GraphicsDriver", 0, &type, buffer, &count );
67 RegCloseKey( hkey );
70 name = buffer;
71 while (name)
73 next = strchr( name, ',' );
74 if (next) *next++ = 0;
76 if ((graphics_driver = LoadLibraryA( name )) != 0) break;
77 name = next;
79 if (!graphics_driver)
81 MESSAGE( "wine: Could not load graphics driver '%s'.\n", buffer );
82 if (!strcasecmp( buffer, "x11drv" ))
83 MESSAGE( "Make sure that your X server is running and that $DISPLAY is set correctly.\n" );
84 ExitProcess(1);
87 GET_USER_FUNC(InitKeyboard);
88 GET_USER_FUNC(VkKeyScanEx);
89 GET_USER_FUNC(MapVirtualKeyEx);
90 GET_USER_FUNC(GetKeyNameText);
91 GET_USER_FUNC(ToUnicodeEx);
92 GET_USER_FUNC(GetKeyboardLayoutList);
93 GET_USER_FUNC(GetKeyboardLayout);
94 GET_USER_FUNC(GetKeyboardLayoutName);
95 GET_USER_FUNC(LoadKeyboardLayout);
96 GET_USER_FUNC(ActivateKeyboardLayout);
97 GET_USER_FUNC(UnloadKeyboardLayout);
98 GET_USER_FUNC(Beep);
99 GET_USER_FUNC(InitMouse);
100 GET_USER_FUNC(SetCursor);
101 GET_USER_FUNC(GetCursorPos);
102 GET_USER_FUNC(SetCursorPos);
103 GET_USER_FUNC(GetScreenSaveActive);
104 GET_USER_FUNC(SetScreenSaveActive);
105 GET_USER_FUNC(AcquireClipboard);
106 GET_USER_FUNC(EmptyClipboard);
107 GET_USER_FUNC(SetClipboardData);
108 GET_USER_FUNC(GetClipboardData);
109 GET_USER_FUNC(CountClipboardFormats);
110 GET_USER_FUNC(EnumClipboardFormats);
111 GET_USER_FUNC(IsClipboardFormatAvailable);
112 GET_USER_FUNC(RegisterClipboardFormat);
113 GET_USER_FUNC(GetClipboardFormatName);
114 GET_USER_FUNC(EndClipboardUpdate);
115 GET_USER_FUNC(ResetSelectionOwner);
116 GET_USER_FUNC(ChangeDisplaySettingsExW);
117 GET_USER_FUNC(EnumDisplaySettingsExW);
118 GET_USER_FUNC(CreateWindow);
119 GET_USER_FUNC(DestroyWindow);
120 GET_USER_FUNC(GetDC);
121 GET_USER_FUNC(ForceWindowRaise);
122 GET_USER_FUNC(MsgWaitForMultipleObjectsEx);
123 GET_USER_FUNC(ReleaseDC);
124 GET_USER_FUNC(ScrollDC);
125 GET_USER_FUNC(SetFocus);
126 GET_USER_FUNC(SetParent);
127 GET_USER_FUNC(SetWindowPos);
128 GET_USER_FUNC(SetWindowRgn);
129 GET_USER_FUNC(SetWindowIcon);
130 GET_USER_FUNC(SetWindowStyle);
131 GET_USER_FUNC(SetWindowText);
132 GET_USER_FUNC(ShowWindow);
133 GET_USER_FUNC(SysCommandSizeMove);
135 return TRUE;
139 /***********************************************************************
140 * UserSelectPalette (Not a Windows API)
142 static HPALETTE WINAPI UserSelectPalette( HDC hDC, HPALETTE hPal, BOOL bForceBackground )
144 WORD wBkgPalette = 1;
146 if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
148 HWND hwnd = WindowFromDC( hDC );
149 if (hwnd)
151 HWND hForeground = GetForegroundWindow();
152 /* set primary palette if it's related to current active */
153 if (hForeground == hwnd || IsChild(hForeground,hwnd))
155 wBkgPalette = 0;
156 hPrimaryPalette = hPal;
160 return pfnGDISelectPalette( hDC, hPal, wBkgPalette);
164 /***********************************************************************
165 * UserRealizePalette (USER32.@)
167 UINT WINAPI UserRealizePalette( HDC hDC )
169 UINT realized = pfnGDIRealizePalette( hDC );
171 /* do not send anything if no colors were changed */
172 if (realized && GetCurrentObject( hDC, OBJ_PAL ) == hPrimaryPalette)
174 /* send palette change notification */
175 HWND hWnd = WindowFromDC( hDC );
176 if (hWnd) SendMessageTimeoutW( HWND_BROADCAST, WM_PALETTECHANGED, (WPARAM)hWnd, 0,
177 SMTO_ABORTIFHUNG, 2000, NULL );
179 return realized;
183 /***********************************************************************
184 * palette_init
186 * Patch the function pointers in GDI for SelectPalette and RealizePalette
188 static void palette_init(void)
190 void **ptr;
191 HMODULE module = GetModuleHandleA( "gdi32" );
192 if (!module)
194 ERR( "cannot get GDI32 handle\n" );
195 return;
197 if ((ptr = (void**)GetProcAddress( module, "pfnSelectPalette" )))
198 pfnGDISelectPalette = InterlockedExchangePointer( ptr, UserSelectPalette );
199 else ERR( "cannot find pfnSelectPalette in GDI32\n" );
200 if ((ptr = (void**)GetProcAddress( module, "pfnRealizePalette" )))
201 pfnGDIRealizePalette = InterlockedExchangePointer( ptr, UserRealizePalette );
202 else ERR( "cannot find pfnRealizePalette in GDI32\n" );
206 /***********************************************************************
207 * USER initialisation routine
209 static BOOL process_attach(void)
211 HINSTANCE16 instance;
213 /* Create USER heap */
214 if ((instance = LoadLibrary16( "USER.EXE" )) >= 32) USER_HeapSel = instance | 7;
215 else
217 USER_HeapSel = GlobalAlloc16( GMEM_FIXED, 65536 );
218 LocalInit16( USER_HeapSel, 32, 65534 );
221 /* some Win9x dlls expect keyboard to be loaded */
222 if (GetVersion() & 0x80000000) LoadLibrary16( "keyboard.drv" );
224 /* Load the graphics driver */
225 if (!load_driver()) return FALSE;
227 /* Initialize system colors and metrics */
228 SYSMETRICS_Init();
229 SYSCOLOR_Init();
231 /* Setup palette function pointers */
232 palette_init();
234 /* Initialize built-in window classes */
235 CLASS_RegisterBuiltinClasses();
237 /* Initialize menus */
238 if (!MENU_Init()) return FALSE;
240 /* Initialize message spying */
241 if (!SPY_Init()) return FALSE;
243 /* Create message queue of initial thread */
244 InitThreadInput16( 0, 0 );
246 /* Create desktop window */
247 if (!WIN_CreateDesktopWindow()) return FALSE;
249 /* Initialize keyboard driver */
250 if (USER_Driver.pInitKeyboard) USER_Driver.pInitKeyboard( InputKeyStateTable );
252 /* Initialize mouse driver */
253 if (USER_Driver.pInitMouse) USER_Driver.pInitMouse( InputKeyStateTable );
255 return TRUE;
259 /**********************************************************************
260 * USER_IsExitingThread
262 BOOL USER_IsExitingThread( DWORD tid )
264 return (tid == exiting_thread_id);
268 /**********************************************************************
269 * thread_detach
271 static void thread_detach(void)
273 exiting_thread_id = GetCurrentThreadId();
275 WDML_NotifyThreadDetach();
277 WIN_DestroyThreadWindows( GetDesktopWindow() );
278 QUEUE_DeleteMsgQueue();
280 exiting_thread_id = 0;
284 /**********************************************************************
285 * process_detach
287 static void process_detach(void)
289 memset(&USER_Driver, 0, sizeof(USER_Driver));
290 FreeLibrary(graphics_driver);
293 /***********************************************************************
294 * UserClientDllInitialize (USER32.@)
296 * USER dll initialisation routine (exported as UserClientDllInitialize for compatibility).
298 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
300 BOOL ret = TRUE;
301 switch(reason)
303 case DLL_PROCESS_ATTACH:
304 user32_module = inst;
305 ret = process_attach();
306 break;
307 case DLL_PROCESS_DETACH:
308 process_detach();
309 break;
310 case DLL_THREAD_DETACH:
311 thread_detach();
312 break;
314 return ret;