1 // Copyright (C)2004 Landmark Graphics Corporation
2 // Copyright (C)2005 Sun Microsystems, Inc.
3 // Copyright (C)2011, 2014, 2019-2020 D. R. Commander
4 //
5 // This library is free software and may be redistributed and/or modified under
6 // the terms of the wxWindows Library License, Version 3.1 or (at your option)
7 // any later version.  The full license is in the LICENSE.txt file included
8 // with this distribution.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // wxWindows Library License for more details.
14 
15 #ifndef __VISUALHASH_H__
16 #define __VISUALHASH_H__
17 
18 #include "glxvisual.h"
19 #include <X11/Xlib.h>
20 #include "Hash.h"
21 
22 
23 #define HASH  Hash<char *, XVisualInfo *, VGLFBConfig>
24 
25 // This maps a XVisualInfo * to a VGLFBConfig
26 
27 namespace vglfaker
28 {
29 	class VisualHash : public HASH
30 	{
31 		public:
32 
getInstance(void)33 			static VisualHash *getInstance(void)
34 			{
35 				if(instance == NULL)
36 				{
37 					vglutil::CriticalSection::SafeLock l(instanceMutex);
38 					if(instance == NULL) instance = new VisualHash;
39 				}
40 				return instance;
41 			}
42 
isAlloc(void)43 			static bool isAlloc(void) { return instance != NULL; }
44 
add(Display * dpy,XVisualInfo * vis,VGLFBConfig config)45 			void add(Display *dpy, XVisualInfo *vis, VGLFBConfig config)
46 			{
47 				if(!dpy || !vis || !config) THROW("Invalid argument");
48 				char *dpystring = strdup(DisplayString(dpy));
49 				if(!HASH::add(dpystring, vis, config))
50 					free(dpystring);
51 			}
52 
getConfig(Display * dpy,XVisualInfo * vis)53 			VGLFBConfig getConfig(Display *dpy, XVisualInfo *vis)
54 			{
55 				if(!dpy || !vis) THROW("Invalid argument");
56 				return HASH::find(DisplayString(dpy), vis);
57 			}
58 
remove(Display * dpy,XVisualInfo * vis)59 			void remove(Display *dpy, XVisualInfo *vis)
60 			{
61 				if(!vis) THROW("Invalid argument");
62 				HASH::remove(dpy ? DisplayString(dpy) : NULL, vis);
63 			}
64 
65 		private:
66 
~VisualHash(void)67 			~VisualHash(void)
68 			{
69 				HASH::kill();
70 			}
71 
compare(char * key1,XVisualInfo * key2,HashEntry * entry)72 			bool compare(char *key1, XVisualInfo *key2, HashEntry *entry)
73 			{
74 				return key2 == entry->key2
75 					&& (!key1 || !strcasecmp(key1, entry->key1));
76 			}
77 
detach(HashEntry * entry)78 			void detach(HashEntry *entry)
79 			{
80 				if(entry) free(entry->key1);
81 			}
82 
83 			static VisualHash *instance;
84 			static vglutil::CriticalSection instanceMutex;
85 	};
86 }
87 
88 #undef HASH
89 
90 
91 #define vishash  (*(vglfaker::VisualHash::getInstance()))
92 
93 #endif  // __VISUALHASH_H__
94