epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 1 | |
| 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 | */ |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 8 | #include <X11/Xlib.h> |
| 9 | #include <X11/Xatom.h> |
| 10 | #include <X11/keysym.h> |
| 11 | #include <GL/glx.h> |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 12 | #include <GL/gl.h> |
| 13 | #include <GL/glu.h> |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 14 | |
| 15 | #include "SkWindow.h" |
| 16 | |
| 17 | #include "SkBitmap.h" |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 18 | #include "SkCanvas.h" |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 19 | #include "SkColor.h" |
| 20 | #include "SkEvent.h" |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 21 | #include "SkKey.h" |
| 22 | #include "SkWindow.h" |
| 23 | #include "XkeysToSkKeys.h" |
| 24 | extern "C" { |
| 25 | #include "keysym2ucs.h" |
| 26 | } |
| 27 | |
Scroggo | aed68d9 | 2011-06-08 14:26:00 +0000 | [diff] [blame] | 28 | const int WIDTH = 500; |
| 29 | const int HEIGHT = 500; |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 30 | |
| 31 | // Determine which events to listen for. |
| 32 | const long EVENT_MASK = StructureNotifyMask|ButtonPressMask|ButtonReleaseMask |
| 33 | |ExposureMask|PointerMotionMask|KeyPressMask|KeyReleaseMask; |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 34 | |
Scroggo | b4490c7 | 2011-06-17 13:53:05 +0000 | [diff] [blame] | 35 | SkOSWindow::SkOSWindow(void* unused) : INHERITED(), fGLAttached(false), fVi(0) |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 36 | { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 37 | 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 }; |
Scroggo | aed68d9 | 2011-06-08 14:26:00 +0000 | [diff] [blame] | 43 | fVi = glXChooseVisual(dsp, DefaultScreen(dsp), att); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 44 | if (fVi) { |
Scroggo | aed68d9 | 2011-06-08 14:26:00 +0000 | [diff] [blame] | 45 | Colormap colorMap = XCreateColormap(dsp, RootWindow(dsp, fVi->screen), |
| 46 | fVi->visual, AllocNone); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 47 | XSetWindowAttributes swa; |
Scroggo | aed68d9 | 2011-06-08 14:26:00 +0000 | [diff] [blame] | 48 | swa.colormap = colorMap; |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 49 | swa.event_mask = EVENT_MASK; |
Scroggo | aed68d9 | 2011-06-08 14:26:00 +0000 | [diff] [blame] | 50 | fUnixWindow.fWin = XCreateWindow(dsp, RootWindow(dsp, fVi->screen), |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 51 | 0, 0, WIDTH, HEIGHT, 0, fVi->depth, |
Scroggo | aed68d9 | 2011-06-08 14:26:00 +0000 | [diff] [blame] | 52 | InputOutput, fVi->visual, CWEventMask | CWColormap, &swa); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 53 | |
| 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); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 64 | fUnixWindow.fGLCreated = false; |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | SkOSWindow::~SkOSWindow() |
| 68 | { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 69 | 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 | } |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 81 | void SkOSWindow::post_linuxevent() |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 82 | { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 83 | // 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.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 88 | Atom myAtom(0); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 89 | 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); |
Scroggo | 5a23424 | 2011-06-13 19:17:58 +0000 | [diff] [blame] | 94 | XFlush(fUnixWindow.fDisplay); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 97 | void 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) { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 105 | 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 | |
| 156 | void 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 | |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 171 | } |
| 172 | |
bsalomon@google.com | c8ad63e | 2011-03-18 14:29:44 +0000 | [diff] [blame] | 173 | bool SkOSWindow::attachGL() |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 174 | { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 175 | 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 | |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 194 | return true; |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void SkOSWindow::detachGL() |
| 198 | { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 199 | if (!fUnixWindow.fDisplay || !fGLAttached) return; |
| 200 | fGLAttached = false; |
| 201 | // Returns back to normal drawing. |
| 202 | glXMakeCurrent(fUnixWindow.fDisplay, None, NULL); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 203 | // Ensure that we redraw when switching back to raster. |
| 204 | this->inval(NULL); |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void SkOSWindow::presentGL() |
| 208 | { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 209 | if (fUnixWindow.fDisplay && fGLAttached) { |
| 210 | glXSwapBuffers(fUnixWindow.fDisplay, fUnixWindow.fWin); |
| 211 | } |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void 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 | |
| 225 | void SkOSWindow::onHandleInval(const SkIRect&) |
| 226 | { |
| 227 | SkEvent* evt = new SkEvent("inval-imageview"); |
| 228 | evt->post(getSinkID()); |
| 229 | } |
| 230 | |
| 231 | bool SkOSWindow::onEvent(const SkEvent& evt) |
| 232 | { |
| 233 | if (evt.isType("inval-imageview")) { |
| 234 | update(NULL); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 235 | if (!fGLAttached) |
| 236 | doPaint(); |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 237 | return true; |
| 238 | } |
| 239 | return INHERITED::onEvent(evt); |
| 240 | } |
| 241 | |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 242 | static bool convertBitmapToXImage(XImage& image, const SkBitmap& bitmap) |
| 243 | { |
scroggo | b66365f | 2011-03-18 21:43:03 +0000 | [diff] [blame] | 244 | 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; |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 258 | return XInitImage(&image); |
| 259 | } |
| 260 | |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 261 | void 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 | |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 268 | XImage image; |
| 269 | if (!convertBitmapToXImage(image, bitmap)) return; |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 270 | |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 271 | XPutImage(fUnixWindow.fDisplay, fUnixWindow.fWin, fUnixWindow.fGc, &image, 0, 0, 0, 0, width, height); |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | bool SkOSWindow::onHandleChar(SkUnichar) |
| 275 | { |
| 276 | return false; |
| 277 | } |
| 278 | |
| 279 | bool SkOSWindow::onHandleKey(SkKey key) |
| 280 | { |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | bool SkOSWindow::onHandleKeyUp(SkKey key) |
| 285 | { |
| 286 | return false; |
| 287 | } |