|
| 1 | +#!/usr/bin/python2 |
| 2 | + |
| 3 | +# |
| 4 | +# Simple SDL2 / cefpython3 example. |
| 5 | +# |
| 6 | +# Only handles mouse events but could be extended to handle others. |
| 7 | +# |
| 8 | +# Requires pysdl2 (and SDL2 library). |
| 9 | +# |
| 10 | +# Tested under Fedora Linux (x86_64). |
| 11 | +# |
| 12 | +# by Neil Munday (www.mundayweb.com) |
| 13 | +# |
| 14 | + |
| 15 | +import os |
| 16 | +import sys |
| 17 | +import sdl2 |
| 18 | +import sdl2.ext |
| 19 | +import sdl2.sdlimage |
| 20 | +import sdl2.joystick |
| 21 | +import sdl2.video |
| 22 | +import sdl2.render |
| 23 | +import sdl2.sdlgfx |
| 24 | +import sdl2.sdlttf |
| 25 | + |
| 26 | +from cefpython3 import cefpython as cef |
| 27 | +from PIL import Image |
| 28 | + |
| 29 | +class LoadHandler(object): |
| 30 | + def OnLoadingStateChange(self, browser, is_loading, **_): |
| 31 | + if not is_loading: |
| 32 | + print "loading complete" |
| 33 | + |
| 34 | + def OnLoadError(self, browser, frame, error_code, failed_url, **_): |
| 35 | + if not frame.IsMain(): |
| 36 | + return |
| 37 | + print "Failed to load %s" % failed_url |
| 38 | + cef.PostTask(cef.TID_UI, exit_app, browser) |
| 39 | + |
| 40 | +class RenderHandler(object): |
| 41 | + |
| 42 | + def __init__(self, renderer, width, height): |
| 43 | + self.__width = width |
| 44 | + self.__height = height |
| 45 | + self.__renderer = renderer |
| 46 | + self.texture = None |
| 47 | + |
| 48 | + def GetViewRect(self, rect_out, **_): |
| 49 | + rect_out.extend([0, 0, self.__width, self.__height]) |
| 50 | + return True |
| 51 | + |
| 52 | + def GetScreenRect(self, browser, rect_out): # noqa: N802 |
| 53 | + return False |
| 54 | + |
| 55 | + def GetScreenPoint(self, browser, view_x, view_y, screen_coordinates_out): |
| 56 | + return False |
| 57 | + |
| 58 | + def OnPaint(self, browser, element_type, paint_buffer, **_): |
| 59 | + # |
| 60 | + # Use PIL to create a set of bytes that we can turn into an SDL2 surface |
| 61 | + # and then convert this to a SDL2 texture for rendering the main program loop. |
| 62 | + # |
| 63 | + if element_type == cef.PET_VIEW: |
| 64 | + data = paint_buffer.GetString(mode="rgba", origin="top-left") |
| 65 | + image = Image.frombuffer('RGBA', (self.__width, self.__height), data, 'raw', 'BGRA') |
| 66 | + # |
| 67 | + # Following PIL to SDL2 surface code from pysdl2 source |
| 68 | + # |
| 69 | + mode = image.mode |
| 70 | + rmask = gmask = bmask = amask = 0 |
| 71 | + if mode == "RGB": |
| 72 | + # 3x8-bit, 24bpp |
| 73 | + if sdl2.endian.SDL_BYTEORDER == sdl2.endian.SDL_LIL_ENDIAN: |
| 74 | + rmask = 0x0000FF |
| 75 | + gmask = 0x00FF00 |
| 76 | + bmask = 0xFF0000 |
| 77 | + else: |
| 78 | + rmask = 0xFF0000 |
| 79 | + gmask = 0x00FF00 |
| 80 | + bmask = 0x0000FF |
| 81 | + depth = 24 |
| 82 | + pitch = self.__width * 3 |
| 83 | + elif mode in ("RGBA", "RGBX"): |
| 84 | + # RGBX: 4x8-bit, no alpha |
| 85 | + # RGBA: 4x8-bit, alpha |
| 86 | + if sdl2.endian.SDL_BYTEORDER == sdl2.endian.SDL_LIL_ENDIAN: |
| 87 | + rmask = 0x00000000 |
| 88 | + gmask = 0x0000FF00 |
| 89 | + bmask = 0x00FF0000 |
| 90 | + if mode == "RGBA": |
| 91 | + amask = 0xFF000000 |
| 92 | + else: |
| 93 | + rmask = 0xFF000000 |
| 94 | + gmask = 0x00FF0000 |
| 95 | + bmask = 0x0000FF00 |
| 96 | + if mode == "RGBA": |
| 97 | + amask = 0x000000FF |
| 98 | + depth = 32 |
| 99 | + pitch = self.__width * 4 |
| 100 | + else: |
| 101 | + print "Unsupported mode: %s" % mode |
| 102 | + exit_app() |
| 103 | + |
| 104 | + pxbuf = image.tobytes() |
| 105 | + # create surface |
| 106 | + surface = sdl2.SDL_CreateRGBSurfaceFrom(pxbuf, self.__width, self.__height, depth, pitch, rmask, gmask, bmask, amask) |
| 107 | + |
| 108 | + if self.texture: |
| 109 | + sdl2.SDL_DestroyTexture(self.texture) |
| 110 | + # create texture |
| 111 | + self.texture = sdl2.SDL_CreateTextureFromSurface(self.__renderer, surface) |
| 112 | + sdl2.SDL_FreeSurface(surface) |
| 113 | + else: |
| 114 | + print "Unsupport element_type in OnPaint" |
| 115 | + |
| 116 | +def exit_app(): |
| 117 | + sdl2.SDL_Quit() |
| 118 | + cef.Shutdown() |
| 119 | + print "exited" |
| 120 | + |
| 121 | +if __name__ == "__main__": |
| 122 | + |
| 123 | + width = 1024 |
| 124 | + height = 768 |
| 125 | + headerHeight = 0 # useful if for leaving space for controls at the top of the window (future implementation?) |
| 126 | + browserHeight = height - headerHeight |
| 127 | + browserWidth = width |
| 128 | + |
| 129 | + WindowUtils = cef.WindowUtils() |
| 130 | + |
| 131 | + sys.excepthook = cef.ExceptHook |
| 132 | + |
| 133 | + cef.Initialize(settings={"windowless_rendering_enabled": True}) |
| 134 | + |
| 135 | + sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO) |
| 136 | + |
| 137 | + window = sdl2.video.SDL_CreateWindow('cefpython3 SDL2 Demo', sdl2.video.SDL_WINDOWPOS_UNDEFINED, sdl2.video.SDL_WINDOWPOS_UNDEFINED, width, height, 0) |
| 138 | + |
| 139 | + backgroundColour = sdl2.SDL_Color(0, 0, 0) |
| 140 | + |
| 141 | + renderer = sdl2.SDL_CreateRenderer(window, -1, sdl2.render.SDL_RENDERER_ACCELERATED) |
| 142 | + |
| 143 | + window_info = cef.WindowInfo() |
| 144 | + window_info.SetAsOffscreen(0) |
| 145 | + |
| 146 | + renderHandler = RenderHandler(renderer, width, height - headerHeight) |
| 147 | + |
| 148 | + browser = cef.CreateBrowserSync(window_info, url="https://www.google.com/") |
| 149 | + browser.SetClientHandler(LoadHandler()) |
| 150 | + browser.SetClientHandler(renderHandler) |
| 151 | + browser.SendFocusEvent(True) |
| 152 | + browser.WasResized() |
| 153 | + |
| 154 | + running = True |
| 155 | + |
| 156 | + # main loop, handle events and rendering here |
| 157 | + while running: |
| 158 | + |
| 159 | + # convert SDL2 events into CEF events (where appropriate) |
| 160 | + events = sdl2.ext.get_events() |
| 161 | + for event in events: |
| 162 | + if event.type == sdl2.SDL_QUIT or (event.type == sdl2.SDL_KEYDOWN and event.key.keysym.sym == sdl2.SDLK_ESCAPE): |
| 163 | + running = False |
| 164 | + break |
| 165 | + |
| 166 | + if event.type == sdl2.SDL_MOUSEBUTTONDOWN: |
| 167 | + if event.button.button == sdl2.SDL_BUTTON_LEFT: |
| 168 | + if event.button.y > headerHeight: |
| 169 | + browser.SendMouseClickEvent(event.button.x, event.button.y - headerHeight, cef.MOUSEBUTTON_LEFT, False, 1) |
| 170 | + elif event.type == sdl2.SDL_MOUSEBUTTONUP: |
| 171 | + if event.button.button == sdl2.SDL_BUTTON_LEFT: |
| 172 | + if event.button.y > headerHeight: |
| 173 | + browser.SendMouseClickEvent(event.button.x, event.button.y - headerHeight, cef.MOUSEBUTTON_LEFT, True, 1) |
| 174 | + elif event.type == sdl2.SDL_MOUSEMOTION: |
| 175 | + if event.button.y > headerHeight: |
| 176 | + browser.SendMouseMoveEvent(event.button.x, event.button.y - headerHeight, True) |
| 177 | + |
| 178 | + sdl2.SDL_SetRenderDrawColor(renderer, backgroundColour.r, backgroundColour.g, backgroundColour.b, 255) |
| 179 | + sdl2.SDL_RenderClear(renderer) |
| 180 | + |
| 181 | + cef.MessageLoopWork() |
| 182 | + |
| 183 | + sdl2.SDL_RenderCopy(renderer, renderHandler.texture, None, sdl2.SDL_Rect(0, headerHeight, browserWidth, browserHeight)) |
| 184 | + |
| 185 | + sdl2.SDL_RenderPresent(renderer) |
| 186 | + |
| 187 | + exit_app() |
| 188 | + |
0 commit comments