blob: 652a1ae6d1c83349da8d7f51bb7f248f2d5546ed [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);
87}
88
Scroggo9df214e2011-04-15 14:48:08 +000089void SkOSWindow::loop()
90{
91 Display* dsp = fUnixWindow.fDisplay;
92 XSelectInput(dsp, fUnixWindow.fWin, EVENT_MASK);
93
94 bool loop = true;
95 XEvent evt;
96 while (loop) {
Scroggo9df214e2011-04-15 14:48:08 +000097 XNextEvent(dsp, &evt);
98 switch (evt.type) {
99 case Expose:
100 if (evt.xexpose.count == 0)
101 this->inval(NULL);
102 break;
103 case ConfigureNotify:
104 this->resize(evt.xconfigure.width, evt.xconfigure.height);
105 break;
106 case ButtonPress:
107 if (evt.xbutton.button == Button1)
108 this->handleClick(evt.xbutton.x, evt.xbutton.y, SkView::Click::kDown_State);
109 break;
110 case ButtonRelease:
111 if (evt.xbutton.button == Button1)
112 this->handleClick(evt.xbutton.x, evt.xbutton.y, SkView::Click::kUp_State);
113 break;
114 case MotionNotify:
115 this->handleClick(evt.xmotion.x, evt.xmotion.y, SkView::Click::kMoved_State);
116 break;
117 case KeyPress:
118 {
119 KeySym keysym = XKeycodeToKeysym(dsp, evt.xkey.keycode, 0);
120 //SkDebugf("pressed key %i!\n\tKeySym:%i\n", evt.xkey.keycode, XKeycodeToKeysym(dsp, evt.xkey.keycode, 0));
121 if (keysym == XK_Escape) {
122 loop = false;
123 break;
124 }
125 this->handleKey(XKeyToSkKey(keysym));
126 long uni = keysym2ucs(keysym);
127 if (uni != -1) {
128 this->handleChar((SkUnichar) uni);
129 }
130 break;
131 }
132 case KeyRelease:
133 //SkDebugf("released key %i\n", evt.xkey.keycode);
134 this->handleKeyUp(XKeyToSkKey(XKeycodeToKeysym(dsp, evt.xkey.keycode, 0)));
135 break;
136 case ClientMessage:
137 if (SkEvent::ProcessEvent()) {
138 this->post_linuxevent();
139 }
140 break;
141 default:
142 // Do nothing for other events
143 break;
144 }
145 }
146}
147
148void SkOSWindow::mapWindowAndWait()
149{
150 Display* dsp = fUnixWindow.fDisplay;
151 Window win = fUnixWindow.fWin;
152 XMapWindow(dsp, win);
153
154 long eventMask = StructureNotifyMask;
155 XSelectInput(dsp, win, eventMask);
156
157 // Wait until screen is ready.
158 XEvent evt;
159 do {
160 XNextEvent(dsp, &evt);
161 } while(evt.type != MapNotify);
162
scroggob7e9aee2011-03-15 15:15:15 +0000163}
164
bsalomon@google.comc8ad63e2011-03-18 14:29:44 +0000165bool SkOSWindow::attachGL()
scroggob7e9aee2011-03-15 15:15:15 +0000166{
Scroggo9df214e2011-04-15 14:48:08 +0000167 if (fGLAttached) return true;
168 Display* dsp = fUnixWindow.fDisplay;
169 if (!dsp || !fVi) return false;
170
171 if (!fUnixWindow.fGLCreated) {
172 fUnixWindow.fGLContext = glXCreateContext(dsp, fVi, NULL, GL_TRUE);
173 fUnixWindow.fGLCreated = true;
174 glXMakeCurrent(dsp, fUnixWindow.fWin, fUnixWindow.fGLContext);
175 glViewport(0, 0, SkScalarRound(this->width()), SkScalarRound(this->height()));
176 glClearColor(0, 0, 0, 0);
177 glClearStencil(0);
178 glStencilMask(0xffffffff);
179 glDisable(GL_SCISSOR_TEST);
180 glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
181 }
182 else
183 glXMakeCurrent(dsp, fUnixWindow.fWin, fUnixWindow.fGLContext);
184 fGLAttached = true;
185
Scroggo9df214e2011-04-15 14:48:08 +0000186 return true;
scroggob7e9aee2011-03-15 15:15:15 +0000187}
188
189void SkOSWindow::detachGL()
190{
Scroggo9df214e2011-04-15 14:48:08 +0000191 if (!fUnixWindow.fDisplay || !fGLAttached) return;
192 fGLAttached = false;
193 // Returns back to normal drawing.
194 glXMakeCurrent(fUnixWindow.fDisplay, None, NULL);
Scroggo9df214e2011-04-15 14:48:08 +0000195 // Ensure that we redraw when switching back to raster.
196 this->inval(NULL);
scroggob7e9aee2011-03-15 15:15:15 +0000197}
198
199void SkOSWindow::presentGL()
200{
Scroggo9df214e2011-04-15 14:48:08 +0000201 if (fUnixWindow.fDisplay && fGLAttached) {
202 glXSwapBuffers(fUnixWindow.fDisplay, fUnixWindow.fWin);
203 }
scroggob7e9aee2011-03-15 15:15:15 +0000204}
205
206void SkOSWindow::onSetTitle(const char title[])
207{
208 if (!fUnixWindow.fDisplay) return;
209 XTextProperty textProp;
210 textProp.value = (unsigned char*)title;
211 textProp.format = 8;
212 textProp.nitems = strlen((char*)textProp.value);
213 textProp.encoding = XA_STRING;
214 XSetWMName(fUnixWindow.fDisplay, fUnixWindow.fWin, &textProp);
215}
216
217void SkOSWindow::onHandleInval(const SkIRect&)
218{
219 SkEvent* evt = new SkEvent("inval-imageview");
220 evt->post(getSinkID());
221}
222
223bool SkOSWindow::onEvent(const SkEvent& evt)
224{
225 if (evt.isType("inval-imageview")) {
226 update(NULL);
Scroggo9df214e2011-04-15 14:48:08 +0000227 if (!fGLAttached)
228 doPaint();
scroggob7e9aee2011-03-15 15:15:15 +0000229 return true;
230 }
231 return INHERITED::onEvent(evt);
232}
233
scroggo08526c02011-03-22 14:03:21 +0000234static bool convertBitmapToXImage(XImage& image, const SkBitmap& bitmap)
235{
scroggob66365f2011-03-18 21:43:03 +0000236 sk_bzero(&image, sizeof(image));
237
238 int bitsPerPixel = bitmap.bytesPerPixel() * 8;
239 image.width = bitmap.width();
240 image.height = bitmap.height();
241 image.format = ZPixmap;
242 image.data = (char*) bitmap.getPixels();
243 image.byte_order = LSBFirst;
244 image.bitmap_unit = bitsPerPixel;
245 image.bitmap_bit_order = LSBFirst;
246 image.bitmap_pad = bitsPerPixel;
247 image.depth = 24;
248 image.bytes_per_line = bitmap.rowBytes() - bitmap.width() * bitmap.bytesPerPixel();
249 image.bits_per_pixel = bitsPerPixel;
scroggo08526c02011-03-22 14:03:21 +0000250 return XInitImage(&image);
251}
252
scroggo08526c02011-03-22 14:03:21 +0000253void SkOSWindow::doPaint() {
254 if (!fUnixWindow.fDisplay) return;
255 // Draw the bitmap to the screen.
256 const SkBitmap& bitmap = getBitmap();
257 int width = bitmap.width();
258 int height = bitmap.height();
259
Scroggo0f185c22011-03-24 18:35:50 +0000260 XImage image;
261 if (!convertBitmapToXImage(image, bitmap)) return;
scroggo08526c02011-03-22 14:03:21 +0000262
Scroggo0f185c22011-03-24 18:35:50 +0000263 XPutImage(fUnixWindow.fDisplay, fUnixWindow.fWin, fUnixWindow.fGc, &image, 0, 0, 0, 0, width, height);
scroggob7e9aee2011-03-15 15:15:15 +0000264}
265
266bool SkOSWindow::onHandleChar(SkUnichar)
267{
268 return false;
269}
270
271bool SkOSWindow::onHandleKey(SkKey key)
272{
273 return false;
274}
275
276bool SkOSWindow::onHandleKeyUp(SkKey key)
277{
278 return false;
279}