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> |
bsalomon@google.com | 75356a7 | 2012-06-19 13:08:59 +0000 | [diff] [blame] | 10 | #include <X11/XKBlib.h> |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 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) |
robertphillips@google.com | 4bdfb8c | 2012-06-12 21:23:49 +0000 | [diff] [blame] | 36 | : fVi(NULL) |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 37 | , fMSAASampleCount(0) { |
| 38 | fUnixWindow.fDisplay = NULL; |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 39 | fUnixWindow.fGLContext = NULL; |
bsalomon@google.com | 64cc810 | 2013-03-05 20:06:05 +0000 | [diff] [blame] | 40 | this->initWindow(0, NULL); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 41 | this->resize(WIDTH, HEIGHT); |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 42 | } |
| 43 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 44 | SkOSWindow::~SkOSWindow() { |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 45 | this->closeWindow(); |
| 46 | } |
| 47 | |
| 48 | void SkOSWindow::closeWindow() { |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 49 | if (NULL != fUnixWindow.fDisplay) { |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 50 | this->detach(); |
| 51 | SkASSERT(NULL != fUnixWindow.fGc); |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 52 | XFreeGC(fUnixWindow.fDisplay, fUnixWindow.fGc); |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 53 | fUnixWindow.fGc = NULL; |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 54 | XDestroyWindow(fUnixWindow.fDisplay, fUnixWindow.fWin); |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 55 | fVi = NULL; |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 56 | XCloseDisplay(fUnixWindow.fDisplay); |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 57 | fUnixWindow.fDisplay = NULL; |
| 58 | fMSAASampleCount = 0; |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 59 | } |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 60 | } |
| 61 | |
bsalomon@google.com | 64cc810 | 2013-03-05 20:06:05 +0000 | [diff] [blame] | 62 | void SkOSWindow::initWindow(int requestedMSAASampleCount, AttachmentInfo* info) { |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 63 | if (fMSAASampleCount != requestedMSAASampleCount) { |
| 64 | this->closeWindow(); |
| 65 | } |
| 66 | // presence of fDisplay means we already have a window |
| 67 | if (NULL != fUnixWindow.fDisplay) { |
bsalomon@google.com | 64cc810 | 2013-03-05 20:06:05 +0000 | [diff] [blame] | 68 | if (NULL != info) { |
| 69 | if (NULL != fVi) { |
| 70 | glXGetConfig(fUnixWindow.fDisplay, fVi, GLX_SAMPLES_ARB, &info->fSampleCount); |
| 71 | glXGetConfig(fUnixWindow.fDisplay, fVi, GLX_STENCIL_SIZE, &info->fStencilBits); |
| 72 | } else { |
| 73 | info->fSampleCount = 0; |
| 74 | info->fStencilBits = 0; |
| 75 | } |
| 76 | } |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 77 | return; |
| 78 | } |
| 79 | fUnixWindow.fDisplay = XOpenDisplay(NULL); |
| 80 | Display* dsp = fUnixWindow.fDisplay; |
| 81 | if (NULL == dsp) { |
| 82 | SkDebugf("Could not open an X Display"); |
| 83 | return; |
| 84 | } |
| 85 | // Attempt to create a window that supports GL |
| 86 | GLint att[] = { |
| 87 | GLX_RGBA, |
| 88 | GLX_DEPTH_SIZE, 24, |
| 89 | GLX_DOUBLEBUFFER, |
| 90 | GLX_STENCIL_SIZE, 8, |
| 91 | None |
| 92 | }; |
| 93 | SkASSERT(NULL == fVi); |
| 94 | if (requestedMSAASampleCount > 0) { |
| 95 | static const GLint kAttCount = SK_ARRAY_COUNT(att); |
| 96 | GLint msaaAtt[kAttCount + 4]; |
| 97 | memcpy(msaaAtt, att, sizeof(att)); |
| 98 | SkASSERT(None == msaaAtt[kAttCount - 1]); |
| 99 | msaaAtt[kAttCount - 1] = GLX_SAMPLE_BUFFERS_ARB; |
| 100 | msaaAtt[kAttCount + 0] = 1; |
| 101 | msaaAtt[kAttCount + 1] = GLX_SAMPLES_ARB; |
| 102 | msaaAtt[kAttCount + 2] = requestedMSAASampleCount; |
| 103 | msaaAtt[kAttCount + 3] = None; |
| 104 | fVi = glXChooseVisual(dsp, DefaultScreen(dsp), msaaAtt); |
| 105 | fMSAASampleCount = requestedMSAASampleCount; |
| 106 | } |
| 107 | if (NULL == fVi) { |
| 108 | fVi = glXChooseVisual(dsp, DefaultScreen(dsp), att); |
| 109 | fMSAASampleCount = 0; |
| 110 | } |
| 111 | |
| 112 | if (fVi) { |
bsalomon@google.com | 64cc810 | 2013-03-05 20:06:05 +0000 | [diff] [blame] | 113 | if (NULL != info) { |
| 114 | glXGetConfig(dsp, fVi, GLX_SAMPLES_ARB, &info->fSampleCount); |
| 115 | glXGetConfig(dsp, fVi, GLX_STENCIL_SIZE, &info->fStencilBits); |
| 116 | } |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 117 | Colormap colorMap = XCreateColormap(dsp, |
| 118 | RootWindow(dsp, fVi->screen), |
| 119 | fVi->visual, |
| 120 | AllocNone); |
| 121 | XSetWindowAttributes swa; |
| 122 | swa.colormap = colorMap; |
| 123 | swa.event_mask = EVENT_MASK; |
| 124 | fUnixWindow.fWin = XCreateWindow(dsp, |
| 125 | RootWindow(dsp, fVi->screen), |
| 126 | 0, 0, // x, y |
| 127 | WIDTH, HEIGHT, |
| 128 | 0, // border width |
| 129 | fVi->depth, |
| 130 | InputOutput, |
| 131 | fVi->visual, |
| 132 | CWEventMask | CWColormap, |
| 133 | &swa); |
| 134 | } else { |
bsalomon@google.com | 64cc810 | 2013-03-05 20:06:05 +0000 | [diff] [blame] | 135 | if (NULL != info) { |
| 136 | info->fSampleCount = 0; |
| 137 | info->fStencilBits = 0; |
| 138 | } |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 139 | // Create a simple window instead. We will not be able to show GL |
| 140 | fUnixWindow.fWin = XCreateSimpleWindow(dsp, |
| 141 | DefaultRootWindow(dsp), |
| 142 | 0, 0, // x, y |
| 143 | WIDTH, HEIGHT, |
| 144 | 0, // border width |
| 145 | 0, // border value |
| 146 | 0); // background value |
| 147 | } |
| 148 | this->mapWindowAndWait(); |
| 149 | fUnixWindow.fGc = XCreateGC(dsp, fUnixWindow.fWin, 0, NULL); |
| 150 | } |
| 151 | |
| 152 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 153 | void SkOSWindow::post_linuxevent() { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 154 | // Put an event in the X queue to fire an SkEvent. |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 155 | if (NULL == fUnixWindow.fDisplay) { |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 156 | return; |
| 157 | } |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 158 | XClientMessageEvent event; |
| 159 | event.type = ClientMessage; |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 160 | Atom myAtom(0); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 161 | event.message_type = myAtom; |
| 162 | event.format = 32; |
| 163 | event.data.l[0] = 0; |
| 164 | XSendEvent(fUnixWindow.fDisplay, fUnixWindow.fWin, false, 0, |
| 165 | (XEvent*) &event); |
Scroggo | 5a23424 | 2011-06-13 19:17:58 +0000 | [diff] [blame] | 166 | XFlush(fUnixWindow.fDisplay); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 167 | } |
| 168 | |
reed@google.com | 3792418 | 2013-01-08 16:25:42 +0000 | [diff] [blame] | 169 | static unsigned getModi(const XEvent& evt) { |
reed@google.com | e378d83 | 2013-01-08 16:50:52 +0000 | [diff] [blame] | 170 | static const struct { |
| 171 | unsigned fXMask; |
| 172 | unsigned fSkMask; |
| 173 | } gModi[] = { |
| 174 | // X values found by experiment. Is there a better way? |
| 175 | { 1, kShift_SkModifierKey }, |
| 176 | { 4, kControl_SkModifierKey }, |
| 177 | { 8, kOption_SkModifierKey }, |
| 178 | }; |
skia.committer@gmail.com | 4e73aa1 | 2013-01-09 02:01:30 +0000 | [diff] [blame] | 179 | |
reed@google.com | e378d83 | 2013-01-08 16:50:52 +0000 | [diff] [blame] | 180 | unsigned modi = 0; |
| 181 | for (size_t i = 0; i < SK_ARRAY_COUNT(gModi); ++i) { |
| 182 | if (evt.xkey.state & gModi[i].fXMask) { |
| 183 | modi |= gModi[i].fSkMask; |
| 184 | } |
| 185 | } |
| 186 | return modi; |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 187 | } |
| 188 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 189 | void SkOSWindow::loop() { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 190 | Display* dsp = fUnixWindow.fDisplay; |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 191 | if (NULL == dsp) { |
| 192 | return; |
| 193 | } |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 194 | XSelectInput(dsp, fUnixWindow.fWin, EVENT_MASK); |
| 195 | |
| 196 | bool loop = true; |
| 197 | XEvent evt; |
| 198 | while (loop) { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 199 | XNextEvent(dsp, &evt); |
| 200 | switch (evt.type) { |
| 201 | case Expose: |
| 202 | if (evt.xexpose.count == 0) |
| 203 | this->inval(NULL); |
| 204 | break; |
| 205 | case ConfigureNotify: |
| 206 | this->resize(evt.xconfigure.width, evt.xconfigure.height); |
| 207 | break; |
| 208 | case ButtonPress: |
| 209 | if (evt.xbutton.button == Button1) |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 210 | this->handleClick(evt.xbutton.x, evt.xbutton.y, |
reed@google.com | 3792418 | 2013-01-08 16:25:42 +0000 | [diff] [blame] | 211 | SkView::Click::kDown_State, NULL, getModi(evt)); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 212 | break; |
| 213 | case ButtonRelease: |
| 214 | if (evt.xbutton.button == Button1) |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 215 | this->handleClick(evt.xbutton.x, evt.xbutton.y, |
reed@google.com | 3792418 | 2013-01-08 16:25:42 +0000 | [diff] [blame] | 216 | SkView::Click::kUp_State, NULL, getModi(evt)); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 217 | break; |
| 218 | case MotionNotify: |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 219 | this->handleClick(evt.xmotion.x, evt.xmotion.y, |
reed@google.com | 3792418 | 2013-01-08 16:25:42 +0000 | [diff] [blame] | 220 | SkView::Click::kMoved_State, NULL, getModi(evt)); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 221 | break; |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 222 | case KeyPress: { |
bsalomon@google.com | 75356a7 | 2012-06-19 13:08:59 +0000 | [diff] [blame] | 223 | KeySym keysym = XkbKeycodeToKeysym(dsp, evt.xkey.keycode, 0, 0); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 224 | //SkDebugf("pressed key %i!\n\tKeySym:%i\n", evt.xkey.keycode, XKeycodeToKeysym(dsp, evt.xkey.keycode, 0)); |
| 225 | if (keysym == XK_Escape) { |
| 226 | loop = false; |
| 227 | break; |
| 228 | } |
| 229 | this->handleKey(XKeyToSkKey(keysym)); |
| 230 | long uni = keysym2ucs(keysym); |
| 231 | if (uni != -1) { |
| 232 | this->handleChar((SkUnichar) uni); |
| 233 | } |
| 234 | break; |
| 235 | } |
| 236 | case KeyRelease: |
| 237 | //SkDebugf("released key %i\n", evt.xkey.keycode); |
bsalomon@google.com | 75356a7 | 2012-06-19 13:08:59 +0000 | [diff] [blame] | 238 | this->handleKeyUp(XKeyToSkKey(XkbKeycodeToKeysym(dsp, evt.xkey.keycode, 0, 0))); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 239 | break; |
| 240 | case ClientMessage: |
| 241 | if (SkEvent::ProcessEvent()) { |
| 242 | this->post_linuxevent(); |
| 243 | } |
| 244 | break; |
| 245 | default: |
| 246 | // Do nothing for other events |
| 247 | break; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 252 | void SkOSWindow::mapWindowAndWait() { |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 253 | SkASSERT(NULL != fUnixWindow.fDisplay); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 254 | Display* dsp = fUnixWindow.fDisplay; |
| 255 | Window win = fUnixWindow.fWin; |
| 256 | XMapWindow(dsp, win); |
| 257 | |
| 258 | long eventMask = StructureNotifyMask; |
| 259 | XSelectInput(dsp, win, eventMask); |
| 260 | |
| 261 | // Wait until screen is ready. |
| 262 | XEvent evt; |
| 263 | do { |
| 264 | XNextEvent(dsp, &evt); |
| 265 | } while(evt.type != MapNotify); |
| 266 | |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 267 | } |
| 268 | |
bsalomon@google.com | 64cc810 | 2013-03-05 20:06:05 +0000 | [diff] [blame] | 269 | bool SkOSWindow::attach(SkBackEndTypes, int msaaSampleCount, AttachmentInfo* info) { |
| 270 | this->initWindow(msaaSampleCount, info); |
| 271 | |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 272 | if (NULL == fUnixWindow.fDisplay) { |
| 273 | return false; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 274 | } |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 275 | if (NULL == fUnixWindow.fGLContext) { |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 276 | SkASSERT(NULL != fVi); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 277 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 278 | fUnixWindow.fGLContext = glXCreateContext(fUnixWindow.fDisplay, |
| 279 | fVi, |
| 280 | NULL, |
| 281 | GL_TRUE); |
| 282 | if (NULL == fUnixWindow.fGLContext) { |
| 283 | return false; |
| 284 | } |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 285 | } |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 286 | glXMakeCurrent(fUnixWindow.fDisplay, |
| 287 | fUnixWindow.fWin, |
| 288 | fUnixWindow.fGLContext); |
| 289 | glViewport(0, 0, |
| 290 | SkScalarRound(this->width()), SkScalarRound(this->height())); |
| 291 | glClearColor(0, 0, 0, 0); |
| 292 | glClearStencil(0); |
| 293 | glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 294 | return true; |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 295 | } |
| 296 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 297 | void SkOSWindow::detach() { |
| 298 | if (NULL == fUnixWindow.fDisplay || NULL == fUnixWindow.fGLContext) { |
| 299 | return; |
| 300 | } |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 301 | glXMakeCurrent(fUnixWindow.fDisplay, None, NULL); |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 302 | glXDestroyContext(fUnixWindow.fDisplay, fUnixWindow.fGLContext); |
| 303 | fUnixWindow.fGLContext = NULL; |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 304 | } |
| 305 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 306 | void SkOSWindow::present() { |
| 307 | if (NULL != fUnixWindow.fDisplay && NULL != fUnixWindow.fGLContext) { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 308 | glXSwapBuffers(fUnixWindow.fDisplay, fUnixWindow.fWin); |
| 309 | } |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 310 | } |
| 311 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 312 | void SkOSWindow::onSetTitle(const char title[]) { |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 313 | if (NULL == fUnixWindow.fDisplay) { |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 314 | return; |
| 315 | } |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 316 | XTextProperty textProp; |
| 317 | textProp.value = (unsigned char*)title; |
| 318 | textProp.format = 8; |
| 319 | textProp.nitems = strlen((char*)textProp.value); |
| 320 | textProp.encoding = XA_STRING; |
| 321 | XSetWMName(fUnixWindow.fDisplay, fUnixWindow.fWin, &textProp); |
| 322 | } |
| 323 | |
reed@google.com | f2164b2 | 2011-08-04 13:57:56 +0000 | [diff] [blame] | 324 | void SkOSWindow::onHandleInval(const SkIRect&) { |
| 325 | (new SkEvent("inval-imageview", this->getSinkID()))->post(); |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 326 | } |
| 327 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 328 | bool SkOSWindow::onEvent(const SkEvent& evt) { |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 329 | if (evt.isType("inval-imageview")) { |
| 330 | update(NULL); |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 331 | if (NULL == fUnixWindow.fGLContext) |
| 332 | this->doPaint(); |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 333 | return true; |
| 334 | } |
| 335 | return INHERITED::onEvent(evt); |
| 336 | } |
| 337 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 338 | static bool convertBitmapToXImage(XImage& image, const SkBitmap& bitmap) { |
scroggo | b66365f | 2011-03-18 21:43:03 +0000 | [diff] [blame] | 339 | sk_bzero(&image, sizeof(image)); |
| 340 | |
| 341 | int bitsPerPixel = bitmap.bytesPerPixel() * 8; |
| 342 | image.width = bitmap.width(); |
| 343 | image.height = bitmap.height(); |
| 344 | image.format = ZPixmap; |
| 345 | image.data = (char*) bitmap.getPixels(); |
| 346 | image.byte_order = LSBFirst; |
| 347 | image.bitmap_unit = bitsPerPixel; |
| 348 | image.bitmap_bit_order = LSBFirst; |
| 349 | image.bitmap_pad = bitsPerPixel; |
| 350 | image.depth = 24; |
| 351 | image.bytes_per_line = bitmap.rowBytes() - bitmap.width() * bitmap.bytesPerPixel(); |
| 352 | image.bits_per_pixel = bitsPerPixel; |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 353 | return XInitImage(&image); |
| 354 | } |
| 355 | |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 356 | void SkOSWindow::doPaint() { |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 357 | if (NULL == fUnixWindow.fDisplay) { |
| 358 | return; |
| 359 | } |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 360 | // Draw the bitmap to the screen. |
| 361 | const SkBitmap& bitmap = getBitmap(); |
| 362 | int width = bitmap.width(); |
| 363 | int height = bitmap.height(); |
| 364 | |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 365 | XImage image; |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 366 | if (!convertBitmapToXImage(image, bitmap)) { |
| 367 | return; |
| 368 | } |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 369 | |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 370 | XPutImage(fUnixWindow.fDisplay, |
| 371 | fUnixWindow.fWin, |
| 372 | fUnixWindow.fGc, |
| 373 | &image, |
| 374 | 0, 0, // src x,y |
| 375 | 0, 0, // dst x,y |
| 376 | width, height); |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 377 | } |
| 378 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 379 | bool SkOSWindow::onHandleChar(SkUnichar) { |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 380 | return false; |
| 381 | } |
| 382 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 383 | bool SkOSWindow::onHandleKey(SkKey key) { |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 384 | return false; |
| 385 | } |
| 386 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 387 | bool SkOSWindow::onHandleKeyUp(SkKey key) { |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 388 | return false; |
| 389 | } |