| 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 |
#include <sys/select.h> |
| 38 |
#include <sys/time.h> |
| 39 |
|
| 40 |
unsigned long LDisplay::color_distance (XColor& color, XColor& colorMatch) |
| 41 |
{ |
| 42 |
unsigned long sh = 16 - m_pVisual->bits_per_rgb; |
| 43 |
//unsigned long mask = (0xffff ^ ((1 << (16 - m_pVisual->bits_per_rgb)) - 1)); |
| 44 |
unsigned long distRed = (color.red >> sh) - (colorMatch.red >> sh); |
| 45 |
unsigned long distGreen = (color.green >> sh) - (colorMatch.green >> sh); |
| 46 |
unsigned long distBlue = (color.blue >> sh) - (colorMatch.blue >> sh); |
| 47 |
return distRed*distRed + distGreen*distGreen + distBlue*distBlue; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
void LDisplay::dispatch (XEvent* pEvent) |
| 52 |
{ |
| 53 |
LWindow* pWindow = find_window (pEvent->xany.window); |
| 54 |
if (!pWindow) |
| 55 |
return; |
| 56 |
PFNEvent pfn = pWindow->find_event (pEvent->type, pEvent); |
| 57 |
if (!pfn) |
| 58 |
return; |
| 59 |
(pWindow->*pfn) (pEvent); |
| 60 |
} |
| 61 |
|
| 62 |
void LDisplay::dispatch_next_event (void) |
| •63 |
{ |
| 64 |
dispatch_next_event_timeout (-1); |
| 65 |
} |
| 66 |
|
| 67 |
void LDisplay::dispatch_next_event_timeout (long usec) |
| 68 |
{ |
| 69 |
XEvent event; |
| •70 |
if (!next_event_timeout (&event, usec)) |
| 71 |
return; |
| 72 |
|
| 73 |
// Update keyboard mapping |
| 74 |
if (event.type == MappingKeyboard) { |
| 75 |
XRefreshKeyboardMapping ((XMappingEvent*) &event); |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
//fprintf (stderr, "event %d window 0x%lx\n", event.type, event.xany.window); |
| 80 |
LWindow* pWindow = find_window (event.xany.window); |
| 81 |
if (!pWindow) |
| 82 |
return; |
| 83 |
PFNEvent pfn = pWindow->find_event (event.type, &event); |
| 84 |
if (!pfn) |
| 85 |
return; |
| 86 |
(pWindow->*pfn) (&event); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
LWindow* LDisplay::find_child (int id) |
| 91 |
{ |
| 92 |
return m_pWindowRoot ? m_pWindowRoot->find_sibling (id) : (LWindow*) NULL; |
| 93 |
} |
| 94 |
|
| •95 |
bool LDisplay::next_event_timeout (XEvent* pEvent, long usec) |
| 96 |
{ |
| 97 |
fd_set fds; |
| 98 |
int r, fd; |
| 99 |
struct timeval tv; |
| 100 |
|
| 101 |
if (!m_pDisplay) |
| 102 |
return false; |
| 103 |
|
| 104 |
if (usec < 0 || XPending (m_pDisplay)) { |
| 105 |
XNextEvent (m_pDisplay, pEvent); |
| 106 |
return true; |
| 107 |
} |
| 108 |
|
| 109 |
fd = ConnectionNumber (m_pDisplay); |
| 110 |
FD_ZERO (&fds); |
| 111 |
FD_SET (fd, &fds); |
| 112 |
tv.tv_sec = 0; |
| 113 |
tv.tv_usec = usec; |
| 114 |
|
| 115 |
r = select (fd + 1, &fds, NULL, NULL, &tv); |
| 116 |
|
| 117 |
if (r > 0 && XPending (m_pDisplay)) { |
| 118 |
XNextEvent (m_pDisplay, pEvent); |
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
bool LDisplay::open (char* szDisplay) |
| 126 |
{ |
| 127 |
m_pDisplay = XOpenDisplay (szDisplay); |
| 128 |
|
| 129 |
if (!m_pDisplay) |
| 130 |
return false; |
| 131 |
|
| 132 |
m_cbitUnit = XBitmapUnit (m_pDisplay); |
| 133 |
m_cbitPadding = XBitmapPad (m_pDisplay); |
| 134 |
m_depth = XDefaultDepth (m_pDisplay, 0); |
| 135 |
|
| 136 |
m_hashWindow.init (sizeof (LWindow*), 301); |
| 137 |
m_font_cache.init (this); |
| 138 |
m_hashTemplate.init (sizeof (LWindow*), 301); |
| 139 |
// dmalloc_validate (); |
| 140 |
|
| 141 |
// -- Get the resource database |
| 142 |
{ |
| 143 |
char* szDatabase = XResourceManagerString (m_pDisplay); |
| 144 |
// ** FIXME ** Seems to me that this is a really bad problem if this fails |
| 145 |
if (szDatabase) |
| 146 |
m_database = XrmGetStringDatabase (szDatabase); |
| 147 |
} |
| 148 |
|
| 149 |
if (!m_visual_class) |
| 150 |
m_visual_class = TrueColor; |
| 151 |
XMatchVisualInfo (m_pDisplay, 0, m_depth, m_visual_class, &m_visualInfo); |
| 152 |
m_pVisual = (m_visualInfo.visual |
| 153 |
? m_visualInfo.visual |
| 154 |
: XDefaultVisual (m_pDisplay, 0)); |
| 155 |
m_colormap = ((m_visualInfo.c_class == TrueColor) |
| 156 |
? XCreateColormap (m_pDisplay, |
| 157 |
XDefaultRootWindow (m_pDisplay), |
| 158 |
m_pVisual, AllocNone) |
| 159 |
: CopyFromParent); |
| 160 |
#if defined (TALK) |
| 161 |
printf ("using visualID 0x%lx\n", |
| 162 |
XVisualIDFromVisual (m_pVisual)); |
| 163 |
#endif |
| 164 |
|
| 165 |
if (m_pVisual->red_mask) { |
| 166 |
unsigned mask; |
| 167 |
for (m_shiftRed = 0, mask = m_pVisual->red_mask; |
| 168 |
!(mask & 0x8000) && mask; ++m_shiftRed, (mask <<= 1)) |
| 169 |
; |
| 170 |
for (m_shiftGreen = 0, mask = m_pVisual->green_mask; |
| 171 |
!(mask & 0x8000) && mask; ++m_shiftGreen, (mask <<= 1)) |
| 172 |
; |
| 173 |
for (m_shiftBlue = 0, mask = m_pVisual->blue_mask; |
| 174 |
!(mask & 0x8000) && mask; ++m_shiftBlue, (mask <<= 1)) |
| 175 |
; |
| 176 |
} |
| 177 |
|
| 178 |
// Cache some GCs |
| 179 |
XGCValues values; |
| 180 |
unsigned long value_mask = 0; |
| 181 |
|
| 182 |
values.foreground = XWhitePixel (m_pDisplay, 0); |
| 183 |
values.background = XBlackPixel (m_pDisplay, 0); |
| 184 |
value_mask |= GCForeground | GCBackground; |
| 185 |
m_gcAttribute = XCreateGC (m_pDisplay, XDefaultRootWindow (m_pDisplay), |
| 186 |
value_mask, &values); |
| 187 |
m_gcDraw = XCreateGC (m_pDisplay, XDefaultRootWindow (m_pDisplay), |
| 188 |
0, 0); |
| 189 |
|
| 190 |
// Evaluate pixmap format |
| 191 |
#if defined (TALK) |
| 192 |
{ |
| 193 |
int c; |
| 194 |
XPixmapFormatValues* pFormat |
| 195 |
= XListPixmapFormats (m_pDisplay, &c); |
| 196 |
for (int i = 0; i < c; ++i) |
| 197 |
printf ("pixmap depth %d bits_per_pixel %d scanline_pad %d\n", |
| 198 |
pFormat[i].depth, |
| 199 |
pFormat[i].bits_per_pixel, |
| 200 |
pFormat[i].scanline_pad); |
| 201 |
XFree (pFormat); |
| 202 |
|
| 203 |
printf ("bitmap unit %d bitmap pad %d\n", |
| 204 |
XBitmapUnit (m_pDisplay), XBitmapPad (m_pDisplay)); |
| 205 |
|
| 206 |
printf ("image byte order %s bit order %s\n", |
| 207 |
XImageByteOrder (m_pDisplay) ? "msb" : "lsb", |
| 208 |
XBitmapBitOrder (m_pDisplay) ? "msb" : "lsb"); |
| 209 |
|
| 210 |
} |
| 211 |
#endif |
| 212 |
|
| 213 |
// dmalloc_validate (); |
| 214 |
|
| 215 |
return true; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
bool LDisplay::find_pixel (char* szColorName, unsigned long* pPixel) |
| 220 |
{ |
| 221 |
Colormap colormap = XDefaultColormap (m_pDisplay, 0); |
| 222 |
XColor color; |
| 223 |
XParseColor (m_pDisplay, colormap, szColorName, &color); |
| 224 |
bool fReturn = false; |
| 225 |
|
| 226 |
if ( m_pVisual->c_class == PseudoColor |
| 227 |
|| m_pVisual->c_class == StaticColor) { |
| 228 |
int cColors = m_pVisual->map_entries; |
| 229 |
XColor* rgColor = (XColor*) malloc (sizeof (XColor)*cColors); |
| 230 |
memset (rgColor, 0, sizeof (XColor)*cColors); |
| 231 |
for (int i = cColors; i--;) { |
| 232 |
rgColor[i].pixel = i; |
| 233 |
rgColor[i].flags = DoRed | DoGreen | DoBlue; |
| 234 |
} |
| 235 |
XQueryColors (m_pDisplay, colormap, rgColor, cColors); |
| 236 |
unsigned long distanceLast = 0xfffffff; |
| 237 |
int pixel = 0; // ** FIXME: shouldn't we detect error? |
| 238 |
for (int i = 0; i < cColors; ++i) { |
| 239 |
unsigned long distance = color_distance (color, rgColor[i]); |
| 240 |
if (distance >= distanceLast) |
| 241 |
continue; |
| 242 |
pixel = i; |
| 243 |
distanceLast = distance; |
| 244 |
if (distance == 0) |
| 245 |
break; |
| 246 |
} |
| 247 |
*pPixel = pixel; |
| 248 |
fReturn = true; |
| 249 |
free (rgColor); |
| 250 |
} |
| 251 |
else if ( m_pVisual->c_class == DirectColor |
| 252 |
|| m_pVisual->c_class == TrueColor) { |
| 253 |
*pPixel = 0; |
| 254 |
*pPixel |= (color.red >> m_shiftRed) & m_pVisual->red_mask; |
| 255 |
*pPixel |= (color.green >> m_shiftGreen) & m_pVisual->green_mask; |
| 256 |
*pPixel |= (color.blue >> m_shiftBlue) & m_pVisual->blue_mask; |
| 257 |
fReturn = true; |
| 258 |
} |
| 259 |
return fReturn; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
LWindow* LDisplay::find_template (const char* szTemplate) |
| 264 |
{ |
| 265 |
LWindow* pWindow = NULL; |
| 266 |
HashKey key = LHashTable::make_string_key (szTemplate); |
| 267 |
|
| 268 |
m_hashTemplate.find (key, &pWindow); |
| 269 |
return pWindow; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
LWindow* LDisplay::find_window (Window window) |
| 274 |
{ |
| 275 |
LWindow* pWindow = NULL; |
| 276 |
|
| 277 |
m_hashWindow.find ((HashKey) window, &pWindow); |
| 278 |
return pWindow; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
GC LDisplay::gc (void) |
| 283 |
{ |
| 284 |
XCopyGC (m_pDisplay, m_gcAttribute, (1 << (GCLastBit + 1)) - 1, m_gcDraw); |
| 285 |
return m_gcDraw; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
bool LDisplay::hash_template (const char* szTemplate, LWindow* pWindow) |
| 290 |
{ |
| 291 |
HashKey key = LHashTable::make_string_key (szTemplate); |
| 292 |
if (m_hashTemplate.find (key)) |
| 293 |
return false; |
| 294 |
|
| 295 |
m_hashTemplate.add (key, &pWindow); |
| 296 |
return true; |
| 297 |
} |
| 298 |
|
| 299 |
void LDisplay::hash_window (LWindow* pWindow) |
| 300 |
{ |
| 301 |
Window window = pWindow->window (); |
| 302 |
|
| 303 |
if (window) |
| 304 |
m_hashWindow.add ((HashKey) window, &pWindow); |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
void LDisplay::release_this (void) |
| 309 |
{ |
| 310 |
// FIXME we cannot *really* release |
| 311 |
// the templates because they may |
| 312 |
// allocate memory. This works for |
| 313 |
// those that do nothing of the sort. |
| 314 |
HashKey key; |
| 315 |
LWindow** ppWindow; |
| 316 |
for (unsigned32 index = 0; |
| 317 |
index = m_hashTemplate.enumerate_reverse (index, &key, |
| 318 |
(const void**) &ppWindow); ) { |
| 319 |
delete *ppWindow; |
| 320 |
m_hashTemplate.remove (key); |
| 321 |
} |
| 322 |
|
| 323 |
// -- Do the rest of the cleanup |
| 324 |
|
| 325 |
m_font_cache.release_this (); |
| 326 |
|
| 327 |
if (m_gcAttribute) { |
| 328 |
XFreeGC (m_pDisplay, m_gcAttribute); |
| 329 |
XFreeGC (m_pDisplay, m_gcDraw); |
| 330 |
m_gcAttribute = m_gcDraw = 0; |
| 331 |
} |
| 332 |
|
| 333 |
if (m_pDisplay) { |
| 334 |
XCloseDisplay (m_pDisplay); |
| 335 |
m_pDisplay = 0; |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
void LDisplay::unhash_window (LWindow* pWindow) |
| 341 |
{ |
| 342 |
Window window = pWindow->window (); |
| 343 |
|
| 344 |
if (window) |
| 345 |
m_hashWindow.remove ((HashKey) window); |
| 346 |
} |