Пример #1
0
 def test_clone(self):
     b = WindowBorder(red=1, blue=0, green=1, alpha=0.5, size=10)
     b2 = b.clone()
     assert b.red == b2.red
     assert b.blue == b2.blue
     assert b.green == b2.green
     assert b.alpha == b2.alpha
     assert b.size == b2.size
Пример #2
0
    def parse_border(self, border_str, extra_args):
        enabled = not border_str.endswith(":off")
        parts = [x.strip() for x in border_str.replace(":off", "").split(",")]
        color_str = parts[0]

        def border_help():
            log.info(" border format: color[,size][:off]")
            log.info("  eg: red,10")
            log.info("  eg: ,5")
            log.info("  eg: auto,5")
            log.info("  eg: blue")

        if color_str.lower() in ("none", "no", "off", "0"):
            return
        if color_str.lower() == "help":
            border_help()
            return
        color_str = color_str.replace(":off", "")
        if color_str == "auto" or color_str == "":
            try:
                try:
                    from hashlib import md5
                except ImportError:
                    from md5 import md5
                m = md5()
                for x in extra_args:
                    m.update(str(x))
                color_str = "#%s" % m.hexdigest()[:6]
                log("border color derived from %s: %s", extra_args, color_str)
            except:
                log.info("failed to derive border color from %s",
                         extra_args,
                         exc_info=True)
                #fail: default to red
                color_str = "red"
        try:
            color = color_parse(color_str)
        except Exception as e:
            log.warn("invalid border color specified: '%s' (%s)", color_str, e)
            border_help()
            color = color_parse("red")
        alpha = 0.6
        size = 4
        if len(parts) == 2:
            size_str = parts[1]
            try:
                size = int(size_str)
            except Exception as e:
                log.warn("invalid size specified: %s (%s)", size_str, e)
            if size <= 0:
                log("border size is %s, disabling it", size)
                return
            if size >= 45:
                log.warn("border size is too high: %s, clipping it", size)
                size = 45
        self.border = WindowBorder(enabled, color.red / 65536.0,
                                   color.green / 65536.0, color.blue / 65536.0,
                                   alpha, size)
        log("parse_border(%s)=%s", border_str, self.border)
