blob: 3f8253041a82cdfac17b6a8184f6ee3abc4271d8 [file] [log] [blame]
Pierre Ossmanb4cb8762011-06-13 13:24:29 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossmanac13abe2014-02-07 14:46:26 +01002 * Copyright 2011-2014 Pierre Ossman for Cendio AB
Pierre Ossmanb4cb8762011-06-13 13:24:29 +00003 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this software; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19
Peter Åstrandc359f362011-08-23 12:04:46 +000020#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000024#include <stdlib.h>
25
26#include <windows.h>
27
28#include <FL/x.H>
29
30#include <rfb/LogWriter.h>
31#include <rfb/Exception.h>
32
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +020033#include "i18n.h"
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000034#include "Win32PixelBuffer.h"
35
36using namespace rfb;
37
Pierre Ossmanac13abe2014-02-07 14:46:26 +010038static rfb::LogWriter vlog("Win32PixelBuffer");
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000039
Pierre Ossmanac13abe2014-02-07 14:46:26 +010040Win32PixelBuffer::Win32PixelBuffer(int width, int height) :
41 PlatformPixelBuffer(rfb::PixelFormat(32, 24, false, true,
42 255, 255, 255, 16, 8, 0),
Pierre Ossman2e5a1062014-01-30 17:57:27 +010043 width, height, NULL, width),
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000044 bitmap(NULL)
45{
46 BITMAPINFOHEADER bih;
47
48 memset(&bih, 0, sizeof(bih));
49
50 bih.biSize = sizeof(BITMAPINFOHEADER);
51 bih.biBitCount = getPF().bpp;
52 bih.biSizeImage = (getPF().bpp / 8) * width * height;
53 bih.biPlanes = 1;
54 bih.biWidth = width;
55 bih.biHeight = -height; // Negative to get top-down
56 bih.biCompression = BI_RGB;
57
58 bitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bih,
59 DIB_RGB_COLORS, (void**)&data, NULL, 0);
60 if (!bitmap) {
61 int err = GetLastError();
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +020062 throw rdr::SystemException(_("unable to create DIB section"), err);
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000063 }
64}
65
66
Pierre Ossmanac13abe2014-02-07 14:46:26 +010067Win32PixelBuffer::~Win32PixelBuffer()
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000068{
69 DeleteObject(bitmap);
70}
71
72
Pierre Ossmanac13abe2014-02-07 14:46:26 +010073void Win32PixelBuffer::draw(int src_x, int src_y, int x, int y, int w, int h)
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000074{
75 HDC dc;
76
77 dc = CreateCompatibleDC(fl_gc);
78 if (!dc)
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +020079 throw rdr::SystemException(_("CreateCompatibleDC failed"), GetLastError());
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000080
81 if (!SelectObject(dc, bitmap))
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +020082 throw rdr::SystemException(_("SelectObject failed"), GetLastError());
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000083
Pierre Ossman43332202011-07-15 14:20:30 +000084 if (!BitBlt(fl_gc, x, y, w, h, dc, src_x, src_y, SRCCOPY)) {
85 // If the desktop we're rendering to is inactive (like when the screen
86 // is locked or the UAC is active), then GDI calls will randomly fail.
87 // This is completely undocumented so we have no idea how best to deal
88 // with it. For now, we've only seen this error and for this function
89 // so only ignore this combination.
90 if (GetLastError() != ERROR_INVALID_HANDLE)
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +020091 throw rdr::SystemException(_("BitBlt failed"), GetLastError());
Pierre Ossman43332202011-07-15 14:20:30 +000092 }
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000093
94 DeleteDC(dc);
95}