wevtapi: Fix typo in spec file.
[wine/zf.git] / dlls / wineandroid.drv / init.c
blobd19969649e0ec2beab0949dd9f4696375fe37271
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/library.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(android);
40 unsigned int screen_width = 0;
41 unsigned int screen_height = 0;
42 RECT virtual_screen_rect = { 0, 0, 0, 0 };
44 MONITORINFOEXW default_monitor =
46 sizeof(default_monitor), /* cbSize */
47 { 0, 0, 0, 0 }, /* rcMonitor */
48 { 0, 0, 0, 0 }, /* rcWork */
49 MONITORINFOF_PRIMARY, /* dwFlags */
50 { '\\','\\','.','\\','D','I','S','P','L','A','Y','1',0 } /* szDevice */
53 static const unsigned int screen_bpp = 32; /* we don't support other modes */
55 static int device_init_done;
57 typedef struct
59 struct gdi_physdev dev;
60 } ANDROID_PDEVICE;
62 static const struct gdi_dc_funcs android_drv_funcs;
65 /******************************************************************************
66 * init_monitors
68 void init_monitors( int width, int height )
70 static const WCHAR trayW[] = {'S','h','e','l','l','_','T','r','a','y','W','n','d',0};
71 RECT rect;
72 HWND hwnd = FindWindowW( trayW, NULL );
74 virtual_screen_rect.right = width;
75 virtual_screen_rect.bottom = height;
76 default_monitor.rcMonitor = default_monitor.rcWork = virtual_screen_rect;
78 if (!hwnd || !IsWindowVisible( hwnd )) return;
79 if (!GetWindowRect( hwnd, &rect )) return;
80 if (rect.top) default_monitor.rcWork.bottom = rect.top;
81 else default_monitor.rcWork.top = rect.bottom;
82 TRACE( "found tray %p %s work area %s\n", hwnd,
83 wine_dbgstr_rect( &rect ), wine_dbgstr_rect( &default_monitor.rcWork ));
87 /******************************************************************************
88 * set_screen_dpi
90 void set_screen_dpi( DWORD dpi )
92 static const WCHAR dpi_key_name[] = {'S','o','f','t','w','a','r','e','\\','F','o','n','t','s',0};
93 static const WCHAR dpi_value_name[] = {'L','o','g','P','i','x','e','l','s',0};
94 HKEY hkey;
96 if (!RegCreateKeyW( HKEY_CURRENT_CONFIG, dpi_key_name, &hkey ))
98 RegSetValueExW( hkey, dpi_value_name, 0, REG_DWORD, (void *)&dpi, sizeof(DWORD) );
99 RegCloseKey( hkey );
103 /**********************************************************************
104 * fetch_display_metrics
106 static void fetch_display_metrics(void)
108 if (wine_get_java_vm()) return; /* for Java threads it will be set when the top view is created */
110 SERVER_START_REQ( get_window_rectangles )
112 req->handle = wine_server_user_handle( GetDesktopWindow() );
113 req->relative = COORDS_CLIENT;
114 if (!wine_server_call( req ))
116 screen_width = reply->window.right;
117 screen_height = reply->window.bottom;
120 SERVER_END_REQ;
122 init_monitors( screen_width, screen_height );
123 TRACE( "screen %ux%u\n", screen_width, screen_height );
127 /**********************************************************************
128 * device_init
130 * Perform initializations needed upon creation of the first device.
132 static void device_init(void)
134 device_init_done = TRUE;
135 fetch_display_metrics();
139 /******************************************************************************
140 * create_android_physdev
142 static ANDROID_PDEVICE *create_android_physdev(void)
144 ANDROID_PDEVICE *physdev;
146 if (!device_init_done) device_init();
148 if (!(physdev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*physdev) ))) return NULL;
149 return physdev;
152 /**********************************************************************
153 * ANDROID_CreateDC
155 static BOOL ANDROID_CreateDC( PHYSDEV *pdev, LPCWSTR driver, LPCWSTR device,
156 LPCWSTR output, const DEVMODEW* initData )
158 ANDROID_PDEVICE *physdev = create_android_physdev();
160 if (!physdev) return FALSE;
162 push_dc_driver( pdev, &physdev->dev, &android_drv_funcs );
163 return TRUE;
167 /**********************************************************************
168 * ANDROID_CreateCompatibleDC
170 static BOOL ANDROID_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
172 ANDROID_PDEVICE *physdev = create_android_physdev();
174 if (!physdev) return FALSE;
176 push_dc_driver( pdev, &physdev->dev, &android_drv_funcs );
177 return TRUE;
181 /**********************************************************************
182 * ANDROID_DeleteDC
184 static BOOL ANDROID_DeleteDC( PHYSDEV dev )
186 HeapFree( GetProcessHeap(), 0, dev );
187 return TRUE;
191 /***********************************************************************
192 * ANDROID_ChangeDisplaySettingsEx
194 LONG CDECL ANDROID_ChangeDisplaySettingsEx( LPCWSTR devname, LPDEVMODEW devmode,
195 HWND hwnd, DWORD flags, LPVOID lpvoid )
197 FIXME( "(%s,%p,%p,0x%08x,%p)\n", debugstr_w( devname ), devmode, hwnd, flags, lpvoid );
198 return DISP_CHANGE_SUCCESSFUL;
202 /***********************************************************************
203 * ANDROID_GetMonitorInfo
205 BOOL CDECL ANDROID_GetMonitorInfo( HMONITOR handle, LPMONITORINFO info )
207 if (handle != (HMONITOR)1)
209 SetLastError( ERROR_INVALID_HANDLE );
210 return FALSE;
212 info->rcMonitor = default_monitor.rcMonitor;
213 info->rcWork = default_monitor.rcWork;
214 info->dwFlags = default_monitor.dwFlags;
215 if (info->cbSize >= sizeof(MONITORINFOEXW))
216 lstrcpyW( ((MONITORINFOEXW *)info)->szDevice, default_monitor.szDevice );
217 return TRUE;
221 /***********************************************************************
222 * ANDROID_EnumDisplayMonitors
224 BOOL CDECL ANDROID_EnumDisplayMonitors( HDC hdc, LPRECT rect, MONITORENUMPROC proc, LPARAM lp )
226 return proc( (HMONITOR)1, 0, &default_monitor.rcMonitor, lp );
230 /***********************************************************************
231 * ANDROID_EnumDisplaySettingsEx
233 BOOL CDECL ANDROID_EnumDisplaySettingsEx( LPCWSTR name, DWORD n, LPDEVMODEW devmode, DWORD flags)
235 static const WCHAR dev_name[CCHDEVICENAME] =
236 { 'W','i','n','e',' ','A','n','d','r','o','i','d',' ','d','r','i','v','e','r',0 };
238 devmode->dmSize = offsetof( DEVMODEW, dmICMMethod );
239 devmode->dmSpecVersion = DM_SPECVERSION;
240 devmode->dmDriverVersion = DM_SPECVERSION;
241 memcpy( devmode->dmDeviceName, dev_name, sizeof(dev_name) );
242 devmode->dmDriverExtra = 0;
243 devmode->u2.dmDisplayFlags = 0;
244 devmode->dmDisplayFrequency = 0;
245 devmode->u1.s2.dmPosition.x = 0;
246 devmode->u1.s2.dmPosition.y = 0;
247 devmode->u1.s2.dmDisplayOrientation = 0;
248 devmode->u1.s2.dmDisplayFixedOutput = 0;
250 if (n == ENUM_CURRENT_SETTINGS || n == ENUM_REGISTRY_SETTINGS) n = 0;
251 if (n == 0)
253 devmode->dmPelsWidth = screen_width;
254 devmode->dmPelsHeight = screen_height;
255 devmode->dmBitsPerPel = screen_bpp;
256 devmode->dmDisplayFrequency = 60;
257 devmode->dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFLAGS | DM_DISPLAYFREQUENCY;
258 TRACE( "mode %d -- %dx%d %d bpp @%d Hz\n", n,
259 devmode->dmPelsWidth, devmode->dmPelsHeight,
260 devmode->dmBitsPerPel, devmode->dmDisplayFrequency );
261 return TRUE;
263 TRACE( "mode %d -- not present\n", n );
264 SetLastError( ERROR_NO_MORE_FILES );
265 return FALSE;
269 /**********************************************************************
270 * ANDROID_wine_get_wgl_driver
272 static struct opengl_funcs * ANDROID_wine_get_wgl_driver( PHYSDEV dev, UINT version )
274 struct opengl_funcs *ret;
276 if (!(ret = get_wgl_driver( version )))
278 dev = GET_NEXT_PHYSDEV( dev, wine_get_wgl_driver );
279 ret = dev->funcs->wine_get_wgl_driver( dev, version );
281 return ret;
285 static const struct gdi_dc_funcs android_drv_funcs =
287 NULL, /* pAbortDoc */
288 NULL, /* pAbortPath */
289 NULL, /* pAlphaBlend */
290 NULL, /* pAngleArc */
291 NULL, /* pArc */
292 NULL, /* pArcTo */
293 NULL, /* pBeginPath */
294 NULL, /* pBlendImage */
295 NULL, /* pChord */
296 NULL, /* pCloseFigure */
297 ANDROID_CreateCompatibleDC, /* pCreateCompatibleDC */
298 ANDROID_CreateDC, /* pCreateDC */
299 ANDROID_DeleteDC, /* pDeleteDC */
300 NULL, /* pDeleteObject */
301 NULL, /* pDeviceCapabilities */
302 NULL, /* pEllipse */
303 NULL, /* pEndDoc */
304 NULL, /* pEndPage */
305 NULL, /* pEndPath */
306 NULL, /* pEnumFonts */
307 NULL, /* pEnumICMProfiles */
308 NULL, /* pExcludeClipRect */
309 NULL, /* pExtDeviceMode */
310 NULL, /* pExtEscape */
311 NULL, /* pExtFloodFill */
312 NULL, /* pExtSelectClipRgn */
313 NULL, /* pExtTextOut */
314 NULL, /* pFillPath */
315 NULL, /* pFillRgn */
316 NULL, /* pFlattenPath */
317 NULL, /* pFontIsLinked */
318 NULL, /* pFrameRgn */
319 NULL, /* pGdiComment */
320 NULL, /* pGetBoundsRect */
321 NULL, /* pGetCharABCWidths */
322 NULL, /* pGetCharABCWidthsI */
323 NULL, /* pGetCharWidth */
324 NULL, /* pGetDeviceCaps */
325 NULL, /* pGetDeviceGammaRamp */
326 NULL, /* pGetFontData */
327 NULL, /* pGetFontRealizationInfo */
328 NULL, /* pGetFontUnicodeRanges */
329 NULL, /* pGetGlyphIndices */
330 NULL, /* pGetGlyphOutline */
331 NULL, /* pGetICMProfile */
332 NULL, /* pGetImage */
333 NULL, /* pGetKerningPairs */
334 NULL, /* pGetNearestColor */
335 NULL, /* pGetOutlineTextMetrics */
336 NULL, /* pGetPixel */
337 NULL, /* pGetSystemPaletteEntries */
338 NULL, /* pGetTextCharsetInfo */
339 NULL, /* pGetTextExtentExPoint */
340 NULL, /* pGetTextExtentExPointI */
341 NULL, /* pGetTextFace */
342 NULL, /* pGetTextMetrics */
343 NULL, /* pGradientFill */
344 NULL, /* pIntersectClipRect */
345 NULL, /* pInvertRgn */
346 NULL, /* pLineTo */
347 NULL, /* pModifyWorldTransform */
348 NULL, /* pMoveTo */
349 NULL, /* pOffsetClipRgn */
350 NULL, /* pOffsetViewportOrg */
351 NULL, /* pOffsetWindowOrg */
352 NULL, /* pPaintRgn */
353 NULL, /* pPatBlt */
354 NULL, /* pPie */
355 NULL, /* pPolyBezier */
356 NULL, /* pPolyBezierTo */
357 NULL, /* pPolyDraw */
358 NULL, /* pPolyPolygon */
359 NULL, /* pPolyPolyline */
360 NULL, /* pPolygon */
361 NULL, /* pPolyline */
362 NULL, /* pPolylineTo */
363 NULL, /* pPutImage */
364 NULL, /* pRealizeDefaultPalette */
365 NULL, /* pRealizePalette */
366 NULL, /* pRectangle */
367 NULL, /* pResetDC */
368 NULL, /* pRestoreDC */
369 NULL, /* pRoundRect */
370 NULL, /* pSaveDC */
371 NULL, /* pScaleViewportExt */
372 NULL, /* pScaleWindowExt */
373 NULL, /* pSelectBitmap */
374 NULL, /* pSelectBrush */
375 NULL, /* pSelectClipPath */
376 NULL, /* pSelectFont */
377 NULL, /* pSelectPalette */
378 NULL, /* pSelectPen */
379 NULL, /* pSetArcDirection */
380 NULL, /* pSetBkColor */
381 NULL, /* pSetBkMode */
382 NULL, /* pSetBoundsRect */
383 NULL, /* pSetDCBrushColor */
384 NULL, /* pSetDCPenColor */
385 NULL, /* pSetDIBitsToDevice */
386 NULL, /* pSetDeviceClipping */
387 NULL, /* pSetDeviceGammaRamp */
388 NULL, /* pSetLayout */
389 NULL, /* pSetMapMode */
390 NULL, /* pSetMapperFlags */
391 NULL, /* pSetPixel */
392 NULL, /* pSetPolyFillMode */
393 NULL, /* pSetROP2 */
394 NULL, /* pSetRelAbs */
395 NULL, /* pSetStretchBltMode */
396 NULL, /* pSetTextAlign */
397 NULL, /* pSetTextCharacterExtra */
398 NULL, /* pSetTextColor */
399 NULL, /* pSetTextJustification */
400 NULL, /* pSetViewportExt */
401 NULL, /* pSetViewportOrg */
402 NULL, /* pSetWindowExt */
403 NULL, /* pSetWindowOrg */
404 NULL, /* pSetWorldTransform */
405 NULL, /* pStartDoc */
406 NULL, /* pStartPage */
407 NULL, /* pStretchBlt */
408 NULL, /* pStretchDIBits */
409 NULL, /* pStrokeAndFillPath */
410 NULL, /* pStrokePath */
411 NULL, /* pUnrealizePalette */
412 NULL, /* pWidenPath */
413 ANDROID_wine_get_wgl_driver, /* wine_get_wgl_driver */
414 NULL, /* wine_get_vulkan_driver */
415 GDI_PRIORITY_GRAPHICS_DRV /* priority */
419 /******************************************************************************
420 * ANDROID_get_gdi_driver
422 const struct gdi_dc_funcs * CDECL ANDROID_get_gdi_driver( unsigned int version )
424 if (version != WINE_GDI_DRIVER_VERSION)
426 ERR( "version mismatch, gdi32 wants %u but wineandroid has %u\n", version, WINE_GDI_DRIVER_VERSION );
427 return NULL;
429 return &android_drv_funcs;
433 static const JNINativeMethod methods[] =
435 { "wine_desktop_changed", "(II)V", desktop_changed },
436 { "wine_config_changed", "(I)V", config_changed },
437 { "wine_surface_changed", "(ILandroid/view/Surface;Z)V", surface_changed },
438 { "wine_motion_event", "(IIIIII)Z", motion_event },
439 { "wine_keyboard_event", "(IIII)Z", keyboard_event },
442 #define DECL_FUNCPTR(f) typeof(f) * p##f = NULL
443 #define LOAD_FUNCPTR(lib, func) do { \
444 if ((p##func = wine_dlsym( lib, #func, NULL, 0 )) == NULL) \
445 { ERR( "can't find symbol %s\n", #func); return; } \
446 } while(0)
448 DECL_FUNCPTR( __android_log_print );
449 DECL_FUNCPTR( ANativeWindow_fromSurface );
450 DECL_FUNCPTR( ANativeWindow_release );
451 DECL_FUNCPTR( hw_get_module );
453 struct gralloc_module_t *gralloc_module = NULL;
455 #ifndef DT_GNU_HASH
456 #define DT_GNU_HASH 0x6ffffef5
457 #endif
459 static unsigned int gnu_hash( const char *name )
461 unsigned int h = 5381;
462 while (*name) h = h * 33 + (unsigned char)*name++;
463 return h;
466 static unsigned int hash_symbol( const char *name )
468 unsigned int hi, hash = 0;
469 while (*name)
471 hash = (hash << 4) + (unsigned char)*name++;
472 hi = hash & 0xf0000000;
473 hash ^= hi;
474 hash ^= hi >> 24;
476 return hash;
479 static void *find_symbol( const struct dl_phdr_info* info, const char *var, int type )
481 const ElfW(Dyn) *dyn = NULL;
482 const ElfW(Phdr) *ph;
483 const ElfW(Sym) *symtab = NULL;
484 const Elf32_Word *hashtab = NULL;
485 const Elf32_Word *gnu_hashtab = NULL;
486 const char *strings = NULL;
487 Elf32_Word idx;
489 for (ph = info->dlpi_phdr; ph < &info->dlpi_phdr[info->dlpi_phnum]; ++ph)
491 if (PT_DYNAMIC == ph->p_type)
493 dyn = (const ElfW(Dyn) *)(info->dlpi_addr + ph->p_vaddr);
494 break;
497 if (!dyn) return NULL;
499 while (dyn->d_tag)
501 if (dyn->d_tag == DT_STRTAB)
502 strings = (const char*)(info->dlpi_addr + dyn->d_un.d_ptr);
503 if (dyn->d_tag == DT_SYMTAB)
504 symtab = (const ElfW(Sym) *)(info->dlpi_addr + dyn->d_un.d_ptr);
505 if (dyn->d_tag == DT_HASH)
506 hashtab = (const Elf32_Word *)(info->dlpi_addr + dyn->d_un.d_ptr);
507 if (dyn->d_tag == DT_GNU_HASH)
508 gnu_hashtab = (const Elf32_Word *)(info->dlpi_addr + dyn->d_un.d_ptr);
509 dyn++;
512 if (!symtab || !strings) return NULL;
514 if (gnu_hashtab) /* new style hash table */
516 const unsigned int hash = gnu_hash(var);
517 const Elf32_Word nbuckets = gnu_hashtab[0];
518 const Elf32_Word symbias = gnu_hashtab[1];
519 const Elf32_Word nwords = gnu_hashtab[2];
520 const ElfW(Addr) *bitmask = (const ElfW(Addr) *)(gnu_hashtab + 4);
521 const Elf32_Word *buckets = (const Elf32_Word *)(bitmask + nwords);
522 const Elf32_Word *chains = buckets + nbuckets - symbias;
524 if (!(idx = buckets[hash % nbuckets])) return NULL;
527 if ((chains[idx] & ~1u) == (hash & ~1u) &&
528 ELF32_ST_BIND(symtab[idx].st_info) == STB_GLOBAL &&
529 ELF32_ST_TYPE(symtab[idx].st_info) == type &&
530 !strcmp( strings + symtab[idx].st_name, var ))
531 return (void *)(info->dlpi_addr + symtab[idx].st_value);
532 } while (!(chains[idx++] & 1u));
534 else if (hashtab) /* old style hash table */
536 const unsigned int hash = hash_symbol( var );
537 const Elf32_Word nbuckets = hashtab[0];
538 const Elf32_Word *buckets = hashtab + 2;
539 const Elf32_Word *chains = buckets + nbuckets;
541 for (idx = buckets[hash % nbuckets]; idx; idx = chains[idx])
543 if (ELF32_ST_BIND(symtab[idx].st_info) == STB_GLOBAL &&
544 ELF32_ST_TYPE(symtab[idx].st_info) == type &&
545 !strcmp( strings + symtab[idx].st_name, var ))
546 return (void *)(info->dlpi_addr + symtab[idx].st_value);
549 return NULL;
552 static int enum_libs( struct dl_phdr_info* info, size_t size, void* data )
554 const char *p;
556 if (!info->dlpi_name) return 0;
557 if (!(p = strrchr( info->dlpi_name, '/' ))) return 0;
558 if (strcmp( p, "/libhardware.so" )) return 0;
559 TRACE( "found libhardware at %p\n", info->dlpi_phdr );
560 phw_get_module = find_symbol( info, "hw_get_module", STT_FUNC );
561 return 1;
564 static void load_hardware_libs(void)
566 const struct hw_module_t *module;
567 void *libhardware;
568 char error[256];
570 if ((libhardware = wine_dlopen( "libhardware.so", RTLD_GLOBAL, error, sizeof(error) )))
572 LOAD_FUNCPTR( libhardware, hw_get_module );
574 else
576 /* Android >= N disallows loading libhardware, so we load libandroid (which imports
577 * libhardware), and then we can find libhardware in the list of loaded libraries.
579 if (!wine_dlopen( "libandroid.so", RTLD_GLOBAL, error, sizeof(error) ))
581 ERR( "failed to load libandroid.so: %s\n", error );
582 return;
584 dl_iterate_phdr( enum_libs, 0 );
585 if (!phw_get_module)
587 ERR( "failed to find hw_get_module\n" );
588 return;
592 if (phw_get_module( GRALLOC_HARDWARE_MODULE_ID, &module ) == 0)
593 gralloc_module = (struct gralloc_module_t *)module;
594 else
595 ERR( "failed to load gralloc module\n" );
598 static void load_android_libs(void)
600 void *libandroid, *liblog;
601 char error[1024];
603 if (!(libandroid = wine_dlopen( "libandroid.so", RTLD_GLOBAL, error, sizeof(error) )))
605 ERR( "failed to load libandroid.so: %s\n", error );
606 return;
608 if (!(liblog = wine_dlopen( "liblog.so", RTLD_GLOBAL, error, sizeof(error) )))
610 ERR( "failed to load liblog.so: %s\n", error );
611 return;
613 LOAD_FUNCPTR( liblog, __android_log_print );
614 LOAD_FUNCPTR( libandroid, ANativeWindow_fromSurface );
615 LOAD_FUNCPTR( libandroid, ANativeWindow_release );
618 #undef DECL_FUNCPTR
619 #undef LOAD_FUNCPTR
621 static BOOL process_attach(void)
623 jclass class;
624 jobject object = wine_get_java_object();
625 JNIEnv *jni_env;
626 JavaVM *java_vm;
628 load_hardware_libs();
630 if ((java_vm = wine_get_java_vm())) /* running under Java */
632 #ifdef __i386__
633 WORD old_fs = wine_get_fs();
634 #endif
635 load_android_libs();
636 (*java_vm)->AttachCurrentThread( java_vm, &jni_env, 0 );
637 class = (*jni_env)->GetObjectClass( jni_env, object );
638 (*jni_env)->RegisterNatives( jni_env, class, methods, ARRAY_SIZE( methods ));
639 (*jni_env)->DeleteLocalRef( jni_env, class );
640 #ifdef __i386__
641 wine_set_fs( old_fs ); /* the Java VM hijacks %fs for its own purposes, restore it */
642 #endif
644 return TRUE;
647 /***********************************************************************
648 * dll initialisation routine
650 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
652 switch (reason)
654 case DLL_PROCESS_ATTACH:
655 DisableThreadLibraryCalls( inst );
656 return process_attach();
658 return TRUE;