events.c

来自「VLC媒体播放程序」· C语言 代码 · 共 756 行 · 第 1/2 页

C
756
字号
/***************************************************************************** * events.c: Windows DirectX video output events handler ***************************************************************************** * Copyright (C) 2001-2004 VideoLAN * $Id: events.c,v 1.38 2004/02/26 13:58:23 gbazin Exp $ * * Authors: Gildas Bazin <gbazin@netcourrier.com> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA. *****************************************************************************//***************************************************************************** * Preamble: This file contains the functions related to the creation of *             a window and the handling of its messages (events). *****************************************************************************/#include <errno.h>                                                 /* ENOMEM */#include <stdlib.h>                                                /* free() */#include <ctype.h>                                              /* tolower() */#include <string.h>                                            /* strerror() */#include <vlc/vlc.h>#include <vlc/intf.h>#include <vlc/input.h>#include <vlc/vout.h>#include <windows.h>#include <windowsx.h>#include <shellapi.h>#include <ddraw.h>#include "vlc_keys.h"#include "vout.h"/***************************************************************************** * Local prototypes. *****************************************************************************/static int  DirectXCreateWindow( vout_thread_t *p_vout );static void DirectXCloseWindow ( vout_thread_t *p_vout );static long FAR PASCAL DirectXEventProc( HWND, UINT, WPARAM, LPARAM );static long FAR PASCAL DirectXVideoEventProc( HWND, UINT, WPARAM, LPARAM );static void DirectXPopupMenu( event_thread_t *p_event, vlc_bool_t b_open ){    playlist_t *p_playlist =        vlc_object_find( p_event, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );    if( p_playlist != NULL )    {        vlc_value_t val;        val.b_bool = b_open;        var_Set( p_playlist, "intf-popupmenu", val );        vlc_object_release( p_playlist );    }}static int DirectXConvertKey( int i_key );/***************************************************************************** * DirectXEventThread: Create video window & handle its messages ***************************************************************************** * This function creates a video window and then enters an infinite loop * that handles the messages sent to that window. * The main goal of this thread is to isolate the Win32 PeekMessage function * because this one can block for a long time. *****************************************************************************/void DirectXEventThread( event_thread_t *p_event ){    MSG msg;    POINT old_mouse_pos = {0,0};    vlc_value_t val;    int i_width, i_height, i_x, i_y;    /* Initialisation */    /* Create a window for the video */    /* Creating a window under Windows also initializes the thread's event     * message queue */    if( DirectXCreateWindow( p_event->p_vout ) )    {        msg_Err( p_event, "out of memory" );        p_event->b_dead = VLC_TRUE;    }    /* Signal the creation of the window */    vlc_thread_ready( p_event );    /* Main loop */    /* GetMessage will sleep if there's no message in the queue */    while( !p_event->b_die && ( p_event->p_vout->p_sys->hparent ||           GetMessage( &msg, p_event->p_vout->p_sys->hwnd, 0, 0 ) ) )    {        /* Check if we are asked to exit */        if( p_event->b_die )            break;        if( p_event->p_vout->p_sys->hparent )        {            /* Parent window was created in another thread so we can't             * access the window messages. */            msleep( INTF_IDLE_SLEEP );            continue;        }        switch( msg.message )        {        case WM_MOUSEMOVE:            vout_PlacePicture( p_event->p_vout,                               p_event->p_vout->p_sys->i_window_width,                               p_event->p_vout->p_sys->i_window_height,                               &i_x, &i_y, &i_width, &i_height );            if( msg.hwnd != p_event->p_vout->p_sys->hwnd )            {                /* Child window */                i_x = i_y = 0;            }            val.i_int = ( GET_X_LPARAM(msg.lParam) - i_x )                         * p_event->p_vout->render.i_width / i_width;            var_Set( p_event->p_vout, "mouse-x", val );            val.i_int = ( GET_Y_LPARAM(msg.lParam) - i_y )                         * p_event->p_vout->render.i_height / i_height;            var_Set( p_event->p_vout, "mouse-y", val );            val.b_bool = VLC_TRUE;            var_Set( p_event->p_vout, "mouse-moved", val );        case WM_NCMOUSEMOVE:            if( (abs(GET_X_LPARAM(msg.lParam) - old_mouse_pos.x) > 2 ||                (abs(GET_Y_LPARAM(msg.lParam) - old_mouse_pos.y)) > 2 ) )            {                GetCursorPos( &old_mouse_pos );                p_event->p_vout->p_sys->i_lastmoved = mdate();                if( p_event->p_vout->p_sys->b_cursor_hidden )                {                    p_event->p_vout->p_sys->b_cursor_hidden = 0;                    ShowCursor( TRUE );                }            }            break;        case WM_VLC_HIDE_MOUSE:            GetCursorPos( &old_mouse_pos );            ShowCursor( FALSE );            break;        case WM_LBUTTONDOWN:            var_Get( p_event->p_vout, "mouse-button-down", &val );            val.i_int |= 1;            var_Set( p_event->p_vout, "mouse-button-down", val );            DirectXPopupMenu( p_event, VLC_FALSE );            break;        case WM_LBUTTONUP:            var_Get( p_event->p_vout, "mouse-button-down", &val );            val.i_int &= ~1;            var_Set( p_event->p_vout, "mouse-button-down", val );            val.b_bool = VLC_TRUE;            var_Set( p_event->p_vout, "mouse-clicked", val );            break;        case WM_LBUTTONDBLCLK:            p_event->p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;            break;        case WM_MBUTTONDOWN:            var_Get( p_event->p_vout, "mouse-button-down", &val );            val.i_int |= 2;            var_Set( p_event->p_vout, "mouse-button-down", val );            DirectXPopupMenu( p_event, VLC_FALSE );            break;        case WM_MBUTTONUP:            var_Get( p_event->p_vout, "mouse-button-down", &val );            val.i_int &= ~2;            var_Set( p_event->p_vout, "mouse-button-down", val );            break;        case WM_RBUTTONDOWN:            var_Get( p_event->p_vout, "mouse-button-down", &val );            val.i_int |= 4;            var_Set( p_event->p_vout, "mouse-button-down", val );            DirectXPopupMenu( p_event, VLC_FALSE );            break;        case WM_RBUTTONUP:            var_Get( p_event->p_vout, "mouse-button-down", &val );            val.i_int &= ~4;            var_Set( p_event->p_vout, "mouse-button-down", val );            DirectXPopupMenu( p_event, VLC_TRUE );            break;        case WM_KEYDOWN:        case WM_SYSKEYDOWN:            /* The key events are first processed here and not translated             * into WM_CHAR events because we need to know the status of the             * modifier keys. */            val.i_int = DirectXConvertKey( msg.wParam );            if( !val.i_int )            {                /* This appears to be a "normal" (ascii) key */                val.i_int = tolower( MapVirtualKey( msg.wParam, 2 ) );            }            if( val.i_int )            {                if( GetKeyState(VK_CONTROL) & 0x8000 )                {                    val.i_int |= KEY_MODIFIER_CTRL;                }                if( GetKeyState(VK_SHIFT) & 0x8000 )                {                    val.i_int |= KEY_MODIFIER_SHIFT;                }                if( GetKeyState(VK_MENU) & 0x8000 )                {                    val.i_int |= KEY_MODIFIER_ALT;                }                var_Set( p_event->p_vlc, "key-pressed", val );            }            break;        case WM_VLC_CHANGE_TEXT:            if( p_event->p_vout->p_sys->b_using_overlay )                SetWindowText( p_event->p_vout->p_sys->hwnd,                    VOUT_TITLE " (hardware YUV overlay DirectX output)" );            else if( p_event->p_vout->p_sys->b_hw_yuv )                SetWindowText( p_event->p_vout->p_sys->hwnd,                    VOUT_TITLE " (hardware YUV DirectX output)" );            else SetWindowText( p_event->p_vout->p_sys->hwnd,                    VOUT_TITLE " (software RGB DirectX output)" );            break;        default:            /* Messages we don't handle directly are dispatched to the             * window procedure */            TranslateMessage(&msg);            DispatchMessage(&msg);            break;        } /* End Switch */    } /* End Main loop */    if( msg.message == WM_QUIT )    {        msg_Warn( p_event, "WM_QUIT... should not happen!!" );        p_event->p_vout->p_sys->hwnd = NULL; /* Window already destroyed */    }    msg_Dbg( p_event, "DirectXEventThread terminating" );    /* clear the changes formerly signaled */    p_event->p_vout->p_sys->i_changes = 0;    DirectXCloseWindow( p_event->p_vout );}/* following functions are local *//***************************************************************************** * DirectXCreateWindow: create a window for the video. ***************************************************************************** * Before creating a direct draw surface, we need to create a window in which * the video will be displayed. This window will also allow us to capture the * events. *****************************************************************************/static int DirectXCreateWindow( vout_thread_t *p_vout ){    HINSTANCE  hInstance;    HMENU      hMenu;    RECT       rect_window;    vlc_value_t val;    msg_Dbg( p_vout, "DirectXCreateWindow" );    /* Get this module's instance */    hInstance = GetModuleHandle(NULL);    /* If an external window was specified, we'll draw in it. */    var_Get( p_vout->p_vlc, "drawable", &val );    p_vout->p_sys->hparent = p_vout->p_sys->hwnd =             val.i_int ?  (void*)(ptrdiff_t) val.i_int : NULL;    if( p_vout->p_sys->hparent )    {        msg_Dbg( p_vout, "using external window %p\n", p_vout->p_sys->hwnd );        /* Set stuff in the window that we can not put directly in         * a class (see below). */        SetClassLong( p_vout->p_sys->hwnd,                      GCL_STYLE, CS_DBLCLKS );        SetClassLong( p_vout->p_sys->hwnd,                      GCL_HBRBACKGROUND, (LONG)GetStockObject(BLACK_BRUSH) );        SetClassLong( p_vout->p_sys->hwnd,                      GCL_HCURSOR, (LONG)LoadCursor(NULL, IDC_ARROW) );        /* Store a p_vout pointer into the window local storage (for later         * use in DirectXEventProc). */        SetWindowLong( p_vout->p_sys->hwnd, GWL_USERDATA, (LONG)p_vout );        p_vout->p_sys->pf_wndproc =               (WNDPROC)SetWindowLong( p_vout->p_sys->hwnd,                                       GWL_WNDPROC, (LONG)DirectXEventProc );        /* Blam! Erase everything that might have been there. */        InvalidateRect( p_vout->p_sys->hwnd, NULL, TRUE );    }    else    {        WNDCLASSEX wc;                            /* window class components */        HICON      vlc_icon = NULL;        char       vlc_path[MAX_PATH+1];        /* Get the Icon from the main app */        vlc_icon = NULL;        if( GetModuleFileName( NULL, vlc_path, MAX_PATH ) )        {            vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );        }        /* Fill in the window class structure */        wc.cbSize        = sizeof(WNDCLASSEX);        wc.style         = CS_DBLCLKS;                   /* style: dbl click */        wc.lpfnWndProc   = (WNDPROC)DirectXEventProc;       /* event handler */        wc.cbClsExtra    = 0;                         /* no extra class data */        wc.cbWndExtra    = 0;                        /* no extra window data */        wc.hInstance     = hInstance;                            /* instance */        wc.hIcon         = vlc_icon;                /* load the vlc big icon */        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);    /* default cursor */        wc.hbrBackground = GetStockObject(BLACK_BRUSH);  /* background color */        wc.lpszMenuName  = NULL;                                  /* no menu */        wc.lpszClassName = "VLC DirectX";             /* use a special class */        wc.hIconSm       = vlc_icon;              /* load the vlc small icon */        /* Register the window class */        if( !RegisterClassEx(&wc) )        {            WNDCLASS wndclass;            if( vlc_icon )            {                DestroyIcon( vlc_icon );            }            /* Check why it failed. If it's because one already exists             * then fine, otherwise return with an error. */            if( !GetClassInfo( hInstance, "VLC DirectX", &wndclass ) )            {                msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED" );                return VLC_EGENERIC;            }        }        /* When you create a window you give the dimensions you wish it to         * have. Unfortunatly these dimensions will include the borders and         * titlebar. We use the following function to find out the size of         * the window corresponding to the useable surface we want */        rect_window.top    = 10;        rect_window.left   = 10;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?