main_interface_win32.cpp 15.5 KB
Newer Older
1
/*****************************************************************************
Pere Orga's avatar
Pere Orga committed
2
 * main_interface_win32.cpp : Main interface
3 4 5 6 7
 ****************************************************************************
 * Copyright (C) 2006-2010 VideoLAN and AUTHORS
 * $Id$
 *
 * Authors: Jean-Baptiste Kempf <jb@videolan.org>
8
 *          Hugo Beauzée-Luyssen <hugo@beauzee.fr>
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

25 26 27
#if HAVE_CONFIG_H
# include "config.h"
#endif
28

29
#include "main_interface_win32.hpp"
30 31 32

#include "input_manager.hpp"
#include "actions_manager.hpp"
33
#include "dialogs_provider.hpp"
34
#include "components/interface_widgets.hpp"
35

36
#include <QBitmap>
37

38 39
#include <assert.h>

40 41
#include <QWindow>
#include <qpa/qplatformnativeinterface.h>
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#define WM_APPCOMMAND 0x0319

#define APPCOMMAND_VOLUME_MUTE            8
#define APPCOMMAND_VOLUME_DOWN            9
#define APPCOMMAND_VOLUME_UP              10
#define APPCOMMAND_MEDIA_NEXTTRACK        11
#define APPCOMMAND_MEDIA_PREVIOUSTRACK    12
#define APPCOMMAND_MEDIA_STOP             13
#define APPCOMMAND_MEDIA_PLAY_PAUSE       14
#define APPCOMMAND_LAUNCH_MEDIA_SELECT    16
#define APPCOMMAND_BASS_DOWN              19
#define APPCOMMAND_BASS_BOOST             20
#define APPCOMMAND_BASS_UP                21
#define APPCOMMAND_TREBLE_DOWN            22
#define APPCOMMAND_TREBLE_UP              23
#define APPCOMMAND_MICROPHONE_VOLUME_MUTE 24
#define APPCOMMAND_MICROPHONE_VOLUME_DOWN 25
#define APPCOMMAND_MICROPHONE_VOLUME_UP   26
61 62
#define APPCOMMAND_HELP                   27
#define APPCOMMAND_OPEN                   30
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#define APPCOMMAND_DICTATE_OR_COMMAND_CONTROL_TOGGLE    43
#define APPCOMMAND_MIC_ON_OFF_TOGGLE      44
#define APPCOMMAND_MEDIA_PLAY             46
#define APPCOMMAND_MEDIA_PAUSE            47
#define APPCOMMAND_MEDIA_RECORD           48
#define APPCOMMAND_MEDIA_FAST_FORWARD     49
#define APPCOMMAND_MEDIA_REWIND           50
#define APPCOMMAND_MEDIA_CHANNEL_UP       51
#define APPCOMMAND_MEDIA_CHANNEL_DOWN     52

#define FAPPCOMMAND_MOUSE 0x8000
#define FAPPCOMMAND_KEY   0
#define FAPPCOMMAND_OEM   0x1000
#define FAPPCOMMAND_MASK  0xF000

#define GET_APPCOMMAND_LPARAM(lParam) ((short)(HIWORD(lParam) & ~FAPPCOMMAND_MASK))
#define GET_DEVICE_LPARAM(lParam)     ((WORD)(HIWORD(lParam) & FAPPCOMMAND_MASK))
#define GET_MOUSEORKEY_LPARAM         GET_DEVICE_LPARAM
#define GET_FLAGS_LPARAM(lParam)      (LOWORD(lParam))
#define GET_KEYSTATE_LPARAM(lParam)   GET_FLAGS_LPARAM(lParam)

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
MainInterfaceWin32::MainInterfaceWin32( intf_thread_t *_p_intf )
    : MainInterface( _p_intf )
    , himl( NULL )
    , p_taskbl( NULL )
{
    /* Volume keys */
    _p_intf->p_sys->disable_volume_keys = var_InheritBool( _p_intf, "qt-disable-volume-keys" );
    taskbar_wmsg = RegisterWindowMessage(TEXT("TaskbarButtonCreated"));
    if (taskbar_wmsg == 0)
        msg_Warn( p_intf, "Failed to register TaskbarButtonCreated message" );
}

