mshtml: Implement onprogress for XMLHttpRequest.
[wine.git] / dlls / wineandroid.drv / init.c
blobf06eb6471c5dbaea05f06539456b52e27894f470
1 /*
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
21 #define NONAMELESSSTRUCT
22 #define NONAMELESSUNION
23 #include "config.h"
25 #include <stdarg.h>
26 #include <string.h>
27 #include <dlfcn.h>
28 #include <link.h>
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winreg.h"
35 #include "android.h"
36 #include "wine/server.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(android);
41 unsigned int screen_width = 0;
42 unsigned int screen_height = 0;
43 RECT virtual_screen_rect = { 0, 0, 0, 0 };
45 static const unsigned int screen_bpp = 32; /* we don't support other modes */
47 static RECT monitor_rc_work;
48 static int device_init_done;
49 static BOOL force_display_devices_refresh;
51 typedef struct
53 struct gdi_physdev dev;
54 } ANDROID_PDEVICE;
56 static const struct user_driver_funcs android_drv_funcs;
59 /******************************************************************************
60 * init_monitors
62 void init_monitors( int width, int height )
64 static const WCHAR trayW[] = {'S','h','e','l','l','_','T','r','a','y','W','n','d',0};
65 UNICODE_STRING name;
66 RECT rect;
67 HWND hwnd;
69 RtlInitUnicodeString( &name, trayW );
70 hwnd = NtUserFindWindowEx( 0, 0, &name, NULL, 0 );
72 virtual_screen_rect.right = width;
73 virtual_screen_rect.bottom = height;
74 monitor_rc_work = virtual_screen_rect;
76 if (!hwnd || !NtUserIsWindowVisible( hwnd )) return;
77 if (!NtUserGetWindowRect( hwnd, &rect )) return;
78 if (rect.top) monitor_rc_work.bottom = rect.top;
79 else monitor_rc_work.top = rect.bottom;
80 TRACE( "found tray %p %s work area %s\n", hwnd,
81 wine_dbgstr_rect( &rect ), wine_dbgstr_rect( &monitor_rc_work ));
83 if (*p_java_vm) /* if we're notified from Java thread, update registry */
85 UINT32 num_path, num_mode;
86 force_display_devices_refresh = TRUE;
87 /* trigger refresh in win32u */
88 NtUserGetDisplayConfigBufferSizes( QDC_ONLY_ACTIVE_PATHS, &num_path, &num_mode );
93 /* wrapper for NtCreateKey that creates the key recursively if necessary */
94 static HKEY reg_create_key( const WCHAR *name, ULONG name_len )
96 UNICODE_STRING nameW = { name_len, name_len, (WCHAR *)name };
97 OBJECT_ATTRIBUTES attr;
98 NTSTATUS status;
99 HANDLE ret;
101 attr.Length = sizeof(attr);
102 attr.RootDirectory = 0;
103 attr.ObjectName = &nameW;
104 attr.Attributes = 0;
105 attr.SecurityDescriptor = NULL;
106 attr.SecurityQualityOfService = NULL;
108 status = NtCreateKey( &ret, MAXIMUM_ALLOWED, &attr, 0, NULL, 0, NULL );
109 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
111 static const WCHAR registry_rootW[] = { '\\','R','e','g','i','s','t','r','y','\\' };
112 DWORD pos = 0, i = 0, len = name_len / sizeof(WCHAR);
114 /* don't try to create registry root */
115 if (len > ARRAY_SIZE(registry_rootW) &&
116 !memcmp( name, registry_rootW, sizeof(registry_rootW) ))
117 i += ARRAY_SIZE(registry_rootW);
119 while (i < len && name[i] != '\\') i++;
120 if (i == len) return 0;
121 for (;;)
123 nameW.Buffer = (WCHAR *)name + pos;
124 nameW.Length = (i - pos) * sizeof(WCHAR);
125 status = NtCreateKey( &ret, MAXIMUM_ALLOWED, &attr, 0, NULL, 0, NULL );
127 if (attr.RootDirectory) NtClose( attr.RootDirectory );
128 if (status) return 0;
129 if (i == len) break;
130 attr.RootDirectory = ret;
131 while (i < len && name[i] == '\\') i++;
132 pos = i;
133 while (i < len && name[i] != '\\') i++;
136 return ret;
140 /******************************************************************************
141 * set_screen_dpi
143 void set_screen_dpi( DWORD dpi )
145 static const WCHAR dpi_value_name[] = {'L','o','g','P','i','x','e','l','s',0};
146 static const WCHAR dpi_key_name[] =
148 '\\','R','e','g','i','s','t','r','y',
149 '\\','M','a','c','h','i','n','e',
150 '\\','S','y','s','t','e','m',
151 '\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t',
152 '\\','H','a','r','d','w','a','r','e',' ','P','r','o','f','i','l','e','s',
153 '\\','C','u','r','r','e','n','t',
154 '\\','S','o','f','t','w','a','r','e',
155 '\\','F','o','n','t','s'
157 HKEY hkey;
159 if ((hkey = reg_create_key( dpi_key_name, sizeof(dpi_key_name ))))
161 UNICODE_STRING name;
162 RtlInitUnicodeString( &name, dpi_value_name );
163 NtSetValueKey( hkey, &name, 0, REG_DWORD, &dpi, sizeof(dpi) );
164 NtClose( hkey );
168 /**********************************************************************
169 * fetch_display_metrics
171 static void fetch_display_metrics(void)
173 if (*p_java_vm) return; /* for Java threads it will be set when the top view is created */
175 SERVER_START_REQ( get_window_rectangles )
177 req->handle = wine_server_user_handle( NtUserGetDesktopWindow() );
178 req->relative = COORDS_CLIENT;
179 if (!wine_server_call( req ))
181 screen_width = reply->window.right;
182 screen_height = reply->window.bottom;
185 SERVER_END_REQ;
187 init_monitors( screen_width, screen_height );
188 TRACE( "screen %ux%u\n", screen_width, screen_height );
192 /**********************************************************************
193 * device_init
195 * Perform initializations needed upon creation of the first device.
197 static void device_init(void)
199 device_init_done = TRUE;
200 fetch_display_metrics();
204 /******************************************************************************
205 * create_android_physdev
207 static ANDROID_PDEVICE *create_android_physdev(void)
209 ANDROID_PDEVICE *physdev;
211 if (!device_init_done) device_init();
213 if (!(physdev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*physdev) ))) return NULL;
214 return physdev;
217 /**********************************************************************
218 * ANDROID_CreateDC
220 static BOOL CDECL ANDROID_CreateDC( PHYSDEV *pdev, LPCWSTR device, LPCWSTR output,
221 const DEVMODEW *initData )
223 ANDROID_PDEVICE *physdev = create_android_physdev();
225 if (!physdev) return FALSE;
227 push_dc_driver( pdev, &physdev->dev, &android_drv_funcs.dc_funcs );
228 return TRUE;
232 /**********************************************************************
233 * ANDROID_CreateCompatibleDC
235 static BOOL CDECL ANDROID_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
237 ANDROID_PDEVICE *physdev = create_android_physdev();
239 if (!physdev) return FALSE;
241 push_dc_driver( pdev, &physdev->dev, &android_drv_funcs.dc_funcs );
242 return TRUE;
246 /**********************************************************************
247 * ANDROID_DeleteDC
249 static BOOL CDECL ANDROID_DeleteDC( PHYSDEV dev )
251 HeapFree( GetProcessHeap(), 0, dev );
252 return TRUE;
256 /***********************************************************************
257 * ANDROID_ChangeDisplaySettingsEx
259 LONG ANDROID_ChangeDisplaySettingsEx( LPCWSTR devname, LPDEVMODEW devmode,
260 HWND hwnd, DWORD flags, LPVOID lpvoid )
262 FIXME( "(%s,%p,%p,0x%08x,%p)\n", debugstr_w( devname ), devmode, hwnd, flags, lpvoid );
263 return DISP_CHANGE_SUCCESSFUL;
267 /***********************************************************************
268 * ANDROID_UpdateDisplayDevices
270 void ANDROID_UpdateDisplayDevices( const struct gdi_device_manager *device_manager,
271 BOOL force, void *param )
273 if (force || force_display_devices_refresh)
275 struct gdi_monitor gdi_monitor =
277 .rc_monitor = virtual_screen_rect,
278 .rc_work = monitor_rc_work,
279 .state_flags = DISPLAY_DEVICE_ACTIVE | DISPLAY_DEVICE_ATTACHED,
281 device_manager->add_monitor( &gdi_monitor, param );
282 force_display_devices_refresh = FALSE;
287 /***********************************************************************
288 * ANDROID_EnumDisplaySettingsEx
290 BOOL ANDROID_EnumDisplaySettingsEx( LPCWSTR name, DWORD n, LPDEVMODEW devmode, DWORD flags )
292 static const WCHAR dev_name[CCHDEVICENAME] =
293 { 'W','i','n','e',' ','A','n','d','r','o','i','d',' ','d','r','i','v','e','r',0 };
295 devmode->dmSize = offsetof( DEVMODEW, dmICMMethod );
296 devmode->dmSpecVersion = DM_SPECVERSION;
297 devmode->dmDriverVersion = DM_SPECVERSION;
298 memcpy( devmode->dmDeviceName, dev_name, sizeof(dev_name) );
299 devmode->dmDriverExtra = 0;
300 devmode->u2.dmDisplayFlags = 0;
301 devmode->dmDisplayFrequency = 0;
302 devmode->u1.s2.dmPosition.x = 0;
303 devmode->u1.s2.dmPosition.y = 0;
304 devmode->u1.s2.dmDisplayOrientation = 0;
305 devmode->u1.s2.dmDisplayFixedOutput = 0;
307 if (n == ENUM_CURRENT_SETTINGS || n == ENUM_REGISTRY_SETTINGS) n = 0;
308 if (n == 0)
310 devmode->dmPelsWidth = screen_width;
311 devmode->dmPelsHeight = screen_height;
312 devmode->dmBitsPerPel = screen_bpp;
313 devmode->dmDisplayFrequency = 60;
314 devmode->dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFLAGS | DM_DISPLAYFREQUENCY;
315 TRACE( "mode %d -- %dx%d %d bpp @%d Hz\n", n,
316 devmode->dmPelsWidth, devmode->dmPelsHeight,
317 devmode->dmBitsPerPel, devmode->dmDisplayFrequency );
318 return TRUE;
320 TRACE( "mode %d -- not present\n", n );
321 SetLastError( ERROR_NO_MORE_FILES );
322 return FALSE;
326 /**********************************************************************
327 * ANDROID_wine_get_wgl_driver
329 static struct opengl_funcs *ANDROID_wine_get_wgl_driver( UINT version )
331 return get_wgl_driver( version );
335 static const struct user_driver_funcs android_drv_funcs =
337 .dc_funcs.pCreateCompatibleDC = ANDROID_CreateCompatibleDC,
338 .dc_funcs.pCreateDC = ANDROID_CreateDC,
339 .dc_funcs.pDeleteDC = ANDROID_DeleteDC,
340 .dc_funcs.priority = GDI_PRIORITY_GRAPHICS_DRV,
342 .pGetKeyNameText = ANDROID_GetKeyNameText,
343 .pMapVirtualKeyEx = ANDROID_MapVirtualKeyEx,
344 .pVkKeyScanEx = ANDROID_VkKeyScanEx,
345 .pSetCursor = ANDROID_SetCursor,
346 .pChangeDisplaySettingsEx = ANDROID_ChangeDisplaySettingsEx,
347 .pEnumDisplaySettingsEx = ANDROID_EnumDisplaySettingsEx,
348 .pUpdateDisplayDevices = ANDROID_UpdateDisplayDevices,
349 .pCreateWindow = ANDROID_CreateWindow,
350 .pDesktopWindowProc = ANDROID_DesktopWindowProc,
351 .pDestroyWindow = ANDROID_DestroyWindow,
352 .pMsgWaitForMultipleObjectsEx = ANDROID_MsgWaitForMultipleObjectsEx,
353 .pSetCapture = ANDROID_SetCapture,
354 .pSetLayeredWindowAttributes = ANDROID_SetLayeredWindowAttributes,
355 .pSetParent = ANDROID_SetParent,
356 .pSetWindowRgn = ANDROID_SetWindowRgn,
357 .pSetWindowStyle = ANDROID_SetWindowStyle,
358 .pShowWindow = ANDROID_ShowWindow,
359 .pUpdateLayeredWindow = ANDROID_UpdateLayeredWindow,
360 .pWindowMessage = ANDROID_WindowMessage,
361 .pWindowPosChanging = ANDROID_WindowPosChanging,
362 .pWindowPosChanged = ANDROID_WindowPosChanged,
363 .pwine_get_wgl_driver = ANDROID_wine_get_wgl_driver,
367 static const JNINativeMethod methods[] =
369 { "wine_desktop_changed", "(II)V", desktop_changed },
370 { "wine_config_changed", "(I)V", config_changed },
371 { "wine_surface_changed", "(ILandroid/view/Surface;Z)V", surface_changed },
372 { "wine_motion_event", "(IIIIII)Z", motion_event },
373 { "wine_keyboard_event", "(IIII)Z", keyboard_event },
376 #define DECL_FUNCPTR(f) typeof(f) * p##f = NULL
377 #define LOAD_FUNCPTR(lib, func) do { \
378 if ((p##func = dlsym( lib, #func )) == NULL) \
379 { ERR( "can't find symbol %s\n", #func); return; } \
380 } while(0)
382 DECL_FUNCPTR( __android_log_print );
383 DECL_FUNCPTR( ANativeWindow_fromSurface );
384 DECL_FUNCPTR( ANativeWindow_release );
385 DECL_FUNCPTR( hw_get_module );
387 #ifndef DT_GNU_HASH
388 #define DT_GNU_HASH 0x6ffffef5
389 #endif
391 static unsigned int gnu_hash( const char *name )
393 unsigned int h = 5381;
394 while (*name) h = h * 33 + (unsigned char)*name++;
395 return h;
398 static unsigned int hash_symbol( const char *name )
400 unsigned int hi, hash = 0;
401 while (*name)
403 hash = (hash << 4) + (unsigned char)*name++;
404 hi = hash & 0xf0000000;
405 hash ^= hi;
406 hash ^= hi >> 24;
408 return hash;
411 static void *find_symbol( const struct dl_phdr_info* info, const char *var, int type )
413 const ElfW(Dyn) *dyn = NULL;
414 const ElfW(Phdr) *ph;
415 const ElfW(Sym) *symtab = NULL;
416 const Elf32_Word *hashtab = NULL;
417 const Elf32_Word *gnu_hashtab = NULL;
418 const char *strings = NULL;
419 Elf32_Word idx;
421 for (ph = info->dlpi_phdr; ph < &info->dlpi_phdr[info->dlpi_phnum]; ++ph)
423 if (PT_DYNAMIC == ph->p_type)
425 dyn = (const ElfW(Dyn) *)(info->dlpi_addr + ph->p_vaddr);
426 break;
429 if (!dyn) return NULL;
431 while (dyn->d_tag)
433 if (dyn->d_tag == DT_STRTAB)
434 strings = (const char*)(info->dlpi_addr + dyn->d_un.d_ptr);
435 if (dyn->d_tag == DT_SYMTAB)
436 symtab = (const ElfW(Sym) *)(info->dlpi_addr + dyn->d_un.d_ptr);
437 if (dyn->d_tag == DT_HASH)
438 hashtab = (const Elf32_Word *)(info->dlpi_addr + dyn->d_un.d_ptr);
439 if (dyn->d_tag == DT_GNU_HASH)
440 gnu_hashtab = (const Elf32_Word *)(info->dlpi_addr + dyn->d_un.d_ptr);
441 dyn++;
444 if (!symtab || !strings) return NULL;
446 if (gnu_hashtab) /* new style hash table */
448 const unsigned int hash = gnu_hash(var);
449 const Elf32_Word nbuckets = gnu_hashtab[0];
450 const Elf32_Word symbias = gnu_hashtab[1];
451 const Elf32_Word nwords = gnu_hashtab[2];
452 const ElfW(Addr) *bitmask = (const ElfW(Addr) *)(gnu_hashtab + 4);
453 const Elf32_Word *buckets = (const Elf32_Word *)(bitmask + nwords);
454 const Elf32_Word *chains = buckets + nbuckets - symbias;
456 if (!(idx = buckets[hash % nbuckets])) return NULL;
459 if ((chains[idx] & ~1u) == (hash & ~1u) &&
460 ELF32_ST_BIND(symtab[idx].st_info) == STB_GLOBAL &&
461 ELF32_ST_TYPE(symtab[idx].st_info) == type &&
462 !strcmp( strings + symtab[idx].st_name, var ))
463 return (void *)(info->dlpi_addr + symtab[idx].st_value);
464 } while (!(chains[idx++] & 1u));
466 else if (hashtab) /* old style hash table */
468 const unsigned int hash = hash_symbol( var );
469 const Elf32_Word nbuckets = hashtab[0];
470 const Elf32_Word *buckets = hashtab + 2;
471 const Elf32_Word *chains = buckets + nbuckets;
473 for (idx = buckets[hash % nbuckets]; idx; idx = chains[idx])
475 if (ELF32_ST_BIND(symtab[idx].st_info) == STB_GLOBAL &&
476 ELF32_ST_TYPE(symtab[idx].st_info) == type &&
477 !strcmp( strings + symtab[idx].st_name, var ))
478 return (void *)(info->dlpi_addr + symtab[idx].st_value);
481 return NULL;
484 static int enum_libs( struct dl_phdr_info* info, size_t size, void* data )
486 const char *p;
488 if (!info->dlpi_name) return 0;
489 if (!(p = strrchr( info->dlpi_name, '/' ))) return 0;
490 if (strcmp( p, "/libhardware.so" )) return 0;
491 TRACE( "found libhardware at %p\n", info->dlpi_phdr );
492 phw_get_module = find_symbol( info, "hw_get_module", STT_FUNC );
493 return 1;
496 static void load_hardware_libs(void)
498 const struct hw_module_t *module;
499 int ret;
500 void *libhardware;
502 if ((libhardware = dlopen( "libhardware.so", RTLD_GLOBAL )))
504 LOAD_FUNCPTR( libhardware, hw_get_module );
506 else
508 /* Android >= N disallows loading libhardware, so we load libandroid (which imports
509 * libhardware), and then we can find libhardware in the list of loaded libraries.
511 if (!dlopen( "libandroid.so", RTLD_GLOBAL ))
513 ERR( "failed to load libandroid.so: %s\n", dlerror() );
514 return;
516 dl_iterate_phdr( enum_libs, 0 );
517 if (!phw_get_module)
519 ERR( "failed to find hw_get_module\n" );
520 return;
524 if ((ret = phw_get_module( GRALLOC_HARDWARE_MODULE_ID, &module )))
526 ERR( "failed to load gralloc module err %d\n", ret );
527 return;
530 init_gralloc( module );
533 static void load_android_libs(void)
535 void *libandroid, *liblog;
537 if (!(libandroid = dlopen( "libandroid.so", RTLD_GLOBAL )))
539 ERR( "failed to load libandroid.so: %s\n", dlerror() );
540 return;
542 if (!(liblog = dlopen( "liblog.so", RTLD_GLOBAL )))
544 ERR( "failed to load liblog.so: %s\n", dlerror() );
545 return;
547 LOAD_FUNCPTR( liblog, __android_log_print );
548 LOAD_FUNCPTR( libandroid, ANativeWindow_fromSurface );
549 LOAD_FUNCPTR( libandroid, ANativeWindow_release );
552 #undef DECL_FUNCPTR
553 #undef LOAD_FUNCPTR
555 JavaVM **p_java_vm = NULL;
556 jobject *p_java_object = NULL;
557 unsigned short *p_java_gdt_sel = NULL;
559 static BOOL process_attach(void)
561 jclass class;
562 jobject object;
563 JNIEnv *jni_env;
564 JavaVM *java_vm;
565 void *ntdll;
567 if (!(ntdll = dlopen( "ntdll.so", RTLD_NOW ))) return FALSE;
569 p_java_vm = dlsym( ntdll, "java_vm" );
570 p_java_object = dlsym( ntdll, "java_object" );
571 p_java_gdt_sel = dlsym( ntdll, "java_gdt_sel" );
573 object = *p_java_object;
575 load_hardware_libs();
577 if ((java_vm = *p_java_vm)) /* running under Java */
579 #ifdef __i386__
580 WORD old_fs;
581 __asm__( "mov %%fs,%0" : "=r" (old_fs) );
582 #endif
583 load_android_libs();
584 (*java_vm)->AttachCurrentThread( java_vm, &jni_env, 0 );
585 class = (*jni_env)->GetObjectClass( jni_env, object );
586 (*jni_env)->RegisterNatives( jni_env, class, methods, ARRAY_SIZE( methods ));
587 (*jni_env)->DeleteLocalRef( jni_env, class );
588 #ifdef __i386__
589 /* the Java VM hijacks %fs for its own purposes, restore it */
590 __asm__( "mov %0,%%fs" :: "r" (old_fs) );
591 #endif
593 __wine_set_user_driver( &android_drv_funcs, WINE_GDI_DRIVER_VERSION );
594 return TRUE;
597 /***********************************************************************
598 * dll initialisation routine
600 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
602 switch (reason)
604 case DLL_PROCESS_ATTACH:
605 DisableThreadLibraryCalls( inst );
606 return process_attach();
608 return TRUE;