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 | |
Pierre Ossman | 8ca4c1d | 2014-09-22 12:54:26 +0200 | [diff] [blame^] | 34 | #include "i18n.h" |
Pierre Ossman | b4cb876 | 2011-06-13 13:24:29 +0000 | [diff] [blame] | 35 | #include "Win32PixelBuffer.h" |
| 36 | |
| 37 | using namespace rfb; |
| 38 | |
Pierre Ossman | ac13abe | 2014-02-07 14:46:26 +0100 | [diff] [blame] | 39 | static rfb::LogWriter vlog("Win32PixelBuffer"); |
Pierre Ossman | b4cb876 | 2011-06-13 13:24:29 +0000 | [diff] [blame] | 40 | |
Pierre Ossman | ac13abe | 2014-02-07 14:46:26 +0100 | [diff] [blame] | 41 | Win32PixelBuffer::Win32PixelBuffer(int width, int height) : |
| 42 | PlatformPixelBuffer(rfb::PixelFormat(32, 24, false, true, |
| 43 | 255, 255, 255, 16, 8, 0), |
Pierre Ossman | 2e5a106 | 2014-01-30 17:57:27 +0100 | [diff] [blame] | 44 | width, height, NULL, width), |
Pierre Ossman | b4cb876 | 2011-06-13 13:24:29 +0000 | [diff] [blame] | 45 | bitmap(NULL) |
| 46 | { |
| 47 | BITMAPINFOHEADER bih; |
| 48 | |
| 49 | memset(&bih, 0, sizeof(bih)); |
| 50 | |
| 51 | bih.biSize = sizeof(BITMAPINFOHEADER); |
| 52 | bih.biBitCount = getPF().bpp; |
| 53 | bih.biSizeImage = (getPF().bpp / 8) * width * height; |
| 54 | bih.biPlanes = 1; |
| 55 | bih.biWidth = width; |
| 56 | bih.biHeight = -height; // Negative to get top-down |
| 57 | bih.biCompression = BI_RGB; |
| 58 | |
| 59 | bitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bih, |
| 60 | DIB_RGB_COLORS, (void**)&data, NULL, 0); |
| 61 | if (!bitmap) { |
| 62 | int err = GetLastError(); |
Pierre Ossman | 8ca4c1d | 2014-09-22 12:54:26 +0200 | [diff] [blame^] | 63 | throw rdr::SystemException(_("unable to create DIB section"), err); |
Pierre Ossman | b4cb876 | 2011-06-13 13:24:29 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | |
Pierre Ossman | ac13abe | 2014-02-07 14:46:26 +0100 | [diff] [blame] | 68 | Win32PixelBuffer::~Win32PixelBuffer() |
Pierre Ossman | b4cb876 | 2011-06-13 13:24:29 +0000 | [diff] [blame] | 69 | { |
| 70 | DeleteObject(bitmap); |
| 71 | } |
| 72 | |
| 73 | |
Pierre Ossman | ac13abe | 2014-02-07 14:46:26 +0100 | [diff] [blame] | 74 | 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] | 75 | { |
| 76 | HDC dc; |
| 77 | |
| 78 | dc = CreateCompatibleDC(fl_gc); |
| 79 | if (!dc) |
Pierre Ossman | 8ca4c1d | 2014-09-22 12:54:26 +0200 | [diff] [blame^] | 80 | throw rdr::SystemException(_("CreateCompatibleDC failed"), GetLastError()); |
Pierre Ossman | b4cb876 | 2011-06-13 13:24:29 +0000 | [diff] [blame] | 81 | |
| 82 | if (!SelectObject(dc, bitmap)) |
Pierre Ossman | 8ca4c1d | 2014-09-22 12:54:26 +0200 | [diff] [blame^] | 83 | throw rdr::SystemException(_("SelectObject failed"), GetLastError()); |
Pierre Ossman | b4cb876 | 2011-06-13 13:24:29 +0000 | [diff] [blame] | 84 | |
Pierre Ossman | 4333220 | 2011-07-15 14:20:30 +0000 | [diff] [blame] | 85 | if (!BitBlt(fl_gc, x, y, w, h, dc, src_x, src_y, SRCCOPY)) { |
| 86 | // If the desktop we're rendering to is inactive (like when the screen |
| 87 | // is locked or the UAC is active), then GDI calls will randomly fail. |
| 88 | // This is completely undocumented so we have no idea how best to deal |
| 89 | // with it. For now, we've only seen this error and for this function |
| 90 | // so only ignore this combination. |
| 91 | if (GetLastError() != ERROR_INVALID_HANDLE) |
Pierre Ossman | 8ca4c1d | 2014-09-22 12:54:26 +0200 | [diff] [blame^] | 92 | throw rdr::SystemException(_("BitBlt failed"), GetLastError()); |
Pierre Ossman | 4333220 | 2011-07-15 14:20:30 +0000 | [diff] [blame] | 93 | } |
Pierre Ossman | b4cb876 | 2011-06-13 13:24:29 +0000 | [diff] [blame] | 94 | |
| 95 | DeleteDC(dc); |
| 96 | } |