Пример #3
0
def test_gl_client_window(gl_client_window_class,
                          max_window_size=(1024, 1024),
                          pixel_depth=24,
                          show=False):
    #try to render using a temporary window:
    draw_result = {}
    window = None
    try:
        x, y = -100, -100
        if show:
            x, y = 100, 100
        w, h = 250, 250
        from xpra.codecs.loader import load_codec
        load_codec("dec_pillow")
        from xpra.client.window_border import WindowBorder
        border = WindowBorder()
        default_cursor_data = None
        noclient = FakeClient()
        #test with alpha, but not on win32
        #because we can't do alpha on win32 with opengl
        metadata = typedict({b"has-alpha": not WIN32})

        class NoHeaderGLClientWindow(gl_client_window_class):
            def add_header_bar(self):
                pass

            def schedule_recheck_focus(self):
                pass

        window = NoHeaderGLClientWindow(noclient, None, None, 2**32 - 1, x, y,
                                        w, h, w, h, metadata, False,
                                        typedict({}), border, max_window_size,
                                        default_cursor_data, pixel_depth)
        window_backing = window._backing
        window_backing.idle_add = no_idle_add
        window_backing.timeout_add = no_timeout_add
        window_backing.source_remove = no_source_remove
        window.realize()
        window_backing.paint_screen = True
        pixel_format = "BGRX"
        bpp = len(pixel_format)
        options = typedict({"pixel_format": pixel_format})
        stride = bpp * w
        coding = "rgb32"
        widget = window_backing._backing
        widget.realize()

        def paint_callback(success, message=""):
            log("paint_callback(%s, %s)", success, message)
            draw_result["success"] = success
            if message:
                draw_result["message"] = message.replace("\n", " ")

        log("OpenGL: testing draw on %s widget %s with %s : %s", window,
            widget, coding, pixel_format)
        pix = AtomicInteger(0x7f)
        REPAINT_DELAY = envint("XPRA_REPAINT_DELAY", int(show) * 16)
        gl_icon = get_icon_filename("opengl", ext="png")
        icon_data = None
        if os.path.exists(gl_icon):
            from PIL import Image
            img = Image.open(gl_icon)
            img.load()
            icon_w, icon_h = img.size
            icon_stride = icon_w * 4
            noalpha = Image.new("RGB", img.size, (255, 255, 255))
            noalpha.paste(img, mask=img.split()[3])  # 3 is the alpha channel
            buf = BytesIO()
            noalpha.save(buf, format="JPEG")
            icon_data = buf.getvalue()
            buf.close()
            icon_format = "jpeg"
        if not icon_data:
            icon_w = 32
            icon_h = 32
            icon_stride = icon_w * 4
            icon_data = bytes([0]) * icon_stride * icon_h
            icon_format = "rgb32"

        def draw():
            v = pix.increase()
            img_data = bytes([v % 256] * stride * h)
            options["flush"] = 1
            window.draw_region(0, 0, w, h, coding, img_data, stride, v,
                               options, [paint_callback])
            options["flush"] = 0
            mx = w // 2 - icon_w // 2
            my = h // 2 - icon_h // 2
            x = iround(mx * (1 + sin(v / 100)))
            y = iround(my * (1 + cos(v / 100)))
            window.draw_region(x, y, icon_w, icon_h, icon_format, icon_data,
                               icon_stride, v, options, [paint_callback])
            return REPAINT_DELAY > 0

        #the paint code is actually synchronous here,
        #so we can check the present_fbo() result:
        if show:
            widget.show()
            window.show()
            from gi.repository import Gtk, GLib

            def window_close_event(*_args):
                Gtk.main_quit()

            noclient.window_close_event = window_close_event
            GLib.timeout_add(REPAINT_DELAY, draw)
            Gtk.main()
        else:
            draw()
        if window_backing.last_present_fbo_error:
            return {
                "success":
                False,
                "message":
                "failed to present FBO on screen: %s" %
                window_backing.last_present_fbo_error
            }
    finally:
        if window:
            window.destroy()
    log("test_gl_client_window(..) draw_result=%s", draw_result)
    return draw_result
Пример #4
0
def test_gl_client_window(gl_client_window_class, max_window_size=(1024, 1024), pixel_depth=24, show=False):
    #try to render using a temporary window:
    draw_result = {}
    window = None
    try:
        x, y = -100, -100
        if show:
            x, y = 100, 100
        w, h = 250, 250
        from xpra.client.window_border import WindowBorder
        border = WindowBorder()
        default_cursor_data = None
        noclient = FakeClient()
        #test with alpha, but not on win32
        #because we can't do alpha on win32 with opengl
        metadata = typedict({b"has-alpha" : not WIN32})
        window = gl_client_window_class(noclient, None, None, 2**32-1, x, y, w, h, w, h,
                                        metadata, False, typedict({}),
                                        border, max_window_size, default_cursor_data, pixel_depth)
        window_backing = window._backing
        window_backing.idle_add = no_idle_add
        window_backing.timeout_add = no_timeout_add
        window_backing.source_remove = no_source_remove
        window.realize()
        window_backing.paint_screen = True
        pixel_format = "BGRX"
        bpp = len(pixel_format)
        options = typedict({"pixel_format" : pixel_format})
        stride = bpp*w
        coding = "rgb32"
        widget = window_backing._backing
        widget.realize()
        def paint_callback(success, message):
            log("paint_callback(%s, %s)", success, message)
            draw_result.update({
                "success"   : success,
                "message"   : message,
                })
        log("OpenGL: testing draw on %s widget %s with %s : %s", window, widget, coding, pixel_format)
        pix = AtomicInteger(0x7f)
        REPAINT_DELAY = envint("XPRA_REPAINT_DELAY", 0)
        def draw():
            if PYTHON3:
                img_data = bytes([pix.increase() % 256]*stride*h)
            else:
                img_data = chr(pix.increase() % 256)*stride*h
            window.draw_region(0, 0, w, h, coding, img_data, stride, 1, options, [paint_callback])
            return REPAINT_DELAY>0
        #the paint code is actually synchronous here,
        #so we can check the present_fbo() result:
        if show:
            widget.show()
            window.show()
            from xpra.gtk_common.gobject_compat import import_gtk, import_glib
            gtk = import_gtk()
            glib = import_glib()
            def window_close_event(*_args):
                gtk.main_quit()
            noclient.window_close_event = window_close_event
            glib.timeout_add(REPAINT_DELAY, draw)
            gtk.main()
        else:
            draw()
        if window_backing.last_present_fbo_error:
            return {
                "success" : False,
                "message" : "failed to present FBO on screen: %s" % window_backing.last_present_fbo_error
                }
    finally:
        if window:
            window.destroy()
    log("test_gl_client_window(..) draw_result=%s", draw_result)
    return draw_result