MainInterfaceWin32::~MainInterfaceWin32()
{
    if( himl )
        ImageList_Destroy( himl );
    if(p_taskbl)
        p_taskbl->Release();
    CoUninitialize();
}

HWND MainInterfaceWin32::WinId( QWidget *w )
106
{
107 108 109 110 111
    if( w && w->windowHandle() )
        return static_cast<HWND>(QGuiApplication::platformNativeInterface()->
            nativeResourceForWindow("handle", w->windowHandle()));
    else
        return 0;
112 113 114
}

Q_GUI_EXPORT HBITMAP qt_pixmapToWinHBITMAP(const QPixmap &p, int hbitmapFormat = 0);
115

116 117 118 119 120 121 122
enum HBitmapFormat
{
    NoAlpha,
    PremultipliedAlpha,
    Alpha
};

123
void MainInterfaceWin32::createTaskBarButtons()
124 125 126 127
{
    /*Here is the code for the taskbar thumb buttons
    FIXME:We need pretty buttons in 16x16 px that are handled correctly by masks in Qt
    */
128 129
    p_taskbl = NULL;
    himl = NULL;
130

131
    HRESULT hr = CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );
132 133
    if( FAILED(hr) )
        return;
134

135 136 137 138
    void *pv;
    hr = CoCreateInstance( CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER,
                           IID_ITaskbarList3, &pv);
    if( FAILED(hr) )
139
    {
140 141
        CoUninitialize();
        return;
142
    }
143 144 145 146

    p_taskbl = (ITaskbarList3 *)pv;
    p_taskbl->HrInit();

147 148 149
    int iconX = GetSystemMetrics(SM_CXSMICON);
    int iconY = GetSystemMetrics(SM_CYSMICON);
    himl = ImageList_Create( iconX /*cx*/, iconY /*cy*/, ILC_COLOR32 /*flags*/,
150 151
                             4 /*cInitial*/, 0 /*cGrow*/);
    if( himl == NULL )
152
    {
153
        p_taskbl->Release();
154
        p_taskbl = NULL;
155 156 157 158
        CoUninitialize();
        return;
    }

159 160 161 162
    QPixmap img   = QPixmap(":/win7/prev.svg").scaled( iconX, iconY );
    QPixmap img2  = QPixmap(":/win7/pause.svg").scaled( iconX, iconY );
    QPixmap img3  = QPixmap(":/win7/play.svg").scaled( iconX, iconY );
    QPixmap img4  = QPixmap(":/win7/next.svg").scaled( iconX, iconY );
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    QBitmap mask  = img.createMaskFromColor(Qt::transparent);
    QBitmap mask2 = img2.createMaskFromColor(Qt::transparent);
    QBitmap mask3 = img3.createMaskFromColor(Qt::transparent);
    QBitmap mask4 = img4.createMaskFromColor(Qt::transparent);

    if( -1 == ImageList_Add(himl, qt_pixmapToWinHBITMAP(img, PremultipliedAlpha), qt_pixmapToWinHBITMAP(mask)))
        msg_Err( p_intf, "%s ImageList_Add failed", "First" );
    if( -1 == ImageList_Add(himl, qt_pixmapToWinHBITMAP(img2, PremultipliedAlpha), qt_pixmapToWinHBITMAP(mask2)))
        msg_Err( p_intf, "%s ImageList_Add failed", "Second" );
    if( -1 == ImageList_Add(himl, qt_pixmapToWinHBITMAP(img3, PremultipliedAlpha), qt_pixmapToWinHBITMAP(mask3)))
        msg_Err( p_intf, "%s ImageList_Add failed", "Third" );
    if( -1 == ImageList_Add(himl, qt_pixmapToWinHBITMAP(img4, PremultipliedAlpha), qt_pixmapToWinHBITMAP(mask4)))
        msg_Err( p_intf, "%s ImageList_Add failed", "Fourth" );

    // Define an array of two buttons. These buttons provide images through an
    // image list and also provide tooltips.
    THUMBBUTTONMASK dwMask = THUMBBUTTONMASK(THB_BITMAP | THB_FLAGS);
    THUMBBUTTON thbButtons[3];

    thbButtons[0].dwMask = dwMask;
    thbButtons[0].iId = 0;
    thbButtons[0].iBitmap = 0;
