def main(): sdl2.sdl2_load( ctypes.util.find_library('SDL2')) # '/usr/local/lib/libSDL2.dylib' sdl2.SDL_Init(sdl2.SDL_INIT_EVERYTHING) # sdl2.SDL_SetMainReady() version = sdl2.SDL_version() sdl2.SDL_GetVersion(ctypes.byref(version)) print('Major, Minor and Patch: ', version.major, version.minor, version.patch) print('Revision and its Number: ', sdl2.SDL_GetRevision(), sdl2.SDL_GetRevisionNumber()) print('Base Path: ', sdl2.SDL_GetBasePath()) print('Touch Devices: ', sdl2.SDL_GetNumTouchDevices()) print('Power State: ', sdl2.SDL_GetPowerInfo(None, None)) print('Have AVX: ', sdl2.SDL_HasAVX()) print('System RAM: ', sdl2.SDL_GetSystemRAM()) print('Platform: ', sdl2.SDL_GetPlatform()) print('Pixel Format: ', sdl2.SDL_GetPixelFormatName(sdl2.SDL_PIXELFORMAT_RGBA32)) # Test features added in 2.0.10 # Ref.: https://discourse.libsdl.org/t/sdl-2-0-10-released/26429 print('Touch Device (0) Type: ', sdl2.SDL_GetTouchDeviceType(sdl2.SDL_GetTouchDevice(0))) print('SIMD Alignment: ', sdl2.SDL_SIMDGetAlignment()) sdl2.SDL_Quit()
def main(): parser = argparse.ArgumentParser( description='PySDL2 / cefpython example', add_help=True ) parser.add_argument( '-v', '--verbose', help='Turn on debug info', dest='verbose', action='store_true' ) parser.add_argument( '-r', '--renderer', help='Specify hardware or software rendering', default='software', dest='renderer', choices=['software', 'hardware'] ) args = parser.parse_args() logLevel = logging.INFO if args.verbose: logLevel = logging.DEBUG logging.basicConfig( format='[%(filename)s %(levelname)s]: %(message)s', level=logLevel ) logging.info("Using PySDL2 %s" % sdl2.__version__) version = sdl2.SDL_version() sdl2.SDL_GetVersion(version) logging.info( "Using SDL2 %s.%s.%s" % (version.major, version.minor, version.patch) ) # The following variables control the dimensions of the window # and browser display area width = 800 height = 600 # headerHeight is useful for leaving space for controls # at the top of the window (future implementation?) headerHeight = 0 browserHeight = height - headerHeight browserWidth = width # Mouse wheel fudge to enhance scrolling scrollEnhance = 40 # desired frame rate frameRate = 100 # Initialise CEF for offscreen rendering sys.excepthook = cef.ExceptHook switches = { # Tweaking OSR performance by setting the same Chromium flags # as in upstream cefclient (Issue #240). "disable-surfaces": "", "disable-gpu": "", "disable-gpu-compositing": "", "enable-begin-frame-scheduling": "", } browser_settings = { # Tweaking OSR performance (Issue #240) "windowless_frame_rate": frameRate } cef.Initialize(settings={"windowless_rendering_enabled": True}, switches=switches) logging.debug("cef initialised") window_info = cef.WindowInfo() window_info.SetAsOffscreen(0) # Initialise SDL2 for video (add other init constants if you # require other SDL2 functionality e.g. mixer, # TTF, joystick etc. sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO) logging.debug("SDL2 initialised") # Create the window window = sdl2.video.SDL_CreateWindow( 'cefpython3 SDL2 Demo', sdl2.video.SDL_WINDOWPOS_UNDEFINED, sdl2.video.SDL_WINDOWPOS_UNDEFINED, width, height, 0 ) # Define default background colour (black in this case) backgroundColour = sdl2.SDL_Color(0, 0, 0) renderer = None if args.renderer == 'hardware': # Create the renderer using hardware acceleration logging.info("Using hardware rendering") renderer = sdl2.SDL_CreateRenderer( window, -1, sdl2.render.SDL_RENDERER_ACCELERATED ) else: # Create the renderer using software acceleration logging.info("Using software rendering") renderer = sdl2.SDL_CreateRenderer( window, -1, sdl2.render.SDL_RENDERER_SOFTWARE ) # Set-up the RenderHandler, passing in the SDL2 renderer renderHandler = RenderHandler(renderer, width, height - headerHeight) # Create the browser instance browser = cef.CreateBrowserSync(window_info, url="https://www.google.com/", settings=browser_settings) browser.SetClientHandler(LoadHandler()) browser.SetClientHandler(renderHandler) # Must call WasResized at least once to let know CEF that # viewport size is available and that OnPaint may be called. browser.SendFocusEvent(True) browser.WasResized() # Begin the main rendering loop running = True # FPS debug variables frames = 0 logging.debug("beginning rendering loop") resetFpsTime = True fpsTime = 0 while running: # record when we started drawing this frame startTime = sdl2.timer.SDL_GetTicks() if resetFpsTime: fpsTime = sdl2.timer.SDL_GetTicks() resetFpsTime = False # Convert SDL2 events into CEF events (where appropriate) events = sdl2.ext.get_events() for event in events: if (event.type == sdl2.SDL_QUIT or (event.type == sdl2.SDL_KEYDOWN and event.key.keysym.sym == sdl2.SDLK_ESCAPE)): running = False logging.debug("SDL2 QUIT event") break if event.type == sdl2.SDL_MOUSEBUTTONDOWN: if event.button.button == sdl2.SDL_BUTTON_LEFT: if event.button.y > headerHeight: logging.debug( "SDL2 MOUSEBUTTONDOWN event (left button)" ) # Mouse click triggered in browser region browser.SendMouseClickEvent( event.button.x, event.button.y - headerHeight, cef.MOUSEBUTTON_LEFT, False, 1 ) elif event.type == sdl2.SDL_MOUSEBUTTONUP: if event.button.button == sdl2.SDL_BUTTON_LEFT: if event.button.y > headerHeight: logging.debug("SDL2 MOUSEBUTTONUP event (left button)") # Mouse click triggered in browser region browser.SendMouseClickEvent( event.button.x, event.button.y - headerHeight, cef.MOUSEBUTTON_LEFT, True, 1 ) elif event.type == sdl2.SDL_MOUSEMOTION: if event.motion.y > headerHeight: # Mouse move triggered in browser region browser.SendMouseMoveEvent(event.motion.x, event.motion.y - headerHeight, False) elif event.type == sdl2.SDL_MOUSEWHEEL: logging.debug("SDL2 MOUSEWHEEL event") # Mouse wheel event x = event.wheel.x if x < 0: x -= scrollEnhance else: x += scrollEnhance y = event.wheel.y if y < 0: y -= scrollEnhance else: y += scrollEnhance browser.SendMouseWheelEvent(0, 0, x, y) elif event.type == sdl2.SDL_TEXTINPUT: # Handle text events to get actual characters typed rather # than the key pressed. logging.debug("SDL2 TEXTINPUT event: %s" % event.text.text) keycode = ord(event.text.text) key_event = { "type": cef.KEYEVENT_CHAR, "windows_key_code": keycode, "character": keycode, "unmodified_character": keycode, "modifiers": cef.EVENTFLAG_NONE } browser.SendKeyEvent(key_event) key_event = { "type": cef.KEYEVENT_KEYUP, "windows_key_code": keycode, "character": keycode, "unmodified_character": keycode, "modifiers": cef.EVENTFLAG_NONE } browser.SendKeyEvent(key_event) elif event.type == sdl2.SDL_KEYDOWN: # Handle key down events for non-text keys logging.debug("SDL2 KEYDOWN event") if event.key.keysym.sym == sdl2.SDLK_RETURN: keycode = event.key.keysym.sym key_event = { "type": cef.KEYEVENT_CHAR, "windows_key_code": keycode, "character": keycode, "unmodified_character": keycode, "modifiers": cef.EVENTFLAG_NONE } browser.SendKeyEvent(key_event) elif event.key.keysym.sym in [ sdl2.SDLK_BACKSPACE, sdl2.SDLK_DELETE, sdl2.SDLK_LEFT, sdl2.SDLK_RIGHT, sdl2.SDLK_UP, sdl2.SDLK_DOWN, sdl2.SDLK_HOME, sdl2.SDLK_END ]: keycode = get_key_code(event.key.keysym.sym) if keycode is not None: key_event = { "type": cef.KEYEVENT_RAWKEYDOWN, "windows_key_code": keycode, "character": keycode, "unmodified_character": keycode, "modifiers": cef.EVENTFLAG_NONE } browser.SendKeyEvent(key_event) elif event.type == sdl2.SDL_KEYUP: # Handle key up events for non-text keys logging.debug("SDL2 KEYUP event") if event.key.keysym.sym in [ sdl2.SDLK_RETURN, sdl2.SDLK_BACKSPACE, sdl2.SDLK_DELETE, sdl2.SDLK_LEFT, sdl2.SDLK_RIGHT, sdl2.SDLK_UP, sdl2.SDLK_DOWN, sdl2.SDLK_HOME, sdl2.SDLK_END ]: keycode = get_key_code(event.key.keysym.sym) if keycode is not None: key_event = { "type": cef.KEYEVENT_KEYUP, "windows_key_code": keycode, "character": keycode, "unmodified_character": keycode, "modifiers": cef.EVENTFLAG_NONE } browser.SendKeyEvent(key_event) # Clear the renderer sdl2.SDL_SetRenderDrawColor( renderer, backgroundColour.r, backgroundColour.g, backgroundColour.b, 255 ) sdl2.SDL_RenderClear(renderer) # Tell CEF to update which will trigger the OnPaint # method of the RenderHandler instance cef.MessageLoopWork() # Update display sdl2.SDL_RenderCopy( renderer, renderHandler.texture, None, sdl2.SDL_Rect(0, headerHeight, browserWidth, browserHeight) ) sdl2.SDL_RenderPresent(renderer) # FPS debug code frames += 1 if sdl2.timer.SDL_GetTicks() - fpsTime > 1000: logging.debug("FPS: %d" % frames) frames = 0 resetFpsTime = True # regulate frame rate if sdl2.timer.SDL_GetTicks() - startTime < 1000.0 / frameRate: sdl2.timer.SDL_Delay( (1000 / frameRate) - (sdl2.timer.SDL_GetTicks() - startTime) ) # User exited exit_app()
def get_library_version(self): v = version.SDL_version() sdl2.SDL_GetVersion(v) return v.major, v.minor, v.patch