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); |
| 87 | } |
| 88 | |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 89 | void 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) { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 97 | 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 | |
| 148 | void 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 | |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 163 | } |
| 164 | |
bsalomon@google.com | c8ad63e | 2011-03-18 14:29:44 +0000 | [diff] [blame] | 165 | bool SkOSWindow::attachGL() |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 166 | { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 167 | 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 | |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 186 | return true; |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | void SkOSWindow::detachGL() |
| 190 | { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 191 | if (!fUnixWindow.fDisplay || !fGLAttached) return; |
| 192 | fGLAttached = false; |
| 193 | // Returns back to normal drawing. |
| 194 | glXMakeCurrent(fUnixWindow.fDisplay, None, NULL); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 195 | // Ensure that we redraw when switching back to raster. |
| 196 | this->inval(NULL); |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void SkOSWindow::presentGL() |
| 200 | { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 201 | if (fUnixWindow.fDisplay && fGLAttached) { |
| 202 | glXSwapBuffers(fUnixWindow.fDisplay, fUnixWindow.fWin); |
| 203 | } |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void 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 | |
| 217 | void SkOSWindow::onHandleInval(const SkIRect&) |
| 218 | { |
| 219 | SkEvent* evt = new SkEvent("inval-imageview"); |
| 220 | evt->post(getSinkID()); |
| 221 | } |
| 222 | |
| 223 | bool SkOSWindow::onEvent(const SkEvent& evt) |
| 224 | { |
| 225 | if (evt.isType("inval-imageview")) { |
| 226 | update(NULL); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 227 | if (!fGLAttached) |
| 228 | doPaint(); |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 229 | return true; |
| 230 | } |
| 231 | return INHERITED::onEvent(evt); |
| 232 | } |
| 233 | |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 234 | static bool convertBitmapToXImage(XImage& image, const SkBitmap& bitmap) |
| 235 | { |
scroggo | b66365f | 2011-03-18 21:43:03 +0000 | [diff] [blame] | 236 | 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; |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 250 | return XInitImage(&image); |
| 251 | } |
| 252 | |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 253 | void 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 | |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 260 | XImage image; |
| 261 | if (!convertBitmapToXImage(image, bitmap)) return; |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 262 | |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 263 | 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] | 264 | } |
| 265 | |
| 266 | bool SkOSWindow::onHandleChar(SkUnichar) |
| 267 | { |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | bool SkOSWindow::onHandleKey(SkKey key) |
| 272 | { |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | bool SkOSWindow::onHandleKeyUp(SkKey key) |
| 277 | { |
| 278 | return false; |
| 279 | } |