blob: 144ad420b2326fdb38847a3758f9b941091d28af [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
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000100 void setCursor(int width, int height, const rfb::Point& hotspot,
101 void* data, void* mask);
102
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000103 // Fl_Widget callback methods
Pierre Ossmane00f0aa2011-06-01 09:31:53 +0000104
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000105 void draw();
Pierre Ossmane00f0aa2011-06-01 09:31:53 +0000106
107 void resize(int x, int y, int w, int h);
108
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000109 int handle(int event);
110
111private:
112
113 void damageRect(const rfb::Rect& r) {
114 damage.assign_union(rfb::Region(r));
115 if (!Fl::has_timeout(handleUpdateTimeout, this))
Pierre Ossmandb7091c2011-06-13 15:08:35 +0000116 Fl::add_timeout(0.500, handleUpdateTimeout, this);
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000117 };
118
119 static void handleUpdateTimeout(void *data);
Pierre Ossmanf3505452011-07-15 08:11:55 +0000120
121 void commitColourMap();
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000122
Pierre Ossman4a6be4a2011-05-19 14:49:18 +0000123 static void handleClipboardChange(int source, void *data);
124
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000125 void handlePointerEvent(const rfb::Point& pos, int buttonMask);
126 static void handlePointerTimeout(void *data);
127
Pierre Ossmancd6ddef2011-05-20 12:05:20 +0000128 rdr::U32 translateKeyEvent(int keyCode, int origKeyCode, const char *keyText);
129 void handleKeyEvent(int keyCode, int origKeyCode, const char *keyText, bool down);
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000130
Pierre Ossmanc7bfaac2011-04-29 11:08:11 +0000131 void initContextMenu();
132 void popupContextMenu();
133
Pierre Ossman4e7271e2011-05-24 12:47:12 +0000134 void setMenuKey();
135
136 static void handleOptions(void *data);
137
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000138private:
139 CConn* cc;
140
Pierre Ossman132b3d02011-06-13 11:19:32 +0000141 PlatformPixelBuffer* frameBuffer;
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000142
143 rfb::PixelTransformer *pixelTrans;
144 rfb::SimpleColourMap colourMap;
Pierre Ossmanf3505452011-07-15 08:11:55 +0000145 bool colourMapChange;
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000146
147 rfb::Region damage;
148
149 rfb::Point lastPointerPos;
150 int lastButtonMask;
151
152 typedef std::map<int, rdr::U32> DownMap;
153 DownMap downKeySym;
Pierre Ossmanc7bfaac2011-04-29 11:08:11 +0000154
Pierre Ossman4e7271e2011-05-24 12:47:12 +0000155 int menuKeyCode;
Pierre Ossmanc7bfaac2011-04-29 11:08:11 +0000156 Fl_Menu_Button *contextMenu;
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000157
Henrik Anderssonabd70ce2011-09-14 06:31:06 +0000158 bool menuCtrlKey;
159 bool menuAltKey;
160
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000161 Fl_RGB_Image *cursor;
162 rfb::Point cursorHotspot;
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000163};
164
165#endif