blob: 99d42218340b2cb76eff6a51854297b48105ac66 [file] [log] [blame]
scroggob7e9aee2011-03-15 15:15:15 +00001#include <X11/Xlib.h>
2#include <X11/Xatom.h>
3#include <X11/keysym.h>
4#include <GL/glx.h>
Scroggo9df214e2011-04-15 14:48:08 +00005#include <GL/gl.h>
6#include <GL/glu.h>
scroggob7e9aee2011-03-15 15:15:15 +00007
8#include "SkWindow.h"
9
10#include "SkBitmap.h"
scroggo08526c02011-03-22 14:03:21 +000011#include "SkCanvas.h"
scroggob7e9aee2011-03-15 15:15:15 +000012#include "SkColor.h"
13#include "SkEvent.h"
Scroggo9df214e2011-04-15 14:48:08 +000014#include "SkKey.h"
15#include "SkWindow.h"
16#include "XkeysToSkKeys.h"
17extern "C" {
18 #include "keysym2ucs.h"
19}
20
21const int WIDTH = 1000;
22const int HEIGHT = 1000;
23
24// Determine which events to listen for.
25const long EVENT_MASK = StructureNotifyMask|ButtonPressMask|ButtonReleaseMask
26 |ExposureMask|PointerMotionMask|KeyPressMask|KeyReleaseMask;
scroggob7e9aee2011-03-15 15:15:15 +000027
28SkOSWindow::SkOSWindow(void* unused)
29{
Scroggo9df214e2011-04-15 14:48:08 +000030 fUnixWindow.fDisplay = XOpenDisplay(NULL);
31 Display* dsp = fUnixWindow.fDisplay;
32 if (dsp) {
33 // Attempt to create a window that supports GL
34 GLint att[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER,
35 GLX_STENCIL_SIZE, 8, None };
36 fVi = glXChooseVisual(dsp, 0, att);
37 if (fVi) {
38 XSetWindowAttributes swa;
39 swa.event_mask = EVENT_MASK;
40 fUnixWindow.fWin = XCreateWindow(dsp, DefaultRootWindow(dsp),
41 0, 0, WIDTH, HEIGHT, 0, fVi->depth,
42 InputOutput, fVi->visual, CWEventMask, &swa);
43
44 } else {
45 // Create a simple window instead. We will not be able to
46 // show GL
47 fUnixWindow.fWin = XCreateSimpleWindow(dsp, DefaultRootWindow(dsp),
48 0, 0, WIDTH, HEIGHT, 0, 0, 0);
49 }
50 mapWindowAndWait();
51 fUnixWindow.fGc = XCreateGC(dsp, fUnixWindow.fWin, 0, NULL);
52 }
53 this->resize(WIDTH, HEIGHT);
54 fRestart = false;
55 fUnixWindow.fGLCreated = false;
scroggob7e9aee2011-03-15 15:15:15 +000056}
57
58SkOSWindow::~SkOSWindow()
59{
Scroggo9df214e2011-04-15 14:48:08 +000060 if (fUnixWindow.fDisplay) {
61 if (fGLAttached)
62 glXMakeCurrent(fUnixWindow.fDisplay, None, NULL);
63 XFreeGC(fUnixWindow.fDisplay, fUnixWindow.fGc);
64 if (fUnixWindow.fGLCreated)
65 glXDestroyContext(fUnixWindow.fDisplay, fUnixWindow.fGLContext);
66 XDestroyWindow(fUnixWindow.fDisplay, fUnixWindow.fWin);
67 XCloseDisplay(fUnixWindow.fDisplay);
68 fUnixWindow.fDisplay = 0;
69 }
scroggob7e9aee2011-03-15 15:15:15 +000070}
71
Scroggo9df214e2011-04-15 14:48:08 +000072void SkOSWindow::post_linuxevent()
scroggob7e9aee2011-03-15 15:15:15 +000073{
Scroggo9df214e2011-04-15 14:48:08 +000074 // Put an event in the X queue to fire an SkEvent.
75 if (!fUnixWindow.fDisplay) return;
76 long event_mask = NoEventMask;
77 XClientMessageEvent event;
78 event.type = ClientMessage;
79 Atom myAtom;
80 event.message_type = myAtom;
81 event.format = 32;
82 event.data.l[0] = 0;
83 XSendEvent(fUnixWindow.fDisplay, fUnixWindow.fWin, false, 0,
84 (XEvent*) &event);
85}
86
87void SkOSWindow::restartLoop()
88{
89 // We have a new window, so we need to set the title again and restart the
90 // loop.
91 this->setTitle(this->getTitle());
92 fRestart = true;
93}
94
95void SkOSWindow::loop()
96{
97 Display* dsp = fUnixWindow.fDisplay;
98 XSelectInput(dsp, fUnixWindow.fWin, EVENT_MASK);
99
100 bool loop = true;
101 XEvent evt;
102 while (loop) {
103 if (fRestart) {
104 fRestart = false;
105 this->loop();
106 return;
107 }
108 XNextEvent(dsp, &evt);
109 switch (evt.type) {
110 case Expose:
111 if (evt.xexpose.count == 0)
112 this->inval(NULL);
113 break;
114 case ConfigureNotify:
115 this->resize(evt.xconfigure.width, evt.xconfigure.height);
116 break;
117 case ButtonPress:
118 if (evt.xbutton.button == Button1)
119 this->handleClick(evt.xbutton.x, evt.xbutton.y, SkView::Click::kDown_State);
120 break;
121 case ButtonRelease:
122 if (evt.xbutton.button == Button1)
123 this->handleClick(evt.xbutton.x, evt.xbutton.y, SkView::Click::kUp_State);
124 break;
125 case MotionNotify:
126 this->handleClick(evt.xmotion.x, evt.xmotion.y, SkView::Click::kMoved_State);
127 break;
128 case KeyPress:
129 {
130 KeySym keysym = XKeycodeToKeysym(dsp, evt.xkey.keycode, 0);
131 //SkDebugf("pressed key %i!\n\tKeySym:%i\n", evt.xkey.keycode, XKeycodeToKeysym(dsp, evt.xkey.keycode, 0));
132 if (keysym == XK_Escape) {
133 loop = false;
134 break;
135 }
136 this->handleKey(XKeyToSkKey(keysym));
137 long uni = keysym2ucs(keysym);
138 if (uni != -1) {
139 this->handleChar((SkUnichar) uni);
140 }
141 break;
142 }
143 case KeyRelease:
144 //SkDebugf("released key %i\n", evt.xkey.keycode);
145 this->handleKeyUp(XKeyToSkKey(XKeycodeToKeysym(dsp, evt.xkey.keycode, 0)));
146 break;
147 case ClientMessage:
148 if (SkEvent::ProcessEvent()) {
149 this->post_linuxevent();
150 }
151 break;
152 default:
153 // Do nothing for other events
154 break;
155 }
156 }
157}
158
159void SkOSWindow::mapWindowAndWait()
160{
161 Display* dsp = fUnixWindow.fDisplay;
162 Window win = fUnixWindow.fWin;
163 XMapWindow(dsp, win);
164
165 long eventMask = StructureNotifyMask;
166 XSelectInput(dsp, win, eventMask);
167
168 // Wait until screen is ready.
169 XEvent evt;
170 do {
171 XNextEvent(dsp, &evt);
172 } while(evt.type != MapNotify);
173
scroggob7e9aee2011-03-15 15:15:15 +0000174}
175
bsalomon@google.comc8ad63e2011-03-18 14:29:44 +0000176bool SkOSWindow::attachGL()
scroggob7e9aee2011-03-15 15:15:15 +0000177{
Scroggo9df214e2011-04-15 14:48:08 +0000178 if (fGLAttached) return true;
179 Display* dsp = fUnixWindow.fDisplay;
180 if (!dsp || !fVi) return false;
181
182 if (!fUnixWindow.fGLCreated) {
183 fUnixWindow.fGLContext = glXCreateContext(dsp, fVi, NULL, GL_TRUE);
184 fUnixWindow.fGLCreated = true;
185 glXMakeCurrent(dsp, fUnixWindow.fWin, fUnixWindow.fGLContext);
186 glViewport(0, 0, SkScalarRound(this->width()), SkScalarRound(this->height()));
187 glClearColor(0, 0, 0, 0);
188 glClearStencil(0);
189 glStencilMask(0xffffffff);
190 glDisable(GL_SCISSOR_TEST);
191 glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
192 }
193 else
194 glXMakeCurrent(dsp, fUnixWindow.fWin, fUnixWindow.fGLContext);
195 fGLAttached = true;
196
197
198 this->restartLoop();
199 return true;
scroggob7e9aee2011-03-15 15:15:15 +0000200}
201
202void SkOSWindow::detachGL()
203{
Scroggo9df214e2011-04-15 14:48:08 +0000204 if (!fUnixWindow.fDisplay || !fGLAttached) return;
205 fGLAttached = false;
206 // Returns back to normal drawing.
207 glXMakeCurrent(fUnixWindow.fDisplay, None, NULL);
208 this->restartLoop();
209 // Ensure that we redraw when switching back to raster.
210 this->inval(NULL);
scroggob7e9aee2011-03-15 15:15:15 +0000211}
212
213void SkOSWindow::presentGL()
214{
Scroggo9df214e2011-04-15 14:48:08 +0000215 if (fUnixWindow.fDisplay && fGLAttached) {
216 glXSwapBuffers(fUnixWindow.fDisplay, fUnixWindow.fWin);
217 }
scroggob7e9aee2011-03-15 15:15:15 +0000218}
219
220void SkOSWindow::onSetTitle(const char title[])
221{
222 if (!fUnixWindow.fDisplay) return;
223 XTextProperty textProp;
224 textProp.value = (unsigned char*)title;
225 textProp.format = 8;
226 textProp.nitems = strlen((char*)textProp.value);
227 textProp.encoding = XA_STRING;
228 XSetWMName(fUnixWindow.fDisplay, fUnixWindow.fWin, &textProp);
229}
230
231void SkOSWindow::onHandleInval(const SkIRect&)
232{
233 SkEvent* evt = new SkEvent("inval-imageview");
234 evt->post(getSinkID());
235}
236
237bool SkOSWindow::onEvent(const SkEvent& evt)
238{
239 if (evt.isType("inval-imageview")) {
240 update(NULL);
Scroggo9df214e2011-04-15 14:48:08 +0000241 if (!fGLAttached)
242 doPaint();
scroggob7e9aee2011-03-15 15:15:15 +0000243 return true;
244 }
245 return INHERITED::onEvent(evt);
246}
247
scroggo08526c02011-03-22 14:03:21 +0000248static bool convertBitmapToXImage(XImage& image, const SkBitmap& bitmap)
249{
scroggob66365f2011-03-18 21:43:03 +0000250 sk_bzero(&image, sizeof(image));
251
252 int bitsPerPixel = bitmap.bytesPerPixel() * 8;
253 image.width = bitmap.width();
254 image.height = bitmap.height();
255 image.format = ZPixmap;
256 image.data = (char*) bitmap.getPixels();
257 image.byte_order = LSBFirst;
258 image.bitmap_unit = bitsPerPixel;
259 image.bitmap_bit_order = LSBFirst;
260 image.bitmap_pad = bitsPerPixel;
261 image.depth = 24;
262 image.bytes_per_line = bitmap.rowBytes() - bitmap.width() * bitmap.bytesPerPixel();
263 image.bits_per_pixel = bitsPerPixel;
scroggo08526c02011-03-22 14:03:21 +0000264 return XInitImage(&image);
265}
266
scroggo08526c02011-03-22 14:03:21 +0000267void SkOSWindow::doPaint() {
268 if (!fUnixWindow.fDisplay) return;
269 // Draw the bitmap to the screen.
270 const SkBitmap& bitmap = getBitmap();
271 int width = bitmap.width();
272 int height = bitmap.height();
273
Scroggo0f185c22011-03-24 18:35:50 +0000274 XImage image;
275 if (!convertBitmapToXImage(image, bitmap)) return;
scroggo08526c02011-03-22 14:03:21 +0000276
Scroggo0f185c22011-03-24 18:35:50 +0000277 XPutImage(fUnixWindow.fDisplay, fUnixWindow.fWin, fUnixWindow.fGc, &image, 0, 0, 0, 0, width, height);
scroggob7e9aee2011-03-15 15:15:15 +0000278}
279
280bool SkOSWindow::onHandleChar(SkUnichar)
281{
282 return false;
283}
284
285bool SkOSWindow::onHandleKey(SkKey key)
286{
287 return false;
288}
289
290bool SkOSWindow::onHandleKeyUp(SkKey key)
291{
292 return false;
293}