blob: c66c19a0f4567c0ae64b529a50cbd891f43689d4 [file] [log] [blame]
Pierre Ossmand50b3d12011-04-15 07:46:56 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 * Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
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
20#ifndef __VIEWPORT_H__
21#define __VIEWPORT_H__
22
23#include <map>
24
25#include <FL/Fl.H>
26#include <FL/Fl_Widget.H>
Pierre Ossmanc7bfaac2011-04-29 11:08:11 +000027#include <FL/Fl_Menu_Button.H>
Pierre Ossman835b4ef2011-06-08 17:02:36 +000028#include <FL/Fl_RGB_Image.H>
Pierre Ossmand50b3d12011-04-15 07:46:56 +000029
30#include <rfb/Rect.h>
31#include <rfb/Region.h>
32#include <rfb/Timer.h>
33#include <rfb/PixelBuffer.h>
34#include <rfb/PixelTransformer.h>
35
Pierre Ossman132b3d02011-06-13 11:19:32 +000036#if defined(WIN32)
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000037#include "Win32PixelBuffer.h"
Pierre Ossman132b3d02011-06-13 11:19:32 +000038#elif defined(__APPLE__)
Pierre Ossmanc18753c2011-06-17 07:35:56 +000039#include "OSXPixelBuffer.h"
Pierre Ossman132b3d02011-06-13 11:19:32 +000040#else
Pierre Ossman13500692011-06-13 11:23:08 +000041#include "X11PixelBuffer.h"
Pierre Ossman132b3d02011-06-13 11:19:32 +000042#endif
43
Pierre Ossmanc18753c2011-06-17 07:35:56 +000044// We also have a generic version of the above, using pure FLTK:
45//
46// #include "PlatformPixelBuffer.h"
47//
48
Pierre Ossmand50b3d12011-04-15 07:46:56 +000049class CConn;
50
51class Viewport : public Fl_Widget {
52public:
53
54 Viewport(int w, int h, const rfb::PixelFormat& serverPF, CConn* cc_);
55 ~Viewport();
56
57 // PixelFormat of incoming write operations
58 void setServerPF(const rfb::PixelFormat& pf);
59 // Most efficient format (from Viewport's point of view)
60 const rfb::PixelFormat &getPreferredPF();
61
62 // Flush updates to screen
63 void updateWindow();
64
65 // Methods forwarded from CConn
66 void setName(const char *name);
67
68 void setColourMapEntries(int firstColour, int nColours, rdr::U16* rgbs);
69
70 void fillRect(const rfb::Rect& r, rfb::Pixel pix) {
71 if (pixelTrans) {
72 rfb::Pixel pix2;
Pierre Ossmanf3505452011-07-15 08:11:55 +000073 if (colourMapChange)
74 commitColourMap();
Pierre Ossmand50b3d12011-04-15 07:46:56 +000075 pixelTrans->translatePixels(&pix, &pix2, 1);
76 pix = pix2;
77 }
78
79 frameBuffer->fillRect(r, pix);
80 damageRect(r);
81 }
82 void imageRect(const rfb::Rect& r, void* pixels) {
Pierre Ossmanf3505452011-07-15 08:11:55 +000083 if (pixelTrans) {
84 if (colourMapChange)
85 commitColourMap();
Pierre Ossmand50b3d12011-04-15 07:46:56 +000086 pixelTrans->translateRect(pixels, r.width(),
87 rfb::Rect(0, 0, r.width(), r.height()),
88 frameBuffer->data, frameBuffer->getStride(),
89 r.tl);
Pierre Ossmanf3505452011-07-15 08:11:55 +000090 } else {
Pierre Ossmand50b3d12011-04-15 07:46:56 +000091 frameBuffer->imageRect(r, pixels);
Pierre Ossmanf3505452011-07-15 08:11:55 +000092 }
Pierre Ossmand50b3d12011-04-15 07:46:56 +000093 damageRect(r);
94 }
95 void copyRect(const rfb::Rect& r, int srcX, int srcY) {
96 frameBuffer->copyRect(r, rfb::Point(r.tl.x-srcX, r.tl.y-srcY));
97 damageRect(r);
98 }
99
DRC33c15e32011-11-03 18:49:21 +0000100 rdr::U8* getPixelsRW(const rfb::Rect& r, int* stride) {
101 return frameBuffer->getPixelsRW(r, stride);
102 }
103
104 void damageRect(const rfb::Rect& r) {
105 damage.assign_union(rfb::Region(r));
106 if (!Fl::has_timeout(handleUpdateTimeout, this))
107 Fl::add_timeout(0.500, handleUpdateTimeout, this);
108 };
109
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000110 void setCursor(int width, int height, const rfb::Point& hotspot,
111 void* data, void* mask);
112
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000113 // Fl_Widget callback methods
Pierre Ossmane00f0aa2011-06-01 09:31:53 +0000114
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000115 void draw();
Pierre Ossmane00f0aa2011-06-01 09:31:53 +0000116
117 void resize(int x, int y, int w, int h);
118
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000119 int handle(int event);
120
121private:
122
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000123 static void handleUpdateTimeout(void *data);
Pierre Ossmanf3505452011-07-15 08:11:55 +0000124
125 void commitColourMap();
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000126
Pierre Ossman4a6be4a2011-05-19 14:49:18 +0000127 static void handleClipboardChange(int source, void *data);
128
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000129 void handlePointerEvent(const rfb::Point& pos, int buttonMask);
130 static void handlePointerTimeout(void *data);
131
Pierre Ossmancd6ddef2011-05-20 12:05:20 +0000132 rdr::U32 translateKeyEvent(int keyCode, int origKeyCode, const char *keyText);
133 void handleKeyEvent(int keyCode, int origKeyCode, const char *keyText, bool down);
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000134
Pierre Ossmanc7bfaac2011-04-29 11:08:11 +0000135 void initContextMenu();
136 void popupContextMenu();
137
Pierre Ossman4e7271e2011-05-24 12:47:12 +0000138 void setMenuKey();
139
140 static void handleOptions(void *data);
141
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000142private:
143 CConn* cc;
144
Pierre Ossman132b3d02011-06-13 11:19:32 +0000145 PlatformPixelBuffer* frameBuffer;
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000146
147 rfb::PixelTransformer *pixelTrans;
148 rfb::SimpleColourMap colourMap;
Pierre Ossmanf3505452011-07-15 08:11:55 +0000149 bool colourMapChange;
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000150
151 rfb::Region damage;
152
153 rfb::Point lastPointerPos;
154 int lastButtonMask;
155
156 typedef std::map<int, rdr::U32> DownMap;
157 DownMap downKeySym;
Pierre Ossmanc7bfaac2011-04-29 11:08:11 +0000158
Pierre Ossman4e7271e2011-05-24 12:47:12 +0000159 int menuKeyCode;
Pierre Ossmanc7bfaac2011-04-29 11:08:11 +0000160 Fl_Menu_Button *contextMenu;
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000161
Henrik Anderssonabd70ce2011-09-14 06:31:06 +0000162 bool menuCtrlKey;
163 bool menuAltKey;
164
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000165 Fl_RGB_Image *cursor;
166 rfb::Point cursorHotspot;
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000167};
168
169#endif