<commit_before>#!/usr/bin/env python

'''
'''

__docformat__ = 'restructuredtext'
__version__ = '$Id$'

from ctypes import *

from pyglet.window import *
from pyglet.window.event import *
from pyglet.window.key import *
from pyglet.window.xlib.constants import *
from pyglet.window.xlib.types import *
from pyglet.window.xlib.glx.VERSION_1_4 import *

# Load X11 library, specify argtypes and restype only when necessary.
Display = c_void_p
xlib = cdll.LoadLibrary('libX11.so')
xlib.XOpenDisplay.argtypes = [c_char_p]
xlib.XOpenDisplay.restype = POINTER(Display)
xlib.XNextEvent.argtypes = [POINTER(Display), POINTER(XEvent)]
xlib.XCheckTypedWindowEvent.argtypes = [POINTER(Display),
    c_ulong, c_int, POINTER(XEvent)]
xlib.XPutBackEvent.argtypes = [POINTER(Display), POINTER(XEvent)]

# Do we have the November 2000 UTF8 extension?
_have_utf8 = hasattr(xlib, 'Xutf8TextListToTextProperty')

class XlibException(WindowException):
    pass

class XlibWindowFactory(BaseWindowFactory):
    def __init__(self, display=None):
        super(XlibWindowFactory, self).__init__()

        self._display = xlib.XOpenDisplay(display)
        if not self._display:
            raise XlibException('Cannot connect to X server') 

    def create_config_prototype(self):
        return XlibGLConfig()

    def get_config_matches(self, window):
        return self.config._get_matches(self._display)

    def create_context(self, window, config, share_context=None):
        context = glXCreateNewContext(self._display, 
            config._fbconfig, GLX_RGBA_TYPE, share_context, True)
        if context == GLXBadContext:
            raise XlibException('Invalid context share')
        elif context == GLXBadFBConfig:
            raise XlibException('Invalid GL configuration')
        elif context < 0:
            raise XlibException('Could not create GL context') 

        window._glx_window = glXCreateWindow(self._display,
            config._fbconfig, window._window, None)
        return context

    def create_window(self, width, height):
        return XlibWindow(width, height, self._display)

class XlibWindow(BaseWindow):
    def __init__(self, width, height, display):
        super(XlibWindow, self).__init__()
        self._display = display

        black = xlib.XBlackPixel(self._display, 
            xlib.XDefaultScreen(self._display))
        self._window = xlib.XCreateSimpleWindow(self._display,
            xlib.XDefaultRootWindow(self._display), 
            0, 0, width, height, 0, black, black)

        xlib.XSelectInput(self._display, self._window, StructureNotifyMask)
        xlib.XMapWindow(self._display, self._window)

        # Wait for window mapping
        e = XEvent()
        while True:
            xlib.XNextEvent(self._display, e)
            if e.type == MapNotify:
                break

        # Select all events
        xlib.XSelectInput(self._display, self._window, 0x1ffffff)

    def close(self):
        glXDestroyContext(self._display, self.context)
        glXDestroyWindow(self._display, self._glx_window)
        xlib.XDestroyWindow(self._display, self._window)

    def switch_to(self):
        glXMakeContextCurrent(self._display,
            self._glx_window, self._glx_window, self.context)

    def flip(self):
        glXSwapBuffers(self._display, self._glx_window)

    def dispatch_events(self):
        e = XEvent()
        while xlib.XCheckWindowEvent(self._display, 
                                     self._window, 0x1ffffff, byref(e)):

            event_translater = _event_translaters.get(e.type, None)
            if event_translater:
                event_translater(self, e)

    def _set_property(self, name, value, allow_utf8=True):
        atom = xlib.XInternAtom(self._display, name, True)
        if not atom:
            raise XlibException('Undefined atom "%s"' % name)
        if type(value) in (str, unicode):
            property = XTextProperty()
            if _have_utf8 and allow_utf8:
                property.buf = create_string_buffer(value.encode('utf8'))
                result = xlib.Xutf8TextListToTextProperty(self._display,
                    byref(pointer(property.buf)), 1, XUTF8StringStyle, 
                    byref(property))
                if result < 0:
                    raise XlibException('Could not create UTF8 text property')
            else:
                property.buf = \
                    create_string_buffer(value.encode('ascii', 'ignore'))
                result = xlib.XStringListToTextProperty( 
                    byref(pointer(property.buf)), 1, byref(property))
                if result < 0:
                    raise XlibException('Could not create text property')
            xlib.XSetTextProperty(self._display, 
                self._window, byref(property), atom)
            xlib.XFree(property.value)

    def set_title(self, title):
        self._title = title
        self._set_property('WM_NAME', title, allow_utf8=False)<commit_msg>Don't unnecessarily attach the "buf" to property (it's not a valid structure attribute anyway and just confuses poor Richard).<commit_after>#!/usr/bin/env python

'''
'''

