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 | |
Scroggo | aed68d9 | 2011-06-08 14:26:00 +0000 | [diff] [blame] | 21 | const int WIDTH = 500; |
| 22 | const int HEIGHT = 500; |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 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 }; |
Scroggo | aed68d9 | 2011-06-08 14:26:00 +0000 | [diff] [blame] | 36 | fVi = glXChooseVisual(dsp, DefaultScreen(dsp), att); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 37 | if (fVi) { |
Scroggo | aed68d9 | 2011-06-08 14:26:00 +0000 | [diff] [blame] | 38 | Colormap colorMap = XCreateColormap(dsp, RootWindow(dsp, fVi->screen), |
| 39 | fVi->visual, AllocNone); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 40 | XSetWindowAttributes swa; |
Scroggo | aed68d9 | 2011-06-08 14:26:00 +0000 | [diff] [blame] | 41 | swa.colormap = colorMap; |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 42 | swa.event_mask = EVENT_MASK; |
Scroggo | aed68d9 | 2011-06-08 14:26:00 +0000 | [diff] [blame] | 43 | fUnixWindow.fWin = XCreateWindow(dsp, RootWindow(dsp, fVi->screen), |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 44 | 0, 0, WIDTH, HEIGHT, 0, fVi->depth, |
Scroggo | aed68d9 | 2011-06-08 14:26:00 +0000 | [diff] [blame] | 45 | InputOutput, fVi->visual, CWEventMask | CWColormap, &swa); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 46 | |
| 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); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 57 | fUnixWindow.fGLCreated = false; |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | SkOSWindow::~SkOSWindow() |
| 61 | { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 62 | 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 | } |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 74 | void SkOSWindow::post_linuxevent() |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 75 | { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 76 | // 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.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 81 | Atom myAtom(0); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 82 | 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); |
Scroggo | 5a23424 | 2011-06-13 19:17:58 +0000 | [diff] [blame^] | 87 | XFlush(fUnixWindow.fDisplay); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 90 | void 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) { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 98 | 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 | |
| 149 | void 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 | |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 164 | } |
| 165 | |
bsalomon@google.com | c8ad63e | 2011-03-18 14:29:44 +0000 | [diff] [blame] | 166 | bool SkOSWindow::attachGL() |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 167 | { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 168 | 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 | |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 187 | return true; |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void SkOSWindow::detachGL() |
| 191 | { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 192 | if (!fUnixWindow.fDisplay || !fGLAttached) return; |
| 193 | fGLAttached = false; |
| 194 | // Returns back to normal drawing. |
| 195 | glXMakeCurrent(fUnixWindow.fDisplay, None, NULL); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 196 | // Ensure that we redraw when switching back to raster. |
| 197 | this->inval(NULL); |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void SkOSWindow::presentGL() |
| 201 | { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 202 | if (fUnixWindow.fDisplay && fGLAttached) { |
| 203 | glXSwapBuffers(fUnixWindow.fDisplay, fUnixWindow.fWin); |
| 204 | } |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void 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 | |
| 218 | void SkOSWindow::onHandleInval(const SkIRect&) |
| 219 | { |
| 220 | SkEvent* evt = new SkEvent("inval-imageview"); |
| 221 | evt->post(getSinkID()); |
| 222 | } |
| 223 | |
| 224 | bool SkOSWindow::onEvent(const SkEvent& evt) |
| 225 | { |
| 226 | if (evt.isType("inval-imageview")) { |
| 227 | update(NULL); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 228 | if (!fGLAttached) |
| 229 | doPaint(); |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 230 | return true; |
| 231 | } |
| 232 | return INHERITED::onEvent(evt); |
| 233 | } |
| 234 | |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 235 | static bool convertBitmapToXImage(XImage& image, const SkBitmap& bitmap) |
| 236 | { |
scroggo | b66365f | 2011-03-18 21:43:03 +0000 | [diff] [blame] | 237 | 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; |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 251 | return XInitImage(&image); |
| 252 | } |
| 253 | |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 254 | void 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 | |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 261 | XImage image; |
| 262 | if (!convertBitmapToXImage(image, bitmap)) return; |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 263 | |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 264 | 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] | 265 | } |
| 266 | |
| 267 | bool SkOSWindow::onHandleChar(SkUnichar) |
| 268 | { |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | bool SkOSWindow::onHandleKey(SkKey key) |
| 273 | { |
| 274 | return false; |
| 275 | } |
| 276 | |
| 277 | bool SkOSWindow::onHandleKeyUp(SkKey key) |
| 278 | { |
| 279 | return false; |
| 280 | } |