<s> '''<STR_LIT>''' <EOL> import ctypes <EOL> import os <EOL> import signal <EOL> import struct <EOL> import threading <EOL> from pyglet . libs . x11 import xlib <EOL> from pyglet . compat import asbytes <EOL> try : <EOL> from pyglet . libs . x11 import xf86vmode <EOL> except : <EOL> pass <EOL> _restore_mode_child_installed = False <EOL> _restorable_screens = set ( ) <EOL> _mode_write_pipe = None <EOL> class ModePacket ( object ) : <EOL> format = '<STR_LIT>' <EOL> size = struct . calcsize ( format ) <EOL> def __init__ ( self , display , screen , width , height , rate ) : <EOL> self . display = display <EOL> self . screen = screen <EOL> self . width = width <EOL> self . height = height <EOL> self . rate = rate <EOL> def encode ( self ) : <EOL> return struct . pack ( self . format , self . display , self . screen , <EOL> self . width , self . height , self . rate ) <EOL> @ classmethod <EOL> def decode ( cls , data ) : <EOL> display , screen , width , height , rate = struct . unpack ( cls . format , data ) <EOL> return cls ( display . strip ( asbytes ( '<STR_LIT>' ) ) , screen , width , height , rate ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , self . display , self . screen , <EOL> self . width , self . height , self . rate ) <EOL> def set ( self ) : <EOL> display = xlib . XOpenDisplay ( self . display ) <EOL> modes , n_modes = get_modes_array ( display , self . screen ) <EOL> mode = get_matching_mode ( modes , n_modes , <EOL> self . width , self . height , self . rate ) <EOL> if mode is not None : <EOL> xf86vmode . XF86VidModeSwitchToMode ( display , self . screen , mode ) <EOL> free_modes_array ( modes , n_modes ) <EOL> xlib . XCloseDisplay ( display ) <EOL> def get_modes_array ( display , screen ) : <EOL> count = ctypes . c_int ( ) <EOL> modes = ctypes . POINTER ( ctypes . POINTER ( xf86vmode . XF86VidModeModeInfo ) ) ( ) <EOL> xf86vmode . XF86VidModeGetAllModeLines ( display , screen , count , modes ) <EOL> return modes , count . value <EOL> def get_matching_mode ( modes , n_modes , width , height , rate ) : <EOL> for i in range ( n_modes ) : <EOL> mode = modes . contents [ i ] <EOL> if ( mode . hdisplay == width and <EOL> mode . vdisplay == height and <EOL> mode . dotclock == rate ) : <EOL> return mode <EOL> return None <EOL> def free_modes_array ( modes , n_modes ) : <EOL> for i in range ( n_modes ) : <EOL> mode = modes . contents [ i ] <EOL> if mode . privsize : <EOL> xlib . XFree ( mode . private ) <EOL> xlib . XFree ( modes ) <EOL> def _install_restore_mode_child ( ) : <EOL> global _mode_write_pipe <EOL> global _restore_mode_child_installed <EOL> if _restore_mode_child_installed : <EOL> return <EOL> mode_read_pipe , _mode_write_pipe = os . pipe ( ) <EOL> if os . fork ( ) == <NUM_LIT:0> : <EOL> os . close ( _mode_write_pipe ) <EOL> PR_SET_PDEATHSIG = <NUM_LIT:1> <EOL> libc = ctypes . cdll . LoadLibrary ( '<STR_LIT>' ) <EOL> libc . prctl . argtypes = ( ctypes . c_int , ctypes . c_ulong , ctypes . c_ulong , <EOL> ctypes . c_ulong , ctypes . c_ulong ) <EOL> libc . prctl ( PR_SET_PDEATHSIG , signal . SIGHUP , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> def _sighup ( signum , frame ) : <EOL> parent_wait_lock . release ( ) ; <EOL> parent_wait_lock = threading . Lock ( ) ; <EOL> parent_wait_lock . acquire ( ) <EOL> signal . signal ( signal . SIGHUP , _sighup ) <EOL> packets = [ ] <EOL> buffer = asbytes ( '<STR_LIT>' ) <EOL> while parent_wait_lock . locked ( ) : <EOL> try : <EOL> data = os . read ( mode_read_pipe , ModePacket . size ) <EOL> buffer += data <EOL> while len ( buffer ) >= ModePacket . size : <EOL> packet = ModePacket . decode ( buffer [ : ModePacket . size ] ) <EOL> packets . append ( packet ) <EOL> buffer = buffer [ ModePacket . size : ] <EOL> except OSError : <EOL> pass <EOL> for packet in packets : <EOL> packet . set ( ) <EOL> os . _exit ( <NUM_LIT:0> ) <EOL> else : <EOL> os . close ( mode_read_pipe ) <EOL> _restore_mode_child_installed = True <EOL> def set_initial_mode ( mode ) : <EOL> _install_restore_mode_child ( ) <EOL> display = xlib . XDisplayString ( mode . screen . display . _display ) <EOL> screen = mode . screen . display . x_screen <EOL> if ( display , screen ) in _restorable_screens : <EOL> return <EOL> packet = ModePacket ( display , screen , mode . width , mode . height , mode . rate ) <EOL> os . write ( _mode_write_pipe , packet . encode ( ) ) <EOL> _restorable_screens . add ( ( display , screen ) ) </s>
