blob: 83db6ad5c9314b6ca29f5478c522da4f0148576b [file] [log] [blame]
scroggob7e9aee2011-03-15 15:15:15 +00001#include <X11/Xlib.h>
2#include <X11/Xatom.h>
3#include <X11/keysym.h>
4#include <GL/glx.h>
5
6#include "SkWindow.h"
7
8#include "SkBitmap.h"
scroggo08526c02011-03-22 14:03:21 +00009#include "SkCanvas.h"
scroggob7e9aee2011-03-15 15:15:15 +000010#include "SkColor.h"
11#include "SkEvent.h"
12
13SkOSWindow::SkOSWindow(void* unused)
14{
15 fUnixWindow.fDisplay = NULL;
16}
17
18SkOSWindow::~SkOSWindow()
19{
20}
21
22void SkOSWindow::setUnixWindow(Display* dsp, Window win, size_t screenNumber, GC gc)
23{
24 fUnixWindow.fDisplay = dsp;
25 fUnixWindow.fWin = win;
26 fUnixWindow.fOSWin = screenNumber;
27 fUnixWindow.fGc = gc;
28}
29
bsalomon@google.comc8ad63e2011-03-18 14:29:44 +000030bool SkOSWindow::attachGL()
scroggob7e9aee2011-03-15 15:15:15 +000031{
32 return false;
33}
34
35void SkOSWindow::detachGL()
36{
37
38}
39
40void SkOSWindow::presentGL()
41{
42
43}
44
45void SkOSWindow::onSetTitle(const char title[])
46{
47 if (!fUnixWindow.fDisplay) return;
48 XTextProperty textProp;
49 textProp.value = (unsigned char*)title;
50 textProp.format = 8;
51 textProp.nitems = strlen((char*)textProp.value);
52 textProp.encoding = XA_STRING;
53 XSetWMName(fUnixWindow.fDisplay, fUnixWindow.fWin, &textProp);
54}
55
56void SkOSWindow::onHandleInval(const SkIRect&)
57{
58 SkEvent* evt = new SkEvent("inval-imageview");
59 evt->post(getSinkID());
60}
61
62bool SkOSWindow::onEvent(const SkEvent& evt)
63{
64 if (evt.isType("inval-imageview")) {
65 update(NULL);
66 doPaint();
67 return true;
68 }
69 return INHERITED::onEvent(evt);
70}
71
scroggo08526c02011-03-22 14:03:21 +000072static bool convertBitmapToXImage(XImage& image, const SkBitmap& bitmap)
73{
scroggob66365f2011-03-18 21:43:03 +000074 sk_bzero(&image, sizeof(image));
75
76 int bitsPerPixel = bitmap.bytesPerPixel() * 8;
77 image.width = bitmap.width();
78 image.height = bitmap.height();
79 image.format = ZPixmap;
80 image.data = (char*) bitmap.getPixels();
81 image.byte_order = LSBFirst;
82 image.bitmap_unit = bitsPerPixel;
83 image.bitmap_bit_order = LSBFirst;
84 image.bitmap_pad = bitsPerPixel;
85 image.depth = 24;
86 image.bytes_per_line = bitmap.rowBytes() - bitmap.width() * bitmap.bytesPerPixel();
87 image.bits_per_pixel = bitsPerPixel;
scroggo08526c02011-03-22 14:03:21 +000088 return XInitImage(&image);
89}
90
scroggo08526c02011-03-22 14:03:21 +000091void SkOSWindow::doPaint() {
92 if (!fUnixWindow.fDisplay) return;
93 // Draw the bitmap to the screen.
94 const SkBitmap& bitmap = getBitmap();
95 int width = bitmap.width();
96 int height = bitmap.height();
97
Scroggo0f185c22011-03-24 18:35:50 +000098 XImage image;
99 if (!convertBitmapToXImage(image, bitmap)) return;
scroggo08526c02011-03-22 14:03:21 +0000100
Scroggo0f185c22011-03-24 18:35:50 +0000101 XPutImage(fUnixWindow.fDisplay, fUnixWindow.fWin, fUnixWindow.fGc, &image, 0, 0, 0, 0, width, height);
scroggob7e9aee2011-03-15 15:15:15 +0000102}
103
104bool SkOSWindow::onHandleChar(SkUnichar)
105{
106 return false;
107}
108
109bool SkOSWindow::onHandleKey(SkKey key)
110{
111 return false;
112}
113
114bool SkOSWindow::onHandleKeyUp(SkKey key)
115{
116 return false;
117}