windows.networking.hostname/tests: Check if passed HSTRING is duplicated.
[wine.git] / dlls / wineandroid.drv / window.c
blobd62a2c539097d1f1f38a7b3821c6a4cc445e7eda
1 /*
2 * Window related functions
4 * Copyright 1993, 1994, 1995, 1996, 2001, 2013-2017 Alexandre Julliard
5 * Copyright 1993 David Metcalfe
6 * Copyright 1995, 1996 Alex Korobka
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #if 0
24 #pragma makedep unix
25 #endif
27 #include "config.h"
29 #include <assert.h>
30 #include <fcntl.h>
31 #include <poll.h>
32 #include <errno.h>
33 #include <stdarg.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <unistd.h>
38 #define OEMRESOURCE
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wingdi.h"
42 #include "winuser.h"
44 #include "android.h"
45 #include "wine/server.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(android);
50 /* private window data */
51 struct android_win_data
53 HWND hwnd; /* hwnd that this private data belongs to */
54 HWND parent; /* parent hwnd for child windows */
55 RECT window_rect; /* USER window rectangle relative to parent */
56 RECT whole_rect; /* X window rectangle for the whole window relative to parent */
57 RECT client_rect; /* client area relative to parent */
58 ANativeWindow *window; /* native window wrapper that forwards calls to the desktop process */
59 struct window_surface *surface;
62 #define SWP_AGG_NOPOSCHANGE (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
64 pthread_mutex_t win_data_mutex;
66 static struct android_win_data *win_data_context[32768];
68 static inline int context_idx( HWND hwnd )
70 return LOWORD( hwnd ) >> 1;
73 static void set_surface_region( struct window_surface *window_surface, HRGN win_region );
75 /* only for use on sanitized BITMAPINFO structures */
76 static inline int get_dib_info_size( const BITMAPINFO *info, UINT coloruse )
78 if (info->bmiHeader.biCompression == BI_BITFIELDS)
79 return sizeof(BITMAPINFOHEADER) + 3 * sizeof(DWORD);
80 if (coloruse == DIB_PAL_COLORS)
81 return sizeof(BITMAPINFOHEADER) + info->bmiHeader.biClrUsed * sizeof(WORD);
82 return FIELD_OFFSET( BITMAPINFO, bmiColors[info->bmiHeader.biClrUsed] );
85 static inline int get_dib_stride( int width, int bpp )
87 return ((width * bpp + 31) >> 3) & ~3;
90 static inline int get_dib_image_size( const BITMAPINFO *info )
92 return get_dib_stride( info->bmiHeader.biWidth, info->bmiHeader.biBitCount )
93 * abs( info->bmiHeader.biHeight );
96 static BOOL intersect_rect( RECT *dst, const RECT *src1, const RECT *src2 )
98 dst->left = max(src1->left, src2->left);
99 dst->top = max(src1->top, src2->top);
100 dst->right = min(src1->right, src2->right);
101 dst->bottom = min(src1->bottom, src2->bottom);
102 return !IsRectEmpty( dst );
106 /**********************************************************************
107 * get_win_monitor_dpi
109 static UINT get_win_monitor_dpi( HWND hwnd )
111 return NtUserGetSystemDpiForProcess( NULL ); /* FIXME: get monitor dpi */
115 /***********************************************************************
116 * alloc_win_data
118 static struct android_win_data *alloc_win_data( HWND hwnd )
120 struct android_win_data *data;
122 if ((data = calloc( 1, sizeof(*data) )))
124 data->hwnd = hwnd;
125 data->window = create_ioctl_window( hwnd, FALSE,
126 (float)get_win_monitor_dpi( hwnd ) / NtUserGetDpiForWindow( hwnd ));
127 pthread_mutex_lock( &win_data_mutex );
128 win_data_context[context_idx(hwnd)] = data;
130 return data;
134 /***********************************************************************
135 * free_win_data
137 static void free_win_data( struct android_win_data *data )
139 win_data_context[context_idx( data->hwnd )] = NULL;
140 pthread_mutex_unlock( &win_data_mutex );
141 if (data->window) release_ioctl_window( data->window );
142 free( data );
146 /***********************************************************************
147 * get_win_data
149 * Lock and return the data structure associated with a window.
151 static struct android_win_data *get_win_data( HWND hwnd )
153 struct android_win_data *data;
155 if (!hwnd) return NULL;
156 pthread_mutex_lock( &win_data_mutex );
157 if ((data = win_data_context[context_idx(hwnd)]) && data->hwnd == hwnd) return data;
158 pthread_mutex_unlock( &win_data_mutex );
159 return NULL;
163 /***********************************************************************
164 * release_win_data
166 * Release the data returned by get_win_data.
168 static void release_win_data( struct android_win_data *data )
170 if (data) pthread_mutex_unlock( &win_data_mutex );
174 /***********************************************************************
175 * get_ioctl_window
177 static struct ANativeWindow *get_ioctl_window( HWND hwnd )
179 struct ANativeWindow *ret;
180 struct android_win_data *data = get_win_data( hwnd );
182 if (!data || !data->window) return NULL;
183 ret = grab_ioctl_window( data->window );
184 release_win_data( data );
185 return ret;
189 /* Handling of events coming from the Java side */
191 struct java_event
193 struct list entry;
194 union event_data data;
197 static struct list event_queue = LIST_INIT( event_queue );
198 static struct java_event *current_event;
199 static int event_pipe[2];
200 static DWORD desktop_tid;
202 /***********************************************************************
203 * send_event
205 int send_event( const union event_data *data )
207 int res;
209 if ((res = write( event_pipe[1], data, sizeof(*data) )) != sizeof(*data))
211 p__android_log_print( ANDROID_LOG_ERROR, "wine", "failed to send event" );
212 return -1;
214 return 0;
218 /***********************************************************************
219 * desktop_changed
221 * JNI callback, runs in the context of the Java thread.
223 void desktop_changed( JNIEnv *env, jobject obj, jint width, jint height )
225 union event_data data;
227 memset( &data, 0, sizeof(data) );
228 data.type = DESKTOP_CHANGED;
229 data.desktop.width = width;
230 data.desktop.height = height;
231 p__android_log_print( ANDROID_LOG_INFO, "wine", "desktop_changed: %ux%u", width, height );
232 send_event( &data );
236 /***********************************************************************
237 * config_changed
239 * JNI callback, runs in the context of the Java thread.
241 void config_changed( JNIEnv *env, jobject obj, jint dpi )
243 union event_data data;
245 memset( &data, 0, sizeof(data) );
246 data.type = CONFIG_CHANGED;
247 data.cfg.dpi = dpi;
248 p__android_log_print( ANDROID_LOG_INFO, "wine", "config_changed: %u dpi", dpi );
249 send_event( &data );
253 /***********************************************************************
254 * surface_changed
256 * JNI callback, runs in the context of the Java thread.
258 void surface_changed( JNIEnv *env, jobject obj, jint win, jobject surface, jboolean client )
260 union event_data data;
262 memset( &data, 0, sizeof(data) );
263 data.surface.hwnd = LongToHandle( win );
264 data.surface.client = client;
265 if (surface)
267 int width, height;
268 ANativeWindow *win = pANativeWindow_fromSurface( env, surface );
270 if (win->query( win, NATIVE_WINDOW_WIDTH, &width ) < 0) width = 0;
271 if (win->query( win, NATIVE_WINDOW_HEIGHT, &height ) < 0) height = 0;
272 data.surface.window = win;
273 data.surface.width = width;
274 data.surface.height = height;
275 p__android_log_print( ANDROID_LOG_INFO, "wine", "surface_changed: %p %s %ux%u",
276 data.surface.hwnd, client ? "client" : "whole", width, height );
278 data.type = SURFACE_CHANGED;
279 send_event( &data );
283 /***********************************************************************
284 * motion_event
286 * JNI callback, runs in the context of the Java thread.
288 jboolean motion_event( JNIEnv *env, jobject obj, jint win, jint action, jint x, jint y, jint state, jint vscroll )
290 static LONG button_state;
291 union event_data data;
292 int prev_state;
294 int mask = action & AMOTION_EVENT_ACTION_MASK;
296 if (!( mask == AMOTION_EVENT_ACTION_DOWN ||
297 mask == AMOTION_EVENT_ACTION_UP ||
298 mask == AMOTION_EVENT_ACTION_CANCEL ||
299 mask == AMOTION_EVENT_ACTION_SCROLL ||
300 mask == AMOTION_EVENT_ACTION_MOVE ||
301 mask == AMOTION_EVENT_ACTION_HOVER_MOVE ||
302 mask == AMOTION_EVENT_ACTION_BUTTON_PRESS ||
303 mask == AMOTION_EVENT_ACTION_BUTTON_RELEASE ))
304 return JNI_FALSE;
306 /* make sure a subsequent AMOTION_EVENT_ACTION_UP is not treated as a touch event */
307 if (mask == AMOTION_EVENT_ACTION_BUTTON_RELEASE) state |= 0x80000000;
309 prev_state = InterlockedExchange( &button_state, state );
311 data.type = MOTION_EVENT;
312 data.motion.hwnd = LongToHandle( win );
313 data.motion.input.type = INPUT_MOUSE;
314 data.motion.input.mi.dx = x;
315 data.motion.input.mi.dy = y;
316 data.motion.input.mi.mouseData = 0;
317 data.motion.input.mi.time = 0;
318 data.motion.input.mi.dwExtraInfo = 0;
319 data.motion.input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
320 switch (action & AMOTION_EVENT_ACTION_MASK)
322 case AMOTION_EVENT_ACTION_DOWN:
323 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
324 if ((state & ~prev_state) & AMOTION_EVENT_BUTTON_PRIMARY)
325 data.motion.input.mi.dwFlags |= MOUSEEVENTF_LEFTDOWN;
326 if ((state & ~prev_state) & AMOTION_EVENT_BUTTON_SECONDARY)
327 data.motion.input.mi.dwFlags |= MOUSEEVENTF_RIGHTDOWN;
328 if ((state & ~prev_state) & AMOTION_EVENT_BUTTON_TERTIARY)
329 data.motion.input.mi.dwFlags |= MOUSEEVENTF_MIDDLEDOWN;
330 if (!(state & ~prev_state)) /* touch event */
331 data.motion.input.mi.dwFlags |= MOUSEEVENTF_LEFTDOWN;
332 break;
333 case AMOTION_EVENT_ACTION_UP:
334 case AMOTION_EVENT_ACTION_CANCEL:
335 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
336 if ((prev_state & ~state) & AMOTION_EVENT_BUTTON_PRIMARY)
337 data.motion.input.mi.dwFlags |= MOUSEEVENTF_LEFTUP;
338 if ((prev_state & ~state) & AMOTION_EVENT_BUTTON_SECONDARY)
339 data.motion.input.mi.dwFlags |= MOUSEEVENTF_RIGHTUP;
340 if ((prev_state & ~state) & AMOTION_EVENT_BUTTON_TERTIARY)
341 data.motion.input.mi.dwFlags |= MOUSEEVENTF_MIDDLEUP;
342 if (!(prev_state & ~state)) /* touch event */
343 data.motion.input.mi.dwFlags |= MOUSEEVENTF_LEFTUP;
344 break;
345 case AMOTION_EVENT_ACTION_SCROLL:
346 data.motion.input.mi.dwFlags |= MOUSEEVENTF_WHEEL;
347 data.motion.input.mi.mouseData = vscroll < 0 ? -WHEEL_DELTA : WHEEL_DELTA;
348 break;
349 case AMOTION_EVENT_ACTION_MOVE:
350 case AMOTION_EVENT_ACTION_HOVER_MOVE:
351 break;
352 default:
353 return JNI_FALSE;
355 send_event( &data );
356 return JNI_TRUE;
360 /***********************************************************************
361 * init_event_queue
363 static void init_event_queue(void)
365 HANDLE handle;
366 int ret;
368 if (pipe2( event_pipe, O_CLOEXEC | O_NONBLOCK ) == -1)
370 ERR( "could not create data\n" );
371 NtTerminateProcess( 0, 1 );
373 if (wine_server_fd_to_handle( event_pipe[0], GENERIC_READ | SYNCHRONIZE, 0, &handle ))
375 ERR( "Can't allocate handle for event fd\n" );
376 NtTerminateProcess( 0, 1 );
378 SERVER_START_REQ( set_queue_fd )
380 req->handle = wine_server_obj_handle( handle );
381 ret = wine_server_call( req );
383 SERVER_END_REQ;
384 if (ret)
386 ERR( "Can't store handle for event fd %x\n", ret );
387 NtTerminateProcess( 0, 1 );
389 NtClose( handle );
390 desktop_tid = GetCurrentThreadId();
394 /***********************************************************************
395 * pull_events
397 * Pull events from the event pipe and add them to the queue
399 static void pull_events(void)
401 struct java_event *event;
402 int res;
404 for (;;)
406 if (!(event = malloc( sizeof(*event) ))) break;
408 res = read( event_pipe[0], &event->data, sizeof(event->data) );
409 if (res != sizeof(event->data)) break;
410 list_add_tail( &event_queue, &event->entry );
412 free( event );
416 /***********************************************************************
417 * process_events
419 static int process_events( DWORD mask )
421 DPI_AWARENESS_CONTEXT context;
422 struct java_event *event, *next, *previous;
423 unsigned int count = 0;
425 assert( GetCurrentThreadId() == desktop_tid );
427 pull_events();
429 previous = current_event;
431 LIST_FOR_EACH_ENTRY_SAFE( event, next, &event_queue, struct java_event, entry )
433 switch (event->data.type)
435 case SURFACE_CHANGED:
436 break; /* always process it to unblock other threads */
437 case MOTION_EVENT:
438 if (event->data.motion.input.mi.dwFlags & (MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_RIGHTDOWN|
439 MOUSEEVENTF_MIDDLEDOWN|MOUSEEVENTF_LEFTUP|
440 MOUSEEVENTF_RIGHTUP|MOUSEEVENTF_MIDDLEUP))
442 if (mask & QS_MOUSEBUTTON) break;
444 else if (mask & QS_MOUSEMOVE) break;
445 continue; /* skip it */
446 case KEYBOARD_EVENT:
447 if (mask & QS_KEY) break;
448 continue; /* skip it */
449 default:
450 if (mask & QS_SENDMESSAGE) break;
451 continue; /* skip it */
454 /* remove it first, in case we process events recursively */
455 list_remove( &event->entry );
456 current_event = event;
458 switch (event->data.type)
460 case DESKTOP_CHANGED:
461 TRACE( "DESKTOP_CHANGED %ux%u\n", event->data.desktop.width, event->data.desktop.height );
462 context = SetThreadDpiAwarenessContext( DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE );
463 screen_width = event->data.desktop.width;
464 screen_height = event->data.desktop.height;
465 init_monitors( screen_width, screen_height );
466 NtUserSetWindowPos( NtUserGetDesktopWindow(), 0, 0, 0, screen_width, screen_height,
467 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW );
468 SetThreadDpiAwarenessContext( context );
469 break;
471 case CONFIG_CHANGED:
472 TRACE( "CONFIG_CHANGED dpi %u\n", event->data.cfg.dpi );
473 set_screen_dpi( event->data.cfg.dpi );
474 break;
476 case SURFACE_CHANGED:
477 TRACE("SURFACE_CHANGED %p %p %s size %ux%u\n", event->data.surface.hwnd,
478 event->data.surface.window, event->data.surface.client ? "client" : "whole",
479 event->data.surface.width, event->data.surface.height );
481 register_native_window( event->data.surface.hwnd, event->data.surface.window, event->data.surface.client );
482 break;
484 case MOTION_EVENT:
486 HWND capture = get_capture_window();
488 if (event->data.motion.input.mi.dwFlags & (MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_RIGHTDOWN|MOUSEEVENTF_MIDDLEDOWN))
489 TRACE( "BUTTONDOWN pos %d,%d hwnd %p flags %x\n",
490 (int)event->data.motion.input.mi.dx, (int)event->data.motion.input.mi.dy,
491 event->data.motion.hwnd, (int)event->data.motion.input.mi.dwFlags );
492 else if (event->data.motion.input.mi.dwFlags & (MOUSEEVENTF_LEFTUP|MOUSEEVENTF_RIGHTUP|MOUSEEVENTF_MIDDLEUP))
493 TRACE( "BUTTONUP pos %d,%d hwnd %p flags %x\n",
494 (int)event->data.motion.input.mi.dx, (int)event->data.motion.input.mi.dy,
495 event->data.motion.hwnd, (int)event->data.motion.input.mi.dwFlags );
496 else
497 TRACE( "MOUSEMOVE pos %d,%d hwnd %p flags %x\n",
498 (int)event->data.motion.input.mi.dx, (int)event->data.motion.input.mi.dy,
499 event->data.motion.hwnd, (int)event->data.motion.input.mi.dwFlags );
500 if (!capture && (event->data.motion.input.mi.dwFlags & MOUSEEVENTF_ABSOLUTE))
502 RECT rect;
503 SetRect( &rect, event->data.motion.input.mi.dx, event->data.motion.input.mi.dy,
504 event->data.motion.input.mi.dx + 1, event->data.motion.input.mi.dy + 1 );
506 SERVER_START_REQ( update_window_zorder )
508 req->window = wine_server_user_handle( event->data.motion.hwnd );
509 req->rect.left = rect.left;
510 req->rect.top = rect.top;
511 req->rect.right = rect.right;
512 req->rect.bottom = rect.bottom;
513 wine_server_call( req );
515 SERVER_END_REQ;
517 __wine_send_input( capture ? capture : event->data.motion.hwnd, &event->data.motion.input, NULL );
519 break;
521 case KEYBOARD_EVENT:
522 if (event->data.kbd.input.ki.dwFlags & KEYEVENTF_KEYUP)
523 TRACE("KEYUP hwnd %p vkey %x '%c' scancode %x\n", event->data.kbd.hwnd,
524 event->data.kbd.input.ki.wVk, event->data.kbd.input.ki.wVk,
525 event->data.kbd.input.ki.wScan );
526 else
527 TRACE("KEYDOWN hwnd %p vkey %x '%c' scancode %x\n", event->data.kbd.hwnd,
528 event->data.kbd.input.ki.wVk, event->data.kbd.input.ki.wVk,
529 event->data.kbd.input.ki.wScan );
530 update_keyboard_lock_state( event->data.kbd.input.ki.wVk, event->data.kbd.lock_state );
531 __wine_send_input( 0, &event->data.kbd.input, NULL );
532 break;
534 default:
535 FIXME( "got event %u\n", event->data.type );
537 free( event );
538 count++;
539 /* next may have been removed by a recursive call, so reset it to the beginning of the list */
540 next = LIST_ENTRY( event_queue.next, struct java_event, entry );
542 current_event = previous;
543 return count;
547 /***********************************************************************
548 * wait_events
550 static int wait_events( int timeout )
552 assert( GetCurrentThreadId() == desktop_tid );
554 for (;;)
556 struct pollfd pollfd;
557 int ret;
559 pollfd.fd = event_pipe[0];
560 pollfd.events = POLLIN | POLLHUP;
561 ret = poll( &pollfd, 1, timeout );
562 if (ret == -1 && errno == EINTR) continue;
563 if (ret && (pollfd.revents & (POLLHUP | POLLERR))) ret = -1;
564 return ret;
569 /* Window surface support */
571 struct android_window_surface
573 struct window_surface header;
574 HWND hwnd;
575 ANativeWindow *window;
576 RECT bounds;
577 BOOL byteswap;
578 RGNDATA *region_data;
579 HRGN region;
580 BYTE alpha;
581 COLORREF color_key;
582 void *bits;
583 pthread_mutex_t mutex;
584 BITMAPINFO info; /* variable size, must be last */
587 static struct android_window_surface *get_android_surface( struct window_surface *surface )
589 return (struct android_window_surface *)surface;
592 static inline void reset_bounds( RECT *bounds )
594 bounds->left = bounds->top = INT_MAX;
595 bounds->right = bounds->bottom = INT_MIN;
598 static inline void add_bounds_rect( RECT *bounds, const RECT *rect )
600 if (rect->left >= rect->right || rect->top >= rect->bottom) return;
601 bounds->left = min( bounds->left, rect->left );
602 bounds->top = min( bounds->top, rect->top );
603 bounds->right = max( bounds->right, rect->right );
604 bounds->bottom = max( bounds->bottom, rect->bottom );
607 /* store the palette or color mask data in the bitmap info structure */
608 static void set_color_info( BITMAPINFO *info, BOOL has_alpha )
610 DWORD *colors = (DWORD *)info->bmiColors;
612 info->bmiHeader.biSize = sizeof(info->bmiHeader);
613 info->bmiHeader.biClrUsed = 0;
614 info->bmiHeader.biBitCount = 32;
615 if (has_alpha)
617 info->bmiHeader.biCompression = BI_RGB;
618 return;
620 info->bmiHeader.biCompression = BI_BITFIELDS;
621 colors[0] = 0xff0000;
622 colors[1] = 0x00ff00;
623 colors[2] = 0x0000ff;
626 /* apply the window region to a single line of the destination image. */
627 static void apply_line_region( DWORD *dst, int width, int x, int y, const RECT *rect, const RECT *end )
629 while (rect < end && rect->top <= y && width > 0)
631 if (rect->left > x)
633 memset( dst, 0, min( rect->left - x, width ) * sizeof(*dst) );
634 dst += rect->left - x;
635 width -= rect->left - x;
636 x = rect->left;
638 if (rect->right > x)
640 dst += rect->right - x;
641 width -= rect->right - x;
642 x = rect->right;
644 rect++;
646 if (width > 0) memset( dst, 0, width * sizeof(*dst) );
649 /***********************************************************************
650 * android_surface_lock
652 static void android_surface_lock( struct window_surface *window_surface )
654 struct android_window_surface *surface = get_android_surface( window_surface );
656 pthread_mutex_lock( &surface->mutex );
659 /***********************************************************************
660 * android_surface_unlock
662 static void android_surface_unlock( struct window_surface *window_surface )
664 struct android_window_surface *surface = get_android_surface( window_surface );
666 pthread_mutex_unlock( &surface->mutex );
669 /***********************************************************************
670 * android_surface_get_bitmap_info
672 static void *android_surface_get_bitmap_info( struct window_surface *window_surface, BITMAPINFO *info )
674 struct android_window_surface *surface = get_android_surface( window_surface );
676 memcpy( info, &surface->info, get_dib_info_size( &surface->info, DIB_RGB_COLORS ));
677 return surface->bits;
680 /***********************************************************************
681 * android_surface_get_bounds
683 static RECT *android_surface_get_bounds( struct window_surface *window_surface )
685 struct android_window_surface *surface = get_android_surface( window_surface );
687 return &surface->bounds;
690 /***********************************************************************
691 * android_surface_set_region
693 static void android_surface_set_region( struct window_surface *window_surface, HRGN region )
695 struct android_window_surface *surface = get_android_surface( window_surface );
697 TRACE( "updating surface %p hwnd %p with %p\n", surface, surface->hwnd, region );
699 window_surface->funcs->lock( window_surface );
700 if (!region)
702 if (surface->region) NtGdiDeleteObjectApp( surface->region );
703 surface->region = 0;
705 else
707 if (!surface->region) surface->region = NtGdiCreateRectRgn( 0, 0, 0, 0 );
708 NtGdiCombineRgn( surface->region, region, 0, RGN_COPY );
710 window_surface->funcs->unlock( window_surface );
711 set_surface_region( &surface->header, (HRGN)1 );
714 /***********************************************************************
715 * android_surface_flush
717 static void android_surface_flush( struct window_surface *window_surface )
719 struct android_window_surface *surface = get_android_surface( window_surface );
720 ANativeWindow_Buffer buffer;
721 ARect rc;
722 RECT rect;
723 BOOL needs_flush;
725 window_surface->funcs->lock( window_surface );
726 SetRect( &rect, 0, 0, surface->header.rect.right - surface->header.rect.left,
727 surface->header.rect.bottom - surface->header.rect.top );
728 needs_flush = intersect_rect( &rect, &rect, &surface->bounds );
729 reset_bounds( &surface->bounds );
730 window_surface->funcs->unlock( window_surface );
731 if (!needs_flush) return;
733 TRACE( "flushing %p hwnd %p surface %s rect %s bits %p alpha %02x key %08x region %u rects\n",
734 surface, surface->hwnd, wine_dbgstr_rect( &surface->header.rect ),
735 wine_dbgstr_rect( &rect ), surface->bits, surface->alpha, (int)surface->color_key,
736 surface->region_data ? (int)surface->region_data->rdh.nCount : 0 );
738 rc.left = rect.left;
739 rc.top = rect.top;
740 rc.right = rect.right;
741 rc.bottom = rect.bottom;
743 if (!surface->window->perform( surface->window, NATIVE_WINDOW_LOCK, &buffer, &rc ))
745 const RECT *rgn_rect = NULL, *end = NULL;
746 unsigned int *src, *dst;
747 int x, y, width;
749 rect.left = rc.left;
750 rect.top = rc.top;
751 rect.right = rc.right;
752 rect.bottom = rc.bottom;
753 intersect_rect( &rect, &rect, &surface->header.rect );
755 if (surface->region_data)
757 rgn_rect = (RECT *)surface->region_data->Buffer;
758 end = rgn_rect + surface->region_data->rdh.nCount;
760 src = (unsigned int *)surface->bits
761 + (rect.top - surface->header.rect.top) * surface->info.bmiHeader.biWidth
762 + (rect.left - surface->header.rect.left);
763 dst = (unsigned int *)buffer.bits + rect.top * buffer.stride + rect.left;
764 width = min( rect.right - rect.left, buffer.stride );
766 for (y = rect.top; y < min( buffer.height, rect.bottom); y++)
768 if (surface->info.bmiHeader.biCompression == BI_RGB)
769 memcpy( dst, src, width * sizeof(*dst) );
770 else if (surface->alpha == 255)
771 for (x = 0; x < width; x++) dst[x] = src[x] | 0xff000000;
772 else
773 for (x = 0; x < width; x++)
774 dst[x] = ((surface->alpha << 24) |
775 (((BYTE)(src[x] >> 16) * surface->alpha / 255) << 16) |
776 (((BYTE)(src[x] >> 8) * surface->alpha / 255) << 8) |
777 (((BYTE)src[x] * surface->alpha / 255)));
779 if (surface->color_key != CLR_INVALID)
780 for (x = 0; x < width; x++) if ((src[x] & 0xffffff) == surface->color_key) dst[x] = 0;
782 if (rgn_rect)
784 while (rgn_rect < end && rgn_rect->bottom <= y) rgn_rect++;
785 apply_line_region( dst, width, rect.left, y, rgn_rect, end );
788 src += surface->info.bmiHeader.biWidth;
789 dst += buffer.stride;
791 surface->window->perform( surface->window, NATIVE_WINDOW_UNLOCK_AND_POST );
793 else TRACE( "Unable to lock surface %p window %p buffer %p\n",
794 surface, surface->hwnd, surface->window );
797 /***********************************************************************
798 * android_surface_destroy
800 static void android_surface_destroy( struct window_surface *window_surface )
802 struct android_window_surface *surface = get_android_surface( window_surface );
804 TRACE( "freeing %p bits %p\n", surface, surface->bits );
806 free( surface->region_data );
807 if (surface->region) NtGdiDeleteObjectApp( surface->region );
808 release_ioctl_window( surface->window );
809 free( surface->bits );
810 free( surface );
813 static const struct window_surface_funcs android_surface_funcs =
815 android_surface_lock,
816 android_surface_unlock,
817 android_surface_get_bitmap_info,
818 android_surface_get_bounds,
819 android_surface_set_region,
820 android_surface_flush,
821 android_surface_destroy
824 static BOOL is_argb_surface( struct window_surface *surface )
826 return surface && surface->funcs == &android_surface_funcs &&
827 get_android_surface( surface )->info.bmiHeader.biCompression == BI_RGB;
830 /***********************************************************************
831 * set_color_key
833 static void set_color_key( struct android_window_surface *surface, COLORREF key )
835 if (key == CLR_INVALID)
836 surface->color_key = CLR_INVALID;
837 else if (surface->info.bmiHeader.biBitCount <= 8)
838 surface->color_key = CLR_INVALID;
839 else if (key & (1 << 24)) /* PALETTEINDEX */
840 surface->color_key = 0;
841 else if (key >> 16 == 0x10ff) /* DIBINDEX */
842 surface->color_key = 0;
843 else if (surface->info.bmiHeader.biBitCount == 24)
844 surface->color_key = key;
845 else
846 surface->color_key = (GetRValue(key) << 16) | (GetGValue(key) << 8) | GetBValue(key);
849 /***********************************************************************
850 * set_surface_region
852 static void set_surface_region( struct window_surface *window_surface, HRGN win_region )
854 struct android_window_surface *surface = get_android_surface( window_surface );
855 struct android_win_data *win_data;
856 HRGN region = win_region;
857 RGNDATA *data = NULL;
858 DWORD size;
859 int offset_x, offset_y;
861 if (window_surface->funcs != &android_surface_funcs) return; /* we may get the null surface */
863 if (!(win_data = get_win_data( surface->hwnd ))) return;
864 offset_x = win_data->window_rect.left - win_data->whole_rect.left;
865 offset_y = win_data->window_rect.top - win_data->whole_rect.top;
866 release_win_data( win_data );
868 if (win_region == (HRGN)1) /* hack: win_region == 1 means retrieve region from server */
870 region = NtGdiCreateRectRgn( 0, 0, win_data->window_rect.right - win_data->window_rect.left,
871 win_data->window_rect.bottom - win_data->window_rect.top );
872 if (NtUserGetWindowRgnEx( surface->hwnd, region, 0 ) == ERROR && !surface->region) goto done;
875 NtGdiOffsetRgn( region, offset_x, offset_y );
876 if (surface->region) NtGdiCombineRgn( region, region, surface->region, RGN_AND );
878 if (!(size = NtGdiGetRegionData( region, 0, NULL ))) goto done;
879 if (!(data = malloc( size ))) goto done;
881 if (!NtGdiGetRegionData( region, size, data ))
883 free( data );
884 data = NULL;
887 done:
888 window_surface->funcs->lock( window_surface );
889 free( surface->region_data );
890 surface->region_data = data;
891 *window_surface->funcs->get_bounds( window_surface ) = surface->header.rect;
892 window_surface->funcs->unlock( window_surface );
893 if (region != win_region) NtGdiDeleteObjectApp( region );
896 /***********************************************************************
897 * create_surface
899 static struct window_surface *create_surface( HWND hwnd, const RECT *rect,
900 BYTE alpha, COLORREF color_key, BOOL src_alpha )
902 struct android_window_surface *surface;
903 int width = rect->right - rect->left, height = rect->bottom - rect->top;
904 pthread_mutexattr_t attr;
906 surface = calloc( 1, FIELD_OFFSET( struct android_window_surface, info.bmiColors[3] ));
907 if (!surface) return NULL;
908 set_color_info( &surface->info, src_alpha );
909 surface->info.bmiHeader.biWidth = width;
910 surface->info.bmiHeader.biHeight = -height; /* top-down */
911 surface->info.bmiHeader.biPlanes = 1;
912 surface->info.bmiHeader.biSizeImage = get_dib_image_size( &surface->info );
914 pthread_mutexattr_init( &attr );
915 pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE );
916 pthread_mutex_init( &surface->mutex, &attr );
917 pthread_mutexattr_destroy( &attr );
919 surface->header.funcs = &android_surface_funcs;
920 surface->header.rect = *rect;
921 surface->header.ref = 1;
922 surface->hwnd = hwnd;
923 surface->window = get_ioctl_window( hwnd );
924 surface->alpha = alpha;
925 set_color_key( surface, color_key );
926 set_surface_region( &surface->header, (HRGN)1 );
927 reset_bounds( &surface->bounds );
929 if (!(surface->bits = malloc( surface->info.bmiHeader.biSizeImage )))
930 goto failed;
932 TRACE( "created %p hwnd %p %s bits %p-%p\n", surface, hwnd, wine_dbgstr_rect(rect),
933 surface->bits, (char *)surface->bits + surface->info.bmiHeader.biSizeImage );
935 return &surface->header;
937 failed:
938 android_surface_destroy( &surface->header );
939 return NULL;
942 /***********************************************************************
943 * set_surface_layered
945 static void set_surface_layered( struct window_surface *window_surface, BYTE alpha, COLORREF color_key )
947 struct android_window_surface *surface = get_android_surface( window_surface );
948 COLORREF prev_key;
949 BYTE prev_alpha;
951 if (window_surface->funcs != &android_surface_funcs) return; /* we may get the null surface */
953 window_surface->funcs->lock( window_surface );
954 prev_key = surface->color_key;
955 prev_alpha = surface->alpha;
956 surface->alpha = alpha;
957 set_color_key( surface, color_key );
958 if (alpha != prev_alpha || surface->color_key != prev_key) /* refresh */
959 *window_surface->funcs->get_bounds( window_surface ) = surface->header.rect;
960 window_surface->funcs->unlock( window_surface );
963 /***********************************************************************
964 * get_mono_icon_argb
966 * Return a monochrome icon/cursor bitmap bits in ARGB format.
968 static unsigned int *get_mono_icon_argb( HDC hdc, HBITMAP bmp, unsigned int *width, unsigned int *height )
970 BITMAP bm;
971 char *mask;
972 unsigned int i, j, stride, mask_size, bits_size, *bits = NULL, *ptr;
974 if (!NtGdiExtGetObjectW( bmp, sizeof(bm), &bm )) return NULL;
975 stride = ((bm.bmWidth + 15) >> 3) & ~1;
976 mask_size = stride * bm.bmHeight;
977 if (!(mask = malloc( mask_size ))) return NULL;
978 if (!NtGdiGetBitmapBits( bmp, mask_size, mask )) goto done;
980 bm.bmHeight /= 2;
981 bits_size = bm.bmWidth * bm.bmHeight * sizeof(*bits);
982 if (!(bits = malloc( bits_size ))) goto done;
984 ptr = bits;
985 for (i = 0; i < bm.bmHeight; i++)
986 for (j = 0; j < bm.bmWidth; j++, ptr++)
988 int and = ((mask[i * stride + j / 8] << (j % 8)) & 0x80);
989 int xor = ((mask[(i + bm.bmHeight) * stride + j / 8] << (j % 8)) & 0x80);
990 if (!xor && and)
991 *ptr = 0;
992 else if (xor && !and)
993 *ptr = 0xffffffff;
994 else
995 /* we can't draw "invert" pixels, so render them as black instead */
996 *ptr = 0xff000000;
999 *width = bm.bmWidth;
1000 *height = bm.bmHeight;
1002 done:
1003 free( mask );
1004 return bits;
1007 /***********************************************************************
1008 * get_bitmap_argb
1010 * Return the bitmap bits in ARGB format. Helper for setting icons and cursors.
1012 static unsigned int *get_bitmap_argb( HDC hdc, HBITMAP color, HBITMAP mask, unsigned int *width,
1013 unsigned int *height )
1015 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1016 BITMAPINFO *info = (BITMAPINFO *)buffer;
1017 BITMAP bm;
1018 unsigned int *ptr, *bits = NULL;
1019 unsigned char *mask_bits = NULL;
1020 int i, j;
1021 BOOL has_alpha = FALSE;
1023 if (!color) return get_mono_icon_argb( hdc, mask, width, height );
1025 if (!NtGdiExtGetObjectW( color, sizeof(bm), &bm )) return NULL;
1026 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1027 info->bmiHeader.biWidth = bm.bmWidth;
1028 info->bmiHeader.biHeight = -bm.bmHeight;
1029 info->bmiHeader.biPlanes = 1;
1030 info->bmiHeader.biBitCount = 32;
1031 info->bmiHeader.biCompression = BI_RGB;
1032 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
1033 info->bmiHeader.biXPelsPerMeter = 0;
1034 info->bmiHeader.biYPelsPerMeter = 0;
1035 info->bmiHeader.biClrUsed = 0;
1036 info->bmiHeader.biClrImportant = 0;
1037 if (!(bits = malloc( bm.bmWidth * bm.bmHeight * sizeof(unsigned int) )))
1038 goto failed;
1039 if (!NtGdiGetDIBitsInternal( hdc, color, 0, bm.bmHeight, bits, info, DIB_RGB_COLORS, 0, 0 ))
1040 goto failed;
1042 *width = bm.bmWidth;
1043 *height = bm.bmHeight;
1045 for (i = 0; i < bm.bmWidth * bm.bmHeight; i++)
1046 if ((has_alpha = (bits[i] & 0xff000000) != 0)) break;
1048 if (!has_alpha)
1050 unsigned int width_bytes = (bm.bmWidth + 31) / 32 * 4;
1051 /* generate alpha channel from the mask */
1052 info->bmiHeader.biBitCount = 1;
1053 info->bmiHeader.biSizeImage = width_bytes * bm.bmHeight;
1054 if (!(mask_bits = malloc( info->bmiHeader.biSizeImage ))) goto failed;
1055 if (!NtGdiGetDIBitsInternal( hdc, mask, 0, bm.bmHeight, mask_bits, info, DIB_RGB_COLORS, 0, 0 ))
1056 goto failed;
1057 ptr = bits;
1058 for (i = 0; i < bm.bmHeight; i++)
1059 for (j = 0; j < bm.bmWidth; j++, ptr++)
1060 if (!((mask_bits[i * width_bytes + j / 8] << (j % 8)) & 0x80)) *ptr |= 0xff000000;
1061 free( mask_bits );
1064 return bits;
1066 failed:
1067 free( bits );
1068 free( mask_bits );
1069 *width = *height = 0;
1070 return NULL;
1074 enum android_system_cursors
1076 TYPE_ARROW = 1000,
1077 TYPE_CONTEXT_MENU = 1001,
1078 TYPE_HAND = 1002,
1079 TYPE_HELP = 1003,
1080 TYPE_WAIT = 1004,
1081 TYPE_CELL = 1006,
1082 TYPE_CROSSHAIR = 1007,
1083 TYPE_TEXT = 1008,
1084 TYPE_VERTICAL_TEXT = 1009,
1085 TYPE_ALIAS = 1010,
1086 TYPE_COPY = 1011,
1087 TYPE_NO_DROP = 1012,
1088 TYPE_ALL_SCROLL = 1013,
1089 TYPE_HORIZONTAL_DOUBLE_ARROW = 1014,
1090 TYPE_VERTICAL_DOUBLE_ARROW = 1015,
1091 TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW = 1016,
1092 TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW = 1017,
1093 TYPE_ZOOM_IN = 1018,
1094 TYPE_ZOOM_OUT = 1019,
1095 TYPE_GRAB = 1020,
1096 TYPE_GRABBING = 1021,
1099 struct system_cursors
1101 WORD id;
1102 enum android_system_cursors android_id;
1105 static const struct system_cursors user32_cursors[] =
1107 { OCR_NORMAL, TYPE_ARROW },
1108 { OCR_IBEAM, TYPE_TEXT },
1109 { OCR_WAIT, TYPE_WAIT },
1110 { OCR_CROSS, TYPE_CROSSHAIR },
1111 { OCR_SIZE, TYPE_ALL_SCROLL },
1112 { OCR_SIZEALL, TYPE_ALL_SCROLL },
1113 { OCR_SIZENWSE, TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW },
1114 { OCR_SIZENESW, TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW },
1115 { OCR_SIZEWE, TYPE_HORIZONTAL_DOUBLE_ARROW },
1116 { OCR_SIZENS, TYPE_VERTICAL_DOUBLE_ARROW },
1117 { OCR_NO, TYPE_NO_DROP },
1118 { OCR_HAND, TYPE_HAND },
1119 { OCR_HELP, TYPE_HELP },
1120 { 0 }
1123 static const struct system_cursors comctl32_cursors[] =
1125 /* 102 TYPE_MOVE doesn't exist */
1126 { 104, TYPE_COPY },
1127 { 105, TYPE_ARROW },
1128 { 106, TYPE_HORIZONTAL_DOUBLE_ARROW },
1129 { 107, TYPE_HORIZONTAL_DOUBLE_ARROW },
1130 { 108, TYPE_GRABBING },
1131 { 135, TYPE_VERTICAL_DOUBLE_ARROW },
1132 { 0 }
1135 static const struct system_cursors ole32_cursors[] =
1137 { 1, TYPE_NO_DROP },
1138 /* 2 TYPE_MOVE doesn't exist */
1139 { 3, TYPE_COPY },
1140 { 4, TYPE_ALIAS },
1141 { 0 }
1144 static const struct system_cursors riched20_cursors[] =
1146 { 105, TYPE_GRABBING },
1147 { 109, TYPE_COPY },
1148 /* 110 TYPE_MOVE doesn't exist */
1149 { 111, TYPE_NO_DROP },
1150 { 0 }
1153 static const struct
1155 const struct system_cursors *cursors;
1156 WCHAR name[16];
1157 } module_cursors[] =
1159 { user32_cursors, {'u','s','e','r','3','2','.','d','l','l',0} },
1160 { comctl32_cursors, {'c','o','m','c','t','l','3','2','.','d','l','l',0} },
1161 { ole32_cursors, {'o','l','e','3','2','.','d','l','l',0} },
1162 { riched20_cursors, {'r','i','c','h','e','d','2','0','.','d','l','l',0} }
1165 static int get_cursor_system_id( const ICONINFOEXW *info )
1167 const struct system_cursors *cursors;
1168 const WCHAR *module;
1169 unsigned int i;
1171 if (info->szResName[0]) return 0; /* only integer resources are supported here */
1173 if ((module = wcsrchr( info->szModName, '\\' ))) module++;
1174 else module = info->szModName;
1175 for (i = 0; i < ARRAY_SIZE( module_cursors ); i++)
1176 if (!wcsicmp( module, module_cursors[i].name )) break;
1177 if (i == ARRAY_SIZE( module_cursors )) return 0;
1179 cursors = module_cursors[i].cursors;
1180 for (i = 0; cursors[i].id; i++)
1181 if (cursors[i].id == info->wResID) return cursors[i].android_id;
1183 return 0;
1187 LRESULT ANDROID_DesktopWindowProc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
1189 switch (msg)
1191 case WM_PARENTNOTIFY:
1192 if (LOWORD(wp) == WM_DESTROY) destroy_ioctl_window( (HWND)lp, FALSE );
1193 break;
1195 return NtUserMessageCall( hwnd, msg, wp, lp, 0, NtUserDefWindowProc, FALSE );
1199 /***********************************************************************
1200 * ANDROID_ProcessEvents
1202 BOOL ANDROID_ProcessEvents( DWORD mask )
1204 if (GetCurrentThreadId() == desktop_tid)
1206 /* don't process nested events */
1207 if (current_event) mask = 0;
1208 return process_events( mask );
1210 return FALSE;
1213 /**********************************************************************
1214 * ANDROID_CreateWindow
1216 BOOL ANDROID_CreateWindow( HWND hwnd )
1218 TRACE( "%p\n", hwnd );
1220 if (hwnd == NtUserGetDesktopWindow())
1222 struct android_win_data *data;
1224 init_event_queue();
1225 start_android_device();
1226 if (!(data = alloc_win_data( hwnd ))) return FALSE;
1227 release_win_data( data );
1229 return TRUE;
1233 /***********************************************************************
1234 * ANDROID_DestroyWindow
1236 void ANDROID_DestroyWindow( HWND hwnd )
1238 struct android_win_data *data;
1240 if (!(data = get_win_data( hwnd ))) return;
1242 if (data->surface) window_surface_release( data->surface );
1243 data->surface = NULL;
1244 destroy_gl_drawable( hwnd );
1245 free_win_data( data );
1249 /***********************************************************************
1250 * create_win_data
1252 * Create a data window structure for an existing window.
1254 static struct android_win_data *create_win_data( HWND hwnd, const RECT *window_rect,
1255 const RECT *client_rect )
1257 struct android_win_data *data;
1258 HWND parent;
1260 if (!(parent = NtUserGetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop or HWND_MESSAGE */
1262 if (!(data = alloc_win_data( hwnd ))) return NULL;
1264 data->parent = (parent == NtUserGetDesktopWindow()) ? 0 : parent;
1265 data->whole_rect = data->window_rect = *window_rect;
1266 data->client_rect = *client_rect;
1267 return data;
1271 static inline BOOL get_surface_rect( const RECT *visible_rect, RECT *surface_rect )
1273 if (!intersect_rect( surface_rect, visible_rect, &virtual_screen_rect )) return FALSE;
1274 OffsetRect( surface_rect, -visible_rect->left, -visible_rect->top );
1275 surface_rect->left &= ~31;
1276 surface_rect->top &= ~31;
1277 surface_rect->right = max( surface_rect->left + 32, (surface_rect->right + 31) & ~31 );
1278 surface_rect->bottom = max( surface_rect->top + 32, (surface_rect->bottom + 31) & ~31 );
1279 return TRUE;
1283 /***********************************************************************
1284 * ANDROID_WindowPosChanging
1286 BOOL ANDROID_WindowPosChanging( HWND hwnd, HWND insert_after, UINT swp_flags,
1287 const RECT *window_rect, const RECT *client_rect, RECT *visible_rect,
1288 struct window_surface **surface )
1290 struct android_win_data *data = get_win_data( hwnd );
1291 RECT surface_rect;
1292 DWORD flags;
1293 COLORREF key;
1294 BYTE alpha;
1295 BOOL layered = NtUserGetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED;
1297 TRACE( "win %p window %s client %s style %08x flags %08x\n",
1298 hwnd, wine_dbgstr_rect(window_rect), wine_dbgstr_rect(client_rect),
1299 (int)NtUserGetWindowLongW( hwnd, GWL_STYLE ), swp_flags );
1301 if (!data && !(data = create_win_data( hwnd, window_rect, client_rect ))) return TRUE;
1303 *visible_rect = *window_rect;
1305 /* create the window surface if necessary */
1307 if (data->parent) goto done;
1308 if (swp_flags & SWP_HIDEWINDOW) goto done;
1309 if (is_argb_surface( data->surface )) goto done;
1310 if (!get_surface_rect( visible_rect, &surface_rect )) goto done;
1312 if (data->surface)
1314 if (!memcmp( &data->surface->rect, &surface_rect, sizeof(surface_rect) ))
1316 /* existing surface is good enough */
1317 window_surface_add_ref( data->surface );
1318 if (*surface) window_surface_release( *surface );
1319 *surface = data->surface;
1320 goto done;
1323 if (!(swp_flags & SWP_SHOWWINDOW) && !(NtUserGetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
1324 goto done;
1326 if (!layered || !NtUserGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags )) flags = 0;
1327 if (!(flags & LWA_ALPHA)) alpha = 255;
1328 if (!(flags & LWA_COLORKEY)) key = CLR_INVALID;
1330 if (*surface) window_surface_release( *surface );
1331 *surface = create_surface( data->hwnd, &surface_rect, alpha, key, FALSE );
1333 done:
1334 release_win_data( data );
1335 return TRUE;
1339 /***********************************************************************
1340 * ANDROID_WindowPosChanged
1342 void ANDROID_WindowPosChanged( HWND hwnd, HWND insert_after, UINT swp_flags,
1343 const RECT *window_rect, const RECT *client_rect,
1344 const RECT *visible_rect, const RECT *valid_rects,
1345 struct window_surface *surface )
1347 struct android_win_data *data;
1348 UINT new_style = NtUserGetWindowLongW( hwnd, GWL_STYLE );
1349 HWND owner = 0;
1351 if (!(data = get_win_data( hwnd ))) return;
1353 data->window_rect = *window_rect;
1354 data->whole_rect = *visible_rect;
1355 data->client_rect = *client_rect;
1357 if (!is_argb_surface( data->surface ))
1359 if (surface) window_surface_add_ref( surface );
1360 if (data->surface) window_surface_release( data->surface );
1361 data->surface = surface;
1363 if (!data->parent) owner = NtUserGetWindowRelative( hwnd, GW_OWNER );
1364 release_win_data( data );
1366 if (!(swp_flags & SWP_NOZORDER)) insert_after = NtUserGetWindowRelative( hwnd, GW_HWNDPREV );
1368 TRACE( "win %p window %s client %s style %08x owner %p after %p flags %08x\n", hwnd,
1369 wine_dbgstr_rect(window_rect), wine_dbgstr_rect(client_rect),
1370 new_style, owner, insert_after, swp_flags );
1372 ioctl_window_pos_changed( hwnd, window_rect, client_rect, visible_rect,
1373 new_style, swp_flags, insert_after, owner );
1377 /***********************************************************************
1378 * ANDROID_ShowWindow
1380 UINT ANDROID_ShowWindow( HWND hwnd, INT cmd, RECT *rect, UINT swp )
1382 if (!(NtUserGetWindowLongW( hwnd, GWL_STYLE ) & WS_MINIMIZE)) return swp;
1383 /* always hide icons off-screen */
1384 if (rect->left != -32000 || rect->top != -32000)
1386 OffsetRect( rect, -32000 - rect->left, -32000 - rect->top );
1387 swp &= ~(SWP_NOMOVE | SWP_NOCLIENTMOVE);
1389 return swp;
1393 /*****************************************************************
1394 * ANDROID_SetParent
1396 void ANDROID_SetParent( HWND hwnd, HWND parent, HWND old_parent )
1398 struct android_win_data *data;
1400 if (parent == old_parent) return;
1401 if (!(data = get_win_data( hwnd ))) return;
1403 TRACE( "win %p parent %p -> %p\n", hwnd, old_parent, parent );
1405 data->parent = (parent == NtUserGetDesktopWindow()) ? 0 : parent;
1406 ioctl_set_window_parent( hwnd, parent, (float)get_win_monitor_dpi( hwnd ) / NtUserGetDpiForWindow( hwnd ));
1407 release_win_data( data );
1411 /***********************************************************************
1412 * ANDROID_SetCapture
1414 void ANDROID_SetCapture( HWND hwnd, UINT flags )
1416 if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
1417 ioctl_set_capture( hwnd );
1421 static BOOL get_icon_info( HICON handle, ICONINFOEXW *ret )
1423 UNICODE_STRING module, res_name;
1424 ICONINFO info;
1426 module.Buffer = ret->szModName;
1427 module.MaximumLength = sizeof(ret->szModName) - sizeof(WCHAR);
1428 res_name.Buffer = ret->szResName;
1429 res_name.MaximumLength = sizeof(ret->szResName) - sizeof(WCHAR);
1430 if (!NtUserGetIconInfo( handle, &info, &module, &res_name, NULL, 0 )) return FALSE;
1431 ret->fIcon = info.fIcon;
1432 ret->xHotspot = info.xHotspot;
1433 ret->yHotspot = info.yHotspot;
1434 ret->hbmColor = info.hbmColor;
1435 ret->hbmMask = info.hbmMask;
1436 ret->wResID = res_name.Length ? 0 : LOWORD(res_name.Buffer);
1437 ret->szModName[module.Length] = 0;
1438 ret->szResName[res_name.Length] = 0;
1439 return TRUE;
1443 /***********************************************************************
1444 * ANDROID_SetCursor
1446 void ANDROID_SetCursor( HWND hwnd, HCURSOR handle )
1448 if (handle)
1450 unsigned int width = 0, height = 0, *bits = NULL;
1451 ICONINFOEXW info;
1452 int id;
1454 if (!get_icon_info( handle, &info )) return;
1456 if (!(id = get_cursor_system_id( &info )))
1458 HDC hdc = NtGdiCreateCompatibleDC( 0 );
1459 bits = get_bitmap_argb( hdc, info.hbmColor, info.hbmMask, &width, &height );
1460 NtGdiDeleteObjectApp( hdc );
1462 /* make sure hotspot is valid */
1463 if (info.xHotspot >= width || info.yHotspot >= height)
1465 info.xHotspot = width / 2;
1466 info.yHotspot = height / 2;
1469 ioctl_set_cursor( id, width, height, info.xHotspot, info.yHotspot, bits );
1470 free( bits );
1471 NtGdiDeleteObjectApp( info.hbmColor );
1472 NtGdiDeleteObjectApp( info.hbmMask );
1474 else ioctl_set_cursor( 0, 0, 0, 0, 0, NULL );
1478 /***********************************************************************
1479 * ANDROID_SetWindowStyle
1481 void ANDROID_SetWindowStyle( HWND hwnd, INT offset, STYLESTRUCT *style )
1483 struct android_win_data *data;
1484 DWORD changed = style->styleNew ^ style->styleOld;
1486 if (hwnd == NtUserGetDesktopWindow()) return;
1487 if (!(data = get_win_data( hwnd ))) return;
1489 if (offset == GWL_EXSTYLE && (changed & WS_EX_LAYERED)) /* changing WS_EX_LAYERED resets attributes */
1491 if (is_argb_surface( data->surface ))
1493 if (data->surface) window_surface_release( data->surface );
1494 data->surface = NULL;
1496 else if (data->surface) set_surface_layered( data->surface, 255, CLR_INVALID );
1498 release_win_data( data );
1502 /***********************************************************************
1503 * ANDROID_SetWindowRgn
1505 void ANDROID_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1507 struct android_win_data *data;
1509 if ((data = get_win_data( hwnd )))
1511 if (data->surface) set_surface_region( data->surface, hrgn );
1512 release_win_data( data );
1514 else FIXME( "not supported on other process window %p\n", hwnd );
1518 /***********************************************************************
1519 * ANDROID_SetLayeredWindowAttributes
1521 void ANDROID_SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
1523 struct android_win_data *data;
1525 if (!(flags & LWA_ALPHA)) alpha = 255;
1526 if (!(flags & LWA_COLORKEY)) key = CLR_INVALID;
1528 if ((data = get_win_data( hwnd )))
1530 if (data->surface) set_surface_layered( data->surface, alpha, key );
1531 release_win_data( data );
1536 /*****************************************************************************
1537 * ANDROID_UpdateLayeredWindow
1539 BOOL ANDROID_UpdateLayeredWindow( HWND hwnd, const UPDATELAYEREDWINDOWINFO *info,
1540 const RECT *window_rect )
1542 struct window_surface *surface;
1543 struct android_win_data *data;
1544 BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, 0 };
1545 COLORREF color_key = (info->dwFlags & ULW_COLORKEY) ? info->crKey : CLR_INVALID;
1546 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1547 BITMAPINFO *bmi = (BITMAPINFO *)buffer;
1548 void *src_bits, *dst_bits;
1549 RECT rect, src_rect;
1550 HDC hdc = 0;
1551 HBITMAP dib;
1552 BOOL ret = FALSE;
1554 if (!(data = get_win_data( hwnd ))) return FALSE;
1556 rect = *window_rect;
1557 OffsetRect( &rect, -window_rect->left, -window_rect->top );
1559 surface = data->surface;
1560 if (!is_argb_surface( surface ))
1562 if (surface) window_surface_release( surface );
1563 surface = NULL;
1566 if (!surface || !EqualRect( &surface->rect, &rect ))
1568 data->surface = create_surface( data->hwnd, &rect, 255, color_key, TRUE );
1569 if (surface) window_surface_release( surface );
1570 surface = data->surface;
1572 else set_surface_layered( surface, 255, color_key );
1574 if (surface) window_surface_add_ref( surface );
1575 release_win_data( data );
1577 if (!surface) return FALSE;
1578 if (!info->hdcSrc)
1580 window_surface_release( surface );
1581 return TRUE;
1584 dst_bits = surface->funcs->get_info( surface, bmi );
1586 if (!(dib = NtGdiCreateDIBSection( info->hdcDst, NULL, 0, bmi, DIB_RGB_COLORS, 0, 0, 0, &src_bits )))
1587 goto done;
1588 if (!(hdc = NtGdiCreateCompatibleDC( 0 ))) goto done;
1590 NtGdiSelectBitmap( hdc, dib );
1592 surface->funcs->lock( surface );
1594 if (info->prcDirty)
1596 intersect_rect( &rect, &rect, info->prcDirty );
1597 memcpy( src_bits, dst_bits, bmi->bmiHeader.biSizeImage );
1598 NtGdiPatBlt( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, BLACKNESS );
1600 src_rect = rect;
1601 if (info->pptSrc) OffsetRect( &src_rect, info->pptSrc->x, info->pptSrc->y );
1602 NtGdiTransformPoints( info->hdcSrc, (POINT *)&src_rect, (POINT *)&src_rect, 2, NtGdiDPtoLP );
1604 if (info->dwFlags & ULW_ALPHA) blend = *info->pblend;
1605 ret = NtGdiAlphaBlend( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
1606 info->hdcSrc, src_rect.left, src_rect.top,
1607 src_rect.right - src_rect.left, src_rect.bottom - src_rect.top,
1608 *(DWORD *)&blend, 0 );
1609 if (ret)
1611 memcpy( dst_bits, src_bits, bmi->bmiHeader.biSizeImage );
1612 add_bounds_rect( surface->funcs->get_bounds( surface ), &rect );
1615 surface->funcs->unlock( surface );
1616 surface->funcs->flush( surface );
1618 done:
1619 window_surface_release( surface );
1620 if (hdc) NtGdiDeleteObjectApp( hdc );
1621 if (dib) NtGdiDeleteObjectApp( dib );
1622 return ret;
1626 /**********************************************************************
1627 * ANDROID_WindowMessage
1629 LRESULT ANDROID_WindowMessage( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
1631 struct android_win_data *data;
1633 switch (msg)
1635 case WM_ANDROID_REFRESH:
1636 if (wp) /* opengl client window */
1638 update_gl_drawable( hwnd );
1640 else if ((data = get_win_data( hwnd )))
1642 struct window_surface *surface = data->surface;
1643 if (surface)
1645 surface->funcs->lock( surface );
1646 *surface->funcs->get_bounds( surface ) = surface->rect;
1647 surface->funcs->unlock( surface );
1648 if (is_argb_surface( surface )) surface->funcs->flush( surface );
1650 release_win_data( data );
1652 return 0;
1653 default:
1654 FIXME( "got window msg %x hwnd %p wp %lx lp %lx\n", msg, hwnd, (long)wp, lp );
1655 return 0;
1660 /***********************************************************************
1661 * ANDROID_CreateDesktop
1663 BOOL ANDROID_CreateDesktop( const WCHAR *name, UINT width, UINT height )
1665 /* wait until we receive the surface changed event */
1666 while (!screen_width)
1668 if (wait_events( 2000 ) != 1)
1670 ERR( "wait timed out\n" );
1671 break;
1673 process_events( QS_ALLINPUT );
1675 return 0;