netapi32: Convert the Unix library to the __wine_unix_call interface.
[wine.git] / dlls / wineandroid.drv / init.c
blob474c30896776a39efbaf28a9fc1262b4228094af
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"
24 #include "wine/port.h"
26 #include <stdarg.h>
27 #include <string.h>
28 #include <link.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winreg.h"
33 #include "android.h"
34 #include "wine/server.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(android);
39 unsigned int screen_width = 0;
40 unsigned int screen_height = 0;
41 RECT virtual_screen_rect = { 0, 0, 0, 0 };
43 MONITORINFOEXW default_monitor =
45 sizeof(default_monitor), /* cbSize */
46 { 0, 0, 0, 0 }, /* rcMonitor */
47 { 0, 0, 0, 0 }, /* rcWork */
48 MONITORINFOF_PRIMARY, /* dwFlags */
49 { '\\','\\','.','\\','D','I','S','P','L','A','Y','1',0 } /* szDevice */
52 static const unsigned int screen_bpp = 32; /* we don't support other modes */
54 static int device_init_done;
56 typedef struct
58 struct gdi_physdev dev;
59 } ANDROID_PDEVICE;
61 static const struct gdi_dc_funcs android_drv_funcs;
64 /******************************************************************************
65 * init_monitors
67 void init_monitors( int width, int height )
69 static const WCHAR trayW[] = {'S','h','e','l','l','_','T','r','a','y','W','n','d',0};
70 RECT rect;
71 HWND hwnd = FindWindowW( trayW, NULL );
73 virtual_screen_rect.right = width;
74 virtual_screen_rect.bottom = height;
75 default_monitor.rcMonitor = default_monitor.rcWork = virtual_screen_rect;
77 if (!hwnd || !IsWindowVisible( hwnd )) return;
78 if (!GetWindowRect( hwnd, &rect )) return;
79 if (rect.top) default_monitor.rcWork.bottom = rect.top;
80 else default_monitor.rcWork.top = rect.bottom;
81 TRACE( "found tray %p %s work area %s\n", hwnd,
82 wine_dbgstr_rect( &rect ), wine_dbgstr_rect( &default_monitor.rcWork ));
86 /******************************************************************************
87 * set_screen_dpi
89 void set_screen_dpi( DWORD dpi )
91 static const WCHAR dpi_key_name[] = {'S','o','f','t','w','a','r','e','\\','F','o','n','t','s',0};
92 static const WCHAR dpi_value_name[] = {'L','o','g','P','i','x','e','l','s',0};
93 HKEY hkey;
95 if (!RegCreateKeyW( HKEY_CURRENT_CONFIG, dpi_key_name, &hkey ))
97 RegSetValueExW( hkey, dpi_value_name, 0, REG_DWORD, (void *)&dpi, sizeof(DWORD) );
98 RegCloseKey( hkey );
102 /**********************************************************************
103 * fetch_display_metrics
105 static void fetch_display_metrics(void)
107 if (*p_java_vm) return; /* for Java threads it will be set when the top view is created */
109 SERVER_START_REQ( get_window_rectangles )
111 req->handle = wine_server_user_handle( GetDesktopWindow() );
112 req->relative = COORDS_CLIENT;
113 if (!wine_server_call( req ))
115 screen_width = reply->window.right;
116 screen_height = reply->window.bottom;
119 SERVER_END_REQ;
121 init_monitors( screen_width, screen_height );
122 TRACE( "screen %ux%u\n", screen_width, screen_height );
126 /**********************************************************************
127 * device_init
129 * Perform initializations needed upon creation of the first device.
131 static void device_init(void)
133 device_init_done = TRUE;
134 fetch_display_metrics();
138 /******************************************************************************
139 * create_android_physdev
141 static ANDROID_PDEVICE *create_android_physdev(void)
143 ANDROID_PDEVICE *physdev;
145 if (!device_init_done) device_init();
147 if (!(physdev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*physdev) ))) return NULL;
148 return physdev;
151 /**********************************************************************
152 * ANDROID_CreateDC
154 static BOOL CDECL ANDROID_CreateDC( PHYSDEV *pdev, LPCWSTR driver, LPCWSTR device,
155 LPCWSTR output, const DEVMODEW* initData )
157 ANDROID_PDEVICE *physdev = create_android_physdev();
159 if (!physdev) return FALSE;
161 push_dc_driver( pdev, &physdev->dev, &android_drv_funcs );
162 return TRUE;
166 /**********************************************************************
167 * ANDROID_CreateCompatibleDC
169 static BOOL CDECL ANDROID_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
171 ANDROID_PDEVICE *physdev = create_android_physdev();
173 if (!physdev) return FALSE;
175 push_dc_driver( pdev, &physdev->dev, &android_drv_funcs );
176 return TRUE;
180 /**********************************************************************
181 * ANDROID_DeleteDC
183 static BOOL CDECL ANDROID_DeleteDC( PHYSDEV dev )
185 HeapFree( GetProcessHeap(), 0, dev );
186 return TRUE;
190 /***********************************************************************
191 * ANDROID_ChangeDisplaySettingsEx
193 LONG CDECL ANDROID_ChangeDisplaySettingsEx( LPCWSTR devname, LPDEVMODEW devmode,
194 HWND hwnd, DWORD flags, LPVOID lpvoid )
196 FIXME( "(%s,%p,%p,0x%08x,%p)\n", debugstr_w( devname ), devmode, hwnd, flags, lpvoid );
197 return DISP_CHANGE_SUCCESSFUL;
201 /***********************************************************************
202 * ANDROID_GetMonitorInfo
204 BOOL CDECL ANDROID_GetMonitorInfo( HMONITOR handle, LPMONITORINFO info )
206 if (handle != (HMONITOR)1)
208 SetLastError( ERROR_INVALID_HANDLE );
209 return FALSE;
211 info->rcMonitor = default_monitor.rcMonitor;
212 info->rcWork = default_monitor.rcWork;
213 info->dwFlags = default_monitor.dwFlags;
214 if (info->cbSize >= sizeof(MONITORINFOEXW))
215 lstrcpyW( ((MONITORINFOEXW *)info)->szDevice, default_monitor.szDevice );
216 return TRUE;
220 /***********************************************************************
221 * ANDROID_EnumDisplayMonitors
223 BOOL CDECL ANDROID_EnumDisplayMonitors( HDC hdc, LPRECT rect, MONITORENUMPROC proc, LPARAM lp )
225 return proc( (HMONITOR)1, 0, &default_monitor.rcMonitor, lp );
229 /***********************************************************************
230 * ANDROID_EnumDisplaySettingsEx
232 BOOL CDECL ANDROID_EnumDisplaySettingsEx( LPCWSTR name, DWORD n, LPDEVMODEW devmode, DWORD flags)
234 static const WCHAR dev_name[CCHDEVICENAME] =
235 { 'W','i','n','e',' ','A','n','d','r','o','i','d',' ','d','r','i','v','e','r',0 };
237 devmode->dmSize = offsetof( DEVMODEW, dmICMMethod );
238 devmode->dmSpecVersion = DM_SPECVERSION;
239 devmode->dmDriverVersion = DM_SPECVERSION;
240 memcpy( devmode->dmDeviceName, dev_name, sizeof(dev_name) );
241 devmode->dmDriverExtra = 0;
242 devmode->u2.dmDisplayFlags = 0;
243 devmode->dmDisplayFrequency = 0;
244 devmode->u1.s2.dmPosition.x = 0;
245 devmode->u1.s2.dmPosition.y = 0;
246 devmode->u1.s2.dmDisplayOrientation = 0;
247 devmode->u1.s2.dmDisplayFixedOutput = 0;
249 if (n == ENUM_CURRENT_SETTINGS || n == ENUM_REGISTRY_SETTINGS) n = 0;
250 if (n == 0)
252 devmode->dmPelsWidth = screen_width;
253 devmode->dmPelsHeight = screen_height;
254 devmode->dmBitsPerPel = screen_bpp;
255 devmode->dmDisplayFrequency = 60;
256 devmode->dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFLAGS | DM_DISPLAYFREQUENCY;
257 TRACE( "mode %d -- %dx%d %d bpp @%d Hz\n", n,
258 devmode->dmPelsWidth, devmode->dmPelsHeight,
259 devmode->dmBitsPerPel, devmode->dmDisplayFrequency );
260 return TRUE;
262 TRACE( "mode %d -- not present\n", n );
263 SetLastError( ERROR_NO_MORE_FILES );
264 return FALSE;
268 /**********************************************************************
269 * ANDROID_wine_get_wgl_driver
271 static struct opengl_funcs * CDECL ANDROID_wine_get_wgl_driver( PHYSDEV dev, UINT version )
273 struct opengl_funcs *ret;
275 if (!(ret = get_wgl_driver( version )))
277 dev = GET_NEXT_PHYSDEV( dev, wine_get_wgl_driver );
278 ret = dev->funcs->wine_get_wgl_driver( dev, version );
280 return ret;
284 static const struct gdi_dc_funcs android_drv_funcs =
286 NULL, /* pAbortDoc */
287 NULL, /* pAbortPath */
288 NULL, /* pAlphaBlend */
289 NULL, /* pAngleArc */
290 NULL, /* pArc */
291 NULL, /* pArcTo */
292 NULL, /* pBeginPath */
293 NULL, /* pBlendImage */
294 NULL, /* pChord */
295 NULL, /* pCloseFigure */
296 ANDROID_CreateCompatibleDC, /* pCreateCompatibleDC */
297 ANDROID_CreateDC, /* pCreateDC */
298 ANDROID_DeleteDC, /* pDeleteDC */
299 NULL, /* pDeleteObject */
300 NULL, /* pDeviceCapabilities */
301 NULL, /* pEllipse */
302 NULL, /* pEndDoc */
303 NULL, /* pEndPage */
304 NULL, /* pEndPath */
305 NULL, /* pEnumFonts */
306 NULL, /* pEnumICMProfiles */
307 NULL, /* pExtDeviceMode */
308 NULL, /* pExtEscape */
309 NULL, /* pExtFloodFill */
310 NULL, /* pExtTextOut */
311 NULL, /* pFillPath */
312 NULL, /* pFillRgn */
313 NULL, /* pFlattenPath */
314 NULL, /* pFontIsLinked */
315 NULL, /* pFrameRgn */
316 NULL, /* pGdiComment */
317 NULL, /* pGetBoundsRect */
318 NULL, /* pGetCharABCWidths */
319 NULL, /* pGetCharABCWidthsI */
320 NULL, /* pGetCharWidth */
321 NULL, /* pGetCharWidthInfo */
322 NULL, /* pGetDeviceCaps */
323 NULL, /* pGetDeviceGammaRamp */
324 NULL, /* pGetFontData */
325 NULL, /* pGetFontRealizationInfo */
326 NULL, /* pGetFontUnicodeRanges */
327 NULL, /* pGetGlyphIndices */
328 NULL, /* pGetGlyphOutline */
329 NULL, /* pGetICMProfile */
330 NULL, /* pGetImage */
331 NULL, /* pGetKerningPairs */
332 NULL, /* pGetNearestColor */
333 NULL, /* pGetOutlineTextMetrics */
334 NULL, /* pGetPixel */
335 NULL, /* pGetSystemPaletteEntries */
336 NULL, /* pGetTextCharsetInfo */
337 NULL, /* pGetTextExtentExPoint */
338 NULL, /* pGetTextExtentExPointI */
339 NULL, /* pGetTextFace */
340 NULL, /* pGetTextMetrics */
341 NULL, /* pGradientFill */
342 NULL, /* pInvertRgn */
343 NULL, /* pLineTo */
344 NULL, /* pModifyWorldTransform */
345 NULL, /* pMoveTo */
346 NULL, /* pPaintRgn */
347 NULL, /* pPatBlt */
348 NULL, /* pPie */
349 NULL, /* pPolyBezier */
350 NULL, /* pPolyBezierTo */
351 NULL, /* pPolyDraw */
352 NULL, /* pPolyPolygon */
353 NULL, /* pPolyPolyline */
354 NULL, /* pPolylineTo */
355 NULL, /* pPutImage */
356 NULL, /* pRealizeDefaultPalette */
357 NULL, /* pRealizePalette */
358 NULL, /* pRectangle */
359 NULL, /* pResetDC */
360 NULL, /* pRestoreDC */
361 NULL, /* pRoundRect */
362 NULL, /* pSelectBitmap */
363 NULL, /* pSelectBrush */
364 NULL, /* pSelectClipPath */
365 NULL, /* pSelectFont */
366 NULL, /* pSelectPen */
367 NULL, /* pSetBkColor */
368 NULL, /* pSetBoundsRect */
369 NULL, /* pSetDCBrushColor */
370 NULL, /* pSetDCPenColor */
371 NULL, /* pSetDIBitsToDevice */
372 NULL, /* pSetDeviceClipping */
373 NULL, /* pSetDeviceGammaRamp */
374 NULL, /* pSetPixel */
375 NULL, /* pSetTextColor */
376 NULL, /* pSetWorldTransform */
377 NULL, /* pStartDoc */
378 NULL, /* pStartPage */
379 NULL, /* pStretchBlt */
380 NULL, /* pStretchDIBits */
381 NULL, /* pStrokeAndFillPath */
382 NULL, /* pStrokePath */
383 NULL, /* pUnrealizePalette */
384 NULL, /* pWidenPath */
385 NULL, /* pD3DKMTCheckVidPnExclusiveOwnership */
386 NULL, /* pD3DKMTSetVidPnSourceOwner */
387 ANDROID_wine_get_wgl_driver, /* wine_get_wgl_driver */
388 NULL, /* wine_get_vulkan_driver */
389 GDI_PRIORITY_GRAPHICS_DRV /* priority */
393 /******************************************************************************
394 * ANDROID_get_gdi_driver
396 const struct gdi_dc_funcs * CDECL ANDROID_get_gdi_driver( unsigned int version )
398 if (version != WINE_GDI_DRIVER_VERSION)
400 ERR( "version mismatch, gdi32 wants %u but wineandroid has %u\n", version, WINE_GDI_DRIVER_VERSION );
401 return NULL;
403 return &android_drv_funcs;
407 static const JNINativeMethod methods[] =
409 { "wine_desktop_changed", "(II)V", desktop_changed },
410 { "wine_config_changed", "(I)V", config_changed },
411 { "wine_surface_changed", "(ILandroid/view/Surface;Z)V", surface_changed },
412 { "wine_motion_event", "(IIIIII)Z", motion_event },
413 { "wine_keyboard_event", "(IIII)Z", keyboard_event },
416 #define DECL_FUNCPTR(f) typeof(f) * p##f = NULL
417 #define LOAD_FUNCPTR(lib, func) do { \
418 if ((p##func = dlsym( lib, #func )) == NULL) \
419 { ERR( "can't find symbol %s\n", #func); return; } \
420 } while(0)
422 DECL_FUNCPTR( __android_log_print );
423 DECL_FUNCPTR( ANativeWindow_fromSurface );
424 DECL_FUNCPTR( ANativeWindow_release );
425 DECL_FUNCPTR( hw_get_module );
427 #ifndef DT_GNU_HASH
428 #define DT_GNU_HASH 0x6ffffef5
429 #endif
431 static unsigned int gnu_hash( const char *name )
433 unsigned int h = 5381;
434 while (*name) h = h * 33 + (unsigned char)*name++;
435 return h;
438 static unsigned int hash_symbol( const char *name )
440 unsigned int hi, hash = 0;
441 while (*name)
443 hash = (hash << 4) + (unsigned char)*name++;
444 hi = hash & 0xf0000000;
445 hash ^= hi;
446 hash ^= hi >> 24;
448 return hash;
451 static void *find_symbol( const struct dl_phdr_info* info, const char *var, int type )
453 const ElfW(Dyn) *dyn = NULL;
454 const ElfW(Phdr) *ph;
455 const ElfW(Sym) *symtab = NULL;
456 const Elf32_Word *hashtab = NULL;
457 const Elf32_Word *gnu_hashtab = NULL;
458 const char *strings = NULL;
459 Elf32_Word idx;
461 for (ph = info->dlpi_phdr; ph < &info->dlpi_phdr[info->dlpi_phnum]; ++ph)
463 if (PT_DYNAMIC == ph->p_type)
465 dyn = (const ElfW(Dyn) *)(info->dlpi_addr + ph->p_vaddr);
466 break;
469 if (!dyn) return NULL;
471 while (dyn->d_tag)
473 if (dyn->d_tag == DT_STRTAB)
474 strings = (const char*)(info->dlpi_addr + dyn->d_un.d_ptr);
475 if (dyn->d_tag == DT_SYMTAB)
476 symtab = (const ElfW(Sym) *)(info->dlpi_addr + dyn->d_un.d_ptr);
477 if (dyn->d_tag == DT_HASH)
478 hashtab = (const Elf32_Word *)(info->dlpi_addr + dyn->d_un.d_ptr);
479 if (dyn->d_tag == DT_GNU_HASH)
480 gnu_hashtab = (const Elf32_Word *)(info->dlpi_addr + dyn->d_un.d_ptr);
481 dyn++;
484 if (!symtab || !strings) return NULL;
486 if (gnu_hashtab) /* new style hash table */
488 const unsigned int hash = gnu_hash(var);
489 const Elf32_Word nbuckets = gnu_hashtab[0];
490 const Elf32_Word symbias = gnu_hashtab[1];
491 const Elf32_Word nwords = gnu_hashtab[2];
492 const ElfW(Addr) *bitmask = (const ElfW(Addr) *)(gnu_hashtab + 4);
493 const Elf32_Word *buckets = (const Elf32_Word *)(bitmask + nwords);
494 const Elf32_Word *chains = buckets + nbuckets - symbias;
496 if (!(idx = buckets[hash % nbuckets])) return NULL;
499 if ((chains[idx] & ~1u) == (hash & ~1u) &&
500 ELF32_ST_BIND(symtab[idx].st_info) == STB_GLOBAL &&
501 ELF32_ST_TYPE(symtab[idx].st_info) == type &&
502 !strcmp( strings + symtab[idx].st_name, var ))
503 return (void *)(info->dlpi_addr + symtab[idx].st_value);
504 } while (!(chains[idx++] & 1u));
506 else if (hashtab) /* old style hash table */
508 const unsigned int hash = hash_symbol( var );
509 const Elf32_Word nbuckets = hashtab[0];
510 const Elf32_Word *buckets = hashtab + 2;
511 const Elf32_Word *chains = buckets + nbuckets;
513 for (idx = buckets[hash % nbuckets]; idx; idx = chains[idx])
515 if (ELF32_ST_BIND(symtab[idx].st_info) == STB_GLOBAL &&
516 ELF32_ST_TYPE(symtab[idx].st_info) == type &&
517 !strcmp( strings + symtab[idx].st_name, var ))
518 return (void *)(info->dlpi_addr + symtab[idx].st_value);
521 return NULL;
524 static int enum_libs( struct dl_phdr_info* info, size_t size, void* data )
526 const char *p;
528 if (!info->dlpi_name) return 0;
529 if (!(p = strrchr( info->dlpi_name, '/' ))) return 0;
530 if (strcmp( p, "/libhardware.so" )) return 0;
531 TRACE( "found libhardware at %p\n", info->dlpi_phdr );
532 phw_get_module = find_symbol( info, "hw_get_module", STT_FUNC );
533 return 1;
536 static void load_hardware_libs(void)
538 const struct hw_module_t *module;
539 int ret;
540 void *libhardware;
542 if ((libhardware = dlopen( "libhardware.so", RTLD_GLOBAL )))
544 LOAD_FUNCPTR( libhardware, hw_get_module );
546 else
548 /* Android >= N disallows loading libhardware, so we load libandroid (which imports
549 * libhardware), and then we can find libhardware in the list of loaded libraries.
551 if (!dlopen( "libandroid.so", RTLD_GLOBAL ))
553 ERR( "failed to load libandroid.so: %s\n", dlerror() );
554 return;
556 dl_iterate_phdr( enum_libs, 0 );
557 if (!phw_get_module)
559 ERR( "failed to find hw_get_module\n" );
560 return;
564 if ((ret = phw_get_module( GRALLOC_HARDWARE_MODULE_ID, &module )))
566 ERR( "failed to load gralloc module err %d\n", ret );
567 return;
570 init_gralloc( module );
573 static void load_android_libs(void)
575 void *libandroid, *liblog;
577 if (!(libandroid = dlopen( "libandroid.so", RTLD_GLOBAL )))
579 ERR( "failed to load libandroid.so: %s\n", dlerror() );
580 return;
582 if (!(liblog = dlopen( "liblog.so", RTLD_GLOBAL )))
584 ERR( "failed to load liblog.so: %s\n", dlerror() );
585 return;
587 LOAD_FUNCPTR( liblog, __android_log_print );
588 LOAD_FUNCPTR( libandroid, ANativeWindow_fromSurface );
589 LOAD_FUNCPTR( libandroid, ANativeWindow_release );
592 #undef DECL_FUNCPTR
593 #undef LOAD_FUNCPTR
595 JavaVM **p_java_vm = NULL;
596 jobject *p_java_object = NULL;
597 unsigned short *p_java_gdt_sel = NULL;
599 static BOOL process_attach(void)
601 jclass class;
602 jobject object;
603 JNIEnv *jni_env;
604 JavaVM *java_vm;
605 void *ntdll;
607 if (!(ntdll = dlopen( "ntdll.so", RTLD_NOW ))) return FALSE;
609 p_java_vm = dlsym( ntdll, "java_vm" );
610 p_java_object = dlsym( ntdll, "java_object" );
611 p_java_gdt_sel = dlsym( ntdll, "java_gdt_sel" );
613 object = *p_java_object;
615 load_hardware_libs();
617 if ((java_vm = *p_java_vm)) /* running under Java */
619 #ifdef __i386__
620 WORD old_fs;
621 __asm__( "mov %%fs,%0" : "=r" (old_fs) );
622 #endif
623 load_android_libs();
624 (*java_vm)->AttachCurrentThread( java_vm, &jni_env, 0 );
625 class = (*jni_env)->GetObjectClass( jni_env, object );
626 (*jni_env)->RegisterNatives( jni_env, class, methods, ARRAY_SIZE( methods ));
627 (*jni_env)->DeleteLocalRef( jni_env, class );
628 #ifdef __i386__
629 /* the Java VM hijacks %fs for its own purposes, restore it */
630 __asm__( "mov %0,%%fs" :: "r" (old_fs) );
631 #endif
633 return TRUE;
636 /***********************************************************************
637 * dll initialisation routine
639 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
641 switch (reason)
643 case DLL_PROCESS_ATTACH:
644 DisableThreadLibraryCalls( inst );
645 return process_attach();
647 return TRUE;