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 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame^] | 35 | SkOSWindow::SkOSWindow(void* unused) |
| 36 | : INHERITED() |
| 37 | , fVi(0) { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 38 | fUnixWindow.fDisplay = XOpenDisplay(NULL); |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame^] | 39 | fUnixWindow.fGLContext = NULL; |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 40 | Display* dsp = fUnixWindow.fDisplay; |
| 41 | if (dsp) { |
| 42 | // Attempt to create a window that supports GL |
| 43 | GLint att[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, |
| 44 | GLX_STENCIL_SIZE, 8, None }; |
Scroggo | aed68d9 | 2011-06-08 14:26:00 +0000 | [diff] [blame] | 45 | fVi = glXChooseVisual(dsp, DefaultScreen(dsp), att); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 46 | if (fVi) { |
Scroggo | aed68d9 | 2011-06-08 14:26:00 +0000 | [diff] [blame] | 47 | Colormap colorMap = XCreateColormap(dsp, RootWindow(dsp, fVi->screen), |
| 48 | fVi->visual, AllocNone); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 49 | XSetWindowAttributes swa; |
Scroggo | aed68d9 | 2011-06-08 14:26:00 +0000 | [diff] [blame] | 50 | swa.colormap = colorMap; |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 51 | swa.event_mask = EVENT_MASK; |
Scroggo | aed68d9 | 2011-06-08 14:26:00 +0000 | [diff] [blame] | 52 | fUnixWindow.fWin = XCreateWindow(dsp, RootWindow(dsp, fVi->screen), |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 53 | 0, 0, WIDTH, HEIGHT, 0, fVi->depth, |
Scroggo | aed68d9 | 2011-06-08 14:26:00 +0000 | [diff] [blame] | 54 | InputOutput, fVi->visual, CWEventMask | CWColormap, &swa); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 55 | |
| 56 | } else { |
| 57 | // Create a simple window instead. We will not be able to |
| 58 | // show GL |
| 59 | fUnixWindow.fWin = XCreateSimpleWindow(dsp, DefaultRootWindow(dsp), |
| 60 | 0, 0, WIDTH, HEIGHT, 0, 0, 0); |
| 61 | } |
| 62 | mapWindowAndWait(); |
| 63 | fUnixWindow.fGc = XCreateGC(dsp, fUnixWindow.fWin, 0, NULL); |
| 64 | } |
| 65 | this->resize(WIDTH, HEIGHT); |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 66 | } |
| 67 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame^] | 68 | SkOSWindow::~SkOSWindow() { |
| 69 | if (NULL != fUnixWindow.fDisplay) { |
| 70 | if (NULL != fUnixWindow.fGLContext) { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 71 | glXMakeCurrent(fUnixWindow.fDisplay, None, NULL); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 72 | glXDestroyContext(fUnixWindow.fDisplay, fUnixWindow.fGLContext); |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame^] | 73 | } |
| 74 | XFreeGC(fUnixWindow.fDisplay, fUnixWindow.fGc); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 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 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame^] | 81 | void SkOSWindow::post_linuxevent() { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 82 | // Put an event in the X queue to fire an SkEvent. |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame^] | 83 | if (!fUnixWindow.fDisplay) { |
| 84 | return; |
| 85 | } |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 86 | long event_mask = NoEventMask; |
| 87 | XClientMessageEvent event; |
| 88 | event.type = ClientMessage; |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 89 | Atom myAtom(0); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 90 | event.message_type = myAtom; |
| 91 | event.format = 32; |
| 92 | event.data.l[0] = 0; |
| 93 | XSendEvent(fUnixWindow.fDisplay, fUnixWindow.fWin, false, 0, |
| 94 | (XEvent*) &event); |
Scroggo | 5a23424 | 2011-06-13 19:17:58 +0000 | [diff] [blame] | 95 | XFlush(fUnixWindow.fDisplay); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 96 | } |
| 97 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame^] | 98 | void SkOSWindow::loop() { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 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; |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame^] | 125 | case KeyPress: { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 126 | KeySym keysym = XKeycodeToKeysym(dsp, evt.xkey.keycode, 0); |
| 127 | //SkDebugf("pressed key %i!\n\tKeySym:%i\n", evt.xkey.keycode, XKeycodeToKeysym(dsp, evt.xkey.keycode, 0)); |
| 128 | if (keysym == XK_Escape) { |
| 129 | loop = false; |
| 130 | break; |
| 131 | } |
| 132 | this->handleKey(XKeyToSkKey(keysym)); |
| 133 | long uni = keysym2ucs(keysym); |
| 134 | if (uni != -1) { |
| 135 | this->handleChar((SkUnichar) uni); |
| 136 | } |
| 137 | break; |
| 138 | } |
| 139 | case KeyRelease: |
| 140 | //SkDebugf("released key %i\n", evt.xkey.keycode); |
| 141 | this->handleKeyUp(XKeyToSkKey(XKeycodeToKeysym(dsp, evt.xkey.keycode, 0))); |
| 142 | break; |
| 143 | case ClientMessage: |
| 144 | if (SkEvent::ProcessEvent()) { |
| 145 | this->post_linuxevent(); |
| 146 | } |
| 147 | break; |
| 148 | default: |
| 149 | // Do nothing for other events |
| 150 | break; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame^] | 155 | void SkOSWindow::mapWindowAndWait() { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 156 | Display* dsp = fUnixWindow.fDisplay; |
| 157 | Window win = fUnixWindow.fWin; |
| 158 | XMapWindow(dsp, win); |
| 159 | |
| 160 | long eventMask = StructureNotifyMask; |
| 161 | XSelectInput(dsp, win, eventMask); |
| 162 | |
| 163 | // Wait until screen is ready. |
| 164 | XEvent evt; |
| 165 | do { |
| 166 | XNextEvent(dsp, &evt); |
| 167 | } while(evt.type != MapNotify); |
| 168 | |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 169 | } |
| 170 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame^] | 171 | bool SkOSWindow::attach(SkBackEndTypes /* attachType */) { |
| 172 | if (NULL == fUnixWindow.fGLContext) { |
| 173 | if (NULL == fUnixWindow.fDisplay || NULL == fVi) { |
| 174 | return false; |
| 175 | } |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 176 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame^] | 177 | fUnixWindow.fGLContext = glXCreateContext(fUnixWindow.fDisplay, |
| 178 | fVi, |
| 179 | NULL, |
| 180 | GL_TRUE); |
| 181 | if (NULL == fUnixWindow.fGLContext) { |
| 182 | return false; |
| 183 | } |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 184 | } |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame^] | 185 | glXMakeCurrent(fUnixWindow.fDisplay, |
| 186 | fUnixWindow.fWin, |
| 187 | fUnixWindow.fGLContext); |
| 188 | glViewport(0, 0, |
| 189 | SkScalarRound(this->width()), SkScalarRound(this->height())); |
| 190 | glClearColor(0, 0, 0, 0); |
| 191 | glClearStencil(0); |
| 192 | glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 193 | return true; |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 194 | } |
| 195 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame^] | 196 | void SkOSWindow::detach() { |
| 197 | if (NULL == fUnixWindow.fDisplay || NULL == fUnixWindow.fGLContext) { |
| 198 | return; |
| 199 | } |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 200 | glXMakeCurrent(fUnixWindow.fDisplay, None, NULL); |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame^] | 201 | glXDestroyContext(fUnixWindow.fDisplay, fUnixWindow.fGLContext); |
| 202 | fUnixWindow.fGLContext = NULL; |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 203 | } |
| 204 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame^] | 205 | void SkOSWindow::present() { |
| 206 | if (NULL != fUnixWindow.fDisplay && NULL != fUnixWindow.fGLContext) { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 207 | glXSwapBuffers(fUnixWindow.fDisplay, fUnixWindow.fWin); |
| 208 | } |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 209 | } |
| 210 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame^] | 211 | void SkOSWindow::onSetTitle(const char title[]) { |
| 212 | if (!fUnixWindow.fDisplay) { |
| 213 | return; |
| 214 | } |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 215 | XTextProperty textProp; |
| 216 | textProp.value = (unsigned char*)title; |
| 217 | textProp.format = 8; |
| 218 | textProp.nitems = strlen((char*)textProp.value); |
| 219 | textProp.encoding = XA_STRING; |
| 220 | XSetWMName(fUnixWindow.fDisplay, fUnixWindow.fWin, &textProp); |
| 221 | } |
| 222 | |
reed@google.com | f2164b2 | 2011-08-04 13:57:56 +0000 | [diff] [blame] | 223 | void SkOSWindow::onHandleInval(const SkIRect&) { |
| 224 | (new SkEvent("inval-imageview", this->getSinkID()))->post(); |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 225 | } |
| 226 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame^] | 227 | bool SkOSWindow::onEvent(const SkEvent& evt) { |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 228 | if (evt.isType("inval-imageview")) { |
| 229 | update(NULL); |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame^] | 230 | if (NULL == fUnixWindow.fGLContext) |
| 231 | this->doPaint(); |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 232 | return true; |
| 233 | } |
| 234 | return INHERITED::onEvent(evt); |
| 235 | } |
| 236 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame^] | 237 | static bool convertBitmapToXImage(XImage& image, const SkBitmap& bitmap) { |
scroggo | b66365f | 2011-03-18 21:43:03 +0000 | [diff] [blame] | 238 | sk_bzero(&image, sizeof(image)); |
| 239 | |
| 240 | int bitsPerPixel = bitmap.bytesPerPixel() * 8; |
| 241 | image.width = bitmap.width(); |
| 242 | image.height = bitmap.height(); |
| 243 | image.format = ZPixmap; |
| 244 | image.data = (char*) bitmap.getPixels(); |
| 245 | image.byte_order = LSBFirst; |
| 246 | image.bitmap_unit = bitsPerPixel; |
| 247 | image.bitmap_bit_order = LSBFirst; |
| 248 | image.bitmap_pad = bitsPerPixel; |
| 249 | image.depth = 24; |
| 250 | image.bytes_per_line = bitmap.rowBytes() - bitmap.width() * bitmap.bytesPerPixel(); |
| 251 | image.bits_per_pixel = bitsPerPixel; |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 252 | return XInitImage(&image); |
| 253 | } |
| 254 | |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 255 | void SkOSWindow::doPaint() { |
| 256 | if (!fUnixWindow.fDisplay) return; |
| 257 | // Draw the bitmap to the screen. |
| 258 | const SkBitmap& bitmap = getBitmap(); |
| 259 | int width = bitmap.width(); |
| 260 | int height = bitmap.height(); |
| 261 | |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 262 | XImage image; |
| 263 | if (!convertBitmapToXImage(image, bitmap)) return; |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 264 | |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 265 | 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] | 266 | } |
| 267 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame^] | 268 | bool SkOSWindow::onHandleChar(SkUnichar) { |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 269 | return false; |
| 270 | } |
| 271 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame^] | 272 | bool SkOSWindow::onHandleKey(SkKey key) { |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 273 | return false; |
| 274 | } |
| 275 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame^] | 276 | bool SkOSWindow::onHandleKeyUp(SkKey key) { |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 277 | return false; |
| 278 | } |