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 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 *driver = null_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(ClipCursor);
92 GET_USER_FUNC(GetScreenSaveActive);
93 GET_USER_FUNC(SetScreenSaveActive);
94 GET_USER_FUNC(AcquireClipboard);
95 GET_USER_FUNC(EmptyClipboard);
96 GET_USER_FUNC(SetClipboardData);
97 GET_USER_FUNC(GetClipboardData);
98 GET_USER_FUNC(CountClipboardFormats);
99 GET_USER_FUNC(EnumClipboardFormats);
100 GET_USER_FUNC(IsClipboardFormatAvailable);
101 GET_USER_FUNC(RegisterClipboardFormat);
102 GET_USER_FUNC(GetClipboardFormatName);
103 GET_USER_FUNC(EndClipboardUpdate);
104 GET_USER_FUNC(ChangeDisplaySettingsEx);
105 GET_USER_FUNC(EnumDisplayMonitors);
106 GET_USER_FUNC(EnumDisplaySettingsEx);
107 GET_USER_FUNC(GetMonitorInfo);
108 GET_USER_FUNC(CreateDesktopWindow);
109 GET_USER_FUNC(CreateWindow);
110 GET_USER_FUNC(DestroyWindow);
111 GET_USER_FUNC(GetDC);
112 GET_USER_FUNC(MsgWaitForMultipleObjectsEx);
113 GET_USER_FUNC(ReleaseDC);
114 GET_USER_FUNC(ScrollDC);
115 GET_USER_FUNC(SetCapture);
116 GET_USER_FUNC(SetFocus);
117 GET_USER_FUNC(SetParent);
118 GET_USER_FUNC(SetWindowPos);
119 GET_USER_FUNC(SetWindowRgn);
120 GET_USER_FUNC(SetWindowIcon);
121 GET_USER_FUNC(SetWindowStyle);
122 GET_USER_FUNC(SetWindowText);
123 GET_USER_FUNC(SysCommand);
124 GET_USER_FUNC(WindowMessage);
128 prev = InterlockedCompareExchangePointer( (void **)&USER_Driver, driver, &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_ClipCursor( LPCRECT clip )
237 static BOOL nulldrv_GetScreenSaveActive(void)
242 static void nulldrv_SetScreenSaveActive( BOOL on )
246 static INT nulldrv_AcquireClipboard( HWND hwnd )
251 static BOOL nulldrv_CountClipboardFormats(void)
256 static void nulldrv_EmptyClipboard( BOOL keepunowned )
260 static void nulldrv_EndClipboardUpdate(void)
264 static UINT nulldrv_EnumClipboardFormats( UINT format )
269 static BOOL nulldrv_GetClipboardData( UINT format, HANDLE16 *h16, HANDLE *h32 )
274 static INT nulldrv_GetClipboardFormatName( UINT format, LPWSTR buffer, UINT len )
279 static BOOL nulldrv_IsClipboardFormatAvailable( UINT format )
284 static UINT nulldrv_RegisterClipboardFormat( LPCWSTR name )
289 static BOOL nulldrv_SetClipboardData( UINT format, HANDLE16 h16, HANDLE h32, BOOL owner )
294 static LONG nulldrv_ChangeDisplaySettingsEx( LPCWSTR name, LPDEVMODEW mode, HWND hwnd,
295 DWORD flags, LPVOID lparam )
297 return DISP_CHANGE_FAILED;
300 static BOOL nulldrv_EnumDisplayMonitors( HDC hdc, LPRECT rect, MONITORENUMPROC proc, LPARAM lp )
305 static BOOL nulldrv_EnumDisplaySettingsEx( LPCWSTR name, DWORD num, LPDEVMODEW mode, DWORD flags )
310 static BOOL nulldrv_GetMonitorInfo( HMONITOR handle, LPMONITORINFO info )
315 static BOOL nulldrv_CreateDesktopWindow( HWND hwnd )
320 static BOOL nulldrv_CreateWindow( HWND hwnd )
326 MESSAGE( "Application tried to create a window, but no driver could be loaded.\n");
327 switch (driver_load_error)
329 case ERROR_MOD_NOT_FOUND:
330 MESSAGE( "The X11 driver is missing. Check your build!\n" );
332 case ERROR_DLL_INIT_FAILED:
333 MESSAGE( "Make sure that your X server is running and that $DISPLAY is set correctly.\n" );
336 MESSAGE( "Unknown error (%d).\n", driver_load_error );
342 static void nulldrv_DestroyWindow( HWND hwnd )
346 static void nulldrv_GetDC( HDC hdc, HWND hwnd, HWND top_win, const RECT *win_rect,
347 const RECT *top_rect, DWORD flags )
351 static DWORD nulldrv_MsgWaitForMultipleObjectsEx( DWORD count, const HANDLE *handles, DWORD timeout,
352 DWORD mask, DWORD flags )
354 return WaitForMultipleObjectsEx( count, handles, flags & MWMO_WAITALL,
355 timeout, flags & MWMO_ALERTABLE );
358 static void nulldrv_ReleaseDC( HWND hwnd, HDC hdc )
362 static BOOL nulldrv_ScrollDC( HDC hdc, INT dx, INT dy, const RECT *scroll, const RECT *clip,
363 HRGN hrgn, LPRECT update )
368 static void nulldrv_SetCapture( HWND hwnd, UINT flags )
372 static void nulldrv_SetFocus( HWND hwnd )
376 static void nulldrv_SetParent( HWND hwnd, HWND parent, HWND old_parent )
380 static void nulldrv_SetWindowPos( HWND hwnd, HWND insert_after, UINT swp_flags,
381 const RECT *window_rect, const RECT *client_rect,
382 const RECT *visible_rect, const RECT *valid_rects )
386 static int nulldrv_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
391 static void nulldrv_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
395 static void nulldrv_SetWindowStyle( HWND hwnd, DWORD old_style )
399 static void nulldrv_SetWindowText( HWND hwnd, LPCWSTR text )
403 static LRESULT nulldrv_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
408 static LRESULT nulldrv_WindowMessage( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
413 static USER_DRIVER null_driver =
415 /* keyboard functions */
416 nulldrv_ActivateKeyboardLayout,
418 nulldrv_GetAsyncKeyState,
419 nulldrv_GetKeyNameText,
420 nulldrv_GetKeyboardLayout,
421 nulldrv_GetKeyboardLayoutList,
422 nulldrv_GetKeyboardLayoutName,
423 nulldrv_LoadKeyboardLayout,
424 nulldrv_MapVirtualKeyEx,
427 nulldrv_UnloadKeyboardLayout,
429 /* mouse functions */
431 nulldrv_GetCursorPos,
432 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,
464 nulldrv_SetWindowPos,
465 nulldrv_SetWindowRgn,
466 nulldrv_SetWindowIcon,
467 nulldrv_SetWindowStyle,
468 nulldrv_SetWindowText,
470 nulldrv_WindowMessage
474 /**********************************************************************
475 * Lazy loading user driver
477 * Initial driver used before another driver is loaded.
478 * Each entry point simply loads the real driver and chains to it.
481 static HKL loaderdrv_ActivateKeyboardLayout( HKL layout, UINT flags )
483 return load_driver()->pActivateKeyboardLayout( layout, flags );
486 static void loaderdrv_Beep(void)
488 load_driver()->pBeep();
491 static SHORT loaderdrv_GetAsyncKeyState( INT key )
493 return load_driver()->pGetAsyncKeyState( key );
496 static INT loaderdrv_GetKeyNameText( LONG lparam, LPWSTR buffer, INT size )
498 return load_driver()->pGetKeyNameText( lparam, buffer, size );
501 static HKL loaderdrv_GetKeyboardLayout( DWORD layout )
503 return load_driver()->pGetKeyboardLayout( layout );
506 static UINT loaderdrv_GetKeyboardLayoutList( INT count, HKL *layouts )
508 return load_driver()->pGetKeyboardLayoutList( count, layouts );
511 static BOOL loaderdrv_GetKeyboardLayoutName( LPWSTR name )
513 return load_driver()->pGetKeyboardLayoutName( name );
516 static HKL loaderdrv_LoadKeyboardLayout( LPCWSTR name, UINT flags )
518 return load_driver()->pLoadKeyboardLayout( name, flags );
521 static UINT loaderdrv_MapVirtualKeyEx( UINT code, UINT type, HKL layout )
523 return load_driver()->pMapVirtualKeyEx( code, type, layout );
526 static UINT loaderdrv_SendInput( UINT count, LPINPUT inputs, int size )
528 return load_driver()->pSendInput( count, inputs, size );
531 static INT loaderdrv_ToUnicodeEx( UINT virt, UINT scan, LPBYTE state, LPWSTR str,
532 int size, UINT flags, HKL layout )
534 return load_driver()->pToUnicodeEx( virt, scan, state, str, size, flags, layout );
537 static BOOL loaderdrv_UnloadKeyboardLayout( HKL layout )
539 return load_driver()->pUnloadKeyboardLayout( layout );
542 static SHORT loaderdrv_VkKeyScanEx( WCHAR ch, HKL layout )
544 return load_driver()->pVkKeyScanEx( ch, layout );
547 static void loaderdrv_SetCursor( struct tagCURSORICONINFO *info )
549 load_driver()->pSetCursor( info );
552 static BOOL loaderdrv_GetCursorPos( LPPOINT pt )
554 return load_driver()->pGetCursorPos( pt );
557 static BOOL loaderdrv_SetCursorPos( INT x, INT y )
559 return load_driver()->pSetCursorPos( x, y );
562 static BOOL loaderdrv_ClipCursor( LPCRECT clip )
564 return load_driver()->pClipCursor( clip );
567 static BOOL loaderdrv_GetScreenSaveActive(void)
569 return load_driver()->pGetScreenSaveActive();
572 static void loaderdrv_SetScreenSaveActive( BOOL on )
574 load_driver()->pSetScreenSaveActive( on );
577 static INT loaderdrv_AcquireClipboard( HWND hwnd )
579 return load_driver()->pAcquireClipboard( hwnd );
582 static BOOL loaderdrv_CountClipboardFormats(void)
584 return load_driver()->pCountClipboardFormats();
587 static void loaderdrv_EmptyClipboard( BOOL keepunowned )
589 load_driver()->pEmptyClipboard( keepunowned );
592 static void loaderdrv_EndClipboardUpdate(void)
594 load_driver()->pEndClipboardUpdate();
597 static UINT loaderdrv_EnumClipboardFormats( UINT format )
599 return load_driver()->pEnumClipboardFormats( format );
602 static BOOL loaderdrv_GetClipboardData( UINT format, HANDLE16 *h16, HANDLE *h32 )
604 return load_driver()->pGetClipboardData( format, h16, h32 );
607 static INT loaderdrv_GetClipboardFormatName( UINT format, LPWSTR buffer, UINT len )
609 return load_driver()->pGetClipboardFormatName( format, buffer, len );
612 static BOOL loaderdrv_IsClipboardFormatAvailable( UINT format )
614 return load_driver()->pIsClipboardFormatAvailable( format );
617 static UINT loaderdrv_RegisterClipboardFormat( LPCWSTR name )
619 return load_driver()->pRegisterClipboardFormat( name );
622 static BOOL loaderdrv_SetClipboardData( UINT format, HANDLE16 h16, HANDLE h32, BOOL owner )
624 return load_driver()->pSetClipboardData( format, h16, h32, owner );
627 static LONG loaderdrv_ChangeDisplaySettingsEx( LPCWSTR name, LPDEVMODEW mode, HWND hwnd,
628 DWORD flags, LPVOID lparam )
630 return load_driver()->pChangeDisplaySettingsEx( name, mode, hwnd, flags, lparam );
633 static BOOL loaderdrv_EnumDisplayMonitors( HDC hdc, LPRECT rect, MONITORENUMPROC proc, LPARAM lp )
635 return load_driver()->pEnumDisplayMonitors( hdc, rect, proc, lp );
638 static BOOL loaderdrv_EnumDisplaySettingsEx( LPCWSTR name, DWORD num, LPDEVMODEW mode, DWORD flags )
640 return load_driver()->pEnumDisplaySettingsEx( name, num, mode, flags );
643 static BOOL loaderdrv_GetMonitorInfo( HMONITOR handle, LPMONITORINFO info )
645 return load_driver()->pGetMonitorInfo( handle, info );
648 static BOOL loaderdrv_CreateDesktopWindow( HWND hwnd )
650 return load_driver()->pCreateDesktopWindow( hwnd );
653 static BOOL loaderdrv_CreateWindow( HWND hwnd )
655 return load_driver()->pCreateWindow( hwnd );
658 static void loaderdrv_DestroyWindow( HWND hwnd )
660 load_driver()->pDestroyWindow( hwnd );
663 static void loaderdrv_GetDC( HDC hdc, HWND hwnd, HWND top_win, const RECT *win_rect,
664 const RECT *top_rect, DWORD flags )
666 load_driver()->pGetDC( hdc, hwnd, top_win, win_rect, top_rect, flags );
669 static DWORD loaderdrv_MsgWaitForMultipleObjectsEx( DWORD count, const HANDLE *handles, DWORD timeout,
670 DWORD mask, DWORD flags )
672 return load_driver()->pMsgWaitForMultipleObjectsEx( count, handles, timeout, mask, flags );
675 static void loaderdrv_ReleaseDC( HWND hwnd, HDC hdc )
677 load_driver()->pReleaseDC( hwnd, hdc );
680 static BOOL loaderdrv_ScrollDC( HDC hdc, INT dx, INT dy, const RECT *scroll, const RECT *clip,
681 HRGN hrgn, LPRECT update )
683 return load_driver()->pScrollDC( hdc, dx, dy, scroll, clip, hrgn, update );
686 static void loaderdrv_SetCapture( HWND hwnd, UINT flags )
688 load_driver()->pSetCapture( hwnd, flags );
691 static void loaderdrv_SetFocus( HWND hwnd )
693 load_driver()->pSetFocus( hwnd );
696 static void loaderdrv_SetParent( HWND hwnd, HWND parent, HWND old_parent )
698 load_driver()->pSetParent( hwnd, parent, old_parent );
701 static void loaderdrv_SetWindowPos( HWND hwnd, HWND insert_after, UINT swp_flags,
702 const RECT *window_rect, const RECT *client_rect,
703 const RECT *visible_rect, const RECT *valid_rects )
705 load_driver()->pSetWindowPos( hwnd, insert_after, swp_flags, window_rect,
706 client_rect, visible_rect, valid_rects );
709 static int loaderdrv_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
711 return load_driver()->pSetWindowRgn( hwnd, hrgn, redraw );
714 static void loaderdrv_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
716 load_driver()->pSetWindowIcon( hwnd, type, icon );
719 static void loaderdrv_SetWindowStyle( HWND hwnd, DWORD old_style )
721 load_driver()->pSetWindowStyle( hwnd, old_style );
724 static void loaderdrv_SetWindowText( HWND hwnd, LPCWSTR text )
726 load_driver()->pSetWindowText( hwnd, text );
729 static LRESULT loaderdrv_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
731 return load_driver()->pSysCommand( hwnd, wparam, lparam );
734 static LRESULT loaderdrv_WindowMessage( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
736 return load_driver()->pWindowMessage( hwnd, msg, wparam, lparam );
739 static USER_DRIVER lazy_load_driver =
741 /* keyboard functions */
742 loaderdrv_ActivateKeyboardLayout,
744 loaderdrv_GetAsyncKeyState,
745 loaderdrv_GetKeyNameText,
746 loaderdrv_GetKeyboardLayout,
747 loaderdrv_GetKeyboardLayoutList,
748 loaderdrv_GetKeyboardLayoutName,
749 loaderdrv_LoadKeyboardLayout,
750 loaderdrv_MapVirtualKeyEx,
752 loaderdrv_ToUnicodeEx,
753 loaderdrv_UnloadKeyboardLayout,
754 loaderdrv_VkKeyScanEx,
755 /* mouse functions */
757 loaderdrv_GetCursorPos,
758 loaderdrv_SetCursorPos,
759 loaderdrv_ClipCursor,
760 /* screen saver functions */
761 loaderdrv_GetScreenSaveActive,
762 loaderdrv_SetScreenSaveActive,
763 /* clipboard functions */
764 loaderdrv_AcquireClipboard,
765 loaderdrv_CountClipboardFormats,
766 loaderdrv_EmptyClipboard,
767 loaderdrv_EndClipboardUpdate,
768 loaderdrv_EnumClipboardFormats,
769 loaderdrv_GetClipboardData,
770 loaderdrv_GetClipboardFormatName,
771 loaderdrv_IsClipboardFormatAvailable,
772 loaderdrv_RegisterClipboardFormat,
773 loaderdrv_SetClipboardData,
775 loaderdrv_ChangeDisplaySettingsEx,
776 loaderdrv_EnumDisplayMonitors,
777 loaderdrv_EnumDisplaySettingsEx,
778 loaderdrv_GetMonitorInfo,
779 /* windowing functions */
780 loaderdrv_CreateDesktopWindow,
781 loaderdrv_CreateWindow,
782 loaderdrv_DestroyWindow,
784 loaderdrv_MsgWaitForMultipleObjectsEx,
787 loaderdrv_SetCapture,
790 loaderdrv_SetWindowPos,
791 loaderdrv_SetWindowRgn,
792 loaderdrv_SetWindowIcon,
793 loaderdrv_SetWindowStyle,
794 loaderdrv_SetWindowText,
795 loaderdrv_SysCommand,
796 loaderdrv_WindowMessage