Skip Navigation

DIY software KVM (keyboard-video-mouse) switch

I hate spending money on hardware when there's a software solution. like, I've got a subwoofer from a 2.1 system (without satellites) for free. instead of sourcing speakers for it, however cheap they might be, I'll just utilize the speakers from my monitor. pipewire to the rescue, it creates a combined sink that outputs sound to both DP and analog audio, et voila - a 2.1 sound system. people are like "your monitor sounds like that!?" and usually I play dumb: "yeah, yours doesn't? well that's linux for ya".

so, I have a desktop and a laptop and I'd like to share the same monitor, keyboard, and mouse. modern monitors have a KVM switch integrated, you connect your keyboard/mouse/etc. to it, one USB Type-C cable to the laptop, a couple to the desktop and you have a seamless switch; it even charges the laptop, how cool is that!

however, my monitor works just fine and I don't replace my hardware unless I really have to. USB KVMs with similar functions aren't cheap. also, the monitor already has multiple display inputs so I got to thinking, how do I re-create this with no money, or as little as possible, with DIY tech?

first, switching the display; this one took me no time at all. I have a USB Type-C to DP cable (with DP-Alt) and with the power of udev (detect a connection then trigger stuff) and ddcutil (sends commands to the monitor) I got it working as seamless as possible - I connect the laptop and it automagically switches the monitor over. when I disconnect it, the monitor falls back to an active connection, which is the desktop. awesome!

now how about switching the keyboard and mouse over? I'd like to do it in software, like barrier/inputleap does it but without having to move the cursor to the adjacent monitor. also, both machines are on wayland which isn't supported. eventual input lag to the laptop is unimportant, I game on the desktop. no idea if that can be accomplished or if that's even a thing...

that is a thing - it's called USB/IP, i.e. sharing your USB device over TCP/IP; it's a kernel module included by default for a long time now and that thing rocks! not only does the USB keyboard work without any perceivable lag on the laptop, it get's "disconnected" from the host, so your keypresses aren't disturbing the host. since it's a kernel module, no need to convince wayland to play ball! this also works for webcams, scanners, readers, etc., the client system thinks this is a local device and it just works.

so all we have to do is expand the shell script to bring over the keyboard and mouse along with switching over the monitor once the USB-C connection is detected annnd... success!

well, sorta. my wireless mouse is second-hand and I haven't got the USB receiver for it, so it connects over bluetooth. tried sharing with usbip and it works, but the radio connection gets interrupted or something and the mouse doesn't work there. maybe there's a workaround but I don't want to dig further.

also, how do I switch back to the desktop to shoot some peggies? I don't want to disconnect the laptop manually so I could come up with a slew of shell scripts and udev triggers and I'd also need a ssh tunnel, I don't want my keyboard input to travel over the LAN in cleartext, etc... kinda cumbersome. also, once the novelty wears off, the automagicallity gets tired, I'd prefer manually switching between devices with a keyboard combo.

enter rkvm. it's written in rust and as everybody knows that's super awesome. unlike usbip, comms are encrypted, so no sniffing possible, and hotkey switching is a default function, and it also handles the mouse!

now, rkvm currently doesn't support triggers, like "do X when hotkey combo pressed", but Plasma can handle running the monitor switch script on each device separately by listening to the same hotkey combo.

both solutions have their advantages and disadvantages, usbip requires more legwork but is more powerful whereas rkvm is simpler and easier to set up.

the final step, powering the laptop over the same cable. sadly, can't handle that in software, but there are power delivery injectors out there, some as cheap as $7. also, there's this cool project, looks easy enough to source and implement. not sure if I'm going that way or just go with a used dock station, as those can be had for a song for popular business ex-flagships, like the Thinkpad T-series, HP Elitebooks, Dell Latitudes, etc..

are there downsides? sure there are. numero uno, the host (desktop, in my case) has to be on all the time. not a big deal for me, it gets woken in the morning and suspended late at night. there are edge cases when rkvm geeks out, but for a thing that's in its 0.x version, this is more than usable.

so, I've been using this setup for the past week or so and haven't yet found it to break or have any negative side-effects. gaming on the desktop is as snappy (or shitty) as ever and using my mech keyboard and giant screen on the laptop allows me to easily compartmentalize my business and private stuff.

thanks for reading!

edit: edited title to clarify I'm talking about a Keyboard-Video-Mouse switch, not a Kernel-Virtual-Machine.