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