1 /*****************************************************************************
2 * events.c: Windows DirectX video output events handler
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: events.c,v 1.25 2003/10/28 17:02:14 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 if( val.i_int == config_GetInt( p_event, "fullscreen-key" ) )
225 p_event->p_vout->p_sys->i_changes |=
226 VOUT_FULLSCREEN_CHANGE;
230 var_Set( p_event->p_vlc, "key-pressed", val );
236 /* Messages we don't handle directly are dispatched to the
237 * window procedure */
238 TranslateMessage(&msg);
239 DispatchMessage(&msg);
244 } /* End Main loop */
246 if( msg.message == WM_QUIT )
248 msg_Warn( p_event, "WM_QUIT... should not happen!!" );
249 p_event->p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
252 msg_Dbg( p_event, "DirectXEventThread terminating" );
254 /* clear the changes formerly signaled */
255 p_event->p_vout->p_sys->i_changes = 0;
257 DirectXCloseWindow( p_event->p_vout );
261 /* following functions are local */
263 /*****************************************************************************
264 * DirectXCreateWindow: create a window for the video.
265 *****************************************************************************
266 * Before creating a direct draw surface, we need to create a window in which
267 * the video will be displayed. This window will also allow us to capture the
269 *****************************************************************************/
270 static int DirectXCreateWindow( vout_thread_t *p_vout )
280 msg_Dbg( p_vout, "DirectXCreateWindow" );
282 /* Get this module's instance */
283 hInstance = GetModuleHandle(NULL);
285 /* Create a BRUSH that will be used by Windows to paint the window
287 * This window background is important for us as it will be used by the
288 * graphics card to display the overlay.
289 * This is why we carefully choose the color for this background, the goal
290 * being to choose a color which isn't complete black but nearly. We
291 * obviously don't want to use black as a colorkey for the overlay because
292 * black is one of the most used color and thus would give us undesirable
294 /* the first step is to find the colorkey we want to use. The difficulty
295 * comes from the potential dithering (depends on the display depth)
296 * because we need to know the real RGB value of the chosen colorkey */
298 for( colorkey = 0x0a; colorkey < 0xff /* all shades of red */; colorkey++ )
300 if( colorkey == GetNearestColor( hdc, colorkey ) )
305 msg_Dbg( p_vout, "background color: %i", colorkey );
307 /* Create the actual brush */
308 p_vout->p_sys->hbrush = CreateSolidBrush(colorkey);
309 p_vout->p_sys->i_rgb_colorkey = (int)colorkey;
311 /* Get the current size of the display and its colour depth */
312 p_vout->p_sys->rect_display.right = GetDeviceCaps( hdc, HORZRES );
313 p_vout->p_sys->rect_display.bottom = GetDeviceCaps( hdc, VERTRES );
314 p_vout->p_sys->i_display_depth = GetDeviceCaps( hdc, BITSPIXEL );
315 msg_Dbg( p_vout, "screen dimensions %ix%i colour depth %i",
316 p_vout->p_sys->rect_display.right,
317 p_vout->p_sys->rect_display.bottom,
318 p_vout->p_sys->i_display_depth );
320 ReleaseDC( NULL, hdc );
322 /* If an external window was specified, we'll draw in it. */
323 var_Get( p_vout->p_vlc, "drawable", &val );
324 p_vout->p_sys->hparent = p_vout->p_sys->hwnd =
325 val.i_int ? (void*)(ptrdiff_t) val.i_int : NULL;
327 if( p_vout->p_sys->hparent )
329 msg_Dbg( p_vout, "using external window %p\n", p_vout->p_sys->hwnd );
331 /* Set stuff in the window that we can not put directly in
332 * a class (see below). */
333 SetClassLong( p_vout->p_sys->hwnd,
334 GCL_STYLE, CS_DBLCLKS );
335 SetClassLong( p_vout->p_sys->hwnd,
336 GCL_HBRBACKGROUND, (LONG)p_vout->p_sys->hbrush );
337 SetClassLong( p_vout->p_sys->hwnd,
338 GCL_HCURSOR, (LONG)LoadCursor(NULL, IDC_ARROW) );
339 /* Store a p_vout pointer into the window local storage (for later
340 * use in DirectXEventProc). */
341 SetWindowLong( p_vout->p_sys->hwnd, GWL_USERDATA, (LONG)p_vout );
343 p_vout->p_sys->pf_wndproc =
344 (WNDPROC)SetWindowLong( p_vout->p_sys->hwnd,
345 GWL_WNDPROC, (LONG)DirectXEventProc );
347 /* Blam! Erase everything that might have been there. */
348 RedrawWindow( p_vout->p_sys->hwnd, NULL, NULL,
349 RDW_INVALIDATE | RDW_ERASE );
353 WNDCLASSEX wc; /* window class components */
354 HICON vlc_icon = NULL;
355 char vlc_path[MAX_PATH+1];
357 /* Get the Icon from the main app */
359 if( GetModuleFileName( NULL, vlc_path, MAX_PATH ) )
361 vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
364 /* Fill in the window class structure */
365 wc.cbSize = sizeof(WNDCLASSEX);
366 wc.style = CS_DBLCLKS; /* style: dbl click */
367 wc.lpfnWndProc = (WNDPROC)DirectXEventProc; /* event handler */
368 wc.cbClsExtra = 0; /* no extra class data */
369 wc.cbWndExtra = 0; /* no extra window data */
370 wc.hInstance = hInstance; /* instance */
371 wc.hIcon = vlc_icon; /* load the vlc big icon */
372 wc.hCursor = LoadCursor(NULL, IDC_ARROW); /* default cursor */
373 wc.hbrBackground = p_vout->p_sys->hbrush; /* background color */
374 wc.lpszMenuName = NULL; /* no menu */
375 wc.lpszClassName = "VLC DirectX"; /* use a special class */
376 wc.hIconSm = vlc_icon; /* load the vlc small icon */
378 /* Register the window class */
379 if( !RegisterClassEx(&wc) )
383 /* Free window background brush */
384 if( p_vout->p_sys->hbrush )
386 DeleteObject( p_vout->p_sys->hbrush );
387 p_vout->p_sys->hbrush = NULL;
392 DestroyIcon( vlc_icon );
395 /* Check why it failed. If it's because one already exists
396 * then fine, otherwise return with an error. */
397 if( !GetClassInfo( hInstance, "VLC DirectX", &wndclass ) )
399 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED" );
404 /* When you create a window you give the dimensions you wish it to
405 * have. Unfortunatly these dimensions will include the borders and
406 * titlebar. We use the following function to find out the size of
407 * the window corresponding to the useable surface we want */
408 rect_window.top = 10;
409 rect_window.left = 10;
410 rect_window.right = rect_window.left + p_vout->p_sys->i_window_width;
411 rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
412 AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
414 /* Create the window */
415 p_vout->p_sys->hwnd =
416 CreateWindow( "VLC DirectX", /* name of window class */
417 VOUT_TITLE " (DirectX Output)", /* window title bar text */
418 WS_OVERLAPPEDWINDOW | WS_SIZEBOX, /* window style */
419 CW_USEDEFAULT, /* default X coordinate */
420 0, /* default Y coordinate */
421 rect_window.right - rect_window.left, /* window width */
422 rect_window.bottom - rect_window.top, /* window height */
423 NULL, /* no parent window */
424 NULL, /* no menu in this window */
425 hInstance, /* handle of this program instance */
426 (LPVOID)p_vout ); /* send p_vout to WM_CREATE */
428 if( !p_vout->p_sys->hwnd )
430 msg_Warn( p_vout, "DirectXCreateWindow create window FAILED" );
435 /* Append a "Always On Top" entry in the system menu */
436 hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
437 AppendMenu( hMenu, MF_SEPARATOR, 0, "" );
438 AppendMenu( hMenu, MF_STRING | MF_UNCHECKED,
439 IDM_TOGGLE_ON_TOP, "Always on &Top" );
441 /* Now display the window */
442 ShowWindow( p_vout->p_sys->hwnd, SW_SHOW );
447 /*****************************************************************************
448 * DirectXCloseWindow: close the window created by DirectXCreateWindow
449 *****************************************************************************
450 * This function returns all resources allocated by DirectXCreateWindow.
451 *****************************************************************************/
452 static void DirectXCloseWindow( vout_thread_t *p_vout )
454 msg_Dbg( p_vout, "DirectXCloseWindow" );
456 if( p_vout->p_sys->hwnd && !p_vout->p_sys->hparent )
458 DestroyWindow( p_vout->p_sys->hwnd );
460 else if( p_vout->p_sys->hparent )
462 /* We don't want our windowproc to be called anymore */
463 SetWindowLong( p_vout->p_sys->hwnd,
464 GWL_WNDPROC, (LONG)p_vout->p_sys->pf_wndproc );
466 /* Blam! Erase everything that might have been there. */
467 InvalidateRect( p_vout->p_sys->hwnd, NULL, TRUE );
470 p_vout->p_sys->hwnd = NULL;
472 /* We don't unregister the Window Class because it could lead to race
473 * conditions and it will be done anyway by the system when the app will
477 /*****************************************************************************
478 * DirectXUpdateRects: update clipping rectangles
479 *****************************************************************************
480 * This function is called when the window position or size are changed, and
481 * its job is to update the source and destination RECTs used to display the
483 *****************************************************************************/
484 void DirectXUpdateRects( vout_thread_t *p_vout, vlc_bool_t b_force )
486 #define rect_src p_vout->p_sys->rect_src
487 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
488 #define rect_dest p_vout->p_sys->rect_dest
489 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
491 int i_width, i_height, i_x, i_y;
496 /* Retrieve the window size */
497 GetClientRect( p_vout->p_sys->hwnd, &rect );
499 /* Retrieve the window position */
500 point.x = point.y = 0;
501 ClientToScreen( p_vout->p_sys->hwnd, &point );
503 /* If nothing changed, we can return */
505 && p_vout->p_sys->i_window_width == rect.right
506 && p_vout->p_sys->i_window_height == rect.bottom
507 && p_vout->p_sys->i_window_x == point.x
508 && p_vout->p_sys->i_window_y == point.y )
513 /* Update the window position and size */
514 p_vout->p_sys->i_window_x = point.x;
515 p_vout->p_sys->i_window_y = point.y;
516 p_vout->p_sys->i_window_width = rect.right;
517 p_vout->p_sys->i_window_height = rect.bottom;
519 vout_PlacePicture( p_vout, rect.right, rect.bottom,
520 &i_x, &i_y, &i_width, &i_height );
522 /* Destination image position and dimensions */
523 rect_dest.left = point.x + i_x;
524 rect_dest.right = rect_dest.left + i_width;
525 rect_dest.top = point.y + i_y;
526 rect_dest.bottom = rect_dest.top + i_height;
529 /* UpdateOverlay directdraw function doesn't automatically clip to the
530 * display size so we need to do it otherwise it will fail */
532 /* Clip the destination window */
533 IntersectRect( &rect_dest_clipped,
535 &p_vout->p_sys->rect_display );
538 msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
540 rect_dest_clipped.left, rect_dest_clipped.top,
541 rect_dest_clipped.right, rect_dest_clipped.bottom );
544 /* the 2 following lines are to fix a bug when clicking on the desktop */
545 if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
546 (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
548 SetRectEmpty( &rect_src_clipped );
552 /* src image dimensions */
555 rect_src.right = p_vout->render.i_width;
556 rect_src.bottom = p_vout->render.i_height;
558 /* Clip the source image */
559 rect_src_clipped.left = (rect_dest_clipped.left - rect_dest.left) *
560 p_vout->render.i_width / (rect_dest.right - rect_dest.left);
561 rect_src_clipped.right = p_vout->render.i_width -
562 (rect_dest.right - rect_dest_clipped.right) * p_vout->render.i_width /
563 (rect_dest.right - rect_dest.left);
564 rect_src_clipped.top = (rect_dest_clipped.top - rect_dest.top) *
565 p_vout->render.i_height / (rect_dest.bottom - rect_dest.top);
566 rect_src_clipped.bottom = p_vout->render.i_height -
567 (rect_dest.bottom - rect_dest_clipped.bottom) * p_vout->render.i_height /
568 (rect_dest.bottom - rect_dest.top);
571 msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
572 " coords: %i,%i,%i,%i",
573 rect_src_clipped.left, rect_src_clipped.top,
574 rect_src_clipped.right, rect_src_clipped.bottom );
577 /* Signal the size change */
578 if( !p_vout->p_sys->p_event->b_die )
580 if( p_vout->p_sys->b_using_overlay )
582 DirectXUpdateOverlay( p_vout );
586 p_vout->p_sys->i_changes |= VOUT_SIZE_CHANGE;
591 #undef rect_src_clipped
593 #undef rect_dest_clipped
596 /*****************************************************************************
597 * DirectXEventProc: This is the window event processing function.
598 *****************************************************************************
599 * On Windows, when you create a window you have to attach an event processing
600 * function to it. The aim of this function is to manage "Queued Messages" and
601 * "Nonqueued Messages".
602 * Queued Messages are those picked up and retransmitted by vout_Manage
603 * (using the GetMessage and DispatchMessage functions).
604 * Nonqueued Messages are those that Windows will send directly to this
605 * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
606 *****************************************************************************/
607 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
608 WPARAM wParam, LPARAM lParam )
610 vout_thread_t *p_vout;
612 if( message == WM_CREATE )
614 /* Store p_vout for future use */
615 p_vout = (vout_thread_t *)((CREATESTRUCT *)lParam)->lpCreateParams;
616 SetWindowLong( hwnd, GWL_USERDATA, (LONG)p_vout );
620 p_vout = (vout_thread_t *)GetWindowLong( hwnd, GWL_USERDATA );
626 case WM_WINDOWPOSCHANGED:
627 DirectXUpdateRects( p_vout, VLC_TRUE );
630 /* the user wants to close the window */
633 playlist_t * p_playlist =
634 (playlist_t *)vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
636 if( p_playlist == NULL )
641 playlist_Stop( p_playlist );
642 vlc_object_release( p_playlist );
646 /* the window has been closed so shut down everything now */
648 msg_Dbg( p_vout, "WinProc WM_DESTROY" );
649 /* just destroy the window */
650 PostQuitMessage( 0 );
656 case SC_SCREENSAVE: /* catch the screensaver */
657 case SC_MONITORPOWER: /* catch the monitor turn-off */
658 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND" );
659 return 0; /* this stops them from happening */
660 case IDM_TOGGLE_ON_TOP: /* toggle the "on top" status */
663 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND: IDM_TOGGLE_ON_TOP");
665 /* Get the current value... */
666 if( var_Get( p_vout, "directx-on-top", &val ) < 0 )
668 /* ...and change it */
669 val.b_bool = !val.b_bool;
670 var_Set( p_vout, "directx-on-top", val );
678 if( !p_vout->p_sys->b_using_overlay )
680 /* We want to eliminate unnecessary background redraws which create
681 * an annoying flickering */
682 int i_width, i_height, i_x, i_y;
684 GetClipBox( (HDC)wParam, &rect_temp );
686 msg_Dbg( p_vout, "WinProc WM_ERASEBKGND %i,%i,%i,%i",
687 rect_temp.left, rect_temp.top,
688 rect_temp.right, rect_temp.bottom );
690 vout_PlacePicture( p_vout, p_vout->p_sys->i_window_width,
691 p_vout->p_sys->i_window_height,
692 &i_x, &i_y, &i_width, &i_height );
693 ExcludeClipRect( (HDC)wParam, i_x, i_y,
694 i_x + i_width, i_y + i_height );
699 //msg_Dbg( p_vout, "WinProc WM Default %i", message );
703 return DefWindowProc(hwnd, message, wParam, lParam);
711 } dxkeys_to_vlckeys[] =
713 { VK_F1, KEY_F1 }, { VK_F2, KEY_F2 }, { VK_F3, KEY_F3 }, { VK_F4, KEY_F4 },
714 { VK_F5, KEY_F5 }, { VK_F6, KEY_F6 }, { VK_F7, KEY_F7 }, { VK_F8, KEY_F8 },
715 { VK_F9, KEY_F9 }, { VK_F10, KEY_F10 }, { VK_F11, KEY_F11 },
718 { VK_RETURN, KEY_ENTER },
719 { VK_SPACE, KEY_SPACE },
720 { VK_ESCAPE, KEY_ESC },
722 { VK_MENU, KEY_MENU },
723 { VK_LEFT, KEY_LEFT },
724 { VK_RIGHT, KEY_RIGHT },
726 { VK_DOWN, KEY_DOWN },
728 { VK_HOME, KEY_HOME },
730 { VK_PRIOR, KEY_PAGEUP },
731 { VK_NEXT, KEY_PAGEDOWN },
735 static int DirectXConvertKey( int i_key )
739 for( i = 0; dxkeys_to_vlckeys[i].i_dxkey != 0; i++ )
741 if( dxkeys_to_vlckeys[i].i_dxkey == i_key )
743 return dxkeys_to_vlckeys[i].i_vlckey;