vlcwindowless_win.cpp 4.36 KB
Newer Older
1 2 3
/*****************************************************************************
 * vlcwindowless_win.cpp: a VLC plugin for Mozilla (Windows windowless)
 *****************************************************************************
4
 * Copyright (C) 2011-2013 VLC authors and VideoLAN
5 6
 * $Id$
 *
7
 * Authors: Cheng Sun <chengsun9@gmail.com>
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 *
 * 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 "vlcwindowless_win.h"
#include <cstdio>

VlcWindowlessWin::VlcWindowlessWin(NPP instance, NPuint16_t mode) :
    VlcWindowlessBase(instance, mode)
{
    unsigned r = 0, g = 0, b = 0;
    HTMLColor2RGB(get_options().get_bg_color().c_str(), &r, &g, &b);
    m_hBgBrush = CreateSolidBrush( RGB(r,g,b) );
}

VlcWindowlessWin::~VlcWindowlessWin()
{
    DeleteObject(m_hBgBrush);
}

bool VlcWindowlessWin::handle_event(void *event)
{
    NPEvent *npevent = reinterpret_cast<NPEvent *> (event);
    switch (npevent->event) {
    case WM_PAINT:
        BOOL ret;
46
        HDC hDC = reinterpret_cast<HDC> (npevent->wParam);
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
        if (!hDC) {
            fprintf(stderr, "NULL HDC given by browser\n");
            return false;
        }
        int savedID = SaveDC(hDC);

        RECT *clipRect = reinterpret_cast<RECT *> (npevent->lParam);

        ret = FillRect(hDC, clipRect, m_hBgBrush);
        if (!ret) {
            LPVOID lpMsgBuf;
            FormatMessage(
                FORMAT_MESSAGE_ALLOCATE_BUFFER |
                FORMAT_MESSAGE_FROM_SYSTEM |
                FORMAT_MESSAGE_IGNORE_INSERTS,
                NULL,
                GetLastError(),
                0, // Default language
                (LPTSTR) &lpMsgBuf,
                0,
                NULL
            );

            fprintf(stderr, "Error in FillRect: %s\n", lpMsgBuf);
            LocalFree( lpMsgBuf );
        }

        if ( m_frame_buf.size() &&
             m_frame_buf.size() >= m_media_width * m_media_height * DEF_PIXEL_BYTES)
        {
            BITMAPINFO BmpInfo; ZeroMemory(&BmpInfo, sizeof(BmpInfo));
            BITMAPINFOHEADER& BmpH = BmpInfo.bmiHeader;
            BmpH.biSize = sizeof(BITMAPINFOHEADER);
            BmpH.biWidth = m_media_width;
            BmpH.biHeight = -((int)m_media_height);
            BmpH.biPlanes = 1;
            BmpH.biBitCount = DEF_PIXEL_BYTES*8;
            BmpH.biCompression = BI_RGB;
            //following members are already zeroed
            //BmpH.biSizeImage = 0;
            //BmpH.biXPelsPerMeter = 0;
            //BmpH.biYPelsPerMeter = 0;
            //BmpH.biClrUsed = 0;
            //BmpH.biClrImportant = 0;


            ret = SetDIBitsToDevice(hDC,
                            npwindow.x + (npwindow.width - m_media_width)/2,
                            npwindow.y + (npwindow.height - m_media_height)/2,
                            m_media_width, m_media_height,
                            0, 0,
                            0, m_media_height,
                            &m_frame_buf[0],
                            &BmpInfo, DIB_RGB_COLORS);
            if (!ret) {
                LPVOID lpMsgBuf;
                FormatMessage(
                    FORMAT_MESSAGE_ALLOCATE_BUFFER |
                    FORMAT_MESSAGE_FROM_SYSTEM |
                    FORMAT_MESSAGE_IGNORE_INSERTS,
                    NULL,
                    GetLastError(),
                    0, // Default language
                    (LPTSTR) &lpMsgBuf,
                    0,
                    NULL
                );

                fprintf(stderr, "Error in SetDIBitsToDevice: %s\n", lpMsgBuf);
                LocalFree( lpMsgBuf );
            }

        }
        RestoreDC(hDC, savedID);
        return true;
    }
    return VlcWindowlessBase::handle_event(event);
}