blob: 7e89a60bf7cb5a1c445feecd82cae4b6d46f92d2 [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 Ossman2aef35c2014-12-03 14:01:56 +010083 vlog.info(_("Using default colormap and visual, TrueColor, depth %d."),
Pierre Ossman13500692011-06-13 11:23:08 +000084 fl_visual->depth);
85
86 redShift = ffs(fl_visual->red_mask) - 1;
87 greenShift = ffs(fl_visual->green_mask) - 1;
88 blueShift = ffs(fl_visual->blue_mask) - 1;
89 redMax = fl_visual->red_mask >> redShift;
90 greenMax = fl_visual->green_mask >> greenShift;
91 blueMax = fl_visual->blue_mask >> blueShift;
92
93 return PixelFormat(bpp, fl_visual->depth, bigEndian, trueColour,
94 redMax, greenMax, blueMax,
95 redShift, greenShift, blueShift);
96}
97
Pierre Ossmanac13abe2014-02-07 14:46:26 +010098X11PixelBuffer::X11PixelBuffer(int width, int height) :
Pierre Ossman2e5a1062014-01-30 17:57:27 +010099 PlatformPixelBuffer(display_pf(), width, height, NULL, 0),
Pierre Ossman13500692011-06-13 11:23:08 +0000100 shminfo(NULL), xim(NULL)
101{
102 // Might not be open at this point
103 fl_open_display();
104
105 if (!setupShm()) {
106 xim = XCreateImage(fl_display, fl_visual->visual, fl_visual->depth,
107 ZPixmap, 0, 0, width, height, BitmapPad(fl_display), 0);
Pierre Ossmand1a853b2014-10-10 13:37:35 +0200108 if (!xim)
109 throw rfb::Exception(_("Could not create framebuffer image"));
Pierre Ossman13500692011-06-13 11:23:08 +0000110
111 xim->data = (char*)malloc(xim->bytes_per_line * xim->height);
Pierre Ossmand1a853b2014-10-10 13:37:35 +0200112 if (!xim->data)
113 throw rfb::Exception(_("Not enough memory for framebuffer"));
Pierre Ossman13500692011-06-13 11:23:08 +0000114 }
115
116 data = (rdr::U8*)xim->data;
Pierre Ossman2e5a1062014-01-30 17:57:27 +0100117 stride = xim->bytes_per_line / (getPF().bpp/8);
Pierre Ossman13500692011-06-13 11:23:08 +0000118}
119
120
Pierre Ossmanac13abe2014-02-07 14:46:26 +0100121X11PixelBuffer::~X11PixelBuffer()
Pierre Ossman13500692011-06-13 11:23:08 +0000122{
123 if (shminfo) {
124 vlog.debug("Freeing shared memory XImage");
125 shmdt(shminfo->shmaddr);
126 shmctl(shminfo->shmid, IPC_RMID, 0);
127 delete shminfo;
128 shminfo = NULL;
129 }
130
131 // XDestroyImage() will free(xim->data) if appropriate
132 if (xim)
133 XDestroyImage(xim);
134 xim = NULL;
135}
136
137
Pierre Ossmanac13abe2014-02-07 14:46:26 +0100138void X11PixelBuffer::draw(int src_x, int src_y, int x, int y, int w, int h)
Pierre Ossman13500692011-06-13 11:23:08 +0000139{
140 if (shminfo)
141 XShmPutImage(fl_display, fl_window, fl_gc, xim, src_x, src_y, x, y, w, h, False);
142 else
143 XPutImage(fl_display, fl_window, fl_gc, xim, src_x, src_y, x, y, w, h);
144}
145
146
Pierre Ossman4d01a9e2011-06-17 08:35:22 +0000147static bool caughtError;
148
149static int XShmAttachErrorHandler(Display *dpy, XErrorEvent *error)
150{
151 caughtError = true;
152 return 0;
153}
Pierre Ossman13500692011-06-13 11:23:08 +0000154
Pierre Ossmanac13abe2014-02-07 14:46:26 +0100155int X11PixelBuffer::setupShm()
Pierre Ossman13500692011-06-13 11:23:08 +0000156{
157 int major, minor;
158 Bool pixmaps;
Pierre Ossman4d01a9e2011-06-17 08:35:22 +0000159 XErrorHandler old_handler;
160 Status status;
Tim Waughb17c9c42014-10-16 14:53:17 +0100161 const char *display_name = XDisplayName (NULL);
162
163 /* Don't use MIT-SHM on remote displays */
164 if (*display_name && *display_name != ':')
165 return 0;
Pierre Ossman13500692011-06-13 11:23:08 +0000166
167 if (!XShmQueryVersion(fl_display, &major, &minor, &pixmaps))
168 return 0;
169
170 shminfo = new XShmSegmentInfo;
171
172 xim = XShmCreateImage(fl_display, fl_visual->visual, fl_visual->depth,
173 ZPixmap, 0, shminfo, width(), height());
174 if (!xim)
175 goto free_shminfo;
176
177 shminfo->shmid = shmget(IPC_PRIVATE,
178 xim->bytes_per_line * xim->height,
179 IPC_CREAT|0777);
180 if (shminfo->shmid == -1)
181 goto free_xim;
182
183 shminfo->shmaddr = xim->data = (char*)shmat(shminfo->shmid, 0, 0);
184 if (shminfo->shmaddr == (char *)-1)
185 goto free_shm;
186
187 shminfo->readOnly = True;
Pierre Ossman4d01a9e2011-06-17 08:35:22 +0000188
189 // This is the only way we can detect that shared memory won't work
190 // (e.g. because we're accessing a remote X11 server)
191 caughtError = false;
192 old_handler = XSetErrorHandler(XShmAttachErrorHandler);
193
Tim Waugh7c56b4c2014-10-17 10:28:55 +0100194 if (!XShmAttach(fl_display, shminfo)) {
195 XSetErrorHandler(old_handler);
196 goto free_shmaddr;
197 }
198
Pierre Ossman4d01a9e2011-06-17 08:35:22 +0000199 XSync(fl_display, False);
200
201 XSetErrorHandler(old_handler);
202
203 if (caughtError)
204 goto free_shmaddr;
Pierre Ossman13500692011-06-13 11:23:08 +0000205
206 vlog.debug("Using shared memory XImage");
207
208 return 1;
209
Pierre Ossman4d01a9e2011-06-17 08:35:22 +0000210free_shmaddr:
211 shmdt(shminfo->shmaddr);
212
Pierre Ossman13500692011-06-13 11:23:08 +0000213free_shm:
214 shmctl(shminfo->shmid, IPC_RMID, 0);
215
216free_xim:
217 XDestroyImage(xim);
218 xim = NULL;
219
220free_shminfo:
221 delete shminfo;
222 shminfo = NULL;
223
224 return 0;
225}