Debian Codegraph @

xo/ldisplay.cxx

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /* ldisplay.cxx
2 $Id: ldisplay.cxx,v 1.19 1997/10/23 02:28:57 elf Exp $
3
4 written by Marc Singer
5 8 May 1997
6
7 This file is part of the project XO. See the file README for
8 more information.
9
10 Copyright (C) 1997 Marc Singer
11
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 in a file called COPYING along with this program; if not, write to
24 the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
25 02139, USA.
26
27 -----------
28 DESCRIPTION
29 -----------
30
31 */
32
33 #include "standard.h"
34 #include "ldisplay.h"
35 #include "lwindow.h"
36
37 unsigned long LDisplay::color_distance (XColor& color, XColor& colorMatch)
38 {
39 unsigned long sh = 16 - m_pVisual->bits_per_rgb;
40 //unsigned long mask = (0xffff ^ ((1 << (16 - m_pVisual->bits_per_rgb)) - 1));
41 unsigned long distRed = (color.red >> sh) - (colorMatch.red >> sh);
42 unsigned long distGreen = (color.green >> sh) - (colorMatch.green >> sh);
43 unsigned long distBlue = (color.blue >> sh) - (colorMatch.blue >> sh);
44 return distRed*distRed + distGreen*distGreen + distBlue*distBlue;
45 }
46
47
48 void LDisplay::dispatch (XEvent* pEvent)
49 {
50 LWindow* pWindow = find_window (pEvent->xany.window);
51 if (!pWindow)
52 return;
53 PFNEvent pfn = pWindow->find_event (pEvent->type, pEvent);
54 if (!pfn)
55 return;
56 (pWindow->*pfn) (pEvent);
57 }
58
59 void LDisplay::dispatch_next_event (void)
60 {
61 XEvent event;
62 next_event (&event);
63
64 // Update keyboard mapping
65 if (event.type == MappingKeyboard) {
66 XRefreshKeyboardMapping ((XMappingEvent*) &event);
67 return;
68 }
69
70 //fprintf (stderr, "event %d window 0x%lx\n", event.type, event.xany.window);
71 LWindow* pWindow = find_window (event.xany.window);
72 if (!pWindow)
73 return;
74 PFNEvent pfn = pWindow->find_event (event.type, &event);
75 if (!pfn)
76 return;
77 (pWindow->*pfn) (&event);
78 }
79
80
81 LWindow* LDisplay::find_child (int id)
82 {
83 return m_pWindowRoot ? m_pWindowRoot->find_sibling (id) : (LWindow*) NULL;
84 }
85
86
87 bool LDisplay::open (char* szDisplay)
88 {
89 m_pDisplay = XOpenDisplay (szDisplay);
90
91 if (!m_pDisplay)
92 return false;
93
94 m_cbitUnit = XBitmapUnit (m_pDisplay);
95 m_cbitPadding = XBitmapPad (m_pDisplay);
96 m_depth = XDefaultDepth (m_pDisplay, 0);
97
98 m_hashWindow.init (sizeof (LWindow*), 301);
99 m_font_cache.init (this);
100 m_hashTemplate.init (sizeof (LWindow*), 301);
101 // dmalloc_validate ();
102
103 // -- Get the resource database
104 {
105 char* szDatabase = XResourceManagerString (m_pDisplay);
106 // ** FIXME ** Seems to me that this is a really bad problem if this fails
107 if (szDatabase)
108 m_database = XrmGetStringDatabase (szDatabase);
109 }
110
111 if (!m_visual_class)
112 m_visual_class = TrueColor;
113 XMatchVisualInfo (m_pDisplay, 0, m_depth, m_visual_class, &m_visualInfo);
114 m_pVisual = (m_visualInfo.visual
115 ? m_visualInfo.visual
116 : XDefaultVisual (m_pDisplay, 0));
117 m_colormap = ((m_visualInfo.c_class == TrueColor)
118 ? XCreateColormap (m_pDisplay,
119 XDefaultRootWindow (m_pDisplay),
120 m_pVisual, AllocNone)
121 : CopyFromParent);
122 #if defined (TALK)
123 printf ("using visualID 0x%lx\n",
124 XVisualIDFromVisual (m_pVisual));
125 #endif
126
127 if (m_pVisual->red_mask) {
128 unsigned mask;
129 for (m_shiftRed = 0, mask = m_pVisual->red_mask;
130 !(mask & 0x8000) && mask; ++m_shiftRed, (mask <<= 1))
131 ;
132 for (m_shiftGreen = 0, mask = m_pVisual->green_mask;
133 !(mask & 0x8000) && mask; ++m_shiftGreen, (mask <<= 1))
134 ;
135 for (m_shiftBlue = 0, mask = m_pVisual->blue_mask;
136 !(mask & 0x8000) && mask; ++m_shiftBlue, (mask <<= 1))
137 ;
138 }
139
140 // Cache some GCs
141 XGCValues values;
142 unsigned long value_mask = 0;
143
144 values.foreground = XWhitePixel (m_pDisplay, 0);
145 values.background = XBlackPixel (m_pDisplay, 0);
146 value_mask |= GCForeground | GCBackground;
147 m_gcAttribute = XCreateGC (m_pDisplay, XDefaultRootWindow (m_pDisplay),
148 value_mask, &values);
149 m_gcDraw = XCreateGC (m_pDisplay, XDefaultRootWindow (m_pDisplay),
150 0, 0);
151
152 // Evaluate pixmap format
153 #if defined (TALK)
154 {
155 int c;
156 XPixmapFormatValues* pFormat
157 = XListPixmapFormats (m_pDisplay, &c);
158 for (int i = 0; i < c; ++i)
159 printf ("pixmap depth %d bits_per_pixel %d scanline_pad %d\n",
160 pFormat[i].depth,
161 pFormat[i].bits_per_pixel,
162 pFormat[i].scanline_pad);
163 XFree (pFormat);
164
165 printf ("bitmap unit %d bitmap pad %d\n",
166 XBitmapUnit (m_pDisplay), XBitmapPad (m_pDisplay));
167
168 printf ("image byte order %s bit order %s\n",
169 XImageByteOrder (m_pDisplay) ? "msb" : "lsb",
170 XBitmapBitOrder (m_pDisplay) ? "msb" : "lsb");
171
172 }
173 #endif
174
175 // dmalloc_validate ();
176
177 return true;
178 }
179
180
181 bool LDisplay::find_pixel (char* szColorName, unsigned long* pPixel)
182 {
183 Colormap colormap = XDefaultColormap (m_pDisplay, 0);
184 XColor color;
185 XParseColor (m_pDisplay, colormap, szColorName, &color);
186 bool fReturn = false;
187
188 if ( m_pVisual->c_class == PseudoColor
189 || m_pVisual->c_class == StaticColor) {
190 int cColors = m_pVisual->map_entries;
191 XColor* rgColor = (XColor*) malloc (sizeof (XColor)*cColors);
192 memset (rgColor, 0, sizeof (XColor)*cColors);
193 for (int i = cColors; i--;) {
194 rgColor[i].pixel = i;
195 rgColor[i].flags = DoRed | DoGreen | DoBlue;
196 }
197 XQueryColors (m_pDisplay, colormap, rgColor, cColors);
198 unsigned long distanceLast = 0xfffffff;
199 int pixel = 0; // ** FIXME: shouldn't we detect error?
200 for (int i = 0; i < cColors; ++i) {
201 unsigned long distance = color_distance (color, rgColor[i]);
202 if (distance >= distanceLast)
203 continue;
204 pixel = i;
205 distanceLast = distance;
206 if (distance == 0)
207 break;
208 }
209 *pPixel = pixel;
210 fReturn = true;
211 free (rgColor);
212 }
213 else if ( m_pVisual->c_class == DirectColor
214 || m_pVisual->c_class == TrueColor) {
215 *pPixel = 0;
216 *pPixel |= (color.red >> m_shiftRed) & m_pVisual->red_mask;
217 *pPixel |= (color.green >> m_shiftGreen) & m_pVisual->green_mask;
218 *pPixel |= (color.blue >> m_shiftBlue) & m_pVisual->blue_mask;
219 fReturn = true;
220 }
221 return fReturn;
222 }
223
224
225 LWindow* LDisplay::find_template (const char* szTemplate)
226 {
227 LWindow* pWindow = NULL;
228 HashKey key = LHashTable::make_string_key (szTemplate);
229
230 m_hashTemplate.find (key, &pWindow);
231 return pWindow;
232 }
233
234
235 LWindow* LDisplay::find_window (Window window)
236 {
237 LWindow* pWindow = NULL;
238
239 m_hashWindow.find ((HashKey) window, &pWindow);
240 return pWindow;
241 }
242
243
244 GC LDisplay::gc (void)
245 {
246 XCopyGC (m_pDisplay, m_gcAttribute, (1 << (GCLastBit + 1)) - 1, m_gcDraw);
247 return m_gcDraw;
248 }
249
250
251 bool LDisplay::hash_template (const char* szTemplate, LWindow* pWindow)
252 {
253 HashKey key = LHashTable::make_string_key (szTemplate);
254 if (m_hashTemplate.find (key))
255 return false;
256
257 m_hashTemplate.add (key, &pWindow);
258 return true;
259 }
260
261 void LDisplay::hash_window (LWindow* pWindow)
262 {
263 Window window = pWindow->window ();
264
265 if (window)
266 m_hashWindow.add ((HashKey) window, &pWindow);
267 }
268
269
270 void LDisplay::release_this (void)
271 {
272 // FIXME we cannot *really* release
273 // the templates because they may
274 // allocate memory. This works for
275 // those that do nothing of the sort.
276 HashKey key;
277 LWindow** ppWindow;
278 for (unsigned32 index = 0;
279 index = m_hashTemplate.enumerate_reverse (index, &key,
280 (const void**) &ppWindow); ) {
281 delete *ppWindow;
282 m_hashTemplate.remove (key);
283 }
284
285 // -- Do the rest of the cleanup
286
287 m_font_cache.release_this ();
288
289 if (m_gcAttribute) {
290 XFreeGC (m_pDisplay, m_gcAttribute);
291 XFreeGC (m_pDisplay, m_gcDraw);
292 m_gcAttribute = m_gcDraw = 0;
293 }
294
295 if (m_pDisplay) {
296 XCloseDisplay (m_pDisplay);
297 m_pDisplay = 0;
298 }
299 }
300
301
302 void LDisplay::unhash_window (LWindow* pWindow)
303 {
304 Window window = pWindow->window ();
305
306 if (window)
307 m_hashWindow.remove ((HashKey) window);
308 }