scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 1 | #include <X11/Xlib.h> |
| 2 | #include <X11/Xatom.h> |
| 3 | #include <X11/keysym.h> |
| 4 | #include <GL/glx.h> |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 5 | #include <GL/gl.h> |
| 6 | #include <GL/glu.h> |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 7 | |
| 8 | #include "SkWindow.h" |
| 9 | |
| 10 | #include "SkBitmap.h" |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 11 | #include "SkCanvas.h" |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 12 | #include "SkColor.h" |
| 13 | #include "SkEvent.h" |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 14 | #include "SkKey.h" |
| 15 | #include "SkWindow.h" |
| 16 | #include "XkeysToSkKeys.h" |
| 17 | extern "C" { |
| 18 | #include "keysym2ucs.h" |
| 19 | } |
| 20 | |
| 21 | const int WIDTH = 1000; |
| 22 | const int HEIGHT = 1000; |
| 23 | |
| 24 | // Determine which events to listen for. |
| 25 | const long EVENT_MASK = StructureNotifyMask|ButtonPressMask|ButtonReleaseMask |
| 26 | |ExposureMask|PointerMotionMask|KeyPressMask|KeyReleaseMask; |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 27 | |
senorblanco@chromium.org | 2dbd044 | 2011-05-04 15:29:04 +0000 | [diff] [blame] | 28 | SkOSWindow::SkOSWindow(void* unused) : fGLAttached(false), fVi(0) |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 29 | { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 30 | 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; |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | SkOSWindow::~SkOSWindow() |
| 59 | { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 60 | 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 | } |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 72 | void SkOSWindow::post_linuxevent() |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 73 | { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 74 | // 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; |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 79 | Atom myAtom(0); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 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 | |
| 87 | void SkOSWindow::restartLoop() |
| 88 | { |
| 89 | // We have a new window, so we need to set the title again and restart the |
| 90 | // loop. |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 91 | this->onSetTitle(this->getTitle()); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 92 | fRestart = true; |
| 93 | } |
| 94 | |
| 95 | void 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 | |
| 159 | void 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 | |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 174 | } |
| 175 | |
bsalomon@google.com | c8ad63e | 2011-03-18 14:29:44 +0000 | [diff] [blame] | 176 | bool SkOSWindow::attachGL() |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 177 | { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 178 | 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; |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void SkOSWindow::detachGL() |
| 203 | { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 204 | 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); |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | void SkOSWindow::presentGL() |
| 214 | { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 215 | if (fUnixWindow.fDisplay && fGLAttached) { |
| 216 | glXSwapBuffers(fUnixWindow.fDisplay, fUnixWindow.fWin); |
| 217 | } |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | void 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 | |
| 231 | void SkOSWindow::onHandleInval(const SkIRect&) |
| 232 | { |
| 233 | SkEvent* evt = new SkEvent("inval-imageview"); |
| 234 | evt->post(getSinkID()); |
| 235 | } |
| 236 | |
| 237 | bool SkOSWindow::onEvent(const SkEvent& evt) |
| 238 | { |
| 239 | if (evt.isType("inval-imageview")) { |
| 240 | update(NULL); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 241 | if (!fGLAttached) |
| 242 | doPaint(); |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 243 | return true; |
| 244 | } |
| 245 | return INHERITED::onEvent(evt); |
| 246 | } |
| 247 | |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 248 | static bool convertBitmapToXImage(XImage& image, const SkBitmap& bitmap) |
| 249 | { |
scroggo | b66365f | 2011-03-18 21:43:03 +0000 | [diff] [blame] | 250 | 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; |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 264 | return XInitImage(&image); |
| 265 | } |
| 266 | |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 267 | void 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 | |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 274 | XImage image; |
| 275 | if (!convertBitmapToXImage(image, bitmap)) return; |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 276 | |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 277 | 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] | 278 | } |
| 279 | |
| 280 | bool SkOSWindow::onHandleChar(SkUnichar) |
| 281 | { |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | bool SkOSWindow::onHandleKey(SkKey key) |
| 286 | { |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | bool SkOSWindow::onHandleKeyUp(SkKey key) |
| 291 | { |
| 292 | return false; |
| 293 | } |