michael@0: michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "SkWindow.h" michael@0: michael@0: #include "SkBitmap.h" michael@0: #include "SkCanvas.h" michael@0: #include "SkColor.h" michael@0: #include "SkEvent.h" michael@0: #include "SkKey.h" michael@0: #include "SkWindow.h" michael@0: #include "XkeysToSkKeys.h" michael@0: extern "C" { michael@0: #include "keysym2ucs.h" michael@0: } michael@0: michael@0: const int WIDTH = 500; michael@0: const int HEIGHT = 500; michael@0: michael@0: // Determine which events to listen for. michael@0: const long EVENT_MASK = StructureNotifyMask|ButtonPressMask|ButtonReleaseMask michael@0: |ExposureMask|PointerMotionMask|KeyPressMask|KeyReleaseMask; michael@0: michael@0: SkOSWindow::SkOSWindow(void*) michael@0: : fVi(NULL) michael@0: , fMSAASampleCount(0) { michael@0: fUnixWindow.fDisplay = NULL; michael@0: fUnixWindow.fGLContext = NULL; michael@0: this->initWindow(0, NULL); michael@0: this->resize(WIDTH, HEIGHT); michael@0: } michael@0: michael@0: SkOSWindow::~SkOSWindow() { michael@0: this->closeWindow(); michael@0: } michael@0: michael@0: void SkOSWindow::closeWindow() { michael@0: if (NULL != fUnixWindow.fDisplay) { michael@0: this->detach(); michael@0: SkASSERT(NULL != fUnixWindow.fGc); michael@0: XFreeGC(fUnixWindow.fDisplay, fUnixWindow.fGc); michael@0: fUnixWindow.fGc = NULL; michael@0: XDestroyWindow(fUnixWindow.fDisplay, fUnixWindow.fWin); michael@0: fVi = NULL; michael@0: XCloseDisplay(fUnixWindow.fDisplay); michael@0: fUnixWindow.fDisplay = NULL; michael@0: fMSAASampleCount = 0; michael@0: } michael@0: } michael@0: michael@0: void SkOSWindow::initWindow(int requestedMSAASampleCount, AttachmentInfo* info) { michael@0: if (fMSAASampleCount != requestedMSAASampleCount) { michael@0: this->closeWindow(); michael@0: } michael@0: // presence of fDisplay means we already have a window michael@0: if (NULL != fUnixWindow.fDisplay) { michael@0: if (NULL != info) { michael@0: if (NULL != fVi) { michael@0: glXGetConfig(fUnixWindow.fDisplay, fVi, GLX_SAMPLES_ARB, &info->fSampleCount); michael@0: glXGetConfig(fUnixWindow.fDisplay, fVi, GLX_STENCIL_SIZE, &info->fStencilBits); michael@0: } else { michael@0: info->fSampleCount = 0; michael@0: info->fStencilBits = 0; michael@0: } michael@0: } michael@0: return; michael@0: } michael@0: fUnixWindow.fDisplay = XOpenDisplay(NULL); michael@0: Display* dsp = fUnixWindow.fDisplay; michael@0: if (NULL == dsp) { michael@0: SkDebugf("Could not open an X Display"); michael@0: return; michael@0: } michael@0: // Attempt to create a window that supports GL michael@0: GLint att[] = { michael@0: GLX_RGBA, michael@0: GLX_DEPTH_SIZE, 24, michael@0: GLX_DOUBLEBUFFER, michael@0: GLX_STENCIL_SIZE, 8, michael@0: None michael@0: }; michael@0: SkASSERT(NULL == fVi); michael@0: if (requestedMSAASampleCount > 0) { michael@0: static const GLint kAttCount = SK_ARRAY_COUNT(att); michael@0: GLint msaaAtt[kAttCount + 4]; michael@0: memcpy(msaaAtt, att, sizeof(att)); michael@0: SkASSERT(None == msaaAtt[kAttCount - 1]); michael@0: msaaAtt[kAttCount - 1] = GLX_SAMPLE_BUFFERS_ARB; michael@0: msaaAtt[kAttCount + 0] = 1; michael@0: msaaAtt[kAttCount + 1] = GLX_SAMPLES_ARB; michael@0: msaaAtt[kAttCount + 2] = requestedMSAASampleCount; michael@0: msaaAtt[kAttCount + 3] = None; michael@0: fVi = glXChooseVisual(dsp, DefaultScreen(dsp), msaaAtt); michael@0: fMSAASampleCount = requestedMSAASampleCount; michael@0: } michael@0: if (NULL == fVi) { michael@0: fVi = glXChooseVisual(dsp, DefaultScreen(dsp), att); michael@0: fMSAASampleCount = 0; michael@0: } michael@0: michael@0: if (fVi) { michael@0: if (NULL != info) { michael@0: glXGetConfig(dsp, fVi, GLX_SAMPLES_ARB, &info->fSampleCount); michael@0: glXGetConfig(dsp, fVi, GLX_STENCIL_SIZE, &info->fStencilBits); michael@0: } michael@0: Colormap colorMap = XCreateColormap(dsp, michael@0: RootWindow(dsp, fVi->screen), michael@0: fVi->visual, michael@0: AllocNone); michael@0: XSetWindowAttributes swa; michael@0: swa.colormap = colorMap; michael@0: swa.event_mask = EVENT_MASK; michael@0: fUnixWindow.fWin = XCreateWindow(dsp, michael@0: RootWindow(dsp, fVi->screen), michael@0: 0, 0, // x, y michael@0: WIDTH, HEIGHT, michael@0: 0, // border width michael@0: fVi->depth, michael@0: InputOutput, michael@0: fVi->visual, michael@0: CWEventMask | CWColormap, michael@0: &swa); michael@0: } else { michael@0: if (NULL != info) { michael@0: info->fSampleCount = 0; michael@0: info->fStencilBits = 0; michael@0: } michael@0: // Create a simple window instead. We will not be able to show GL michael@0: fUnixWindow.fWin = XCreateSimpleWindow(dsp, michael@0: DefaultRootWindow(dsp), michael@0: 0, 0, // x, y michael@0: WIDTH, HEIGHT, michael@0: 0, // border width michael@0: 0, // border value michael@0: 0); // background value michael@0: } michael@0: this->mapWindowAndWait(); michael@0: fUnixWindow.fGc = XCreateGC(dsp, fUnixWindow.fWin, 0, NULL); michael@0: } michael@0: michael@0: static unsigned getModi(const XEvent& evt) { michael@0: static const struct { michael@0: unsigned fXMask; michael@0: unsigned fSkMask; michael@0: } gModi[] = { michael@0: // X values found by experiment. Is there a better way? michael@0: { 1, kShift_SkModifierKey }, michael@0: { 4, kControl_SkModifierKey }, michael@0: { 8, kOption_SkModifierKey }, michael@0: }; michael@0: michael@0: unsigned modi = 0; michael@0: for (size_t i = 0; i < SK_ARRAY_COUNT(gModi); ++i) { michael@0: if (evt.xkey.state & gModi[i].fXMask) { michael@0: modi |= gModi[i].fSkMask; michael@0: } michael@0: } michael@0: return modi; michael@0: } michael@0: michael@0: static SkMSec gTimerDelay; michael@0: michael@0: static bool MyXNextEventWithDelay(Display* dsp, XEvent* evt) { michael@0: // Check for pending events before entering the select loop. There might michael@0: // be events in the in-memory queue but not processed yet. michael@0: if (XPending(dsp)) { michael@0: XNextEvent(dsp, evt); michael@0: return true; michael@0: } michael@0: michael@0: SkMSec ms = gTimerDelay; michael@0: if (ms > 0) { michael@0: int x11_fd = ConnectionNumber(dsp); michael@0: fd_set input_fds; michael@0: FD_ZERO(&input_fds); michael@0: FD_SET(x11_fd, &input_fds); michael@0: michael@0: timeval tv; michael@0: tv.tv_sec = ms / 1000; // seconds michael@0: tv.tv_usec = (ms % 1000) * 1000; // microseconds michael@0: michael@0: if (!select(x11_fd + 1, &input_fds, NULL, NULL, &tv)) { michael@0: if (!XPending(dsp)) { michael@0: return false; michael@0: } michael@0: } michael@0: } michael@0: XNextEvent(dsp, evt); michael@0: return true; michael@0: } michael@0: michael@0: SkOSWindow::NextXEventResult SkOSWindow::nextXEvent() { michael@0: XEvent evt; michael@0: Display* dsp = fUnixWindow.fDisplay; michael@0: michael@0: if (!MyXNextEventWithDelay(fUnixWindow.fDisplay, &evt)) { michael@0: return kContinue_NextXEventResult; michael@0: } michael@0: michael@0: switch (evt.type) { michael@0: case Expose: michael@0: if (0 == evt.xexpose.count) { michael@0: return kPaintRequest_NextXEventResult; michael@0: } michael@0: break; michael@0: case ConfigureNotify: michael@0: this->resize(evt.xconfigure.width, evt.xconfigure.height); michael@0: break; michael@0: case ButtonPress: michael@0: if (evt.xbutton.button == Button1) michael@0: this->handleClick(evt.xbutton.x, evt.xbutton.y, michael@0: SkView::Click::kDown_State, NULL, getModi(evt)); michael@0: break; michael@0: case ButtonRelease: michael@0: if (evt.xbutton.button == Button1) michael@0: this->handleClick(evt.xbutton.x, evt.xbutton.y, michael@0: SkView::Click::kUp_State, NULL, getModi(evt)); michael@0: break; michael@0: case MotionNotify: michael@0: this->handleClick(evt.xmotion.x, evt.xmotion.y, michael@0: SkView::Click::kMoved_State, NULL, getModi(evt)); michael@0: break; michael@0: case KeyPress: { michael@0: int shiftLevel = (evt.xkey.state & ShiftMask) ? 1 : 0; michael@0: KeySym keysym = XkbKeycodeToKeysym(dsp, evt.xkey.keycode, michael@0: 0, shiftLevel); michael@0: if (keysym == XK_Escape) { michael@0: return kQuitRequest_NextXEventResult; michael@0: } michael@0: this->handleKey(XKeyToSkKey(keysym)); michael@0: long uni = keysym2ucs(keysym); michael@0: if (uni != -1) { michael@0: this->handleChar((SkUnichar) uni); michael@0: } michael@0: break; michael@0: } michael@0: case KeyRelease: michael@0: this->handleKeyUp(XKeyToSkKey(XkbKeycodeToKeysym(dsp, evt.xkey.keycode, 0, 0))); michael@0: break; michael@0: default: michael@0: // Do nothing for other events michael@0: break; michael@0: } michael@0: return kContinue_NextXEventResult; michael@0: } michael@0: michael@0: void SkOSWindow::loop() { michael@0: Display* dsp = fUnixWindow.fDisplay; michael@0: if (NULL == dsp) { michael@0: return; michael@0: } michael@0: Window win = fUnixWindow.fWin; michael@0: michael@0: XSelectInput(dsp, win, EVENT_MASK); michael@0: michael@0: bool sentExposeEvent = false; michael@0: michael@0: for (;;) { michael@0: SkEvent::ServiceQueueTimer(); michael@0: michael@0: bool moreToDo = SkEvent::ProcessEvent(); michael@0: michael@0: if (this->isDirty() && !sentExposeEvent) { michael@0: sentExposeEvent = true; michael@0: michael@0: XEvent evt; michael@0: sk_bzero(&evt, sizeof(evt)); michael@0: evt.type = Expose; michael@0: evt.xexpose.display = dsp; michael@0: XSendEvent(dsp, win, false, ExposureMask, &evt); michael@0: } michael@0: michael@0: if (XPending(dsp) || !moreToDo) { michael@0: switch (this->nextXEvent()) { michael@0: case kContinue_NextXEventResult: michael@0: break; michael@0: case kPaintRequest_NextXEventResult: michael@0: sentExposeEvent = false; michael@0: if (this->isDirty()) { michael@0: this->update(NULL); michael@0: } michael@0: this->doPaint(); michael@0: break; michael@0: case kQuitRequest_NextXEventResult: michael@0: return; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkOSWindow::mapWindowAndWait() { michael@0: SkASSERT(NULL != fUnixWindow.fDisplay); michael@0: Display* dsp = fUnixWindow.fDisplay; michael@0: Window win = fUnixWindow.fWin; michael@0: XMapWindow(dsp, win); michael@0: michael@0: long eventMask = StructureNotifyMask; michael@0: XSelectInput(dsp, win, eventMask); michael@0: michael@0: // Wait until screen is ready. michael@0: XEvent evt; michael@0: do { michael@0: XNextEvent(dsp, &evt); michael@0: } while(evt.type != MapNotify); michael@0: michael@0: } michael@0: michael@0: bool SkOSWindow::attach(SkBackEndTypes, int msaaSampleCount, AttachmentInfo* info) { michael@0: this->initWindow(msaaSampleCount, info); michael@0: michael@0: if (NULL == fUnixWindow.fDisplay) { michael@0: return false; michael@0: } michael@0: if (NULL == fUnixWindow.fGLContext) { michael@0: SkASSERT(NULL != fVi); michael@0: michael@0: fUnixWindow.fGLContext = glXCreateContext(fUnixWindow.fDisplay, michael@0: fVi, michael@0: NULL, michael@0: GL_TRUE); michael@0: if (NULL == fUnixWindow.fGLContext) { michael@0: return false; michael@0: } michael@0: } michael@0: glXMakeCurrent(fUnixWindow.fDisplay, michael@0: fUnixWindow.fWin, michael@0: fUnixWindow.fGLContext); michael@0: glViewport(0, 0, michael@0: SkScalarRoundToInt(this->width()), michael@0: SkScalarRoundToInt(this->height())); michael@0: glClearColor(0, 0, 0, 0); michael@0: glClearStencil(0); michael@0: glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); michael@0: return true; michael@0: } michael@0: michael@0: void SkOSWindow::detach() { michael@0: if (NULL == fUnixWindow.fDisplay || NULL == fUnixWindow.fGLContext) { michael@0: return; michael@0: } michael@0: glXMakeCurrent(fUnixWindow.fDisplay, None, NULL); michael@0: glXDestroyContext(fUnixWindow.fDisplay, fUnixWindow.fGLContext); michael@0: fUnixWindow.fGLContext = NULL; michael@0: } michael@0: michael@0: void SkOSWindow::present() { michael@0: if (NULL != fUnixWindow.fDisplay && NULL != fUnixWindow.fGLContext) { michael@0: glXSwapBuffers(fUnixWindow.fDisplay, fUnixWindow.fWin); michael@0: } michael@0: } michael@0: michael@0: void SkOSWindow::onSetTitle(const char title[]) { michael@0: if (NULL == fUnixWindow.fDisplay) { michael@0: return; michael@0: } michael@0: XTextProperty textProp; michael@0: textProp.value = (unsigned char*)title; michael@0: textProp.format = 8; michael@0: textProp.nitems = strlen((char*)textProp.value); michael@0: textProp.encoding = XA_STRING; michael@0: XSetWMName(fUnixWindow.fDisplay, fUnixWindow.fWin, &textProp); michael@0: } michael@0: michael@0: static bool convertBitmapToXImage(XImage& image, const SkBitmap& bitmap) { michael@0: sk_bzero(&image, sizeof(image)); michael@0: michael@0: int bitsPerPixel = bitmap.bytesPerPixel() * 8; michael@0: image.width = bitmap.width(); michael@0: image.height = bitmap.height(); michael@0: image.format = ZPixmap; michael@0: image.data = (char*) bitmap.getPixels(); michael@0: image.byte_order = LSBFirst; michael@0: image.bitmap_unit = bitsPerPixel; michael@0: image.bitmap_bit_order = LSBFirst; michael@0: image.bitmap_pad = bitsPerPixel; michael@0: image.depth = 24; michael@0: image.bytes_per_line = bitmap.rowBytes() - bitmap.width() * 4; michael@0: image.bits_per_pixel = bitsPerPixel; michael@0: return XInitImage(&image); michael@0: } michael@0: michael@0: void SkOSWindow::doPaint() { michael@0: if (NULL == fUnixWindow.fDisplay) { michael@0: return; michael@0: } michael@0: // If we are drawing with GL, we don't need XPutImage. michael@0: if (NULL != fUnixWindow.fGLContext) { michael@0: return; michael@0: } michael@0: // Draw the bitmap to the screen. michael@0: const SkBitmap& bitmap = getBitmap(); michael@0: int width = bitmap.width(); michael@0: int height = bitmap.height(); michael@0: michael@0: XImage image; michael@0: if (!convertBitmapToXImage(image, bitmap)) { michael@0: return; michael@0: } michael@0: michael@0: XPutImage(fUnixWindow.fDisplay, michael@0: fUnixWindow.fWin, michael@0: fUnixWindow.fGc, michael@0: &image, michael@0: 0, 0, // src x,y michael@0: 0, 0, // dst x,y michael@0: width, height); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void SkEvent::SignalNonEmptyQueue() { michael@0: // nothing to do, since we spin on our event-queue, polling for XPending michael@0: } michael@0: michael@0: void SkEvent::SignalQueueTimer(SkMSec delay) { michael@0: // just need to record the delay time. We handle waking up for it in michael@0: // MyXNextEventWithDelay() michael@0: gTimerDelay = delay; michael@0: }