~ubuntu-branches/ubuntu/trusty/fluxbox/trusty-proposed

« back to all changes in this revision

Viewing changes to src/FbTk/FbPixmap.cc

  • Committer: Bazaar Package Importer
  • Author(s): Dmitry E. Oboukhov
  • Date: 2008-07-01 10:38:14 UTC
  • mfrom: (2.1.12 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080701103814-khx2b6il152x9p93
Tags: 1.0.0+deb1-8
* x-dev has been removed from build-depends (out-of-date package).
* Standards-Version bumped to 3.8.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// FbPixmap.cc for FbTk - Fluxbox ToolKit
2
 
// Copyright (c) 2003 - 2005 Henrik Kinnunen (fluxgen at fluxbox dot org)
3
 
//
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:
10
 
//
11
 
// The above copyright notice and this permission notice shall be included in
12
 
// all copies or substantial portions of the Software.
13
 
//
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.
21
 
 
22
 
// $Id: FbPixmap.cc 4004 2005-05-10 16:29:00Z simonb $
23
 
 
24
 
#include "FbPixmap.hh"
25
 
#include "App.hh"
26
 
#include "GContext.hh"
27
 
#include "Transparent.hh"
28
 
 
29
 
#include <X11/Xutil.h>
30
 
#include <X11/Xatom.h>
31
 
#include <iostream>
32
 
#include <string>
33
 
 
34
 
using namespace std;
35
 
 
36
 
namespace FbTk {
37
 
 
38
 
Pixmap *FbPixmap::m_root_pixmaps = 0;
39
 
 
40
 
const char* FbPixmap::root_prop_ids[] = {
41
 
    "_XROOTPMAP_ID",
42
 
    "_XSETROOT_ID",
43
 
    0
44
 
};
45
 
 
46
 
// same number as in root_prop_ids
47
 
Atom FbPixmap::root_prop_atoms[] = {
48
 
    None,
49
 
    None,
50
 
    None
51
 
};
52
 
 
53
 
 
54
 
FbPixmap::FbPixmap():m_pm(0),
55
 
                     m_width(0), m_height(0),
56
 
                     m_depth(0) {
57
 
}
58
 
 
59
 
FbPixmap::FbPixmap(const FbPixmap &the_copy):FbDrawable(), m_pm(0),
60
 
                                             m_width(0), m_height(0),
61
 
                                             m_depth(0){
62
 
    copy(the_copy);
63
 
}
64
 
 
65
 
FbPixmap::FbPixmap(Pixmap pm):m_pm(0),
66
 
                              m_width(0), m_height(0),
67
 
                              m_depth(0) {
68
 
    if (pm == 0)
69
 
        return;
70
 
    // assign X pixmap to this
71
 
    (*this) = pm;
72
 
}
73
 
 
74
 
FbPixmap::FbPixmap(const FbDrawable &src,
75
 
                   unsigned int width, unsigned int height,
76
 
                   int depth):m_pm(0),
77
 
                              m_width(0), m_height(0),
78
 
                              m_depth(0) {
79
 
 
80
 
    create(src.drawable(), width, height, depth);
81
 
}
82
 
 
83
 
FbPixmap::FbPixmap(Drawable src,
84
 
                   unsigned int width, unsigned int height,
85
 
                   int depth):m_pm(0),
86
 
                              m_width(0), m_height(0),
87
 
                              m_depth(0) {
88
 
 
89
 
    create(src, width, height, depth);
90
 
}
91
 
 
92
 
FbPixmap::~FbPixmap() {
93
 
    free();
94
 
}
95
 
 
96
 
FbPixmap &FbPixmap::operator = (const FbPixmap &the_copy) {
97
 
    copy(the_copy);
98
 
    return *this;
99
 
}
100
 
 
101
 
FbPixmap &FbPixmap::operator = (Pixmap pm) {
102
 
    // free pixmap before we set new
103
 
    free();
104
 
 
105
 
    if (pm == 0)
106
 
        return *this;
107
 
 
108
 
    // get width, height and depth for the pixmap
109
 
    Window root;
110
 
    int x, y;
111
 
    unsigned int border_width, bpp;
112
 
    XGetGeometry(display(),
113
 
                 pm,
114
 
                 &root,
115
 
                 &x, &y,
116
 
                 &m_width, &m_height,
117
 
                 &border_width,
118
 
                 &bpp);
119
 
 
120
 
    m_depth = bpp;
121
 
 
122
 
    m_pm = pm;
123
 
 
124
 
    return *this;
125
 
}
126
 
 
127
 
void FbPixmap::copy(const FbPixmap &the_copy) {
128
 
 
129
 
    bool create_new = false;
130
 
 
131
 
    if (the_copy.width() != width() ||
132
 
        the_copy.height() != height() ||
133
 
        the_copy.depth() != depth() ||
134
 
        drawable() == 0)
135
 
        create_new = true;
136
 
 
137
 
    if (create_new)
138
 
        free();
139
 
 
140
 
    if (the_copy.drawable() != 0) {
141
 
        if (create_new) {
142
 
            create(the_copy.drawable(),
143
 
                   the_copy.width(), the_copy.height(),
144
 
                   the_copy.depth());
145
 
        }
146
 
 
147
 
        if (drawable()) {
148
 
            GContext gc(drawable());
149
 
 
150
 
            copyArea(the_copy.drawable(),
151
 
                     gc.gc(),
152
 
                     0, 0,
153
 
                     0, 0,
154
 
                     width(), height());
155
 
        }
156
 
    }
157
 
}
158
 
 
159
 
// screen doesn't count if depth is "zero"...
160
 
void FbPixmap::copy(Pixmap pm, int depth, int screen_num) {
161
 
    free();
162
 
    if (pm == 0)
163
 
        return;
164
 
 
165
 
    // get width, height and depth for the pixmap
166
 
    Window root;
167
 
    int x, y;
168
 
    unsigned int border_width, bpp;
169
 
    unsigned int new_width, new_height;
170
 
 
171
 
    XGetGeometry(display(),
172
 
                 pm,
173
 
                 &root,
174
 
                 &x, &y,
175
 
                 &new_width, &new_height,
176
 
                 &border_width,
177
 
                 &bpp);
178
 
 
179
 
    if (depth == 0)
180
 
        depth = bpp;
181
 
 
182
 
    // create new pixmap and copy area
183
 
    create(root, new_width, new_height, depth);
184
 
 
185
 
    GC gc = XCreateGC(display(), drawable(), 0, 0);
186
 
 
187
 
    if (depth == bpp) {
188
 
        XCopyArea(display(), pm, drawable(), gc,
189
 
                  0, 0,
190
 
                  width(), height(),
191
 
                  0, 0);
192
 
    } else {
193
 
        XSetForeground(display(), gc, Color("black", screen_num).pixel());
194
 
        XSetBackground(display(), gc, Color("white", screen_num).pixel());
195
 
        XCopyPlane(display(), pm, drawable(), gc,
196
 
                   0, 0,
197
 
                   width(), height(),
198
 
                   0, 0, 1);
199
 
    }
200
 
 
201
 
    XFreeGC(display(), gc);
202
 
}
203
 
 
204
 
void FbPixmap::rotate() {
205
 
 
206
 
    // make an image copy
207
 
    XImage *src_image = XGetImage(display(), drawable(),
208
 
                                  0, 0, // pos
209
 
                                  width(), height(), // size
210
 
                                  ~0, // plane mask
211
 
                                  ZPixmap); // format
212
 
    // reverse height/width for new pixmap
213
 
    FbPixmap new_pm(drawable(), height(), width(), depth());
214
 
 
215
 
    GContext gc(drawable());
216
 
 
217
 
    // copy new area
218
 
    for (unsigned int y = 0; y < height(); ++y) {
219
 
        for (unsigned int x = 0; x < width(); ++x) {
220
 
            gc.setForeground(XGetPixel(src_image, x, y));
221
 
            // revers coordinates
222
 
            XDrawPoint(display(), new_pm.drawable(), gc.gc(), y, x);
223
 
        }
224
 
    }
225
 
 
226
 
    XDestroyImage(src_image);
227
 
    // free old pixmap and set new from new_pm
228
 
    free();
229
 
 
230
 
    m_width = new_pm.width();
231
 
    m_height = new_pm.height();
232
 
    m_depth = new_pm.depth();
233
 
    m_pm = new_pm.release();
234
 
}
235
 
 
236
 
void FbPixmap::scale(unsigned int dest_width, unsigned int dest_height) {
237
 
 
238
 
    if (drawable() == 0 ||
239
 
        (dest_width == width() && dest_height == height()))
240
 
        return;
241
 
 
242
 
    XImage *src_image = XGetImage(display(), drawable(),
243
 
                                  0, 0, // pos
244
 
                                  width(), height(), // size
245
 
                                  ~0, // plane mask
246
 
                                  ZPixmap); // format
247
 
    if (src_image == 0)
248
 
        return;
249
 
 
250
 
    // create new pixmap with dest size
251
 
    FbPixmap new_pm(drawable(), dest_width, dest_height, depth());
252
 
 
253
 
    GContext gc(drawable());
254
 
    // calc zoom
255
 
    float zoom_x = static_cast<float>(width())/static_cast<float>(dest_width);
256
 
    float zoom_y = static_cast<float>(height())/static_cast<float>(dest_height);
257
 
 
258
 
    // start scaling
259
 
    float src_x = 0, src_y = 0;
260
 
    for (unsigned int tx=0; tx < dest_width; ++tx, src_x += zoom_x) {
261
 
        src_y = 0;
262
 
        for (unsigned int ty=0; ty < dest_height; ++ty, src_y += zoom_y) {
263
 
            gc.setForeground(XGetPixel(src_image,
264
 
                                       static_cast<int>(src_x),
265
 
                                       static_cast<int>(src_y)));
266
 
            XDrawPoint(display(), new_pm.drawable(), gc.gc(), tx, ty);
267
 
        }
268
 
    }
269
 
 
270
 
    XDestroyImage(src_image);
271
 
 
272
 
    // free old pixmap and set new from new_pm
273
 
    free();
274
 
 
275
 
    m_width = new_pm.width();
276
 
    m_height = new_pm.height();
277
 
    m_depth = new_pm.depth();
278
 
    m_pm = new_pm.release();
279
 
}
280
 
 
281
 
void FbPixmap::tile(unsigned int dest_width, unsigned int dest_height) {
282
 
    if (drawable() == 0 ||
283
 
        (dest_width == width() && dest_height == height()))
284
 
        return;
285
 
 
286
 
    FbPixmap new_pm(drawable(), width(), height(), depth());
287
 
 
288
 
    new_pm.copy(m_pm, 0, 0);
289
 
 
290
 
    resize(dest_width, dest_height);
291
 
 
292
 
    FbTk::GContext gc(*this);
293
 
 
294
 
    gc.setTile(new_pm);
295
 
    gc.setFillStyle(FillTiled);
296
 
 
297
 
    fillRectangle(gc.gc(), 0, 0, dest_width, dest_height);
298
 
 
299
 
}
300
 
 
301
 
 
302
 
 
303
 
void FbPixmap::resize(unsigned int width, unsigned int height) {
304
 
    FbPixmap pm(drawable(), width, height, depth());
305
 
    *this = pm.release();
306
 
}
307
 
 
308
 
Pixmap FbPixmap::release() {
309
 
    Pixmap ret = m_pm;
310
 
    m_pm = 0;
311
 
    m_width = 0;
312
 
    m_height = 0;
313
 
    m_depth = 0;
314
 
    return ret;
315
 
}
316
 
 
317
 
void FbPixmap::rootwinPropertyNotify(int screen_num, Atom atom) {
318
 
    if (!FbTk::Transparent::haveRender()) 
319
 
        return;
320
 
 
321
 
    checkAtoms();
322
 
    for (int i=0; root_prop_ids[i] != 0; ++i) {
323
 
        if (root_prop_atoms[i] == atom) {
324
 
            Pixmap root_pm = None;
325
 
            Atom real_type;
326
 
            int real_format;
327
 
            unsigned long items_read, items_left;
328
 
            unsigned long *data;
329
 
 
330
 
            unsigned int prop = 0;
331
 
            if (XGetWindowProperty(display(),
332
 
                                   RootWindow(display(), i),
333
 
                                   root_prop_atoms[i],
334
 
                                   0l, 1l,
335
 
                                   False, XA_PIXMAP,
336
 
                                   &real_type, &real_format,
337
 
                                   &items_read, &items_left,
338
 
                                   (unsigned char **) &data) == Success) {
339
 
                if (real_format == 32 && items_read == 1) {
340
 
                    root_pm = (Pixmap) (*data);
341
 
                }
342
 
                XFree(data);
343
 
                if (root_pm != None)
344
 
                    setRootPixmap(screen_num, root_pm);
345
 
            }
346
 
            break;
347
 
        }
348
 
    }
349
 
}
350
 
 
351
 
void FbPixmap::setRootPixmap(int screen_num, Pixmap pm) {
352
 
    if (!m_root_pixmaps) {
353
 
        m_root_pixmaps = new Pixmap[ScreenCount(display())];
354
 
    }
355
 
 
356
 
    m_root_pixmaps[screen_num] = pm;
357
 
}
358
 
 
359
 
Pixmap FbPixmap::getRootPixmap(int screen_num) {
360
 
    if (!FbTk::Transparent::haveRender()) 
361
 
        return None;
362
 
 
363
 
    if (!m_root_pixmaps) {
364
 
        int numscreens = ScreenCount(display());
365
 
        for (int i=0; i < numscreens; ++i) {
366
 
            Atom real_type;
367
 
            int real_format;
368
 
            unsigned long items_read, items_left;
369
 
            unsigned long *data;
370
 
 
371
 
            unsigned int prop = 0;
372
 
 
373
 
            static bool print_error = true; // print error_message only once
374
 
            static const char* error_message = { "\n\n !!! WARNING WARNING WARNING WARNING !!!!!\n"
375
 
        "   if you experience problems with transparency:\n"
376
 
        "   you are using a wallpapersetter that \n"
377
 
        "   uses _XSETROOT_ID .. which we do not support.\n"
378
 
        "   consult 'fbsetbg -i' or try any other wallpapersetter\n"
379
 
        "   that uses _XROOTPMAP_ID !\n"
380
 
        " !!! WARNING WARNING WARNING WARNING !!!!!!\n\n"
381
 
            };
382
 
 
383
 
            Pixmap root_pm = None;
384
 
            for (prop = 0; root_prop_ids[prop]; prop++) {
385
 
                checkAtoms();
386
 
                if (XGetWindowProperty(display(),
387
 
                                       RootWindow(display(), i),
388
 
                                       root_prop_atoms[i],
389
 
                                       0l, 1l,
390
 
                                       False, XA_PIXMAP,
391
 
                                       &real_type, &real_format,
392
 
                                       &items_read, &items_left,
393
 
                                       (unsigned char **) &data) == Success) {
394
 
                    if (real_format == 32 && items_read == 1) {
395
 
                        if (print_error && strcmp(root_prop_ids[prop], "_XSETROOT_ID") == 0) {
396
 
                            cerr<<error_message;
397
 
                            print_error = false;
398
 
                        } else
399
 
                            root_pm = (Pixmap) (*data);
400
 
                    }
401
 
                    XFree(data);
402
 
                    if (root_pm != None)
403
 
                        break;
404
 
                }
405
 
            }
406
 
            setRootPixmap(i, root_pm);
407
 
        }
408
 
    }
409
 
    return m_root_pixmaps[screen_num];
410
 
}
411
 
 
412
 
void FbPixmap::checkAtoms() {
413
 
    for (int i=0; root_prop_ids[i] != 0; ++i) {
414
 
        if (root_prop_atoms[i] == None) {
415
 
            root_prop_atoms[i] = XInternAtom(display(), root_prop_ids[i], False);
416
 
        }
417
 
    }
418
 
}
419
 
 
420
 
void FbPixmap::free() {
421
 
    if (m_pm != 0) {
422
 
        XFreePixmap(display(), m_pm);
423
 
        m_pm = 0;
424
 
    }
425
 
    m_width = 0;
426
 
    m_height = 0;
427
 
    m_depth = 0;
428
 
}
429
 
 
430
 
void FbPixmap::create(Drawable src,
431
 
                      unsigned int width, unsigned int height,
432
 
                      int depth) {
433
 
    if (src == 0)
434
 
        return;
435
 
 
436
 
    m_pm = XCreatePixmap(display(),
437
 
                         src, width, height, depth);
438
 
    if (m_pm == 0)
439
 
        return;
440
 
 
441
 
    m_width = width;
442
 
    m_height = height;
443
 
    m_depth = depth;
444
 
}
445
 
 
446
 
}; // end namespace FbTk