4 * Copyright 2000, 2005 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
27 #include "user_private.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(user);
32 static const USER_DRIVER null_driver, lazy_load_driver;
34 const USER_DRIVER *USER_Driver = &lazy_load_driver;
35 static DWORD driver_load_error;
37 /* load the graphics driver */
38 static const USER_DRIVER *load_driver(void)
40 char buffer[MAX_PATH], libname[32], *name, *next;
43 HMODULE graphics_driver;
44 USER_DRIVER *driver, *prev;
46 strcpy( buffer, "x11" ); /* default value */
47 /* @@ Wine registry key: HKCU\Software\Wine\Drivers */
48 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Drivers", &hkey ))
50 DWORD type, count = sizeof(buffer);
51 RegQueryValueExA( hkey, "Graphics", 0, &type, (LPBYTE) buffer, &count );
58 next = strchr( name, ',' );
59 if (next) *next++ = 0;
61 snprintf( libname, sizeof(libname), "wine%s.drv", name );
62 if ((graphics_driver = LoadLibraryA( libname )) != 0) break;
67 driver_load_error = GetLastError();
69 driver = HeapAlloc( GetProcessHeap(), 0, sizeof(*driver) );
70 memcpy( driver, &null_driver, sizeof(*driver) );
74 #define GET_USER_FUNC(name) \
75 do { if ((ptr = GetProcAddress( graphics_driver, #name ))) driver->p##name = ptr; } while(0)
77 GET_USER_FUNC(ActivateKeyboardLayout);
79 GET_USER_FUNC(GetAsyncKeyState);
80 GET_USER_FUNC(GetKeyNameText);
81 GET_USER_FUNC(GetKeyboardLayout);
82 GET_USER_FUNC(GetKeyboardLayoutList);
83 GET_USER_FUNC(GetKeyboardLayoutName);
84 GET_USER_FUNC(LoadKeyboardLayout);
85 GET_USER_FUNC(MapVirtualKeyEx);
86 GET_USER_FUNC(SendInput);
87 GET_USER_FUNC(ToUnicodeEx);
88 GET_USER_FUNC(UnloadKeyboardLayout);
89 GET_USER_FUNC(VkKeyScanEx);
90 GET_USER_FUNC(SetCursor);
91 GET_USER_FUNC(GetCursorPos);
92 GET_USER_FUNC(SetCursorPos);
93 GET_USER_FUNC(GetScreenSaveActive);
94 GET_USER_FUNC(SetScreenSaveActive);
95 GET_USER_FUNC(AcquireClipboard);
96 GET_USER_FUNC(EmptyClipboard);
97 GET_USER_FUNC(SetClipboardData);
98 GET_USER_FUNC(GetClipboardData);
99 GET_USER_FUNC(CountClipboardFormats);
100 GET_USER_FUNC(EnumClipboardFormats);
101 GET_USER_FUNC(IsClipboardFormatAvailable);
102 GET_USER_FUNC(RegisterClipboardFormat);
103 GET_USER_FUNC(GetClipboardFormatName);
104 GET_USER_FUNC(EndClipboardUpdate);
105 GET_USER_FUNC(ResetSelectionOwner);
106 GET_USER_FUNC(ChangeDisplaySettingsEx);
107 GET_USER_FUNC(EnumDisplaySettingsEx);
108 GET_USER_FUNC(CreateDesktopWindow);
109 GET_USER_FUNC(CreateWindow);
110 GET_USER_FUNC(DestroyWindow);
111 GET_USER_FUNC(GetDCEx);
112 GET_USER_FUNC(MsgWaitForMultipleObjectsEx);
113 GET_USER_FUNC(ReleaseDC);
114 GET_USER_FUNC(ScrollDC);
115 GET_USER_FUNC(SetFocus);
116 GET_USER_FUNC(SetParent);
117 GET_USER_FUNC(SetWindowPos);
118 GET_USER_FUNC(SetWindowRgn);
119 GET_USER_FUNC(SetWindowIcon);
120 GET_USER_FUNC(SetWindowStyle);
121 GET_USER_FUNC(SetWindowText);
122 GET_USER_FUNC(ShowWindow);
123 GET_USER_FUNC(SysCommandSizeMove);
124 GET_USER_FUNC(WindowFromDC);
125 GET_USER_FUNC(WindowMessage);
129 prev = InterlockedCompareExchangePointer( (void **)&USER_Driver, driver, (void *)&lazy_load_driver );
130 if (prev != &lazy_load_driver)
132 /* another thread beat us to it */
133 HeapFree( GetProcessHeap(), 0, driver );
134 FreeLibrary( graphics_driver );
140 /* unload the graphics driver on process exit */
141 void USER_unload_driver(void)
143 /* make sure we don't try to call the driver after it has been detached */
144 USER_Driver = &null_driver;
148 /**********************************************************************
151 * These are fallbacks for entry points that are not implemented in the real driver.
154 static HKL nulldrv_ActivateKeyboardLayout( HKL layout, UINT flags )
159 static void nulldrv_Beep(void)
163 static SHORT nulldrv_GetAsyncKeyState( INT key )
168 static INT nulldrv_GetKeyNameText( LONG lparam, LPWSTR buffer, INT size )
173 static HKL nulldrv_GetKeyboardLayout( DWORD layout )
178 static UINT nulldrv_GetKeyboardLayoutList( INT count, HKL *layouts )
183 static BOOL nulldrv_GetKeyboardLayoutName( LPWSTR name )
188 static HKL nulldrv_LoadKeyboardLayout( LPCWSTR name, UINT flags )
193 static UINT nulldrv_MapVirtualKeyEx( UINT code, UINT type, HKL layout )
198 static UINT nulldrv_SendInput( UINT count, LPINPUT inputs, int size )
203 static INT nulldrv_ToUnicodeEx( UINT virt, UINT scan, LPBYTE state, LPWSTR str,
204 int size, UINT flags, HKL layout )
209 static BOOL nulldrv_UnloadKeyboardLayout( HKL layout )
214 static SHORT nulldrv_VkKeyScanEx( WCHAR ch, HKL layout )
219 static void nulldrv_SetCursor( struct tagCURSORICONINFO *info )
223 static BOOL nulldrv_GetCursorPos( LPPOINT pt )
228 static BOOL nulldrv_SetCursorPos( INT x, INT y )
233 static BOOL nulldrv_GetScreenSaveActive(void)
238 static void nulldrv_SetScreenSaveActive( BOOL on )
242 static void nulldrv_AcquireClipboard( HWND hwnd )
246 static BOOL nulldrv_CountClipboardFormats(void)
251 static void nulldrv_EmptyClipboard( BOOL keepunowned )
255 static void nulldrv_EndClipboardUpdate(void)
259 static UINT nulldrv_EnumClipboardFormats( UINT format )
264 static BOOL nulldrv_GetClipboardData( UINT format, HANDLE16 *h16, HANDLE *h32 )
269 static INT nulldrv_GetClipboardFormatName( UINT format, LPWSTR buffer, UINT len )
274 static BOOL nulldrv_IsClipboardFormatAvailable( UINT format )
279 static UINT nulldrv_RegisterClipboardFormat( LPCWSTR name )
284 static void nulldrv_ResetSelectionOwner( HWND hwnd, BOOL flag )
288 static BOOL nulldrv_SetClipboardData( UINT format, HANDLE16 h16, HANDLE h32, BOOL owner )
293 static LONG nulldrv_ChangeDisplaySettingsEx( LPCWSTR name, LPDEVMODEW mode, HWND hwnd,
294 DWORD flags, LPVOID lparam )
296 return DISP_CHANGE_FAILED;
299 static BOOL nulldrv_EnumDisplaySettingsEx( LPCWSTR name, DWORD num, LPDEVMODEW mode, DWORD flags )
304 static BOOL nulldrv_CreateDesktopWindow( HWND hwnd )
309 static BOOL nulldrv_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
315 MESSAGE( "Application tried to create a window, but no driver could be loaded.\n");
316 switch (driver_load_error)
318 case ERROR_MOD_NOT_FOUND:
319 MESSAGE( "The X11 driver is missing. Check your build!\n" );
321 case ERROR_DLL_INIT_FAILED:
322 MESSAGE( "Make sure that your X server is running and that $DISPLAY is set correctly.\n" );
325 MESSAGE( "Unknown error (%ld).\n", driver_load_error );
331 static void nulldrv_DestroyWindow( HWND hwnd )
335 static HDC nulldrv_GetDCEx( HWND hwnd, HRGN hrgn, DWORD flags )
340 static DWORD nulldrv_MsgWaitForMultipleObjectsEx( DWORD count, const HANDLE *handles, DWORD timeout,
341 DWORD mask, DWORD flags )
343 return WaitForMultipleObjectsEx( count, handles, flags & MWMO_WAITALL,
344 timeout, flags & MWMO_ALERTABLE );
347 static INT nulldrv_ReleaseDC( HWND hwnd, HDC hdc, BOOL end_paint )
352 static BOOL nulldrv_ScrollDC( HDC hdc, INT dx, INT dy, const RECT *scroll, const RECT *clip,
353 HRGN hrgn, LPRECT update )
358 static void nulldrv_SetFocus( HWND hwnd )
362 static HWND nulldrv_SetParent( HWND hwnd, HWND parent )
367 static BOOL nulldrv_SetWindowPos( WINDOWPOS *pos )
372 static int nulldrv_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
377 static void nulldrv_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
381 static void nulldrv_SetWindowStyle( HWND hwnd, DWORD old_style )
385 static void nulldrv_SetWindowText( HWND hwnd, LPCWSTR text )
389 static BOOL nulldrv_ShowWindow( HWND hwnd, INT cmd )
394 static void nulldrv_SysCommandSizeMove( HWND hwnd, WPARAM wparam )
398 static HWND nulldrv_WindowFromDC( HDC hdc )
403 static LRESULT nulldrv_WindowMessage( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
408 static const USER_DRIVER null_driver =
410 /* keyboard functions */
411 nulldrv_ActivateKeyboardLayout,
413 nulldrv_GetAsyncKeyState,
414 nulldrv_GetKeyNameText,
415 nulldrv_GetKeyboardLayout,
416 nulldrv_GetKeyboardLayoutList,
417 nulldrv_GetKeyboardLayoutName,
418 nulldrv_LoadKeyboardLayout,
419 nulldrv_MapVirtualKeyEx,
422 nulldrv_UnloadKeyboardLayout,
424 /* mouse functions */
426 nulldrv_GetCursorPos,
427 nulldrv_SetCursorPos,
428 /* screen saver functions */
429 nulldrv_GetScreenSaveActive,
430 nulldrv_SetScreenSaveActive,
431 /* clipboard functions */
432 nulldrv_AcquireClipboard,
433 nulldrv_CountClipboardFormats,
434 nulldrv_EmptyClipboard,
435 nulldrv_EndClipboardUpdate,
436 nulldrv_EnumClipboardFormats,
437 nulldrv_GetClipboardData,
438 nulldrv_GetClipboardFormatName,
439 nulldrv_IsClipboardFormatAvailable,
440 nulldrv_RegisterClipboardFormat,
441 nulldrv_ResetSelectionOwner,
442 nulldrv_SetClipboardData,
444 nulldrv_ChangeDisplaySettingsEx,
445 nulldrv_EnumDisplaySettingsEx,
446 /* windowing functions */
447 nulldrv_CreateDesktopWindow,
448 nulldrv_CreateWindow,
449 nulldrv_DestroyWindow,
451 nulldrv_MsgWaitForMultipleObjectsEx,
456 nulldrv_SetWindowPos,
457 nulldrv_SetWindowRgn,
458 nulldrv_SetWindowIcon,
459 nulldrv_SetWindowStyle,
460 nulldrv_SetWindowText,
462 nulldrv_SysCommandSizeMove,
463 nulldrv_WindowFromDC,
464 nulldrv_WindowMessage
468 /**********************************************************************
469 * Lazy loading user driver
471 * Initial driver used before another driver is loaded.
472 * Each entry point simply loads the real driver and chains to it.
475 static HKL loaderdrv_ActivateKeyboardLayout( HKL layout, UINT flags )
477 return load_driver()->pActivateKeyboardLayout( layout, flags );
480 static void loaderdrv_Beep(void)
482 load_driver()->pBeep();
485 static SHORT loaderdrv_GetAsyncKeyState( INT key )
487 return load_driver()->pGetAsyncKeyState( key );
490 static INT loaderdrv_GetKeyNameText( LONG lparam, LPWSTR buffer, INT size )
492 return load_driver()->pGetKeyNameText( lparam, buffer, size );
495 static HKL loaderdrv_GetKeyboardLayout( DWORD layout )
497 return load_driver()->pGetKeyboardLayout( layout );
500 static UINT loaderdrv_GetKeyboardLayoutList( INT count, HKL *layouts )
502 return load_driver()->pGetKeyboardLayoutList( count, layouts );
505 static BOOL loaderdrv_GetKeyboardLayoutName( LPWSTR name )
507 return load_driver()->pGetKeyboardLayoutName( name );
510 static HKL loaderdrv_LoadKeyboardLayout( LPCWSTR name, UINT flags )
512 return load_driver()->pLoadKeyboardLayout( name, flags );
515 static UINT loaderdrv_MapVirtualKeyEx( UINT code, UINT type, HKL layout )
517 return load_driver()->pMapVirtualKeyEx( code, type, layout );
520 static UINT loaderdrv_SendInput( UINT count, LPINPUT inputs, int size )
522 return load_driver()->pSendInput( count, inputs, size );
525 static INT loaderdrv_ToUnicodeEx( UINT virt, UINT scan, LPBYTE state, LPWSTR str,
526 int size, UINT flags, HKL layout )
528 return load_driver()->pToUnicodeEx( virt, scan, state, str, size, flags, layout );
531 static BOOL loaderdrv_UnloadKeyboardLayout( HKL layout )
533 return load_driver()->pUnloadKeyboardLayout( layout );
536 static SHORT loaderdrv_VkKeyScanEx( WCHAR ch, HKL layout )
538 return load_driver()->pVkKeyScanEx( ch, layout );
541 static void loaderdrv_SetCursor( struct tagCURSORICONINFO *info )
543 load_driver()->pSetCursor( info );
546 static BOOL loaderdrv_GetCursorPos( LPPOINT pt )
548 return load_driver()->pGetCursorPos( pt );
551 static BOOL loaderdrv_SetCursorPos( INT x, INT y )
553 return load_driver()->pSetCursorPos( x, y );
556 static BOOL loaderdrv_GetScreenSaveActive(void)
558 return load_driver()->pGetScreenSaveActive();
561 static void loaderdrv_SetScreenSaveActive( BOOL on )
563 load_driver()->pSetScreenSaveActive( on );
566 static void loaderdrv_AcquireClipboard( HWND hwnd )
568 load_driver()->pAcquireClipboard( hwnd );
571 static BOOL loaderdrv_CountClipboardFormats(void)
573 return load_driver()->pCountClipboardFormats();
576 static void loaderdrv_EmptyClipboard( BOOL keepunowned )
578 load_driver()->pEmptyClipboard( keepunowned );
581 static void loaderdrv_EndClipboardUpdate(void)
583 load_driver()->pEndClipboardUpdate();
586 static UINT loaderdrv_EnumClipboardFormats( UINT format )
588 return load_driver()->pEnumClipboardFormats( format );
591 static BOOL loaderdrv_GetClipboardData( UINT format, HANDLE16 *h16, HANDLE *h32 )
593 return load_driver()->pGetClipboardData( format, h16, h32 );
596 static INT loaderdrv_GetClipboardFormatName( UINT format, LPWSTR buffer, UINT len )
598 return load_driver()->pGetClipboardFormatName( format, buffer, len );
601 static BOOL loaderdrv_IsClipboardFormatAvailable( UINT format )
603 return load_driver()->pIsClipboardFormatAvailable( format );
606 static UINT loaderdrv_RegisterClipboardFormat( LPCWSTR name )
608 return load_driver()->pRegisterClipboardFormat( name );
611 static void loaderdrv_ResetSelectionOwner( HWND hwnd, BOOL flag )
613 load_driver()->pResetSelectionOwner( hwnd, flag );
616 static BOOL loaderdrv_SetClipboardData( UINT format, HANDLE16 h16, HANDLE h32, BOOL owner )
618 return load_driver()->pSetClipboardData( format, h16, h32, owner );
621 static LONG loaderdrv_ChangeDisplaySettingsEx( LPCWSTR name, LPDEVMODEW mode, HWND hwnd,
622 DWORD flags, LPVOID lparam )
624 return load_driver()->pChangeDisplaySettingsEx( name, mode, hwnd, flags, lparam );
627 static BOOL loaderdrv_EnumDisplaySettingsEx( LPCWSTR name, DWORD num, LPDEVMODEW mode, DWORD flags )
629 return load_driver()->pEnumDisplaySettingsEx( name, num, mode, flags );
632 static BOOL loaderdrv_CreateDesktopWindow( HWND hwnd )
634 return load_driver()->pCreateDesktopWindow( hwnd );
637 static BOOL loaderdrv_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
639 return load_driver()->pCreateWindow( hwnd, cs, unicode );
642 static void loaderdrv_DestroyWindow( HWND hwnd )
644 load_driver()->pDestroyWindow( hwnd );
647 static HDC loaderdrv_GetDCEx( HWND hwnd, HRGN hrgn, DWORD flags )
649 return load_driver()->pGetDCEx( hwnd, hrgn, flags );
652 static DWORD loaderdrv_MsgWaitForMultipleObjectsEx( DWORD count, const HANDLE *handles, DWORD timeout,
653 DWORD mask, DWORD flags )
655 return load_driver()->pMsgWaitForMultipleObjectsEx( count, handles, timeout, mask, flags );
658 static INT loaderdrv_ReleaseDC( HWND hwnd, HDC hdc, BOOL end_paint )
660 return load_driver()->pReleaseDC( hwnd, hdc, end_paint );
663 static BOOL loaderdrv_ScrollDC( HDC hdc, INT dx, INT dy, const RECT *scroll, const RECT *clip,
664 HRGN hrgn, LPRECT update )
666 return load_driver()->pScrollDC( hdc, dx, dy, scroll, clip, hrgn, update );
669 static void loaderdrv_SetFocus( HWND hwnd )
671 load_driver()->pSetFocus( hwnd );
674 static HWND loaderdrv_SetParent( HWND hwnd, HWND parent )
676 return load_driver()->pSetParent( hwnd, parent );
679 static BOOL loaderdrv_SetWindowPos( WINDOWPOS *pos )
681 return load_driver()->pSetWindowPos( pos );
684 static int loaderdrv_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
686 return load_driver()->pSetWindowRgn( hwnd, hrgn, redraw );
689 static void loaderdrv_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
691 load_driver()->pSetWindowIcon( hwnd, type, icon );
694 static void loaderdrv_SetWindowStyle( HWND hwnd, DWORD old_style )
696 load_driver()->pSetWindowStyle( hwnd, old_style );
699 static void loaderdrv_SetWindowText( HWND hwnd, LPCWSTR text )
701 load_driver()->pSetWindowText( hwnd, text );
704 static BOOL loaderdrv_ShowWindow( HWND hwnd, INT cmd )
706 return load_driver()->pShowWindow( hwnd, cmd );
709 static void loaderdrv_SysCommandSizeMove( HWND hwnd, WPARAM wparam )
711 load_driver()->pSysCommandSizeMove( hwnd, wparam );
714 static HWND loaderdrv_WindowFromDC( HDC hdc )
716 return load_driver()->pWindowFromDC( hdc );
719 static LRESULT loaderdrv_WindowMessage( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
721 return load_driver()->pWindowMessage( hwnd, msg, wparam, lparam );
724 static const USER_DRIVER lazy_load_driver =
726 /* keyboard functions */
727 loaderdrv_ActivateKeyboardLayout,
729 loaderdrv_GetAsyncKeyState,
730 loaderdrv_GetKeyNameText,
731 loaderdrv_GetKeyboardLayout,
732 loaderdrv_GetKeyboardLayoutList,
733 loaderdrv_GetKeyboardLayoutName,
734 loaderdrv_LoadKeyboardLayout,
735 loaderdrv_MapVirtualKeyEx,
737 loaderdrv_ToUnicodeEx,
738 loaderdrv_UnloadKeyboardLayout,
739 loaderdrv_VkKeyScanEx,
740 /* mouse functions */
742 loaderdrv_GetCursorPos,
743 loaderdrv_SetCursorPos,
744 /* screen saver functions */
745 loaderdrv_GetScreenSaveActive,
746 loaderdrv_SetScreenSaveActive,
747 /* clipboard functions */
748 loaderdrv_AcquireClipboard,
749 loaderdrv_CountClipboardFormats,
750 loaderdrv_EmptyClipboard,
751 loaderdrv_EndClipboardUpdate,
752 loaderdrv_EnumClipboardFormats,
753 loaderdrv_GetClipboardData,
754 loaderdrv_GetClipboardFormatName,
755 loaderdrv_IsClipboardFormatAvailable,
756 loaderdrv_RegisterClipboardFormat,
757 loaderdrv_ResetSelectionOwner,
758 loaderdrv_SetClipboardData,
760 loaderdrv_ChangeDisplaySettingsEx,
761 loaderdrv_EnumDisplaySettingsEx,
762 /* windowing functions */
763 loaderdrv_CreateDesktopWindow,
764 loaderdrv_CreateWindow,
765 loaderdrv_DestroyWindow,
767 loaderdrv_MsgWaitForMultipleObjectsEx,
772 loaderdrv_SetWindowPos,
773 loaderdrv_SetWindowRgn,
774 loaderdrv_SetWindowIcon,
775 loaderdrv_SetWindowStyle,
776 loaderdrv_SetWindowText,
777 loaderdrv_ShowWindow,
778 loaderdrv_SysCommandSizeMove,
779 loaderdrv_WindowFromDC,
780 loaderdrv_WindowMessage