blob: a7794277b70af85b1728cb3d1893846d1eba1a65 [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 Ossman86750632014-10-10 13:33:19 +020059 throw rfb::Exception(_("Display lacks pixmap format for default depth"));
Pierre Ossman13500692011-06-13 11:23:08 +000060
61 switch (format[i].bits_per_pixel) {
62 case 8:
63 case 16:
64 case 32:
65 bpp = format[i].bits_per_pixel;
66 break;
67 default:
Pierre Ossman86750632014-10-10 13:33:19 +020068 throw rfb::Exception(_("Couldn't find suitable pixmap format"));
Pierre Ossman13500692011-06-13 11:23:08 +000069 }
70
71 XFree(format);
72
73 bigEndian = (ImageByteOrder(fl_display) == MSBFirst);
74 trueColour = (fl_visual->c_class == TrueColor);
75
76 if (!trueColour)
Pierre Ossman86750632014-10-10 13:33:19 +020077 throw rfb::Exception(_("Only true colour displays supported"));
Pierre Ossman13500692011-06-13 11:23:08 +000078
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +020079 vlog.info(_("Using default colormap and visual, %sdepth %d."),
Pierre Ossman13500692011-06-13 11:23:08 +000080 (fl_visual->c_class == TrueColor) ? "TrueColor, " :
81 ((fl_visual->c_class == PseudoColor) ? "PseudoColor, " : ""),
82 fl_visual->depth);
83
84 redShift = ffs(fl_visual->red_mask) - 1;
85 greenShift = ffs(fl_visual->green_mask) - 1;
86 blueShift = ffs(fl_visual->blue_mask) - 1;
87 redMax = fl_visual->red_mask >> redShift;
88 greenMax = fl_visual->green_mask >> greenShift;
89 blueMax = fl_visual->blue_mask >> blueShift;
90
91 return PixelFormat(bpp, fl_visual->depth, bigEndian, trueColour,
92 redMax, greenMax, blueMax,
93 redShift, greenShift, blueShift);
94}
95
Pierre Ossmanac13abe2014-02-07 14:46:26 +010096X11PixelBuffer::X11PixelBuffer(int width, int height) :
Pierre Ossman2e5a1062014-01-30 17:57:27 +010097 PlatformPixelBuffer(display_pf(), width, height, NULL, 0),
Pierre Ossman13500692011-06-13 11:23:08 +000098 shminfo(NULL), xim(NULL)
99{
100 // Might not be open at this point
101 fl_open_display();
102
103 if (!setupShm()) {
104 xim = XCreateImage(fl_display, fl_visual->visual, fl_visual->depth,
105 ZPixmap, 0, 0, width, height, BitmapPad(fl_display), 0);
Pierre Ossmand1a853b2014-10-10 13:37:35 +0200106 if (!xim)
107 throw rfb::Exception(_("Could not create framebuffer image"));
Pierre Ossman13500692011-06-13 11:23:08 +0000108
109 xim->data = (char*)malloc(xim->bytes_per_line * xim->height);
Pierre Ossmand1a853b2014-10-10 13:37:35 +0200110 if (!xim->data)
111 throw rfb::Exception(_("Not enough memory for framebuffer"));
Pierre Ossman13500692011-06-13 11:23:08 +0000112 }
113
114 data = (rdr::U8*)xim->data;
Pierre Ossman2e5a1062014-01-30 17:57:27 +0100115 stride = xim->bytes_per_line / (getPF().bpp/8);
Pierre Ossman13500692011-06-13 11:23:08 +0000116}
117
118
Pierre Ossmanac13abe2014-02-07 14:46:26 +0100119X11PixelBuffer::~X11PixelBuffer()
Pierre Ossman13500692011-06-13 11:23:08 +0000120{
121 if (shminfo) {
122 vlog.debug("Freeing shared memory XImage");
123 shmdt(shminfo->shmaddr);
124 shmctl(shminfo->shmid, IPC_RMID, 0);
125 delete shminfo;
126 shminfo = NULL;
127 }
128
129 // XDestroyImage() will free(xim->data) if appropriate
130 if (xim)
131 XDestroyImage(xim);
132 xim = NULL;
133}
134
135
Pierre Ossmanac13abe2014-02-07 14:46:26 +0100136void X11PixelBuffer::draw(int src_x, int src_y, int x, int y, int w, int h)
Pierre Ossman13500692011-06-13 11:23:08 +0000137{
138 if (shminfo)
139 XShmPutImage(fl_display, fl_window, fl_gc, xim, src_x, src_y, x, y, w, h, False);
140 else
141 XPutImage(fl_display, fl_window, fl_gc, xim, src_x, src_y, x, y, w, h);
142}
143
144
Pierre Ossman4d01a9e2011-06-17 08:35:22 +0000145static bool caughtError;
146
147static int XShmAttachErrorHandler(Display *dpy, XErrorEvent *error)
148{
149 caughtError = true;
150 return 0;
151}
Pierre Ossman13500692011-06-13 11:23:08 +0000152
Pierre Ossmanac13abe2014-02-07 14:46:26 +0100153int X11PixelBuffer::setupShm()
Pierre Ossman13500692011-06-13 11:23:08 +0000154{
155 int major, minor;
156 Bool pixmaps;
Pierre Ossman4d01a9e2011-06-17 08:35:22 +0000157 XErrorHandler old_handler;
158 Status status;
Tim Waughb17c9c42014-10-16 14:53:17 +0100159 const char *display_name = XDisplayName (NULL);
160
161 /* Don't use MIT-SHM on remote displays */
162 if (*display_name && *display_name != ':')
163 return 0;
Pierre Ossman13500692011-06-13 11:23:08 +0000164
165 if (!XShmQueryVersion(fl_display, &major, &minor, &pixmaps))
166 return 0;
167
168 shminfo = new XShmSegmentInfo;
169
170 xim = XShmCreateImage(fl_display, fl_visual->visual, fl_visual->depth,
171 ZPixmap, 0, shminfo, width(), height());
172 if (!xim)
173 goto free_shminfo;
174
175 shminfo->shmid = shmget(IPC_PRIVATE,
176 xim->bytes_per_line * xim->height,
177 IPC_CREAT|0777);
178 if (shminfo->shmid == -1)
179 goto free_xim;
180
181 shminfo->shmaddr = xim->data = (char*)shmat(shminfo->shmid, 0, 0);
182 if (shminfo->shmaddr == (char *)-1)
183 goto free_shm;
184
185 shminfo->readOnly = True;
Pierre Ossman4d01a9e2011-06-17 08:35:22 +0000186
187 // This is the only way we can detect that shared memory won't work
188 // (e.g. because we're accessing a remote X11 server)
189 caughtError = false;
190 old_handler = XSetErrorHandler(XShmAttachErrorHandler);
191
Tim Waugh7c56b4c2014-10-17 10:28:55 +0100192 if (!XShmAttach(fl_display, shminfo)) {
193 XSetErrorHandler(old_handler);
194 goto free_shmaddr;
195 }
196
Pierre Ossman4d01a9e2011-06-17 08:35:22 +0000197 XSync(fl_display, False);
198
199 XSetErrorHandler(old_handler);
200
201 if (caughtError)
202 goto free_shmaddr;
Pierre Ossman13500692011-06-13 11:23:08 +0000203
204 vlog.debug("Using shared memory XImage");
205
206 return 1;
207
Pierre Ossman4d01a9e2011-06-17 08:35:22 +0000208free_shmaddr:
209 shmdt(shminfo->shmaddr);
210
Pierre Ossman13500692011-06-13 11:23:08 +0000211free_shm:
212 shmctl(shminfo->shmid, IPC_RMID, 0);
213
214free_xim:
215 XDestroyImage(xim);
216 xim = NULL;
217
218free_shminfo:
219 delete shminfo;
220 shminfo = NULL;
221
222 return 0;
223}