1 /*****************************************************************************
2 * events.c: Windows DirectX video output events handler
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: events.c,v 1.12 2003/03/04 22:48:55 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>
48 /*****************************************************************************
50 *****************************************************************************/
51 static int DirectXCreateWindow( vout_thread_t *p_vout );
52 static void DirectXCloseWindow ( vout_thread_t *p_vout );
53 static long FAR PASCAL DirectXEventProc ( HWND hwnd, UINT message,
54 WPARAM wParam, LPARAM lParam );
56 /*****************************************************************************
57 * DirectXEventThread: Create video window & handle its messages
58 *****************************************************************************
59 * This function creates a video window and then enters an infinite loop
60 * that handles the messages sent to that window.
61 * The main goal of this thread is to isolate the Win32 PeekMessage function
62 * because this one can block for a long time.
63 *****************************************************************************/
64 void DirectXEventThread( event_thread_t *p_event )
69 int i_width, i_height, i_x, i_y;
73 /* Create a window for the video */
74 /* Creating a window under Windows also initializes the thread's event
76 if( DirectXCreateWindow( p_event->p_vout ) )
78 msg_Err( p_event, "out of memory" );
79 p_event->b_dead = VLC_TRUE;
82 /* signal the creation of the window */
83 vlc_thread_ready( p_event );
86 /* GetMessage will sleep if there's no message in the queue */
87 while( !p_event->b_die
88 && GetMessage( &msg, p_event->p_vout->p_sys->hwnd, 0, 0 ) )
90 /* Check if we are asked to exit */
99 vout_PlacePicture( p_event->p_vout,
100 p_event->p_vout->p_sys->i_window_width,
101 p_event->p_vout->p_sys->i_window_height,
102 &i_x, &i_y, &i_width, &i_height );
104 val.i_int = ( GET_X_LPARAM(msg.lParam) - i_x )
105 * p_event->p_vout->render.i_width / i_width;
106 var_Set( p_event->p_vout, "mouse-x", val );
107 val.i_int = ( GET_Y_LPARAM(msg.lParam) - i_y )
108 * p_event->p_vout->render.i_height / i_height;
109 var_Set( p_event->p_vout, "mouse-y", val );
111 val.b_bool = VLC_TRUE;
112 var_Set( p_event->p_vout, "mouse-moved", val );
114 if( (abs(GET_X_LPARAM(msg.lParam) - old_mouse_pos.x) > 2 ||
115 (abs(GET_Y_LPARAM(msg.lParam) - old_mouse_pos.y)) > 2 ) )
117 GetCursorPos( &old_mouse_pos );
118 p_event->p_vout->p_sys->i_lastmoved = mdate();
120 if( p_event->p_vout->p_sys->b_cursor_hidden )
122 p_event->p_vout->p_sys->b_cursor_hidden = 0;
128 case WM_VLC_HIDE_MOUSE:
129 GetCursorPos( &old_mouse_pos );
134 var_Get( p_event->p_vout, "mouse-button-down", &val );
136 var_Set( p_event->p_vout, "mouse-button-down", val );
140 var_Get( p_event->p_vout, "mouse-button-down", &val );
142 var_Set( p_event->p_vout, "mouse-button-down", val );
144 val.b_bool = VLC_TRUE;
145 var_Set( p_event->p_vout, "mouse-clicked", val );
148 case WM_LBUTTONDBLCLK:
149 p_event->p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
153 var_Get( p_event->p_vout, "mouse-button-down", &val );
155 var_Set( p_event->p_vout, "mouse-button-down", val );
159 var_Get( p_event->p_vout, "mouse-button-down", &val );
161 var_Set( p_event->p_vout, "mouse-button-down", val );
165 var_Get( p_event->p_vout, "mouse-button-down", &val );
167 var_Set( p_event->p_vout, "mouse-button-down", val );
171 var_Get( p_event->p_vout, "mouse-button-down", &val );
173 var_Set( p_event->p_vout, "mouse-button-down", val );
175 intf_thread_t *p_intf;
176 p_intf = vlc_object_find( p_event, VLC_OBJECT_INTF,
180 p_intf->b_menu_change = 1;
181 vlc_object_release( p_intf );
187 /* the key events are first processed here. The next
188 * message processed by this main message loop will be the
189 * char translation of the key event */
190 msg_Dbg( p_event, "WM_KEYDOWN" );
194 /* exit application */
195 p_event->p_vlc->b_die = VLC_TRUE;
200 vlc_object_t *p_input = vlc_object_find( p_event->p_vout,
201 VLC_OBJECT_INPUT, FIND_ANYWHERE );
204 input_SetStatus( (input_thread_t *)p_input,
205 INPUT_STATUS_PAUSE );
206 vlc_object_release( p_input );
211 case VK_F1: network_ChannelJoin( p_event, 1 ); break;
212 case VK_F2: network_ChannelJoin( p_event, 2 ); break;
213 case VK_F3: network_ChannelJoin( p_event, 3 ); break;
214 case VK_F4: network_ChannelJoin( p_event, 4 ); break;
215 case VK_F5: network_ChannelJoin( p_event, 5 ); break;
216 case VK_F6: network_ChannelJoin( p_event, 6 ); break;
217 case VK_F7: network_ChannelJoin( p_event, 7 ); break;
218 case VK_F8: network_ChannelJoin( p_event, 8 ); break;
219 case VK_F9: network_ChannelJoin( p_event, 9 ); break;
220 case VK_F10: network_ChannelJoin( p_event, 10 ); break;
221 case VK_F11: network_ChannelJoin( p_event, 11 ); break;
222 case VK_F12: network_ChannelJoin( p_event, 12 ); break;
224 TranslateMessage(&msg);
232 /* exit application */
233 p_event->p_vlc->b_die = VLC_TRUE;
236 case 'f': /* switch to fullscreen */
238 p_event->p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
241 case 'c': /* toggle grayscale */
243 p_event->p_vout->b_grayscale = ! p_event->p_vout->b_grayscale;
244 p_event->p_vout->p_sys->i_changes |= VOUT_GRAYSCALE_CHANGE;
247 case 'i': /* toggle info */
249 p_event->p_vout->b_info = ! p_event->p_vout->b_info;
250 p_event->p_vout->p_sys->i_changes |= VOUT_INFO_CHANGE;
253 case 's': /* toggle scaling */
255 p_event->p_vout->b_scale = ! p_event->p_vout->b_scale;
256 p_event->p_vout->p_sys->i_changes |= VOUT_SCALE_CHANGE;
259 case ' ': /* toggle interface */
260 p_event->p_vout->b_interface = ! p_event->p_vout->b_interface;
261 p_event->p_vout->p_sys->i_changes |= VOUT_INTF_CHANGE;
269 /* Messages we don't handle directly are dispatched to the
270 * window procedure */
271 TranslateMessage(&msg);
272 DispatchMessage(&msg);
277 } /* End Main loop */
279 if( msg.message == WM_QUIT )
281 msg_Warn( p_event, "WM_QUIT... should not happen!!" );
282 p_event->p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
285 msg_Dbg( p_event, "DirectXEventThread terminating" );
287 /* clear the changes formerly signaled */
288 p_event->p_vout->p_sys->i_changes = 0;
290 DirectXCloseWindow( p_event->p_vout );
294 /* following functions are local */
296 /*****************************************************************************
297 * DirectXCreateWindow: create a window for the video.
298 *****************************************************************************
299 * Before creating a direct draw surface, we need to create a window in which
300 * the video will be displayed. This window will also allow us to capture the
302 *****************************************************************************/
303 static int DirectXCreateWindow( vout_thread_t *p_vout )
313 msg_Dbg( p_vout, "DirectXCreateWindow" );
315 /* Get this module's instance */
316 hInstance = GetModuleHandle(NULL);
318 /* Create a BRUSH that will be used by Windows to paint the window
320 * This window background is important for us as it will be used by the
321 * graphics card to display the overlay.
322 * This is why we carefully choose the color for this background, the goal
323 * being to choose a color which isn't complete black but nearly. We
324 * obviously don't want to use black as a colorkey for the overlay because
325 * black is one of the most used color and thus would give us undesirable
327 /* the first step is to find the colorkey we want to use. The difficulty
328 * comes from the potential dithering (depends on the display depth)
329 * because we need to know the real RGB value of the chosen colorkey */
331 for( colorkey = 0x0a; colorkey < 0xff /* all shades of red */; colorkey++ )
333 if( colorkey == GetNearestColor( hdc, colorkey ) )
338 msg_Dbg( p_vout, "background color: %i", colorkey );
340 /* Create the actual brush */
341 p_vout->p_sys->hbrush = CreateSolidBrush(colorkey);
342 p_vout->p_sys->i_rgb_colorkey = (int)colorkey;
344 /* Get the current size of the display and its colour depth */
345 p_vout->p_sys->rect_display.right = GetDeviceCaps( hdc, HORZRES );
346 p_vout->p_sys->rect_display.bottom = GetDeviceCaps( hdc, VERTRES );
347 p_vout->p_sys->i_display_depth = GetDeviceCaps( hdc, BITSPIXEL );
348 msg_Dbg( p_vout, "screen dimensions %ix%i colour depth %i",
349 p_vout->p_sys->rect_display.right,
350 p_vout->p_sys->rect_display.bottom,
351 p_vout->p_sys->i_display_depth );
353 ReleaseDC( NULL, hdc );
355 /* If an external window was specified, we'll draw in it. */
356 var_Get( p_vout->p_vlc, "drawable", &val );
357 p_vout->p_sys->hparent = p_vout->p_sys->hwnd =
358 val.i_int ? (void*)(ptrdiff_t) val.i_int : NULL;
360 if( p_vout->p_sys->hparent )
362 msg_Dbg( p_vout, "using external window %p\n", p_vout->p_sys->hwnd );
364 /* Set stuff in the window that we can not put directly in
365 * a class (see below). */
366 SetClassLong( p_vout->p_sys->hwnd,
367 GCL_STYLE, CS_DBLCLKS );
368 SetClassLong( p_vout->p_sys->hwnd,
369 GCL_HBRBACKGROUND, (LONG)p_vout->p_sys->hbrush );
370 SetClassLong( p_vout->p_sys->hwnd,
371 GCL_HCURSOR, (LONG)LoadCursor(NULL, IDC_ARROW) );
372 /* Store a p_vout pointer into the window local storage (for later
373 * use in DirectXEventProc). */
374 SetWindowLong( p_vout->p_sys->hwnd, GWL_USERDATA, (LONG)p_vout );
376 p_vout->p_sys->pf_wndproc =
377 (WNDPROC)SetWindowLong( p_vout->p_sys->hwnd,
378 GWL_WNDPROC, (LONG)DirectXEventProc );
380 /* Blam! Erase everything that might have been there. */
381 RedrawWindow( p_vout->p_sys->hwnd, NULL, NULL,
382 RDW_INVALIDATE | RDW_ERASE );
386 WNDCLASSEX wc; /* window class components */
387 HICON vlc_icon = NULL;
388 char vlc_path[MAX_PATH+1];
390 /* Get the Icon from the main app */
392 if( GetModuleFileName( NULL, vlc_path, MAX_PATH ) )
394 vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
397 /* Fill in the window class structure */
398 wc.cbSize = sizeof(WNDCLASSEX);
399 wc.style = CS_DBLCLKS; /* style: dbl click */
400 wc.lpfnWndProc = (WNDPROC)DirectXEventProc; /* event handler */
401 wc.cbClsExtra = 0; /* no extra class data */
402 wc.cbWndExtra = 0; /* no extra window data */
403 wc.hInstance = hInstance; /* instance */
404 wc.hIcon = vlc_icon; /* load the vlc big icon */
405 wc.hCursor = LoadCursor(NULL, IDC_ARROW); /* default cursor */
406 wc.hbrBackground = p_vout->p_sys->hbrush; /* background color */
407 wc.lpszMenuName = NULL; /* no menu */
408 wc.lpszClassName = "VLC DirectX"; /* use a special class */
409 wc.hIconSm = vlc_icon; /* load the vlc small icon */
411 /* Register the window class */
412 if( !RegisterClassEx(&wc) )
416 /* Free window background brush */
417 if( p_vout->p_sys->hbrush )
419 DeleteObject( p_vout->p_sys->hbrush );
420 p_vout->p_sys->hbrush = NULL;
425 DestroyIcon( vlc_icon );
428 /* Check why it failed. If it's because one already exists
429 * then fine, otherwise return with an error. */
430 if( !GetClassInfo( hInstance, "VLC DirectX", &wndclass ) )
432 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED" );
437 /* When you create a window you give the dimensions you wish it to
438 * have. Unfortunatly these dimensions will include the borders and
439 * titlebar. We use the following function to find out the size of
440 * the window corresponding to the useable surface we want */
441 rect_window.top = 10;
442 rect_window.left = 10;
443 rect_window.right = rect_window.left + p_vout->p_sys->i_window_width;
444 rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
445 AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
447 /* Create the window */
448 p_vout->p_sys->hwnd =
449 CreateWindow( "VLC DirectX", /* name of window class */
450 VOUT_TITLE " (DirectX Output)", /* window title bar text */
451 WS_OVERLAPPEDWINDOW | WS_SIZEBOX, /* window style */
452 CW_USEDEFAULT, /* default X coordinate */
453 0, /* default Y coordinate */
454 rect_window.right - rect_window.left, /* window width */
455 rect_window.bottom - rect_window.top, /* window height */
456 NULL, /* no parent window */
457 NULL, /* no menu in this window */
458 hInstance, /* handle of this program instance */
459 (LPVOID)p_vout ); /* send p_vout to WM_CREATE */
461 if( !p_vout->p_sys->hwnd )
463 msg_Warn( p_vout, "DirectXCreateWindow create window FAILED" );
468 /* Append a "Always On Top" entry in the system menu */
469 hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
470 AppendMenu( hMenu, MF_SEPARATOR, 0, "" );
471 AppendMenu( hMenu, MF_STRING | MF_UNCHECKED,
472 IDM_TOGGLE_ON_TOP, "Always on &Top" );
474 /* Now display the window */
475 ShowWindow( p_vout->p_sys->hwnd, SW_SHOW );
480 /*****************************************************************************
481 * DirectXCloseWindow: close the window created by DirectXCreateWindow
482 *****************************************************************************
483 * This function returns all resources allocated by DirectXCreateWindow.
484 *****************************************************************************/
485 static void DirectXCloseWindow( vout_thread_t *p_vout )
487 msg_Dbg( p_vout, "DirectXCloseWindow" );
489 if( p_vout->p_sys->hwnd && !p_vout->p_sys->hparent )
491 DestroyWindow( p_vout->p_sys->hwnd );
494 p_vout->p_sys->hwnd = NULL;
496 /* We don't unregister the Window Class because it could lead to race
497 * conditions and it will be done anyway by the system when the app will
501 /*****************************************************************************
502 * DirectXUpdateRects: update clipping rectangles
503 *****************************************************************************
504 * This function is called when the window position or size are changed, and
505 * its job is to update the source and destination RECTs used to display the
507 *****************************************************************************/
508 void DirectXUpdateRects( vout_thread_t *p_vout, vlc_bool_t b_force )
510 #define rect_src p_vout->p_sys->rect_src
511 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
512 #define rect_dest p_vout->p_sys->rect_dest
513 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
515 int i_width, i_height, i_x, i_y;
520 /* Retrieve the window size */
521 GetClientRect( p_vout->p_sys->hwnd, &rect );
523 /* Retrieve the window position */
524 point.x = point.y = 0;
525 ClientToScreen( p_vout->p_sys->hwnd, &point );
527 /* If nothing changed, we can return */
529 && p_vout->p_sys->i_window_width == rect.right
530 && p_vout->p_sys->i_window_height == rect.bottom
531 && p_vout->p_sys->i_window_x == point.x
532 && p_vout->p_sys->i_window_y == point.y )
537 /* Update the window position and size */
538 p_vout->p_sys->i_window_x = point.x;
539 p_vout->p_sys->i_window_y = point.y;
540 p_vout->p_sys->i_window_width = rect.right;
541 p_vout->p_sys->i_window_height = rect.bottom;
543 vout_PlacePicture( p_vout, rect.right, rect.bottom,
544 &i_x, &i_y, &i_width, &i_height );
546 /* Destination image position and dimensions */
547 rect_dest.left = point.x + i_x;
548 rect_dest.right = rect_dest.left + i_width;
549 rect_dest.top = point.y + i_y;
550 rect_dest.bottom = rect_dest.top + i_height;
553 /* UpdateOverlay directdraw function doesn't automatically clip to the
554 * display size so we need to do it otherwise it will fail */
556 /* Clip the destination window */
557 IntersectRect( &rect_dest_clipped,
559 &p_vout->p_sys->rect_display );
562 msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
564 rect_dest_clipped.left, rect_dest_clipped.top,
565 rect_dest_clipped.right, rect_dest_clipped.bottom );
568 /* the 2 following lines are to fix a bug when clicking on the desktop */
569 if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
570 (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
572 SetRectEmpty( &rect_src_clipped );
576 /* src image dimensions */
579 rect_src.right = p_vout->render.i_width;
580 rect_src.bottom = p_vout->render.i_height;
582 /* Clip the source image */
583 rect_src_clipped.left = (rect_dest_clipped.left - rect_dest.left) *
584 p_vout->render.i_width / (rect_dest.right - rect_dest.left);
585 rect_src_clipped.right = p_vout->render.i_width -
586 (rect_dest.right - rect_dest_clipped.right) * p_vout->render.i_width /
587 (rect_dest.right - rect_dest.left);
588 rect_src_clipped.top = (rect_dest_clipped.top - rect_dest.top) *
589 p_vout->render.i_height / (rect_dest.bottom - rect_dest.top);
590 rect_src_clipped.bottom = p_vout->render.i_height -
591 (rect_dest.bottom - rect_dest_clipped.bottom) * p_vout->render.i_height /
592 (rect_dest.bottom - rect_dest.top);
595 msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
596 " coords: %i,%i,%i,%i",
597 rect_src_clipped.left, rect_src_clipped.top,
598 rect_src_clipped.right, rect_src_clipped.bottom );
601 /* Signal the size change */
602 if( !p_vout->p_sys->p_event->b_die )
604 if( p_vout->p_sys->b_using_overlay )
606 DirectXUpdateOverlay( p_vout );
610 p_vout->p_sys->i_changes |= VOUT_SIZE_CHANGE;
615 #undef rect_src_clipped
617 #undef rect_dest_clipped
620 /*****************************************************************************
621 * DirectXEventProc: This is the window event processing function.
622 *****************************************************************************
623 * On Windows, when you create a window you have to attach an event processing
624 * function to it. The aim of this function is to manage "Queued Messages" and
625 * "Nonqueued Messages".
626 * Queued Messages are those picked up and retransmitted by vout_Manage
627 * (using the GetMessage and DispatchMessage functions).
628 * Nonqueued Messages are those that Windows will send directly to this
629 * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
630 *****************************************************************************/
631 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
632 WPARAM wParam, LPARAM lParam )
634 vout_thread_t *p_vout;
636 if( message == WM_CREATE )
638 /* Store p_vout for future use */
639 p_vout = (vout_thread_t *)((CREATESTRUCT *)lParam)->lpCreateParams;
640 SetWindowLong( hwnd, GWL_USERDATA, (LONG)p_vout );
644 p_vout = (vout_thread_t *)GetWindowLong( hwnd, GWL_USERDATA );
650 case WM_WINDOWPOSCHANGED:
651 DirectXUpdateRects( p_vout, VLC_TRUE );
654 /* the user wants to close the window */
656 msg_Dbg( p_vout, "WinProc WM_CLOSE" );
657 /* exit application */
658 p_vout->p_vlc->b_die = VLC_TRUE;
661 /* the window has been closed so shut down everything now */
663 msg_Dbg( p_vout, "WinProc WM_DESTROY" );
664 /* just destroy the window */
665 PostQuitMessage( 0 );
671 case SC_SCREENSAVE: /* catch the screensaver */
672 case SC_MONITORPOWER: /* catch the monitor turn-off */
673 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND" );
674 return 0; /* this stops them from happening */
675 case IDM_TOGGLE_ON_TOP: /* toggle the "on top" status */
678 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND: IDM_TOGGLE_ON_TOP");
680 /* Get the current value... */
681 if( var_Get( p_vout, "directx-on-top", &val ) < 0 )
683 /* ...and change it */
684 val.b_bool = !val.b_bool;
685 var_Set( p_vout, "directx-on-top", val );
693 if( !p_vout->p_sys->b_using_overlay )
695 /* We want to eliminate unnecessary background redraws which create
696 * an annoying flickering */
697 int i_width, i_height, i_x, i_y;
699 GetClipBox( (HDC)wParam, &rect_temp );
701 msg_Dbg( p_vout, "WinProc WM_ERASEBKGND %i,%i,%i,%i",
702 rect_temp.left, rect_temp.top,
703 rect_temp.right, rect_temp.bottom );
705 vout_PlacePicture( p_vout, p_vout->p_sys->i_window_width,
706 p_vout->p_sys->i_window_height,
707 &i_x, &i_y, &i_width, &i_height );
708 ExcludeClipRect( (HDC)wParam, i_x, i_y,
709 i_x + i_width, i_y + i_height );
714 //msg_Dbg( p_vout, "WinProc WM Default %i", message );
718 return DefWindowProc(hwnd, message, wParam, lParam);