ddraw/tests: Rewrite LimitTest().
[wine.git] / dlls / wineandroid.drv / window.c
blob568f5ab17b332de6560442dbf1c5b80effeaf2b7
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 #define NONAMELESSUNION
24 #define NONAMELESSSTRUCT
26 #include "config.h"
27 #include "wine/port.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 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
40 #include "windef.h"
41 #include "winbase.h"
42 #include "wingdi.h"
43 #include "winuser.h"
44 #include "wine/unicode.h"
46 #include "android.h"
47 #include "wine/server.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(android);
52 /* private window data */
53 struct android_win_data
55 HWND hwnd; /* hwnd that this private data belongs to */
56 HWND parent; /* parent hwnd for child windows */
57 RECT window_rect; /* USER window rectangle relative to parent */
58 RECT whole_rect; /* X window rectangle for the whole window relative to parent */
59 RECT client_rect; /* client area relative to parent */
60 ANativeWindow *window; /* native window wrapper that forwards calls to the desktop process */
61 struct window_surface *surface;
64 #define SWP_AGG_NOPOSCHANGE (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
66 static CRITICAL_SECTION win_data_section;
67 static CRITICAL_SECTION_DEBUG critsect_debug =
69 0, 0, &win_data_section,
70 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
71 0, 0, { (DWORD_PTR)(__FILE__ ": win_data_section") }
73 static CRITICAL_SECTION win_data_section = { &critsect_debug, -1, 0, 0, 0, 0 };
75 static struct android_win_data *win_data_context[32768];
77 static inline int context_idx( HWND hwnd )
79 return LOWORD( hwnd ) >> 1;
82 static void set_surface_region( struct window_surface *window_surface, HRGN win_region );
84 /* only for use on sanitized BITMAPINFO structures */
85 static inline int get_dib_info_size( const BITMAPINFO *info, UINT coloruse )
87 if (info->bmiHeader.biCompression == BI_BITFIELDS)
88 return sizeof(BITMAPINFOHEADER) + 3 * sizeof(DWORD);
89 if (coloruse == DIB_PAL_COLORS)
90 return sizeof(BITMAPINFOHEADER) + info->bmiHeader.biClrUsed * sizeof(WORD);
91 return FIELD_OFFSET( BITMAPINFO, bmiColors[info->bmiHeader.biClrUsed] );
94 static inline int get_dib_stride( int width, int bpp )
96 return ((width * bpp + 31) >> 3) & ~3;
99 static inline int get_dib_image_size( const BITMAPINFO *info )
101 return get_dib_stride( info->bmiHeader.biWidth, info->bmiHeader.biBitCount )
102 * abs( info->bmiHeader.biHeight );
106 /***********************************************************************
107 * alloc_win_data
109 static struct android_win_data *alloc_win_data( HWND hwnd )
111 struct android_win_data *data;
113 if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
115 data->hwnd = hwnd;
116 data->window = create_ioctl_window( hwnd, FALSE );
117 EnterCriticalSection( &win_data_section );
118 win_data_context[context_idx(hwnd)] = data;
120 return data;
124 /***********************************************************************
125 * free_win_data
127 static void free_win_data( struct android_win_data *data )
129 win_data_context[context_idx( data->hwnd )] = NULL;
130 LeaveCriticalSection( &win_data_section );
131 if (data->window) release_ioctl_window( data->window );
132 HeapFree( GetProcessHeap(), 0, data );
136 /***********************************************************************
137 * get_win_data
139 * Lock and return the data structure associated with a window.
141 static struct android_win_data *get_win_data( HWND hwnd )
143 struct android_win_data *data;
145 if (!hwnd) return NULL;
146 EnterCriticalSection( &win_data_section );
147 if ((data = win_data_context[context_idx(hwnd)]) && data->hwnd == hwnd) return data;
148 LeaveCriticalSection( &win_data_section );
149 return NULL;
153 /***********************************************************************
154 * release_win_data
156 * Release the data returned by get_win_data.
158 static void release_win_data( struct android_win_data *data )
160 if (data) LeaveCriticalSection( &win_data_section );
164 /***********************************************************************
165 * get_ioctl_window
167 static struct ANativeWindow *get_ioctl_window( HWND hwnd )
169 struct ANativeWindow *ret;
170 struct android_win_data *data = get_win_data( hwnd );
172 if (!data || !data->window) return NULL;
173 ret = grab_ioctl_window( data->window );
174 release_win_data( data );
175 return ret;
179 /* Handling of events coming from the Java side */
181 struct java_event
183 struct list entry;
184 union event_data data;
187 static struct list event_queue = LIST_INIT( event_queue );
188 static struct java_event *current_event;
189 static int event_pipe[2];
190 static DWORD desktop_tid;
192 /***********************************************************************
193 * send_event
195 int send_event( const union event_data *data )
197 int res;
199 if ((res = write( event_pipe[1], data, sizeof(*data) )) != sizeof(*data))
201 p__android_log_print( ANDROID_LOG_ERROR, "wine", "failed to send event" );
202 return -1;
204 return 0;
208 /***********************************************************************
209 * desktop_changed
211 * JNI callback, runs in the context of the Java thread.
213 void desktop_changed( JNIEnv *env, jobject obj, jint width, jint height )
215 union event_data data;
217 memset( &data, 0, sizeof(data) );
218 data.type = DESKTOP_CHANGED;
219 data.desktop.width = width;
220 data.desktop.height = height;
221 p__android_log_print( ANDROID_LOG_INFO, "wine", "desktop_changed: %ux%u", width, height );
222 send_event( &data );
226 /***********************************************************************
227 * config_changed
229 * JNI callback, runs in the context of the Java thread.
231 void config_changed( JNIEnv *env, jobject obj, jint dpi )
233 union event_data data;
235 memset( &data, 0, sizeof(data) );
236 data.type = CONFIG_CHANGED;
237 data.cfg.dpi = dpi;
238 p__android_log_print( ANDROID_LOG_INFO, "wine", "config_changed: %u dpi", dpi );
239 send_event( &data );
243 /***********************************************************************
244 * surface_changed
246 * JNI callback, runs in the context of the Java thread.
248 void surface_changed( JNIEnv *env, jobject obj, jint win, jobject surface, jboolean client )
250 union event_data data;
252 memset( &data, 0, sizeof(data) );
253 data.surface.hwnd = LongToHandle( win );
254 data.surface.client = client;
255 if (surface)
257 int width, height;
258 ANativeWindow *win = pANativeWindow_fromSurface( env, surface );
260 if (win->query( win, NATIVE_WINDOW_WIDTH, &width ) < 0) width = 0;
261 if (win->query( win, NATIVE_WINDOW_HEIGHT, &height ) < 0) height = 0;
262 data.surface.window = win;
263 data.surface.width = width;
264 data.surface.height = height;
265 p__android_log_print( ANDROID_LOG_INFO, "wine", "surface_changed: %p %s %ux%u",
266 data.surface.hwnd, client ? "client" : "whole", width, height );
268 data.type = SURFACE_CHANGED;
269 send_event( &data );
273 /***********************************************************************
274 * motion_event
276 * JNI callback, runs in the context of the Java thread.
278 jboolean motion_event( JNIEnv *env, jobject obj, jint win, jint action, jint x, jint y, jint state, jint vscroll )
280 static LONG button_state;
281 union event_data data;
282 int prev_state;
284 int mask = action & AMOTION_EVENT_ACTION_MASK;
286 if (!( mask == AMOTION_EVENT_ACTION_DOWN ||
287 mask == AMOTION_EVENT_ACTION_UP ||
288 mask == AMOTION_EVENT_ACTION_SCROLL ||
289 mask == AMOTION_EVENT_ACTION_MOVE ||
290 mask == AMOTION_EVENT_ACTION_HOVER_MOVE ))
291 return JNI_FALSE;
293 prev_state = InterlockedExchange( &button_state, state );
295 data.type = MOTION_EVENT;
296 data.motion.hwnd = LongToHandle( win );
297 data.motion.input.type = INPUT_MOUSE;
298 data.motion.input.u.mi.dx = x;
299 data.motion.input.u.mi.dy = y;
300 data.motion.input.u.mi.mouseData = 0;
301 data.motion.input.u.mi.time = 0;
302 data.motion.input.u.mi.dwExtraInfo = 0;
303 data.motion.input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
304 switch (action & AMOTION_EVENT_ACTION_MASK)
306 case AMOTION_EVENT_ACTION_DOWN:
307 if ((state & ~prev_state) & AMOTION_EVENT_BUTTON_PRIMARY)
308 data.motion.input.u.mi.dwFlags |= MOUSEEVENTF_LEFTDOWN;
309 if ((state & ~prev_state) & AMOTION_EVENT_BUTTON_SECONDARY)
310 data.motion.input.u.mi.dwFlags |= MOUSEEVENTF_RIGHTDOWN;
311 if ((state & ~prev_state) & AMOTION_EVENT_BUTTON_TERTIARY)
312 data.motion.input.u.mi.dwFlags |= MOUSEEVENTF_MIDDLEDOWN;
313 if (!(state & ~prev_state)) /* touch event */
314 data.motion.input.u.mi.dwFlags |= MOUSEEVENTF_LEFTDOWN;
315 break;
316 case AMOTION_EVENT_ACTION_UP:
317 if ((prev_state & ~state) & AMOTION_EVENT_BUTTON_PRIMARY)
318 data.motion.input.u.mi.dwFlags |= MOUSEEVENTF_LEFTUP;
319 if ((prev_state & ~state) & AMOTION_EVENT_BUTTON_SECONDARY)
320 data.motion.input.u.mi.dwFlags |= MOUSEEVENTF_RIGHTUP;
321 if ((prev_state & ~state) & AMOTION_EVENT_BUTTON_TERTIARY)
322 data.motion.input.u.mi.dwFlags |= MOUSEEVENTF_MIDDLEUP;
323 if (!(prev_state & ~state)) /* touch event */
324 data.motion.input.u.mi.dwFlags |= MOUSEEVENTF_LEFTUP;
325 break;
326 case AMOTION_EVENT_ACTION_SCROLL:
327 data.motion.input.u.mi.dwFlags |= MOUSEEVENTF_WHEEL;
328 data.motion.input.u.mi.mouseData = vscroll < 0 ? -WHEEL_DELTA : WHEEL_DELTA;
329 break;
330 case AMOTION_EVENT_ACTION_MOVE:
331 case AMOTION_EVENT_ACTION_HOVER_MOVE:
332 break;
333 default:
334 return JNI_FALSE;
336 send_event( &data );
337 return JNI_TRUE;
341 /***********************************************************************
342 * init_event_queue
344 static void init_event_queue(void)
346 HANDLE handle;
347 int ret;
349 if (pipe2( event_pipe, O_CLOEXEC | O_NONBLOCK ) == -1)
351 ERR( "could not create data\n" );
352 ExitProcess(1);
354 if (wine_server_fd_to_handle( event_pipe[0], GENERIC_READ | SYNCHRONIZE, 0, &handle ))
356 ERR( "Can't allocate handle for event fd\n" );
357 ExitProcess(1);
359 SERVER_START_REQ( set_queue_fd )
361 req->handle = wine_server_obj_handle( handle );
362 ret = wine_server_call( req );
364 SERVER_END_REQ;
365 if (ret)
367 ERR( "Can't store handle for event fd %x\n", ret );
368 ExitProcess(1);
370 CloseHandle( handle );
371 desktop_tid = GetCurrentThreadId();
375 /***********************************************************************
376 * pull_events
378 * Pull events from the event pipe and add them to the queue
380 static void pull_events(void)
382 struct java_event *event;
383 int res;
385 for (;;)
387 if (!(event = HeapAlloc( GetProcessHeap(), 0, sizeof(*event) ))) break;
389 res = read( event_pipe[0], &event->data, sizeof(event->data) );
390 if (res != sizeof(event->data)) break;
391 list_add_tail( &event_queue, &event->entry );
393 HeapFree( GetProcessHeap(), 0, event );
397 /***********************************************************************
398 * process_events
400 static int process_events( DWORD mask )
402 struct java_event *event, *next, *previous;
403 unsigned int count = 0;
405 assert( GetCurrentThreadId() == desktop_tid );
407 pull_events();
409 previous = current_event;
411 LIST_FOR_EACH_ENTRY_SAFE( event, next, &event_queue, struct java_event, entry )
413 switch (event->data.type)
415 case SURFACE_CHANGED:
416 break; /* always process it to unblock other threads */
417 case MOTION_EVENT:
418 if (event->data.motion.input.u.mi.dwFlags & (MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_RIGHTDOWN|
419 MOUSEEVENTF_MIDDLEDOWN|MOUSEEVENTF_LEFTUP|
420 MOUSEEVENTF_RIGHTUP|MOUSEEVENTF_MIDDLEUP))
422 if (mask & QS_MOUSEBUTTON) break;
424 else if (mask & QS_MOUSEMOVE) break;
425 continue; /* skip it */
426 case KEYBOARD_EVENT:
427 if (mask & QS_KEY) break;
428 continue; /* skip it */
429 default:
430 if (mask & QS_SENDMESSAGE) break;
431 continue; /* skip it */
434 /* remove it first, in case we process events recursively */
435 list_remove( &event->entry );
436 current_event = event;
438 switch (event->data.type)
440 case DESKTOP_CHANGED:
441 TRACE( "DESKTOP_CHANGED %ux%u\n", event->data.desktop.width, event->data.desktop.height );
442 screen_width = event->data.desktop.width;
443 screen_height = event->data.desktop.height;
444 init_monitors( screen_width, screen_height );
445 SetWindowPos( GetDesktopWindow(), 0, 0, 0, screen_width, screen_height,
446 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW );
447 break;
449 case CONFIG_CHANGED:
450 TRACE( "CONFIG_CHANGED dpi %u\n", event->data.cfg.dpi );
451 set_screen_dpi( event->data.cfg.dpi );
452 break;
454 case SURFACE_CHANGED:
455 TRACE("SURFACE_CHANGED %p %p %s size %ux%u\n", event->data.surface.hwnd,
456 event->data.surface.window, event->data.surface.client ? "client" : "whole",
457 event->data.surface.width, event->data.surface.height );
459 register_native_window( event->data.surface.hwnd, event->data.surface.window, event->data.surface.client );
460 break;
462 case MOTION_EVENT:
464 HWND capture = get_capture_window();
466 if (event->data.motion.input.u.mi.dwFlags & (MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_RIGHTDOWN|MOUSEEVENTF_MIDDLEDOWN))
467 TRACE( "BUTTONDOWN pos %d,%d hwnd %p flags %x\n",
468 event->data.motion.input.u.mi.dx, event->data.motion.input.u.mi.dy,
469 event->data.motion.hwnd, event->data.motion.input.u.mi.dwFlags );
470 else if (event->data.motion.input.u.mi.dwFlags & (MOUSEEVENTF_LEFTUP|MOUSEEVENTF_RIGHTUP|MOUSEEVENTF_MIDDLEUP))
471 TRACE( "BUTTONUP pos %d,%d hwnd %p flags %x\n",
472 event->data.motion.input.u.mi.dx, event->data.motion.input.u.mi.dy,
473 event->data.motion.hwnd, event->data.motion.input.u.mi.dwFlags );
474 else
475 TRACE( "MOUSEMOVE pos %d,%d hwnd %p flags %x\n",
476 event->data.motion.input.u.mi.dx, event->data.motion.input.u.mi.dy,
477 event->data.motion.hwnd, event->data.motion.input.u.mi.dwFlags );
478 if (!capture && (event->data.motion.input.u.mi.dwFlags & MOUSEEVENTF_ABSOLUTE))
480 RECT rect;
481 SetRect( &rect, event->data.motion.input.u.mi.dx, event->data.motion.input.u.mi.dy,
482 event->data.motion.input.u.mi.dx + 1, event->data.motion.input.u.mi.dy + 1 );
483 MapWindowPoints( 0, event->data.motion.hwnd, (POINT *)&rect, 2 );
485 SERVER_START_REQ( update_window_zorder )
487 req->window = wine_server_user_handle( event->data.motion.hwnd );
488 req->rect.left = rect.left;
489 req->rect.top = rect.top;
490 req->rect.right = rect.right;
491 req->rect.bottom = rect.bottom;
492 wine_server_call( req );
494 SERVER_END_REQ;
496 __wine_send_input( capture ? capture : event->data.motion.hwnd, &event->data.motion.input );
498 break;
500 case KEYBOARD_EVENT:
501 if (event->data.kbd.input.u.ki.dwFlags & KEYEVENTF_KEYUP)
502 TRACE("KEYUP hwnd %p vkey %x '%c' scancode %x\n", event->data.kbd.hwnd,
503 event->data.kbd.input.u.ki.wVk, event->data.kbd.input.u.ki.wVk,
504 event->data.kbd.input.u.ki.wScan );
505 else
506 TRACE("KEYDOWN hwnd %p vkey %x '%c' scancode %x\n", event->data.kbd.hwnd,
507 event->data.kbd.input.u.ki.wVk, event->data.kbd.input.u.ki.wVk,
508 event->data.kbd.input.u.ki.wScan );
509 update_keyboard_lock_state( event->data.kbd.input.u.ki.wVk, event->data.kbd.lock_state );
510 __wine_send_input( 0, &event->data.kbd.input );
511 break;
513 default:
514 FIXME( "got event %u\n", event->data.type );
516 HeapFree( GetProcessHeap(), 0, event );
517 count++;
518 /* next may have been removed by a recursive call, so reset it to the beginning of the list */
519 next = LIST_ENTRY( event_queue.next, struct java_event, entry );
521 current_event = previous;
522 return count;
526 /***********************************************************************
527 * wait_events
529 static int wait_events( int timeout )
531 assert( GetCurrentThreadId() == desktop_tid );
533 for (;;)
535 struct pollfd pollfd;
536 int ret;
538 pollfd.fd = event_pipe[0];
539 pollfd.events = POLLIN | POLLHUP;
540 ret = poll( &pollfd, 1, timeout );
541 if (ret == -1 && errno == EINTR) continue;
542 if (ret && (pollfd.revents & (POLLHUP | POLLERR))) ret = -1;
543 return ret;
548 /* Window surface support */
550 struct android_window_surface
552 struct window_surface header;
553 HWND hwnd;
554 ANativeWindow *window;
555 RECT bounds;
556 BOOL byteswap;
557 RGNDATA *region_data;
558 HRGN region;
559 BYTE alpha;
560 COLORREF color_key;
561 void *bits;
562 CRITICAL_SECTION crit;
563 BITMAPINFO info; /* variable size, must be last */
566 static struct android_window_surface *get_android_surface( struct window_surface *surface )
568 return (struct android_window_surface *)surface;
571 static inline void reset_bounds( RECT *bounds )
573 bounds->left = bounds->top = INT_MAX;
574 bounds->right = bounds->bottom = INT_MIN;
577 static inline void add_bounds_rect( RECT *bounds, const RECT *rect )
579 if (rect->left >= rect->right || rect->top >= rect->bottom) return;
580 bounds->left = min( bounds->left, rect->left );
581 bounds->top = min( bounds->top, rect->top );
582 bounds->right = max( bounds->right, rect->right );
583 bounds->bottom = max( bounds->bottom, rect->bottom );
586 /* store the palette or color mask data in the bitmap info structure */
587 static void set_color_info( BITMAPINFO *info, BOOL has_alpha )
589 DWORD *colors = (DWORD *)info->bmiColors;
591 info->bmiHeader.biSize = sizeof(info->bmiHeader);
592 info->bmiHeader.biClrUsed = 0;
593 info->bmiHeader.biBitCount = 32;
594 if (has_alpha)
596 info->bmiHeader.biCompression = BI_RGB;
597 return;
599 info->bmiHeader.biCompression = BI_BITFIELDS;
600 colors[0] = 0xff0000;
601 colors[1] = 0x00ff00;
602 colors[2] = 0x0000ff;
605 /* apply the window region to a single line of the destination image. */
606 static void apply_line_region( DWORD *dst, int width, int x, int y, const RECT *rect, const RECT *end )
608 while (rect < end && rect->top <= y && width > 0)
610 if (rect->left > x)
612 memset( dst, 0, min( rect->left - x, width ) * sizeof(*dst) );
613 dst += rect->left - x;
614 width -= rect->left - x;
615 x = rect->left;
617 if (rect->right > x)
619 dst += rect->right - x;
620 width -= rect->right - x;
621 x = rect->right;
623 rect++;
625 if (width > 0) memset( dst, 0, width * sizeof(*dst) );
628 /***********************************************************************
629 * android_surface_lock
631 static void android_surface_lock( struct window_surface *window_surface )
633 struct android_window_surface *surface = get_android_surface( window_surface );
635 EnterCriticalSection( &surface->crit );
638 /***********************************************************************
639 * android_surface_unlock
641 static void android_surface_unlock( struct window_surface *window_surface )
643 struct android_window_surface *surface = get_android_surface( window_surface );
645 LeaveCriticalSection( &surface->crit );
648 /***********************************************************************
649 * android_surface_get_bitmap_info
651 static void *android_surface_get_bitmap_info( struct window_surface *window_surface, BITMAPINFO *info )
653 struct android_window_surface *surface = get_android_surface( window_surface );
655 memcpy( info, &surface->info, get_dib_info_size( &surface->info, DIB_RGB_COLORS ));
656 return surface->bits;
659 /***********************************************************************
660 * android_surface_get_bounds
662 static RECT *android_surface_get_bounds( struct window_surface *window_surface )
664 struct android_window_surface *surface = get_android_surface( window_surface );
666 return &surface->bounds;
669 /***********************************************************************
670 * android_surface_set_region
672 static void android_surface_set_region( struct window_surface *window_surface, HRGN region )
674 struct android_window_surface *surface = get_android_surface( window_surface );
676 TRACE( "updating surface %p hwnd %p with %p\n", surface, surface->hwnd, region );
678 window_surface->funcs->lock( window_surface );
679 if (!region)
681 if (surface->region) DeleteObject( surface->region );
682 surface->region = 0;
684 else
686 if (!surface->region) surface->region = CreateRectRgn( 0, 0, 0, 0 );
687 CombineRgn( surface->region, region, 0, RGN_COPY );
689 window_surface->funcs->unlock( window_surface );
690 set_surface_region( &surface->header, (HRGN)1 );
693 /***********************************************************************
694 * android_surface_flush
696 static void android_surface_flush( struct window_surface *window_surface )
698 struct android_window_surface *surface = get_android_surface( window_surface );
699 ANativeWindow_Buffer buffer;
700 ARect rc;
701 RECT rect;
702 BOOL needs_flush;
704 window_surface->funcs->lock( window_surface );
705 SetRect( &rect, 0, 0, surface->header.rect.right - surface->header.rect.left,
706 surface->header.rect.bottom - surface->header.rect.top );
707 needs_flush = IntersectRect( &rect, &rect, &surface->bounds );
708 reset_bounds( &surface->bounds );
709 window_surface->funcs->unlock( window_surface );
710 if (!needs_flush) return;
712 TRACE( "flushing %p hwnd %p surface %s rect %s bits %p alpha %02x key %08x region %u rects\n",
713 surface, surface->hwnd, wine_dbgstr_rect( &surface->header.rect ),
714 wine_dbgstr_rect( &rect ), surface->bits, surface->alpha, surface->color_key,
715 surface->region_data ? surface->region_data->rdh.nCount : 0 );
717 rc.left = rect.left;
718 rc.top = rect.top;
719 rc.right = rect.right;
720 rc.bottom = rect.bottom;
722 if (!surface->window->perform( surface->window, NATIVE_WINDOW_LOCK, &buffer, &rc ))
724 const RECT *rgn_rect = NULL, *end = NULL;
725 unsigned int *src, *dst;
726 int x, y, width;
728 rect.left = rc.left;
729 rect.top = rc.top;
730 rect.right = rc.right;
731 rect.bottom = rc.bottom;
732 IntersectRect( &rect, &rect, &surface->header.rect );
734 if (surface->region_data)
736 rgn_rect = (RECT *)surface->region_data->Buffer;
737 end = rgn_rect + surface->region_data->rdh.nCount;
739 src = (unsigned int *)surface->bits
740 + (rect.top - surface->header.rect.top) * surface->info.bmiHeader.biWidth
741 + (rect.left - surface->header.rect.left);
742 dst = (unsigned int *)buffer.bits + rect.top * buffer.stride + rect.left;
743 width = min( rect.right - rect.left, buffer.stride );
745 for (y = rect.top; y < min( buffer.height, rect.bottom); y++)
747 if (surface->info.bmiHeader.biCompression == BI_RGB)
748 memcpy( dst, src, width * sizeof(*dst) );
749 else if (surface->alpha == 255)
750 for (x = 0; x < width; x++) dst[x] = src[x] | 0xff000000;
751 else
752 for (x = 0; x < width; x++)
753 dst[x] = ((surface->alpha << 24) |
754 (((BYTE)(src[x] >> 16) * surface->alpha / 255) << 16) |
755 (((BYTE)(src[x] >> 8) * surface->alpha / 255) << 8) |
756 (((BYTE)src[x] * surface->alpha / 255)));
758 if (surface->color_key != CLR_INVALID)
759 for (x = 0; x < width; x++) if ((src[x] & 0xffffff) == surface->color_key) dst[x] = 0;
761 if (rgn_rect)
763 while (rgn_rect < end && rgn_rect->bottom <= y) rgn_rect++;
764 apply_line_region( dst, width, rect.left, y, rgn_rect, end );
767 src += surface->info.bmiHeader.biWidth;
768 dst += buffer.stride;
770 surface->window->perform( surface->window, NATIVE_WINDOW_UNLOCK_AND_POST );
772 else TRACE( "Unable to lock surface %p window %p buffer %p\n",
773 surface, surface->hwnd, surface->window );
776 /***********************************************************************
777 * android_surface_destroy
779 static void android_surface_destroy( struct window_surface *window_surface )
781 struct android_window_surface *surface = get_android_surface( window_surface );
783 TRACE( "freeing %p bits %p\n", surface, surface->bits );
785 surface->crit.DebugInfo->Spare[0] = 0;
786 DeleteCriticalSection( &surface->crit );
787 HeapFree( GetProcessHeap(), 0, surface->region_data );
788 if (surface->region) DeleteObject( surface->region );
789 release_ioctl_window( surface->window );
790 HeapFree( GetProcessHeap(), 0, surface->bits );
791 HeapFree( GetProcessHeap(), 0, surface );
794 static const struct window_surface_funcs android_surface_funcs =
796 android_surface_lock,
797 android_surface_unlock,
798 android_surface_get_bitmap_info,
799 android_surface_get_bounds,
800 android_surface_set_region,
801 android_surface_flush,
802 android_surface_destroy
805 static BOOL is_argb_surface( struct window_surface *surface )
807 return surface && surface->funcs == &android_surface_funcs &&
808 get_android_surface( surface )->info.bmiHeader.biCompression == BI_RGB;
811 /***********************************************************************
812 * set_color_key
814 static void set_color_key( struct android_window_surface *surface, COLORREF key )
816 if (key == CLR_INVALID)
817 surface->color_key = CLR_INVALID;
818 else if (surface->info.bmiHeader.biBitCount <= 8)
819 surface->color_key = CLR_INVALID;
820 else if (key & (1 << 24)) /* PALETTEINDEX */
821 surface->color_key = 0;
822 else if (key >> 16 == 0x10ff) /* DIBINDEX */
823 surface->color_key = 0;
824 else if (surface->info.bmiHeader.biBitCount == 24)
825 surface->color_key = key;
826 else
827 surface->color_key = (GetRValue(key) << 16) | (GetGValue(key) << 8) | GetBValue(key);
830 /***********************************************************************
831 * set_surface_region
833 static void set_surface_region( struct window_surface *window_surface, HRGN win_region )
835 struct android_window_surface *surface = get_android_surface( window_surface );
836 struct android_win_data *win_data;
837 HRGN region = win_region;
838 RGNDATA *data = NULL;
839 DWORD size;
840 int offset_x, offset_y;
842 if (window_surface->funcs != &android_surface_funcs) return; /* we may get the null surface */
844 if (!(win_data = get_win_data( surface->hwnd ))) return;
845 offset_x = win_data->window_rect.left - win_data->whole_rect.left;
846 offset_y = win_data->window_rect.top - win_data->whole_rect.top;
847 release_win_data( win_data );
849 if (win_region == (HRGN)1) /* hack: win_region == 1 means retrieve region from server */
851 region = CreateRectRgn( 0, 0, win_data->window_rect.right - win_data->window_rect.left,
852 win_data->window_rect.bottom - win_data->window_rect.top );
853 if (GetWindowRgn( surface->hwnd, region ) == ERROR && !surface->region) goto done;
856 OffsetRgn( region, offset_x, offset_y );
857 if (surface->region) CombineRgn( region, region, surface->region, RGN_AND );
859 if (!(size = GetRegionData( region, 0, NULL ))) goto done;
860 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) goto done;
862 if (!GetRegionData( region, size, data ))
864 HeapFree( GetProcessHeap(), 0, data );
865 data = NULL;
868 done:
869 window_surface->funcs->lock( window_surface );
870 HeapFree( GetProcessHeap(), 0, surface->region_data );
871 surface->region_data = data;
872 *window_surface->funcs->get_bounds( window_surface ) = surface->header.rect;
873 window_surface->funcs->unlock( window_surface );
874 if (region != win_region) DeleteObject( region );
877 /***********************************************************************
878 * create_surface
880 static struct window_surface *create_surface( HWND hwnd, const RECT *rect,
881 BYTE alpha, COLORREF color_key, BOOL src_alpha )
883 struct android_window_surface *surface;
884 int width = rect->right - rect->left, height = rect->bottom - rect->top;
886 surface = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
887 FIELD_OFFSET( struct android_window_surface, info.bmiColors[3] ));
888 if (!surface) return NULL;
889 set_color_info( &surface->info, src_alpha );
890 surface->info.bmiHeader.biWidth = width;
891 surface->info.bmiHeader.biHeight = -height; /* top-down */
892 surface->info.bmiHeader.biPlanes = 1;
893 surface->info.bmiHeader.biSizeImage = get_dib_image_size( &surface->info );
895 InitializeCriticalSection( &surface->crit );
896 surface->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": surface");
898 surface->header.funcs = &android_surface_funcs;
899 surface->header.rect = *rect;
900 surface->header.ref = 1;
901 surface->hwnd = hwnd;
902 surface->window = get_ioctl_window( hwnd );
903 surface->alpha = alpha;
904 set_color_key( surface, color_key );
905 set_surface_region( &surface->header, (HRGN)1 );
906 reset_bounds( &surface->bounds );
908 if (!(surface->bits = HeapAlloc( GetProcessHeap(), 0, surface->info.bmiHeader.biSizeImage )))
909 goto failed;
911 TRACE( "created %p hwnd %p %s bits %p-%p\n", surface, hwnd, wine_dbgstr_rect(rect),
912 surface->bits, (char *)surface->bits + surface->info.bmiHeader.biSizeImage );
914 return &surface->header;
916 failed:
917 android_surface_destroy( &surface->header );
918 return NULL;
921 /***********************************************************************
922 * set_surface_layered
924 static void set_surface_layered( struct window_surface *window_surface, BYTE alpha, COLORREF color_key )
926 struct android_window_surface *surface = get_android_surface( window_surface );
927 COLORREF prev_key;
928 BYTE prev_alpha;
930 if (window_surface->funcs != &android_surface_funcs) return; /* we may get the null surface */
932 window_surface->funcs->lock( window_surface );
933 prev_key = surface->color_key;
934 prev_alpha = surface->alpha;
935 surface->alpha = alpha;
936 set_color_key( surface, color_key );
937 if (alpha != prev_alpha || surface->color_key != prev_key) /* refresh */
938 *window_surface->funcs->get_bounds( window_surface ) = surface->header.rect;
939 window_surface->funcs->unlock( window_surface );
943 static WNDPROC desktop_orig_wndproc;
945 static LRESULT CALLBACK desktop_wndproc_wrapper( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
947 switch (msg)
949 case WM_PARENTNOTIFY:
950 if (LOWORD(wp) == WM_DESTROY) destroy_ioctl_window( (HWND)lp, FALSE );
951 break;
953 return desktop_orig_wndproc( hwnd, msg, wp, lp );
957 /***********************************************************************
958 * ANDROID_MsgWaitForMultipleObjectsEx
960 DWORD CDECL ANDROID_MsgWaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
961 DWORD timeout, DWORD mask, DWORD flags )
963 if (GetCurrentThreadId() == desktop_tid)
965 /* don't process nested events */
966 if (current_event) mask = 0;
967 if (process_events( mask )) return count - 1;
969 return WaitForMultipleObjectsEx( count, handles, flags & MWMO_WAITALL,
970 timeout, flags & MWMO_ALERTABLE );
973 /**********************************************************************
974 * ANDROID_CreateWindow
976 BOOL CDECL ANDROID_CreateWindow( HWND hwnd )
978 TRACE( "%p\n", hwnd );
980 if (hwnd == GetDesktopWindow())
982 struct android_win_data *data;
984 init_event_queue();
985 start_android_device();
986 if (!(data = alloc_win_data( hwnd ))) return FALSE;
987 release_win_data( data );
989 return TRUE;
993 /***********************************************************************
994 * ANDROID_DestroyWindow
996 void CDECL ANDROID_DestroyWindow( HWND hwnd )
998 struct android_win_data *data;
1000 if (!(data = get_win_data( hwnd ))) return;
1002 if (data->surface) window_surface_release( data->surface );
1003 data->surface = NULL;
1004 destroy_gl_drawable( hwnd );
1005 free_win_data( data );
1009 /***********************************************************************
1010 * create_win_data
1012 * Create a data window structure for an existing window.
1014 static struct android_win_data *create_win_data( HWND hwnd, const RECT *window_rect,
1015 const RECT *client_rect )
1017 struct android_win_data *data;
1018 HWND parent;
1020 if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop or HWND_MESSAGE */
1022 if (!(data = alloc_win_data( hwnd ))) return NULL;
1024 data->parent = (parent == GetDesktopWindow()) ? 0 : parent;
1025 data->whole_rect = data->window_rect = *window_rect;
1026 data->client_rect = *client_rect;
1027 return data;
1031 static inline RECT get_surface_rect( const RECT *visible_rect )
1033 RECT rect;
1035 IntersectRect( &rect, visible_rect, &virtual_screen_rect );
1036 OffsetRect( &rect, -visible_rect->left, -visible_rect->top );
1037 rect.left &= ~31;
1038 rect.top &= ~31;
1039 rect.right = max( rect.left + 32, (rect.right + 31) & ~31 );
1040 rect.bottom = max( rect.top + 32, (rect.bottom + 31) & ~31 );
1041 return rect;
1045 /***********************************************************************
1046 * ANDROID_WindowPosChanging
1048 void CDECL ANDROID_WindowPosChanging( HWND hwnd, HWND insert_after, UINT swp_flags,
1049 const RECT *window_rect, const RECT *client_rect, RECT *visible_rect,
1050 struct window_surface **surface )
1052 struct android_win_data *data = get_win_data( hwnd );
1053 RECT surface_rect;
1054 DWORD flags;
1055 COLORREF key;
1056 BYTE alpha;
1057 BOOL layered = GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED;
1059 TRACE( "win %p window %s client %s style %08x flags %08x\n",
1060 hwnd, wine_dbgstr_rect(window_rect), wine_dbgstr_rect(client_rect),
1061 GetWindowLongW( hwnd, GWL_STYLE ), swp_flags );
1063 if (!data && !(data = create_win_data( hwnd, window_rect, client_rect ))) return;
1065 *visible_rect = *window_rect;
1067 /* create the window surface if necessary */
1069 if (data->parent) goto done;
1070 if (swp_flags & SWP_HIDEWINDOW) goto done;
1071 if (is_argb_surface( data->surface )) goto done;
1073 surface_rect = get_surface_rect( visible_rect );
1074 if (data->surface)
1076 if (!memcmp( &data->surface->rect, &surface_rect, sizeof(surface_rect) ))
1078 /* existing surface is good enough */
1079 window_surface_add_ref( data->surface );
1080 if (*surface) window_surface_release( *surface );
1081 *surface = data->surface;
1082 goto done;
1085 if (!(swp_flags & SWP_SHOWWINDOW) && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)) goto done;
1087 if (!layered || !GetLayeredWindowAttributes( hwnd, &key, &alpha, &flags )) flags = 0;
1088 if (!(flags & LWA_ALPHA)) alpha = 255;
1089 if (!(flags & LWA_COLORKEY)) key = CLR_INVALID;
1091 if (*surface) window_surface_release( *surface );
1092 *surface = create_surface( data->hwnd, &surface_rect, alpha, key, FALSE );
1094 done:
1095 release_win_data( data );
1099 /***********************************************************************
1100 * ANDROID_WindowPosChanged
1102 void CDECL ANDROID_WindowPosChanged( HWND hwnd, HWND insert_after, UINT swp_flags,
1103 const RECT *window_rect, const RECT *client_rect,
1104 const RECT *visible_rect, const RECT *valid_rects,
1105 struct window_surface *surface )
1107 struct android_win_data *data;
1108 DWORD new_style = GetWindowLongW( hwnd, GWL_STYLE );
1109 HWND owner = 0;
1111 if (!(data = get_win_data( hwnd ))) return;
1113 data->window_rect = *window_rect;
1114 data->whole_rect = *visible_rect;
1115 data->client_rect = *client_rect;
1117 if (!is_argb_surface( data->surface ))
1119 if (surface) window_surface_add_ref( surface );
1120 if (data->surface) window_surface_release( data->surface );
1121 data->surface = surface;
1123 if (!data->parent) owner = GetWindow( hwnd, GW_OWNER );
1124 release_win_data( data );
1126 if (!(swp_flags & SWP_NOZORDER)) insert_after = GetWindow( hwnd, GW_HWNDPREV );
1128 TRACE( "win %p window %s client %s style %08x owner %p after %p flags %08x\n", hwnd,
1129 wine_dbgstr_rect(window_rect), wine_dbgstr_rect(client_rect),
1130 new_style, owner, insert_after, swp_flags );
1132 ioctl_window_pos_changed( hwnd, window_rect, client_rect, visible_rect,
1133 new_style, swp_flags, insert_after, owner );
1137 /***********************************************************************
1138 * ANDROID_ShowWindow
1140 UINT CDECL ANDROID_ShowWindow( HWND hwnd, INT cmd, RECT *rect, UINT swp )
1142 if (IsRectEmpty( rect )) return swp;
1143 if (!IsIconic( hwnd )) return swp;
1144 /* always hide icons off-screen */
1145 if (rect->left != -32000 || rect->top != -32000)
1147 OffsetRect( rect, -32000 - rect->left, -32000 - rect->top );
1148 swp &= ~(SWP_NOMOVE | SWP_NOCLIENTMOVE);
1150 return swp;
1154 /*****************************************************************
1155 * ANDROID_SetParent
1157 void CDECL ANDROID_SetParent( HWND hwnd, HWND parent, HWND old_parent )
1159 struct android_win_data *data;
1161 if (parent == old_parent) return;
1162 if (!(data = get_win_data( hwnd ))) return;
1164 TRACE( "win %p parent %p -> %p\n", hwnd, old_parent, parent );
1166 data->parent = (parent == GetDesktopWindow()) ? 0 : parent;
1167 ioctl_set_window_parent( hwnd, parent );
1168 release_win_data( data );
1172 /***********************************************************************
1173 * ANDROID_SetCapture
1175 void CDECL ANDROID_SetCapture( HWND hwnd, UINT flags )
1177 if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
1178 ioctl_set_capture( hwnd );
1182 /***********************************************************************
1183 * ANDROID_SetWindowStyle
1185 void CDECL ANDROID_SetWindowStyle( HWND hwnd, INT offset, STYLESTRUCT *style )
1187 struct android_win_data *data;
1188 DWORD changed = style->styleNew ^ style->styleOld;
1190 if (hwnd == GetDesktopWindow()) return;
1191 if (!(data = get_win_data( hwnd ))) return;
1193 if (offset == GWL_EXSTYLE && (changed & WS_EX_LAYERED)) /* changing WS_EX_LAYERED resets attributes */
1195 if (is_argb_surface( data->surface ))
1197 if (data->surface) window_surface_release( data->surface );
1198 data->surface = NULL;
1200 else if (data->surface) set_surface_layered( data->surface, 255, CLR_INVALID );
1202 release_win_data( data );
1206 /***********************************************************************
1207 * ANDROID_SetWindowRgn
1209 void CDECL ANDROID_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1211 struct android_win_data *data;
1213 if ((data = get_win_data( hwnd )))
1215 if (data->surface) set_surface_region( data->surface, hrgn );
1216 release_win_data( data );
1218 else FIXME( "not supported on other process window %p\n", hwnd );
1222 /***********************************************************************
1223 * ANDROID_SetLayeredWindowAttributes
1225 void CDECL ANDROID_SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
1227 struct android_win_data *data;
1229 if (!(flags & LWA_ALPHA)) alpha = 255;
1230 if (!(flags & LWA_COLORKEY)) key = CLR_INVALID;
1232 if ((data = get_win_data( hwnd )))
1234 if (data->surface) set_surface_layered( data->surface, alpha, key );
1235 release_win_data( data );
1240 /*****************************************************************************
1241 * ANDROID_UpdateLayeredWindow
1243 BOOL CDECL ANDROID_UpdateLayeredWindow( HWND hwnd, const UPDATELAYEREDWINDOWINFO *info,
1244 const RECT *window_rect )
1246 struct window_surface *surface;
1247 struct android_win_data *data;
1248 BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, 0 };
1249 COLORREF color_key = (info->dwFlags & ULW_COLORKEY) ? info->crKey : CLR_INVALID;
1250 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1251 BITMAPINFO *bmi = (BITMAPINFO *)buffer;
1252 void *src_bits, *dst_bits;
1253 RECT rect, src_rect;
1254 HDC hdc = 0;
1255 HBITMAP dib;
1256 BOOL ret = FALSE;
1258 if (!(data = get_win_data( hwnd ))) return FALSE;
1260 rect = *window_rect;
1261 OffsetRect( &rect, -window_rect->left, -window_rect->top );
1263 surface = data->surface;
1264 if (!is_argb_surface( surface ))
1266 if (surface) window_surface_release( surface );
1267 surface = NULL;
1270 if (!surface || !EqualRect( &surface->rect, &rect ))
1272 data->surface = create_surface( data->hwnd, &rect, 255, color_key, TRUE );
1273 if (surface) window_surface_release( surface );
1274 surface = data->surface;
1276 else set_surface_layered( surface, 255, color_key );
1278 if (surface) window_surface_add_ref( surface );
1279 release_win_data( data );
1281 if (!surface) return FALSE;
1282 if (!info->hdcSrc)
1284 window_surface_release( surface );
1285 return TRUE;
1288 dst_bits = surface->funcs->get_info( surface, bmi );
1290 if (!(dib = CreateDIBSection( info->hdcDst, bmi, DIB_RGB_COLORS, &src_bits, NULL, 0 ))) goto done;
1291 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
1293 SelectObject( hdc, dib );
1295 surface->funcs->lock( surface );
1297 if (info->prcDirty)
1299 IntersectRect( &rect, &rect, info->prcDirty );
1300 memcpy( src_bits, dst_bits, bmi->bmiHeader.biSizeImage );
1301 PatBlt( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, BLACKNESS );
1303 src_rect = rect;
1304 if (info->pptSrc) OffsetRect( &src_rect, info->pptSrc->x, info->pptSrc->y );
1305 DPtoLP( info->hdcSrc, (POINT *)&src_rect, 2 );
1307 ret = GdiAlphaBlend( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
1308 info->hdcSrc, src_rect.left, src_rect.top,
1309 src_rect.right - src_rect.left, src_rect.bottom - src_rect.top,
1310 (info->dwFlags & ULW_ALPHA) ? *info->pblend : blend );
1311 if (ret)
1313 memcpy( dst_bits, src_bits, bmi->bmiHeader.biSizeImage );
1314 add_bounds_rect( surface->funcs->get_bounds( surface ), &rect );
1317 surface->funcs->unlock( surface );
1318 surface->funcs->flush( surface );
1320 done:
1321 window_surface_release( surface );
1322 if (hdc) DeleteDC( hdc );
1323 if (dib) DeleteObject( dib );
1324 return ret;
1328 /**********************************************************************
1329 * ANDROID_WindowMessage
1331 LRESULT CDECL ANDROID_WindowMessage( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
1333 struct android_win_data *data;
1335 switch (msg)
1337 case WM_ANDROID_REFRESH:
1338 if (wp) /* opengl client window */
1340 update_gl_drawable( hwnd );
1342 else if ((data = get_win_data( hwnd )))
1344 struct window_surface *surface = data->surface;
1345 if (surface)
1347 surface->funcs->lock( surface );
1348 *surface->funcs->get_bounds( surface ) = surface->rect;
1349 surface->funcs->unlock( surface );
1350 if (is_argb_surface( surface )) surface->funcs->flush( surface );
1352 release_win_data( data );
1354 return 0;
1355 default:
1356 FIXME( "got window msg %x hwnd %p wp %lx lp %lx\n", msg, hwnd, wp, lp );
1357 return 0;
1362 /***********************************************************************
1363 * ANDROID_create_desktop
1365 BOOL CDECL ANDROID_create_desktop( UINT width, UINT height )
1367 desktop_orig_wndproc = (WNDPROC)SetWindowLongPtrW( GetDesktopWindow(), GWLP_WNDPROC,
1368 (LONG_PTR)desktop_wndproc_wrapper );
1370 /* wait until we receive the surface changed event */
1371 while (!screen_width)
1373 if (wait_events( 2000 ) != 1)
1375 ERR( "wait timed out\n" );
1376 break;
1378 process_events( QS_ALLINPUT );
1380 return TRUE;