3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #include <X11/XKBlib.h>
23 #include "XkeysToSkKeys.h"
25 #include "keysym2ucs.h"
28 const int WIDTH = 500;
29 const int HEIGHT = 500;
31 // Determine which events to listen for.
32 const long EVENT_MASK = StructureNotifyMask|ButtonPressMask|ButtonReleaseMask
33 |ExposureMask|PointerMotionMask|KeyPressMask|KeyReleaseMask;
35 SkOSWindow::SkOSWindow(void*)
37 , fMSAASampleCount(0) {
38 fUnixWindow.fDisplay = NULL;
39 fUnixWindow.fGLContext = NULL;
40 this->initWindow(0, NULL);
41 this->resize(WIDTH, HEIGHT);
44 SkOSWindow::~SkOSWindow() {
48 void SkOSWindow::closeWindow() {
49 if (fUnixWindow.fDisplay) {
51 SkASSERT(fUnixWindow.fGc);
52 XFreeGC(fUnixWindow.fDisplay, fUnixWindow.fGc);
53 fUnixWindow.fGc = NULL;
54 XDestroyWindow(fUnixWindow.fDisplay, fUnixWindow.fWin);
56 XCloseDisplay(fUnixWindow.fDisplay);
57 fUnixWindow.fDisplay = NULL;
62 void SkOSWindow::initWindow(int requestedMSAASampleCount, AttachmentInfo* info) {
63 if (fMSAASampleCount != requestedMSAASampleCount) {
66 // presence of fDisplay means we already have a window
67 if (fUnixWindow.fDisplay) {
70 glXGetConfig(fUnixWindow.fDisplay, fVi, GLX_SAMPLES_ARB, &info->fSampleCount);
71 glXGetConfig(fUnixWindow.fDisplay, fVi, GLX_STENCIL_SIZE, &info->fStencilBits);
73 info->fSampleCount = 0;
74 info->fStencilBits = 0;
79 fUnixWindow.fDisplay = XOpenDisplay(NULL);
80 Display* dsp = fUnixWindow.fDisplay;
82 SkDebugf("Could not open an X Display");
85 // Attempt to create a window that supports GL
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;
108 fVi = glXChooseVisual(dsp, DefaultScreen(dsp), att);
109 fMSAASampleCount = 0;
114 glXGetConfig(dsp, fVi, GLX_SAMPLES_ARB, &info->fSampleCount);
115 glXGetConfig(dsp, fVi, GLX_STENCIL_SIZE, &info->fStencilBits);
117 Colormap colorMap = XCreateColormap(dsp,
118 RootWindow(dsp, fVi->screen),
121 XSetWindowAttributes swa;
122 swa.colormap = colorMap;
123 swa.event_mask = EVENT_MASK;
124 fUnixWindow.fWin = XCreateWindow(dsp,
125 RootWindow(dsp, fVi->screen),
132 CWEventMask | CWColormap,
136 info->fSampleCount = 0;
137 info->fStencilBits = 0;
139 // Create a simple window instead. We will not be able to show GL
140 fUnixWindow.fWin = XCreateSimpleWindow(dsp,
141 DefaultRootWindow(dsp),
146 0); // background value
148 this->mapWindowAndWait();
149 fUnixWindow.fGc = XCreateGC(dsp, fUnixWindow.fWin, 0, NULL);
152 static unsigned getModi(const XEvent& evt) {
153 static const struct {
157 // X values found by experiment. Is there a better way?
158 { 1, kShift_SkModifierKey },
159 { 4, kControl_SkModifierKey },
160 { 8, kOption_SkModifierKey },
164 for (size_t i = 0; i < SK_ARRAY_COUNT(gModi); ++i) {
165 if (evt.xkey.state & gModi[i].fXMask) {
166 modi |= gModi[i].fSkMask;
172 static SkMSec gTimerDelay;
174 static bool MyXNextEventWithDelay(Display* dsp, XEvent* evt) {
175 // Check for pending events before entering the select loop. There might
176 // be events in the in-memory queue but not processed yet.
178 XNextEvent(dsp, evt);
182 SkMSec ms = gTimerDelay;
184 int x11_fd = ConnectionNumber(dsp);
187 FD_SET(x11_fd, &input_fds);
190 tv.tv_sec = ms / 1000; // seconds
191 tv.tv_usec = (ms % 1000) * 1000; // microseconds
193 if (!select(x11_fd + 1, &input_fds, NULL, NULL, &tv)) {
194 if (!XPending(dsp)) {
199 XNextEvent(dsp, evt);
203 static Atom wm_delete_window_message;
205 SkOSWindow::NextXEventResult SkOSWindow::nextXEvent() {
207 Display* dsp = fUnixWindow.fDisplay;
209 if (!MyXNextEventWithDelay(dsp, &evt)) {
210 return kContinue_NextXEventResult;
215 if (0 == evt.xexpose.count) {
216 return kPaintRequest_NextXEventResult;
219 case ConfigureNotify:
220 this->resize(evt.xconfigure.width, evt.xconfigure.height);
223 if (evt.xbutton.button == Button1)
224 this->handleClick(evt.xbutton.x, evt.xbutton.y,
225 SkView::Click::kDown_State, NULL, getModi(evt));
228 if (evt.xbutton.button == Button1)
229 this->handleClick(evt.xbutton.x, evt.xbutton.y,
230 SkView::Click::kUp_State, NULL, getModi(evt));
233 this->handleClick(evt.xmotion.x, evt.xmotion.y,
234 SkView::Click::kMoved_State, NULL, getModi(evt));
237 int shiftLevel = (evt.xkey.state & ShiftMask) ? 1 : 0;
238 KeySym keysym = XkbKeycodeToKeysym(dsp, evt.xkey.keycode,
240 if (keysym == XK_Escape) {
241 return kQuitRequest_NextXEventResult;
243 this->handleKey(XKeyToSkKey(keysym));
244 long uni = keysym2ucs(keysym);
246 this->handleChar((SkUnichar) uni);
251 this->handleKeyUp(XKeyToSkKey(XkbKeycodeToKeysym(dsp, evt.xkey.keycode, 0, 0)));
254 if ((Atom)evt.xclient.data.l[0] == wm_delete_window_message) {
255 return kQuitRequest_NextXEventResult;
259 // Do nothing for other events
262 return kContinue_NextXEventResult;
265 void SkOSWindow::loop() {
266 Display* dsp = fUnixWindow.fDisplay;
270 Window win = fUnixWindow.fWin;
272 wm_delete_window_message = XInternAtom(dsp, "WM_DELETE_WINDOW", False);
273 XSetWMProtocols(dsp, win, &wm_delete_window_message, 1);
275 XSelectInput(dsp, win, EVENT_MASK);
277 bool sentExposeEvent = false;
280 SkEvent::ServiceQueueTimer();
282 bool moreToDo = SkEvent::ProcessEvent();
284 if (this->isDirty() && !sentExposeEvent) {
285 sentExposeEvent = true;
288 sk_bzero(&evt, sizeof(evt));
290 evt.xexpose.display = dsp;
291 XSendEvent(dsp, win, false, ExposureMask, &evt);
294 if (XPending(dsp) || !moreToDo) {
295 switch (this->nextXEvent()) {
296 case kContinue_NextXEventResult:
298 case kPaintRequest_NextXEventResult:
299 sentExposeEvent = false;
300 if (this->isDirty()) {
305 case kQuitRequest_NextXEventResult:
312 void SkOSWindow::mapWindowAndWait() {
313 SkASSERT(fUnixWindow.fDisplay);
314 Display* dsp = fUnixWindow.fDisplay;
315 Window win = fUnixWindow.fWin;
316 XMapWindow(dsp, win);
318 long eventMask = StructureNotifyMask;
319 XSelectInput(dsp, win, eventMask);
321 // Wait until screen is ready.
324 XNextEvent(dsp, &evt);
325 } while(evt.type != MapNotify);
329 bool SkOSWindow::attach(SkBackEndTypes, int msaaSampleCount, AttachmentInfo* info) {
330 this->initWindow(msaaSampleCount, info);
332 if (NULL == fUnixWindow.fDisplay) {
335 if (NULL == fUnixWindow.fGLContext) {
338 fUnixWindow.fGLContext = glXCreateContext(fUnixWindow.fDisplay,
342 if (NULL == fUnixWindow.fGLContext) {
346 glXMakeCurrent(fUnixWindow.fDisplay,
348 fUnixWindow.fGLContext);
350 SkScalarRoundToInt(this->width()),
351 SkScalarRoundToInt(this->height()));
352 glClearColor(0, 0, 0, 0);
354 glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
358 void SkOSWindow::detach() {
359 if (NULL == fUnixWindow.fDisplay || NULL == fUnixWindow.fGLContext) {
362 glXMakeCurrent(fUnixWindow.fDisplay, None, NULL);
363 glXDestroyContext(fUnixWindow.fDisplay, fUnixWindow.fGLContext);
364 fUnixWindow.fGLContext = NULL;
367 void SkOSWindow::present() {
368 if (fUnixWindow.fDisplay && fUnixWindow.fGLContext) {
369 glXSwapBuffers(fUnixWindow.fDisplay, fUnixWindow.fWin);
373 void SkOSWindow::onSetTitle(const char title[]) {
374 if (NULL == fUnixWindow.fDisplay) {
377 XTextProperty textProp;
378 textProp.value = (unsigned char*)title;
380 textProp.nitems = strlen((char*)textProp.value);
381 textProp.encoding = XA_STRING;
382 XSetWMName(fUnixWindow.fDisplay, fUnixWindow.fWin, &textProp);
385 static bool convertBitmapToXImage(XImage& image, const SkBitmap& bitmap) {
386 sk_bzero(&image, sizeof(image));
388 int bitsPerPixel = bitmap.bytesPerPixel() * 8;
389 image.width = bitmap.width();
390 image.height = bitmap.height();
391 image.format = ZPixmap;
392 image.data = (char*) bitmap.getPixels();
393 image.byte_order = LSBFirst;
394 image.bitmap_unit = bitsPerPixel;
395 image.bitmap_bit_order = LSBFirst;
396 image.bitmap_pad = bitsPerPixel;
398 image.bytes_per_line = bitmap.rowBytes() - bitmap.width() * 4;
399 image.bits_per_pixel = bitsPerPixel;
400 return XInitImage(&image);
403 void SkOSWindow::doPaint() {
404 if (NULL == fUnixWindow.fDisplay) {
407 // If we are drawing with GL, we don't need XPutImage.
408 if (fUnixWindow.fGLContext) {
411 // Draw the bitmap to the screen.
412 const SkBitmap& bitmap = getBitmap();
413 int width = bitmap.width();
414 int height = bitmap.height();
417 if (!convertBitmapToXImage(image, bitmap)) {
421 XPutImage(fUnixWindow.fDisplay,
430 ///////////////////////////////////////////////////////////////////////////////
432 void SkEvent::SignalNonEmptyQueue() {
433 // nothing to do, since we spin on our event-queue, polling for XPending
436 void SkEvent::SignalQueueTimer(SkMSec delay) {
437 // just need to record the delay time. We handle waking up for it in
438 // MyXNextEventWithDelay()