blob: 2e4ae882123a1c32e8b27030a3e42e5419aaeca6 [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
Scroggoaed68d92011-06-08 14:26:00 +000021const int WIDTH = 500;
22const int HEIGHT = 500;
Scroggo9df214e2011-04-15 14:48:08 +000023
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
senorblanco@chromium.org2dbd0442011-05-04 15:29:04 +000028SkOSWindow::SkOSWindow(void* unused) : fGLAttached(false), fVi(0)
scroggob7e9aee2011-03-15 15:15:15 +000029{
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 };
Scroggoaed68d92011-06-08 14:26:00 +000036 fVi = glXChooseVisual(dsp, DefaultScreen(dsp), att);
Scroggo9df214e2011-04-15 14:48:08 +000037 if (fVi) {
Scroggoaed68d92011-06-08 14:26:00 +000038 Colormap colorMap = XCreateColormap(dsp, RootWindow(dsp, fVi->screen),
39 fVi->visual, AllocNone);
Scroggo9df214e2011-04-15 14:48:08 +000040 XSetWindowAttributes swa;
Scroggoaed68d92011-06-08 14:26:00 +000041 swa.colormap = colorMap;
Scroggo9df214e2011-04-15 14:48:08 +000042 swa.event_mask = EVENT_MASK;
Scroggoaed68d92011-06-08 14:26:00 +000043 fUnixWindow.fWin = XCreateWindow(dsp, RootWindow(dsp, fVi->screen),
Scroggo9df214e2011-04-15 14:48:08 +000044 0, 0, WIDTH, HEIGHT, 0, fVi->depth,
Scroggoaed68d92011-06-08 14:26:00 +000045 InputOutput, fVi->visual, CWEventMask | CWColormap, &swa);
Scroggo9df214e2011-04-15 14:48:08 +000046
47 } else {
48 // Create a simple window instead. We will not be able to
49 // show GL
50 fUnixWindow.fWin = XCreateSimpleWindow(dsp, DefaultRootWindow(dsp),
51 0, 0, WIDTH, HEIGHT, 0, 0, 0);
52 }
53 mapWindowAndWait();
54 fUnixWindow.fGc = XCreateGC(dsp, fUnixWindow.fWin, 0, NULL);
55 }
56 this->resize(WIDTH, HEIGHT);
Scroggo9df214e2011-04-15 14:48:08 +000057 fUnixWindow.fGLCreated = false;
scroggob7e9aee2011-03-15 15:15:15 +000058}
59
60SkOSWindow::~SkOSWindow()
61{
Scroggo9df214e2011-04-15 14:48:08 +000062 if (fUnixWindow.fDisplay) {
63 if (fGLAttached)
64 glXMakeCurrent(fUnixWindow.fDisplay, None, NULL);
65 XFreeGC(fUnixWindow.fDisplay, fUnixWindow.fGc);
66 if (fUnixWindow.fGLCreated)
67 glXDestroyContext(fUnixWindow.fDisplay, fUnixWindow.fGLContext);
68 XDestroyWindow(fUnixWindow.fDisplay, fUnixWindow.fWin);
69 XCloseDisplay(fUnixWindow.fDisplay);
70 fUnixWindow.fDisplay = 0;
71 }
scroggob7e9aee2011-03-15 15:15:15 +000072}
73
Scroggo9df214e2011-04-15 14:48:08 +000074void SkOSWindow::post_linuxevent()
scroggob7e9aee2011-03-15 15:15:15 +000075{
Scroggo9df214e2011-04-15 14:48:08 +000076 // Put an event in the X queue to fire an SkEvent.
77 if (!fUnixWindow.fDisplay) return;
78 long event_mask = NoEventMask;
79 XClientMessageEvent event;
80 event.type = ClientMessage;
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000081 Atom myAtom(0);
Scroggo9df214e2011-04-15 14:48:08 +000082 event.message_type = myAtom;
83 event.format = 32;
84 event.data.l[0] = 0;
85 XSendEvent(fUnixWindow.fDisplay, fUnixWindow.fWin, false, 0,
86 (XEvent*) &event);
Scroggo5a234242011-06-13 19:17:58 +000087 XFlush(fUnixWindow.fDisplay);
Scroggo9df214e2011-04-15 14:48:08 +000088}
89
Scroggo9df214e2011-04-15 14:48:08 +000090void SkOSWindow::loop()
91{
92 Display* dsp = fUnixWindow.fDisplay;
93 XSelectInput(dsp, fUnixWindow.fWin, EVENT_MASK);
94
95 bool loop = true;
96 XEvent evt;
97 while (loop) {
Scroggo9df214e2011-04-15 14:48:08 +000098 XNextEvent(dsp, &evt);
99 switch (evt.type) {
100 case Expose:
101 if (evt.xexpose.count == 0)
102 this->inval(NULL);
103 break;
104 case ConfigureNotify:
105 this->resize(evt.xconfigure.width, evt.xconfigure.height);
106 break;
107 case ButtonPress:
108 if (evt.xbutton.button == Button1)
109 this->handleClick(evt.xbutton.x, evt.xbutton.y, SkView::Click::kDown_State);
110 break;
111 case ButtonRelease:
112 if (evt.xbutton.button == Button1)
113 this->handleClick(evt.xbutton.x, evt.xbutton.y, SkView::Click::kUp_State);
114 break;
115 case MotionNotify:
116 this->handleClick(evt.xmotion.x, evt.xmotion.y, SkView::Click::kMoved_State);
117 break;
118 case KeyPress:
119 {
120 KeySym keysym = XKeycodeToKeysym(dsp, evt.xkey.keycode, 0);
121 //SkDebugf("pressed key %i!\n\tKeySym:%i\n", evt.xkey.keycode, XKeycodeToKeysym(dsp, evt.xkey.keycode, 0));
122 if (keysym == XK_Escape) {
123 loop = false;
124 break;
125 }
126 this->handleKey(XKeyToSkKey(keysym));
127 long uni = keysym2ucs(keysym);
128 if (uni != -1) {
129 this->handleChar((SkUnichar) uni);
130 }
131 break;
132 }
133 case KeyRelease:
134 //SkDebugf("released key %i\n", evt.xkey.keycode);
135 this->handleKeyUp(XKeyToSkKey(XKeycodeToKeysym(dsp, evt.xkey.keycode, 0)));
136 break;
137 case ClientMessage:
138 if (SkEvent::ProcessEvent()) {
139 this->post_linuxevent();
140 }
141 break;
142 default:
143 // Do nothing for other events
144 break;
145 }
146 }
147}
148
149void SkOSWindow::mapWindowAndWait()
150{
151 Display* dsp = fUnixWindow.fDisplay;
152 Window win = fUnixWindow.fWin;
153 XMapWindow(dsp, win);
154
155 long eventMask = StructureNotifyMask;
156 XSelectInput(dsp, win, eventMask);
157
158 // Wait until screen is ready.
159 XEvent evt;
160 do {
161 XNextEvent(dsp, &evt);
162 } while(evt.type != MapNotify);
163
scroggob7e9aee2011-03-15 15:15:15 +0000164}
165
bsalomon@google.comc8ad63e2011-03-18 14:29:44 +0000166bool SkOSWindow::attachGL()
scroggob7e9aee2011-03-15 15:15:15 +0000167{
Scroggo9df214e2011-04-15 14:48:08 +0000168 if (fGLAttached) return true;
169 Display* dsp = fUnixWindow.fDisplay;
170 if (!dsp || !fVi) return false;
171
172 if (!fUnixWindow.fGLCreated) {
173 fUnixWindow.fGLContext = glXCreateContext(dsp, fVi, NULL, GL_TRUE);
174 fUnixWindow.fGLCreated = true;
175 glXMakeCurrent(dsp, fUnixWindow.fWin, fUnixWindow.fGLContext);
176 glViewport(0, 0, SkScalarRound(this->width()), SkScalarRound(this->height()));
177 glClearColor(0, 0, 0, 0);
178 glClearStencil(0);
179 glStencilMask(0xffffffff);
180 glDisable(GL_SCISSOR_TEST);
181 glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
182 }
183 else
184 glXMakeCurrent(dsp, fUnixWindow.fWin, fUnixWindow.fGLContext);
185 fGLAttached = true;
186
Scroggo9df214e2011-04-15 14:48:08 +0000187 return true;
scroggob7e9aee2011-03-15 15:15:15 +0000188}
189
190void SkOSWindow::detachGL()
191{
Scroggo9df214e2011-04-15 14:48:08 +0000192 if (!fUnixWindow.fDisplay || !fGLAttached) return;
193 fGLAttached = false;
194 // Returns back to normal drawing.
195 glXMakeCurrent(fUnixWindow.fDisplay, None, NULL);
Scroggo9df214e2011-04-15 14:48:08 +0000196 // Ensure that we redraw when switching back to raster.
197 this->inval(NULL);
scroggob7e9aee2011-03-15 15:15:15 +0000198}
199
200void SkOSWindow::presentGL()
201{
Scroggo9df214e2011-04-15 14:48:08 +0000202 if (fUnixWindow.fDisplay && fGLAttached) {
203 glXSwapBuffers(fUnixWindow.fDisplay, fUnixWindow.fWin);
204 }
scroggob7e9aee2011-03-15 15:15:15 +0000205}
206
207void SkOSWindow::onSetTitle(const char title[])
208{
209 if (!fUnixWindow.fDisplay) return;
210 XTextProperty textProp;
211 textProp.value = (unsigned char*)title;
212 textProp.format = 8;
213 textProp.nitems = strlen((char*)textProp.value);
214 textProp.encoding = XA_STRING;
215 XSetWMName(fUnixWindow.fDisplay, fUnixWindow.fWin, &textProp);
216}
217
218void SkOSWindow::onHandleInval(const SkIRect&)
219{
220 SkEvent* evt = new SkEvent("inval-imageview");
221 evt->post(getSinkID());
222}
223
224bool SkOSWindow::onEvent(const SkEvent& evt)
225{
226 if (evt.isType("inval-imageview")) {
227 update(NULL);
Scroggo9df214e2011-04-15 14:48:08 +0000228 if (!fGLAttached)
229 doPaint();
scroggob7e9aee2011-03-15 15:15:15 +0000230 return true;
231 }
232 return INHERITED::onEvent(evt);
233}
234
scroggo08526c02011-03-22 14:03:21 +0000235static bool convertBitmapToXImage(XImage& image, const SkBitmap& bitmap)
236{
scroggob66365f2011-03-18 21:43:03 +0000237 sk_bzero(&image, sizeof(image));
238
239 int bitsPerPixel = bitmap.bytesPerPixel() * 8;
240 image.width = bitmap.width();
241 image.height = bitmap.height();
242 image.format = ZPixmap;
243 image.data = (char*) bitmap.getPixels();
244 image.byte_order = LSBFirst;
245 image.bitmap_unit = bitsPerPixel;
246 image.bitmap_bit_order = LSBFirst;
247 image.bitmap_pad = bitsPerPixel;
248 image.depth = 24;
249 image.bytes_per_line = bitmap.rowBytes() - bitmap.width() * bitmap.bytesPerPixel();
250 image.bits_per_pixel = bitsPerPixel;
scroggo08526c02011-03-22 14:03:21 +0000251 return XInitImage(&image);
252}
253
scroggo08526c02011-03-22 14:03:21 +0000254void SkOSWindow::doPaint() {
255 if (!fUnixWindow.fDisplay) return;
256 // Draw the bitmap to the screen.
257 const SkBitmap& bitmap = getBitmap();
258 int width = bitmap.width();
259 int height = bitmap.height();
260
Scroggo0f185c22011-03-24 18:35:50 +0000261 XImage image;
262 if (!convertBitmapToXImage(image, bitmap)) return;
scroggo08526c02011-03-22 14:03:21 +0000263
Scroggo0f185c22011-03-24 18:35:50 +0000264 XPutImage(fUnixWindow.fDisplay, fUnixWindow.fWin, fUnixWindow.fGc, &image, 0, 0, 0, 0, width, height);
scroggob7e9aee2011-03-15 15:15:15 +0000265}
266
267bool SkOSWindow::onHandleChar(SkUnichar)
268{
269 return false;
270}
271
272bool SkOSWindow::onHandleKey(SkKey key)
273{
274 return false;
275}
276
277bool SkOSWindow::onHandleKeyUp(SkKey key)
278{
279 return false;
280}