Index: xpra/os_util.py =================================================================== --- xpra/os_util.py (revision 16723) +++ xpra/os_util.py (working copy) @@ -182,6 +182,10 @@ return rel(sys_platform) +def get_rand_chars(l=16, chars=b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"): + import random + return b"".join(chars[random.randint(0, len(chars)-1)] for _ in range(l)) + def get_hex_uuid(): return uuid.uuid4().hex Index: xpra/scripts/server.py =================================================================== --- xpra/scripts/server.py (revision 16766) +++ xpra/scripts/server.py (working copy) @@ -91,6 +91,12 @@ def get_dbus_pid(): return _get_int("_XPRA_DBUS_PID") +def save_uinput_id(uuid): + _save_str("_XPRA_UINPUT_ID", uuid) + +#def get_uinput_id(): +# return _get_str("_XPRA_UINPUT_ID") + def get_dbus_env(): env = {} for n,load in ( @@ -157,7 +163,26 @@ except: pass +def close_gtk_display(): + # Close our display(s) first, so the server dying won't kill us. + # (if gtk has been loaded) + gtk_mod = sys.modules.get("gtk") + if gtk_mod: + for d in gtk_mod.gdk.display_manager_get().list_displays(): + d.close() +def kill_xvfb(xvfb_pid): + if xvfb_pid: + from xpra.log import Logger + log = Logger("server") + log.info("killing xvfb with pid %s", xvfb_pid) + try: + os.kill(xvfb_pid, signal.SIGTERM) + except OSError as e: + log.info("failed to kill xvfb process with pid %s:", xvfb_pid) + log.info(" %s", e) + + def print_DE_warnings(desktop_display, pulseaudio, notifications, dbus_launch): de = os.environ.get("XDG_SESSION_DESKTOP") or os.environ.get("SESSION_DESKTOP") if not de: @@ -348,6 +373,7 @@ def run_server(error_cb, opts, mode, xpra_file, extra_args, desktop_display=None): + from xpra.os_util import strtobytes try: cwd = os.getcwd() except: @@ -675,20 +701,21 @@ os.environ.update(protected_env) log("env=%s", os.environ) - #create devices for vfb if needed: - devices = {} - if start_vfb and opts.input_devices.lower()=="uinput": - devices = create_input_devices(uid) - # Start the Xvfb server first to get the display_name if needed odisplay_name = display_name xvfb = None xvfb_pid = None + uinput_uuid = None if start_vfb: assert not proxying and xauth_data pixel_depth = validate_pixel_depth(opts.pixel_depth) from xpra.x11.vfb_util import start_Xvfb - xvfb, display_name, cleanups = start_Xvfb(opts.xvfb, pixel_depth, display_name, cwd, uid, gid, username, xauth_data, devices) + from xpra.server.server_util import has_uinput + uinput_uuid = None + if has_uinput() and opts.input_devices.lower() in ("uinput", "auto") and not shadowing: + from xpra.os_util import get_rand_chars + uinput_uuid = get_rand_chars(8) + xvfb, display_name, cleanups = start_Xvfb(opts.xvfb, pixel_depth, display_name, cwd, uid, gid, username, xauth_data, uinput_uuid) for f in cleanups: add_cleanup(f) xvfb_pid = xvfb.pid @@ -698,6 +725,7 @@ os.environ.update(protected_env) if display_name!=odisplay_name and pam: pam.set_items({"XDISPLAY" : display_name}) + if POSIX and not OSX and displayfd>0: from xpra.platform.displayfd import write_displayfd try: @@ -713,23 +741,13 @@ except: pass - close_display = None if not proxying: def close_display(): - # Close our display(s) first, so the server dying won't kill us. - # (if gtk has been loaded) - gtk_mod = sys.modules.get("gtk") - if gtk_mod: - for d in gtk_mod.gdk.display_manager_get().list_displays(): - d.close() - if xvfb_pid: - log.info("killing xvfb with pid %s", xvfb_pid) - try: - os.kill(xvfb_pid, signal.SIGTERM) - except OSError as e: - log.info("failed to kill xvfb process with pid %s:", xvfb_pid) - log.info(" %s", e) + close_gtk_display() + kill_xvfb(xvfb_pid) add_cleanup(close_display) + else: + close_display = None if opts.daemon: def noerr(fn, *args): @@ -758,6 +776,25 @@ #xvfb problem: exit now return 1 + #create devices for vfb if needed: + devices = {} + if not start_vfb and not proxying and not shadowing: + #try to find the existing uinput uuid: + #use a subprocess to avoid polluting our current process + #with X11 connections before we get a chance to change uid + from xpra.os_util import get_status_output + cmd = ["xprop", "-display", display_name, "-root", "_XPRA_UINPUT_ID"] + try: + code, out, err = get_status_output(cmd) + except Exception as e: + log("failed to get existing uinput id: %s", e) + else: + log("Popen(%s)=%s", cmd, (code, out, err)) + if code==0 and out.find("=")>0: + uinput_uuid = strtobytes(out.split("=", 1)[1]) + if uinput_uuid: + devices = create_input_devices(uinput_uuid, uid) + if ROOT and (uid!=0 or gid!=0): log("root: switching to uid=%i, gid=%i", uid, gid) setuidgid(uid, gid) @@ -784,7 +821,7 @@ from xpra.x11.vfb_util import verify_display_ready if not verify_display_ready(xvfb, display_name, shadowing): return 1 - from xpra.x11.gtk2.gdk_display_util import verify_gdk_display + from xpra.x11.gtk2.gdk_display_util import verify_gdk_display #@Reimport display = verify_gdk_display(display_name) if not display: return 1 @@ -833,6 +870,7 @@ save_xvfb_pid(xvfb_pid) if POSIX: + save_uinput_id(uinput_uuid or "") dbus_pid = -1 dbus_env = {} if clobber: @@ -904,7 +942,6 @@ #publish mdns records: if opts.mdns: - from xpra.os_util import strtobytes from xpra.platform.info import get_username from xpra.server.socket_util import mdns_publish mdns_info = { Index: xpra/server/server_util.py =================================================================== --- xpra/server/server_util.py (revision 16711) +++ xpra/server/server_util.py (working copy) @@ -295,13 +295,26 @@ log.error(" %", e) return None -def create_uinput_pointer_device(uid, gid): +def has_uinput(): from xpra.log import Logger log = Logger("server") try: import uinput + assert uinput except (ImportError, NameError) as e: - log.error("Error: cannot create uinput devices:") + log.info("cannot access python uinput module:") + log.info(" %s", e) + return False + else: + return True + +def create_uinput_pointer_device(uuid, uid, gid): + from xpra.log import Logger + log = Logger("server") + try: + import uinput + except (ImportError, NameError) as e: + log.error("Error: cannot access python uinput module:") log.error(" %s", e) return None events = ( @@ -320,38 +333,48 @@ ) BUS_USB = 0x03 #BUS_VIRTUAL = 0x06 + VENDOR = 0xffff + PRODUCT = 0xffff + VERSION = 0 + #name = "Xpra Virtual Pointer %s" % uuid name = "Xpra Virtual Pointer" try: - uinput_pointer = uinput.Device(events, name=name, bustype=BUS_USB, vendor=0, product=0, version=0) + uinput_pointer = uinput.Device(events, name=name, bustype=BUS_USB, vendor=VENDOR, product=PRODUCT, version=VERSION) except OSError as e: - log.error("Error: cannot open uinput,") - log.error(" make sure that the kernel module is loaded") - log.error(" and that the /dev/uinput device exists:") - log.error(" %s", e) + log("uinput.Device creation failed", exc_info=True) + if os.getuid()==0: + #running as root, this should work! + log.error("Error: cannot open uinput,") + log.error(" make sure that the kernel module is loaded") + log.error(" and that the /dev/uinput device exists:") + log.error(" %s", e) + else: + log.info("cannot access uinput: %s", e) return None dev_path = get_uinput_device_path(uinput_pointer) if not dev_path: uinput_pointer.destroy() return None - try: - #log("chown%s", (dev_path, uid, gid)) - os.lchown(dev_path, uid, gid) - #udev rules change the device ownership - #FIXME: fix udev or use inotify? (racy) - import time - time.sleep(1) - log("chown%s", (dev_path, uid, gid)) - os.lchown(dev_path, uid, gid) - #os.lchown("/dev/input/mouse2", uid, gid) - except OSError as e: - log.error("Error: failed to change ownership of '%s':", name) - log.error(" at %s:", dev_path) - log.error(" %s", e) - return None + if False: + try: + #log("chown%s", (dev_path, uid, gid)) + os.lchown(dev_path, uid, gid) + #udev rules change the device ownership + #FIXME: fix udev or use inotify? (racy) + import time + time.sleep(1) + log("chown%s", (dev_path, uid, gid)) + os.lchown(dev_path, uid, gid) + #os.lchown("/dev/input/mouse2", uid, gid) + except OSError as e: + log.error("Error: failed to change ownership of '%s':", name) + log.error(" at %s:", dev_path) + log.error(" %s", e) + return None return name, uinput_pointer, dev_path -def create_uinput_devices(uid, gid): - d = create_uinput_pointer_device(uid, gid) +def create_uinput_devices(uinput_uuid, uid, gid): + d = create_uinput_pointer_device(uinput_uuid, uid, gid) if not d: return {} name, uinput_pointer, dev_path = d @@ -363,5 +386,5 @@ } } -def create_input_devices(uid, gid=-1): - return create_uinput_devices(uid, gid) +def create_input_devices(uinput_uuid, uid, gid=-1): + return create_uinput_devices(uinput_uuid, uid, gid) Index: xpra/x11/vfb_util.py =================================================================== --- xpra/x11/vfb_util.py (revision 16711) +++ xpra/x11/vfb_util.py (working copy) @@ -23,21 +23,11 @@ assert len(DEFAULT_DESKTOP_VFB_RESOLUTION)==2 -XORG_DEVICE_OPTIONS = { - "pointer" : """ - MatchIsPointer "True" - Driver "libinput" - Option "AccelProfile" "flat" -""", - "keyboard" : 'MatchIsKeyboard "True"', - } - - -def create_xorg_device_configs(xorg_conf_dir, devices, uid, gid): +def create_xorg_device_configs(xorg_conf_dir, device_uuid, uid, gid): from xpra.log import Logger log = Logger("server", "x11") cleanups = [] - if not devices: + if not device_uuid: return cleanups def makedir(dirname): @@ -62,40 +52,35 @@ for d in dirs: makedir(d) - #create individual device files: + #create individual device files, + #only pointer for now: i = 0 - for dev_type, devdef in devices.items(): - #ie: - #name = "pointer" - #devdef = {"uinput" : uninput.Device, "device" : "/dev/input20" } - match_type = XORG_DEVICE_OPTIONS.get(dev_type) - uinput = devdef.get("uinput") - device = devdef.get("device") - name = devdef.get("name") - if match_type and uinput and device and name: - conf_file = os.path.join(xorg_conf_dir, "%02i-%s.conf" % (i, dev_type)) - with open(conf_file, "wb") as f: - f.write(""" -Section "InputClass" + dev_type = "pointer" + #name = "Xpra Virtual Pointer %s" % device_uuid + name = "Xpra Virtual Pointer" + conf_file = os.path.join(xorg_conf_dir, "%02i-%s.conf" % (i, dev_type)) + with open(conf_file, "wb") as f: + f.write(b"""Section "InputClass" Identifier "xpra-virtual-%s" MatchProduct "%s" - MatchDevicePath "%s" + MatchUSBID "ffff:ffff" + MatchIsPointer "True" + Driver "libinput" + Option "AccelProfile" "flat" Option "Ignore" "False" -%s EndSection -""" % (dev_type, name, device, match_type)) - os.fchown(f.fileno(), uid, gid) - #Option "AccelerationProfile" "-1" - #Option "AccelerationScheme" "none" - #Option "AccelSpeed" "-1" - def cleanup_conf_file(): - log("cleanup_conf_file: %s", conf_file) - os.unlink(conf_file) - cleanups.insert(0, cleanup_conf_file) +""" % (dev_type, name)) + os.fchown(f.fileno(), uid, gid) + #Option "AccelerationProfile" "-1" + #Option "AccelerationScheme" "none" + #Option "AccelSpeed" "-1" + def cleanup_conf_file(): + log("cleanup_conf_file: %s", conf_file) + os.unlink(conf_file) + cleanups.insert(0, cleanup_conf_file) return cleanups - -def start_Xvfb(xvfb_str, pixel_depth, display_name, cwd, uid, gid, username, xauth_data, devices={}): +def start_Xvfb(xvfb_str, pixel_depth, display_name, cwd, uid, gid, username, xauth_data, uinput_uuid=None): if not POSIX: raise InitException("starting an Xvfb is not supported on %s" % os.name) if not xvfb_str: @@ -134,11 +119,6 @@ def pathexpand(s): return shellsub(s, subs) - #create uinput device definition files: - #(we are assuming that Xorg is configured to use this path..) - xorg_conf_dir = pathexpand(get_Xdummy_confdir()) - cleanups = create_xorg_device_configs(xorg_conf_dir, devices, uid, gid) - #identify logfile argument if it exists, #as we may have to rename it, or create the directory for it: import shlex @@ -172,7 +152,8 @@ if not xvfb_cmd: raise InitException("cannot start Xvfb, the command definition is missing!") - if devices: + if uinput_uuid: + #use uinput: #identify -config xorg.conf argument and replace it with the uinput one: try: config_argindex = xvfb_cmd.index("-config") @@ -184,6 +165,10 @@ xorg_conf = xorg_conf.replace("xorg.conf", "xorg-uinput.conf") if os.path.exists(xorg_conf): xvfb_cmd[config_argindex+1] = xorg_conf + #create uinput device definition files: + #(we have to assume that Xorg is configured to use this path..) + xorg_conf_dir = pathexpand(get_Xdummy_confdir()) + cleanups = create_xorg_device_configs(xorg_conf_dir, uinput_uuid, uid, gid) xvfb_executable = xvfb_cmd[0] if (xvfb_executable.endswith("Xorg") or xvfb_executable.endswith("Xdummy")) and pixel_depth>0: Index: xpra/x11/x11_server_base.py =================================================================== --- xpra/x11/x11_server_base.py (revision 16775) +++ xpra/x11/x11_server_base.py (working copy) @@ -178,6 +178,7 @@ X11ServerCore.last_client_exited(self) def init_virtual_devices(self, devices): + #(this runs in the main thread - before the main loop starts) #for the time being, we only use the pointer if there is one: pointer = devices.get("pointer") if pointer: Index: udev/rules.d/71-xpra-virtual-pointer.rules =================================================================== --- udev/rules.d/71-xpra-virtual-pointer.rules (revision 16652) +++ udev/rules.d/71-xpra-virtual-pointer.rules (working copy) @@ -1,3 +1,3 @@ # allow xpra to use fine grained scrolling -ACTION=="add|change", ATTRS{name}=="Xpra Virtual Pointer", ENV{MOUSE_WHEEL_CLICK_ANGLE}="1", ENV{MOUSE_WHEEL_CLICK_COUNT}="360" +ACTION=="add|change", ATTRS{name}=="Xpra Virtual Pointer", ENV{MOUSE_WHEEL_CLICK_ANGLE}="1", ENV{MOUSE_WHEEL_CLICK_COUNT}="360", MODE="0666"