Beispiel #1
0
    def add_socket(self, name: Optional[str] = None) -> str:
        """Add a socket to Wayland display for the clients to connect.

        This adds a Unix socket to Wayland display which can be used by clients
        to connect to Wayland display.

        If `None` is passed as name, then it would look for `WAYLAND_DISPLAY`
        environment variable for the socket name. If `WAYLAND_DISPLAY` is not
        set, then default `wayland-0` is used.

        The Unix socket will be created in the directory pointed to by
        environment variable `XDG_RUNTIME_DIR`. If `XDG_RUNTIME_DIR` is not
        set, then this function throws an exception.

        The length of socket path, i.e., the path set in `XDG_RUNTIME_DIR` and
        the socket name, must not exceed the maxium length of a Unix socket
        path.  The function also fails if the user do not have write permission
        in the `XDG_RUNTIME_DIR` path or if the socket name is already in use.

        :param name: Name of the Unix socket.
        :type name: string or None
        """
        if name is None:
            name_ptr = lib.wl_display_add_socket_auto(self._ptr)
            if name_ptr == ffi.NULL:
                raise RuntimeError("Unable to create socket")
            name = ffi.string(name_ptr)
        else:
            ret = lib.wl_display_add_socket(self._ptr, name.encode())

            if ret == -1:
                # TODO: raise better
                raise Exception()

        return name
Beispiel #2
0
    def c_to_arguments(self, args_ptr):
        """Create a list of arguments

        Generate the arguments of the method from a CFFI cdata array of
        `wl_argument` structs that correspond to the arguments of the method as
        specified by the method signature.

        :param args_ptr: Input arguments
        :type args_ptr: cdata `union wl_argument []`
        :returns: list of args
        """
        args = []
        for i, argument in enumerate(self.arguments):
            arg_ptr = args_ptr[i]

            # Match numbers (int, unsigned, float, file descriptor)
            if argument.argument_type == ArgumentType.Int:
                args.append(arg_ptr.i)
            elif argument.argument_type == ArgumentType.Uint:
                args.append(arg_ptr.u)
            elif argument.argument_type == ArgumentType.Fixed:
                f = lib.wl_fixed_to_double(arg_ptr.f)
                args.append(f)
            elif argument.argument_type == ArgumentType.FileDescriptor:
                args.append(arg_ptr.h)
            elif argument.argument_type == ArgumentType.String:
                if arg_ptr == ffi.NULL:
                    if not argument.nullable:
                        raise Exception
                    args.append(None)
                else:
                    args.append(ffi.string(arg_ptr.s).decode())
            elif argument.argument_type == ArgumentType.Object:
                if arg_ptr.o == ffi.NULL:
                    if not argument.nullable:
                        message = "Got null object parsing arguments for '{}' message, may already be destroyed".format(
                            self.name)
                        raise RuntimeError(message)
                    args.append(None)
                else:
                    iface = self.types[i]
                    proxy_ptr = ffi.cast('struct wl_proxy *', arg_ptr.o)
                    obj = iface.proxy_class.registry.get(proxy_ptr)
                    if obj is None:
                        raise RuntimeError(
                            "Unable to get object for {}, was it garbage collected?"
                            .format(proxy_ptr))
                    args.append(obj)
            elif argument.argument_type == ArgumentType.NewId:
                # TODO
                raise NotImplementedError
            elif argument.argument_type == ArgumentType.Array:
                array_ptr = arg_ptr.a
                args.append(ffi.buffer(array_ptr.data, array_ptr.size)[:])
            else:
                raise Exception(f"Bad argument: {argument}")

        return args
Beispiel #3
0
    def c_to_arguments(self, args_ptr):
        """Create a list of arguments

        Generate the arguments of the method from a CFFI cdata array of
        `wl_argument` structs that correspond to the arguments of the method as
        specified by the method signature.

        :param args_ptr: Input arguments
        :type args_ptr: cdata `union wl_argument []`
        :returns: list of args
        """
        args = []
        for i, sig_match in enumerate(re_arg.finditer(self.signature)):
            arg_ptr = args_ptr[i]
            null, sig = sig_match.groups()

            # Match numbers (int, unsigned, float, file descriptor)
            if sig == 'i':
                args.append(arg_ptr.i)
            elif sig == 'u':
                args.append(arg_ptr.u)
            elif sig == 'f':
                f = lib.wl_fixed_to_double(arg_ptr.f)
                args.append(f)
            elif sig == 'h':
                args.append(arg_ptr.h)
            # Match string
            elif sig == 's':
                if arg_ptr == ffi.NULL:
                    if not null:
                        raise Exception
                    args.append(None)
                else:
                    args.append(ffi.string(arg_ptr.s).decode())
            # Object or new id
            elif sig in ('o', 'n'):
                # TODO
                pass
            # Array (i.e. buffer of bytes)
            elif sig == 'a':
                # TODO
                pass

        return args
Beispiel #4
0
    def add_socket(self, name=None):
        """Add a socket to Wayland display for the clients to connect.

        This adds a Unix socket to Wayland display which can be used by clients
        to connect to Wayland display.

        If `None` is passed as name, then it would look for `WAYLAND_DISPLAY`
        environment variable for the socket name. If `WAYLAND_DISPLAY` is not
        set, then default `wayland-0` is used.

        The Unix socket will be created in the directory pointed to by
        environment variable `XDG_RUNTIME_DIR`. If `XDG_RUNTIME_DIR` is not
        set, then this function throws an exception.

        The length of socket path, i.e., the path set in `XDG_RUNTIME_DIR` and
        the socket name, must not exceed the maxium length of a Unix socket
        path.  The function also fails if the user do not have write permission
        in the `XDG_RUNTIME_DIR` path or if the socket name is already in use.

        :param name: Name of the Unix socket.
        :type name: string or None
        """
        if name is None:
            name_ptr = lib.wl_display_add_socket_auto(self._ptr)
            name = ffi.string(name_ptr)

            if not name:
                # TODO: raise better
                raise Exception()

            return name
        else:
            name_ptr = ffi.new('char []', name.encode())
            ret = lib.wl_display_add_socket(self._ptr, name_ptr)

            if ret == -1:
                # TODO: raise better
                raise Exception()