main_interface_win32.cpp 11.7 KB
Newer Older
1
/*****************************************************************************
Pere Orga's avatar
Pere Orga committed
2
 * main_interface_win32.cpp : Main interface
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 ****************************************************************************
 * Copyright (C) 2006-2010 VideoLAN and AUTHORS
 * $Id$
 *
 * Authors: Jean-Baptiste Kempf <jb@videolan.org>
 *
 * 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.
 *****************************************************************************/


#include "main_interface.hpp"

#include "input_manager.hpp"
#include "actions_manager.hpp"
29
#include "dialogs_provider.hpp"
30
#include "components/interface_widgets.hpp"
31

32
#include <QBitmap>
33

34 35
#include <assert.h>

36 37 38
#if defined(_WIN32) && HAS_QT5
# include <QWindow>
# include <qpa/qplatformnativeinterface.h>
39 40 41
#endif


42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#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
60 61
#define APPCOMMAND_HELP                   27
#define APPCOMMAND_OPEN                   30
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
#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)

83
HWND MainInterface::WinId( QWidget *w )
84
{
85
#if HAS_QT5
86 87 88 89 90
    if( w && w->windowHandle() )
        return static_cast<HWND>(QGuiApplication::platformNativeInterface()->
            nativeResourceForWindow("handle", w->windowHandle()));
    else
        return 0;
91 92 93 94 95
#else
    return winId();
#endif
}

96
#if defined(_WIN32) && !HAS_QT5
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
static const int PremultipliedAlpha = QPixmap::PremultipliedAlpha;
static HBITMAP qt_pixmapToWinHBITMAP(const QPixmap &p, int hbitmapFormat = 0)
{
    return p.toWinHBITMAP((enum QBitmap::HBitmapFormat)hbitmapFormat);
}
#else
Q_GUI_EXPORT HBITMAP qt_pixmapToWinHBITMAP(const QPixmap &p, int hbitmapFormat = 0);
enum HBitmapFormat
{
    NoAlpha,
    PremultipliedAlpha,
    Alpha
};
#endif

112 113 114 115 116 117
void MainInterface::createTaskBarButtons()
{
    /*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
    FIXME:the play button's picture doesn't changed to pause when clicked
    */
118 119
    p_taskbl = NULL;
    himl = NULL;
120

121
    HRESULT hr = CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );
122 123
    if( FAILED(hr) )
        return;
124

125 126 127 128
    void *pv;
    hr = CoCreateInstance( CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER,
                           IID_ITaskbarList3, &pv);
    if( FAILED(hr) )
129
    {
130 131
        CoUninitialize();
        return;
132
    }
133 134 135 136 137 138 139

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

    himl = ImageList_Create( 16 /*cx*/, 16 /*cy*/, ILC_COLOR32 /*flags*/,
                             4 /*cInitial*/, 0 /*cGrow*/);
    if( himl == NULL )
140
    {
141
        p_taskbl->Release();
142
        p_taskbl = NULL;
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
        CoUninitialize();
        return;
    }

    QPixmap img   = QPixmap(":/win7/prev");
    QPixmap img2  = QPixmap(":/win7/pause");
    QPixmap img3  = QPixmap(":/win7/play");
    QPixmap img4  = QPixmap(":/win7/next");
    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;
    thbButtons[0].dwFlags = THBF_HIDDEN;

    thbButtons[1].dwMask = dwMask;
    thbButtons[1].iId = 1;
    thbButtons[1].iBitmap = 2;
    thbButtons[1].dwFlags = THBF_HIDDEN;

    thbButtons[2].dwMask = dwMask;
    thbButtons[2].iId = 2;
    thbButtons[2].iBitmap = 3;
    thbButtons[2].dwFlags = THBF_HIDDEN;

    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 );
195
    }
196 197
    CONNECT( THEMIM->getIM(), playingStatusChanged( int ),
             this, changeThumbbarButtons( int ) );
198 199 200 201 202 203
}

