aboutsummaryrefslogtreecommitdiff
path: root/src/Shape.cc
blob: a073b9ffc8b9bd165e43ed2f7404e7a375604070 (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Shape.cc
// Copyright (c) 2003 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

// $Id$

#include "Shape.hh"

#include "FbTk/FbWindow.hh"
#include "FbTk/App.hh"
#include "FbTk/GContext.hh"
#include "FbTk/FbPixmap.hh"

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif // HAVE_CONFIG_H

#ifdef HAVE_CSTRING
  #include <cstring>
#else
  #include <string.h>
#endif

#include <X11/Xutil.h>

#ifdef SHAPE
#include <X11/extensions/shape.h>
#endif // SHAPE

#include <algorithm>

using std::min;

// ignore_border basically means do clip shape instead of bounding shape
FbTk::FbPixmap *Shape::createShape(bool ignore_border) {
    if (m_win->window() == 0 || m_shapeplaces == 0 ||
        m_win->width() < 3 || m_win->height() < 3)
        return 0;

    static char left_bits[] = { 0xc0, 0xf8, 0xfc, 0xfe, 0xfe, 0xfe, 0xff, 0xff };
    static char right_bits[] = { 0x03, 0x1f, 0x3f, 0x7f, 0x7f, 0x7f, 0xff, 0xff};
    static char bottom_left_bits[] = { 0xff, 0xff, 0xfe, 0xfe, 0xfe, 0xfc, 0xf8, 0xc0 };
    static char bottom_right_bits[] = { 0xff, 0xff, 0x7f, 0x7f, 0x7f, 0x3f, 0x1f, 0x03 };

    const int borderw = m_win->borderWidth();
    const int win_width = m_win->width() + (ignore_border?0:2*borderw);
    const int win_height = m_win->height() + (ignore_border?0:2*borderw);
    const int pixmap_width = min(8, win_width);
    const int pixmap_height = min(8, win_height);

    Display *disp = FbTk::App::instance()->display();
    const size_t data_size = win_width * win_height;
    // we use calloc here so we get consistent C alloc/free with XDestroyImage
    // and no warnings in valgrind :)
    char *data = (char *)calloc(data_size, sizeof (char));
    if (data == 0)
        return 0;

    memset(data, 0xFF, data_size);

    XImage *ximage = XCreateImage(disp,
                                  DefaultVisual(disp, m_win->screenNumber()),
                                  1,
                                  XYPixmap, 0,
                                  data,
                                  win_width, win_height,
                                  32, 0);
    if (ximage == 0)
        return 0;

    XInitImage(ximage);

    // shape corners

    if (m_shapeplaces & Shape::TOPLEFT) {
        for (int y=0; y<pixmap_height; y++) {
            for (int x=0; x<pixmap_width; x++) {
                XPutPixel(ximage, x, y, (left_bits[y] & (0x01 << x)) ? 1 : 0);
            }
        }
    }

    if (m_shapeplaces & Shape::TOPRIGHT) {
        for (int y=0; y<pixmap_height; y++) {
            for (int x=0; x<pixmap_width; x++) {
                XPutPixel(ximage, x + win_width - pixmap_width, y,
                          (right_bits[y] & (0x01 << x)) ? 1 : 0);
            }
        }
    }

    if (m_shapeplaces & Shape::BOTTOMLEFT) {
        for (int y=0; y<pixmap_height; y++) {
            for (int x=0; x<pixmap_width; x++) {
                XPutPixel(ximage, x, y + win_height - pixmap_height,
                          (bottom_left_bits[y] & (0x01 << x)) ? 1 : 0);
            }
        }
    }

    if (m_shapeplaces & Shape::BOTTOMRIGHT) {
        for (int y=0; y<pixmap_height; y++) {
            for (int x=0; x<pixmap_width; x++) {
                XPutPixel(ximage, x + win_width - pixmap_width, y + win_height - pixmap_height,
                          (bottom_right_bits[y] & (0x01 << x)) ? 1 : 0);
            }
        }
    }

    FbTk::FbPixmap *pm = new FbTk::FbPixmap(*m_win, win_width, win_height, 1);


    FbTk::GContext gc(*pm);

    XPutImage(disp, pm->drawable(), gc.gc(), ximage, 0, 0, 0, 0,
              win_width, win_height);

    XDestroyImage(ximage);

    return pm;

}

Shape::Shape(FbTk::FbWindow &win, int shapeplaces):
    m_win(&win),
    m_shapeplaces(shapeplaces) {

    m_clipshape.reset(createShape(true));
    m_boundingshape.reset(createShape(false));
}

Shape::~Shape() {

#ifdef SHAPE
    if (m_win != 0 && m_win->window()) {
        // Reset shape of window
        XShapeCombineMask(FbTk::App::instance()->display(),
                          m_win->window(),
                          ShapeClip,
                          0, 0,
                          0,
                          ShapeSet);
        // Reset shape of window
        if (m_boundingshape.get()) {
            XShapeCombineMask(FbTk::App::instance()->display(),
                              m_win->window(),
                              ShapeBounding,
                              0, 0,
                              0,
                              ShapeSet);
        }
            }
#endif // SHAPE
}

void Shape::setPlaces(int shapeplaces) {
    m_shapeplaces = shapeplaces;
}

void Shape::update() {
    if (m_win == 0 || m_win->window() == 0)
        return;
#ifdef SHAPE
    if (m_clipshape.get() == 0 ||
        m_win->width() != clipWidth() ||
        m_win->height() != clipHeight()) {
        m_clipshape.reset(createShape(true));
    }
    if (m_boundingshape.get() == 0 ||
        (m_win->width()+m_win->borderWidth()*2) != width() ||
        (m_win->height()+m_win->borderWidth()*2) != height()) {
        if (m_win->borderWidth() != 0)
            m_boundingshape.reset(createShape(false));
        else 
            m_boundingshape.reset(0);

    }

    // the m_shape can be = 0 which will just raeset the shape mask
    // and make the window normal
    XShapeCombineMask(FbTk::App::instance()->display(),
                      m_win->window(),
                      ShapeClip,
                      0, 0,
                      (m_clipshape.get() != 0)? m_clipshape->drawable() : 0,
                      ShapeSet);

    // the m_shape can be = 0 which will just raeset the shape mask
    // and make the window normal
    XShapeCombineMask(FbTk::App::instance()->display(),
                      m_win->window(),
                      ShapeBounding,
                      -m_win->borderWidth(), -m_win->borderWidth(),
                      (m_boundingshape.get() != 0)? m_boundingshape->drawable() :
                      ((m_clipshape.get() != 0)? m_clipshape->drawable(): 0),
                      ShapeSet);


#endif // SHAPE

}

void Shape::setWindow(FbTk::FbWindow &win) {
    m_win = &win;
    update();
}

void Shape::setShapeNotify(const FbTk::FbWindow &win) {
#ifdef SHAPE
    XShapeSelectInput(FbTk::App::instance()->display(),
                      win.window(), ShapeNotifyMask);
#endif // SHAPE
}

bool Shape::isShaped(const FbTk::FbWindow &win) {
    int shaped = 0;

#ifdef SHAPE
    int not_used;
    unsigned int not_used2;
    XShapeQueryExtents(FbTk::App::instance()->display(),
                       win.window(),
                       &shaped,  /// bShaped
                       &not_used, &not_used,  // xbs, ybs
                       &not_used2, &not_used2, // wbs, hbs
                       &not_used, // cShaped
                       &not_used, &not_used, // xcs, ycs
                       &not_used2, &not_used2); // wcs, hcs
#endif // SHAPE

    return (shaped != 0 ? true : false);
}

unsigned int Shape::width() const {
    return m_boundingshape.get() ? m_boundingshape->width() : 0;
}

unsigned int Shape::height() const {
    return m_boundingshape.get() ? m_boundingshape->height() : 0;
}

unsigned int Shape::clipWidth() const {
    return m_clipshape.get() ? m_clipshape->width() : 0;
}

unsigned int Shape::clipHeight() const {
    return m_clipshape.get() ? m_clipshape->height() : 0;
}