Pierre Ossman | b4cb876 | 2011-06-13 13:24:29 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Pierre Ossman | ac13abe | 2014-02-07 14:46:26 +0100 | [diff] [blame^] | 2 | * Copyright 2011-2014 Pierre Ossman for Cendio AB |
Pierre Ossman | b4cb876 | 2011-06-13 13:24:29 +0000 | [diff] [blame] | 3 | * |
| 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 Åstrand | c359f36 | 2011-08-23 12:04:46 +0000 | [diff] [blame] | 20 | #ifdef HAVE_CONFIG_H |
| 21 | #include <config.h> |
| 22 | #endif |
| 23 | |
Pierre Ossman | b4cb876 | 2011-06-13 13:24:29 +0000 | [diff] [blame] | 24 | #include <assert.h> |
| 25 | #include <stdlib.h> |
| 26 | |
| 27 | #include <windows.h> |
| 28 | |
| 29 | #include <FL/x.H> |
| 30 | |
| 31 | #include <rfb/LogWriter.h> |
| 32 | #include <rfb/Exception.h> |
| 33 | |
| 34 | #include "Win32PixelBuffer.h" |
| 35 | |
| 36 | using namespace rfb; |
| 37 | |
Pierre Ossman | ac13abe | 2014-02-07 14:46:26 +0100 | [diff] [blame^] | 38 | static rfb::LogWriter vlog("Win32PixelBuffer"); |
Pierre Ossman | b4cb876 | 2011-06-13 13:24:29 +0000 | [diff] [blame] | 39 | |
Pierre Ossman | ac13abe | 2014-02-07 14:46:26 +0100 | [diff] [blame^] | 40 | Win32PixelBuffer::Win32PixelBuffer(int width, int height) : |
| 41 | PlatformPixelBuffer(rfb::PixelFormat(32, 24, false, true, |
| 42 | 255, 255, 255, 16, 8, 0), |
| 43 | width, height, NULL), |
Pierre Ossman | b4cb876 | 2011-06-13 13:24:29 +0000 | [diff] [blame] | 44 | 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(); |
| 62 | throw rdr::SystemException("unable to create DIB section", err); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | |
Pierre Ossman | ac13abe | 2014-02-07 14:46:26 +0100 | [diff] [blame^] | 67 | Win32PixelBuffer::~Win32PixelBuffer() |
Pierre Ossman | b4cb876 | 2011-06-13 13:24:29 +0000 | [diff] [blame] | 68 | { |
| 69 | DeleteObject(bitmap); |
| 70 | } |
| 71 | |
| 72 | |
Pierre Ossman | ac13abe | 2014-02-07 14:46:26 +0100 | [diff] [blame^] | 73 | void Win32PixelBuffer::draw(int src_x, int src_y, int x, int y, int w, int h) |
Pierre Ossman | b4cb876 | 2011-06-13 13:24:29 +0000 | [diff] [blame] | 74 | { |
| 75 | HDC dc; |
| 76 | |
| 77 | dc = CreateCompatibleDC(fl_gc); |
| 78 | if (!dc) |
| 79 | throw rdr::SystemException("CreateCompatibleDC failed", GetLastError()); |
| 80 | |
| 81 | if (!SelectObject(dc, bitmap)) |
| 82 | throw rdr::SystemException("SelectObject failed", GetLastError()); |
| 83 | |
Pierre Ossman | 4333220 | 2011-07-15 14:20:30 +0000 | [diff] [blame] | 84 | 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) |
| 91 | throw rdr::SystemException("BitBlt failed", GetLastError()); |
| 92 | } |
Pierre Ossman | b4cb876 | 2011-06-13 13:24:29 +0000 | [diff] [blame] | 93 | |
| 94 | DeleteDC(dc); |
| 95 | } |