blob: 29f0f9333be2e888ff5a25fe263b757590762872 [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 Ossman13500692011-06-13 11:23:08 +000024#include <stdlib.h>
25
26#include <FL/x.H>
27
28#include <rfb/LogWriter.h>
29#include <rfb/Exception.h>
30
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +020031#include "i18n.h"
Pierre Ossman13500692011-06-13 11:23:08 +000032#include "X11PixelBuffer.h"
33
34using namespace rfb;
35
Pierre Ossmanac13abe2014-02-07 14:46:26 +010036static rfb::LogWriter vlog("X11PixelBuffer");
Pierre Ossman13500692011-06-13 11:23:08 +000037
38static PixelFormat display_pf()
39{
40 int i;
41
42 int bpp;
43 int trueColour, bigEndian;
44 int redShift, greenShift, blueShift;
45 int redMax, greenMax, blueMax;
46
47 int nformats;
48 XPixmapFormatValues* format;
49
50 // Might not be open at this point
51 fl_open_display();
52
53 format = XListPixmapFormats(fl_display, &nformats);
54
55 for (i = 0; i < nformats; i++)
56 if (format[i].depth == fl_visual->depth) break;
57
58 if (i == nformats)
Pierre Ossman744e55c2014-12-03 14:00:54 +010059 // TRANSLATORS: "pixmap" is an X11 concept and may not be suitable
60 // to translate.
Pierre Ossman86750632014-10-10 13:33:19 +020061 throw rfb::Exception(_("Display lacks pixmap format for default depth"));
Pierre Ossman13500692011-06-13 11:23:08 +000062
63 switch (format[i].bits_per_pixel) {
64 case 8:
65 case 16:
66 case 32:
67 bpp = format[i].bits_per_pixel;
68 break;
69 default:
Pierre Ossman744e55c2014-12-03 14:00:54 +010070 // TRANSLATORS: "pixmap" is an X11 concept and may not be suitable
71 // to translate.
Pierre Ossman86750632014-10-10 13:33:19 +020072 throw rfb::Exception(_("Couldn't find suitable pixmap format"));
Pierre Ossman13500692011-06-13 11:23:08 +000073 }
74
75 XFree(format);
76
77 bigEndian = (ImageByteOrder(fl_display) == MSBFirst);
78 trueColour = (fl_visual->c_class == TrueColor);
79
80 if (!trueColour)
Pierre Ossman86750632014-10-10 13:33:19 +020081 throw rfb::Exception(_("Only true colour displays supported"));
Pierre Ossman13500692011-06-13 11:23:08 +000082
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +020083 vlog.info(_("Using default colormap and visual, %sdepth %d."),
Pierre Ossman13500692011-06-13 11:23:08 +000084 (fl_visual->c_class == TrueColor) ? "TrueColor, " :
85 ((fl_visual->c_class == PseudoColor) ? "PseudoColor, " : ""),
86 fl_visual->depth);
87
88 redShift = ffs(fl_visual->red_mask) - 1;
89 greenShift = ffs(fl_visual->green_mask) - 1;
90 blueShift = ffs(fl_visual->blue_mask) - 1;
91 redMax = fl_visual->red_mask >> redShift;
92 greenMax = fl_visual->green_mask >> greenShift;
93 blueMax = fl_visual->blue_mask >> blueShift;
94
95 return PixelFormat(bpp, fl_visual->depth, bigEndian, trueColour,
96 redMax, greenMax, blueMax,
97 redShift, greenShift, blueShift);
98}
99
Pierre Ossmanac13abe2014-02-07 14:46:26 +0100100X11PixelBuffer::X11PixelBuffer(int width, int height) :
Pierre Ossman2e5a1062014-01-30 17:57:27 +0100101 PlatformPixelBuffer(display_pf(), width, height, NULL, 0),
Pierre Ossman13500692011-06-13 11:23:08 +0000102 shminfo(NULL), xim(NULL)
103{
104 // Might not be open at this point
105 fl_open_display();
106
107 if (!setupShm()) {
108 xim = XCreateImage(fl_display, fl_visual->visual, fl_visual->depth,
109 ZPixmap, 0, 0, width, height, BitmapPad(fl_display), 0);
Pierre Ossmand1a853b2014-10-10 13:37:35 +0200110 if (!xim)
111 throw rfb::Exception(_("Could not create framebuffer image"));
Pierre Ossman13500692011-06-13 11:23:08 +0000112
113 xim->data = (char*)malloc(xim->bytes_per_line * xim->height);
Pierre Ossmand1a853b2014-10-10 13:37:35 +0200114 if (!xim->data)
115 throw rfb::Exception(_("Not enough memory for framebuffer"));
Pierre Ossman13500692011-06-13 11:23:08 +0000116 }
117
118 data = (rdr::U8*)xim->data;
Pierre Ossman2e5a1062014-01-30 17:57:27 +0100119 stride = xim->bytes_per_line / (getPF().bpp/8);
Pierre Ossman13500692011-06-13 11:23:08 +0000120}
121
122
Pierre Ossmanac13abe2014-02-07 14:46:26 +0100123X11PixelBuffer::~X11PixelBuffer()
Pierre Ossman13500692011-06-13 11:23:08 +0000124{
125 if (shminfo) {
126 vlog.debug("Freeing shared memory XImage");
127 shmdt(shminfo->shmaddr);
128 shmctl(shminfo->shmid, IPC_RMID, 0);
129 delete shminfo;
130 shminfo = NULL;
131 }
132
133 // XDestroyImage() will free(xim->data) if appropriate
134 if (xim)
135 XDestroyImage(xim);
136 xim = NULL;
137}
138
139
Pierre Ossmanac13abe2014-02-07 14:46:26 +0100140void X11PixelBuffer::draw(int src_x, int src_y, int x, int y, int w, int h)
Pierre Ossman13500692011-06-13 11:23:08 +0000141{
142 if (shminfo)
143 XShmPutImage(fl_display, fl_window, fl_gc, xim, src_x, src_y, x, y, w, h, False);
144 else
145 XPutImage(fl_display, fl_window, fl_gc, xim, src_x, src_y, x, y, w, h);
146}
147
148
Pierre Ossman4d01a9e2011-06-17 08:35:22 +0000149static bool caughtError;
150
151static int XShmAttachErrorHandler(Display *dpy, XErrorEvent *error)
152{
153 caughtError = true;
154 return 0;
155}
Pierre Ossman13500692011-06-13 11:23:08 +0000156
Pierre Ossmanac13abe2014-02-07 14:46:26 +0100157int X11PixelBuffer::setupShm()
Pierre Ossman13500692011-06-13 11:23:08 +0000158{
159 int major, minor;
160 Bool pixmaps;
Pierre Ossman4d01a9e2011-06-17 08:35:22 +0000161 XErrorHandler old_handler;
162 Status status;
Tim Waughb17c9c42014-10-16 14:53:17 +0100163 const char *display_name = XDisplayName (NULL);
164
165 /* Don't use MIT-SHM on remote displays */
166 if (*display_name && *display_name != ':')
167 return 0;
Pierre Ossman13500692011-06-13 11:23:08 +0000168
169 if (!XShmQueryVersion(fl_display, &major, &minor, &pixmaps))
170 return 0;
171
172 shminfo = new XShmSegmentInfo;
173
174 xim = XShmCreateImage(fl_display, fl_visual->visual, fl_visual->depth,
175 ZPixmap, 0, shminfo, width(), height());
176 if (!xim)
177 goto free_shminfo;
178
179 shminfo->shmid = shmget(IPC_PRIVATE,
180 xim->bytes_per_line * xim->height,
181 IPC_CREAT|0777);
182 if (shminfo->shmid == -1)
183 goto free_xim;
184
185 shminfo->shmaddr = xim->data = (char*)shmat(shminfo->shmid, 0, 0);
186 if (shminfo->shmaddr == (char *)-1)
187 goto free_shm;
188
189 shminfo->readOnly = True;
Pierre Ossman4d01a9e2011-06-17 08:35:22 +0000190
191 // This is the only way we can detect that shared memory won't work
192 // (e.g. because we're accessing a remote X11 server)
193 caughtError = false;
194 old_handler = XSetErrorHandler(XShmAttachErrorHandler);
195
Tim Waugh7c56b4c2014-10-17 10:28:55 +0100196 if (!XShmAttach(fl_display, shminfo)) {
197 XSetErrorHandler(old_handler);
198 goto free_shmaddr;
199 }
200
Pierre Ossman4d01a9e2011-06-17 08:35:22 +0000201 XSync(fl_display, False);
202
203 XSetErrorHandler(old_handler);
204
205 if (caughtError)
206 goto free_shmaddr;
Pierre Ossman13500692011-06-13 11:23:08 +0000207
208 vlog.debug("Using shared memory XImage");
209
210 return 1;
211
Pierre Ossman4d01a9e2011-06-17 08:35:22 +0000212free_shmaddr:
213 shmdt(shminfo->shmaddr);
214
Pierre Ossman13500692011-06-13 11:23:08 +0000215free_shm:
216 shmctl(shminfo->shmid, IPC_RMID, 0);
217
218free_xim:
219 XDestroyImage(xim);
220 xim = NULL;
221
222free_shminfo:
223 delete shminfo;
224 shminfo = NULL;
225
226 return 0;
227}