all repos — openbox @ aceea3c0814778317be7a903f63b8363d2b1b0ef

openbox fork - make it a bit more like ryudo

otk/surface.cc (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-

#include "config.h"

#include "surface.hh"
#include "display.hh"
#include "screeninfo.hh"
#include "rendercolor.hh"

extern "C" {
#include <X11/Xutil.h>
#include <cstring>
}

namespace otk {

Surface::Surface(int screen, const Size &size)
  : _screen(screen),
    _size(size),
    _pixel_data(new pixel32[size.width()*size.height()]),
    _pixmap(None),
    _xftdraw(0)
{
}

Surface::~Surface()
{
  destroyObjects();
  delete [] _pixel_data;
}

void Surface::setPixmap(const RenderColor &color)
{
  if (_pixmap == None)
    createObjects();

  XFillRectangle(**display, _pixmap, color.gc(), 0, 0,
                 _size.width(), _size.height());

  pixel32 val = 0; // XXX set this from the color and shift amounts!
  for (unsigned int i = 0, s = _size.width() * _size.height(); i < s; ++i) {
    unsigned char *p = (unsigned char*)&_pixel_data[i];
    *p = (unsigned char) (val >> 24);
    *++p = (unsigned char) (val >> 16);
    *++p = (unsigned char) (val >> 8);
    *++p = (unsigned char) val;
  }
}

void Surface::setPixmap(XImage *image)
{
  assert(image->width == _size.width());
  assert(image->height == _size.height());
  
  if (_pixmap == None)
    createObjects();

  XPutImage(**display, _pixmap, DefaultGC(**display, _screen),
            image, 0, 0, 0, 0, _size.width(), _size.height());
}

void Surface::createObjects()
{
  assert(_pixmap == None); assert(!_xftdraw);

  const ScreenInfo *info = display->screenInfo(_screen);
  
  _pixmap = XCreatePixmap(**display, info->rootWindow(),
                          _size.width(), _size.height(), info->depth());
  assert(_pixmap != None);
    
  _xftdraw = XftDrawCreate(**display, _pixmap,
                           info->visual(), info->colormap());
  assert(_xftdraw);
}

void Surface::destroyObjects()
{
  if (_xftdraw) {
    XftDrawDestroy(_xftdraw);
    _xftdraw = 0;
  }

  if (_pixmap != None) {
    XFreePixmap(**display, _pixmap);
    _pixmap = None;
  }
}

}