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
;
52 PNTAPCFUNC register_window_callback
;
56 struct gdi_physdev dev
;
59 static const struct user_driver_funcs android_drv_funcs
;
62 /******************************************************************************
65 void init_monitors( int width
, int height
)
67 static const WCHAR trayW
[] = {'S','h','e','l','l','_','T','r','a','y','W','n','d',0};
72 RtlInitUnicodeString( &name
, trayW
);
73 hwnd
= NtUserFindWindowEx( 0, 0, &name
, NULL
, 0 );
75 virtual_screen_rect
.right
= width
;
76 virtual_screen_rect
.bottom
= height
;
77 monitor_rc_work
= virtual_screen_rect
;
79 if (!hwnd
|| !NtUserIsWindowVisible( hwnd
)) return;
80 if (!NtUserGetWindowRect( hwnd
, &rect
, get_win_monitor_dpi( hwnd
) )) return;
81 if (rect
.top
) monitor_rc_work
.bottom
= rect
.top
;
82 else monitor_rc_work
.top
= rect
.bottom
;
83 TRACE( "found tray %p %s work area %s\n", hwnd
,
84 wine_dbgstr_rect( &rect
), wine_dbgstr_rect( &monitor_rc_work
));
86 /* if we're notified from Java thread, update registry */
87 if (*p_java_vm
) NtUserCallNoParam( NtUserCallNoParam_DisplayModeChanged
);
91 /* wrapper for NtCreateKey that creates the key recursively if necessary */
92 static HKEY
reg_create_key( const WCHAR
*name
, ULONG name_len
)
94 UNICODE_STRING nameW
= { name_len
, name_len
, (WCHAR
*)name
};
95 OBJECT_ATTRIBUTES attr
;
99 attr
.Length
= sizeof(attr
);
100 attr
.RootDirectory
= 0;
101 attr
.ObjectName
= &nameW
;
103 attr
.SecurityDescriptor
= NULL
;
104 attr
.SecurityQualityOfService
= NULL
;
106 status
= NtCreateKey( &ret
, MAXIMUM_ALLOWED
, &attr
, 0, NULL
, 0, NULL
);
107 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
109 static const WCHAR registry_rootW
[] = { '\\','R','e','g','i','s','t','r','y','\\' };
110 DWORD pos
= 0, i
= 0, len
= name_len
/ sizeof(WCHAR
);
112 /* don't try to create registry root */
113 if (len
> ARRAY_SIZE(registry_rootW
) &&
114 !memcmp( name
, registry_rootW
, sizeof(registry_rootW
) ))
115 i
+= ARRAY_SIZE(registry_rootW
);
117 while (i
< len
&& name
[i
] != '\\') i
++;
118 if (i
== len
) return 0;
121 nameW
.Buffer
= (WCHAR
*)name
+ pos
;
122 nameW
.Length
= (i
- pos
) * sizeof(WCHAR
);
123 status
= NtCreateKey( &ret
, MAXIMUM_ALLOWED
, &attr
, 0, NULL
, 0, NULL
);
125 if (attr
.RootDirectory
) NtClose( attr
.RootDirectory
);
126 if (status
) return 0;
128 attr
.RootDirectory
= ret
;
129 while (i
< len
&& name
[i
] == '\\') i
++;
131 while (i
< len
&& name
[i
] != '\\') i
++;
138 /******************************************************************************
141 void set_screen_dpi( DWORD dpi
)
143 static const WCHAR dpi_value_name
[] = {'L','o','g','P','i','x','e','l','s',0};
144 static const WCHAR dpi_key_name
[] =
146 '\\','R','e','g','i','s','t','r','y',
147 '\\','M','a','c','h','i','n','e',
148 '\\','S','y','s','t','e','m',
149 '\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t',
150 '\\','H','a','r','d','w','a','r','e',' ','P','r','o','f','i','l','e','s',
151 '\\','C','u','r','r','e','n','t',
152 '\\','S','o','f','t','w','a','r','e',
153 '\\','F','o','n','t','s'
157 if ((hkey
= reg_create_key( dpi_key_name
, sizeof(dpi_key_name
))))
160 RtlInitUnicodeString( &name
, dpi_value_name
);
161 NtSetValueKey( hkey
, &name
, 0, REG_DWORD
, &dpi
, sizeof(dpi
) );
166 /**********************************************************************
167 * fetch_display_metrics
169 static void fetch_display_metrics(void)
171 if (*p_java_vm
) return; /* for Java threads it will be set when the top view is created */
173 SERVER_START_REQ( get_window_rectangles
)
175 req
->handle
= wine_server_user_handle( NtUserGetDesktopWindow() );
176 req
->relative
= COORDS_CLIENT
;
177 if (!wine_server_call( req
))
179 screen_width
= reply
->window
.right
;
180 screen_height
= reply
->window
.bottom
;
185 init_monitors( screen_width
, screen_height
);
186 TRACE( "screen %ux%u\n", screen_width
, screen_height
);
190 /**********************************************************************
193 * Perform initializations needed upon creation of the first device.
195 static void device_init(void)
197 device_init_done
= TRUE
;
198 fetch_display_metrics();
202 /******************************************************************************
203 * create_android_physdev
205 static ANDROID_PDEVICE
*create_android_physdev(void)
207 ANDROID_PDEVICE
*physdev
;
209 if (!device_init_done
) device_init();
211 if (!(physdev
= calloc( 1, sizeof(*physdev
) ))) return NULL
;
215 /**********************************************************************
218 static BOOL
ANDROID_CreateDC( PHYSDEV
*pdev
, LPCWSTR device
, LPCWSTR output
, const DEVMODEW
*initData
)
220 ANDROID_PDEVICE
*physdev
= create_android_physdev();
222 if (!physdev
) return FALSE
;
224 push_dc_driver( pdev
, &physdev
->dev
, &android_drv_funcs
.dc_funcs
);
229 /**********************************************************************
230 * ANDROID_CreateCompatibleDC
232 static BOOL
ANDROID_CreateCompatibleDC( PHYSDEV orig
, PHYSDEV
*pdev
)
234 ANDROID_PDEVICE
*physdev
= create_android_physdev();
236 if (!physdev
) return FALSE
;
238 push_dc_driver( pdev
, &physdev
->dev
, &android_drv_funcs
.dc_funcs
);
243 /**********************************************************************
246 static BOOL
ANDROID_DeleteDC( PHYSDEV dev
)
253 /***********************************************************************
254 * ANDROID_ChangeDisplaySettings
256 LONG
ANDROID_ChangeDisplaySettings( LPDEVMODEW displays
, LPCWSTR primary_name
, HWND hwnd
, DWORD flags
, LPVOID lpvoid
)
258 FIXME( "(%p,%s,%p,0x%08x,%p)\n", displays
, debugstr_w(primary_name
), hwnd
, (int)flags
, lpvoid
);
259 return DISP_CHANGE_SUCCESSFUL
;
263 /***********************************************************************
264 * ANDROID_UpdateDisplayDevices
266 UINT
ANDROID_UpdateDisplayDevices( const struct gdi_device_manager
*device_manager
, void *param
)
268 static const DWORD source_flags
= DISPLAY_DEVICE_ATTACHED_TO_DESKTOP
| DISPLAY_DEVICE_PRIMARY_DEVICE
| DISPLAY_DEVICE_VGA_COMPATIBLE
;
269 struct pci_id pci_id
= {0};
270 struct gdi_monitor gdi_monitor
=
272 .rc_monitor
= virtual_screen_rect
,
273 .rc_work
= monitor_rc_work
,
275 const DEVMODEW mode
=
277 .dmSize
= sizeof(mode
),
278 .dmFields
= DM_DISPLAYORIENTATION
| DM_PELSWIDTH
| DM_PELSHEIGHT
| DM_BITSPERPEL
|
279 DM_DISPLAYFLAGS
| DM_DISPLAYFREQUENCY
,
280 .dmBitsPerPel
= screen_bpp
, .dmPelsWidth
= screen_width
, .dmPelsHeight
= screen_height
, .dmDisplayFrequency
= 60,
282 DEVMODEW current
= mode
;
284 device_manager
->add_gpu( "Wine GPU", &pci_id
, NULL
, param
);
285 device_manager
->add_source( "Default", source_flags
, param
);
286 device_manager
->add_monitor( &gdi_monitor
, param
);
288 current
.dmFields
|= DM_POSITION
;
289 device_manager
->add_modes( ¤t
, 1, &mode
, param
);
291 return STATUS_SUCCESS
;
295 /***********************************************************************
296 * ANDROID_GetCurrentDisplaySettings
298 BOOL
ANDROID_GetCurrentDisplaySettings( LPCWSTR name
, BOOL is_primary
, LPDEVMODEW devmode
)
300 devmode
->dmDisplayFlags
= 0;
301 devmode
->dmPosition
.x
= 0;
302 devmode
->dmPosition
.y
= 0;
303 devmode
->dmDisplayOrientation
= 0;
304 devmode
->dmDisplayFixedOutput
= 0;
305 devmode
->dmPelsWidth
= screen_width
;
306 devmode
->dmPelsHeight
= screen_height
;
307 devmode
->dmBitsPerPel
= screen_bpp
;
308 devmode
->dmDisplayFrequency
= 60;
309 devmode
->dmFields
= DM_POSITION
| DM_DISPLAYORIENTATION
| DM_PELSWIDTH
| DM_PELSHEIGHT
|
310 DM_BITSPERPEL
| DM_DISPLAYFLAGS
| DM_DISPLAYFREQUENCY
;
311 TRACE( "current mode -- %dx%d %d bpp @%d Hz\n",
312 (int)devmode
->dmPelsWidth
, (int)devmode
->dmPelsHeight
,
313 (int)devmode
->dmBitsPerPel
, (int)devmode
->dmDisplayFrequency
);
318 /**********************************************************************
319 * ANDROID_wine_get_wgl_driver
321 static struct opengl_funcs
*ANDROID_wine_get_wgl_driver( UINT version
)
323 return get_wgl_driver( version
);
327 static const struct user_driver_funcs android_drv_funcs
=
329 .dc_funcs
.pCreateCompatibleDC
= ANDROID_CreateCompatibleDC
,
330 .dc_funcs
.pCreateDC
= ANDROID_CreateDC
,
331 .dc_funcs
.pDeleteDC
= ANDROID_DeleteDC
,
332 .dc_funcs
.priority
= GDI_PRIORITY_GRAPHICS_DRV
,
334 .pGetKeyNameText
= ANDROID_GetKeyNameText
,
335 .pMapVirtualKeyEx
= ANDROID_MapVirtualKeyEx
,
336 .pVkKeyScanEx
= ANDROID_VkKeyScanEx
,
337 .pSetCursor
= ANDROID_SetCursor
,
338 .pChangeDisplaySettings
= ANDROID_ChangeDisplaySettings
,
339 .pGetCurrentDisplaySettings
= ANDROID_GetCurrentDisplaySettings
,
340 .pUpdateDisplayDevices
= ANDROID_UpdateDisplayDevices
,
341 .pCreateDesktop
= ANDROID_CreateDesktop
,
342 .pCreateWindow
= ANDROID_CreateWindow
,
343 .pDesktopWindowProc
= ANDROID_DesktopWindowProc
,
344 .pDestroyWindow
= ANDROID_DestroyWindow
,
345 .pProcessEvents
= ANDROID_ProcessEvents
,
346 .pSetCapture
= ANDROID_SetCapture
,
347 .pSetParent
= ANDROID_SetParent
,
348 .pSetWindowStyle
= ANDROID_SetWindowStyle
,
349 .pShowWindow
= ANDROID_ShowWindow
,
350 .pCreateLayeredWindow
= ANDROID_CreateLayeredWindow
,
351 .pWindowMessage
= ANDROID_WindowMessage
,
352 .pWindowPosChanging
= ANDROID_WindowPosChanging
,
353 .pCreateWindowSurface
= ANDROID_CreateWindowSurface
,
354 .pWindowPosChanged
= ANDROID_WindowPosChanged
,
355 .pwine_get_wgl_driver
= ANDROID_wine_get_wgl_driver
,
359 static const JNINativeMethod methods
[] =
361 { "wine_desktop_changed", "(II)V", desktop_changed
},
362 { "wine_config_changed", "(I)V", config_changed
},
363 { "wine_surface_changed", "(ILandroid/view/Surface;Z)V", surface_changed
},
364 { "wine_motion_event", "(IIIIII)Z", motion_event
},
365 { "wine_keyboard_event", "(IIII)Z", keyboard_event
},
368 #define DECL_FUNCPTR(f) typeof(f) * p##f = NULL
369 #define LOAD_FUNCPTR(lib, func) do { \
370 if ((p##func = dlsym( lib, #func )) == NULL) \
371 { ERR( "can't find symbol %s\n", #func); return; } \
374 DECL_FUNCPTR( __android_log_print
);
375 DECL_FUNCPTR( ANativeWindow_fromSurface
);
376 DECL_FUNCPTR( ANativeWindow_release
);
377 DECL_FUNCPTR( hw_get_module
);
380 #define DT_GNU_HASH 0x6ffffef5
383 static unsigned int gnu_hash( const char *name
)
385 unsigned int h
= 5381;
386 while (*name
) h
= h
* 33 + (unsigned char)*name
++;
390 static unsigned int hash_symbol( const char *name
)
392 unsigned int hi
, hash
= 0;
395 hash
= (hash
<< 4) + (unsigned char)*name
++;
396 hi
= hash
& 0xf0000000;
403 static void *find_symbol( const struct dl_phdr_info
* info
, const char *var
, int type
)
405 const ElfW(Dyn
) *dyn
= NULL
;
406 const ElfW(Phdr
) *ph
;
407 const ElfW(Sym
) *symtab
= NULL
;
408 const Elf32_Word
*hashtab
= NULL
;
409 const Elf32_Word
*gnu_hashtab
= NULL
;
410 const char *strings
= NULL
;
413 for (ph
= info
->dlpi_phdr
; ph
< &info
->dlpi_phdr
[info
->dlpi_phnum
]; ++ph
)
415 if (PT_DYNAMIC
== ph
->p_type
)
417 dyn
= (const ElfW(Dyn
) *)(info
->dlpi_addr
+ ph
->p_vaddr
);
421 if (!dyn
) return NULL
;
425 if (dyn
->d_tag
== DT_STRTAB
)
426 strings
= (const char*)(info
->dlpi_addr
+ dyn
->d_un
.d_ptr
);
427 if (dyn
->d_tag
== DT_SYMTAB
)
428 symtab
= (const ElfW(Sym
) *)(info
->dlpi_addr
+ dyn
->d_un
.d_ptr
);
429 if (dyn
->d_tag
== DT_HASH
)
430 hashtab
= (const Elf32_Word
*)(info
->dlpi_addr
+ dyn
->d_un
.d_ptr
);
431 if (dyn
->d_tag
== DT_GNU_HASH
)
432 gnu_hashtab
= (const Elf32_Word
*)(info
->dlpi_addr
+ dyn
->d_un
.d_ptr
);
436 if (!symtab
|| !strings
) return NULL
;
438 if (gnu_hashtab
) /* new style hash table */
440 const unsigned int hash
= gnu_hash(var
);
441 const Elf32_Word nbuckets
= gnu_hashtab
[0];
442 const Elf32_Word symbias
= gnu_hashtab
[1];
443 const Elf32_Word nwords
= gnu_hashtab
[2];
444 const ElfW(Addr
) *bitmask
= (const ElfW(Addr
) *)(gnu_hashtab
+ 4);
445 const Elf32_Word
*buckets
= (const Elf32_Word
*)(bitmask
+ nwords
);
446 const Elf32_Word
*chains
= buckets
+ nbuckets
- symbias
;
448 if (!(idx
= buckets
[hash
% nbuckets
])) return NULL
;
451 if ((chains
[idx
] & ~1u) == (hash
& ~1u) &&
452 ELF32_ST_BIND(symtab
[idx
].st_info
) == STB_GLOBAL
&&
453 ELF32_ST_TYPE(symtab
[idx
].st_info
) == type
&&
454 !strcmp( strings
+ symtab
[idx
].st_name
, var
))
455 return (void *)(info
->dlpi_addr
+ symtab
[idx
].st_value
);
456 } while (!(chains
[idx
++] & 1u));
458 else if (hashtab
) /* old style hash table */
460 const unsigned int hash
= hash_symbol( var
);
461 const Elf32_Word nbuckets
= hashtab
[0];
462 const Elf32_Word
*buckets
= hashtab
+ 2;
463 const Elf32_Word
*chains
= buckets
+ nbuckets
;
465 for (idx
= buckets
[hash
% nbuckets
]; idx
; idx
= chains
[idx
])
467 if (ELF32_ST_BIND(symtab
[idx
].st_info
) == STB_GLOBAL
&&
468 ELF32_ST_TYPE(symtab
[idx
].st_info
) == type
&&
469 !strcmp( strings
+ symtab
[idx
].st_name
, var
))
470 return (void *)(info
->dlpi_addr
+ symtab
[idx
].st_value
);
476 static int enum_libs( struct dl_phdr_info
* info
, size_t size
, void* data
)
480 if (!info
->dlpi_name
) return 0;
481 if (!(p
= strrchr( info
->dlpi_name
, '/' ))) return 0;
482 if (strcmp( p
, "/libhardware.so" )) return 0;
483 TRACE( "found libhardware at %p\n", info
->dlpi_phdr
);
484 phw_get_module
= find_symbol( info
, "hw_get_module", STT_FUNC
);
488 static void load_hardware_libs(void)
490 const struct hw_module_t
*module
;
494 if ((libhardware
= dlopen( "libhardware.so", RTLD_GLOBAL
)))
496 LOAD_FUNCPTR( libhardware
, hw_get_module
);
500 /* Android >= N disallows loading libhardware, so we load libandroid (which imports
501 * libhardware), and then we can find libhardware in the list of loaded libraries.
503 if (!dlopen( "libandroid.so", RTLD_GLOBAL
))
505 ERR( "failed to load libandroid.so: %s\n", dlerror() );
508 dl_iterate_phdr( enum_libs
, 0 );
511 ERR( "failed to find hw_get_module\n" );
516 if ((ret
= phw_get_module( GRALLOC_HARDWARE_MODULE_ID
, &module
)))
518 ERR( "failed to load gralloc module err %d\n", ret
);
522 init_gralloc( module
);
525 static void load_android_libs(void)
527 void *libandroid
, *liblog
;
529 if (!(libandroid
= dlopen( "libandroid.so", RTLD_GLOBAL
)))
531 ERR( "failed to load libandroid.so: %s\n", dlerror() );
534 if (!(liblog
= dlopen( "liblog.so", RTLD_GLOBAL
)))
536 ERR( "failed to load liblog.so: %s\n", dlerror() );
539 LOAD_FUNCPTR( liblog
, __android_log_print
);
540 LOAD_FUNCPTR( libandroid
, ANativeWindow_fromSurface
);
541 LOAD_FUNCPTR( libandroid
, ANativeWindow_release
);
547 JavaVM
**p_java_vm
= NULL
;
548 jobject
*p_java_object
= NULL
;
549 unsigned short *p_java_gdt_sel
= NULL
;
551 static HRESULT
android_init( void *arg
)
553 struct init_params
*params
= arg
;
554 pthread_mutexattr_t attr
;
561 if (!(ntdll
= dlopen( "ntdll.so", RTLD_NOW
))) return STATUS_UNSUCCESSFUL
;
563 p_java_vm
= dlsym( ntdll
, "java_vm" );
564 p_java_object
= dlsym( ntdll
, "java_object" );
565 p_java_gdt_sel
= dlsym( ntdll
, "java_gdt_sel" );
567 object
= *p_java_object
;
569 load_hardware_libs();
571 pthread_mutexattr_init( &attr
);
572 pthread_mutexattr_settype( &attr
, PTHREAD_MUTEX_RECURSIVE
);
573 pthread_mutex_init( &drawable_mutex
, &attr
);
574 pthread_mutex_init( &win_data_mutex
, &attr
);
575 pthread_mutexattr_destroy( &attr
);
577 register_window_callback
= params
->register_window_callback
;
579 if ((java_vm
= *p_java_vm
)) /* running under Java */
583 __asm__( "mov %%fs,%0" : "=r" (old_fs
) );
586 (*java_vm
)->AttachCurrentThread( java_vm
, &jni_env
, 0 );
587 class = (*jni_env
)->GetObjectClass( jni_env
, object
);
588 (*jni_env
)->RegisterNatives( jni_env
, class, methods
, ARRAY_SIZE( methods
));
589 (*jni_env
)->DeleteLocalRef( jni_env
, class );
591 /* the Java VM hijacks %fs for its own purposes, restore it */
592 __asm__( "mov %0,%%fs" :: "r" (old_fs
) );
595 __wine_set_user_driver( &android_drv_funcs
, WINE_GDI_DRIVER_VERSION
);
596 return STATUS_SUCCESS
;
599 const unixlib_entry_t __wine_unix_call_funcs
[] =
601 android_dispatch_ioctl
,
605 android_register_window
,
609 C_ASSERT( ARRAYSIZE(__wine_unix_call_funcs
) == unix_funcs_count
);