185
    thbButtons[0].dwFlags = THEPL->items.i_size > 1 ? THBF_ENABLED : THBF_HIDDEN;
186 187 188 189

    thbButtons[1].dwMask = dwMask;
    thbButtons[1].iId = 1;
    thbButtons[1].iBitmap = 2;
190
    thbButtons[1].dwFlags = THEPL->items.i_size > 0 ? THBF_ENABLED : THBF_HIDDEN;
191 192 193 194

    thbButtons[2].dwMask = dwMask;
    thbButtons[2].iId = 2;
    thbButtons[2].iBitmap = 3;
195
    thbButtons[2].dwFlags = THEPL->items.i_size > 1 ? THBF_ENABLED : THBF_HIDDEN;
196 197 198 199 200 201 202 203 204 205 206

    hr = p_taskbl->ThumbBarSetImageList( WinId(this), himl );
    if( FAILED(hr) )
        msg_Err( p_intf, "%s failed with error %08lx", "ThumbBarSetImageList",
                 hr );
    else
    {
        hr = p_taskbl->ThumbBarAddButtons( WinId(this), 3, thbButtons);
        if( FAILED(hr) )
            msg_Err( p_intf, "%s failed with error %08lx",
                     "ThumbBarAddButtons", hr );
207
    }
208 209
    CONNECT( THEMIM->getIM(), playingStatusChanged( int ),
             this, changeThumbbarButtons( int ) );
210 211 212 213
    CONNECT( THEMIM, playlistItemAppended( int, int ),
            this, playlistItemAppended( int, int ) );
    CONNECT( THEMIM, playlistItemRemoved( int ),
            this, playlistItemRemoved( int ) );
214 215
    if( THEMIM->getIM()->playingStatus() == PLAYING_S )
        changeThumbbarButtons( THEMIM->getIM()->playingStatus() );
216 217
}

218
bool MainInterfaceWin32::nativeEvent(const QByteArray &, void *message, long *result)
219 220 221 222
{
    return winEvent( static_cast<MSG*>( message ), result );
}

