Example #1
0
/*****************************************************************************
 * EventThread: 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.
 *****************************************************************************/
static void *EventThread( void *p_this )
{
    event_thread_t *p_event = (event_thread_t *)p_this;
    vout_display_t *vd = p_event->vd;
    MSG msg;
    POINT old_mouse_pos = {0,0}, mouse_pos;
    int canc = vlc_savecancel ();

    bool b_mouse_support = var_InheritBool( p_event->vd, "mouse-events" );
    bool b_key_support = var_InheritBool( p_event->vd, "keyboard-events" );

    vlc_mutex_lock( &p_event->lock );
    /* Create a window for the video */
    /* Creating a window under Windows also initializes the thread's event
     * message queue */
    if( DirectXCreateWindow( p_event ) )
        p_event->b_error = true;

    p_event->b_ready = true;
    vlc_cond_signal( &p_event->wait );

    const bool b_error = p_event->b_error;
    vlc_mutex_unlock( &p_event->lock );

    if( b_error )
    {
        vlc_restorecancel( canc );
        return NULL;
    }

#ifndef UNDER_CE
    /* Prevent monitor from powering off */
    SetThreadExecutionState( ES_DISPLAY_REQUIRED | ES_CONTINUOUS );
#endif

    /* Main loop */
    /* GetMessage will sleep if there's no message in the queue */
    for( ;; )
    {
        vout_display_place_t place;
        video_format_t       source;

        if( !GetMessage( &msg, 0, 0, 0 ) )
        {
            vlc_mutex_lock( &p_event->lock );
            p_event->b_done = true;
            vlc_mutex_unlock( &p_event->lock );
            break;
        }

        /* Check if we are asked to exit */
        vlc_mutex_lock( &p_event->lock );
        const bool b_done = p_event->b_done;
        vlc_mutex_unlock( &p_event->lock );
        if( b_done )
            break;

        if( !b_mouse_support && isMouseEvent( msg.message ) )
            continue;

        if( !b_key_support && isKeyEvent( msg.message ) )
            continue;

        /* Handle mouse state */
        if( msg.message == WM_MOUSEMOVE ||
            msg.message == WM_NCMOUSEMOVE )
        {
            GetCursorPos( &mouse_pos );
            /* FIXME, why this >2 limits ? */
            if( (abs(mouse_pos.x - old_mouse_pos.x) > 2 ||
                (abs(mouse_pos.y - old_mouse_pos.y)) > 2 ) )
            {
                old_mouse_pos = mouse_pos;
                UpdateCursor( p_event, true );
            }
        }
        else if( isMouseEvent( msg.message ) )
        {
            UpdateCursor( p_event, true );
        }
        else if( msg.message == WM_VLC_HIDE_MOUSE )
        {
            UpdateCursor( p_event, false );
        }

        /* */
        switch( msg.message )
        {
        case WM_MOUSEMOVE:
            vlc_mutex_lock( &p_event->lock );
            place  = p_event->place;
            source = p_event->source;
            vlc_mutex_unlock( &p_event->lock );

            if( place.width > 0 && place.height > 0 )
            {
                if( msg.hwnd == p_event->hvideownd )
                {
                    /* Child window */
                    place.x = 0;
                    place.y = 0;
                }
                const int x = source.i_x_offset +
                    (int64_t)(GET_X_LPARAM(msg.lParam) - place.x) * source.i_width  / place.width;
                const int y = source.i_y_offset +
                    (int64_t)(GET_Y_LPARAM(msg.lParam) - place.y) * source.i_height / place.height;
                vout_display_SendEventMouseMoved(vd, x, y);
            }
            break;
        case WM_NCMOUSEMOVE:
            break;

        case WM_VLC_HIDE_MOUSE:
            break;

        case WM_LBUTTONDOWN:
            MousePressed( p_event, msg.hwnd, MOUSE_BUTTON_LEFT );
            break;
        case WM_LBUTTONUP:
            MouseReleased( p_event, MOUSE_BUTTON_LEFT );
            break;
        case WM_LBUTTONDBLCLK:
            vout_display_SendEventMouseDoubleClick(vd);
            break;

        case WM_MBUTTONDOWN:
            MousePressed( p_event, msg.hwnd, MOUSE_BUTTON_CENTER );
            break;
        case WM_MBUTTONUP:
            MouseReleased( p_event, MOUSE_BUTTON_CENTER );
            break;

        case WM_RBUTTONDOWN:
            MousePressed( p_event, msg.hwnd, MOUSE_BUTTON_RIGHT );
            break;
        case WM_RBUTTONUP:
            MouseReleased( p_event, MOUSE_BUTTON_RIGHT );
            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. */
            int i_key = DirectXConvertKey( msg.wParam );
            if( !i_key )
            {
                /* This appears to be a "normal" (ascii) key */
                i_key = tolower( MapVirtualKey( msg.wParam, 2 ) );
            }

            if( i_key )
            {
                if( GetKeyState(VK_CONTROL) & 0x8000 )
                {
                    i_key |= KEY_MODIFIER_CTRL;
                }
                if( GetKeyState(VK_SHIFT) & 0x8000 )
                {
                    i_key |= KEY_MODIFIER_SHIFT;
                }
                if( GetKeyState(VK_MENU) & 0x8000 )
                {
                    i_key |= KEY_MODIFIER_ALT;
                }

                vout_display_SendEventKey(vd, i_key);
            }
            break;
        }

        case WM_MOUSEWHEEL:
        {
            int i_key;
            if( GET_WHEEL_DELTA_WPARAM( msg.wParam ) > 0 )
            {
                i_key = KEY_MOUSEWHEELUP;
            }
            else
            {
                i_key = KEY_MOUSEWHEELDOWN;
            }
            if( i_key )
            {
                if( GetKeyState(VK_CONTROL) & 0x8000 )
                {
                    i_key |= KEY_MODIFIER_CTRL;
                }
                if( GetKeyState(VK_SHIFT) & 0x8000 )
                {
                    i_key |= KEY_MODIFIER_SHIFT;
                }
                if( GetKeyState(VK_MENU) & 0x8000 )
                {
                    i_key |= KEY_MODIFIER_ALT;
                }
                vout_display_SendEventKey(vd, i_key);
            }
            break;
        }

        case WM_VLC_CHANGE_TEXT:
        {
            vlc_mutex_lock( &p_event->lock );
            wchar_t *pwz_title = NULL;
            if( p_event->psz_title )
            {
                const size_t i_length = strlen(p_event->psz_title);
                pwz_title = malloc( 2 * (i_length + 1) );
                if( pwz_title )
                {
                    mbstowcs( pwz_title, p_event->psz_title, 2 * i_length );
                    pwz_title[i_length] = 0;
                }
            }
            vlc_mutex_unlock( &p_event->lock );

            if( pwz_title )
            {
                SetWindowTextW( p_event->hwnd, pwz_title );
                if( p_event->hfswnd )
                    SetWindowTextW( p_event->hfswnd, pwz_title );
                free( pwz_title );
            }
            break;
        }

        default:
            /* Messages we don't handle directly are dispatched to the
             * window procedure */
            TranslateMessage(&msg);
            DispatchMessage(&msg);
            break;

        } /* End Switch */

    } /* End Main loop */

    /* Check for WM_QUIT if we created the window */
    if( !p_event->hparent && msg.message == WM_QUIT )
    {
        msg_Warn( vd, "WM_QUIT... should not happen!!" );
        p_event->hwnd = NULL; /* Window already destroyed */
    }

    msg_Dbg( vd, "DirectXEventThread terminating" );

    DirectXCloseWindow( p_event );
    vlc_restorecancel(canc);
    return NULL;
}
Example #2
0
static MRESULT EXPENTRY WndProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
{
    vout_display_t * vd = WinQueryWindowPtr( hwnd, 0 );
    MRESULT result = ( MRESULT )TRUE;

    if ( !vd )
        return WinDefWindowProc( hwnd, msg, mp1, mp2 );

    vout_display_sys_t * sys = vd->sys;
    RECTL rcl;
    SWP   swp;

    if ( sys->is_mouse_hidden &&
            ((msg >= WM_MOUSEFIRST    && msg <= WM_MOUSELAST) ||
             (msg >= WM_EXTMOUSEFIRST && msg <= WM_EXTMOUSELAST) ||
             msg == WM_MOUSELEAVE))
    {
        WinShowPointer(HWND_DESKTOP, TRUE);
        sys->is_mouse_hidden = false;
    }

    switch( msg )
    {
    /* the user wants to close the window */
    case WM_CLOSE:
        vout_display_SendEventClose(vd);
        result = 0;
        break;

    case WM_MOUSEMOVE :
    {
        SHORT i_mouse_x = SHORT1FROMMP( mp1 );
        SHORT i_mouse_y = SHORT2FROMMP( mp1 );
        RECTL movie_rect;
        int   i_movie_width, i_movie_height;
        int   i_src_width, i_src_height;

        /* Get a current movie area */
        kvaAdjustDstRect( &sys->kvas.rclSrcRect, &movie_rect );
        i_movie_width = movie_rect.xRight - movie_rect.xLeft;
        i_movie_height = movie_rect.yTop - movie_rect.yBottom;

        i_src_width =  sys->kvas.rclSrcRect.xRight -
                       sys->kvas.rclSrcRect.xLeft;
        i_src_height = sys->kvas.rclSrcRect.yBottom -
                       sys->kvas.rclSrcRect.yTop;

        int x = ( i_mouse_x - movie_rect.xLeft ) *
                i_src_width / i_movie_width +
                sys->kvas.rclSrcRect.xLeft;
        int y = ( i_mouse_y - movie_rect.yBottom ) *
                i_src_height / i_movie_height;

        /* Invert Y coordinate and add y offset */
        y = ( i_src_height - y ) + sys->kvas.rclSrcRect.yTop;;

        vout_display_SendEventMouseMoved(vd, x, y);

        result = WinDefWindowProc( hwnd, msg, mp1,mp2 );
        break;
    }

    case WM_BUTTON1DOWN :
        MousePressed( vd, hwnd, MOUSE_BUTTON_LEFT );
        break;

    case WM_BUTTON2DOWN :
        MousePressed( vd, hwnd, MOUSE_BUTTON_RIGHT );
        break;

    case WM_BUTTON3DOWN :
        MousePressed( vd, hwnd, MOUSE_BUTTON_CENTER );
        break;

    case WM_BUTTON1UP :
        MouseReleased( vd, MOUSE_BUTTON_LEFT );
        break;

    case WM_BUTTON2UP :
        MouseReleased( vd, MOUSE_BUTTON_RIGHT );
        break;

    case WM_BUTTON3UP :
        MouseReleased( vd, MOUSE_BUTTON_CENTER );
        break;

    case WM_BUTTON1DBLCLK :
        vout_display_SendEventMouseDoubleClick(vd);
        break;

    case WM_TRANSLATEACCEL :
        /* We have no accelerator table at all */
        result = ( MRESULT )FALSE;
        break;

    case WM_CHAR :
    {
        USHORT i_flags = SHORT1FROMMP( mp1 );
        USHORT i_ch    = SHORT1FROMMP( mp2 );
        USHORT i_vk    = SHORT2FROMMP( mp2 );
        int    i_key   = 0;

        /* If embedded window, let the parent process keys */
        if( sys->parent_window )
        {
            WinPostMsg( sys->parent, msg, mp1, mp2 );
            break;
        }

        if( !( i_flags & KC_KEYUP ))
        {
            if( i_flags & KC_VIRTUALKEY )
                /* convert the key if possible */
                i_key = ConvertKey( i_vk );
            else if(( i_flags & KC_CHAR ) && !HIBYTE( i_ch ))
                i_key = tolower( i_ch );

            if( i_key )
            {
                if( i_flags & KC_SHIFT )
                    i_key |= KEY_MODIFIER_SHIFT;

                if( i_flags & KC_CTRL )
                    i_key |= KEY_MODIFIER_CTRL;

                if( i_flags & KC_ALT )
                    i_key |= KEY_MODIFIER_ALT;

                vout_display_SendEventKey(vd, i_key);
            }
        }
        break;
    }

    /* Process Manage() call */
    case WM_VLC_MANAGE :
        break;

    /* Fullscreen change */
    case WM_VLC_FULLSCREEN_CHANGE :
        if( LONGFROMMP( mp1 ))
        {
            WinQueryWindowPos( sys->frame, &swp );
            sys->client_rect.xLeft   = swp.x;
            sys->client_rect.yBottom = swp.y;
            sys->client_rect.xRight  = sys->client_rect.xLeft   + swp.cx;
            sys->client_rect.yTop    = sys->client_rect.yBottom + swp.cy;
            WinCalcFrameRect( sys->frame, &sys->client_rect, TRUE );

            rcl.xLeft   = 0;
            rcl.yBottom = 0;
            rcl.xRight  = sys->i_screen_width;
            rcl.yTop    = sys->i_screen_height;
        }
        else
            rcl = sys->client_rect;

        WinCalcFrameRect( sys->frame, &rcl, FALSE );

        WinSetWindowPos( sys->frame, HWND_TOP,
                         rcl.xLeft, rcl.yBottom,
                         rcl.xRight - rcl.xLeft, rcl.yTop - rcl.yBottom,
                         SWP_MOVE | SWP_SIZE | SWP_ZORDER | SWP_SHOW |
                         SWP_ACTIVATE );
        break;

    /* Size change */
    case WM_VLC_SIZE_CHANGE :
        rcl.xLeft   = 0;
        rcl.yBottom = 0;
        rcl.xRight  = LONGFROMMP( mp1 );
        rcl.yTop    = LONGFROMMP( mp2 );
        WinCalcFrameRect( sys->frame, &rcl, FALSE );

        WinSetWindowPos( sys->frame, NULLHANDLE,
                         0, 0,
                         rcl.xRight - rcl.xLeft, rcl.yTop - rcl.yBottom,
                         SWP_SIZE );

        WinQueryWindowPos( sys->frame, &swp );
        sys->client_rect.xLeft   = swp.x;
        sys->client_rect.yBottom = swp.y;
        sys->client_rect.xRight  = sys->client_rect.xLeft   + swp.cx;
        sys->client_rect.yTop    = sys->client_rect.yBottom + swp.cy;
        WinCalcFrameRect( sys->frame, &sys->client_rect, TRUE );
        break;

    default :
        return WinDefWindowProc( hwnd, msg, mp1, mp2 );
    }

    /* If embedded window, we need to change our window size according to a
     * parent window size */
    if( sys->parent_window )
    {
        WinQueryWindowRect( sys->parent, &rcl );

        if( rcl.xLeft   != sys->parent_rect.xLeft   ||
                rcl.yBottom != sys->parent_rect.yBottom ||
                rcl.xRight  != sys->parent_rect.xRight  ||
                rcl.yTop    != sys->parent_rect.yTop)
        {
            sys->parent_rect = rcl;

            WinCalcFrameRect( sys->frame, &rcl, FALSE );

            WinSetWindowPos( sys->frame, NULLHANDLE,
                             rcl.xLeft, rcl.yBottom,
                             rcl.xRight - rcl.xLeft, rcl.yTop - rcl.yBottom,
                             SWP_SIZE | SWP_MOVE );
        }
    }

    return result;
}