bool MainInterface::winEvent ( MSG * msg, long * result )
{
    if (msg->message == taskbar_wmsg)
    {
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
204
        //We received the taskbarbuttoncreated, now we can really create the buttons
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
        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);
230

231
            if( p_intf->p_sys->disable_volume_keys &&
232 233 234 235 236 237 238
                    (   cmd == APPCOMMAND_VOLUME_DOWN   ||
                        cmd == APPCOMMAND_VOLUME_UP     ||
                        cmd == APPCOMMAND_VOLUME_MUTE ) )
            {
                break;
            }

239
            *result = TRUE;
240

241 242 243 244 245 246 247 248 249 250 251
            switch(cmd)
            {
                case APPCOMMAND_MEDIA_PLAY_PAUSE:
                    THEMIM->togglePlayPause();
                    break;
                case APPCOMMAND_MEDIA_PLAY:
                    THEMIM->play();
                    break;
                case APPCOMMAND_MEDIA_PAUSE:
                    THEMIM->pause();
                    break;
252
                case APPCOMMAND_MEDIA_CHANNEL_DOWN:
253 254 255
                case APPCOMMAND_MEDIA_PREVIOUSTRACK:
                    THEMIM->prev();
                    break;
256
                case APPCOMMAND_MEDIA_CHANNEL_UP:
257 258 259 260 261 262
                case APPCOMMAND_MEDIA_NEXTTRACK:
                    THEMIM->next();
                    break;
                case APPCOMMAND_MEDIA_STOP:
                    THEMIM->stop();
                    break;
263 264 265
                case APPCOMMAND_MEDIA_RECORD:
                    THEAM->record();
                    break;
266 267 268 269 270 271 272 273 274
                case APPCOMMAND_VOLUME_DOWN:
                    THEAM->AudioDown();
                    break;
                case APPCOMMAND_VOLUME_UP:
                    THEAM->AudioUp();
                    break;
                case APPCOMMAND_VOLUME_MUTE:
                    THEAM->toggleMuteAudio();
                    break;
275 276 277 278 279 280 281 282 283 284 285 286
                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;
287 288
                default:
                     msg_Dbg( p_intf, "unknown APPCOMMAND = %d", cmd);
289
                     *result = FALSE;
290 291
                     break;
            }
292
            if (*result) return true;
293 294 295 296 297
            break;
    }
    return false;
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
298
void MainInterface::changeThumbbarButtons( int i_status )
299
{
300 301 302
    if( p_taskbl == NULL )
        return;

303 304
    // Define an array of three buttons. These buttons provide images through an
    // image list and also provide tooltips.
305
    THUMBBUTTONMASK dwMask = THUMBBUTTONMASK(THB_BITMAP | THB_FLAGS);
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

    THUMBBUTTON thbButtons[3];
    //prev
    thbButtons[0].dwMask = dwMask;
    thbButtons[0].iId = 0;
    thbButtons[0].iBitmap = 0;

    //play/pause
    thbButtons[1].dwMask = dwMask;
    thbButtons[1].iId = 1;

    //next
    thbButtons[2].dwMask = dwMask;
    thbButtons[2].iId = 2;
    thbButtons[2].iBitmap = 3;

    switch( i_status )
    {
324
        case OPENING_S:
325 326 327 328 329 330 331 332
        case PLAYING_S:
            {
                thbButtons[0].dwFlags = THBF_ENABLED;
                thbButtons[1].dwFlags = THBF_ENABLED;
                thbButtons[2].dwFlags = THBF_ENABLED;
                thbButtons[1].iBitmap = 1;
                break;
            }
333
        case END_S:
334
        case PAUSE_S:
335
        case ERROR_S:
336 337 338 339 340 341 342 343 344 345
            {
                thbButtons[0].dwFlags = THBF_ENABLED;
                thbButtons[1].dwFlags = THBF_ENABLED;
                thbButtons[2].dwFlags = THBF_ENABLED;
                thbButtons[1].iBitmap = 2;
                break;
            }
        default:
            return;
    }
346 347 348 349 350 351 352

    HRESULT hr;
    if( videoWidget && THEMIM->getIM()->hasVideo() )
        hr =  p_taskbl->ThumbBarUpdateButtons(WinId(videoWidget), 3, thbButtons);
    else
        hr =  p_taskbl->ThumbBarUpdateButtons(WinId(this), 3, thbButtons);

353
    if(S_OK != hr)
354
        msg_Err( p_intf, "ThumbBarUpdateButtons failed with error %08lx", hr );
355
}