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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wine/debug.h"
28 #include "user_private.h"
30 static const USER_DRIVER null_driver, lazy_load_driver;
32 const USER_DRIVER *USER_Driver = &lazy_load_driver;
33 static DWORD driver_load_error;
35 /* load the graphics driver */
36 static const USER_DRIVER *load_driver(void)
38 char buffer[MAX_PATH], libname[32], *name, *next;
41 HMODULE graphics_driver;
42 USER_DRIVER *driver, *prev;
44 strcpy( buffer, "x11" ); /* default value */
45 /* @@ Wine registry key: HKCU\Software\Wine\Drivers */
46 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Drivers", &hkey ))
48 DWORD type, count = sizeof(buffer);
49 RegQueryValueExA( hkey, "Graphics", 0, &type, (LPBYTE) buffer, &count );
56 next = strchr( name, ',' );
57 if (next) *next++ = 0;
59 snprintf( libname, sizeof(libname), "wine%s.drv", name );
60 if ((graphics_driver = LoadLibraryA( libname )) != 0) break;
65 driver_load_error = GetLastError();
67 driver = HeapAlloc( GetProcessHeap(), 0, sizeof(*driver) );
68 memcpy( driver, &null_driver, sizeof(*driver) );
72 #define GET_USER_FUNC(name) \
73 do { if ((ptr = GetProcAddress( graphics_driver, #name ))) driver->p##name = ptr; } while(0)
75 GET_USER_FUNC(ActivateKeyboardLayout);
77 GET_USER_FUNC(GetAsyncKeyState);
78 GET_USER_FUNC(GetKeyNameText);
79 GET_USER_FUNC(GetKeyboardLayout);
80 GET_USER_FUNC(GetKeyboardLayoutList);
81 GET_USER_FUNC(GetKeyboardLayoutName);
82 GET_USER_FUNC(LoadKeyboardLayout);
83 GET_USER_FUNC(MapVirtualKeyEx);
84 GET_USER_FUNC(SendInput);
85 GET_USER_FUNC(ToUnicodeEx);
86 GET_USER_FUNC(UnloadKeyboardLayout);
87 GET_USER_FUNC(VkKeyScanEx);
88 GET_USER_FUNC(SetCursor);
89 GET_USER_FUNC(GetCursorPos);
90 GET_USER_FUNC(SetCursorPos);
91 GET_USER_FUNC(GetScreenSaveActive);
92 GET_USER_FUNC(SetScreenSaveActive);
93 GET_USER_FUNC(AcquireClipboard);
94 GET_USER_FUNC(EmptyClipboard);
95 GET_USER_FUNC(SetClipboardData);
96 GET_USER_FUNC(GetClipboardData);
97 GET_USER_FUNC(CountClipboardFormats);
98 GET_USER_FUNC(EnumClipboardFormats);
99 GET_USER_FUNC(IsClipboardFormatAvailable);
100 GET_USER_FUNC(RegisterClipboardFormat);
101 GET_USER_FUNC(GetClipboardFormatName);
102 GET_USER_FUNC(EndClipboardUpdate);
103 GET_USER_FUNC(ChangeDisplaySettingsEx);
104 GET_USER_FUNC(EnumDisplayMonitors);
105 GET_USER_FUNC(EnumDisplaySettingsEx);
106 GET_USER_FUNC(GetMonitorInfo);
107 GET_USER_FUNC(CreateDesktopWindow);
108 GET_USER_FUNC(CreateWindow);
109 GET_USER_FUNC(DestroyWindow);
110 GET_USER_FUNC(GetDCEx);
111 GET_USER_FUNC(MsgWaitForMultipleObjectsEx);
112 GET_USER_FUNC(ReleaseDC);
113 GET_USER_FUNC(ScrollDC);
114 GET_USER_FUNC(SetFocus);
115 GET_USER_FUNC(SetParent);
116 GET_USER_FUNC(SetWindowPos);
117 GET_USER_FUNC(SetWindowRgn);
118 GET_USER_FUNC(SetWindowIcon);
119 GET_USER_FUNC(SetWindowStyle);
120 GET_USER_FUNC(SetWindowText);
121 GET_USER_FUNC(ShowWindow);
122 GET_USER_FUNC(SysCommandSizeMove);
123 GET_USER_FUNC(WindowFromDC);
124 GET_USER_FUNC(WindowMessage);
128 prev = InterlockedCompareExchangePointer( (void **)&USER_Driver, driver, (void *)&lazy_load_driver );
129 if (prev != &lazy_load_driver)
131 /* another thread beat us to it */
132 HeapFree( GetProcessHeap(), 0, driver );
133 FreeLibrary( graphics_driver );
139 /* unload the graphics driver on process exit */
140 void USER_unload_driver(void)
142 /* make sure we don't try to call the driver after it has been detached */
143 USER_Driver = &null_driver;
147 /**********************************************************************
150 * These are fallbacks for entry points that are not implemented in the real driver.
153 static HKL nulldrv_ActivateKeyboardLayout( HKL layout, UINT flags )
158 static void nulldrv_Beep(void)
162 static SHORT nulldrv_GetAsyncKeyState( INT key )
167 static INT nulldrv_GetKeyNameText( LONG lparam, LPWSTR buffer, INT size )
172 static HKL nulldrv_GetKeyboardLayout( DWORD layout )
177 static UINT nulldrv_GetKeyboardLayoutList( INT count, HKL *layouts )
182 static BOOL nulldrv_GetKeyboardLayoutName( LPWSTR name )
187 static HKL nulldrv_LoadKeyboardLayout( LPCWSTR name, UINT flags )
192 static UINT nulldrv_MapVirtualKeyEx( UINT code, UINT type, HKL layout )
197 static UINT nulldrv_SendInput( UINT count, LPINPUT inputs, int size )
202 static INT nulldrv_ToUnicodeEx( UINT virt, UINT scan, LPBYTE state, LPWSTR str,
203 int size, UINT flags, HKL layout )
208 static BOOL nulldrv_UnloadKeyboardLayout( HKL layout )
213 static SHORT nulldrv_VkKeyScanEx( WCHAR ch, HKL layout )
218 static void nulldrv_SetCursor( struct tagCURSORICONINFO *info )
222 static BOOL nulldrv_GetCursorPos( LPPOINT pt )
227 static BOOL nulldrv_SetCursorPos( INT x, INT y )
232 static BOOL nulldrv_GetScreenSaveActive(void)
237 static void nulldrv_SetScreenSaveActive( BOOL on )
241 static INT 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 BOOL nulldrv_SetClipboardData( UINT format, HANDLE16 h16, HANDLE h32, BOOL owner )
289 static LONG nulldrv_ChangeDisplaySettingsEx( LPCWSTR name, LPDEVMODEW mode, HWND hwnd,
290 DWORD flags, LPVOID lparam )
292 return DISP_CHANGE_FAILED;
295 static BOOL nulldrv_EnumDisplayMonitors( HDC hdc, LPRECT rect, MONITORENUMPROC proc, LPARAM lp )
300 static BOOL nulldrv_EnumDisplaySettingsEx( LPCWSTR name, DWORD num, LPDEVMODEW mode, DWORD flags )
305 static BOOL nulldrv_GetMonitorInfo( HMONITOR handle, LPMONITORINFO info )
310 static BOOL nulldrv_CreateDesktopWindow( HWND hwnd )
315 static BOOL nulldrv_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
321 MESSAGE( "Application tried to create a window, but no driver could be loaded.\n");
322 switch (driver_load_error)
324 case ERROR_MOD_NOT_FOUND:
325 MESSAGE( "The X11 driver is missing. Check your build!\n" );
327 case ERROR_DLL_INIT_FAILED:
328 MESSAGE( "Make sure that your X server is running and that $DISPLAY is set correctly.\n" );
331 MESSAGE( "Unknown error (%d).\n", driver_load_error );
337 static void nulldrv_DestroyWindow( HWND hwnd )
341 static HDC nulldrv_GetDCEx( HWND hwnd, HRGN hrgn, DWORD flags )
346 static DWORD nulldrv_MsgWaitForMultipleObjectsEx( DWORD count, const HANDLE *handles, DWORD timeout,
347 DWORD mask, DWORD flags )
349 return WaitForMultipleObjectsEx( count, handles, flags & MWMO_WAITALL,
350 timeout, flags & MWMO_ALERTABLE );
353 static INT nulldrv_ReleaseDC( HWND hwnd, HDC hdc, BOOL end_paint )
358 static BOOL nulldrv_ScrollDC( HDC hdc, INT dx, INT dy, const RECT *scroll, const RECT *clip,
359 HRGN hrgn, LPRECT update )
364 static void nulldrv_SetFocus( HWND hwnd )
368 static void nulldrv_SetParent( HWND hwnd, HWND parent, HWND old_parent )
372 static BOOL nulldrv_SetWindowPos( HWND hwnd, HWND insert_after, const RECT *rectWindow,
373 const RECT *rectClient, UINT swp_flags, const RECT *valid_rects )
378 static int nulldrv_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
383 static void nulldrv_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
387 static void nulldrv_SetWindowStyle( HWND hwnd, DWORD old_style )
391 static void nulldrv_SetWindowText( HWND hwnd, LPCWSTR text )
395 static BOOL nulldrv_ShowWindow( HWND hwnd, INT cmd )
400 static void nulldrv_SysCommandSizeMove( HWND hwnd, WPARAM wparam )
404 static HWND nulldrv_WindowFromDC( HDC hdc )
409 static LRESULT nulldrv_WindowMessage( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
414 static const USER_DRIVER null_driver =
416 /* keyboard functions */
417 nulldrv_ActivateKeyboardLayout,
419 nulldrv_GetAsyncKeyState,
420 nulldrv_GetKeyNameText,
421 nulldrv_GetKeyboardLayout,
422 nulldrv_GetKeyboardLayoutList,
423 nulldrv_GetKeyboardLayoutName,
424 nulldrv_LoadKeyboardLayout,
425 nulldrv_MapVirtualKeyEx,
428 nulldrv_UnloadKeyboardLayout,
430 /* mouse functions */
432 nulldrv_GetCursorPos,
433 nulldrv_SetCursorPos,
434 /* screen saver functions */
435 nulldrv_GetScreenSaveActive,
436 nulldrv_SetScreenSaveActive,
437 /* clipboard functions */
438 nulldrv_AcquireClipboard,
439 nulldrv_CountClipboardFormats,
440 nulldrv_EmptyClipboard,
441 nulldrv_EndClipboardUpdate,
442 nulldrv_EnumClipboardFormats,
443 nulldrv_GetClipboardData,
444 nulldrv_GetClipboardFormatName,
445 nulldrv_IsClipboardFormatAvailable,
446 nulldrv_RegisterClipboardFormat,
447 nulldrv_SetClipboardData,
449 nulldrv_ChangeDisplaySettingsEx,
450 nulldrv_EnumDisplayMonitors,
451 nulldrv_EnumDisplaySettingsEx,
452 nulldrv_GetMonitorInfo,
453 /* windowing functions */
454 nulldrv_CreateDesktopWindow,
455 nulldrv_CreateWindow,
456 nulldrv_DestroyWindow,
458 nulldrv_MsgWaitForMultipleObjectsEx,
463 nulldrv_SetWindowPos,
464 nulldrv_SetWindowRgn,
465 nulldrv_SetWindowIcon,
466 nulldrv_SetWindowStyle,
467 nulldrv_SetWindowText,
469 nulldrv_SysCommandSizeMove,
470 nulldrv_WindowFromDC,
471 nulldrv_WindowMessage
475 /**********************************************************************
476 * Lazy loading user driver
478 * Initial driver used before another driver is loaded.
479 * Each entry point simply loads the real driver and chains to it.
482 static HKL loaderdrv_ActivateKeyboardLayout( HKL layout, UINT flags )
484 return load_driver()->pActivateKeyboardLayout( layout, flags );
487 static void loaderdrv_Beep(void)
489 load_driver()->pBeep();
492 static SHORT loaderdrv_GetAsyncKeyState( INT key )
494 return load_driver()->pGetAsyncKeyState( key );
497 static INT loaderdrv_GetKeyNameText( LONG lparam, LPWSTR buffer, INT size )
499 return load_driver()->pGetKeyNameText( lparam, buffer, size );
502 static HKL loaderdrv_GetKeyboardLayout( DWORD layout )
504 return load_driver()->pGetKeyboardLayout( layout );
507 static UINT loaderdrv_GetKeyboardLayoutList( INT count, HKL *layouts )
509 return load_driver()->pGetKeyboardLayoutList( count, layouts );
512 static BOOL loaderdrv_GetKeyboardLayoutName( LPWSTR name )
514 return load_driver()->pGetKeyboardLayoutName( name );
517 static HKL loaderdrv_LoadKeyboardLayout( LPCWSTR name, UINT flags )
519 return load_driver()->pLoadKeyboardLayout( name, flags );
522 static UINT loaderdrv_MapVirtualKeyEx( UINT code, UINT type, HKL layout )
524 return load_driver()->pMapVirtualKeyEx( code, type, layout );
527 static UINT loaderdrv_SendInput( UINT count, LPINPUT inputs, int size )
529 return load_driver()->pSendInput( count, inputs, size );
532 static INT loaderdrv_ToUnicodeEx( UINT virt, UINT scan, LPBYTE state, LPWSTR str,
533 int size, UINT flags, HKL layout )
535 return load_driver()->pToUnicodeEx( virt, scan, state, str, size, flags, layout );
538 static BOOL loaderdrv_UnloadKeyboardLayout( HKL layout )
540 return load_driver()->pUnloadKeyboardLayout( layout );
543 static SHORT loaderdrv_VkKeyScanEx( WCHAR ch, HKL layout )
545 return load_driver()->pVkKeyScanEx( ch, layout );
548 static void loaderdrv_SetCursor( struct tagCURSORICONINFO *info )
550 load_driver()->pSetCursor( info );
553 static BOOL loaderdrv_GetCursorPos( LPPOINT pt )
555 return load_driver()->pGetCursorPos( pt );
558 static BOOL loaderdrv_SetCursorPos( INT x, INT y )
560 return load_driver()->pSetCursorPos( x, y );
563 static BOOL loaderdrv_GetScreenSaveActive(void)
565 return load_driver()->pGetScreenSaveActive();
568 static void loaderdrv_SetScreenSaveActive( BOOL on )
570 load_driver()->pSetScreenSaveActive( on );
573 static INT loaderdrv_AcquireClipboard( HWND hwnd )
575 return load_driver()->pAcquireClipboard( hwnd );
578 static BOOL loaderdrv_CountClipboardFormats(void)
580 return load_driver()->pCountClipboardFormats();
583 static void loaderdrv_EmptyClipboard( BOOL keepunowned )
585 load_driver()->pEmptyClipboard( keepunowned );
588 static void loaderdrv_EndClipboardUpdate(void)
590 load_driver()->pEndClipboardUpdate();
593 static UINT loaderdrv_EnumClipboardFormats( UINT format )
595 return load_driver()->pEnumClipboardFormats( format );
598 static BOOL loaderdrv_GetClipboardData( UINT format, HANDLE16 *h16, HANDLE *h32 )
600 return load_driver()->pGetClipboardData( format, h16, h32 );
603 static INT loaderdrv_GetClipboardFormatName( UINT format, LPWSTR buffer, UINT len )
605 return load_driver()->pGetClipboardFormatName( format, buffer, len );
608 static BOOL loaderdrv_IsClipboardFormatAvailable( UINT format )
610 return load_driver()->pIsClipboardFormatAvailable( format );
613 static UINT loaderdrv_RegisterClipboardFormat( LPCWSTR name )
615 return load_driver()->pRegisterClipboardFormat( name );
618 static BOOL loaderdrv_SetClipboardData( UINT format, HANDLE16 h16, HANDLE h32, BOOL owner )
620 return load_driver()->pSetClipboardData( format, h16, h32, owner );
623 static LONG loaderdrv_ChangeDisplaySettingsEx( LPCWSTR name, LPDEVMODEW mode, HWND hwnd,
624 DWORD flags, LPVOID lparam )
626 return load_driver()->pChangeDisplaySettingsEx( name, mode, hwnd, flags, lparam );
629 static BOOL loaderdrv_EnumDisplayMonitors( HDC hdc, LPRECT rect, MONITORENUMPROC proc, LPARAM lp )
631 return load_driver()->pEnumDisplayMonitors( hdc, rect, proc, lp );
634 static BOOL loaderdrv_EnumDisplaySettingsEx( LPCWSTR name, DWORD num, LPDEVMODEW mode, DWORD flags )
636 return load_driver()->pEnumDisplaySettingsEx( name, num, mode, flags );
639 static BOOL loaderdrv_GetMonitorInfo( HMONITOR handle, LPMONITORINFO info )
641 return load_driver()->pGetMonitorInfo( handle, info );
644 static BOOL loaderdrv_CreateDesktopWindow( HWND hwnd )
646 return load_driver()->pCreateDesktopWindow( hwnd );
649 static BOOL loaderdrv_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
651 return load_driver()->pCreateWindow( hwnd, cs, unicode );
654 static void loaderdrv_DestroyWindow( HWND hwnd )
656 load_driver()->pDestroyWindow( hwnd );
659 static HDC loaderdrv_GetDCEx( HWND hwnd, HRGN hrgn, DWORD flags )
661 return load_driver()->pGetDCEx( hwnd, hrgn, flags );
664 static DWORD loaderdrv_MsgWaitForMultipleObjectsEx( DWORD count, const HANDLE *handles, DWORD timeout,
665 DWORD mask, DWORD flags )
667 return load_driver()->pMsgWaitForMultipleObjectsEx( count, handles, timeout, mask, flags );
670 static INT loaderdrv_ReleaseDC( HWND hwnd, HDC hdc, BOOL end_paint )
672 return load_driver()->pReleaseDC( hwnd, hdc, end_paint );
675 static BOOL loaderdrv_ScrollDC( HDC hdc, INT dx, INT dy, const RECT *scroll, const RECT *clip,
676 HRGN hrgn, LPRECT update )
678 return load_driver()->pScrollDC( hdc, dx, dy, scroll, clip, hrgn, update );
681 static void loaderdrv_SetFocus( HWND hwnd )
683 load_driver()->pSetFocus( hwnd );
686 static void loaderdrv_SetParent( HWND hwnd, HWND parent, HWND old_parent )
688 load_driver()->pSetParent( hwnd, parent, old_parent );
691 static BOOL loaderdrv_SetWindowPos( HWND hwnd, HWND insert_after, const RECT *rectWindow,
692 const RECT *rectClient, UINT swp_flags, const RECT *valid_rects )
694 return load_driver()->pSetWindowPos( hwnd, insert_after, rectWindow, rectClient, swp_flags, valid_rects );
697 static int loaderdrv_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
699 return load_driver()->pSetWindowRgn( hwnd, hrgn, redraw );
702 static void loaderdrv_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
704 load_driver()->pSetWindowIcon( hwnd, type, icon );
707 static void loaderdrv_SetWindowStyle( HWND hwnd, DWORD old_style )
709 load_driver()->pSetWindowStyle( hwnd, old_style );
712 static void loaderdrv_SetWindowText( HWND hwnd, LPCWSTR text )
714 load_driver()->pSetWindowText( hwnd, text );
717 static BOOL loaderdrv_ShowWindow( HWND hwnd, INT cmd )
719 return load_driver()->pShowWindow( hwnd, cmd );
722 static void loaderdrv_SysCommandSizeMove( HWND hwnd, WPARAM wparam )
724 load_driver()->pSysCommandSizeMove( hwnd, wparam );
727 static HWND loaderdrv_WindowFromDC( HDC hdc )
729 return load_driver()->pWindowFromDC( hdc );
732 static LRESULT loaderdrv_WindowMessage( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
734 return load_driver()->pWindowMessage( hwnd, msg, wparam, lparam );
737 static const USER_DRIVER lazy_load_driver =
739 /* keyboard functions */
740 loaderdrv_ActivateKeyboardLayout,
742 loaderdrv_GetAsyncKeyState,
743 loaderdrv_GetKeyNameText,
744 loaderdrv_GetKeyboardLayout,
745 loaderdrv_GetKeyboardLayoutList,
746 loaderdrv_GetKeyboardLayoutName,
747 loaderdrv_LoadKeyboardLayout,
748 loaderdrv_MapVirtualKeyEx,
750 loaderdrv_ToUnicodeEx,
751 loaderdrv_UnloadKeyboardLayout,
752 loaderdrv_VkKeyScanEx,
753 /* mouse functions */
755 loaderdrv_GetCursorPos,
756 loaderdrv_SetCursorPos,
757 /* screen saver functions */
758 loaderdrv_GetScreenSaveActive,
759 loaderdrv_SetScreenSaveActive,
760 /* clipboard functions */
761 loaderdrv_AcquireClipboard,
762 loaderdrv_CountClipboardFormats,
763 loaderdrv_EmptyClipboard,
764 loaderdrv_EndClipboardUpdate,
765 loaderdrv_EnumClipboardFormats,
766 loaderdrv_GetClipboardData,
767 loaderdrv_GetClipboardFormatName,
768 loaderdrv_IsClipboardFormatAvailable,
769 loaderdrv_RegisterClipboardFormat,
770 loaderdrv_SetClipboardData,
772 loaderdrv_ChangeDisplaySettingsEx,
773 loaderdrv_EnumDisplayMonitors,
774 loaderdrv_EnumDisplaySettingsEx,
775 loaderdrv_GetMonitorInfo,
776 /* windowing functions */
777 loaderdrv_CreateDesktopWindow,
778 loaderdrv_CreateWindow,
779 loaderdrv_DestroyWindow,
781 loaderdrv_MsgWaitForMultipleObjectsEx,
786 loaderdrv_SetWindowPos,
787 loaderdrv_SetWindowRgn,
788 loaderdrv_SetWindowIcon,
789 loaderdrv_SetWindowStyle,
790 loaderdrv_SetWindowText,
791 loaderdrv_ShowWindow,
792 loaderdrv_SysCommandSizeMove,
793 loaderdrv_WindowFromDC,
794 loaderdrv_WindowMessage