Skip to content

Commit 3e244b3

Browse files
author
Jonathan Wright
committed
Try to match display to existing
1 parent ec2f425 commit 3e244b3

File tree

2 files changed

+219
-10
lines changed

2 files changed

+219
-10
lines changed

pyopengltk.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def printContext(self):
8282
bool( msk & GL.GL_CONTEXT_COMPATIBILITY_PROFILE_BIT) )
8383
except:
8484
print("Old context errors arose")
85-
# raise
85+
raise
8686

8787
def tkCreateContext( self ):
8888
# Platform dependent part
@@ -186,9 +186,11 @@ def tkSwapBuffers( self ):
186186

187187
if sys.platform.startswith( 'linux' ):
188188

189-
from ctypes import c_int, c_char_p, c_void_p, cdll, POINTER, util
189+
from ctypes import c_int, c_char_p, c_void_p, cdll, POINTER, util, \
190+
pointer
190191
from OpenGL import GLX
191192
from OpenGL.raw._GLX import Display
193+
import tk_read_XWindowAttributes
192194

193195
_x11lib = cdll.LoadLibrary(util.find_library( "X11" ) )
194196
XOpenDisplay = _x11lib.XOpenDisplay
@@ -228,12 +230,11 @@ def tkCreateContext( self ):
228230
minor = c_int(0)
229231
GLX.glXQueryVersion( self.__window, major, minor )
230232
print("GLX version:",major.value,minor.value)
231-
visual = GLX.glXChooseVisual( self.__window, 0,
232-
(GL.GLint * len(att))(* att) )
233-
if not visual:
234-
_log.error("glXChooseVisual call failed" )
235233
if major.value == 1 and minor.value < 3: # e.g. 1.2 and down
236-
print( visual.contents.visualid )
234+
visual = GLX.glXChooseVisual( self.__window, 0,
235+
(GL.GLint * len(att))(* att) )
236+
if not visual:
237+
_log.error("glXChooseVisual call failed" )
237238
self.__context = GLX.glXCreateContext(self.__window,
238239
visual,
239240
None,
@@ -242,20 +243,23 @@ def tkCreateContext( self ):
242243
ncfg = GL.GLint(0)
243244
XDefaultScreen = _x11lib.XDefaultScreen
244245
XDefaultScreen.argtypes = [POINTER(Display)]
245-
XOpenDisplay.restype = c_int
246+
XDefaultScreen.restype = c_int
246247
screen = XDefaultScreen( self.__window )
247248
print("Screen is ",screen)
248249
cfgs = GLX.glXChooseFBConfig( self.__window,
249250
screen,
250251
(GL.GLint * len(fbatt))(* fbatt),
251252
ncfg )
252253
print( "Number of configs",ncfg.value )
253-
print(visual.contents.visualid)
254+
xwa = tk_read_XWindowAttributes.getXWA(self._wid)
255+
256+
print("xwa....id" ,xwa.visual.contents.visualid)
257+
ideal = xwa.visual.contents.visualid
254258
best = -1
255259
for i in range(ncfg.value):
256260
vis = GLX.glXGetVisualFromFBConfig(self.__window, cfgs[i])
257261
# print("i %d visualid %d:" %(i,vis.contents.visualid))
258-
if visual.contents.visualid == vis.contents.visualid:
262+
if ideal == vis.contents.visualid:
259263
best = i
260264
if best >= 0:
261265
print("Got a matching visual?",best )

tk_read_XWindowAttributes.py

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
2+
3+
# Poke around to see about matching visuals...
4+
# From https://github.com/garrybodsworth/pyxlib-ctypes
5+
6+
from ctypes import *
7+
from ctypes import util
8+
libX11 = CDLL( util.find_library( "X11" ))
9+
10+
XPointer = c_char_p
11+
XID = c_ulong
12+
VisualID = c_ulong
13+
Window = XID
14+
Bool = c_int
15+
Colormap = XID
16+
Status = c_int
17+
18+
class _XExtData(Structure):
19+
pass
20+
_XExtData._fields_ = [
21+
('number', c_int),
22+
('next', POINTER(_XExtData)),
23+
('free_private', c_void_p),
24+
('private_data', XPointer),
25+
]
26+
XExtData = _XExtData
27+
28+
GContext = XID
29+
class _XGC(Structure):
30+
_fields_ = [
31+
('ext_data', POINTER(XExtData)),
32+
('gid', GContext),
33+
]
34+
GC = POINTER(_XGC)
35+
36+
class Visual(Structure):
37+
_fields_ = [
38+
('ext_data', POINTER(XExtData)),
39+
('visualid', VisualID),
40+
('c_class', c_int),
41+
('red_mask', c_ulong),
42+
('green_mask', c_ulong),
43+
('blue_mask', c_ulong),
44+
('bits_per_rgb', c_int),
45+
('map_entries', c_int),
46+
]
47+
48+
class Depth(Structure):
49+
_fields_ = [
50+
('depth', c_int),
51+
('nvisuals', c_int),
52+
('visuals', POINTER(Visual)),
53+
]
54+
55+
56+
class ScreenFormat(Structure):
57+
_fields_ = [
58+
('ext_data', POINTER(XExtData)),
59+
('depth', c_int),
60+
('bits_per_pixel', c_int),
61+
('scanline_pad', c_int),
62+
]
63+
64+
class _XrmHashBucketRec(Structure): pass
65+
66+
class _XPrivate(Structure): pass
67+
68+
class _XDisplay(Structure): pass
69+
70+
class Screen(Structure):
71+
_fields_ = [
72+
('ext_data', POINTER(XExtData)),
73+
('display', POINTER(_XDisplay)),
74+
('root', Window),
75+
('width', c_int),
76+
('height', c_int),
77+
('mwidth', c_int),
78+
('mheight', c_int),
79+
('ndepths', c_int),
80+
('depths', POINTER(Depth)),
81+
('root_depth', c_int),
82+
('root_visual', POINTER(Visual)),
83+
('default_gc', GC),
84+
('cmap', Colormap),
85+
('white_pixel', c_ulong),
86+
('black_pixel', c_ulong),
87+
('max_maps', c_int),
88+
('min_maps', c_int),
89+
('backing_store', c_int),
90+
('save_unders', Bool),
91+
('root_input_mask', c_long),
92+
]
93+
class _XDisplay(Structure): pass
94+
95+
_XDisplay._fields_ = [
96+
('ext_data', POINTER(XExtData)),
97+
('private1', POINTER(_XPrivate)),
98+
('fd', c_int),
99+
('private2', c_int),
100+
('proto_major_version', c_int),
101+
('proto_minor_version', c_int),
102+
('vendor', c_char_p),
103+
('private3', XID),
104+
('private4', XID),
105+
('private5', XID),
106+
('private6', c_int),
107+
('resource_alloc', c_void_p),
108+
('byte_order', c_int),
109+
('bitmap_unit', c_int),
110+
('bitmap_pad', c_int),
111+
('bitmap_bit_order', c_int),
112+
('nformats', c_int),
113+
('pixmap_format', POINTER(ScreenFormat)),
114+
('private8', c_int),
115+
('release', c_int),
116+
('private9', POINTER(_XPrivate)),
117+
('private10', POINTER(_XPrivate)),
118+
('qlen', c_int),
119+
('last_request_read', c_ulong),
120+
('request', c_ulong),
121+
('private11', XPointer),
122+
('private12', XPointer),
123+
('private13', XPointer),
124+
('private14', XPointer),
125+
('max_request_size', c_uint),
126+
('db', POINTER(_XrmHashBucketRec)),
127+
('private15', c_void_p),
128+
('display_name', c_char_p),
129+
('default_screen', c_int),
130+
('nscreens', c_int),
131+
('screens', POINTER(Screen)),
132+
('motion_buffer', c_ulong),
133+
('private16', c_ulong),
134+
('min_keycode', c_int),
135+
('max_keycode', c_int),
136+
('private17', XPointer),
137+
('private18', XPointer),
138+
('private19', c_int),
139+
('xdefaults', c_char_p),
140+
]
141+
Display = _XDisplay
142+
143+
144+
class XWindowAttributes(Structure):
145+
_fields_ = [
146+
('x', c_int),
147+
('y', c_int),
148+
('width', c_int),
149+
('height', c_int),
150+
('border_width', c_int),
151+
('depth', c_int),
152+
('visual', POINTER(Visual)),
153+
('root', Window),
154+
('c_class', c_int),
155+
('bit_gravity', c_int),
156+
('win_gravity', c_int),
157+
('backing_store', c_int),
158+
('backing_planes', c_ulong),
159+
('backing_pixel', c_ulong),
160+
('save_under', Bool),
161+
('colormap', Colormap),
162+
('map_installed', Bool),
163+
('map_state', c_int),
164+
('all_event_masks', c_long),
165+
('your_event_mask', c_long),
166+
('do_not_propagate_mask', c_long),
167+
('override_redirect', Bool),
168+
('screen', POINTER(Screen)),
169+
]
170+
171+
XGetWindowAttributes = libX11.XGetWindowAttributes
172+
XGetWindowAttributes.restype = Status
173+
XGetWindowAttributes.argtypes = [POINTER(Display), Window, POINTER(XWindowAttributes)]
174+
175+
XOpenDisplay = libX11.XOpenDisplay
176+
XOpenDisplay.restype = POINTER(Display)
177+
XOpenDisplay.argtypes = [c_char_p]
178+
179+
def getXWA( wid ):
180+
dpy = XOpenDisplay("")
181+
attrs = XWindowAttributes()
182+
status = XGetWindowAttributes( dpy, Window(wid), pointer(attrs) )
183+
return attrs
184+
185+
__all__ = [ getXWA ]
186+
187+
if __name__ == "__main__":
188+
import Tkinter
189+
root = Tkinter.Tk()
190+
frm = Tkinter.Frame(root, width=200, height=200, bg="" )
191+
frm.pack()
192+
193+
def testframe():
194+
dpy = XOpenDisplay("")
195+
attrs = XWindowAttributes()
196+
print dpy
197+
print attrs
198+
status = XGetWindowAttributes( dpy, Window(frm.winfo_id()), pointer(attrs) )
199+
print status
200+
print attrs.visual.contents.bits_per_rgb
201+
print attrs.visual.contents.visualid
202+
203+
frm.after(100, testframe)
204+
root.mainloop()
205+

0 commit comments

Comments
 (0)