Пример #5
0
def test_gl_client_window(gl_client_window_class,
                          max_window_size=(1024, 1024),
                          pixel_depth=24):
    #try to render using a temporary window:
    draw_result = {}
    window = None
    try:
        w, h = 50, 50
        from xpra.client.window_border import WindowBorder
        border = WindowBorder()
        default_cursor_data = None
        noclient = AdHocStruct()

        def no_idle_add(fn, *args, **kwargs):
            fn(*args, **kwargs)

        def no_timeout_add(*args, **kwargs):
            raise Exception("timeout_add should not have been called")

        def no_source_remove(*args, **kwargs):
            raise Exception("source_remove should not have been called")

        def no_scaling(*args):
            return args

        def get_None(*args):
            return None

        def noop(*args):
            pass

        #we have to suspend idle_add to make this synchronous
        #we can do this because this method must be running in the UI thread already:
        noclient.idle_add = no_idle_add
        noclient.timeout_add = no_timeout_add
        noclient.source_remove = no_source_remove
        noclient.sp = noclient.sx = noclient.sy = noclient.srect = no_scaling
        noclient.xscale = noclient.yscale = 1
        noclient.server_window_decorations = True
        noclient.mmap_enabled = False
        noclient.mmap = None
        noclient.encoding_defaults = {}
        noclient.get_window_frame_sizes = get_None
        noclient._set_window_menu = None
        noclient._focused = None
        noclient.request_frame_extents = noop
        window = gl_client_window_class(noclient, None, None, 2**32 - 1, -100,
                                        -100, w, h, w, h, typedict({}), False,
                                        typedict({}), border, max_window_size,
                                        default_cursor_data, pixel_depth)
        window._backing.idle_add = no_idle_add
        window._backing.timeout_add = no_timeout_add
        window._backing.source_remove = no_source_remove
        window.realize()
        pixel_format = "BGRX"
        bpp = len(pixel_format)
        options = typedict({"pixel_format": pixel_format})
        stride = bpp * w
        img_data = b"\0" * stride * h
        coding = "rgb32"
        widget = window._backing._backing
        widget.realize()

        def paint_callback(success, message):
            log("paint_callback(%s, %s)", success, message)
            draw_result.update({
                "success": success,
                "message": message,
            })

        log("OpenGL: testing draw on %s widget %s with %s : %s", window,
            widget, coding, pixel_format)
        window.draw_region(0, 0, w, h, coding, img_data, stride, 1, options,
                           [paint_callback])
    finally:
        if window:
            window.destroy()
    log("test_gl_client_window(..) draw_result=%s", draw_result)
    return draw_result
Пример #6
0
 def test_repr(self):
     b = WindowBorder(red=0, blue=1, green=0.5)
     assert repr(b).find("00") >= 0
     assert repr(b).find("FF") >= 0
Пример #7
0
 def test_toggle(self):
     b = WindowBorder(shown=True)
     b.toggle()
     assert b.shown is False