def set_window_focus_callback(window, cbfun):
'''
Sets the focus callback for the specified window.
Wrapper for:
GLFWwindowfocusfun glfwSetWindowFocusCallback(GLFWwindow* window, GLFWwindowfocusfun cbfun);
'''
window_addr = ctypes.cast(ctypes.pointer(window),
ctypes.POINTER(ctypes.c_long)).contents.value
if window_addr in _window_focus_callback_repository:
previous_callback = _window_focus_callback_repository[window_addr]
else:
previous_callback = None
if cbfun is None:
cbfun = 0
c_cbfun = _GLFWwindowfocusfun(cbfun)
_window_focus_callback_repository[window_addr] = (cbfun, c_cbfun)
cbfun = c_cbfun
_glfw.glfwSetWindowFocusCallback(window, cbfun)
if previous_callback is not None and previous_callback[0] != 0:
return previous_callback[0]
python类cast()的实例源码
def set_window_iconify_callback(window, cbfun):
'''
Sets the iconify callback for the specified window.
Wrapper for:
GLFWwindowiconifyfun glfwSetWindowIconifyCallback(GLFWwindow* window, GLFWwindowiconifyfun cbfun);
'''
window_addr = ctypes.cast(ctypes.pointer(window),
ctypes.POINTER(ctypes.c_long)).contents.value
if window_addr in _window_iconify_callback_repository:
previous_callback = _window_iconify_callback_repository[window_addr]
else:
previous_callback = None
if cbfun is None:
cbfun = 0
c_cbfun = _GLFWwindowiconifyfun(cbfun)
_window_iconify_callback_repository[window_addr] = (cbfun, c_cbfun)
cbfun = c_cbfun
_glfw.glfwSetWindowIconifyCallback(window, cbfun)
if previous_callback is not None and previous_callback[0] != 0:
return previous_callback[0]
def set_framebuffer_size_callback(window, cbfun):
'''
Sets the framebuffer resize callback for the specified window.
Wrapper for:
GLFWframebuffersizefun glfwSetFramebufferSizeCallback(GLFWwindow* window, GLFWframebuffersizefun cbfun);
'''
window_addr = ctypes.cast(ctypes.pointer(window),
ctypes.POINTER(ctypes.c_long)).contents.value
if window_addr in _framebuffer_size_callback_repository:
previous_callback = _framebuffer_size_callback_repository[window_addr]
else:
previous_callback = None
if cbfun is None:
cbfun = 0
c_cbfun = _GLFWframebuffersizefun(cbfun)
_framebuffer_size_callback_repository[window_addr] = (cbfun, c_cbfun)
cbfun = c_cbfun
_glfw.glfwSetFramebufferSizeCallback(window, cbfun)
if previous_callback is not None and previous_callback[0] != 0:
return previous_callback[0]
def set_key_callback(window, cbfun):
'''
Sets the key callback.
Wrapper for:
GLFWkeyfun glfwSetKeyCallback(GLFWwindow* window, GLFWkeyfun cbfun);
'''
window_addr = ctypes.cast(ctypes.pointer(window),
ctypes.POINTER(ctypes.c_long)).contents.value
if window_addr in _key_callback_repository:
previous_callback = _key_callback_repository[window_addr]
else:
previous_callback = None
if cbfun is None:
cbfun = 0
c_cbfun = _GLFWkeyfun(cbfun)
_key_callback_repository[window_addr] = (cbfun, c_cbfun)
cbfun = c_cbfun
_glfw.glfwSetKeyCallback(window, cbfun)
if previous_callback is not None and previous_callback[0] != 0:
return previous_callback[0]
def set_char_callback(window, cbfun):
'''
Sets the Unicode character callback.
Wrapper for:
GLFWcharfun glfwSetCharCallback(GLFWwindow* window, GLFWcharfun cbfun);
'''
window_addr = ctypes.cast(ctypes.pointer(window),
ctypes.POINTER(ctypes.c_long)).contents.value
if window_addr in _char_callback_repository:
previous_callback = _char_callback_repository[window_addr]
else:
previous_callback = None
if cbfun is None:
cbfun = 0
c_cbfun = _GLFWcharfun(cbfun)
_char_callback_repository[window_addr] = (cbfun, c_cbfun)
cbfun = c_cbfun
_glfw.glfwSetCharCallback(window, cbfun)
if previous_callback is not None and previous_callback[0] != 0:
return previous_callback[0]
def set_cursor_pos_callback(window, cbfun):
'''
Sets the cursor position callback.
Wrapper for:
GLFWcursorposfun glfwSetCursorPosCallback(GLFWwindow* window, GLFWcursorposfun cbfun);
'''
window_addr = ctypes.cast(ctypes.pointer(window),
ctypes.POINTER(ctypes.c_long)).contents.value
if window_addr in _cursor_pos_callback_repository:
previous_callback = _cursor_pos_callback_repository[window_addr]
else:
previous_callback = None
if cbfun is None:
cbfun = 0
c_cbfun = _GLFWcursorposfun(cbfun)
_cursor_pos_callback_repository[window_addr] = (cbfun, c_cbfun)
cbfun = c_cbfun
_glfw.glfwSetCursorPosCallback(window, cbfun)
if previous_callback is not None and previous_callback[0] != 0:
return previous_callback[0]
def set_cursor_enter_callback(window, cbfun):
'''
Sets the cursor enter/exit callback.
Wrapper for:
GLFWcursorenterfun glfwSetCursorEnterCallback(GLFWwindow* window, GLFWcursorenterfun cbfun);
'''
window_addr = ctypes.cast(ctypes.pointer(window),
ctypes.POINTER(ctypes.c_long)).contents.value
if window_addr in _cursor_enter_callback_repository:
previous_callback = _cursor_enter_callback_repository[window_addr]
else:
previous_callback = None
if cbfun is None:
cbfun = 0
c_cbfun = _GLFWcursorenterfun(cbfun)
_cursor_enter_callback_repository[window_addr] = (cbfun, c_cbfun)
cbfun = c_cbfun
_glfw.glfwSetCursorEnterCallback(window, cbfun)
if previous_callback is not None and previous_callback[0] != 0:
return previous_callback[0]
def set_scroll_callback(window, cbfun):
'''
Sets the scroll callback.
Wrapper for:
GLFWscrollfun glfwSetScrollCallback(GLFWwindow* window, GLFWscrollfun cbfun);
'''
window_addr = ctypes.cast(ctypes.pointer(window),
ctypes.POINTER(ctypes.c_long)).contents.value
if window_addr in _scroll_callback_repository:
previous_callback = _scroll_callback_repository[window_addr]
else:
previous_callback = None
if cbfun is None:
cbfun = 0
c_cbfun = _GLFWscrollfun(cbfun)
_scroll_callback_repository[window_addr] = (cbfun, c_cbfun)
cbfun = c_cbfun
_glfw.glfwSetScrollCallback(window, cbfun)
if previous_callback is not None and previous_callback[0] != 0:
return previous_callback[0]
def render(self):
if not self.data:
return
self.gui_lock.acquire()
rect = self.get_rect()
arr = (ctypes.c_double*3)(0, 0, 0)
mjlib.mjv_makeGeoms(self.model.ptr, self.data.ptr, byref(self.objects), byref(self.vopt), mjCAT_ALL, 0, None, None, ctypes.cast(arr, ctypes.POINTER(ctypes.c_double)))
mjlib.mjv_makeLights(self.model.ptr, self.data.ptr, byref(self.objects))
mjlib.mjv_setCamera(self.model.ptr, self.data.ptr, byref(self.cam))
mjlib.mjv_updateCameraPose(byref(self.cam), rect.width*1.0/rect.height)
mjlib.mjr_render(0, rect, byref(self.objects), byref(self.ropt), byref(self.cam.pose), byref(self.con))
self.gui_lock.release()
def cast_2d_list_to_2d_pointer(list_value_2d, c_type=ctypes.c_double):
'''Cast Python 2d-list to 2d-pointer of c_types
Args:
list_value_2d (List[List[:object:]]): 2d-Python list
c_type (ctypes, default: ctypes.c_double): target type
Returns:
POINTER(POINTER(c_type)): ctypes 2d-pointer
'''
inner_arr_type = c_type * len(list_value_2d[0])
outer_arr_type = inner_arr_type * len(list_value_2d)
tmp_buffer = outer_arr_type()
pointer_2d = ctypes.cast(tmp_buffer, ctypes.POINTER(ctypes.POINTER(c_type)))
outer_size = len(list_value_2d)
for i in range(outer_size):
pointer_2d[i] = ctypes.cast(inner_arr_type(*list_value_2d[i]), ctypes.POINTER(c_type))
return pointer_2d
def load_name(self, offset):
"""
Load a timezone name from a DLL offset (integer).
>>> from dateutil.tzwin import tzres
>>> tzr = tzres()
>>> print(tzr.load_name(112))
'Eastern Standard Time'
:param offset:
A positive integer value referring to a string from the tzres dll.
..note:
Offsets found in the registry are generally of the form
`@tzres.dll,-114`. The offset in this case if 114, not -114.
"""
resource = self.p_wchar()
lpBuffer = ctypes.cast(ctypes.byref(resource), wintypes.LPWSTR)
nchar = self.LoadStringW(self._tzres._handle, offset, lpBuffer, 0)
return resource[:nchar]
def load_name(self, offset):
"""
Load a timezone name from a DLL offset (integer).
>>> from dateutil.tzwin import tzres
>>> tzr = tzres()
>>> print(tzr.load_name(112))
'Eastern Standard Time'
:param offset:
A positive integer value referring to a string from the tzres dll.
..note:
Offsets found in the registry are generally of the form
`@tzres.dll,-114`. The offset in this case if 114, not -114.
"""
resource = self.p_wchar()
lpBuffer = ctypes.cast(ctypes.byref(resource), wintypes.LPWSTR)
nchar = self.LoadStringW(self._tzres._handle, offset, lpBuffer, 0)
return resource[:nchar]
def tracks_get(self):
"""Get media descriptor's elementary streams description
Note, you need to call L{parse}() or play the media at least once
before calling this function.
Not doing this will result in an empty array.
The result must be freed with L{tracks_release}.
@version: LibVLC 2.1.0 and later.
"""
mediaTrack_pp = ctypes.POINTER(MediaTrack)()
n = libvlc_media_tracks_get(self, ctypes.byref(mediaTrack_pp))
info = ctypes.cast(mediaTrack_pp, ctypes.POINTER(ctypes.POINTER(MediaTrack) * n))
try:
contents = info.contents
except ValueError:
# Media not parsed, no info.
return None
tracks = ( contents[i].contents for i in range(len(contents)) )
# libvlc_media_tracks_release(mediaTrack_pp, n)
return tracks
def __str__(self):
longest_name = max([len(k[0]) for k in self._fields_])
indent_fmt = '{{:>{}s}}: {{}}\n'.format(longest_name)
indent_verbose_fmt = '{{:>{}s}}: {{}} ({{}})\n'.format(longest_name)
out = ''
for k in self._fields_:
value = getattr(self, k[0])
if k[0] in ('chaddr', 'sname', 'file'):
value = ctypes.cast(value, c_char_p).value
out += indent_fmt.format(k[0], value.hex() if PY3 else value.encode('hex'))
elif k[0] in ('xid'):
out += indent_verbose_fmt.format(k[0], value, hex(value).rstrip('L'))
elif k[0] in ('flags'):
out += indent_verbose_fmt.format(k[0], value, 'broadcast' if value & 0x8000 else hex(value))
elif k[0] in ('op'):
out += indent_verbose_fmt.format(k[0], value, opcodes.get(value, '?'))
elif k[0] in ('htype'):
out += indent_verbose_fmt.format(k[0], value, hw_types.get(value, '?'))
elif k[0] in ('ciaddr', 'yiaddr', 'siaddr', 'giaddr'):
out += indent_verbose_fmt.format(k[0], value, int_to_ip(value))
else:
out += indent_fmt.format(k[0], value)
return out.rstrip()
def load_name(self, offset):
"""
Load a timezone name from a DLL offset (integer).
>>> from dateutil.tzwin import tzres
>>> tzr = tzres()
>>> print(tzr.load_name(112))
'Eastern Standard Time'
:param offset:
A positive integer value referring to a string from the tzres dll.
..note:
Offsets found in the registry are generally of the form
`@tzres.dll,-114`. The offset in this case if 114, not -114.
"""
resource = self.p_wchar()
lpBuffer = ctypes.cast(ctypes.byref(resource), wintypes.LPWSTR)
nchar = self.LoadStringW(self._tzres._handle, offset, lpBuffer, 0)
return resource[:nchar]
def gcp(self, cdata, destructor):
BType = self.typeof(cdata)
if destructor is None:
if not (hasattr(BType, '_gcp_type') and
BType._gcp_type is BType):
raise TypeError("Can remove destructor only on a object "
"previously returned by ffi.gc()")
cdata._destructor = None
return None
try:
gcp_type = BType._gcp_type
except AttributeError:
class CTypesDataGcp(BType):
__slots__ = ['_orig', '_destructor']
def __del__(self):
if self._destructor is not None:
self._destructor(self._orig)
gcp_type = BType._gcp_type = CTypesDataGcp
new_cdata = self.cast(gcp_type, cdata)
new_cdata._orig = cdata
new_cdata._destructor = destructor
return new_cdata
def rawaddressof(self, BTypePtr, cdata, offset=None):
if isinstance(cdata, CTypesBaseStructOrUnion):
ptr = ctypes.pointer(type(cdata)._to_ctypes(cdata))
elif isinstance(cdata, CTypesGenericPtr):
if offset is None or not issubclass(type(cdata)._BItem,
CTypesBaseStructOrUnion):
raise TypeError("unexpected cdata type")
ptr = type(cdata)._to_ctypes(cdata)
elif isinstance(cdata, CTypesGenericArray):
ptr = type(cdata)._to_ctypes(cdata)
else:
raise TypeError("expected a <cdata 'struct-or-union'>")
if offset:
ptr = ctypes.cast(
ctypes.c_void_p(
ctypes.cast(ptr, ctypes.c_void_p).value + offset),
type(ptr))
return BTypePtr._from_ctypes(ptr)
def _cf_string_to_unicode(value):
"""
Creates a Unicode string from a CFString object. Used entirely for error
reporting.
Yes, it annoys me quite a lot that this function is this complex.
"""
value_as_void_p = ctypes.cast(value, ctypes.POINTER(ctypes.c_void_p))
string = CoreFoundation.CFStringGetCStringPtr(
value_as_void_p,
CFConst.kCFStringEncodingUTF8
)
if string is None:
buffer = ctypes.create_string_buffer(1024)
result = CoreFoundation.CFStringGetCString(
value_as_void_p,
buffer,
1024,
CFConst.kCFStringEncodingUTF8
)
if not result:
raise OSError('Error copying C string from CFStringRef')
string = buffer.value
if string is not None:
string = string.decode('utf-8')
return string
def get_global(self, name, typ):
'''Return a global address'''
addr = self.engine.get_global_value_address(name)
ptr = ct.cast(ct.c_void_p(addr), typ)
return ptr
# Runtime configuration #####################################################
def to_rain(self, val):
if isinstance(val, (list, tuple)):
table_box = T.cbox.to_rain(None)
self.rain_set_table(table_box)
meta_ptr = self.get_global('core.types.array.module', T.carg)
self.rain_set_env(table_box, meta_ptr)
for i, n in enumerate(val):
self.rain_put_py(table_box, i, self.to_rain(n))
return table_box
elif isinstance(val, A.node):
table_box = T.cbox.to_rain(None)
self.rain_set_table(table_box)
ast_ptr = self.get_global('core.ast.module', T.carg)
meta_ptr = self.rain_get_ptr_py(ast_ptr, val.__tag__)
if not ct.cast(meta_ptr, ct.c_void_p).value:
Q.abort('Unable to look up core.ast.{}'.format(val.__tag__))
self.rain_set_env(table_box, meta_ptr)
slots = [self.to_rain(getattr(val, key, None)) for key in val.__slots__]
for key, box in zip(val.__slots__, slots):
self.rain_put_py(table_box, key, box)
return table_box
elif isinstance(val, K.value_token):
return T.cbox.to_rain(val.value)
return T.cbox.to_rain(val)