]> icculus.org git repositories - mikachu/openbox.git/blob - src/GCCache.cc
add another return, and return a value for a non-void funtion.
[mikachu/openbox.git] / src / GCCache.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // GCCache.cc 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 #ifdef HAVE_CONFIG_H
25 #  include "../config.h"
26 #endif // HAVE_CONFIG_H
27
28 extern "C" {
29 #include <stdio.h>
30 }
31
32 #include "GCCache.hh"
33 #include "BaseDisplay.hh"
34 #include "Color.hh"
35 #include "Util.hh"
36
37
38 BGCCacheContext::~BGCCacheContext(void) {
39   if (gc)
40     XFreeGC(display->getXDisplay(), gc);
41 }
42
43
44 void BGCCacheContext::set(const BColor &_color,
45                           const XFontStruct * const _font,
46                           const int _function, const int _subwindow) {
47   XGCValues gcv;
48   pixel = gcv.foreground = _color.pixel();
49   function = gcv.function = _function;
50   subwindow = gcv.subwindow_mode = _subwindow;
51   unsigned long mask = GCForeground | GCFunction | GCSubwindowMode;
52
53   if (_font) {
54     fontid = gcv.font = _font->fid;
55     mask |= GCFont;
56   } else {
57     fontid = 0;
58   }
59
60   XChangeGC(display->getXDisplay(), gc, mask, &gcv);
61 }
62
63
64 void BGCCacheContext::set(const XFontStruct * const _font) {
65   if (! _font) {
66     fontid = 0;
67     return;
68   }
69
70   XGCValues gcv;
71   fontid = gcv.font = _font->fid;
72   XChangeGC(display->getXDisplay(), gc, GCFont, &gcv);
73 }
74
75
76 BGCCache::BGCCache(const BaseDisplay * const _display)
77   : display(_display),  context_count(128u),
78     cache_size(16u), cache_buckets(8u),
79     cache_total_size(cache_size * cache_buckets) {
80
81   contexts = new BGCCacheContext*[context_count];
82   unsigned int i;
83   for (i = 0; i < context_count; i++) {
84     contexts[i] = new BGCCacheContext(display);
85   }
86
87   cache = new BGCCacheItem*[cache_total_size];
88   for (i = 0; i < cache_total_size; ++i) {
89     cache[i] = new BGCCacheItem;
90   }
91 }
92
93
94 BGCCache::~BGCCache(void) {
95   std::for_each(contexts, contexts + context_count, PointerAssassin());
96   std::for_each(cache, cache + cache_total_size, PointerAssassin());
97   delete [] cache;
98   delete [] contexts;
99 }
100
101
102 BGCCacheContext *BGCCache::nextContext(unsigned int scr) {
103   Window hd = display->getScreenInfo(scr)->getRootWindow();
104
105   BGCCacheContext *c;
106
107   for (unsigned int i = 0; i < context_count; ++i) {
108     c = contexts[i];
109
110     if (! c->gc) {
111       c->gc = XCreateGC(display->getXDisplay(), hd, 0, 0);
112       c->used = false;
113       c->screen = scr;
114     }
115     if (! c->used && c->screen == scr) {
116       c->used = true;
117       return c;
118     }
119   }
120
121   fprintf(stderr, "BGCCache: context fault!\n");
122   abort();
123   return (BGCCacheContext*) 0; // not reached
124 }
125
126
127 void BGCCache::release(BGCCacheContext *ctx) {
128   ctx->used = false;
129 }
130
131
132 BGCCacheItem *BGCCache::find(const BColor &_color,
133                              const XFontStruct * const _font,
134                              int _function, int _subwindow) {
135   const unsigned long pixel = _color.pixel();
136   const unsigned int screen = _color.screen();
137   const int key = _color.red() ^ _color.green() ^ _color.blue();
138   int k = (key % cache_size) * cache_buckets;
139   int i = 0; // loop variable
140   BGCCacheItem *c = cache[ k ], *prev = 0;
141
142   // this will either loop 8 times then return/abort or it will stop matching
143   while (c->ctx &&
144          (c->ctx->pixel != pixel || c->ctx->function != _function ||
145           c->ctx->subwindow != _subwindow || c->ctx->screen != screen)) {
146     if (i < 7) {
147       prev = c;
148       c = cache[ ++k ];
149       ++i;
150       continue;
151     }
152     if (c->count == 0 && c->ctx->screen == screen) {
153       // use this cache item
154       c->ctx->set(_color, _font, _function, _subwindow);
155       c->ctx->used = true;
156       c->count = 1;
157       c->hits = 1;
158       return c;
159     }
160     // cache fault!
161     fprintf(stderr, "BGCCache: cache fault\n");
162     abort();
163   }
164
165   const unsigned long fontid = _font ? _font->fid : 0;
166   if (c->ctx) {
167     // reuse existing context
168     if (fontid && fontid != c->ctx->fontid)
169       c->ctx->set(_font);
170     c->count++;
171     c->hits++;
172     if (prev && c->hits > prev->hits) {
173       cache[ k     ] = prev;
174       cache[ k - 1 ] = c;
175     }
176   } else {
177     c->ctx = nextContext(screen);
178     c->ctx->set(_color, _font, _function, _subwindow);
179     c->ctx->used = true;
180     c->count = 1;
181     c->hits = 1;
182   }
183
184   return c;
185 }
186
187
188 void BGCCache::release(BGCCacheItem *_item) {
189   _item->count--;
190 }
191
192
193 void BGCCache::purge(void) {
194   for (unsigned int i = 0; i < cache_total_size; ++i) {
195     BGCCacheItem *d = cache[ i ];
196
197     if (d->ctx && d->count == 0) {
198       release(d->ctx);
199       d->ctx = 0;
200     }
201   }
202 }