1 /* 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 __DESKTOPWINDOW_H__
21 #define __DESKTOPWINDOW_H__
22 
23 #include <map>
24 
25 #include <sys/time.h>
26 
27 #include <rfb/Rect.h>
28 #include <rfb/Pixel.h>
29 
30 #include <FL/Fl_Window.H>
31 
32 namespace rfb { class ModifiablePixelBuffer; }
33 
34 class CConn;
35 class Surface;
36 class Viewport;
37 
38 class Fl_Scrollbar;
39 
40 #ifdef __GNUC__
41 #  define __printf_attr(a, b) __attribute__((__format__ (__printf__, a, b)))
42 #else
43 #  define __printf_attr(a, b)
44 #endif // __GNUC__
45 
46 class DesktopWindow : public Fl_Window {
47 public:
48 
49   DesktopWindow(int w, int h, const char *name,
50                 const rfb::PixelFormat& serverPF, CConn* cc_);
51   ~DesktopWindow();
52 
53   // Most efficient format (from DesktopWindow's point of view)
54   const rfb::PixelFormat &getPreferredPF();
55 
56   // Flush updates to screen
57   void updateWindow();
58 
59   // Updated session title
60   void setName(const char *name);
61 
62   // Resize the current framebuffer, but retain the contents
63   void resizeFramebuffer(int new_w, int new_h);
64 
65   // New image for the locally rendered cursor
66   void setCursor(int width, int height, const rfb::Point& hotspot,
67                  const rdr::U8* data);
68 
69   // Server-provided cursor position
70   void setCursorPos(const rfb::Point& pos);
71 
72   // Change client LED state
73   void setLEDState(unsigned int state);
74 
75   // Clipboard events
76   void handleClipboardRequest();
77   void handleClipboardAnnounce(bool available);
78   void handleClipboardData(const char* data);
79 
80   // Fl_Window callback methods
81   virtual void show();
82   virtual void draw();
83   virtual void resize(int x, int y, int w, int h);
84 
85   virtual int handle(int event);
86 
87   void fullscreen_on();
88 
89 private:
90   static void menuOverlay(void *data);
91 
92   void setOverlay(const char *text, ...) __printf_attr(2, 3);
93   static void updateOverlay(void *data);
94 
95   static int fltkDispatch(int event, Fl_Window *win);
96   static int fltkHandle(int event);
97 
98   bool hasFocus();
99 
100   void maybeGrabKeyboard();
101   void grabKeyboard();
102   void ungrabKeyboard();
103   void grabPointer();
104   void ungrabPointer();
105 
106   static void handleGrab(void *data);
107 
108   void maximizeWindow();
109 
110   void handleDesktopSize();
111   static void handleResizeTimeout(void *data);
112   static void reconfigureFullscreen(void *data);
113   void remoteResize(int width, int height);
114 
115   void repositionWidgets();
116 
117   static void handleClose(Fl_Widget *wnd, void *data);
118 
119   static void handleOptions(void *data);
120 
121   static void handleFullscreenTimeout(void *data);
122 
123   void scrollTo(int x, int y);
124   static void handleScroll(Fl_Widget *wnd, void *data);
125   static void handleEdgeScroll(void *data);
126 
127   static void handleStatsTimeout(void *data);
128 
129 private:
130   CConn* cc;
131   Fl_Scrollbar *hscroll, *vscroll;
132   Viewport *viewport;
133   Surface *offscreen;
134   Surface *overlay;
135   unsigned char overlayAlpha;
136   struct timeval overlayStart;
137 
138   bool firstUpdate;
139   bool delayedFullscreen;
140   bool delayedDesktopSize;
141 
142   bool keyboardGrabbed;
143   bool mouseGrabbed;
144 
145   struct statsEntry {
146     unsigned ups;
147     unsigned pps;
148     unsigned bps;
149   };
150   struct statsEntry stats[100];
151 
152   struct timeval statsLastTime;
153   unsigned statsLastUpdates;
154   unsigned statsLastPixels;
155   unsigned statsLastPosition;
156 
157   Surface *statsGraph;
158 };
159 
160 #endif
161