blob: ce5c4d82afe3c8957b369592ec0d552b3f26b601 [file] [log] [blame]
Pierre Ossman13500692011-06-13 11:23:08 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossmanac13abe2014-02-07 14:46:26 +01002 * Copyright 2011-2014 Pierre Ossman for Cendio AB
Pierre Ossman13500692011-06-13 11:23:08 +00003 *
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 Åstrandc359f362011-08-23 12:04:46 +000020#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
Pierre Ossmanc9dd3a42015-11-18 16:24:16 +010024#include <assert.h>
Pierre Ossman13500692011-06-13 11:23:08 +000025#include <stdlib.h>
26
Pierre Ossmanc9dd3a42015-11-18 16:24:16 +010027#include <FL/Fl.H>
Pierre Ossman13500692011-06-13 11:23:08 +000028#include <FL/x.H>
29
30#include <rfb/LogWriter.h>
31#include <rfb/Exception.h>
32
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +020033#include "i18n.h"
Pierre Ossman13500692011-06-13 11:23:08 +000034#include "X11PixelBuffer.h"
35
36using namespace rfb;
37
Pierre Ossmanac13abe2014-02-07 14:46:26 +010038static rfb::LogWriter vlog("X11PixelBuffer");
Pierre Ossman13500692011-06-13 11:23:08 +000039
Pierre Ossmanc9dd3a42015-11-18 16:24:16 +010040std::list<X11PixelBuffer*> X11PixelBuffer::shmList;
41
Pierre Ossman13500692011-06-13 11:23:08 +000042static PixelFormat display_pf()
43{
44 int i;
45
46 int bpp;
47 int trueColour, bigEndian;
48 int redShift, greenShift, blueShift;
49 int redMax, greenMax, blueMax;
50
51 int nformats;
52 XPixmapFormatValues* format;
53
54 // Might not be open at this point
55 fl_open_display();
56
57 format = XListPixmapFormats(fl_display, &nformats);
58
59 for (i = 0; i < nformats; i++)
60 if (format[i].depth == fl_visual->depth) break;
61
62 if (i == nformats)
Pierre Ossman744e55c2014-12-03 14:00:54 +010063 // TRANSLATORS: "pixmap" is an X11 concept and may not be suitable
64 // to translate.
Pierre Ossman86750632014-10-10 13:33:19 +020065 throw rfb::Exception(_("Display lacks pixmap format for default depth"));
Pierre Ossman13500692011-06-13 11:23:08 +000066
67 switch (format[i].bits_per_pixel) {
68 case 8:
69 case 16:
70 case 32:
71 bpp = format[i].bits_per_pixel;
72 break;
73 default:
Pierre Ossman744e55c2014-12-03 14:00:54 +010074 // TRANSLATORS: "pixmap" is an X11 concept and may not be suitable
75 // to translate.
Pierre Ossman86750632014-10-10 13:33:19 +020076 throw rfb::Exception(_("Couldn't find suitable pixmap format"));
Pierre Ossman13500692011-06-13 11:23:08 +000077 }
78
79 XFree(format);
80
81 bigEndian = (ImageByteOrder(fl_display) == MSBFirst);
82 trueColour = (fl_visual->c_class == TrueColor);
83
84 if (!trueColour)
Pierre Ossman86750632014-10-10 13:33:19 +020085 throw rfb::Exception(_("Only true colour displays supported"));
Pierre Ossman13500692011-06-13 11:23:08 +000086
Pierre Ossman2aef35c2014-12-03 14:01:56 +010087 vlog.info(_("Using default colormap and visual, TrueColor, depth %d."),
Pierre Ossman13500692011-06-13 11:23:08 +000088 fl_visual->depth);
89
90 redShift = ffs(fl_visual->red_mask) - 1;
91 greenShift = ffs(fl_visual->green_mask) - 1;
92 blueShift = ffs(fl_visual->blue_mask) - 1;
93 redMax = fl_visual->red_mask >> redShift;
94 greenMax = fl_visual->green_mask >> greenShift;
95 blueMax = fl_visual->blue_mask >> blueShift;
96
97 return PixelFormat(bpp, fl_visual->depth, bigEndian, trueColour,
98 redMax, greenMax, blueMax,
99 redShift, greenShift, blueShift);
100}
101
Pierre Ossmanac13abe2014-02-07 14:46:26 +0100102X11PixelBuffer::X11PixelBuffer(int width, int height) :
Pierre Ossman2e5a1062014-01-30 17:57:27 +0100103 PlatformPixelBuffer(display_pf(), width, height, NULL, 0),
Pierre Ossman919ae452016-05-04 17:51:19 +0200104 shminfo(NULL), xim(NULL), pendingPutImage(0), pendingDrawable(0)
Pierre Ossman13500692011-06-13 11:23:08 +0000105{
106 // Might not be open at this point
107 fl_open_display();
108
109 if (!setupShm()) {
110 xim = XCreateImage(fl_display, fl_visual->visual, fl_visual->depth,
111 ZPixmap, 0, 0, width, height, BitmapPad(fl_display), 0);
Pierre Ossmand1a853b2014-10-10 13:37:35 +0200112 if (!xim)
113 throw rfb::Exception(_("Could not create framebuffer image"));
Pierre Ossman13500692011-06-13 11:23:08 +0000114
115 xim->data = (char*)malloc(xim->bytes_per_line * xim->height);
Pierre Ossmand1a853b2014-10-10 13:37:35 +0200116 if (!xim->data)
117 throw rfb::Exception(_("Not enough memory for framebuffer"));
Pierre Ossman13500692011-06-13 11:23:08 +0000118 }
119
120 data = (rdr::U8*)xim->data;
Pierre Ossman2e5a1062014-01-30 17:57:27 +0100121 stride = xim->bytes_per_line / (getPF().bpp/8);
Pierre Ossman13500692011-06-13 11:23:08 +0000122}
123
124
Pierre Ossmanac13abe2014-02-07 14:46:26 +0100125X11PixelBuffer::~X11PixelBuffer()
Pierre Ossman13500692011-06-13 11:23:08 +0000126{
127 if (shminfo) {
128 vlog.debug("Freeing shared memory XImage");
Pierre Ossmanc9dd3a42015-11-18 16:24:16 +0100129 shmList.remove(this);
130 Fl::remove_system_handler(handleSystemEvent);
Pierre Ossman13500692011-06-13 11:23:08 +0000131 shmdt(shminfo->shmaddr);
132 shmctl(shminfo->shmid, IPC_RMID, 0);
133 delete shminfo;
134 shminfo = NULL;
135 }
136
137 // XDestroyImage() will free(xim->data) if appropriate
138 if (xim)
139 XDestroyImage(xim);
140 xim = NULL;
141}
142
143
Pierre Ossmanac13abe2014-02-07 14:46:26 +0100144void X11PixelBuffer::draw(int src_x, int src_y, int x, int y, int w, int h)
Pierre Ossman13500692011-06-13 11:23:08 +0000145{
Pierre Ossmanc9dd3a42015-11-18 16:24:16 +0100146 if (shminfo) {
147 XShmPutImage(fl_display, fl_window, fl_gc, xim, src_x, src_y, x, y, w, h, True);
148 pendingPutImage++;
Pierre Ossman919ae452016-05-04 17:51:19 +0200149 assert((pendingPutImage == 1) || (pendingDrawable == fl_window));
150 pendingDrawable = fl_window;
Pierre Ossmanc9dd3a42015-11-18 16:24:16 +0100151 } else {
Pierre Ossman13500692011-06-13 11:23:08 +0000152 XPutImage(fl_display, fl_window, fl_gc, xim, src_x, src_y, x, y, w, h);
Pierre Ossmanc9dd3a42015-11-18 16:24:16 +0100153 }
Pierre Ossman13500692011-06-13 11:23:08 +0000154}
155
Pierre Ossmanc9dd3a42015-11-18 16:24:16 +0100156bool X11PixelBuffer::isRendering(void)
157{
158 return pendingPutImage > 0;
159}
Pierre Ossman13500692011-06-13 11:23:08 +0000160
Pierre Ossman4d01a9e2011-06-17 08:35:22 +0000161static bool caughtError;
162
163static int XShmAttachErrorHandler(Display *dpy, XErrorEvent *error)
164{
165 caughtError = true;
166 return 0;
167}
Pierre Ossman13500692011-06-13 11:23:08 +0000168
Pierre Ossmanac13abe2014-02-07 14:46:26 +0100169int X11PixelBuffer::setupShm()
Pierre Ossman13500692011-06-13 11:23:08 +0000170{
171 int major, minor;
172 Bool pixmaps;
Pierre Ossman4d01a9e2011-06-17 08:35:22 +0000173 XErrorHandler old_handler;
Tim Waughb17c9c42014-10-16 14:53:17 +0100174 const char *display_name = XDisplayName (NULL);
175
176 /* Don't use MIT-SHM on remote displays */
177 if (*display_name && *display_name != ':')
178 return 0;
Pierre Ossman13500692011-06-13 11:23:08 +0000179
180 if (!XShmQueryVersion(fl_display, &major, &minor, &pixmaps))
181 return 0;
182
183 shminfo = new XShmSegmentInfo;
184
185 xim = XShmCreateImage(fl_display, fl_visual->visual, fl_visual->depth,
186 ZPixmap, 0, shminfo, width(), height());
187 if (!xim)
188 goto free_shminfo;
189
190 shminfo->shmid = shmget(IPC_PRIVATE,
191 xim->bytes_per_line * xim->height,
192 IPC_CREAT|0777);
193 if (shminfo->shmid == -1)
194 goto free_xim;
195
196 shminfo->shmaddr = xim->data = (char*)shmat(shminfo->shmid, 0, 0);
197 if (shminfo->shmaddr == (char *)-1)
198 goto free_shm;
199
200 shminfo->readOnly = True;
Pierre Ossman4d01a9e2011-06-17 08:35:22 +0000201
202 // This is the only way we can detect that shared memory won't work
203 // (e.g. because we're accessing a remote X11 server)
204 caughtError = false;
205 old_handler = XSetErrorHandler(XShmAttachErrorHandler);
206
Tim Waugh7c56b4c2014-10-17 10:28:55 +0100207 if (!XShmAttach(fl_display, shminfo)) {
208 XSetErrorHandler(old_handler);
209 goto free_shmaddr;
210 }
211
Pierre Ossman4d01a9e2011-06-17 08:35:22 +0000212 XSync(fl_display, False);
213
214 XSetErrorHandler(old_handler);
215
216 if (caughtError)
217 goto free_shmaddr;
Pierre Ossman13500692011-06-13 11:23:08 +0000218
Pierre Ossmanc9dd3a42015-11-18 16:24:16 +0100219 // FLTK is a bit stupid and unreliable if you register the same
220 // callback with different data values.
221 Fl::add_system_handler(handleSystemEvent, NULL);
222 shmList.push_back(this);
223
Pierre Ossman13500692011-06-13 11:23:08 +0000224 vlog.debug("Using shared memory XImage");
225
226 return 1;
227
Pierre Ossman4d01a9e2011-06-17 08:35:22 +0000228free_shmaddr:
229 shmdt(shminfo->shmaddr);
230
Pierre Ossman13500692011-06-13 11:23:08 +0000231free_shm:
232 shmctl(shminfo->shmid, IPC_RMID, 0);
233
234free_xim:
235 XDestroyImage(xim);
236 xim = NULL;
237
238free_shminfo:
239 delete shminfo;
240 shminfo = NULL;
241
242 return 0;
243}
Pierre Ossmanc9dd3a42015-11-18 16:24:16 +0100244
245int X11PixelBuffer::handleSystemEvent(void* event, void* data)
246{
247 XEvent* xevent;
248 XShmCompletionEvent* shmevent;
249
250 std::list<X11PixelBuffer*>::iterator iter;
251
252 xevent = (XEvent*)event;
253 assert(xevent);
254
Pierre Ossman9927d252016-05-04 17:52:49 +0200255 if (xevent->type != XShmGetEventBase(fl_display) + ShmCompletion)
Pierre Ossmanc9dd3a42015-11-18 16:24:16 +0100256 return 0;
257
258 shmevent = (XShmCompletionEvent*)event;
259
Pierre Ossman919ae452016-05-04 17:51:19 +0200260 if (shmevent->send_event)
261 return 0;
262
Pierre Ossmanc9dd3a42015-11-18 16:24:16 +0100263 for (iter = shmList.begin();iter != shmList.end();++iter) {
264 if (shmevent->shmseg != (*iter)->shminfo->shmseg)
265 continue;
266
Pierre Ossman919ae452016-05-04 17:51:19 +0200267 /* HP has a buggy X server on their thin clients that sends bogus
268 * extra events with an incorrect drawable id */
269 if (shmevent->drawable != (*iter)->pendingDrawable)
270 continue;
271
Pierre Ossmanc9dd3a42015-11-18 16:24:16 +0100272 (*iter)->pendingPutImage--;
273 assert((*iter)->pendingPutImage >= 0);
274
275 return 1;
276 }
277
278 return 0;
279}