]> icculus.org git repositories - mikachu/openbox.git/blob - src/GCCache.hh
merging in netwm changes at merge point "netwm-merge1". This add the XAtom class...
[mikachu/openbox.git] / src / GCCache.hh
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // GCCache.hh for Blackbox - an X11 Window manager
3 // Copyright (c) 2001 - 2002 Sean 'Shaleh' Perry <shaleh at debian.org>
4 // Copyright (c) 1997 - 2000, 2002 Bradley T Hughes <bhughes at trolltech.com>
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a
7 // copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 // DEALINGS IN THE SOFTWARE.
23
24 #ifndef GCCACHE_HH
25 #define GCCACHE_HH
26
27 extern "C" {
28 #include <X11/Xlib.h>
29 }
30
31 #include "BaseDisplay.hh"
32 #include "Color.hh"
33
34 class BGCCacheItem;
35
36 class BGCCacheContext {
37 public:
38   void set(const BColor &_color, const XFontStruct * const _font,
39            const int _function, const int _subwindow);
40   void set(const XFontStruct * const _font);
41
42 private:
43   BGCCacheContext(const BaseDisplay * const _display)
44     : display(_display), gc(0), pixel(0ul), fontid(0ul),
45       function(0), subwindow(0), used(false), screen(~(0u)) {}
46
47   const BaseDisplay *display;
48   GC gc;
49   unsigned long pixel;
50   unsigned long fontid;
51   int function;
52   int subwindow;
53   bool used;
54   unsigned int screen;
55
56   BGCCacheContext(const BGCCacheContext &_nocopy);
57   BGCCacheContext &operator=(const BGCCacheContext &_nocopy);
58
59   friend class BGCCache;
60   friend class BGCCacheItem;
61 };
62
63 class BGCCacheItem {
64 public:
65   inline const GC &gc(void) const { return ctx->gc; }
66
67 private:
68   BGCCacheItem(void) : ctx(0), count(0), hits(0), fault(false) { }
69
70   BGCCacheContext *ctx;
71   unsigned int count;
72   unsigned int hits;
73   bool fault;
74
75   BGCCacheItem(const BGCCacheItem &_nocopy);
76   BGCCacheItem &operator=(const BGCCacheItem &_nocopy);
77
78   friend class BGCCache;
79 };
80
81 class BGCCache {
82 public:
83   explicit BGCCache(const BaseDisplay * const _display);
84   ~BGCCache(void);
85
86   // cleans up the cache
87   void purge(void);
88
89   BGCCacheItem *find(const BColor &_color, const XFontStruct * const _font = 0,
90                      int _function = GXcopy, int _subwindow = ClipByChildren);
91   void release(BGCCacheItem *_item);
92
93 private:
94   BGCCacheContext *nextContext(unsigned int _screen);
95   void release(BGCCacheContext *ctx);
96
97   // this is closely modelled after the Qt GC cache, but with some of the
98   // complexity stripped out
99   const BaseDisplay *display;
100
101   const unsigned int context_count;
102   const unsigned int cache_size;
103   const unsigned int cache_buckets;
104   const unsigned int cache_total_size;
105   BGCCacheContext **contexts;
106   BGCCacheItem **cache;
107 };
108
109 class BPen {
110 public:
111   inline BPen(const BColor &_color,  const XFontStruct * const _font = 0,
112               int _function = GXcopy, int _subwindow = ClipByChildren)
113     : color(_color), font(_font), function(_function), subwindow(_subwindow),
114       cache(_color.display()->gcCache()), item(0) { }
115   inline ~BPen(void) { if (item) cache->release(item); }
116
117   inline const GC &gc(void) const {
118     if (! item) item = cache->find(color, font, function, subwindow);
119     return item->gc();
120   }
121
122 private:
123   const BColor &color;
124   const XFontStruct *font;
125   int function;
126   int subwindow;
127
128   mutable BGCCache *cache;
129   mutable BGCCacheItem *item;
130 };
131
132
133 #endif // GCCACHE_HH