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
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(ChangeDisplaySettingsEx);
106 GET_USER_FUNC(EnumDisplaySettingsEx);
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_EnumDisplaySettingsEx( LPCWSTR name, DWORD num, LPDEVMODEW mode, DWORD flags )
300 static BOOL nulldrv_CreateDesktopWindow( HWND hwnd )
305 static BOOL nulldrv_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
311 MESSAGE( "Application tried to create a window, but no driver could be loaded.\n");
312 switch (driver_load_error)
314 case ERROR_MOD_NOT_FOUND:
315 MESSAGE( "The X11 driver is missing. Check your build!\n" );
317 case ERROR_DLL_INIT_FAILED:
318 MESSAGE( "Make sure that your X server is running and that $DISPLAY is set correctly.\n" );
321 MESSAGE( "Unknown error (%ld).\n", driver_load_error );
327 static void nulldrv_DestroyWindow( HWND hwnd )
331 static HDC nulldrv_GetDCEx( HWND hwnd, HRGN hrgn, DWORD flags )
336 static DWORD nulldrv_MsgWaitForMultipleObjectsEx( DWORD count, const HANDLE *handles, DWORD timeout,
337 DWORD mask, DWORD flags )
339 return WaitForMultipleObjectsEx( count, handles, flags & MWMO_WAITALL,
340 timeout, flags & MWMO_ALERTABLE );
343 static INT nulldrv_ReleaseDC( HWND hwnd, HDC hdc, BOOL end_paint )
348 static BOOL nulldrv_ScrollDC( HDC hdc, INT dx, INT dy, const RECT *scroll, const RECT *clip,
349 HRGN hrgn, LPRECT update )
354 static void nulldrv_SetFocus( HWND hwnd )
358 static HWND nulldrv_SetParent( HWND hwnd, HWND parent )
363 static BOOL nulldrv_SetWindowPos( WINDOWPOS *pos )
368 static int nulldrv_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
373 static void nulldrv_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
377 static void nulldrv_SetWindowStyle( HWND hwnd, DWORD old_style )
381 static void nulldrv_SetWindowText( HWND hwnd, LPCWSTR text )
385 static BOOL nulldrv_ShowWindow( HWND hwnd, INT cmd )
390 static void nulldrv_SysCommandSizeMove( HWND hwnd, WPARAM wparam )
394 static HWND nulldrv_WindowFromDC( HDC hdc )
399 static LRESULT nulldrv_WindowMessage( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
404 static const USER_DRIVER null_driver =
406 /* keyboard functions */
407 nulldrv_ActivateKeyboardLayout,
409 nulldrv_GetAsyncKeyState,
410 nulldrv_GetKeyNameText,
411 nulldrv_GetKeyboardLayout,
412 nulldrv_GetKeyboardLayoutList,
413 nulldrv_GetKeyboardLayoutName,
414 nulldrv_LoadKeyboardLayout,
415 nulldrv_MapVirtualKeyEx,
418 nulldrv_UnloadKeyboardLayout,
420 /* mouse functions */
422 nulldrv_GetCursorPos,
423 nulldrv_SetCursorPos,
424 /* screen saver functions */
425 nulldrv_GetScreenSaveActive,
426 nulldrv_SetScreenSaveActive,
427 /* clipboard functions */
428 nulldrv_AcquireClipboard,
429 nulldrv_CountClipboardFormats,
430 nulldrv_EmptyClipboard,
431 nulldrv_EndClipboardUpdate,
432 nulldrv_EnumClipboardFormats,
433 nulldrv_GetClipboardData,
434 nulldrv_GetClipboardFormatName,
435 nulldrv_IsClipboardFormatAvailable,
436 nulldrv_RegisterClipboardFormat,
437 nulldrv_SetClipboardData,
439 nulldrv_ChangeDisplaySettingsEx,
440 nulldrv_EnumDisplaySettingsEx,
441 /* windowing functions */
442 nulldrv_CreateDesktopWindow,
443 nulldrv_CreateWindow,
444 nulldrv_DestroyWindow,
446 nulldrv_MsgWaitForMultipleObjectsEx,
451 nulldrv_SetWindowPos,
452 nulldrv_SetWindowRgn,
453 nulldrv_SetWindowIcon,
454 nulldrv_SetWindowStyle,
455 nulldrv_SetWindowText,
457 nulldrv_SysCommandSizeMove,
458 nulldrv_WindowFromDC,
459 nulldrv_WindowMessage
463 /**********************************************************************
464 * Lazy loading user driver
466 * Initial driver used before another driver is loaded.
467 * Each entry point simply loads the real driver and chains to it.
470 static HKL loaderdrv_ActivateKeyboardLayout( HKL layout, UINT flags )
472 return load_driver()->pActivateKeyboardLayout( layout, flags );
475 static void loaderdrv_Beep(void)
477 load_driver()->pBeep();
480 static SHORT loaderdrv_GetAsyncKeyState( INT key )
482 return load_driver()->pGetAsyncKeyState( key );
485 static INT loaderdrv_GetKeyNameText( LONG lparam, LPWSTR buffer, INT size )
487 return load_driver()->pGetKeyNameText( lparam, buffer, size );
490 static HKL loaderdrv_GetKeyboardLayout( DWORD layout )
492 return load_driver()->pGetKeyboardLayout( layout );
495 static UINT loaderdrv_GetKeyboardLayoutList( INT count, HKL *layouts )
497 return load_driver()->pGetKeyboardLayoutList( count, layouts );
500 static BOOL loaderdrv_GetKeyboardLayoutName( LPWSTR name )
502 return load_driver()->pGetKeyboardLayoutName( name );
505 static HKL loaderdrv_LoadKeyboardLayout( LPCWSTR name, UINT flags )
507 return load_driver()->pLoadKeyboardLayout( name, flags );
510 static UINT loaderdrv_MapVirtualKeyEx( UINT code, UINT type, HKL layout )
512 return load_driver()->pMapVirtualKeyEx( code, type, layout );
515 static UINT loaderdrv_SendInput( UINT count, LPINPUT inputs, int size )
517 return load_driver()->pSendInput( count, inputs, size );
520 static INT loaderdrv_ToUnicodeEx( UINT virt, UINT scan, LPBYTE state, LPWSTR str,
521 int size, UINT flags, HKL layout )
523 return load_driver()->pToUnicodeEx( virt, scan, state, str, size, flags, layout );
526 static BOOL loaderdrv_UnloadKeyboardLayout( HKL layout )
528 return load_driver()->pUnloadKeyboardLayout( layout );
531 static SHORT loaderdrv_VkKeyScanEx( WCHAR ch, HKL layout )
533 return load_driver()->pVkKeyScanEx( ch, layout );
536 static void loaderdrv_SetCursor( struct tagCURSORICONINFO *info )
538 load_driver()->pSetCursor( info );
541 static BOOL loaderdrv_GetCursorPos( LPPOINT pt )
543 return load_driver()->pGetCursorPos( pt );
546 static BOOL loaderdrv_SetCursorPos( INT x, INT y )
548 return load_driver()->pSetCursorPos( x, y );
551 static BOOL loaderdrv_GetScreenSaveActive(void)
553 return load_driver()->pGetScreenSaveActive();
556 static void loaderdrv_SetScreenSaveActive( BOOL on )
558 load_driver()->pSetScreenSaveActive( on );
561 static INT loaderdrv_AcquireClipboard( HWND hwnd )
563 return load_driver()->pAcquireClipboard( hwnd );
566 static BOOL loaderdrv_CountClipboardFormats(void)
568 return load_driver()->pCountClipboardFormats();
571 static void loaderdrv_EmptyClipboard( BOOL keepunowned )
573 load_driver()->pEmptyClipboard( keepunowned );
576 static void loaderdrv_EndClipboardUpdate(void)
578 load_driver()->pEndClipboardUpdate();
581 static UINT loaderdrv_EnumClipboardFormats( UINT format )
583 return load_driver()->pEnumClipboardFormats( format );
586 static BOOL loaderdrv_GetClipboardData( UINT format, HANDLE16 *h16, HANDLE *h32 )
588 return load_driver()->pGetClipboardData( format, h16, h32 );
591 static INT loaderdrv_GetClipboardFormatName( UINT format, LPWSTR buffer, UINT len )
593 return load_driver()->pGetClipboardFormatName( format, buffer, len );
596 static BOOL loaderdrv_IsClipboardFormatAvailable( UINT format )
598 return load_driver()->pIsClipboardFormatAvailable( format );
601 static UINT loaderdrv_RegisterClipboardFormat( LPCWSTR name )
603 return load_driver()->pRegisterClipboardFormat( name );
606 static BOOL loaderdrv_SetClipboardData( UINT format, HANDLE16 h16, HANDLE h32, BOOL owner )
608 return load_driver()->pSetClipboardData( format, h16, h32, owner );
611 static LONG loaderdrv_ChangeDisplaySettingsEx( LPCWSTR name, LPDEVMODEW mode, HWND hwnd,
612 DWORD flags, LPVOID lparam )
614 return load_driver()->pChangeDisplaySettingsEx( name, mode, hwnd, flags, lparam );
617 static BOOL loaderdrv_EnumDisplaySettingsEx( LPCWSTR name, DWORD num, LPDEVMODEW mode, DWORD flags )
619 return load_driver()->pEnumDisplaySettingsEx( name, num, mode, flags );
622 static BOOL loaderdrv_CreateDesktopWindow( HWND hwnd )
624 return load_driver()->pCreateDesktopWindow( hwnd );
627 static BOOL loaderdrv_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
629 return load_driver()->pCreateWindow( hwnd, cs, unicode );
632 static void loaderdrv_DestroyWindow( HWND hwnd )
634 load_driver()->pDestroyWindow( hwnd );
637 static HDC loaderdrv_GetDCEx( HWND hwnd, HRGN hrgn, DWORD flags )
639 return load_driver()->pGetDCEx( hwnd, hrgn, flags );
642 static DWORD loaderdrv_MsgWaitForMultipleObjectsEx( DWORD count, const HANDLE *handles, DWORD timeout,
643 DWORD mask, DWORD flags )
645 return load_driver()->pMsgWaitForMultipleObjectsEx( count, handles, timeout, mask, flags );
648 static INT loaderdrv_ReleaseDC( HWND hwnd, HDC hdc, BOOL end_paint )
650 return load_driver()->pReleaseDC( hwnd, hdc, end_paint );
653 static BOOL loaderdrv_ScrollDC( HDC hdc, INT dx, INT dy, const RECT *scroll, const RECT *clip,
654 HRGN hrgn, LPRECT update )
656 return load_driver()->pScrollDC( hdc, dx, dy, scroll, clip, hrgn, update );
659 static void loaderdrv_SetFocus( HWND hwnd )
661 load_driver()->pSetFocus( hwnd );
664 static HWND loaderdrv_SetParent( HWND hwnd, HWND parent )
666 return load_driver()->pSetParent( hwnd, parent );
669 static BOOL loaderdrv_SetWindowPos( WINDOWPOS *pos )
671 return load_driver()->pSetWindowPos( pos );
674 static int loaderdrv_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
676 return load_driver()->pSetWindowRgn( hwnd, hrgn, redraw );
679 static void loaderdrv_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
681 load_driver()->pSetWindowIcon( hwnd, type, icon );
684 static void loaderdrv_SetWindowStyle( HWND hwnd, DWORD old_style )
686 load_driver()->pSetWindowStyle( hwnd, old_style );
689 static void loaderdrv_SetWindowText( HWND hwnd, LPCWSTR text )
691 load_driver()->pSetWindowText( hwnd, text );
694 static BOOL loaderdrv_ShowWindow( HWND hwnd, INT cmd )
696 return load_driver()->pShowWindow( hwnd, cmd );
699 static void loaderdrv_SysCommandSizeMove( HWND hwnd, WPARAM wparam )
701 load_driver()->pSysCommandSizeMove( hwnd, wparam );
704 static HWND loaderdrv_WindowFromDC( HDC hdc )
706 return load_driver()->pWindowFromDC( hdc );
709 static LRESULT loaderdrv_WindowMessage( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
711 return load_driver()->pWindowMessage( hwnd, msg, wparam, lparam );
714 static const USER_DRIVER lazy_load_driver =
716 /* keyboard functions */
717 loaderdrv_ActivateKeyboardLayout,
719 loaderdrv_GetAsyncKeyState,
720 loaderdrv_GetKeyNameText,
721 loaderdrv_GetKeyboardLayout,
722 loaderdrv_GetKeyboardLayoutList,
723 loaderdrv_GetKeyboardLayoutName,
724 loaderdrv_LoadKeyboardLayout,
725 loaderdrv_MapVirtualKeyEx,
727 loaderdrv_ToUnicodeEx,
728 loaderdrv_UnloadKeyboardLayout,
729 loaderdrv_VkKeyScanEx,
730 /* mouse functions */
732 loaderdrv_GetCursorPos,
733 loaderdrv_SetCursorPos,
734 /* screen saver functions */
735 loaderdrv_GetScreenSaveActive,
736 loaderdrv_SetScreenSaveActive,
737 /* clipboard functions */
738 loaderdrv_AcquireClipboard,
739 loaderdrv_CountClipboardFormats,
740 loaderdrv_EmptyClipboard,
741 loaderdrv_EndClipboardUpdate,
742 loaderdrv_EnumClipboardFormats,
743 loaderdrv_GetClipboardData,
744 loaderdrv_GetClipboardFormatName,
745 loaderdrv_IsClipboardFormatAvailable,
746 loaderdrv_RegisterClipboardFormat,
747 loaderdrv_SetClipboardData,
749 loaderdrv_ChangeDisplaySettingsEx,
750 loaderdrv_EnumDisplaySettingsEx,
751 /* windowing functions */
752 loaderdrv_CreateDesktopWindow,
753 loaderdrv_CreateWindow,
754 loaderdrv_DestroyWindow,
756 loaderdrv_MsgWaitForMultipleObjectsEx,
761 loaderdrv_SetWindowPos,
762 loaderdrv_SetWindowRgn,
763 loaderdrv_SetWindowIcon,
764 loaderdrv_SetWindowStyle,
765 loaderdrv_SetWindowText,
766 loaderdrv_ShowWindow,
767 loaderdrv_SysCommandSizeMove,
768 loaderdrv_WindowFromDC,
769 loaderdrv_WindowMessage