2 * Android driver initialisation functions
4 * Copyright 1996, 2013, 2017 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
33 #define WIN32_NO_STATUS
38 #include "wine/server.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(android
);
43 unsigned int screen_width
= 0;
44 unsigned int screen_height
= 0;
45 RECT virtual_screen_rect
= { 0, 0, 0, 0 };
47 static const unsigned int screen_bpp
= 32; /* we don't support other modes */
49 static RECT monitor_rc_work
;
50 static int device_init_done
;
51 static BOOL force_display_devices_refresh
;
53 PNTAPCFUNC register_window_callback
;
57 struct gdi_physdev dev
;
60 static const struct user_driver_funcs android_drv_funcs
;
63 /******************************************************************************
66 void init_monitors( int width
, int height
)
68 static const WCHAR trayW
[] = {'S','h','e','l','l','_','T','r','a','y','W','n','d',0};
73 RtlInitUnicodeString( &name
, trayW
);
74 hwnd
= NtUserFindWindowEx( 0, 0, &name
, NULL
, 0 );
76 virtual_screen_rect
.right
= width
;
77 virtual_screen_rect
.bottom
= height
;
78 monitor_rc_work
= virtual_screen_rect
;
80 if (!hwnd
|| !NtUserIsWindowVisible( hwnd
)) return;
81 if (!NtUserGetWindowRect( hwnd
, &rect
)) return;
82 if (rect
.top
) monitor_rc_work
.bottom
= rect
.top
;
83 else monitor_rc_work
.top
= rect
.bottom
;
84 TRACE( "found tray %p %s work area %s\n", hwnd
,
85 wine_dbgstr_rect( &rect
), wine_dbgstr_rect( &monitor_rc_work
));
87 if (*p_java_vm
) /* if we're notified from Java thread, update registry */
89 UINT32 num_path
, num_mode
;
90 force_display_devices_refresh
= TRUE
;
91 /* trigger refresh in win32u */
92 NtUserGetDisplayConfigBufferSizes( QDC_ONLY_ACTIVE_PATHS
, &num_path
, &num_mode
);
97 /* wrapper for NtCreateKey that creates the key recursively if necessary */
98 static HKEY
reg_create_key( const WCHAR
*name
, ULONG name_len
)
100 UNICODE_STRING nameW
= { name_len
, name_len
, (WCHAR
*)name
};
101 OBJECT_ATTRIBUTES attr
;
105 attr
.Length
= sizeof(attr
);
106 attr
.RootDirectory
= 0;
107 attr
.ObjectName
= &nameW
;
109 attr
.SecurityDescriptor
= NULL
;
110 attr
.SecurityQualityOfService
= NULL
;
112 status
= NtCreateKey( &ret
, MAXIMUM_ALLOWED
, &attr
, 0, NULL
, 0, NULL
);
113 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
115 static const WCHAR registry_rootW
[] = { '\\','R','e','g','i','s','t','r','y','\\' };
116 DWORD pos
= 0, i
= 0, len
= name_len
/ sizeof(WCHAR
);
118 /* don't try to create registry root */
119 if (len
> ARRAY_SIZE(registry_rootW
) &&
120 !memcmp( name
, registry_rootW
, sizeof(registry_rootW
) ))
121 i
+= ARRAY_SIZE(registry_rootW
);
123 while (i
< len
&& name
[i
] != '\\') i
++;
124 if (i
== len
) return 0;
127 nameW
.Buffer
= (WCHAR
*)name
+ pos
;
128 nameW
.Length
= (i
- pos
) * sizeof(WCHAR
);
129 status
= NtCreateKey( &ret
, MAXIMUM_ALLOWED
, &attr
, 0, NULL
, 0, NULL
);
131 if (attr
.RootDirectory
) NtClose( attr
.RootDirectory
);
132 if (status
) return 0;
134 attr
.RootDirectory
= ret
;
135 while (i
< len
&& name
[i
] == '\\') i
++;
137 while (i
< len
&& name
[i
] != '\\') i
++;
144 /******************************************************************************
147 void set_screen_dpi( DWORD dpi
)
149 static const WCHAR dpi_value_name
[] = {'L','o','g','P','i','x','e','l','s',0};
150 static const WCHAR dpi_key_name
[] =
152 '\\','R','e','g','i','s','t','r','y',
153 '\\','M','a','c','h','i','n','e',
154 '\\','S','y','s','t','e','m',
155 '\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t',
156 '\\','H','a','r','d','w','a','r','e',' ','P','r','o','f','i','l','e','s',
157 '\\','C','u','r','r','e','n','t',
158 '\\','S','o','f','t','w','a','r','e',
159 '\\','F','o','n','t','s'
163 if ((hkey
= reg_create_key( dpi_key_name
, sizeof(dpi_key_name
))))
166 RtlInitUnicodeString( &name
, dpi_value_name
);
167 NtSetValueKey( hkey
, &name
, 0, REG_DWORD
, &dpi
, sizeof(dpi
) );
172 /**********************************************************************
173 * fetch_display_metrics
175 static void fetch_display_metrics(void)
177 if (*p_java_vm
) return; /* for Java threads it will be set when the top view is created */
179 SERVER_START_REQ( get_window_rectangles
)
181 req
->handle
= wine_server_user_handle( NtUserGetDesktopWindow() );
182 req
->relative
= COORDS_CLIENT
;
183 if (!wine_server_call( req
))
185 screen_width
= reply
->window
.right
;
186 screen_height
= reply
->window
.bottom
;
191 init_monitors( screen_width
, screen_height
);
192 TRACE( "screen %ux%u\n", screen_width
, screen_height
);
196 /**********************************************************************
199 * Perform initializations needed upon creation of the first device.
201 static void device_init(void)
203 device_init_done
= TRUE
;
204 fetch_display_metrics();
208 /******************************************************************************
209 * create_android_physdev
211 static ANDROID_PDEVICE
*create_android_physdev(void)
213 ANDROID_PDEVICE
*physdev
;
215 if (!device_init_done
) device_init();
217 if (!(physdev
= calloc( 1, sizeof(*physdev
) ))) return NULL
;
221 /**********************************************************************
224 static BOOL
ANDROID_CreateDC( PHYSDEV
*pdev
, LPCWSTR device
, LPCWSTR output
, const DEVMODEW
*initData
)
226 ANDROID_PDEVICE
*physdev
= create_android_physdev();
228 if (!physdev
) return FALSE
;
230 push_dc_driver( pdev
, &physdev
->dev
, &android_drv_funcs
.dc_funcs
);
235 /**********************************************************************
236 * ANDROID_CreateCompatibleDC
238 static BOOL
ANDROID_CreateCompatibleDC( PHYSDEV orig
, PHYSDEV
*pdev
)
240 ANDROID_PDEVICE
*physdev
= create_android_physdev();
242 if (!physdev
) return FALSE
;
244 push_dc_driver( pdev
, &physdev
->dev
, &android_drv_funcs
.dc_funcs
);
249 /**********************************************************************
252 static BOOL
ANDROID_DeleteDC( PHYSDEV dev
)
259 /***********************************************************************
260 * ANDROID_ChangeDisplaySettings
262 LONG
ANDROID_ChangeDisplaySettings( LPDEVMODEW displays
, LPCWSTR primary_name
, HWND hwnd
, DWORD flags
, LPVOID lpvoid
)
264 FIXME( "(%p,%s,%p,0x%08x,%p)\n", displays
, debugstr_w(primary_name
), hwnd
, (int)flags
, lpvoid
);
265 return DISP_CHANGE_SUCCESSFUL
;
269 /***********************************************************************
270 * ANDROID_UpdateDisplayDevices
272 BOOL
ANDROID_UpdateDisplayDevices( const struct gdi_device_manager
*device_manager
, BOOL force
, void *param
)
274 if (force
|| force_display_devices_refresh
)
276 static const struct gdi_gpu gpu
;
277 static const struct gdi_adapter adapter
=
279 .state_flags
= DISPLAY_DEVICE_ATTACHED_TO_DESKTOP
| DISPLAY_DEVICE_PRIMARY_DEVICE
| DISPLAY_DEVICE_VGA_COMPATIBLE
,
281 struct gdi_monitor gdi_monitor
=
283 .rc_monitor
= virtual_screen_rect
,
284 .rc_work
= monitor_rc_work
,
285 .state_flags
= DISPLAY_DEVICE_ACTIVE
| DISPLAY_DEVICE_ATTACHED
,
287 const DEVMODEW mode
=
289 .dmFields
= DM_DISPLAYORIENTATION
| DM_PELSWIDTH
| DM_PELSHEIGHT
| DM_BITSPERPEL
|
290 DM_DISPLAYFLAGS
| DM_DISPLAYFREQUENCY
| DM_POSITION
,
291 .dmBitsPerPel
= screen_bpp
, .dmPelsWidth
= screen_width
, .dmPelsHeight
= screen_height
, .dmDisplayFrequency
= 60,
293 device_manager
->add_gpu( &gpu
, param
);
294 device_manager
->add_adapter( &adapter
, param
);
295 device_manager
->add_monitor( &gdi_monitor
, param
);
296 device_manager
->add_mode( &mode
, TRUE
, param
);
297 force_display_devices_refresh
= FALSE
;
304 /***********************************************************************
305 * ANDROID_GetCurrentDisplaySettings
307 BOOL
ANDROID_GetCurrentDisplaySettings( LPCWSTR name
, BOOL is_primary
, LPDEVMODEW devmode
)
309 devmode
->dmDisplayFlags
= 0;
310 devmode
->dmPosition
.x
= 0;
311 devmode
->dmPosition
.y
= 0;
312 devmode
->dmDisplayOrientation
= 0;
313 devmode
->dmDisplayFixedOutput
= 0;
314 devmode
->dmPelsWidth
= screen_width
;
315 devmode
->dmPelsHeight
= screen_height
;
316 devmode
->dmBitsPerPel
= screen_bpp
;
317 devmode
->dmDisplayFrequency
= 60;
318 devmode
->dmFields
= DM_POSITION
| DM_DISPLAYORIENTATION
| DM_PELSWIDTH
| DM_PELSHEIGHT
|
319 DM_BITSPERPEL
| DM_DISPLAYFLAGS
| DM_DISPLAYFREQUENCY
;
320 TRACE( "current mode -- %dx%d %d bpp @%d Hz\n",
321 (int)devmode
->dmPelsWidth
, (int)devmode
->dmPelsHeight
,
322 (int)devmode
->dmBitsPerPel
, (int)devmode
->dmDisplayFrequency
);
327 /**********************************************************************
328 * ANDROID_wine_get_wgl_driver
330 static struct opengl_funcs
*ANDROID_wine_get_wgl_driver( UINT version
)
332 return get_wgl_driver( version
);
336 static const struct user_driver_funcs android_drv_funcs
=
338 .dc_funcs
.pCreateCompatibleDC
= ANDROID_CreateCompatibleDC
,
339 .dc_funcs
.pCreateDC
= ANDROID_CreateDC
,
340 .dc_funcs
.pDeleteDC
= ANDROID_DeleteDC
,
341 .dc_funcs
.priority
= GDI_PRIORITY_GRAPHICS_DRV
,
343 .pGetKeyNameText
= ANDROID_GetKeyNameText
,
344 .pMapVirtualKeyEx
= ANDROID_MapVirtualKeyEx
,
345 .pVkKeyScanEx
= ANDROID_VkKeyScanEx
,
346 .pSetCursor
= ANDROID_SetCursor
,
347 .pChangeDisplaySettings
= ANDROID_ChangeDisplaySettings
,
348 .pGetCurrentDisplaySettings
= ANDROID_GetCurrentDisplaySettings
,
349 .pUpdateDisplayDevices
= ANDROID_UpdateDisplayDevices
,
350 .pCreateDesktop
= ANDROID_CreateDesktop
,
351 .pCreateWindow
= ANDROID_CreateWindow
,
352 .pDesktopWindowProc
= ANDROID_DesktopWindowProc
,
353 .pDestroyWindow
= ANDROID_DestroyWindow
,
354 .pProcessEvents
= ANDROID_ProcessEvents
,
355 .pSetCapture
= ANDROID_SetCapture
,
356 .pSetLayeredWindowAttributes
= ANDROID_SetLayeredWindowAttributes
,
357 .pSetParent
= ANDROID_SetParent
,
358 .pSetWindowRgn
= ANDROID_SetWindowRgn
,
359 .pSetWindowStyle
= ANDROID_SetWindowStyle
,
360 .pShowWindow
= ANDROID_ShowWindow
,
361 .pUpdateLayeredWindow
= ANDROID_UpdateLayeredWindow
,
362 .pWindowMessage
= ANDROID_WindowMessage
,
363 .pWindowPosChanging
= ANDROID_WindowPosChanging
,
364 .pWindowPosChanged
= ANDROID_WindowPosChanged
,
365 .pwine_get_wgl_driver
= ANDROID_wine_get_wgl_driver
,
369 static const JNINativeMethod methods
[] =
371 { "wine_desktop_changed", "(II)V", desktop_changed
},
372 { "wine_config_changed", "(I)V", config_changed
},
373 { "wine_surface_changed", "(ILandroid/view/Surface;Z)V", surface_changed
},
374 { "wine_motion_event", "(IIIIII)Z", motion_event
},
375 { "wine_keyboard_event", "(IIII)Z", keyboard_event
},
378 #define DECL_FUNCPTR(f) typeof(f) * p##f = NULL
379 #define LOAD_FUNCPTR(lib, func) do { \
380 if ((p##func = dlsym( lib, #func )) == NULL) \
381 { ERR( "can't find symbol %s\n", #func); return; } \
384 DECL_FUNCPTR( __android_log_print
);
385 DECL_FUNCPTR( ANativeWindow_fromSurface
);
386 DECL_FUNCPTR( ANativeWindow_release
);
387 DECL_FUNCPTR( hw_get_module
);
390 #define DT_GNU_HASH 0x6ffffef5
393 static unsigned int gnu_hash( const char *name
)
395 unsigned int h
= 5381;
396 while (*name
) h
= h
* 33 + (unsigned char)*name
++;
400 static unsigned int hash_symbol( const char *name
)
402 unsigned int hi
, hash
= 0;
405 hash
= (hash
<< 4) + (unsigned char)*name
++;
406 hi
= hash
& 0xf0000000;
413 static void *find_symbol( const struct dl_phdr_info
* info
, const char *var
, int type
)
415 const ElfW(Dyn
) *dyn
= NULL
;
416 const ElfW(Phdr
) *ph
;
417 const ElfW(Sym
) *symtab
= NULL
;
418 const Elf32_Word
*hashtab
= NULL
;
419 const Elf32_Word
*gnu_hashtab
= NULL
;
420 const char *strings
= NULL
;
423 for (ph
= info
->dlpi_phdr
; ph
< &info
->dlpi_phdr
[info
->dlpi_phnum
]; ++ph
)
425 if (PT_DYNAMIC
== ph
->p_type
)
427 dyn
= (const ElfW(Dyn
) *)(info
->dlpi_addr
+ ph
->p_vaddr
);
431 if (!dyn
) return NULL
;
435 if (dyn
->d_tag
== DT_STRTAB
)
436 strings
= (const char*)(info
->dlpi_addr
+ dyn
->d_un
.d_ptr
);
437 if (dyn
->d_tag
== DT_SYMTAB
)
438 symtab
= (const ElfW(Sym
) *)(info
->dlpi_addr
+ dyn
->d_un
.d_ptr
);
439 if (dyn
->d_tag
== DT_HASH
)
440 hashtab
= (const Elf32_Word
*)(info
->dlpi_addr
+ dyn
->d_un
.d_ptr
);
441 if (dyn
->d_tag
== DT_GNU_HASH
)
442 gnu_hashtab
= (const Elf32_Word
*)(info
->dlpi_addr
+ dyn
->d_un
.d_ptr
);
446 if (!symtab
|| !strings
) return NULL
;
448 if (gnu_hashtab
) /* new style hash table */
450 const unsigned int hash
= gnu_hash(var
);
451 const Elf32_Word nbuckets
= gnu_hashtab
[0];
452 const Elf32_Word symbias
= gnu_hashtab
[1];
453 const Elf32_Word nwords
= gnu_hashtab
[2];
454 const ElfW(Addr
) *bitmask
= (const ElfW(Addr
) *)(gnu_hashtab
+ 4);
455 const Elf32_Word
*buckets
= (const Elf32_Word
*)(bitmask
+ nwords
);
456 const Elf32_Word
*chains
= buckets
+ nbuckets
- symbias
;
458 if (!(idx
= buckets
[hash
% nbuckets
])) return NULL
;
461 if ((chains
[idx
] & ~1u) == (hash
& ~1u) &&
462 ELF32_ST_BIND(symtab
[idx
].st_info
) == STB_GLOBAL
&&
463 ELF32_ST_TYPE(symtab
[idx
].st_info
) == type
&&
464 !strcmp( strings
+ symtab
[idx
].st_name
, var
))
465 return (void *)(info
->dlpi_addr
+ symtab
[idx
].st_value
);
466 } while (!(chains
[idx
++] & 1u));
468 else if (hashtab
) /* old style hash table */
470 const unsigned int hash
= hash_symbol( var
);
471 const Elf32_Word nbuckets
= hashtab
[0];
472 const Elf32_Word
*buckets
= hashtab
+ 2;
473 const Elf32_Word
*chains
= buckets
+ nbuckets
;
475 for (idx
= buckets
[hash
% nbuckets
]; idx
; idx
= chains
[idx
])
477 if (ELF32_ST_BIND(symtab
[idx
].st_info
) == STB_GLOBAL
&&
478 ELF32_ST_TYPE(symtab
[idx
].st_info
) == type
&&
479 !strcmp( strings
+ symtab
[idx
].st_name
, var
))
480 return (void *)(info
->dlpi_addr
+ symtab
[idx
].st_value
);
486 static int enum_libs( struct dl_phdr_info
* info
, size_t size
, void* data
)
490 if (!info
->dlpi_name
) return 0;
491 if (!(p
= strrchr( info
->dlpi_name
, '/' ))) return 0;
492 if (strcmp( p
, "/libhardware.so" )) return 0;
493 TRACE( "found libhardware at %p\n", info
->dlpi_phdr
);
494 phw_get_module
= find_symbol( info
, "hw_get_module", STT_FUNC
);
498 static void load_hardware_libs(void)
500 const struct hw_module_t
*module
;
504 if ((libhardware
= dlopen( "libhardware.so", RTLD_GLOBAL
)))
506 LOAD_FUNCPTR( libhardware
, hw_get_module
);
510 /* Android >= N disallows loading libhardware, so we load libandroid (which imports
511 * libhardware), and then we can find libhardware in the list of loaded libraries.
513 if (!dlopen( "libandroid.so", RTLD_GLOBAL
))
515 ERR( "failed to load libandroid.so: %s\n", dlerror() );
518 dl_iterate_phdr( enum_libs
, 0 );
521 ERR( "failed to find hw_get_module\n" );
526 if ((ret
= phw_get_module( GRALLOC_HARDWARE_MODULE_ID
, &module
)))
528 ERR( "failed to load gralloc module err %d\n", ret
);
532 init_gralloc( module
);
535 static void load_android_libs(void)
537 void *libandroid
, *liblog
;
539 if (!(libandroid
= dlopen( "libandroid.so", RTLD_GLOBAL
)))
541 ERR( "failed to load libandroid.so: %s\n", dlerror() );
544 if (!(liblog
= dlopen( "liblog.so", RTLD_GLOBAL
)))
546 ERR( "failed to load liblog.so: %s\n", dlerror() );
549 LOAD_FUNCPTR( liblog
, __android_log_print
);
550 LOAD_FUNCPTR( libandroid
, ANativeWindow_fromSurface
);
551 LOAD_FUNCPTR( libandroid
, ANativeWindow_release
);
557 JavaVM
**p_java_vm
= NULL
;
558 jobject
*p_java_object
= NULL
;
559 unsigned short *p_java_gdt_sel
= NULL
;
561 static HRESULT
android_init( void *arg
)
563 struct init_params
*params
= arg
;
564 pthread_mutexattr_t attr
;
571 if (!(ntdll
= dlopen( "ntdll.so", RTLD_NOW
))) return STATUS_UNSUCCESSFUL
;
573 p_java_vm
= dlsym( ntdll
, "java_vm" );
574 p_java_object
= dlsym( ntdll
, "java_object" );
575 p_java_gdt_sel
= dlsym( ntdll
, "java_gdt_sel" );
577 object
= *p_java_object
;
579 load_hardware_libs();
581 pthread_mutexattr_init( &attr
);
582 pthread_mutexattr_settype( &attr
, PTHREAD_MUTEX_RECURSIVE
);
583 pthread_mutex_init( &drawable_mutex
, &attr
);
584 pthread_mutex_init( &win_data_mutex
, &attr
);
585 pthread_mutexattr_destroy( &attr
);
587 register_window_callback
= params
->register_window_callback
;
589 if ((java_vm
= *p_java_vm
)) /* running under Java */
593 __asm__( "mov %%fs,%0" : "=r" (old_fs
) );
596 (*java_vm
)->AttachCurrentThread( java_vm
, &jni_env
, 0 );
597 class = (*jni_env
)->GetObjectClass( jni_env
, object
);
598 (*jni_env
)->RegisterNatives( jni_env
, class, methods
, ARRAY_SIZE( methods
));
599 (*jni_env
)->DeleteLocalRef( jni_env
, class );
601 /* the Java VM hijacks %fs for its own purposes, restore it */
602 __asm__( "mov %0,%%fs" :: "r" (old_fs
) );
605 __wine_set_user_driver( &android_drv_funcs
, WINE_GDI_DRIVER_VERSION
);
606 return STATUS_SUCCESS
;
609 const unixlib_entry_t __wine_unix_call_funcs
[] =
611 android_dispatch_ioctl
,
615 android_register_window
,
619 C_ASSERT( ARRAYSIZE(__wine_unix_call_funcs
) == unix_funcs_count
);