blob: 8fe064d87e2b629f0ddba4a9e38d5ab8f7734129 [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>
10#include <X11/keysym.h>
11#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
Scroggob4490c72011-06-17 13:53:05 +000035SkOSWindow::SkOSWindow(void* unused) : INHERITED(), fGLAttached(false), fVi(0)
scroggob7e9aee2011-03-15 15:15:15 +000036{
Scroggo9df214e2011-04-15 14:48:08 +000037 fUnixWindow.fDisplay = XOpenDisplay(NULL);
38 Display* dsp = fUnixWindow.fDisplay;
39 if (dsp) {
40 // Attempt to create a window that supports GL
41 GLint att[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER,
42 GLX_STENCIL_SIZE, 8, None };
Scroggoaed68d92011-06-08 14:26:00 +000043 fVi = glXChooseVisual(dsp, DefaultScreen(dsp), att);
Scroggo9df214e2011-04-15 14:48:08 +000044 if (fVi) {
Scroggoaed68d92011-06-08 14:26:00 +000045 Colormap colorMap = XCreateColormap(dsp, RootWindow(dsp, fVi->screen),
46 fVi->visual, AllocNone);
Scroggo9df214e2011-04-15 14:48:08 +000047 XSetWindowAttributes swa;
Scroggoaed68d92011-06-08 14:26:00 +000048 swa.colormap = colorMap;
Scroggo9df214e2011-04-15 14:48:08 +000049 swa.event_mask = EVENT_MASK;
Scroggoaed68d92011-06-08 14:26:00 +000050 fUnixWindow.fWin = XCreateWindow(dsp, RootWindow(dsp, fVi->screen),
Scroggo9df214e2011-04-15 14:48:08 +000051 0, 0, WIDTH, HEIGHT, 0, fVi->depth,
Scroggoaed68d92011-06-08 14:26:00 +000052 InputOutput, fVi->visual, CWEventMask | CWColormap, &swa);
Scroggo9df214e2011-04-15 14:48:08 +000053
54 } else {
55 // Create a simple window instead. We will not be able to
56 // show GL
57 fUnixWindow.fWin = XCreateSimpleWindow(dsp, DefaultRootWindow(dsp),
58 0, 0, WIDTH, HEIGHT, 0, 0, 0);
59 }
60 mapWindowAndWait();
61 fUnixWindow.fGc = XCreateGC(dsp, fUnixWindow.fWin, 0, NULL);
62 }
63 this->resize(WIDTH, HEIGHT);
Scroggo9df214e2011-04-15 14:48:08 +000064 fUnixWindow.fGLCreated = false;
scroggob7e9aee2011-03-15 15:15:15 +000065}
66
67SkOSWindow::~SkOSWindow()
68{
Scroggo9df214e2011-04-15 14:48:08 +000069 if (fUnixWindow.fDisplay) {
70 if (fGLAttached)
71 glXMakeCurrent(fUnixWindow.fDisplay, None, NULL);
72 XFreeGC(fUnixWindow.fDisplay, fUnixWindow.fGc);
73 if (fUnixWindow.fGLCreated)
74 glXDestroyContext(fUnixWindow.fDisplay, fUnixWindow.fGLContext);
75 XDestroyWindow(fUnixWindow.fDisplay, fUnixWindow.fWin);
76 XCloseDisplay(fUnixWindow.fDisplay);
77 fUnixWindow.fDisplay = 0;
78 }
scroggob7e9aee2011-03-15 15:15:15 +000079}
80
Scroggo9df214e2011-04-15 14:48:08 +000081void SkOSWindow::post_linuxevent()
scroggob7e9aee2011-03-15 15:15:15 +000082{
Scroggo9df214e2011-04-15 14:48:08 +000083 // Put an event in the X queue to fire an SkEvent.
84 if (!fUnixWindow.fDisplay) return;
85 long event_mask = NoEventMask;
86 XClientMessageEvent event;
87 event.type = ClientMessage;
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000088 Atom myAtom(0);
Scroggo9df214e2011-04-15 14:48:08 +000089 event.message_type = myAtom;
90 event.format = 32;
91 event.data.l[0] = 0;
92 XSendEvent(fUnixWindow.fDisplay, fUnixWindow.fWin, false, 0,
93 (XEvent*) &event);
Scroggo5a234242011-06-13 19:17:58 +000094 XFlush(fUnixWindow.fDisplay);
Scroggo9df214e2011-04-15 14:48:08 +000095}
96
Scroggo9df214e2011-04-15 14:48:08 +000097void SkOSWindow::loop()
98{
99 Display* dsp = fUnixWindow.fDisplay;
100 XSelectInput(dsp, fUnixWindow.fWin, EVENT_MASK);
101
102 bool loop = true;
103 XEvent evt;
104 while (loop) {
Scroggo9df214e2011-04-15 14:48:08 +0000105 XNextEvent(dsp, &evt);
106 switch (evt.type) {
107 case Expose:
108 if (evt.xexpose.count == 0)
109 this->inval(NULL);
110 break;
111 case ConfigureNotify:
112 this->resize(evt.xconfigure.width, evt.xconfigure.height);
113 break;
114 case ButtonPress:
115 if (evt.xbutton.button == Button1)
116 this->handleClick(evt.xbutton.x, evt.xbutton.y, SkView::Click::kDown_State);
117 break;
118 case ButtonRelease:
119 if (evt.xbutton.button == Button1)
120 this->handleClick(evt.xbutton.x, evt.xbutton.y, SkView::Click::kUp_State);
121 break;
122 case MotionNotify:
123 this->handleClick(evt.xmotion.x, evt.xmotion.y, SkView::Click::kMoved_State);
124 break;
125 case KeyPress:
126 {
127 KeySym keysym = XKeycodeToKeysym(dsp, evt.xkey.keycode, 0);
128 //SkDebugf("pressed key %i!\n\tKeySym:%i\n", evt.xkey.keycode, XKeycodeToKeysym(dsp, evt.xkey.keycode, 0));
129 if (keysym == XK_Escape) {
130 loop = false;
131 break;
132 }
133 this->handleKey(XKeyToSkKey(keysym));
134 long uni = keysym2ucs(keysym);
135 if (uni != -1) {
136 this->handleChar((SkUnichar) uni);
137 }
138 break;
139 }
140 case KeyRelease:
141 //SkDebugf("released key %i\n", evt.xkey.keycode);
142 this->handleKeyUp(XKeyToSkKey(XKeycodeToKeysym(dsp, evt.xkey.keycode, 0)));
143 break;
144 case ClientMessage:
145 if (SkEvent::ProcessEvent()) {
146 this->post_linuxevent();
147 }
148 break;
149 default:
150 // Do nothing for other events
151 break;
152 }
153 }
154}
155
156void SkOSWindow::mapWindowAndWait()
157{
158 Display* dsp = fUnixWindow.fDisplay;
159 Window win = fUnixWindow.fWin;
160 XMapWindow(dsp, win);
161
162 long eventMask = StructureNotifyMask;
163 XSelectInput(dsp, win, eventMask);
164
165 // Wait until screen is ready.
166 XEvent evt;
167 do {
168 XNextEvent(dsp, &evt);
169 } while(evt.type != MapNotify);
170
scroggob7e9aee2011-03-15 15:15:15 +0000171}
172
bsalomon@google.comc8ad63e2011-03-18 14:29:44 +0000173bool SkOSWindow::attachGL()
scroggob7e9aee2011-03-15 15:15:15 +0000174{
Scroggo9df214e2011-04-15 14:48:08 +0000175 if (fGLAttached) return true;
176 Display* dsp = fUnixWindow.fDisplay;
177 if (!dsp || !fVi) return false;
178
179 if (!fUnixWindow.fGLCreated) {
180 fUnixWindow.fGLContext = glXCreateContext(dsp, fVi, NULL, GL_TRUE);
181 fUnixWindow.fGLCreated = true;
182 glXMakeCurrent(dsp, fUnixWindow.fWin, fUnixWindow.fGLContext);
183 glViewport(0, 0, SkScalarRound(this->width()), SkScalarRound(this->height()));
184 glClearColor(0, 0, 0, 0);
185 glClearStencil(0);
186 glStencilMask(0xffffffff);
187 glDisable(GL_SCISSOR_TEST);
188 glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
189 }
190 else
191 glXMakeCurrent(dsp, fUnixWindow.fWin, fUnixWindow.fGLContext);
192 fGLAttached = true;
193
Scroggo9df214e2011-04-15 14:48:08 +0000194 return true;
scroggob7e9aee2011-03-15 15:15:15 +0000195}
196
197void SkOSWindow::detachGL()
198{
Scroggo9df214e2011-04-15 14:48:08 +0000199 if (!fUnixWindow.fDisplay || !fGLAttached) return;
200 fGLAttached = false;
201 // Returns back to normal drawing.
202 glXMakeCurrent(fUnixWindow.fDisplay, None, NULL);
Scroggo9df214e2011-04-15 14:48:08 +0000203 // Ensure that we redraw when switching back to raster.
204 this->inval(NULL);
scroggob7e9aee2011-03-15 15:15:15 +0000205}
206
207void SkOSWindow::presentGL()
208{
Scroggo9df214e2011-04-15 14:48:08 +0000209 if (fUnixWindow.fDisplay && fGLAttached) {
210 glXSwapBuffers(fUnixWindow.fDisplay, fUnixWindow.fWin);
211 }
scroggob7e9aee2011-03-15 15:15:15 +0000212}
213
214void SkOSWindow::onSetTitle(const char title[])
215{
216 if (!fUnixWindow.fDisplay) return;
217 XTextProperty textProp;
218 textProp.value = (unsigned char*)title;
219 textProp.format = 8;
220 textProp.nitems = strlen((char*)textProp.value);
221 textProp.encoding = XA_STRING;
222 XSetWMName(fUnixWindow.fDisplay, fUnixWindow.fWin, &textProp);
223}
224
225void SkOSWindow::onHandleInval(const SkIRect&)
226{
227 SkEvent* evt = new SkEvent("inval-imageview");
228 evt->post(getSinkID());
229}
230
231bool SkOSWindow::onEvent(const SkEvent& evt)
232{
233 if (evt.isType("inval-imageview")) {
234 update(NULL);
Scroggo9df214e2011-04-15 14:48:08 +0000235 if (!fGLAttached)
236 doPaint();
scroggob7e9aee2011-03-15 15:15:15 +0000237 return true;
238 }
239 return INHERITED::onEvent(evt);
240}
241
scroggo08526c02011-03-22 14:03:21 +0000242static bool convertBitmapToXImage(XImage& image, const SkBitmap& bitmap)
243{
scroggob66365f2011-03-18 21:43:03 +0000244 sk_bzero(&image, sizeof(image));
245
246 int bitsPerPixel = bitmap.bytesPerPixel() * 8;
247 image.width = bitmap.width();
248 image.height = bitmap.height();
249 image.format = ZPixmap;
250 image.data = (char*) bitmap.getPixels();
251 image.byte_order = LSBFirst;
252 image.bitmap_unit = bitsPerPixel;
253 image.bitmap_bit_order = LSBFirst;
254 image.bitmap_pad = bitsPerPixel;
255 image.depth = 24;
256 image.bytes_per_line = bitmap.rowBytes() - bitmap.width() * bitmap.bytesPerPixel();
257 image.bits_per_pixel = bitsPerPixel;
scroggo08526c02011-03-22 14:03:21 +0000258 return XInitImage(&image);
259}
260
scroggo08526c02011-03-22 14:03:21 +0000261void SkOSWindow::doPaint() {
262 if (!fUnixWindow.fDisplay) return;
263 // Draw the bitmap to the screen.
264 const SkBitmap& bitmap = getBitmap();
265 int width = bitmap.width();
266 int height = bitmap.height();
267
Scroggo0f185c22011-03-24 18:35:50 +0000268 XImage image;
269 if (!convertBitmapToXImage(image, bitmap)) return;
scroggo08526c02011-03-22 14:03:21 +0000270
Scroggo0f185c22011-03-24 18:35:50 +0000271 XPutImage(fUnixWindow.fDisplay, fUnixWindow.fWin, fUnixWindow.fGc, &image, 0, 0, 0, 0, width, height);
scroggob7e9aee2011-03-15 15:15:15 +0000272}
273
274bool SkOSWindow::onHandleChar(SkUnichar)
275{
276 return false;
277}
278
279bool SkOSWindow::onHandleKey(SkKey key)
280{
281 return false;
282}
283
284bool SkOSWindow::onHandleKeyUp(SkKey key)
285{
286 return false;
287}