blob: 59b90e2cbf25a96e24ebccacdd87772e687bff3d [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;
Pierre Ossman13500692011-06-13 11:23:08 +0000159
160 if (!XShmQueryVersion(fl_display, &major, &minor, &pixmaps))
161 return 0;
162
163 shminfo = new XShmSegmentInfo;
164
165 xim = XShmCreateImage(fl_display, fl_visual->visual, fl_visual->depth,
166 ZPixmap, 0, shminfo, width(), height());
167 if (!xim)
168 goto free_shminfo;
169
170 shminfo->shmid = shmget(IPC_PRIVATE,
171 xim->bytes_per_line * xim->height,
172 IPC_CREAT|0777);
173 if (shminfo->shmid == -1)
174 goto free_xim;
175
176 shminfo->shmaddr = xim->data = (char*)shmat(shminfo->shmid, 0, 0);
177 if (shminfo->shmaddr == (char *)-1)
178 goto free_shm;
179
180 shminfo->readOnly = True;
Pierre Ossman4d01a9e2011-06-17 08:35:22 +0000181
182 // This is the only way we can detect that shared memory won't work
183 // (e.g. because we're accessing a remote X11 server)
184 caughtError = false;
185 old_handler = XSetErrorHandler(XShmAttachErrorHandler);
186
Pierre Ossman13500692011-06-13 11:23:08 +0000187 XShmAttach(fl_display, shminfo);
Pierre Ossman4d01a9e2011-06-17 08:35:22 +0000188 XSync(fl_display, False);
189
190 XSetErrorHandler(old_handler);
191
192 if (caughtError)
193 goto free_shmaddr;
Pierre Ossman13500692011-06-13 11:23:08 +0000194
195 vlog.debug("Using shared memory XImage");
196
197 return 1;
198
Pierre Ossman4d01a9e2011-06-17 08:35:22 +0000199free_shmaddr:
200 shmdt(shminfo->shmaddr);
201
Pierre Ossman13500692011-06-13 11:23:08 +0000202free_shm:
203 shmctl(shminfo->shmid, IPC_RMID, 0);
204
205free_xim:
206 XDestroyImage(xim);
207 xim = NULL;
208
209free_shminfo:
210 delete shminfo;
211 shminfo = NULL;
212
213 return 0;
214}