223
bool MainInterfaceWin32::winEvent ( MSG * msg, long * result )
224 225 226
{
    if (msg->message == taskbar_wmsg)
    {
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
227
        //We received the taskbarbuttoncreated, now we can really create the buttons
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
        createTaskBarButtons();
    }

    short cmd;
    switch( msg->message )
    {
        case WM_COMMAND:
            if (HIWORD(msg->wParam) == THBN_CLICKED)
            {
                switch(LOWORD(msg->wParam))
                {
                    case 0:
                        THEMIM->prev();
                        break;
                    case 1:
                        THEMIM->togglePlayPause();
                        break;
                    case 2:
                        THEMIM->next();
                        break;
                }
            }
            break;
        case WM_APPCOMMAND:
            cmd = GET_APPCOMMAND_LPARAM(msg->lParam);
253

254
            if( p_intf->p_sys->disable_volume_keys &&
255 256 257 258 259 260 261
                    (   cmd == APPCOMMAND_VOLUME_DOWN   ||
                        cmd == APPCOMMAND_VOLUME_UP     ||
                        cmd == APPCOMMAND_VOLUME_MUTE ) )
            {
                break;
            }

262
            *result = TRUE;
263

264 265 266 267 268 269 270 271 272 273 274
            switch(cmd)
            {
                case APPCOMMAND_MEDIA_PLAY_PAUSE:
                    THEMIM->togglePlayPause();
                    break;
                case APPCOMMAND_MEDIA_PLAY:
                    THEMIM->play();
                    break;
                case APPCOMMAND_MEDIA_PAUSE:
                    THEMIM->pause();
                    break;
275
                case APPCOMMAND_MEDIA_CHANNEL_DOWN:
276 277 278
                case APPCOMMAND_MEDIA_PREVIOUSTRACK:
                    THEMIM->prev();
                    break;
279
                case APPCOMMAND_MEDIA_CHANNEL_UP:
280 281 282 283 284 285
                case APPCOMMAND_MEDIA_NEXTTRACK:
                    THEMIM->next();
                    break;
                case APPCOMMAND_MEDIA_STOP:
                    THEMIM->stop();
                    break;
286 287 288
                case APPCOMMAND_MEDIA_RECORD:
                    THEAM->record();
                    break;
289 290 291 292 293 294 295 296 297
                case APPCOMMAND_VOLUME_DOWN:
                    THEAM->AudioDown();
                    break;
                case APPCOMMAND_VOLUME_UP:
                    THEAM->AudioUp();
                    break;
                case APPCOMMAND_VOLUME_MUTE:
                    THEAM->toggleMuteAudio();
                    break;
298 299 300 301 302 303 304 305 306 307 308 309
                case APPCOMMAND_MEDIA_FAST_FORWARD:
                    THEMIM->getIM()->faster();
                    break;
                case APPCOMMAND_MEDIA_REWIND:
                    THEMIM->getIM()->slower();
                    break;
                case APPCOMMAND_HELP:
                    THEDP->mediaInfoDialog();
                    break;
                case APPCOMMAND_OPEN:
                    THEDP->simpleOpenDialog();
                    break;
310 311
                default:
                     msg_Dbg( p_intf, "unknown APPCOMMAND = %d", cmd);
312
                     *result = FALSE;
313 314
                     break;
            }
315
            if (*result) return true;
316 317 318 319 320
            break;
    }
    return false;
}

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
void MainInterfaceWin32::setVideoFullScreen( bool fs )
{
    MainInterface::setVideoFullScreen( fs );
    if( !fs )
        changeThumbbarButtons( THEMIM->getIM()->playingStatus() );
}

void MainInterfaceWin32::toggleUpdateSystrayMenuWhenVisible()
{
    /* check if any visible window is above vlc in the z-order,
     * but ignore the ones always on top
     * and the ones which can't be activated */
    HWND winId;
    QWindow *window = windowHandle();
    winId = static_cast<HWND>(QGuiApplication::platformNativeInterface()->nativeResourceForWindow("handle", window));

    WINDOWINFO wi;
    HWND hwnd;
    wi.cbSize = sizeof( WINDOWINFO );
    for( hwnd = GetNextWindow( winId, GW_HWNDPREV );
            hwnd && ( !IsWindowVisible( hwnd ) || ( GetWindowInfo( hwnd, &wi ) &&
                                                    ( wi.dwExStyle&WS_EX_NOACTIVATE ) ) );
            hwnd = GetNextWindow( hwnd, GW_HWNDPREV ) )
    {
    }
    if( !hwnd || !GetWindowInfo( hwnd, &wi ) || (wi.dwExStyle&WS_EX_TOPMOST) )
        hide();
    else
        activateWindow();
}

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385

