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., 59 Temple Place - Suite 330, Boston, MA 02111, 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 0x0400
40 #include <vlc/input.h>
52 /*****************************************************************************
54 *****************************************************************************/
55 static int DirectXCreateWindow( vout_thread_t *p_vout );
56 static void DirectXCloseWindow ( vout_thread_t *p_vout );
57 static long FAR PASCAL DirectXEventProc( HWND, UINT, WPARAM, LPARAM );
59 static int Control( vout_thread_t *p_vout, int i_query, va_list args );
61 static void DirectXPopupMenu( event_thread_t *p_event, vlc_bool_t b_open )
63 playlist_t *p_playlist =
64 vlc_object_find( p_event, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
65 if( p_playlist != NULL )
69 var_Set( p_playlist, "intf-popupmenu", val );
70 vlc_object_release( p_playlist );
74 static int DirectXConvertKey( int i_key );
76 /*****************************************************************************
77 * DirectXEventThread: Create video window & handle its messages
78 *****************************************************************************
79 * This function creates a video window and then enters an infinite loop
80 * that handles the messages sent to that window.
81 * The main goal of this thread is to isolate the Win32 PeekMessage function
82 * because this one can block for a long time.
83 *****************************************************************************/
84 void E_(DirectXEventThread)( event_thread_t *p_event )
87 POINT old_mouse_pos = {0,0}, mouse_pos;
89 int i_width, i_height, i_x, i_y;
93 p_event->p_vout->pf_control = Control;
95 /* Create a window for the video */
96 /* Creating a window under Windows also initializes the thread's event
98 if( DirectXCreateWindow( p_event->p_vout ) )
100 msg_Err( p_event, "out of memory" );
101 p_event->b_dead = VLC_TRUE;
104 /* Signal the creation of the window */
105 vlc_thread_ready( p_event );
107 /* Set power management stuff */
108 if( (hkernel32 = GetModuleHandle( _T("KERNEL32") ) ) )
110 ULONG (WINAPI* OurSetThreadExecutionState)( ULONG );
112 OurSetThreadExecutionState = (ULONG (WINAPI*)( ULONG ))
113 GetProcAddress( hkernel32, _T("SetThreadExecutionState") );
115 if( OurSetThreadExecutionState )
116 /* Prevent monitor from powering off */
117 OurSetThreadExecutionState( ES_DISPLAY_REQUIRED | ES_CONTINUOUS );
119 msg_Dbg( p_event, "no support for SetThreadExecutionState()" );
123 /* GetMessage will sleep if there's no message in the queue */
124 while( !p_event->b_die && GetMessage( &msg, 0, 0, 0 ) )
126 /* Check if we are asked to exit */
130 switch( msg.message )
134 vout_PlacePicture( p_event->p_vout,
135 p_event->p_vout->p_sys->i_window_width,
136 p_event->p_vout->p_sys->i_window_height,
137 &i_x, &i_y, &i_width, &i_height );
139 if( msg.hwnd == p_event->p_vout->p_sys->hvideownd )
145 if( i_width && i_height )
147 val.i_int = ( GET_X_LPARAM(msg.lParam) - i_x ) *
148 p_event->p_vout->fmt_in.i_visible_width / i_width +
149 p_event->p_vout->fmt_in.i_x_offset;
150 var_Set( p_event->p_vout, "mouse-x", val );
151 val.i_int = ( GET_Y_LPARAM(msg.lParam) - i_y ) *
152 p_event->p_vout->fmt_in.i_visible_height / i_height +
153 p_event->p_vout->fmt_in.i_y_offset;
154 var_Set( p_event->p_vout, "mouse-y", val );
156 val.b_bool = VLC_TRUE;
157 var_Set( p_event->p_vout, "mouse-moved", val );
161 GetCursorPos( &mouse_pos );
162 if( (abs(mouse_pos.x - old_mouse_pos.x) > 2 ||
163 (abs(mouse_pos.y - old_mouse_pos.y)) > 2 ) )
165 GetCursorPos( &old_mouse_pos );
166 p_event->p_vout->p_sys->i_lastmoved = mdate();
168 if( p_event->p_vout->p_sys->b_cursor_hidden )
170 p_event->p_vout->p_sys->b_cursor_hidden = 0;
176 case WM_VLC_HIDE_MOUSE:
177 if( p_event->p_vout->p_sys->b_cursor_hidden ) break;
178 p_event->p_vout->p_sys->b_cursor_hidden = VLC_TRUE;
179 GetCursorPos( &old_mouse_pos );
183 case WM_VLC_SHOW_MOUSE:
184 if( !p_event->p_vout->p_sys->b_cursor_hidden ) break;
185 p_event->p_vout->p_sys->b_cursor_hidden = VLC_FALSE;
186 GetCursorPos( &old_mouse_pos );
191 var_Get( p_event->p_vout, "mouse-button-down", &val );
193 var_Set( p_event->p_vout, "mouse-button-down", val );
194 DirectXPopupMenu( p_event, VLC_FALSE );
198 var_Get( p_event->p_vout, "mouse-button-down", &val );
200 var_Set( p_event->p_vout, "mouse-button-down", val );
202 val.b_bool = VLC_TRUE;
203 var_Set( p_event->p_vout, "mouse-clicked", val );
206 case WM_LBUTTONDBLCLK:
207 p_event->p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
211 var_Get( p_event->p_vout, "mouse-button-down", &val );
213 var_Set( p_event->p_vout, "mouse-button-down", val );
214 DirectXPopupMenu( p_event, VLC_FALSE );
218 var_Get( p_event->p_vout, "mouse-button-down", &val );
220 var_Set( p_event->p_vout, "mouse-button-down", val );
224 var_Get( p_event->p_vout, "mouse-button-down", &val );
226 var_Set( p_event->p_vout, "mouse-button-down", val );
227 DirectXPopupMenu( p_event, VLC_FALSE );
231 var_Get( p_event->p_vout, "mouse-button-down", &val );
233 var_Set( p_event->p_vout, "mouse-button-down", val );
234 DirectXPopupMenu( p_event, VLC_TRUE );
239 /* The key events are first processed here and not translated
240 * into WM_CHAR events because we need to know the status of the
242 val.i_int = DirectXConvertKey( msg.wParam );
245 /* This appears to be a "normal" (ascii) key */
246 val.i_int = tolower( MapVirtualKey( msg.wParam, 2 ) );
251 if( GetKeyState(VK_CONTROL) & 0x8000 )
253 val.i_int |= KEY_MODIFIER_CTRL;
255 if( GetKeyState(VK_SHIFT) & 0x8000 )
257 val.i_int |= KEY_MODIFIER_SHIFT;
259 if( GetKeyState(VK_MENU) & 0x8000 )
261 val.i_int |= KEY_MODIFIER_ALT;
264 var_Set( p_event->p_vlc, "key-pressed", val );
269 if( GET_WHEEL_DELTA_WPARAM( msg.wParam ) > 0 )
271 val.i_int = KEY_MOUSEWHEELUP;
275 val.i_int = KEY_MOUSEWHEELDOWN;
279 if( GetKeyState(VK_CONTROL) & 0x8000 )
281 val.i_int |= KEY_MODIFIER_CTRL;
283 if( GetKeyState(VK_SHIFT) & 0x8000 )
285 val.i_int |= KEY_MODIFIER_SHIFT;
287 if( GetKeyState(VK_MENU) & 0x8000 )
289 val.i_int |= KEY_MODIFIER_ALT;
292 var_Set( p_event->p_vlc, "key-pressed", val );
296 case WM_VLC_CHANGE_TEXT:
297 var_Get( p_event->p_vout, "video-title", &val );
299 if( !val.psz_string || !*val.psz_string ) /* Default video title */
301 #ifdef MODULE_NAME_IS_glwin32
302 SetWindowText( p_event->p_vout->p_sys->hwnd,
303 _T(VOUT_TITLE) _T(" (OpenGL output)") );
305 if( p_event->p_vout->p_sys->b_using_overlay )
306 SetWindowText( p_event->p_vout->p_sys->hwnd, _T(VOUT_TITLE)
307 _T(" (hardware YUV overlay DirectX output)") );
308 else if( p_event->p_vout->p_sys->b_hw_yuv )
309 SetWindowText( p_event->p_vout->p_sys->hwnd, _T(VOUT_TITLE)
310 _T(" (hardware YUV DirectX output)") );
312 SetWindowText( p_event->p_vout->p_sys->hwnd, _T(VOUT_TITLE)
313 _T(" (software RGB DirectX output)") );
318 SetWindowText( p_event->p_vout->p_sys->hwnd, val.psz_string );
323 /* Messages we don't handle directly are dispatched to the
324 * window procedure */
325 TranslateMessage(&msg);
326 DispatchMessage(&msg);
331 } /* End Main loop */
333 /* Check for WM_QUIT if we created the window */
334 if( !p_event->p_vout->p_sys->hparent && msg.message == WM_QUIT )
336 msg_Warn( p_event, "WM_QUIT... should not happen!!" );
337 p_event->p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
340 msg_Dbg( p_event, "DirectXEventThread terminating" );
342 /* clear the changes formerly signaled */
343 p_event->p_vout->p_sys->i_changes = 0;
345 DirectXCloseWindow( p_event->p_vout );
349 /* following functions are local */
351 /*****************************************************************************
352 * DirectXCreateWindow: create a window for the video.
353 *****************************************************************************
354 * Before creating a direct draw surface, we need to create a window in which
355 * the video will be displayed. This window will also allow us to capture the
357 *****************************************************************************/
358 static int DirectXCreateWindow( vout_thread_t *p_vout )
363 WNDCLASS wc; /* window class components */
364 HICON vlc_icon = NULL;
365 char vlc_path[MAX_PATH+1];
366 int i_style, i_stylex;
368 msg_Dbg( p_vout, "DirectXCreateWindow" );
370 /* Get this module's instance */
371 hInstance = GetModuleHandle(NULL);
373 /* If an external window was specified, we'll draw in it. */
374 p_vout->p_sys->hparent =
375 vout_RequestWindow( p_vout, &p_vout->p_sys->i_window_x,
376 &p_vout->p_sys->i_window_y,
377 &p_vout->p_sys->i_window_width,
378 &p_vout->p_sys->i_window_height );
380 /* We create the window ourself, there is no previous window proc. */
381 p_vout->p_sys->pf_wndproc = NULL;
383 /* Get the Icon from the main app */
386 if( GetModuleFileName( NULL, vlc_path, MAX_PATH ) )
388 vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
392 /* Fill in the window class structure */
393 wc.style = CS_OWNDC|CS_DBLCLKS; /* style: dbl click */
394 wc.lpfnWndProc = (WNDPROC)DirectXEventProc; /* event handler */
395 wc.cbClsExtra = 0; /* no extra class data */
396 wc.cbWndExtra = 0; /* no extra window data */
397 wc.hInstance = hInstance; /* instance */
398 wc.hIcon = vlc_icon; /* load the vlc big icon */
399 wc.hCursor = LoadCursor(NULL, IDC_ARROW); /* default cursor */
400 wc.hbrBackground = GetStockObject(BLACK_BRUSH); /* background color */
401 wc.lpszMenuName = NULL; /* no menu */
402 wc.lpszClassName = _T("VLC DirectX"); /* use a special class */
404 /* Register the window class */
405 if( !RegisterClass(&wc) )
409 if( vlc_icon ) DestroyIcon( vlc_icon );
411 /* Check why it failed. If it's because one already exists
412 * then fine, otherwise return with an error. */
413 if( !GetClassInfo( hInstance, _T("VLC DirectX"), &wndclass ) )
415 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED" );
420 /* Register the video sub-window class */
421 wc.lpszClassName = _T("VLC DirectX video"); wc.hIcon = 0;
422 if( !RegisterClass(&wc) )
426 /* Check why it failed. If it's because one already exists
427 * then fine, otherwise return with an error. */
428 if( !GetClassInfo( hInstance, _T("VLC DirectX video"), &wndclass ) )
430 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED" );
435 /* When you create a window you give the dimensions you wish it to
436 * have. Unfortunatly these dimensions will include the borders and
437 * titlebar. We use the following function to find out the size of
438 * the window corresponding to the useable surface we want */
439 rect_window.top = 10;
440 rect_window.left = 10;
441 rect_window.right = rect_window.left + p_vout->p_sys->i_window_width;
442 rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
444 if( var_GetBool( p_vout, "video-deco" ) )
446 /* Open with window decoration */
447 AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
448 i_style = WS_OVERLAPPEDWINDOW|WS_SIZEBOX|WS_VISIBLE|WS_CLIPCHILDREN;
453 /* No window decoration */
454 AdjustWindowRect( &rect_window, WS_POPUP, 0 );
455 i_style = WS_POPUP|WS_VISIBLE|WS_CLIPCHILDREN;
456 i_stylex = 0; // WS_EX_TOOLWINDOW; Is TOOLWINDOW really needed ?
457 // It messes up the fullscreen window.
460 if( p_vout->p_sys->hparent )
462 i_style = WS_VISIBLE|WS_CLIPCHILDREN|WS_CHILD;
466 p_vout->p_sys->i_window_style = i_style;
468 /* Create the window */
469 p_vout->p_sys->hwnd =
470 CreateWindowEx( WS_EX_NOPARENTNOTIFY | i_stylex,
471 _T("VLC DirectX"), /* name of window class */
472 _T(VOUT_TITLE) _T(" (DirectX Output)"), /* window title */
473 i_style, /* window style */
474 (p_vout->p_sys->i_window_x < 0) ? CW_USEDEFAULT :
475 p_vout->p_sys->i_window_x, /* default X coordinate */
476 (p_vout->p_sys->i_window_y < 0) ? CW_USEDEFAULT :
477 p_vout->p_sys->i_window_y, /* default Y coordinate */
478 rect_window.right - rect_window.left, /* window width */
479 rect_window.bottom - rect_window.top, /* window height */
480 p_vout->p_sys->hparent, /* parent window */
481 NULL, /* no menu in this window */
482 hInstance, /* handle of this program instance */
483 (LPVOID)p_vout ); /* send p_vout to WM_CREATE */
485 if( !p_vout->p_sys->hwnd )
487 msg_Warn( p_vout, "DirectXCreateWindow create window FAILED" );
491 if( p_vout->p_sys->hparent )
495 /* We don't want the window owner to overwrite our client area */
496 i_style = GetWindowLong( p_vout->p_sys->hparent, GWL_STYLE );
498 if( !(i_style & WS_CLIPCHILDREN) )
499 /* Hmmm, apparently this is a blocking call... */
500 SetWindowLong( p_vout->p_sys->hparent, GWL_STYLE,
501 i_style | WS_CLIPCHILDREN );
503 /* Create our fullscreen window */
504 p_vout->p_sys->hfswnd =
505 CreateWindowEx( WS_EX_APPWINDOW, _T("VLC DirectX"),
506 _T(VOUT_TITLE) _T(" (DirectX Output)"),
507 WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN|WS_SIZEBOX,
508 CW_USEDEFAULT, CW_USEDEFAULT,
509 CW_USEDEFAULT, CW_USEDEFAULT,
510 NULL, NULL, hInstance, NULL );
513 /* Now display the window */
514 ShowWindow( p_vout->p_sys->hwnd, SW_SHOW );
516 /* Create video sub-window. This sub window will always exactly match
517 * the size of the video, which allows us to use crazy overlay colorkeys
518 * without having them shown outside of the video area. */
519 SendMessage( p_vout->p_sys->hwnd, WM_VLC_CREATE_VIDEO_WIN, 0, 0 );
521 /* Append a "Always On Top" entry in the system menu */
522 hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
523 AppendMenu( hMenu, MF_SEPARATOR, 0, _T("") );
524 AppendMenu( hMenu, MF_STRING | MF_UNCHECKED,
525 IDM_TOGGLE_ON_TOP, _T("Always on &Top") );
530 /*****************************************************************************
531 * DirectXCloseWindow: close the window created by DirectXCreateWindow
532 *****************************************************************************
533 * This function returns all resources allocated by DirectXCreateWindow.
534 *****************************************************************************/
535 static void DirectXCloseWindow( vout_thread_t *p_vout )
537 msg_Dbg( p_vout, "DirectXCloseWindow" );
539 DestroyWindow( p_vout->p_sys->hwnd );
540 if( p_vout->p_sys->hfswnd ) DestroyWindow( p_vout->p_sys->hfswnd );
542 if( p_vout->p_sys->hparent )
543 vout_ReleaseWindow( p_vout, (void *)p_vout->p_sys->hparent );
545 p_vout->p_sys->hwnd = NULL;
547 /* We don't unregister the Window Class because it could lead to race
548 * conditions and it will be done anyway by the system when the app will
552 /*****************************************************************************
553 * DirectXUpdateRects: update clipping rectangles
554 *****************************************************************************
555 * This function is called when the window position or size are changed, and
556 * its job is to update the source and destination RECTs used to display the
558 *****************************************************************************/
559 void E_(DirectXUpdateRects)( vout_thread_t *p_vout, vlc_bool_t b_force )
561 #define rect_src p_vout->p_sys->rect_src
562 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
563 #define rect_dest p_vout->p_sys->rect_dest
564 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
566 int i_width, i_height, i_x, i_y;
571 /* Retrieve the window size */
572 GetClientRect( p_vout->p_sys->hwnd, &rect );
574 /* Retrieve the window position */
575 point.x = point.y = 0;
576 ClientToScreen( p_vout->p_sys->hwnd, &point );
578 /* If nothing changed, we can return */
580 && p_vout->p_sys->i_window_width == rect.right
581 && p_vout->p_sys->i_window_height == rect.bottom
582 && p_vout->p_sys->i_window_x == point.x
583 && p_vout->p_sys->i_window_y == point.y )
588 /* Update the window position and size */
589 p_vout->p_sys->i_window_x = point.x;
590 p_vout->p_sys->i_window_y = point.y;
591 p_vout->p_sys->i_window_width = rect.right;
592 p_vout->p_sys->i_window_height = rect.bottom;
594 vout_PlacePicture( p_vout, rect.right, rect.bottom,
595 &i_x, &i_y, &i_width, &i_height );
597 if( p_vout->p_sys->hvideownd )
598 SetWindowPos( p_vout->p_sys->hvideownd, HWND_TOP,
599 i_x, i_y, i_width, i_height, 0 );
601 /* Destination image position and dimensions */
602 rect_dest.left = point.x + i_x;
603 rect_dest.right = rect_dest.left + i_width;
604 rect_dest.top = point.y + i_y;
605 rect_dest.bottom = rect_dest.top + i_height;
607 /* Apply overlay hardware constraints */
608 if( p_vout->p_sys->b_using_overlay )
610 if( p_vout->p_sys->i_align_dest_boundary )
611 rect_dest.left = ( rect_dest.left +
612 p_vout->p_sys->i_align_dest_boundary / 2 ) &
613 ~p_vout->p_sys->i_align_dest_boundary;
615 if( p_vout->p_sys->i_align_dest_size )
616 rect_dest.right = (( rect_dest.right - rect_dest.left +
617 p_vout->p_sys->i_align_dest_size / 2 ) &
618 ~p_vout->p_sys->i_align_dest_size) + rect_dest.left;
621 /* UpdateOverlay directdraw function doesn't automatically clip to the
622 * display size so we need to do it otherwise it will fail */
624 /* Clip the destination window */
625 if( !IntersectRect( &rect_dest_clipped, &rect_dest,
626 &p_vout->p_sys->rect_display ) )
628 SetRectEmpty( &rect_src_clipped );
633 msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
635 rect_dest_clipped.left, rect_dest_clipped.top,
636 rect_dest_clipped.right, rect_dest_clipped.bottom );
639 /* the 2 following lines are to fix a bug when clicking on the desktop */
640 if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
641 (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
643 SetRectEmpty( &rect_src_clipped );
647 /* src image dimensions */
650 rect_src.right = p_vout->render.i_width;
651 rect_src.bottom = p_vout->render.i_height;
653 /* Clip the source image */
654 rect_src_clipped.left = p_vout->fmt_out.i_x_offset +
655 (rect_dest_clipped.left - rect_dest.left) *
656 p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
657 rect_src_clipped.right = p_vout->fmt_out.i_x_offset +
658 p_vout->fmt_out.i_visible_width -
659 (rect_dest.right - rect_dest_clipped.right) *
660 p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
661 rect_src_clipped.top = p_vout->fmt_out.i_y_offset +
662 (rect_dest_clipped.top - rect_dest.top) *
663 p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
664 rect_src_clipped.bottom = p_vout->fmt_out.i_y_offset +
665 p_vout->fmt_out.i_visible_height -
666 (rect_dest.bottom - rect_dest_clipped.bottom) *
667 p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
669 /* Apply overlay hardware constraints */
670 if( p_vout->p_sys->b_using_overlay )
672 if( p_vout->p_sys->i_align_src_boundary )
673 rect_src_clipped.left = ( rect_src_clipped.left +
674 p_vout->p_sys->i_align_src_boundary / 2 ) &
675 ~p_vout->p_sys->i_align_src_boundary;
677 if( p_vout->p_sys->i_align_src_size )
678 rect_src_clipped.right = (( rect_src_clipped.right -
679 rect_src_clipped.left +
680 p_vout->p_sys->i_align_src_size / 2 ) &
681 ~p_vout->p_sys->i_align_src_size) + rect_src_clipped.left;
685 msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
686 " coords: %i,%i,%i,%i",
687 rect_src_clipped.left, rect_src_clipped.top,
688 rect_src_clipped.right, rect_src_clipped.bottom );
691 /* The destination coordinates need to be relative to the current
692 * directdraw primary surface (display) */
693 rect_dest_clipped.left -= p_vout->p_sys->rect_display.left;
694 rect_dest_clipped.right -= p_vout->p_sys->rect_display.left;
695 rect_dest_clipped.top -= p_vout->p_sys->rect_display.top;
696 rect_dest_clipped.bottom -= p_vout->p_sys->rect_display.top;
698 if( p_vout->p_sys->b_using_overlay )
699 E_(DirectXUpdateOverlay)( p_vout );
701 /* Signal the change in size/position */
702 p_vout->p_sys->i_changes |= DX_POSITION_CHANGE;
705 #undef rect_src_clipped
707 #undef rect_dest_clipped
710 /*****************************************************************************
711 * DirectXEventProc: This is the window event processing function.
712 *****************************************************************************
713 * On Windows, when you create a window you have to attach an event processing
714 * function to it. The aim of this function is to manage "Queued Messages" and
715 * "Nonqueued Messages".
716 * Queued Messages are those picked up and retransmitted by vout_Manage
717 * (using the GetMessage and DispatchMessage functions).
718 * Nonqueued Messages are those that Windows will send directly to this
719 * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
720 *****************************************************************************/
721 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
722 WPARAM wParam, LPARAM lParam )
724 vout_thread_t *p_vout;
726 if( message == WM_CREATE )
728 /* Store p_vout for future use */
729 p_vout = (vout_thread_t *)((CREATESTRUCT *)lParam)->lpCreateParams;
730 SetWindowLongPtr( hwnd, GWLP_USERDATA, (LONG_PTR)p_vout );
734 p_vout = (vout_thread_t *)GetWindowLongPtr( hwnd, GWLP_USERDATA );
737 /* Catch the screensaver and the monitor turn-off */
738 if( message == WM_SYSCOMMAND &&
739 ( wParam == SC_SCREENSAVE || wParam == SC_MONITORPOWER ) )
741 //if( p_vout ) msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND screensaver" );
742 return 0; /* this stops them from happening */
747 /* Hmmm mozilla does manage somehow to save the pointer to our
748 * windowproc and still calls it after the vout has been closed. */
749 return DefWindowProc(hwnd, message, wParam, lParam);
752 if( hwnd == p_vout->p_sys->hvideownd )
753 return DefWindowProc(hwnd, message, wParam, lParam);
758 case WM_WINDOWPOSCHANGED:
759 E_(DirectXUpdateRects)( p_vout, VLC_TRUE );
762 /* the user wants to close the window */
765 playlist_t * p_playlist =
766 (playlist_t *)vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
768 if( p_playlist == NULL )
773 playlist_Stop( p_playlist );
774 vlc_object_release( p_playlist );
778 /* the window has been closed so shut down everything now */
780 msg_Dbg( p_vout, "WinProc WM_DESTROY" );
781 /* just destroy the window */
782 PostQuitMessage( 0 );
788 case IDM_TOGGLE_ON_TOP: /* toggle the "on top" status */
791 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND: IDM_TOGGLE_ON_TOP");
793 /* Change the current value */
794 var_Get( p_vout, "video-on-top", &val );
795 val.b_bool = !val.b_bool;
796 var_Set( p_vout, "video-on-top", val );
802 case WM_VLC_CREATE_VIDEO_WIN:
803 /* Create video sub-window */
804 p_vout->p_sys->hvideownd =
805 CreateWindow( _T("VLC DirectX video"), _T(""), /* window class */
806 WS_CHILD | WS_VISIBLE, /* window style */
807 CW_USEDEFAULT, CW_USEDEFAULT, /* default coordinates */
808 CW_USEDEFAULT, CW_USEDEFAULT,
809 hwnd, /* parent window */
810 NULL, GetModuleHandle(NULL),
811 (LPVOID)p_vout ); /* send p_vout to WM_CREATE */
813 if( !p_vout->p_sys->hvideownd )
814 msg_Warn( p_vout, "Can't create video sub-window" );
816 msg_Dbg( p_vout, "Created video sub-window" );
822 /* We do not want to relay these messages to the parent window
823 * because we rely on the background color for the overlay. */
824 return DefWindowProc(hwnd, message, wParam, lParam);
828 //msg_Dbg( p_vout, "WinProc WM Default %i", message );
832 /* Let windows handle the message */
833 return DefWindowProc(hwnd, message, wParam, lParam);
841 } dxkeys_to_vlckeys[] =
843 { VK_F1, KEY_F1 }, { VK_F2, KEY_F2 }, { VK_F3, KEY_F3 }, { VK_F4, KEY_F4 },
844 { VK_F5, KEY_F5 }, { VK_F6, KEY_F6 }, { VK_F7, KEY_F7 }, { VK_F8, KEY_F8 },
845 { VK_F9, KEY_F9 }, { VK_F10, KEY_F10 }, { VK_F11, KEY_F11 },
848 { VK_RETURN, KEY_ENTER },
849 { VK_SPACE, KEY_SPACE },
850 { VK_ESCAPE, KEY_ESC },
852 { VK_LEFT, KEY_LEFT },
853 { VK_RIGHT, KEY_RIGHT },
855 { VK_DOWN, KEY_DOWN },
857 { VK_HOME, KEY_HOME },
859 { VK_PRIOR, KEY_PAGEUP },
860 { VK_NEXT, KEY_PAGEDOWN },
862 { VK_INSERT, KEY_INSERT },
863 { VK_DELETE, KEY_DELETE },
872 static int DirectXConvertKey( int i_key )
876 for( i = 0; dxkeys_to_vlckeys[i].i_dxkey != 0; i++ )
878 if( dxkeys_to_vlckeys[i].i_dxkey == i_key )
880 return dxkeys_to_vlckeys[i].i_vlckey;
887 /*****************************************************************************
888 * Control: control facility for the vout
889 *****************************************************************************/
890 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
899 if( p_vout->p_sys->hparent )
900 return vout_ControlWindow( p_vout,
901 (void *)p_vout->p_sys->hparent, i_query, args );
903 f_arg = va_arg( args, double );
905 /* Update dimensions */
906 rect_window.top = rect_window.left = 0;
907 rect_window.right = p_vout->i_window_width * f_arg;
908 rect_window.bottom = p_vout->i_window_height * f_arg;
909 AdjustWindowRect( &rect_window, p_vout->p_sys->i_window_style, 0 );
911 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
912 rect_window.right - rect_window.left,
913 rect_window.bottom - rect_window.top, SWP_NOMOVE );
918 ShowWindow( p_vout->p_sys->hwnd, SW_HIDE );
920 /* Change window style, borders and title bar */
921 vlc_mutex_lock( &p_vout->p_sys->lock );
922 p_vout->p_sys->hparent = 0;
923 vlc_mutex_unlock( &p_vout->p_sys->lock );
925 /* Retrieve the window position */
926 point.x = point.y = 0;
927 ClientToScreen( p_vout->p_sys->hwnd, &point );
929 SetParent( p_vout->p_sys->hwnd, 0 );
930 p_vout->p_sys->i_window_style =
931 WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW | WS_SIZEBOX;
932 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE,
933 p_vout->p_sys->i_window_style |
934 (i_query == VOUT_CLOSE ? 0 : WS_VISIBLE) );
935 SetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW );
936 SetWindowPos( p_vout->p_sys->hwnd, 0, point.x, point.y, 0, 0,
937 SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED );
939 return vout_vaControlDefault( p_vout, i_query, args );
941 case VOUT_SET_STAY_ON_TOP:
942 if( p_vout->p_sys->hparent )
943 return vout_ControlWindow( p_vout,
944 (void *)p_vout->p_sys->hparent, i_query, args );
946 p_vout->p_sys->b_on_top_change = VLC_TRUE;
950 return vout_vaControlDefault( p_vout, i_query, args );