blob: f6e32e42f2c9ae0f0e2b4782cea6dfe60d38d401 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
scroggob7e9aee2011-03-15 15:15:15 +00008#include <X11/Xlib.h>
9#include <X11/Xatom.h>
bsalomon@google.com75356a72012-06-19 13:08:59 +000010#include <X11/XKBlib.h>
scroggob7e9aee2011-03-15 15:15:15 +000011#include <GL/glx.h>
Scroggo9df214e2011-04-15 14:48:08 +000012#include <GL/gl.h>
13#include <GL/glu.h>
scroggob7e9aee2011-03-15 15:15:15 +000014
15#include "SkWindow.h"
16
17#include "SkBitmap.h"
scroggo08526c02011-03-22 14:03:21 +000018#include "SkCanvas.h"
scroggob7e9aee2011-03-15 15:15:15 +000019#include "SkColor.h"
20#include "SkEvent.h"
Scroggo9df214e2011-04-15 14:48:08 +000021#include "SkKey.h"
22#include "SkWindow.h"
23#include "XkeysToSkKeys.h"
24extern "C" {
25 #include "keysym2ucs.h"
26}
27
Scroggoaed68d92011-06-08 14:26:00 +000028const int WIDTH = 500;
29const int HEIGHT = 500;
Scroggo9df214e2011-04-15 14:48:08 +000030
31// Determine which events to listen for.
32const long EVENT_MASK = StructureNotifyMask|ButtonPressMask|ButtonReleaseMask
33 |ExposureMask|PointerMotionMask|KeyPressMask|KeyReleaseMask;
scroggob7e9aee2011-03-15 15:15:15 +000034
bsalomon@google.com8108c472012-04-03 19:33:08 +000035SkOSWindow::SkOSWindow(void* unused)
robertphillips@google.com4bdfb8c2012-06-12 21:23:49 +000036 : fVi(NULL)
bsalomon@google.com11959252012-04-06 20:13:38 +000037 , fMSAASampleCount(0) {
38 fUnixWindow.fDisplay = NULL;
bsalomon@google.com8108c472012-04-03 19:33:08 +000039 fUnixWindow.fGLContext = NULL;
bsalomon@google.com64cc8102013-03-05 20:06:05 +000040 this->initWindow(0, NULL);
Scroggo9df214e2011-04-15 14:48:08 +000041 this->resize(WIDTH, HEIGHT);
scroggob7e9aee2011-03-15 15:15:15 +000042}
43
bsalomon@google.com8108c472012-04-03 19:33:08 +000044SkOSWindow::~SkOSWindow() {
bsalomon@google.com11959252012-04-06 20:13:38 +000045 this->closeWindow();
46}
47
48void SkOSWindow::closeWindow() {
bsalomon@google.com8108c472012-04-03 19:33:08 +000049 if (NULL != fUnixWindow.fDisplay) {
bsalomon@google.com11959252012-04-06 20:13:38 +000050 this->detach();
51 SkASSERT(NULL != fUnixWindow.fGc);
bsalomon@google.com8108c472012-04-03 19:33:08 +000052 XFreeGC(fUnixWindow.fDisplay, fUnixWindow.fGc);
bsalomon@google.com11959252012-04-06 20:13:38 +000053 fUnixWindow.fGc = NULL;
Scroggo9df214e2011-04-15 14:48:08 +000054 XDestroyWindow(fUnixWindow.fDisplay, fUnixWindow.fWin);
bsalomon@google.com11959252012-04-06 20:13:38 +000055 fVi = NULL;
Scroggo9df214e2011-04-15 14:48:08 +000056 XCloseDisplay(fUnixWindow.fDisplay);
bsalomon@google.com11959252012-04-06 20:13:38 +000057 fUnixWindow.fDisplay = NULL;
58 fMSAASampleCount = 0;
Scroggo9df214e2011-04-15 14:48:08 +000059 }
scroggob7e9aee2011-03-15 15:15:15 +000060}
61
bsalomon@google.com64cc8102013-03-05 20:06:05 +000062void SkOSWindow::initWindow(int requestedMSAASampleCount, AttachmentInfo* info) {
bsalomon@google.com11959252012-04-06 20:13:38 +000063 if (fMSAASampleCount != requestedMSAASampleCount) {
64 this->closeWindow();
65 }
66 // presence of fDisplay means we already have a window
67 if (NULL != fUnixWindow.fDisplay) {
bsalomon@google.com64cc8102013-03-05 20:06:05 +000068 if (NULL != info) {
69 if (NULL != fVi) {
70 glXGetConfig(fUnixWindow.fDisplay, fVi, GLX_SAMPLES_ARB, &info->fSampleCount);
71 glXGetConfig(fUnixWindow.fDisplay, fVi, GLX_STENCIL_SIZE, &info->fStencilBits);
72 } else {
73 info->fSampleCount = 0;
74 info->fStencilBits = 0;
75 }
76 }
bsalomon@google.com11959252012-04-06 20:13:38 +000077 return;
78 }
79 fUnixWindow.fDisplay = XOpenDisplay(NULL);
80 Display* dsp = fUnixWindow.fDisplay;
81 if (NULL == dsp) {
82 SkDebugf("Could not open an X Display");
83 return;
84 }
85 // Attempt to create a window that supports GL
86 GLint att[] = {
87 GLX_RGBA,
88 GLX_DEPTH_SIZE, 24,
89 GLX_DOUBLEBUFFER,
90 GLX_STENCIL_SIZE, 8,
91 None
92 };
93 SkASSERT(NULL == fVi);
94 if (requestedMSAASampleCount > 0) {
95 static const GLint kAttCount = SK_ARRAY_COUNT(att);
96 GLint msaaAtt[kAttCount + 4];
97 memcpy(msaaAtt, att, sizeof(att));
98 SkASSERT(None == msaaAtt[kAttCount - 1]);
99 msaaAtt[kAttCount - 1] = GLX_SAMPLE_BUFFERS_ARB;
100 msaaAtt[kAttCount + 0] = 1;
101 msaaAtt[kAttCount + 1] = GLX_SAMPLES_ARB;
102 msaaAtt[kAttCount + 2] = requestedMSAASampleCount;
103 msaaAtt[kAttCount + 3] = None;
104 fVi = glXChooseVisual(dsp, DefaultScreen(dsp), msaaAtt);
105 fMSAASampleCount = requestedMSAASampleCount;
106 }
107 if (NULL == fVi) {
108 fVi = glXChooseVisual(dsp, DefaultScreen(dsp), att);
109 fMSAASampleCount = 0;
110 }
111
112 if (fVi) {
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000113 if (NULL != info) {
114 glXGetConfig(dsp, fVi, GLX_SAMPLES_ARB, &info->fSampleCount);
115 glXGetConfig(dsp, fVi, GLX_STENCIL_SIZE, &info->fStencilBits);
116 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000117 Colormap colorMap = XCreateColormap(dsp,
118 RootWindow(dsp, fVi->screen),
119 fVi->visual,
120 AllocNone);
121 XSetWindowAttributes swa;
122 swa.colormap = colorMap;
123 swa.event_mask = EVENT_MASK;
124 fUnixWindow.fWin = XCreateWindow(dsp,
125 RootWindow(dsp, fVi->screen),
126 0, 0, // x, y
127 WIDTH, HEIGHT,
128 0, // border width
129 fVi->depth,
130 InputOutput,
131 fVi->visual,
132 CWEventMask | CWColormap,
133 &swa);
134 } else {
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000135 if (NULL != info) {
136 info->fSampleCount = 0;
137 info->fStencilBits = 0;
138 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000139 // Create a simple window instead. We will not be able to show GL
140 fUnixWindow.fWin = XCreateSimpleWindow(dsp,
141 DefaultRootWindow(dsp),
142 0, 0, // x, y
143 WIDTH, HEIGHT,
144 0, // border width
145 0, // border value
146 0); // background value
147 }
148 this->mapWindowAndWait();
149 fUnixWindow.fGc = XCreateGC(dsp, fUnixWindow.fWin, 0, NULL);
150}
151
152
bsalomon@google.com8108c472012-04-03 19:33:08 +0000153void SkOSWindow::post_linuxevent() {
Scroggo9df214e2011-04-15 14:48:08 +0000154 // Put an event in the X queue to fire an SkEvent.
bsalomon@google.com11959252012-04-06 20:13:38 +0000155 if (NULL == fUnixWindow.fDisplay) {
bsalomon@google.com8108c472012-04-03 19:33:08 +0000156 return;
157 }
Scroggo9df214e2011-04-15 14:48:08 +0000158 long event_mask = NoEventMask;
159 XClientMessageEvent event;
160 event.type = ClientMessage;
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000161 Atom myAtom(0);
Scroggo9df214e2011-04-15 14:48:08 +0000162 event.message_type = myAtom;
163 event.format = 32;
164 event.data.l[0] = 0;
165 XSendEvent(fUnixWindow.fDisplay, fUnixWindow.fWin, false, 0,
166 (XEvent*) &event);
Scroggo5a234242011-06-13 19:17:58 +0000167 XFlush(fUnixWindow.fDisplay);
Scroggo9df214e2011-04-15 14:48:08 +0000168}
169
reed@google.com37924182013-01-08 16:25:42 +0000170static unsigned getModi(const XEvent& evt) {
reed@google.come378d832013-01-08 16:50:52 +0000171 static const struct {
172 unsigned fXMask;
173 unsigned fSkMask;
174 } gModi[] = {
175 // X values found by experiment. Is there a better way?
176 { 1, kShift_SkModifierKey },
177 { 4, kControl_SkModifierKey },
178 { 8, kOption_SkModifierKey },
179 };
skia.committer@gmail.com4e73aa12013-01-09 02:01:30 +0000180
reed@google.come378d832013-01-08 16:50:52 +0000181 unsigned modi = 0;
182 for (size_t i = 0; i < SK_ARRAY_COUNT(gModi); ++i) {
183 if (evt.xkey.state & gModi[i].fXMask) {
184 modi |= gModi[i].fSkMask;
185 }
186 }
187 return modi;
reed@google.com4d5c26d2013-01-08 16:17:50 +0000188}
189
bsalomon@google.com8108c472012-04-03 19:33:08 +0000190void SkOSWindow::loop() {
Scroggo9df214e2011-04-15 14:48:08 +0000191 Display* dsp = fUnixWindow.fDisplay;
bsalomon@google.com11959252012-04-06 20:13:38 +0000192 if (NULL == dsp) {
193 return;
194 }
Scroggo9df214e2011-04-15 14:48:08 +0000195 XSelectInput(dsp, fUnixWindow.fWin, EVENT_MASK);
196
197 bool loop = true;
198 XEvent evt;
199 while (loop) {
Scroggo9df214e2011-04-15 14:48:08 +0000200 XNextEvent(dsp, &evt);
201 switch (evt.type) {
202 case Expose:
203 if (evt.xexpose.count == 0)
204 this->inval(NULL);
205 break;
206 case ConfigureNotify:
207 this->resize(evt.xconfigure.width, evt.xconfigure.height);
208 break;
209 case ButtonPress:
210 if (evt.xbutton.button == Button1)
reed@google.com4d5c26d2013-01-08 16:17:50 +0000211 this->handleClick(evt.xbutton.x, evt.xbutton.y,
reed@google.com37924182013-01-08 16:25:42 +0000212 SkView::Click::kDown_State, NULL, getModi(evt));
Scroggo9df214e2011-04-15 14:48:08 +0000213 break;
214 case ButtonRelease:
215 if (evt.xbutton.button == Button1)
reed@google.com4d5c26d2013-01-08 16:17:50 +0000216 this->handleClick(evt.xbutton.x, evt.xbutton.y,
reed@google.com37924182013-01-08 16:25:42 +0000217 SkView::Click::kUp_State, NULL, getModi(evt));
Scroggo9df214e2011-04-15 14:48:08 +0000218 break;
219 case MotionNotify:
reed@google.com4d5c26d2013-01-08 16:17:50 +0000220 this->handleClick(evt.xmotion.x, evt.xmotion.y,
reed@google.com37924182013-01-08 16:25:42 +0000221 SkView::Click::kMoved_State, NULL, getModi(evt));
Scroggo9df214e2011-04-15 14:48:08 +0000222 break;
bsalomon@google.com8108c472012-04-03 19:33:08 +0000223 case KeyPress: {
bsalomon@google.com75356a72012-06-19 13:08:59 +0000224 KeySym keysym = XkbKeycodeToKeysym(dsp, evt.xkey.keycode, 0, 0);
Scroggo9df214e2011-04-15 14:48:08 +0000225 //SkDebugf("pressed key %i!\n\tKeySym:%i\n", evt.xkey.keycode, XKeycodeToKeysym(dsp, evt.xkey.keycode, 0));
226 if (keysym == XK_Escape) {
227 loop = false;
228 break;
229 }
230 this->handleKey(XKeyToSkKey(keysym));
231 long uni = keysym2ucs(keysym);
232 if (uni != -1) {
233 this->handleChar((SkUnichar) uni);
234 }
235 break;
236 }
237 case KeyRelease:
238 //SkDebugf("released key %i\n", evt.xkey.keycode);
bsalomon@google.com75356a72012-06-19 13:08:59 +0000239 this->handleKeyUp(XKeyToSkKey(XkbKeycodeToKeysym(dsp, evt.xkey.keycode, 0, 0)));
Scroggo9df214e2011-04-15 14:48:08 +0000240 break;
241 case ClientMessage:
242 if (SkEvent::ProcessEvent()) {
243 this->post_linuxevent();
244 }
245 break;
246 default:
247 // Do nothing for other events
248 break;
249 }
250 }
251}
252
bsalomon@google.com8108c472012-04-03 19:33:08 +0000253void SkOSWindow::mapWindowAndWait() {
bsalomon@google.com11959252012-04-06 20:13:38 +0000254 SkASSERT(NULL != fUnixWindow.fDisplay);
Scroggo9df214e2011-04-15 14:48:08 +0000255 Display* dsp = fUnixWindow.fDisplay;
256 Window win = fUnixWindow.fWin;
257 XMapWindow(dsp, win);
258
259 long eventMask = StructureNotifyMask;
260 XSelectInput(dsp, win, eventMask);
261
262 // Wait until screen is ready.
263 XEvent evt;
264 do {
265 XNextEvent(dsp, &evt);
266 } while(evt.type != MapNotify);
267
scroggob7e9aee2011-03-15 15:15:15 +0000268}
269
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000270bool SkOSWindow::attach(SkBackEndTypes, int msaaSampleCount, AttachmentInfo* info) {
271 this->initWindow(msaaSampleCount, info);
272
bsalomon@google.com11959252012-04-06 20:13:38 +0000273 if (NULL == fUnixWindow.fDisplay) {
274 return false;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000275 }
bsalomon@google.com8108c472012-04-03 19:33:08 +0000276 if (NULL == fUnixWindow.fGLContext) {
bsalomon@google.com11959252012-04-06 20:13:38 +0000277 SkASSERT(NULL != fVi);
Scroggo9df214e2011-04-15 14:48:08 +0000278
bsalomon@google.com8108c472012-04-03 19:33:08 +0000279 fUnixWindow.fGLContext = glXCreateContext(fUnixWindow.fDisplay,
280 fVi,
281 NULL,
282 GL_TRUE);
283 if (NULL == fUnixWindow.fGLContext) {
284 return false;
285 }
Scroggo9df214e2011-04-15 14:48:08 +0000286 }
bsalomon@google.com8108c472012-04-03 19:33:08 +0000287 glXMakeCurrent(fUnixWindow.fDisplay,
288 fUnixWindow.fWin,
289 fUnixWindow.fGLContext);
290 glViewport(0, 0,
291 SkScalarRound(this->width()), SkScalarRound(this->height()));
292 glClearColor(0, 0, 0, 0);
293 glClearStencil(0);
294 glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
Scroggo9df214e2011-04-15 14:48:08 +0000295 return true;
scroggob7e9aee2011-03-15 15:15:15 +0000296}
297
bsalomon@google.com8108c472012-04-03 19:33:08 +0000298void SkOSWindow::detach() {
299 if (NULL == fUnixWindow.fDisplay || NULL == fUnixWindow.fGLContext) {
300 return;
301 }
Scroggo9df214e2011-04-15 14:48:08 +0000302 glXMakeCurrent(fUnixWindow.fDisplay, None, NULL);
bsalomon@google.com8108c472012-04-03 19:33:08 +0000303 glXDestroyContext(fUnixWindow.fDisplay, fUnixWindow.fGLContext);
304 fUnixWindow.fGLContext = NULL;
scroggob7e9aee2011-03-15 15:15:15 +0000305}
306
bsalomon@google.com8108c472012-04-03 19:33:08 +0000307void SkOSWindow::present() {
308 if (NULL != fUnixWindow.fDisplay && NULL != fUnixWindow.fGLContext) {
Scroggo9df214e2011-04-15 14:48:08 +0000309 glXSwapBuffers(fUnixWindow.fDisplay, fUnixWindow.fWin);
310 }
scroggob7e9aee2011-03-15 15:15:15 +0000311}
312
bsalomon@google.com8108c472012-04-03 19:33:08 +0000313void SkOSWindow::onSetTitle(const char title[]) {
bsalomon@google.com11959252012-04-06 20:13:38 +0000314 if (NULL == fUnixWindow.fDisplay) {
bsalomon@google.com8108c472012-04-03 19:33:08 +0000315 return;
316 }
scroggob7e9aee2011-03-15 15:15:15 +0000317 XTextProperty textProp;
318 textProp.value = (unsigned char*)title;
319 textProp.format = 8;
320 textProp.nitems = strlen((char*)textProp.value);
321 textProp.encoding = XA_STRING;
322 XSetWMName(fUnixWindow.fDisplay, fUnixWindow.fWin, &textProp);
323}
324
reed@google.comf2164b22011-08-04 13:57:56 +0000325void SkOSWindow::onHandleInval(const SkIRect&) {
326 (new SkEvent("inval-imageview", this->getSinkID()))->post();
scroggob7e9aee2011-03-15 15:15:15 +0000327}
328
bsalomon@google.com8108c472012-04-03 19:33:08 +0000329bool SkOSWindow::onEvent(const SkEvent& evt) {
scroggob7e9aee2011-03-15 15:15:15 +0000330 if (evt.isType("inval-imageview")) {
331 update(NULL);
bsalomon@google.com8108c472012-04-03 19:33:08 +0000332 if (NULL == fUnixWindow.fGLContext)
333 this->doPaint();
scroggob7e9aee2011-03-15 15:15:15 +0000334 return true;
335 }
336 return INHERITED::onEvent(evt);
337}
338
bsalomon@google.com8108c472012-04-03 19:33:08 +0000339static bool convertBitmapToXImage(XImage& image, const SkBitmap& bitmap) {
scroggob66365f2011-03-18 21:43:03 +0000340 sk_bzero(&image, sizeof(image));
341
342 int bitsPerPixel = bitmap.bytesPerPixel() * 8;
343 image.width = bitmap.width();
344 image.height = bitmap.height();
345 image.format = ZPixmap;
346 image.data = (char*) bitmap.getPixels();
347 image.byte_order = LSBFirst;
348 image.bitmap_unit = bitsPerPixel;
349 image.bitmap_bit_order = LSBFirst;
350 image.bitmap_pad = bitsPerPixel;
351 image.depth = 24;
352 image.bytes_per_line = bitmap.rowBytes() - bitmap.width() * bitmap.bytesPerPixel();
353 image.bits_per_pixel = bitsPerPixel;
scroggo08526c02011-03-22 14:03:21 +0000354 return XInitImage(&image);
355}
356
scroggo08526c02011-03-22 14:03:21 +0000357void SkOSWindow::doPaint() {
bsalomon@google.com11959252012-04-06 20:13:38 +0000358 if (NULL == fUnixWindow.fDisplay) {
359 return;
360 }
scroggo08526c02011-03-22 14:03:21 +0000361 // Draw the bitmap to the screen.
362 const SkBitmap& bitmap = getBitmap();
363 int width = bitmap.width();
364 int height = bitmap.height();
365
Scroggo0f185c22011-03-24 18:35:50 +0000366 XImage image;
bsalomon@google.com11959252012-04-06 20:13:38 +0000367 if (!convertBitmapToXImage(image, bitmap)) {
368 return;
369 }
scroggo08526c02011-03-22 14:03:21 +0000370
bsalomon@google.com11959252012-04-06 20:13:38 +0000371 XPutImage(fUnixWindow.fDisplay,
372 fUnixWindow.fWin,
373 fUnixWindow.fGc,
374 &image,
375 0, 0, // src x,y
376 0, 0, // dst x,y
377 width, height);
scroggob7e9aee2011-03-15 15:15:15 +0000378}
379
bsalomon@google.com8108c472012-04-03 19:33:08 +0000380bool SkOSWindow::onHandleChar(SkUnichar) {
scroggob7e9aee2011-03-15 15:15:15 +0000381 return false;
382}
383
bsalomon@google.com8108c472012-04-03 19:33:08 +0000384bool SkOSWindow::onHandleKey(SkKey key) {
scroggob7e9aee2011-03-15 15:15:15 +0000385 return false;
386}
387
bsalomon@google.com8108c472012-04-03 19:33:08 +0000388bool SkOSWindow::onHandleKeyUp(SkKey key) {
scroggob7e9aee2011-03-15 15:15:15 +0000389 return false;
390}