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
24 #include "wine/port.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
;
58 struct gdi_physdev dev
;
61 static const struct gdi_dc_funcs android_drv_funcs
;
64 /******************************************************************************
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};
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 /******************************************************************************
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};
95 if (!RegCreateKeyW( HKEY_CURRENT_CONFIG
, dpi_key_name
, &hkey
))
97 RegSetValueExW( hkey
, dpi_value_name
, 0, REG_DWORD
, (void *)&dpi
, sizeof(DWORD
) );
102 /**********************************************************************
103 * fetch_display_metrics
105 static void fetch_display_metrics(void)
107 if (p_wine_get_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
;
121 init_monitors( screen_width
, screen_height
);
122 TRACE( "screen %ux%u\n", screen_width
, screen_height
);
126 /**********************************************************************
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
;
151 /**********************************************************************
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
);
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
);
180 /**********************************************************************
183 static BOOL CDECL
ANDROID_DeleteDC( PHYSDEV dev
)
185 HeapFree( GetProcessHeap(), 0, dev
);
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
);
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
);
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;
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
);
262 TRACE( "mode %d -- not present\n", n
);
263 SetLastError( ERROR_NO_MORE_FILES
);
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
);
284 static const struct gdi_dc_funcs android_drv_funcs
=
286 NULL
, /* pAbortDoc */
287 NULL
, /* pAbortPath */
288 NULL
, /* pAlphaBlend */
289 NULL
, /* pAngleArc */
292 NULL
, /* pBeginPath */
293 NULL
, /* pBlendImage */
295 NULL
, /* pCloseFigure */
296 ANDROID_CreateCompatibleDC
, /* pCreateCompatibleDC */
297 ANDROID_CreateDC
, /* pCreateDC */
298 ANDROID_DeleteDC
, /* pDeleteDC */
299 NULL
, /* pDeleteObject */
300 NULL
, /* pDeviceCapabilities */
305 NULL
, /* pEnumFonts */
306 NULL
, /* pEnumICMProfiles */
307 NULL
, /* pExcludeClipRect */
308 NULL
, /* pExtDeviceMode */
309 NULL
, /* pExtEscape */
310 NULL
, /* pExtFloodFill */
311 NULL
, /* pExtSelectClipRgn */
312 NULL
, /* pExtTextOut */
313 NULL
, /* pFillPath */
315 NULL
, /* pFlattenPath */
316 NULL
, /* pFontIsLinked */
317 NULL
, /* pFrameRgn */
318 NULL
, /* pGdiComment */
319 NULL
, /* pGetBoundsRect */
320 NULL
, /* pGetCharABCWidths */
321 NULL
, /* pGetCharABCWidthsI */
322 NULL
, /* pGetCharWidth */
323 NULL
, /* pGetCharWidthInfo */
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 */
347 NULL
, /* pModifyWorldTransform */
349 NULL
, /* pOffsetClipRgn */
350 NULL
, /* pOffsetViewportOrg */
351 NULL
, /* pOffsetWindowOrg */
352 NULL
, /* pPaintRgn */
355 NULL
, /* pPolyBezier */
356 NULL
, /* pPolyBezierTo */
357 NULL
, /* pPolyDraw */
358 NULL
, /* pPolyPolygon */
359 NULL
, /* pPolyPolyline */
361 NULL
, /* pPolyline */
362 NULL
, /* pPolylineTo */
363 NULL
, /* pPutImage */
364 NULL
, /* pRealizeDefaultPalette */
365 NULL
, /* pRealizePalette */
366 NULL
, /* pRectangle */
368 NULL
, /* pRestoreDC */
369 NULL
, /* pRoundRect */
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 */
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 NULL
, /* pD3DKMTCheckVidPnExclusiveOwnership */
414 NULL
, /* pD3DKMTSetVidPnSourceOwner */
415 ANDROID_wine_get_wgl_driver
, /* wine_get_wgl_driver */
416 NULL
, /* wine_get_vulkan_driver */
417 GDI_PRIORITY_GRAPHICS_DRV
/* priority */
421 /******************************************************************************
422 * ANDROID_get_gdi_driver
424 const struct gdi_dc_funcs
* CDECL
ANDROID_get_gdi_driver( unsigned int version
)
426 if (version
!= WINE_GDI_DRIVER_VERSION
)
428 ERR( "version mismatch, gdi32 wants %u but wineandroid has %u\n", version
, WINE_GDI_DRIVER_VERSION
);
431 return &android_drv_funcs
;
435 static const JNINativeMethod methods
[] =
437 { "wine_desktop_changed", "(II)V", desktop_changed
},
438 { "wine_config_changed", "(I)V", config_changed
},
439 { "wine_surface_changed", "(ILandroid/view/Surface;Z)V", surface_changed
},
440 { "wine_motion_event", "(IIIIII)Z", motion_event
},
441 { "wine_keyboard_event", "(IIII)Z", keyboard_event
},
444 #define DECL_FUNCPTR(f) typeof(f) * p##f = NULL
445 #define LOAD_FUNCPTR(lib, func) do { \
446 if ((p##func = dlsym( lib, #func )) == NULL) \
447 { ERR( "can't find symbol %s\n", #func); return; } \
450 DECL_FUNCPTR( __android_log_print
);
451 DECL_FUNCPTR( ANativeWindow_fromSurface
);
452 DECL_FUNCPTR( ANativeWindow_release
);
453 DECL_FUNCPTR( hw_get_module
);
456 #define DT_GNU_HASH 0x6ffffef5
459 static unsigned int gnu_hash( const char *name
)
461 unsigned int h
= 5381;
462 while (*name
) h
= h
* 33 + (unsigned char)*name
++;
466 static unsigned int hash_symbol( const char *name
)
468 unsigned int hi
, hash
= 0;
471 hash
= (hash
<< 4) + (unsigned char)*name
++;
472 hi
= hash
& 0xf0000000;
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
;
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
);
497 if (!dyn
) return NULL
;
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
);
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
);
552 static int enum_libs( struct dl_phdr_info
* info
, size_t size
, void* data
)
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
);
564 static void load_hardware_libs(void)
566 const struct hw_module_t
*module
;
570 if ((libhardware
= dlopen( "libhardware.so", RTLD_GLOBAL
)))
572 LOAD_FUNCPTR( libhardware
, hw_get_module
);
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 (!dlopen( "libandroid.so", RTLD_GLOBAL
))
581 ERR( "failed to load libandroid.so: %s\n", dlerror() );
584 dl_iterate_phdr( enum_libs
, 0 );
587 ERR( "failed to find hw_get_module\n" );
592 if ((ret
= phw_get_module( GRALLOC_HARDWARE_MODULE_ID
, &module
)))
594 ERR( "failed to load gralloc module err %d\n", ret
);
598 init_gralloc( module
);
601 static void load_android_libs(void)
603 void *libandroid
, *liblog
;
605 if (!(libandroid
= dlopen( "libandroid.so", RTLD_GLOBAL
)))
607 ERR( "failed to load libandroid.so: %s\n", dlerror() );
610 if (!(liblog
= dlopen( "liblog.so", RTLD_GLOBAL
)))
612 ERR( "failed to load liblog.so: %s\n", dlerror() );
615 LOAD_FUNCPTR( liblog
, __android_log_print
);
616 LOAD_FUNCPTR( libandroid
, ANativeWindow_fromSurface
);
617 LOAD_FUNCPTR( libandroid
, ANativeWindow_release
);
623 JavaVM
* (*p_wine_get_java_vm
)(void) = NULL
;
624 jobject (*p_wine_get_java_object
)(void) = NULL
;
626 static BOOL
process_attach(void)
634 if (!(libwine
= dlopen( "libwine.so", RTLD_NOW
))) return FALSE
;
636 p_wine_get_java_vm
= dlsym( libwine
, "wine_get_java_vm" );
637 p_wine_get_java_object
= dlsym( libwine
, "wine_get_java_object" );
639 object
= p_wine_get_java_object();
641 load_hardware_libs();
643 if ((java_vm
= p_wine_get_java_vm())) /* running under Java */
647 __asm__( "mov %%fs,%0" : "=r" (old_fs
) );
650 (*java_vm
)->AttachCurrentThread( java_vm
, &jni_env
, 0 );
651 class = (*jni_env
)->GetObjectClass( jni_env
, object
);
652 (*jni_env
)->RegisterNatives( jni_env
, class, methods
, ARRAY_SIZE( methods
));
653 (*jni_env
)->DeleteLocalRef( jni_env
, class );
655 /* the Java VM hijacks %fs for its own purposes, restore it */
656 __asm__( "mov %0,%%fs" :: "r" (old_fs
) );
662 /***********************************************************************
663 * dll initialisation routine
665 BOOL WINAPI
DllMain( HINSTANCE inst
, DWORD reason
, LPVOID reserved
)
669 case DLL_PROCESS_ATTACH
:
670 DisableThreadLibraryCalls( inst
);
671 return process_attach();