__docformat__ = 'restructuredtext'
__version__ = '$Id$'

from ctypes import *

from pyglet.window import *
from pyglet.window.event import *
from pyglet.window.key import *
from pyglet.window.xlib.constants import *
from pyglet.window.xlib.types import *
from pyglet.window.xlib.glx.VERSION_1_4 import *

# Load X11 library, specify argtypes and restype only when necessary.
Display = c_void_p
xlib = cdll.LoadLibrary('libX11.so')
xlib.XOpenDisplay.argtypes = [c_char_p]
xlib.XOpenDisplay.restype = POINTER(Display)
xlib.XNextEvent.argtypes = [POINTER(Display), POINTER(XEvent)]
xlib.XCheckTypedWindowEvent.argtypes = [POINTER(Display),
    c_ulong, c_int, POINTER(XEvent)]
xlib.XPutBackEvent.argtypes = [POINTER(Display), POINTER(XEvent)]

# Do we have the November 2000 UTF8 extension?
_have_utf8 = hasattr(xlib, 'Xutf8TextListToTextProperty')

class XlibException(WindowException):
    pass

class XlibWindowFactory(BaseWindowFactory):
    def __init__(self, display=None):
        super(XlibWindowFactory, self).__init__()

        self._display = xlib.XOpenDisplay(display)
        if not self._display:
            raise XlibException('Cannot connect to X server') 

    def create_config_prototype(self):
        return XlibGLConfig()

    def get_config_matches(self, window):
        return self.config._get_matches(self._display)

    def create_context(self, window, config, share_context=None):
        context = glXCreateNewContext(self._display, 
            config._fbconfig, GLX_RGBA_TYPE, share_context, True)
        if context == GLXBadContext:
            raise XlibException('Invalid context share')
        elif context == GLXBadFBConfig:
            raise XlibException('Invalid GL configuration')
        elif context < 0:
            raise XlibException('Could not create GL context') 

        window._glx_window = glXCreateWindow(self._display,
            config._fbconfig, window._window, None)
        return context

    def create_window(self, width, height):
        return XlibWindow(width, height, self._display)

class XlibWindow(BaseWindow):
    def __init__(self, width, height, display):
        super(XlibWindow, self).__init__()
        self._display = display

        black = xlib.XBlackPixel(self._display, 
            xlib.XDefaultScreen(self._display))
        self._window = xlib.XCreateSimpleWindow(self._display,
            xlib.XDefaultRootWindow(self._display), 
            0, 0, width, height, 0, black, black)

        xlib.XSelectInput(self._display, self._window, StructureNotifyMask)
        xlib.XMapWindow(self._display, self._window)

        # Wait for window mapping
        e = XEvent()
        while True:
            xlib.XNextEvent(self._display, e)
            if e.type == MapNotify:
                break

        # Select all events
        xlib.XSelectInput(self._display, self._window, 0x1ffffff)

    def close(self):
        glXDestroyContext(self._display, self.context)
        glXDestroyWindow(self._display, self._glx_window)
        xlib.XDestroyWindow(self._display, self._window)

    def switch_to(self):
        glXMakeContextCurrent(self._display,
            self._glx_window, self._glx_window, self.context)

    def flip(self):
        glXSwapBuffers(self._display, self._glx_window)

    def dispatch_events(self):
        e = XEvent()
        while xlib.XCheckWindowEvent(self._display, 
                                     self._window, 0x1ffffff, byref(e)):

            event_translater = _event_translaters.get(e.type, None)
            if event_translater:
                event_translater(self, e)

    def _set_property(self, name, value, allow_utf8=True):
        atom = xlib.XInternAtom(self._display, name, True)
        if not atom:
            raise XlibException('Undefined atom "%s"' % name)
        if type(value) in (str, unicode):
            property = XTextProperty()
            if _have_utf8 and allow_utf8:
                buf = create_string_buffer(value.encode('utf8'))
                result = xlib.Xutf8TextListToTextProperty(self._display,
                    byref(pointer(buf)), 1, XUTF8StringStyle, byref(property))
                if result < 0:
                    raise XlibException('Could not create UTF8 text property')
            else:
                buf = create_string_buffer(value.encode('ascii', 'ignore'))
                result = xlib.XStringListToTextProperty(byref(pointer(buf)),
                    1, byref(property))
                if result < 0:
                    raise XlibException('Could not create text property')
            xlib.XSetTextProperty(self._display, 
                self._window, byref(property), atom)
            # <rj> Xlib doesn't like us freeing this
            #xlib.XFree(property.value)

    def set_title(self, title):
        self._title = title
        self._set_property('WM_NAME', title, allow_utf8=False)
{'fraction_non_alphanumeric': 0.09527134876296887, 'fraction_numerical': 0.006584197924980048, 'mean_word_length': 2.673506779039941, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2621882', 'n_tokens_mistral': 3026, 'n_tokens_neox': 2896, 'n_words': 635}