2
// Copyright (c) 2003 - 2005 Henrik Kinnunen (fluxgen at fluxbox dot org)
4
// Permission is hereby granted, free of charge, to any person obtaining a
5
// copy of this software and associated documentation files (the "Software"),
6
// to deal in the Software without restriction, including without limitation
7
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
// and/or sell copies of the Software, and to permit persons to whom the
9
// Software is furnished to do so, subject to the following conditions:
11
// The above copyright notice and this permission notice shall be included in
12
// all copies or substantial portions of the Software.
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20
// DEALINGS IN THE SOFTWARE.
22
// $Id: Shape.cc 3865 2005-01-24 18:34:57Z mathias $
26
#include "FbTk/FbWindow.hh"
27
#include "FbTk/App.hh"
28
#include "FbTk/GContext.hh"
29
#include "FbTk/FbPixmap.hh"
37
#include <X11/Xutil.h>
40
#endif // HAVE_CONFIG_H
43
#include <X11/extensions/shape.h>
52
FbTk::FbPixmap *createShape(const FbTk::FbWindow &win, int place) {
53
if (win.window() == 0 || place == 0 ||
54
win.width() < 3 || win.height() < 3)
57
static char left_bits[] = { 0xc0, 0xf8, 0xfc, 0xfe, 0xfe, 0xfe, 0xff, 0xff };
58
static char right_bits[] = { 0x03, 0x1f, 0x3f, 0x7f, 0x7f, 0x7f, 0xff, 0xff};
59
static char bottom_left_bits[] = { 0xff, 0xff, 0xfe, 0xfe, 0xfe, 0xfc, 0xf8, 0xc0 };
60
static char bottom_right_bits[] = { 0xff, 0xff, 0x7f, 0x7f, 0x7f, 0x3f, 0x1f, 0x03 };
62
const int borderw = win.borderWidth();
63
const int win_width = win.width() + 2*borderw;
64
const int win_height = win.height() + 2*borderw;
65
const int pixmap_width = min(8, win_width);
66
const int pixmap_height = min(8, win_height);
68
Display *disp = FbTk::App::instance()->display();
69
const size_t data_size = win_width * win_height;
70
// we use calloc here so we get consistent C alloc/free with XDestroyImage
71
// and no warnings in valgrind :)
72
char *data = (char *)calloc(data_size, sizeof (char));
76
memset(data, 0xFF, data_size);
78
XImage *ximage = XCreateImage(disp,
79
DefaultVisual(disp, win.screenNumber()),
83
win_width, win_height,
92
if (place & Shape::TOPLEFT) {
93
for (int y=0; y<pixmap_height; y++) {
94
for (int x=0; x<pixmap_width; x++) {
95
XPutPixel(ximage, x, y, (left_bits[y] & (0x01 << x)) ? 1 : 0);
100
if (place & Shape::TOPRIGHT) {
101
for (int y=0; y<pixmap_height; y++) {
102
for (int x=0; x<pixmap_width; x++) {
103
XPutPixel(ximage, x + win_width - pixmap_width, y,
104
(right_bits[y] & (0x01 << x)) ? 1 : 0);
109
if (place & Shape::BOTTOMLEFT) {
110
for (int y=0; y<pixmap_height; y++) {
111
for (int x=0; x<pixmap_width; x++) {
112
XPutPixel(ximage, x, y + win_height - pixmap_height,
113
(bottom_left_bits[y] & (0x01 << x)) ? 1 : 0);
118
if (place & Shape::BOTTOMRIGHT) {
119
for (int y=0; y<pixmap_height; y++) {
120
for (int x=0; x<pixmap_width; x++) {
121
XPutPixel(ximage, x + win_width - pixmap_width, y + win_height - pixmap_height,
122
(bottom_right_bits[y] & (0x01 << x)) ? 1 : 0);
127
FbTk::FbPixmap *pm = new FbTk::FbPixmap(win, win_width, win_height, 1);
130
FbTk::GContext gc(*pm);
132
XPutImage(disp, pm->drawable(), gc.gc(), ximage, 0, 0, 0, 0,
133
win_width, win_height);
135
XDestroyImage(ximage);
141
} // end anonymous namespace
143
Shape::Shape(FbTk::FbWindow &win, int shapeplaces):
145
m_shapeplaces(shapeplaces) {
147
m_shape.reset(createShape(win, shapeplaces));
153
if (m_win != 0 && m_win->window()) {
154
// Reset shape of window
155
XShapeCombineMask(FbTk::App::instance()->display(),
165
void Shape::setPlaces(int shapeplaces) {
166
m_shapeplaces = shapeplaces;
169
void Shape::update() {
170
if (m_win == 0 || m_win->window() == 0)
173
if (m_shape.get() == 0 ||
174
m_win->width() != width() ||
175
m_win->height() != height()) {
176
m_shape.reset(createShape(*m_win, m_shapeplaces));
179
// the m_shape can be = 0 which will just reset the shape mask
180
// and make the window normal
181
XShapeCombineMask(FbTk::App::instance()->display(),
184
-m_win->borderWidth(), -m_win->borderWidth(),
185
m_shape.get() ? m_shape->drawable() : 0,
193
void Shape::setWindow(FbTk::FbWindow &win) {
198
void Shape::setShapeNotify(const FbTk::FbWindow &win) {
200
XShapeSelectInput(FbTk::App::instance()->display(),
201
win.window(), ShapeNotifyMask);
205
bool Shape::isShaped(const FbTk::FbWindow &win) {
210
unsigned int not_used2;
211
XShapeQueryExtents(FbTk::App::instance()->display(),
214
¬_used, ¬_used, // xbs, ybs
215
¬_used2, ¬_used2, // wbs, hbs
216
¬_used, // cShaped
217
¬_used, ¬_used, // xcs, ycs
218
¬_used2, ¬_used2); // wcs, hcs
221
return (shaped != 0 ? true : false);
224
unsigned int Shape::width() const {
225
return m_shape.get() ? m_shape->width() : 0;
228
unsigned int Shape::height() const {
229
return m_shape.get() ? m_shape->height() : 0;