void MainInterfaceWin32::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);

    /*
     * Detects if window placement is not in its normal position (ex: win7 aero snap)
     * This function compares the normal position (non snapped) to the current position.
     * The current position is translated from screen referential to workspace referential
     * to workspace referential
     */
    b_isWindowTiled = false;
    HWND winHwnd = WinId( this );

    WINDOWPLACEMENT windowPlacement;
    windowPlacement.length = sizeof( windowPlacement );
    if ( GetWindowPlacement( winHwnd, &windowPlacement ) == 0 )
        return;

    if ( windowPlacement.showCmd != SW_SHOWNORMAL )
        return;

    HMONITOR monitor = MonitorFromWindow( winHwnd, MONITOR_DEFAULTTONEAREST );

    MONITORINFO monitorInfo;
    monitorInfo.cbSize = sizeof( monitorInfo );
    if ( GetMonitorInfo( monitor, &monitorInfo )  == 0 )
        return;

    RECT windowRect;
    if ( GetWindowRect( winHwnd, &windowRect ) == 0 )
        return;

    OffsetRect( &windowRect,
Pierre Lamot's avatar
Pierre Lamot committed
386 387
                monitorInfo.rcMonitor.left - monitorInfo.rcWork.left ,
                monitorInfo.rcMonitor.top - monitorInfo.rcWork.top );
388 389 390 391

    b_isWindowTiled = ( EqualRect( &windowPlacement.rcNormalPosition, &windowRect ) == 0 );
}

392 393 394 395 396 397 398
void MainInterfaceWin32::reloadPrefs()
{
    p_intf->p_sys->disable_volume_keys = var_InheritBool( p_intf, "qt-disable-volume-keys" );
    MainInterface::reloadPrefs();
}

void MainInterfaceWin32::playlistItemAppended( int, int )
399 400 401 402
{
    changeThumbbarButtons( THEMIM->getIM()->playingStatus() );
}

403
void MainInterfaceWin32::playlistItemRemoved( int )
404 405 406 407
{
    changeThumbbarButtons( THEMIM->getIM()->playingStatus() );
}

408
void MainInterfaceWin32::changeThumbbarButtons( int i_status )
409
{
410 411 412
    if( p_taskbl == NULL )
        return;

413 414
    // Define an array of three buttons. These buttons provide images through an
    // image list and also provide tooltips.
415
    THUMBBUTTONMASK dwMask = THUMBBUTTONMASK(THB_BITMAP | THB_FLAGS);
416 417 418 419 420 421

    THUMBBUTTON thbButtons[3];
    //prev
    thbButtons[0].dwMask = dwMask;
    thbButtons[0].iId = 0;
    thbButtons[0].iBitmap = 0;
422
    thbButtons[0].dwFlags = THEPL->items.i_size > 1 ? THBF_ENABLED : THBF_HIDDEN;
423 424 425 426

    //play/pause
    thbButtons[1].dwMask = dwMask;
    thbButtons[1].iId = 1;
427
    thbButtons[1].dwFlags = THBF_ENABLED;
428 429 430 431 432

    //next
    thbButtons[2].dwMask = dwMask;
    thbButtons[2].iId = 2;
    thbButtons[2].iBitmap = 3;
433
    thbButtons[2].dwFlags = THEPL->items.i_size > 1 ? THBF_ENABLED : THBF_HIDDEN;
434 435 436

    switch( i_status )
    {
437
        case OPENING_S:
438 439 440 441 442
        case PLAYING_S:
            {
                thbButtons[1].iBitmap = 1;
                break;
            }
443
        case END_S:
444
        case PAUSE_S:
445
        case ERROR_S:
446 447 448 449 450 451 452
            {
                thbButtons[1].iBitmap = 2;
                break;
            }
        default:
            return;
    }
453

454
    HRESULT hr =  p_taskbl->ThumbBarUpdateButtons(WinId(this), 3, thbButtons);
455

456
    if(S_OK != hr)
457
        msg_Err( p_intf, "ThumbBarUpdateButtons failed with error %08lx", hr );
458

459 460
    // If a video is playing, let the vout handle the thumbnail.
    if( !videoWidget || !THEMIM->getIM()->hasVideo() )
461 462
    {
        hr = p_taskbl->SetThumbnailClip(WinId(this), NULL);
463 464 465
        if(S_OK != hr)
            msg_Err( p_intf, "SetThumbnailClip failed with error %08lx", hr );
    }
466
}