1 /*****************************************************************************
2 * events.c: Windows DirectX video output events handler
3 *****************************************************************************
4 * Copyright (C) 2001-2004 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble: This file contains the functions related to the creation of
27 * a window and the handling of its messages (events).
28 *****************************************************************************/
29 #include <errno.h> /* ENOMEM */
30 #include <stdlib.h> /* free() */
31 #include <ctype.h> /* tolower() */
32 #include <string.h> /* strerror() */
35 # define _WIN32_WINNT 0x0500
40 #include <vlc/input.h>
47 #ifdef MODULE_NAME_IS_vout_directx
50 #ifdef MODULE_NAME_IS_direct3d
53 #ifdef MODULE_NAME_IS_glwin32
60 /*****************************************************************************
62 *****************************************************************************/
63 static int DirectXCreateWindow( vout_thread_t *p_vout );
64 static void DirectXCloseWindow ( vout_thread_t *p_vout );
65 static long FAR PASCAL DirectXEventProc( HWND, UINT, WPARAM, LPARAM );
67 static int Control( vout_thread_t *p_vout, int i_query, va_list args );
69 static void DirectXPopupMenu( event_thread_t *p_event, vlc_bool_t b_open )
71 playlist_t *p_playlist =
72 vlc_object_find( p_event, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
73 if( p_playlist != NULL )
77 var_Set( p_playlist, "intf-popupmenu", val );
78 vlc_object_release( p_playlist );
82 static int DirectXConvertKey( int i_key );
84 /*****************************************************************************
85 * DirectXEventThread: Create video window & handle its messages
86 *****************************************************************************
87 * This function creates a video window and then enters an infinite loop
88 * that handles the messages sent to that window.
89 * The main goal of this thread is to isolate the Win32 PeekMessage function
90 * because this one can block for a long time.
91 *****************************************************************************/
92 void E_(DirectXEventThread)( event_thread_t *p_event )
95 POINT old_mouse_pos = {0,0}, mouse_pos;
97 int i_width, i_height, i_x, i_y;
101 p_event->p_vout->pf_control = Control;
103 /* Create a window for the video */
104 /* Creating a window under Windows also initializes the thread's event
106 if( DirectXCreateWindow( p_event->p_vout ) )
108 msg_Err( p_event, "out of memory" );
109 p_event->b_dead = VLC_TRUE;
112 /* Signal the creation of the window */
113 vlc_thread_ready( p_event );
115 /* Set power management stuff */
116 if( (hkernel32 = GetModuleHandle( _T("KERNEL32") ) ) )
118 ULONG (WINAPI* OurSetThreadExecutionState)( ULONG );
120 OurSetThreadExecutionState = (ULONG (WINAPI*)( ULONG ))
121 GetProcAddress( hkernel32, _T("SetThreadExecutionState") );
123 if( OurSetThreadExecutionState )
124 /* Prevent monitor from powering off */
125 OurSetThreadExecutionState( ES_DISPLAY_REQUIRED | ES_CONTINUOUS );
127 msg_Dbg( p_event, "no support for SetThreadExecutionState()" );
131 /* GetMessage will sleep if there's no message in the queue */
132 while( !p_event->b_die && GetMessage( &msg, 0, 0, 0 ) )
134 /* Check if we are asked to exit */
138 switch( msg.message )
142 vout_PlacePicture( p_event->p_vout,
143 p_event->p_vout->p_sys->i_window_width,
144 p_event->p_vout->p_sys->i_window_height,
145 &i_x, &i_y, &i_width, &i_height );
147 if( msg.hwnd == p_event->p_vout->p_sys->hvideownd )
153 if( i_width && i_height )
155 val.i_int = ( GET_X_LPARAM(msg.lParam) - i_x ) *
156 p_event->p_vout->fmt_in.i_visible_width / i_width +
157 p_event->p_vout->fmt_in.i_x_offset;
158 var_Set( p_event->p_vout, "mouse-x", val );
159 val.i_int = ( GET_Y_LPARAM(msg.lParam) - i_y ) *
160 p_event->p_vout->fmt_in.i_visible_height / i_height +
161 p_event->p_vout->fmt_in.i_y_offset;
162 var_Set( p_event->p_vout, "mouse-y", val );
164 val.b_bool = VLC_TRUE;
165 var_Set( p_event->p_vout, "mouse-moved", val );
169 GetCursorPos( &mouse_pos );
170 if( (abs(mouse_pos.x - old_mouse_pos.x) > 2 ||
171 (abs(mouse_pos.y - old_mouse_pos.y)) > 2 ) )
173 GetCursorPos( &old_mouse_pos );
174 p_event->p_vout->p_sys->i_lastmoved = mdate();
176 if( p_event->p_vout->p_sys->b_cursor_hidden )
178 p_event->p_vout->p_sys->b_cursor_hidden = 0;
184 case WM_VLC_HIDE_MOUSE:
185 if( p_event->p_vout->p_sys->b_cursor_hidden ) break;
186 p_event->p_vout->p_sys->b_cursor_hidden = VLC_TRUE;
187 GetCursorPos( &old_mouse_pos );
191 case WM_VLC_SHOW_MOUSE:
192 if( !p_event->p_vout->p_sys->b_cursor_hidden ) break;
193 p_event->p_vout->p_sys->b_cursor_hidden = VLC_FALSE;
194 GetCursorPos( &old_mouse_pos );
199 var_Get( p_event->p_vout, "mouse-button-down", &val );
201 var_Set( p_event->p_vout, "mouse-button-down", val );
202 DirectXPopupMenu( p_event, VLC_FALSE );
206 var_Get( p_event->p_vout, "mouse-button-down", &val );
208 var_Set( p_event->p_vout, "mouse-button-down", val );
210 val.b_bool = VLC_TRUE;
211 var_Set( p_event->p_vout, "mouse-clicked", val );
214 case WM_LBUTTONDBLCLK:
215 p_event->p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
219 var_Get( p_event->p_vout, "mouse-button-down", &val );
221 var_Set( p_event->p_vout, "mouse-button-down", val );
222 DirectXPopupMenu( p_event, VLC_FALSE );
226 var_Get( p_event->p_vout, "mouse-button-down", &val );
228 var_Set( p_event->p_vout, "mouse-button-down", val );
232 var_Get( p_event->p_vout, "mouse-button-down", &val );
234 var_Set( p_event->p_vout, "mouse-button-down", val );
235 DirectXPopupMenu( p_event, VLC_FALSE );
239 var_Get( p_event->p_vout, "mouse-button-down", &val );
241 var_Set( p_event->p_vout, "mouse-button-down", val );
242 DirectXPopupMenu( p_event, VLC_TRUE );
247 /* The key events are first processed here and not translated
248 * into WM_CHAR events because we need to know the status of the
250 val.i_int = DirectXConvertKey( msg.wParam );
253 /* This appears to be a "normal" (ascii) key */
254 val.i_int = tolower( MapVirtualKey( msg.wParam, 2 ) );
259 if( GetKeyState(VK_CONTROL) & 0x8000 )
261 val.i_int |= KEY_MODIFIER_CTRL;
263 if( GetKeyState(VK_SHIFT) & 0x8000 )
265 val.i_int |= KEY_MODIFIER_SHIFT;
267 if( GetKeyState(VK_MENU) & 0x8000 )
269 val.i_int |= KEY_MODIFIER_ALT;
272 var_Set( p_event->p_libvlc, "key-pressed", val );
277 if( GET_WHEEL_DELTA_WPARAM( msg.wParam ) > 0 )
279 val.i_int = KEY_MOUSEWHEELUP;
283 val.i_int = KEY_MOUSEWHEELDOWN;
287 if( GetKeyState(VK_CONTROL) & 0x8000 )
289 val.i_int |= KEY_MODIFIER_CTRL;
291 if( GetKeyState(VK_SHIFT) & 0x8000 )
293 val.i_int |= KEY_MODIFIER_SHIFT;
295 if( GetKeyState(VK_MENU) & 0x8000 )
297 val.i_int |= KEY_MODIFIER_ALT;
300 var_Set( p_event->p_libvlc, "key-pressed", val );
304 case WM_VLC_CHANGE_TEXT:
305 var_Get( p_event->p_vout, "video-title", &val );
306 if( !val.psz_string || !*val.psz_string ) /* Default video title */
308 if( val.psz_string ) free( val.psz_string );
310 #ifdef MODULE_NAME_IS_glwin32
311 val.psz_string = strdup( VOUT_TITLE " (OpenGL output)" );
313 #ifdef MODULE_NAME_IS_direct3d
314 val.psz_string = strdup( VOUT_TITLE " (Direct3D output)" );
316 #ifdef MODULE_NAME_IS_directx
317 if( p_event->p_vout->p_sys->b_using_overlay ) val.psz_string =
318 strdup( VOUT_TITLE " (hardware YUV overlay DirectX output)" );
319 else if( p_event->p_vout->p_sys->b_hw_yuv ) val.psz_string =
320 strdup( VOUT_TITLE " (hardware YUV DirectX output)" );
321 else val.psz_string =
322 strdup( VOUT_TITLE " (software RGB DirectX output)" );
328 wchar_t *psz_title = malloc( strlen(val.psz_string) * 2 + 2 );
329 mbstowcs( psz_title, val.psz_string, strlen(val.psz_string)*2);
330 psz_title[strlen(val.psz_string)] = 0;
331 free( val.psz_string ); val.psz_string = (char *)psz_title;
335 SetWindowText( p_event->p_vout->p_sys->hwnd,
336 (LPCTSTR)val.psz_string );
337 if( p_event->p_vout->p_sys->hfswnd )
338 SetWindowText( p_event->p_vout->p_sys->hfswnd,
339 (LPCTSTR)val.psz_string );
340 free( val.psz_string );
344 /* Messages we don't handle directly are dispatched to the
345 * window procedure */
346 TranslateMessage(&msg);
347 DispatchMessage(&msg);
352 } /* End Main loop */
354 /* Check for WM_QUIT if we created the window */
355 if( !p_event->p_vout->p_sys->hparent && msg.message == WM_QUIT )
357 msg_Warn( p_event, "WM_QUIT... should not happen!!" );
358 p_event->p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
361 msg_Dbg( p_event, "DirectXEventThread terminating" );
363 /* clear the changes formerly signaled */
364 p_event->p_vout->p_sys->i_changes = 0;
366 DirectXCloseWindow( p_event->p_vout );
370 /* following functions are local */
372 /*****************************************************************************
373 * DirectXCreateWindow: create a window for the video.
374 *****************************************************************************
375 * Before creating a direct draw surface, we need to create a window in which
376 * the video will be displayed. This window will also allow us to capture the
378 *****************************************************************************/
379 static int DirectXCreateWindow( vout_thread_t *p_vout )
384 WNDCLASS wc; /* window class components */
385 HICON vlc_icon = NULL;
386 char vlc_path[MAX_PATH+1];
387 int i_style, i_stylex;
389 msg_Dbg( p_vout, "DirectXCreateWindow" );
391 /* Get this module's instance */
392 hInstance = GetModuleHandle(NULL);
394 /* If an external window was specified, we'll draw in it. */
395 p_vout->p_sys->hparent =
396 vout_RequestWindow( p_vout, &p_vout->p_sys->i_window_x,
397 &p_vout->p_sys->i_window_y,
398 &p_vout->p_sys->i_window_width,
399 &p_vout->p_sys->i_window_height );
401 /* We create the window ourself, there is no previous window proc. */
402 p_vout->p_sys->pf_wndproc = NULL;
404 /* Get the Icon from the main app */
407 if( GetModuleFileName( NULL, vlc_path, MAX_PATH ) )
409 vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
413 /* Fill in the window class structure */
414 wc.style = CS_OWNDC|CS_DBLCLKS; /* style: dbl click */
415 wc.lpfnWndProc = (WNDPROC)DirectXEventProc; /* event handler */
416 wc.cbClsExtra = 0; /* no extra class data */
417 wc.cbWndExtra = 0; /* no extra window data */
418 wc.hInstance = hInstance; /* instance */
419 wc.hIcon = vlc_icon; /* load the vlc big icon */
420 wc.hCursor = LoadCursor(NULL, IDC_ARROW); /* default cursor */
421 wc.hbrBackground = GetStockObject(BLACK_BRUSH); /* background color */
422 wc.lpszMenuName = NULL; /* no menu */
423 wc.lpszClassName = _T("VLC DirectX"); /* use a special class */
425 /* Register the window class */
426 if( !RegisterClass(&wc) )
430 if( vlc_icon ) DestroyIcon( vlc_icon );
432 /* Check why it failed. If it's because one already exists
433 * then fine, otherwise return with an error. */
434 if( !GetClassInfo( hInstance, _T("VLC DirectX"), &wndclass ) )
436 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED" );
441 /* Register the video sub-window class */
442 wc.lpszClassName = _T("VLC DirectX video"); wc.hIcon = 0;
443 if( !RegisterClass(&wc) )
447 /* Check why it failed. If it's because one already exists
448 * then fine, otherwise return with an error. */
449 if( !GetClassInfo( hInstance, _T("VLC DirectX video"), &wndclass ) )
451 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED" );
456 /* When you create a window you give the dimensions you wish it to
457 * have. Unfortunatly these dimensions will include the borders and
458 * titlebar. We use the following function to find out the size of
459 * the window corresponding to the useable surface we want */
460 rect_window.top = 10;
461 rect_window.left = 10;
462 rect_window.right = rect_window.left + p_vout->p_sys->i_window_width;
463 rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
465 if( var_GetBool( p_vout, "video-deco" ) )
467 /* Open with window decoration */
468 AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
469 i_style = WS_OVERLAPPEDWINDOW|WS_SIZEBOX|WS_VISIBLE|WS_CLIPCHILDREN;
474 /* No window decoration */
475 AdjustWindowRect( &rect_window, WS_POPUP, 0 );
476 i_style = WS_POPUP|WS_VISIBLE|WS_CLIPCHILDREN;
477 i_stylex = 0; // WS_EX_TOOLWINDOW; Is TOOLWINDOW really needed ?
478 // It messes up the fullscreen window.
481 if( p_vout->p_sys->hparent )
483 i_style = WS_VISIBLE|WS_CLIPCHILDREN|WS_CHILD;
487 p_vout->p_sys->i_window_style = i_style;
489 /* Create the window */
490 p_vout->p_sys->hwnd =
491 CreateWindowEx( WS_EX_NOPARENTNOTIFY | i_stylex,
492 _T("VLC DirectX"), /* name of window class */
493 _T(VOUT_TITLE) _T(" (DirectX Output)"), /* window title */
494 i_style, /* window style */
495 (p_vout->p_sys->i_window_x < 0) ? CW_USEDEFAULT :
496 (UINT)p_vout->p_sys->i_window_x, /* default X coordinate */
497 (p_vout->p_sys->i_window_y < 0) ? CW_USEDEFAULT :
498 (UINT)p_vout->p_sys->i_window_y, /* default Y coordinate */
499 rect_window.right - rect_window.left, /* window width */
500 rect_window.bottom - rect_window.top, /* window height */
501 p_vout->p_sys->hparent, /* parent window */
502 NULL, /* no menu in this window */
503 hInstance, /* handle of this program instance */
504 (LPVOID)p_vout ); /* send p_vout to WM_CREATE */
506 if( !p_vout->p_sys->hwnd )
508 msg_Warn( p_vout, "DirectXCreateWindow create window FAILED" );
512 if( p_vout->p_sys->hparent )
516 /* We don't want the window owner to overwrite our client area */
517 i_style = GetWindowLong( p_vout->p_sys->hparent, GWL_STYLE );
519 if( !(i_style & WS_CLIPCHILDREN) )
520 /* Hmmm, apparently this is a blocking call... */
521 SetWindowLong( p_vout->p_sys->hparent, GWL_STYLE,
522 i_style | WS_CLIPCHILDREN );
524 /* Create our fullscreen window */
525 p_vout->p_sys->hfswnd =
526 CreateWindowEx( WS_EX_APPWINDOW, _T("VLC DirectX"),
527 _T(VOUT_TITLE) _T(" (DirectX Output)"),
528 WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN|WS_SIZEBOX,
529 CW_USEDEFAULT, CW_USEDEFAULT,
530 CW_USEDEFAULT, CW_USEDEFAULT,
531 NULL, NULL, hInstance, NULL );
534 /* Append a "Always On Top" entry in the system menu */
535 hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
536 AppendMenu( hMenu, MF_SEPARATOR, 0, _T("") );
537 AppendMenu( hMenu, MF_STRING | MF_UNCHECKED,
538 IDM_TOGGLE_ON_TOP, _T("Always on &Top") );
540 /* Create video sub-window. This sub window will always exactly match
541 * the size of the video, which allows us to use crazy overlay colorkeys
542 * without having them shown outside of the video area. */
543 p_vout->p_sys->hvideownd =
544 CreateWindow( _T("VLC DirectX video"), _T(""), /* window class */
545 WS_CHILD | WS_VISIBLE, /* window style */
547 p_vout->render.i_width, /* default width */
548 p_vout->render.i_height, /* default height */
549 p_vout->p_sys->hwnd, /* parent window */
551 (LPVOID)p_vout ); /* send p_vout to WM_CREATE */
553 if( !p_vout->p_sys->hvideownd )
554 msg_Warn( p_vout, "can't create video sub-window" );
556 msg_Dbg( p_vout, "created video sub-window" );
558 /* Now display the window */
559 ShowWindow( p_vout->p_sys->hwnd, SW_SHOW );
564 /*****************************************************************************
565 * DirectXCloseWindow: close the window created by DirectXCreateWindow
566 *****************************************************************************
567 * This function returns all resources allocated by DirectXCreateWindow.
568 *****************************************************************************/
569 static void DirectXCloseWindow( vout_thread_t *p_vout )
571 msg_Dbg( p_vout, "DirectXCloseWindow" );
573 DestroyWindow( p_vout->p_sys->hwnd );
574 if( p_vout->p_sys->hfswnd ) DestroyWindow( p_vout->p_sys->hfswnd );
576 if( p_vout->p_sys->hparent )
577 vout_ReleaseWindow( p_vout, (void *)p_vout->p_sys->hparent );
579 p_vout->p_sys->hwnd = NULL;
581 /* We don't unregister the Window Class because it could lead to race
582 * conditions and it will be done anyway by the system when the app will
586 /*****************************************************************************
587 * DirectXUpdateRects: update clipping rectangles
588 *****************************************************************************
589 * This function is called when the window position or size are changed, and
590 * its job is to update the source and destination RECTs used to display the
592 *****************************************************************************/
593 void E_(DirectXUpdateRects)( vout_thread_t *p_vout, vlc_bool_t b_force )
595 #define rect_src p_vout->p_sys->rect_src
596 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
597 #define rect_dest p_vout->p_sys->rect_dest
598 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
600 int i_width, i_height, i_x, i_y;
605 /* Retrieve the window size */
606 GetClientRect( p_vout->p_sys->hwnd, &rect );
608 /* Retrieve the window position */
609 point.x = point.y = 0;
610 ClientToScreen( p_vout->p_sys->hwnd, &point );
612 /* If nothing changed, we can return */
614 && p_vout->p_sys->i_window_width == rect.right
615 && p_vout->p_sys->i_window_height == rect.bottom
616 && p_vout->p_sys->i_window_x == point.x
617 && p_vout->p_sys->i_window_y == point.y )
622 /* Update the window position and size */
623 p_vout->p_sys->i_window_x = point.x;
624 p_vout->p_sys->i_window_y = point.y;
625 p_vout->p_sys->i_window_width = rect.right;
626 p_vout->p_sys->i_window_height = rect.bottom;
628 vout_PlacePicture( p_vout, rect.right, rect.bottom,
629 &i_x, &i_y, &i_width, &i_height );
631 if( p_vout->p_sys->hvideownd )
632 SetWindowPos( p_vout->p_sys->hvideownd, HWND_TOP,
633 i_x, i_y, i_width, i_height, 0 );
635 /* Destination image position and dimensions */
636 rect_dest.left = point.x + i_x;
637 rect_dest.right = rect_dest.left + i_width;
638 rect_dest.top = point.y + i_y;
639 rect_dest.bottom = rect_dest.top + i_height;
641 #ifdef MODULE_NAME_IS_vout_directx
642 /* Apply overlay hardware constraints */
643 if( p_vout->p_sys->b_using_overlay )
645 if( p_vout->p_sys->i_align_dest_boundary )
646 rect_dest.left = ( rect_dest.left +
647 p_vout->p_sys->i_align_dest_boundary / 2 ) &
648 ~p_vout->p_sys->i_align_dest_boundary;
650 if( p_vout->p_sys->i_align_dest_size )
651 rect_dest.right = (( rect_dest.right - rect_dest.left +
652 p_vout->p_sys->i_align_dest_size / 2 ) &
653 ~p_vout->p_sys->i_align_dest_size) + rect_dest.left;
657 /* UpdateOverlay directdraw function doesn't automatically clip to the
658 * display size so we need to do it otherwise it will fail */
660 /* Clip the destination window */
661 if( !IntersectRect( &rect_dest_clipped, &rect_dest,
662 &p_vout->p_sys->rect_display ) )
664 SetRectEmpty( &rect_src_clipped );
669 msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
671 rect_dest_clipped.left, rect_dest_clipped.top,
672 rect_dest_clipped.right, rect_dest_clipped.bottom );
675 /* the 2 following lines are to fix a bug when clicking on the desktop */
676 if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
677 (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
679 SetRectEmpty( &rect_src_clipped );
683 /* src image dimensions */
686 rect_src.right = p_vout->render.i_width;
687 rect_src.bottom = p_vout->render.i_height;
689 /* Clip the source image */
690 rect_src_clipped.left = p_vout->fmt_out.i_x_offset +
691 (rect_dest_clipped.left - rect_dest.left) *
692 p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
693 rect_src_clipped.right = p_vout->fmt_out.i_x_offset +
694 p_vout->fmt_out.i_visible_width -
695 (rect_dest.right - rect_dest_clipped.right) *
696 p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
697 rect_src_clipped.top = p_vout->fmt_out.i_y_offset +
698 (rect_dest_clipped.top - rect_dest.top) *
699 p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
700 rect_src_clipped.bottom = p_vout->fmt_out.i_y_offset +
701 p_vout->fmt_out.i_visible_height -
702 (rect_dest.bottom - rect_dest_clipped.bottom) *
703 p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
705 #ifdef MODULE_NAME_IS_vout_directx
706 /* Apply overlay hardware constraints */
707 if( p_vout->p_sys->b_using_overlay )
709 if( p_vout->p_sys->i_align_src_boundary )
710 rect_src_clipped.left = ( rect_src_clipped.left +
711 p_vout->p_sys->i_align_src_boundary / 2 ) &
712 ~p_vout->p_sys->i_align_src_boundary;
714 if( p_vout->p_sys->i_align_src_size )
715 rect_src_clipped.right = (( rect_src_clipped.right -
716 rect_src_clipped.left +
717 p_vout->p_sys->i_align_src_size / 2 ) &
718 ~p_vout->p_sys->i_align_src_size) + rect_src_clipped.left;
723 msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
724 " coords: %i,%i,%i,%i",
725 rect_src_clipped.left, rect_src_clipped.top,
726 rect_src_clipped.right, rect_src_clipped.bottom );
729 /* The destination coordinates need to be relative to the current
730 * directdraw primary surface (display) */
731 rect_dest_clipped.left -= p_vout->p_sys->rect_display.left;
732 rect_dest_clipped.right -= p_vout->p_sys->rect_display.left;
733 rect_dest_clipped.top -= p_vout->p_sys->rect_display.top;
734 rect_dest_clipped.bottom -= p_vout->p_sys->rect_display.top;
736 #ifdef MODULE_NAME_IS_vout_directx
737 if( p_vout->p_sys->b_using_overlay )
738 E_(DirectXUpdateOverlay)( p_vout );
741 /* Signal the change in size/position */
742 p_vout->p_sys->i_changes |= DX_POSITION_CHANGE;
745 #undef rect_src_clipped
747 #undef rect_dest_clipped
750 /*****************************************************************************
751 * DirectXEventProc: This is the window event processing function.
752 *****************************************************************************
753 * On Windows, when you create a window you have to attach an event processing
754 * function to it. The aim of this function is to manage "Queued Messages" and
755 * "Nonqueued Messages".
756 * Queued Messages are those picked up and retransmitted by vout_Manage
757 * (using the GetMessage and DispatchMessage functions).
758 * Nonqueued Messages are those that Windows will send directly to this
759 * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
760 *****************************************************************************/
761 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
762 WPARAM wParam, LPARAM lParam )
764 vout_thread_t *p_vout;
766 if( message == WM_CREATE )
768 /* Store p_vout for future use */
769 p_vout = (vout_thread_t *)((CREATESTRUCT *)lParam)->lpCreateParams;
770 SetWindowLongPtr( hwnd, GWLP_USERDATA, (LONG_PTR)p_vout );
774 p_vout = (vout_thread_t *)GetWindowLongPtr( hwnd, GWLP_USERDATA );
777 /* Catch the screensaver and the monitor turn-off */
778 if( message == WM_SYSCOMMAND &&
779 ( (wParam & 0xFFF0) == SC_SCREENSAVE || (wParam & 0xFFF0) == SC_MONITORPOWER ) )
781 //if( p_vout ) msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND screensaver" );
782 return 0; /* this stops them from happening */
787 /* Hmmm mozilla does manage somehow to save the pointer to our
788 * windowproc and still calls it after the vout has been closed. */
789 return DefWindowProc(hwnd, message, wParam, lParam);
792 if( hwnd == p_vout->p_sys->hvideownd )
793 return DefWindowProc(hwnd, message, wParam, lParam);
798 case WM_WINDOWPOSCHANGED:
799 E_(DirectXUpdateRects)( p_vout, VLC_TRUE );
802 /* the user wants to close the window */
805 playlist_t * p_playlist =
806 (playlist_t *)vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
808 if( p_playlist == NULL )
813 playlist_Stop( p_playlist );
814 vlc_object_release( p_playlist );
818 /* the window has been closed so shut down everything now */
820 msg_Dbg( p_vout, "WinProc WM_DESTROY" );
821 /* just destroy the window */
822 PostQuitMessage( 0 );
828 case IDM_TOGGLE_ON_TOP: /* toggle the "on top" status */
831 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND: IDM_TOGGLE_ON_TOP");
833 /* Change the current value */
834 var_Get( p_vout, "video-on-top", &val );
835 val.b_bool = !val.b_bool;
836 var_Set( p_vout, "video-on-top", val );
845 /* We do not want to relay these messages to the parent window
846 * because we rely on the background color for the overlay. */
847 return DefWindowProc(hwnd, message, wParam, lParam);
851 //msg_Dbg( p_vout, "WinProc WM Default %i", message );
855 /* Let windows handle the message */
856 return DefWindowProc(hwnd, message, wParam, lParam);
864 } dxkeys_to_vlckeys[] =
866 { VK_F1, KEY_F1 }, { VK_F2, KEY_F2 }, { VK_F3, KEY_F3 }, { VK_F4, KEY_F4 },
867 { VK_F5, KEY_F5 }, { VK_F6, KEY_F6 }, { VK_F7, KEY_F7 }, { VK_F8, KEY_F8 },
868 { VK_F9, KEY_F9 }, { VK_F10, KEY_F10 }, { VK_F11, KEY_F11 },
871 { VK_RETURN, KEY_ENTER },
872 { VK_SPACE, KEY_SPACE },
873 { VK_ESCAPE, KEY_ESC },
875 { VK_LEFT, KEY_LEFT },
876 { VK_RIGHT, KEY_RIGHT },
878 { VK_DOWN, KEY_DOWN },
880 { VK_HOME, KEY_HOME },
882 { VK_PRIOR, KEY_PAGEUP },
883 { VK_NEXT, KEY_PAGEDOWN },
885 { VK_INSERT, KEY_INSERT },
886 { VK_DELETE, KEY_DELETE },
892 { VK_BROWSER_BACK, KEY_BROWSER_BACK },
893 { VK_BROWSER_FORWARD, KEY_BROWSER_FORWARD },
894 { VK_BROWSER_REFRESH, KEY_BROWSER_REFRESH },
895 { VK_BROWSER_STOP, KEY_BROWSER_STOP },
896 { VK_BROWSER_SEARCH, KEY_BROWSER_SEARCH },
897 { VK_BROWSER_FAVORITES, KEY_BROWSER_FAVORITES },
898 { VK_BROWSER_HOME, KEY_BROWSER_HOME },
899 { VK_VOLUME_MUTE, KEY_VOLUME_MUTE },
900 { VK_VOLUME_DOWN, KEY_VOLUME_DOWN },
901 { VK_VOLUME_UP, KEY_VOLUME_UP },
902 { VK_MEDIA_NEXT_TRACK, KEY_MEDIA_NEXT_TRACK },
903 { VK_MEDIA_PREV_TRACK, KEY_MEDIA_PREV_TRACK },
904 { VK_MEDIA_STOP, KEY_MEDIA_STOP },
905 { VK_MEDIA_PLAY_PAUSE, KEY_MEDIA_PLAY_PAUSE },
910 static int DirectXConvertKey( int i_key )
914 for( i = 0; dxkeys_to_vlckeys[i].i_dxkey != 0; i++ )
916 if( dxkeys_to_vlckeys[i].i_dxkey == i_key )
918 return dxkeys_to_vlckeys[i].i_vlckey;
925 /*****************************************************************************
926 * Control: control facility for the vout
927 *****************************************************************************/
928 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
930 unsigned int *pi_width, *pi_height;
937 if( p_vout->p_sys->hparent )
938 return vout_ControlWindow( p_vout,
939 (void *)p_vout->p_sys->hparent, i_query, args );
941 pi_width = va_arg( args, unsigned int * );
942 pi_height = va_arg( args, unsigned int * );
944 GetClientRect( p_vout->p_sys->hwnd, &rect_window );
946 *pi_width = rect_window.right - rect_window.left;
947 *pi_height = rect_window.bottom - rect_window.top;
951 if( p_vout->p_sys->hparent )
952 return vout_ControlWindow( p_vout,
953 (void *)p_vout->p_sys->hparent, i_query, args );
955 /* Update dimensions */
956 rect_window.top = rect_window.left = 0;
957 rect_window.right = va_arg( args, unsigned int );
958 rect_window.bottom = va_arg( args, unsigned int );
959 if( !rect_window.right ) rect_window.right = p_vout->i_window_width;
960 if( !rect_window.bottom ) rect_window.bottom = p_vout->i_window_height;
961 AdjustWindowRect( &rect_window, p_vout->p_sys->i_window_style, 0 );
963 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
964 rect_window.right - rect_window.left,
965 rect_window.bottom - rect_window.top, SWP_NOMOVE );
970 ShowWindow( p_vout->p_sys->hwnd, SW_HIDE );
972 /* Change window style, borders and title bar */
973 vlc_mutex_lock( &p_vout->p_sys->lock );
974 p_vout->p_sys->hparent = 0;
975 vlc_mutex_unlock( &p_vout->p_sys->lock );
977 /* Retrieve the window position */
978 point.x = point.y = 0;
979 ClientToScreen( p_vout->p_sys->hwnd, &point );
981 SetParent( p_vout->p_sys->hwnd, 0 );
982 p_vout->p_sys->i_window_style =
983 WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW | WS_SIZEBOX;
984 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE,
985 p_vout->p_sys->i_window_style |
986 (i_query == VOUT_CLOSE ? 0 : WS_VISIBLE) );
987 SetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW );
988 SetWindowPos( p_vout->p_sys->hwnd, 0, point.x, point.y, 0, 0,
989 SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED );
991 return vout_vaControlDefault( p_vout, i_query, args );
993 case VOUT_SET_STAY_ON_TOP:
994 if( p_vout->p_sys->hparent )
995 return vout_ControlWindow( p_vout,
996 (void *)p_vout->p_sys->hparent, i_query, args );
998 p_vout->p_sys->b_on_top_change = VLC_TRUE;
1002 return vout_vaControlDefault( p_vout, i_query, args );