1 /*****************************************************************************
2 * events.c: Windows DirectX video output events handler
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: events.c,v 1.26 2003/10/29 01:33:27 gbazin Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
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 <string.h> /* strerror() */
35 #include <vlc/input.h>
47 /*****************************************************************************
49 *****************************************************************************/
50 static int DirectXCreateWindow( vout_thread_t *p_vout );
51 static void DirectXCloseWindow ( vout_thread_t *p_vout );
52 static long FAR PASCAL DirectXEventProc ( HWND hwnd, UINT message,
53 WPARAM wParam, LPARAM lParam );
55 static void DirectXPopupMenu( event_thread_t *p_event, vlc_bool_t b_open )
57 playlist_t *p_playlist =
58 vlc_object_find( p_event, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
59 if( p_playlist != NULL )
63 var_Set( p_playlist, "intf-popupmenu", val );
64 vlc_object_release( p_playlist );
68 static int DirectXConvertKey( int i_key );
70 /*****************************************************************************
71 * DirectXEventThread: Create video window & handle its messages
72 *****************************************************************************
73 * This function creates a video window and then enters an infinite loop
74 * that handles the messages sent to that window.
75 * The main goal of this thread is to isolate the Win32 PeekMessage function
76 * because this one can block for a long time.
77 *****************************************************************************/
78 void DirectXEventThread( event_thread_t *p_event )
81 POINT old_mouse_pos = {0,0};
83 int i_width, i_height, i_x, i_y;
87 /* Create a window for the video */
88 /* Creating a window under Windows also initializes the thread's event
90 if( DirectXCreateWindow( p_event->p_vout ) )
92 msg_Err( p_event, "out of memory" );
93 p_event->b_dead = VLC_TRUE;
96 /* signal the creation of the window */
97 vlc_thread_ready( p_event );
100 /* GetMessage will sleep if there's no message in the queue */
101 while( !p_event->b_die && ( p_event->p_vout->p_sys->hparent ||
102 GetMessage( &msg, p_event->p_vout->p_sys->hwnd, 0, 0 ) ) )
104 /* Check if we are asked to exit */
108 if( p_event->p_vout->p_sys->hparent ) msleep( INTF_IDLE_SLEEP );
110 switch( msg.message )
115 vout_PlacePicture( p_event->p_vout,
116 p_event->p_vout->p_sys->i_window_width,
117 p_event->p_vout->p_sys->i_window_height,
118 &i_x, &i_y, &i_width, &i_height );
120 val.i_int = ( GET_X_LPARAM(msg.lParam) - i_x )
121 * p_event->p_vout->render.i_width / i_width;
122 var_Set( p_event->p_vout, "mouse-x", val );
123 val.i_int = ( GET_Y_LPARAM(msg.lParam) - i_y )
124 * p_event->p_vout->render.i_height / i_height;
125 var_Set( p_event->p_vout, "mouse-y", val );
127 val.b_bool = VLC_TRUE;
128 var_Set( p_event->p_vout, "mouse-moved", val );
130 if( (abs(GET_X_LPARAM(msg.lParam) - old_mouse_pos.x) > 2 ||
131 (abs(GET_Y_LPARAM(msg.lParam) - old_mouse_pos.y)) > 2 ) )
133 GetCursorPos( &old_mouse_pos );
134 p_event->p_vout->p_sys->i_lastmoved = mdate();
136 if( p_event->p_vout->p_sys->b_cursor_hidden )
138 p_event->p_vout->p_sys->b_cursor_hidden = 0;
144 case WM_VLC_HIDE_MOUSE:
145 GetCursorPos( &old_mouse_pos );
150 var_Get( p_event->p_vout, "mouse-button-down", &val );
152 var_Set( p_event->p_vout, "mouse-button-down", val );
153 DirectXPopupMenu( p_event, VLC_FALSE );
157 var_Get( p_event->p_vout, "mouse-button-down", &val );
159 var_Set( p_event->p_vout, "mouse-button-down", val );
161 val.b_bool = VLC_TRUE;
162 var_Set( p_event->p_vout, "mouse-clicked", val );
165 case WM_LBUTTONDBLCLK:
166 p_event->p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
170 var_Get( p_event->p_vout, "mouse-button-down", &val );
172 var_Set( p_event->p_vout, "mouse-button-down", val );
173 DirectXPopupMenu( p_event, VLC_FALSE );
177 var_Get( p_event->p_vout, "mouse-button-down", &val );
179 var_Set( p_event->p_vout, "mouse-button-down", val );
183 var_Get( p_event->p_vout, "mouse-button-down", &val );
185 var_Set( p_event->p_vout, "mouse-button-down", val );
186 DirectXPopupMenu( p_event, VLC_FALSE );
190 var_Get( p_event->p_vout, "mouse-button-down", &val );
192 var_Set( p_event->p_vout, "mouse-button-down", val );
193 DirectXPopupMenu( p_event, VLC_TRUE );
197 /* the key events are first processed here. The next
198 * message processed by this main message loop will be the
199 * char translation of the key event */
201 if( msg.message == WM_KEYDOWN )
203 val.i_int = DirectXConvertKey( msg.wParam );
204 TranslateMessage(&msg);
206 else val.i_int = msg.wParam;
210 if( GetKeyState(VK_CONTROL) & 0x8000 )
212 val.i_int |= KEY_MODIFIER_CTRL;
214 else if( GetKeyState(VK_SHIFT) & 0x8000 )
216 val.i_int |= KEY_MODIFIER_SHIFT;
218 else if( GetKeyState(VK_MENU) & 0x8000 )
220 val.i_int |= KEY_MODIFIER_ALT;
223 var_Set( p_event->p_vlc, "key-pressed", val );
228 /* Messages we don't handle directly are dispatched to the
229 * window procedure */
230 TranslateMessage(&msg);
231 DispatchMessage(&msg);
236 } /* End Main loop */
238 if( msg.message == WM_QUIT )
240 msg_Warn( p_event, "WM_QUIT... should not happen!!" );
241 p_event->p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
244 msg_Dbg( p_event, "DirectXEventThread terminating" );
246 /* clear the changes formerly signaled */
247 p_event->p_vout->p_sys->i_changes = 0;
249 DirectXCloseWindow( p_event->p_vout );
253 /* following functions are local */
255 /*****************************************************************************
256 * DirectXCreateWindow: create a window for the video.
257 *****************************************************************************
258 * Before creating a direct draw surface, we need to create a window in which
259 * the video will be displayed. This window will also allow us to capture the
261 *****************************************************************************/
262 static int DirectXCreateWindow( vout_thread_t *p_vout )
272 msg_Dbg( p_vout, "DirectXCreateWindow" );
274 /* Get this module's instance */
275 hInstance = GetModuleHandle(NULL);
277 /* Create a BRUSH that will be used by Windows to paint the window
279 * This window background is important for us as it will be used by the
280 * graphics card to display the overlay.
281 * This is why we carefully choose the color for this background, the goal
282 * being to choose a color which isn't complete black but nearly. We
283 * obviously don't want to use black as a colorkey for the overlay because
284 * black is one of the most used color and thus would give us undesirable
286 /* the first step is to find the colorkey we want to use. The difficulty
287 * comes from the potential dithering (depends on the display depth)
288 * because we need to know the real RGB value of the chosen colorkey */
290 for( colorkey = 0x0a; colorkey < 0xff /* all shades of red */; colorkey++ )
292 if( colorkey == GetNearestColor( hdc, colorkey ) )
297 msg_Dbg( p_vout, "background color: %i", colorkey );
299 /* Create the actual brush */
300 p_vout->p_sys->hbrush = CreateSolidBrush(colorkey);
301 p_vout->p_sys->i_rgb_colorkey = (int)colorkey;
303 /* Get the current size of the display and its colour depth */
304 p_vout->p_sys->rect_display.right = GetDeviceCaps( hdc, HORZRES );
305 p_vout->p_sys->rect_display.bottom = GetDeviceCaps( hdc, VERTRES );
306 p_vout->p_sys->i_display_depth = GetDeviceCaps( hdc, BITSPIXEL );
307 msg_Dbg( p_vout, "screen dimensions %ix%i colour depth %i",
308 p_vout->p_sys->rect_display.right,
309 p_vout->p_sys->rect_display.bottom,
310 p_vout->p_sys->i_display_depth );
312 ReleaseDC( NULL, hdc );
314 /* If an external window was specified, we'll draw in it. */
315 var_Get( p_vout->p_vlc, "drawable", &val );
316 p_vout->p_sys->hparent = p_vout->p_sys->hwnd =
317 val.i_int ? (void*)(ptrdiff_t) val.i_int : NULL;
319 if( p_vout->p_sys->hparent )
321 msg_Dbg( p_vout, "using external window %p\n", p_vout->p_sys->hwnd );
323 /* Set stuff in the window that we can not put directly in
324 * a class (see below). */
325 SetClassLong( p_vout->p_sys->hwnd,
326 GCL_STYLE, CS_DBLCLKS );
327 SetClassLong( p_vout->p_sys->hwnd,
328 GCL_HBRBACKGROUND, (LONG)p_vout->p_sys->hbrush );
329 SetClassLong( p_vout->p_sys->hwnd,
330 GCL_HCURSOR, (LONG)LoadCursor(NULL, IDC_ARROW) );
331 /* Store a p_vout pointer into the window local storage (for later
332 * use in DirectXEventProc). */
333 SetWindowLong( p_vout->p_sys->hwnd, GWL_USERDATA, (LONG)p_vout );
335 p_vout->p_sys->pf_wndproc =
336 (WNDPROC)SetWindowLong( p_vout->p_sys->hwnd,
337 GWL_WNDPROC, (LONG)DirectXEventProc );
339 /* Blam! Erase everything that might have been there. */
340 RedrawWindow( p_vout->p_sys->hwnd, NULL, NULL,
341 RDW_INVALIDATE | RDW_ERASE );
345 WNDCLASSEX wc; /* window class components */
346 HICON vlc_icon = NULL;
347 char vlc_path[MAX_PATH+1];
349 /* Get the Icon from the main app */
351 if( GetModuleFileName( NULL, vlc_path, MAX_PATH ) )
353 vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
356 /* Fill in the window class structure */
357 wc.cbSize = sizeof(WNDCLASSEX);
358 wc.style = CS_DBLCLKS; /* style: dbl click */
359 wc.lpfnWndProc = (WNDPROC)DirectXEventProc; /* event handler */
360 wc.cbClsExtra = 0; /* no extra class data */
361 wc.cbWndExtra = 0; /* no extra window data */
362 wc.hInstance = hInstance; /* instance */
363 wc.hIcon = vlc_icon; /* load the vlc big icon */
364 wc.hCursor = LoadCursor(NULL, IDC_ARROW); /* default cursor */
365 wc.hbrBackground = p_vout->p_sys->hbrush; /* background color */
366 wc.lpszMenuName = NULL; /* no menu */
367 wc.lpszClassName = "VLC DirectX"; /* use a special class */
368 wc.hIconSm = vlc_icon; /* load the vlc small icon */
370 /* Register the window class */
371 if( !RegisterClassEx(&wc) )
375 /* Free window background brush */
376 if( p_vout->p_sys->hbrush )
378 DeleteObject( p_vout->p_sys->hbrush );
379 p_vout->p_sys->hbrush = NULL;
384 DestroyIcon( vlc_icon );
387 /* Check why it failed. If it's because one already exists
388 * then fine, otherwise return with an error. */
389 if( !GetClassInfo( hInstance, "VLC DirectX", &wndclass ) )
391 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED" );
396 /* When you create a window you give the dimensions you wish it to
397 * have. Unfortunatly these dimensions will include the borders and
398 * titlebar. We use the following function to find out the size of
399 * the window corresponding to the useable surface we want */
400 rect_window.top = 10;
401 rect_window.left = 10;
402 rect_window.right = rect_window.left + p_vout->p_sys->i_window_width;
403 rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
404 AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
406 /* Create the window */
407 p_vout->p_sys->hwnd =
408 CreateWindow( "VLC DirectX", /* name of window class */
409 VOUT_TITLE " (DirectX Output)", /* window title bar text */
410 WS_OVERLAPPEDWINDOW | WS_SIZEBOX, /* window style */
411 CW_USEDEFAULT, /* default X coordinate */
412 0, /* default Y coordinate */
413 rect_window.right - rect_window.left, /* window width */
414 rect_window.bottom - rect_window.top, /* window height */
415 NULL, /* no parent window */
416 NULL, /* no menu in this window */
417 hInstance, /* handle of this program instance */
418 (LPVOID)p_vout ); /* send p_vout to WM_CREATE */
420 if( !p_vout->p_sys->hwnd )
422 msg_Warn( p_vout, "DirectXCreateWindow create window FAILED" );
427 /* Append a "Always On Top" entry in the system menu */
428 hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
429 AppendMenu( hMenu, MF_SEPARATOR, 0, "" );
430 AppendMenu( hMenu, MF_STRING | MF_UNCHECKED,
431 IDM_TOGGLE_ON_TOP, "Always on &Top" );
433 /* Now display the window */
434 ShowWindow( p_vout->p_sys->hwnd, SW_SHOW );
439 /*****************************************************************************
440 * DirectXCloseWindow: close the window created by DirectXCreateWindow
441 *****************************************************************************
442 * This function returns all resources allocated by DirectXCreateWindow.
443 *****************************************************************************/
444 static void DirectXCloseWindow( vout_thread_t *p_vout )
446 msg_Dbg( p_vout, "DirectXCloseWindow" );
448 if( p_vout->p_sys->hwnd && !p_vout->p_sys->hparent )
450 DestroyWindow( p_vout->p_sys->hwnd );
452 else if( p_vout->p_sys->hparent )
454 /* We don't want our windowproc to be called anymore */
455 SetWindowLong( p_vout->p_sys->hwnd,
456 GWL_WNDPROC, (LONG)p_vout->p_sys->pf_wndproc );
458 /* Blam! Erase everything that might have been there. */
459 InvalidateRect( p_vout->p_sys->hwnd, NULL, TRUE );
462 p_vout->p_sys->hwnd = NULL;
464 /* We don't unregister the Window Class because it could lead to race
465 * conditions and it will be done anyway by the system when the app will
469 /*****************************************************************************
470 * DirectXUpdateRects: update clipping rectangles
471 *****************************************************************************
472 * This function is called when the window position or size are changed, and
473 * its job is to update the source and destination RECTs used to display the
475 *****************************************************************************/
476 void DirectXUpdateRects( vout_thread_t *p_vout, vlc_bool_t b_force )
478 #define rect_src p_vout->p_sys->rect_src
479 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
480 #define rect_dest p_vout->p_sys->rect_dest
481 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
483 int i_width, i_height, i_x, i_y;
488 /* Retrieve the window size */
489 GetClientRect( p_vout->p_sys->hwnd, &rect );
491 /* Retrieve the window position */
492 point.x = point.y = 0;
493 ClientToScreen( p_vout->p_sys->hwnd, &point );
495 /* If nothing changed, we can return */
497 && p_vout->p_sys->i_window_width == rect.right
498 && p_vout->p_sys->i_window_height == rect.bottom
499 && p_vout->p_sys->i_window_x == point.x
500 && p_vout->p_sys->i_window_y == point.y )
505 /* Update the window position and size */
506 p_vout->p_sys->i_window_x = point.x;
507 p_vout->p_sys->i_window_y = point.y;
508 p_vout->p_sys->i_window_width = rect.right;
509 p_vout->p_sys->i_window_height = rect.bottom;
511 vout_PlacePicture( p_vout, rect.right, rect.bottom,
512 &i_x, &i_y, &i_width, &i_height );
514 /* Destination image position and dimensions */
515 rect_dest.left = point.x + i_x;
516 rect_dest.right = rect_dest.left + i_width;
517 rect_dest.top = point.y + i_y;
518 rect_dest.bottom = rect_dest.top + i_height;
521 /* UpdateOverlay directdraw function doesn't automatically clip to the
522 * display size so we need to do it otherwise it will fail */
524 /* Clip the destination window */
525 IntersectRect( &rect_dest_clipped,
527 &p_vout->p_sys->rect_display );
530 msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
532 rect_dest_clipped.left, rect_dest_clipped.top,
533 rect_dest_clipped.right, rect_dest_clipped.bottom );
536 /* the 2 following lines are to fix a bug when clicking on the desktop */
537 if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
538 (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
540 SetRectEmpty( &rect_src_clipped );
544 /* src image dimensions */
547 rect_src.right = p_vout->render.i_width;
548 rect_src.bottom = p_vout->render.i_height;
550 /* Clip the source image */
551 rect_src_clipped.left = (rect_dest_clipped.left - rect_dest.left) *
552 p_vout->render.i_width / (rect_dest.right - rect_dest.left);
553 rect_src_clipped.right = p_vout->render.i_width -
554 (rect_dest.right - rect_dest_clipped.right) * p_vout->render.i_width /
555 (rect_dest.right - rect_dest.left);
556 rect_src_clipped.top = (rect_dest_clipped.top - rect_dest.top) *
557 p_vout->render.i_height / (rect_dest.bottom - rect_dest.top);
558 rect_src_clipped.bottom = p_vout->render.i_height -
559 (rect_dest.bottom - rect_dest_clipped.bottom) * p_vout->render.i_height /
560 (rect_dest.bottom - rect_dest.top);
563 msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
564 " coords: %i,%i,%i,%i",
565 rect_src_clipped.left, rect_src_clipped.top,
566 rect_src_clipped.right, rect_src_clipped.bottom );
569 /* Signal the size change */
570 if( !p_vout->p_sys->p_event->b_die )
572 if( p_vout->p_sys->b_using_overlay )
574 DirectXUpdateOverlay( p_vout );
578 p_vout->p_sys->i_changes |= VOUT_SIZE_CHANGE;
583 #undef rect_src_clipped
585 #undef rect_dest_clipped
588 /*****************************************************************************
589 * DirectXEventProc: This is the window event processing function.
590 *****************************************************************************
591 * On Windows, when you create a window you have to attach an event processing
592 * function to it. The aim of this function is to manage "Queued Messages" and
593 * "Nonqueued Messages".
594 * Queued Messages are those picked up and retransmitted by vout_Manage
595 * (using the GetMessage and DispatchMessage functions).
596 * Nonqueued Messages are those that Windows will send directly to this
597 * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
598 *****************************************************************************/
599 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
600 WPARAM wParam, LPARAM lParam )
602 vout_thread_t *p_vout;
604 if( message == WM_CREATE )
606 /* Store p_vout for future use */
607 p_vout = (vout_thread_t *)((CREATESTRUCT *)lParam)->lpCreateParams;
608 SetWindowLong( hwnd, GWL_USERDATA, (LONG)p_vout );
612 p_vout = (vout_thread_t *)GetWindowLong( hwnd, GWL_USERDATA );
618 case WM_WINDOWPOSCHANGED:
619 DirectXUpdateRects( p_vout, VLC_TRUE );
622 /* the user wants to close the window */
625 playlist_t * p_playlist =
626 (playlist_t *)vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
628 if( p_playlist == NULL )
633 playlist_Stop( p_playlist );
634 vlc_object_release( p_playlist );
638 /* the window has been closed so shut down everything now */
640 msg_Dbg( p_vout, "WinProc WM_DESTROY" );
641 /* just destroy the window */
642 PostQuitMessage( 0 );
648 case SC_SCREENSAVE: /* catch the screensaver */
649 case SC_MONITORPOWER: /* catch the monitor turn-off */
650 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND" );
651 return 0; /* this stops them from happening */
652 case IDM_TOGGLE_ON_TOP: /* toggle the "on top" status */
655 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND: IDM_TOGGLE_ON_TOP");
657 /* Get the current value... */
658 if( var_Get( p_vout, "directx-on-top", &val ) < 0 )
660 /* ...and change it */
661 val.b_bool = !val.b_bool;
662 var_Set( p_vout, "directx-on-top", val );
670 if( !p_vout->p_sys->b_using_overlay )
672 /* We want to eliminate unnecessary background redraws which create
673 * an annoying flickering */
674 int i_width, i_height, i_x, i_y;
676 GetClipBox( (HDC)wParam, &rect_temp );
678 msg_Dbg( p_vout, "WinProc WM_ERASEBKGND %i,%i,%i,%i",
679 rect_temp.left, rect_temp.top,
680 rect_temp.right, rect_temp.bottom );
682 vout_PlacePicture( p_vout, p_vout->p_sys->i_window_width,
683 p_vout->p_sys->i_window_height,
684 &i_x, &i_y, &i_width, &i_height );
685 ExcludeClipRect( (HDC)wParam, i_x, i_y,
686 i_x + i_width, i_y + i_height );
691 //msg_Dbg( p_vout, "WinProc WM Default %i", message );
695 return DefWindowProc(hwnd, message, wParam, lParam);
703 } dxkeys_to_vlckeys[] =
705 { VK_F1, KEY_F1 }, { VK_F2, KEY_F2 }, { VK_F3, KEY_F3 }, { VK_F4, KEY_F4 },
706 { VK_F5, KEY_F5 }, { VK_F6, KEY_F6 }, { VK_F7, KEY_F7 }, { VK_F8, KEY_F8 },
707 { VK_F9, KEY_F9 }, { VK_F10, KEY_F10 }, { VK_F11, KEY_F11 },
710 { VK_RETURN, KEY_ENTER },
711 { VK_SPACE, KEY_SPACE },
712 { VK_ESCAPE, KEY_ESC },
714 { VK_MENU, KEY_MENU },
715 { VK_LEFT, KEY_LEFT },
716 { VK_RIGHT, KEY_RIGHT },
718 { VK_DOWN, KEY_DOWN },
720 { VK_HOME, KEY_HOME },
722 { VK_PRIOR, KEY_PAGEUP },
723 { VK_NEXT, KEY_PAGEDOWN },
727 static int DirectXConvertKey( int i_key )
731 for( i = 0; dxkeys_to_vlckeys[i].i_dxkey != 0; i++ )
733 if( dxkeys_to_vlckeys[i].i_dxkey == i_key )
735 return dxkeys_to_vlckeys[i].i_vlckey;