Back

Show HN: X11 tool to share a screen area in any video meeting

346 points1 yeargithub.com
splitbrain1 year ago

I have a big 49" wide screen monitor and sharing my screen in Google Meet was cumbersome because you can only share a window or the whole screen, but not a screen region.

So I wrote a small tool that uses the xrandr extension to mirror an area to a virtual monitor which then can be shared.

See my blog post for some more details: https://www.splitbrain.org/blog/2024-10/11-introducing_clips...

wooptoo1 year ago

This is very cool and definitely useful when you have a large screen at your disposal. I have a 27" screen which doesn't give you as much screen real-estate as yours. So what I'm using is a script which spawns a separate Xephyr window as DISPLAY 9, and puts a bunch of windows on that screen. https://gist.github.com/radupotop/d77a47767e2e65a7e7d40d1ea8... I use this as my demo environment.

kees991 year ago

Neat trick. Another, similar one, is possible with "xrandr --setmonitor". That doesn't require starting a separate X server & WM instance:

https://askubuntu.com/q/150066#998435

However, both Xephyr/Xnest and "xrandr --setmonitor" create separate non-overlapping screen(s), which means sharing part of a window (say, browser sans chrome [1]) can't be done here, but is possible with OP's tool.

[1] EDIT: "chrome", as in - parts of browser's window other than webpage itself: tabs, URL bar, bookmarks, etc...

OsrsNeedsf2P1 year ago

I love how simple this is- Barely 100 lines or C++ (ignoring comments). That's one thing that makes me prefer X11 over Wayland.

asveikau1 year ago

The code is a little weird. There is no XLib event loop. It calls sleep(100) in a loop until it hits SIGINT. That will have high cpu usage for no reason.

diath1 year ago

It will not, even adding just a 1ms sleep in a loop will drop CPU usage to barely noticeable levels, 10 wakes a second is barely anything for any CPU from the past 3 decades.

thwarted1 year ago

This is what the pause(2) syscall was made for, waiting for a signal forever.

larschdk1 year ago

Sure, if that is the only program, but it is not. This kind of thinking drains batteries faster than necessary, drains the cache, and reduces CPU efficiency. sleep() is a wasteful system call, a kludge at best, and is never the correct solution to a synchronization problem.

Too1 year ago

It’s a good way to drain your battery on mobile devices, even if usage looks low.

Not that this matters for this particular tool.

+1
erickj1 year ago
asveikau1 year ago

Not my experience at all. Granted I haven't tried writing a loop like this in 20ish years, because once you spot that mistake you don't tend to make it again, and CPUs are better now.

Another thing to note is when you call sleep with a low value it may decide not to sleep at all, so this loop just might be constantly doing syscalls in a tight loop.

+2
diath1 year ago
funcDropShadow1 year ago

Whether the CPU is busy because of a loop with a sleep depends on the ration of the sleep time and the time to perform the rest of one loop iteration. Doing stuff in a loop iteration that takes 1min and then adding a ms sleep will not drop CPU usage a measurable amount.

account421 year ago

The question is about waiting, i.e. when you have no real work to do. If you have significant work to do then there is no point in sleeping until that work is done.

splitbrain1 year ago

FYI the code has been updated to use sigwait instead.

EasyMark1 year ago

This is a sleep which allows the kernel scheduler to move to a new process and not a busy-wait so it will have very very low processor usage.

tapoxi1 year ago

In Wayland you just start a capture with the xdg-desktop-portal API and it notifies the user and let them select the area to capture.

gchamonlive1 year ago

Yes, but I believe op was refering to how interacting with all things Wayland seems to be more involved than with x11. I'm not sure this is indeed like this, I have zero experience in developing for Wayland, but I think this is what op meant.

yndoendo1 year ago

Wayland is more focused on security. That onion layer right there will increase the complexity of usage. X11 doesn't have the extra abstractions to limit and prevent intrusive interactions with the desktop.

Example of this would be where "runas /user:smith application.exe" is simple but does not work when a Windows Service is required to run an application as the user signed in. One must use Window's API to pull in the account's token and use more API to execute "application.exe". UltraVNC is a great source to see all the extras needed.

tapoxi1 year ago

From a quick "how do I implement this in Python" with ChatGPT it seems to be about 30 lines, since most of the heavy lifting is done for you by the API.

Zetaphor1 year ago

As someone who uses LLM's regularly to assist in code creation, take that output with a huge grain of salt until you've actually tested it. Especially as it relates to Wayland, I've pulled my hair out trying to get an LLM to assist with very similar tasks to this.

p_l1 year ago

It means you got to tickle the banana, good luck making sure that the gorilla holding it is fine with that.

jchw1 year ago

This certainly is an elegant X.org party trick that can't be done easily in almost any other windowing system: creating a virtual Xrandr display that overlaps with existing physical displays. It's slightly awkward since if it exits outside of sigint it will leave a virtual output and no overlay window but that's a pretty minor issue. (All of that having been said, I would strongly advise to not over-index on SLoC as a measure of quality or elegance.)

This flat-out can't be done in Wayland. Though all is not lost, you might not need this at all in Wayland. The standard way to capture the screen from an unprivileged process in Wayland is through desktop portals, and at least KDE supports a wide variety of different capture options including capturing a rectangle of the screen. I haven't tried, but I suspect this is even true when running X.org applications, thanks to XWaylandVideoBridge.

I am not really thrilled about D-Bus stuff everywhere, but it is nice that you can pretty much override any screen capture behavior you want by changing the org.freedesktop.impl.portal.ScreenCast implementation: I think that's actually a step in a better direction versus having every application implement its own functionality for selecting capture targets.

rnhmjoj1 year ago

To me it's quite sad that for a lot of things, the "standard" way of doing something is not actually part of the standard (XDG portals, third party protocols, etc.). Yes, X.org is old, bloated, unmaintainable and whathever, but at least every desktop environment used the same X server implementation and the same tools worked everywhere.

Besides the duplication of efforts in implementing the same stuff over and over, now someone developing somewhat non-trivial programs needs to be aware of the differences in supported features and non-standard extensions in all desktops, for example [1].

[1]: https://wayland.app/protocols/cursor-shape-v1#compositor-sup...

jchw1 year ago

I think Wayland had made some mistakes, no doubt. Cursor shape just should've been part of the protocol. Wayland has its fair share of misfires.

That said, I understand what they were going for. They really wanted to make the compositor as small and simple as possible, so for example you would just use libwayland-cursor instead of bothering with cursors yourself. However there are a lot of ways libwayland-cursor worked out poorly... Not everyone agreed on how scaling should work, GTK4 ditched it for performance reasons, and overall it's just inconvenient for a lot of cases (languages other than C, sandboxing, etc.) And to make matters even worse, in practice every compositor needed to load and handle XCursor themes anyways...

That said, I think that it's okay if Wayland doesn't own the majority of the Linux desktop stack the way X11 did. It's fine for compositors and their helpers to implement protocols from other projects, too. That way Wayland can be more applicable to graphical machines other than desktops without bringing unnecessary baggage. It'll always have trade-offs, of course, but I think it's far from the end of the world.

yxhuvud1 year ago

re cursors: There is a replacement/amendment protocol for cursor handling coming though I don't know the current status of it. It seemed a lot easier to work with for the vast majority of cases.

sim7c001 year ago

there's very little code because there's very little error handling / sanity checking. not saying X11 isn't hackable and cool, but a lot of code gets bloated and complex (and robust!) by not assuming perfect usage.

for example. run ./clipscreen 1 2 3 4

splitbrain1 year ago

True. If something goes wrong this will just crash. But to be fair, the only error handling I could think of would probably just exit with a vague error message... Pull requests to make it more robust welcome anyway!

xrd1 year ago

To the parent, splitbrain just got you to QA this for him. The true cost of software is the maintenance and QA, and he got you to do free work, and here I am doing free work writing about it. How hard we BOTH just got pwned! </joke>

sim7c001 year ago

will work for food

sim7c001 year ago

haha yeah, its ok for a tool its really cool honestly :p just commenting on the 'so little code' might be good to check if the x y etc. are within the screen / set resolution perhaps.

ajross1 year ago

Yeah. I mean, not to deny the decades of arguments over its warts, but it's kind of amazing to me the extent to which X11 has emerged as, well, the simplest/best and most hackable desktop graphics environment available. You want to play a trick, it's right there. The ICCCM got a ton of hate back in the early 90's, but... no one else has an equivalent and people still innovate in the WM space even today.

WD-421 year ago

Hackable is right. But not always in the positive sense of the word.

l721 year ago

I find it very interesting how much our threat model has changed in the last 10-15 years. We no longer trust even local software, as we have to assume everything is now malicious. Commercial software from "reputable" companies can't be trusted to not pull a ton of analytics and personal data off your computer. We now have to worry about every piece of software being a keylogger and spying on other windows/applications and reporting back.

We've had to give up so much flexibility. Wayland certainly focuses on plugging this hole, but it means we've lost all these cool utilities like this one. There was just so much you could do with devilspie, xdotool, and others to make sure my operating system and window environment worked for me.

I still really miss X11's Zaphod mode, where you had two independent X sessions (:0.0 and :0.1) on two different monitors, with different window managers and different windowing rules.

I miss the days of being able to trust my computer and trust my software.

+2
singpolyma31 year ago
BlueTemplar1 year ago

> Commercial software from "reputable" companies can't be trusted to not pull a ton of analytics and personal data off your computer.

Thankfully, for a lot of software, there is no reason to ever give them network access in the first place.

account421 year ago

What is interesting is that physical home security has gone in the opposite direction - people are happy to put dozens of devices in they home which can (and some definitely do) stream everything they hear and see to the cloud.

ajross1 year ago

FWIW, the threat model you're imagining is an attacker being able to run code to display directly to the desktop using the lowest level native API. A local[1] code exploit at the level of an interactive user is already a huge failure in the modern world.

Is that a reasonable argument against using X11? Sure, for some use cases. Is it a good argument for wayland/windows/OSX/whatever to do your tiling WM experimentation? Not really, those environments kinda suck for playing around with.

[1] Or "local-ish", your system or a trusted remote has to have been compromised already. Untrusted X11 protocol still exists but is deliberately disabled (and often blocked) everywhere. Even ssh won't forward it anymore unless you dig out the option and turn it on manually.

+2
boudin1 year ago
account421 year ago

It very much is. I expect programs running on my computer to not be restricted in how they can help me.

anthk1 year ago

The most hackable would have been a Lisp based desktop.

themerone1 year ago

X11 is the opposite of simple and hackable. What you are thinking of as "hackable" is actually the result of it having a ton of legacy features that enable users to do neat tricks.

Wayland breaks a lot of these tools because it is so much simpler than X.

ajross1 year ago

Lacking features isn't the same thing as "simpler", Wayland is great, but is very much a subset of the features implemented on an X11 desktop. Wayland doesn't do selections or provide any IPC mechanism of its own, much less something like an ICCCM that allows you to identify/target other users of the desktop and interact with them in a flexible way. In fact as I understand it the linked tool is in fact impossible to write in Wayland.

Again, this isn't the fault of "Wayland", which is just a compositor framework. The complaint is that the ecosystem of "desktop" software which evolved around Wayland is an ad hoc monstrosity that lacks the unified structure that its ancestor had way back in the X11R5 days.

vidarh1 year ago

By window manager started out as ~50 lines of Ruby copying an equivalent amount of C.

You can say many things about Wayland, but it's "simple" from a point of view I for one really do not care about. Wayland may be "simple" in some respects, but it makes most of the things I care about doing unnecessarily complex.

+1
bee_rider1 year ago
RedShift11 year ago

Wayland is simple because it shoves all the responsibilities to somewhere else.

teekert1 year ago

Is it much more difficult under Wayland?

favorited1 year ago

Wayland intentionally makes this more difficult, because one of the security goals of the project is that (by default) Wayland clients shouldn't have visibility into other clients' window contents/events/etc.

Of course, it still needs to be possible under Wayland, because there are plenty of legitimate use-cases (screenshots, screen sharing, video capture, etc.), but it was a non-goal to make it as simple as X.

Wayland merged the image-capture-source and image-copy-capture protocol extensions earlier this year: https://www.phoronix.com/news/Wayland-Merges-Screen-Capture

enriquto1 year ago

> Wayland intentionally makes this more difficult,

some men just want to watch the world burn

+3
favorited1 year ago
EasyMark1 year ago

This was in the wrong thread…

Brajeshwar1 year ago

Also, I remember a friend showing me in Zoom that you can share not just one but multiple screens/windows—press the SHFT key while clicking the windows you want to share.

HPsquared1 year ago

How do people discover these things?

jdbdndj1 year ago

Isn't that the same of how you select multiple files in most file managers?

Shift+Click: select from currently selected item to clicked item

Ctrl+Click: add/remove clicked item to set of selected items

hackernewds1 year ago

Sure, but the idea you can share multiple windows this way.

Can Google Meet (or hangout or w/e they call it now adays) do some of this?

starkparker1 year ago

Reading documentation, usually: Sharing your desktop, screen, or content in https://support.zoom.com/hc/en/article?id=zm_kb&sysparm_arti...

More useful shortcuts in https://support.zoom.com/hc/en/article?id=zm_kb&sysparm_arti...

Brajeshwar1 year ago

The same question I asked (that was me after using Zoom for 3+ years).

IceDane1 year ago

You can literally do this with just xrandr.

xrandr --setmonitor screenshare 2560/1x1440/1+0+0 none

Liskni_si1 year ago

To make the selection interactive, the command becomes:

geo=$(slop -f '%w/1x%h/1+%x+%y') && xrandr --setmonitor screenshare "$geo" none

Liskni_si1 year ago

Also looks like one can just do

xrandr --delmonitor screenshare

before this to seamlessly update the shared region — Chrome will immediately pick up the change and continue sharing the updated virtual monitor.

attah_1 year ago

In fairness; that and the overlay is what is happening, just from C++. Props for nice oneliner none the less. :)

salviati1 year ago

Do I understand correctly that you could to this with OBS on any platform, including Wayland? I'm reading many comments that make me think either many people don't know about OBS, or I'm overestimating it's abilities.

splitbrain1 year ago

You probably can. I never used OBS, but it's probably a bit more than a 20kb binary though ;-)

lopkeny12ko1 year ago

I don't understand, what is the significance of a 20kb binary? The only person using this would be someone who takes Zoom meetings on a company-issued computer and I can't imagine such machines are disk space-constrained.

hamdouni1 year ago

I'm not aware of company issued computer with x11. Is it really a thing ?

phkahler1 year ago

Some companies let you run Linux on their company issued computer.

movedx1 year ago

It can do that, yes, but it's a bit more work. There are several GUI hoops you'll have to run through to get that to work, and if you have to adjust it each and every time, before a meeting, then it would become burdensome. But yes, it can be done.

phkahler1 year ago

OBS lets you share a window or just the client area of an app.

movedx1 year ago

With OBS, you can add an entire screen to your canvas and then add a filter to crop it down to a particular part of that screen. This nets you the same results as the small C++ tool being proposed here.

A lot more work involved, though.

alexcroox1 year ago

With my 49" I use OBS + Mouse follow script + OBS preview window on laptop screen and share my entire laptop screen. That way I know the window size is suitable for others viewing from their laptops and the preview follows my mouse and you can tweak zoom levels and mouse boundaries on the fly. Also the OBS preview window opens on launch, without opening the main OBS window. So you never have to see/interact with the main OBS window/application again so it really feels standalone which is great.

https://github.com/BlankSourceCode/obs-zoom-to-mouse

yanchep1 year ago

When I click share in Jitsi (or whatever) on KDE 6.1 w/ Wayland in Chromium, I get offered to share the 'Entire Screen". After that dialog another one pops up with the options 'Full Workspace', 'New Virtual Output', 'Rectangular Region' and a list of my displays. 'Rectangular Region' allows to share a selected part of the screen, 'New Virtual Output' instantly creates a virtual screen, visible in KDE 'Display Configuration' that may or may not be positioned on top of pre-existing physical displays.

z9911 year ago

Wow, this is fantastic! This exact use case, on Linux, is why our company selected Zoom instead of Meet.

Awesome!

z9911 year ago

Built it and took a fullscreen screenshot with GIMP to figure out the width/height/x/y coordinates I wanted and tested with Google Meet. Working perfectly!

machinestops1 year ago

https://github.com/naelstrof/slop Can also use a utility like this one, which lets you select an area of the screen and output it in a specified format.

z9911 year ago

Wow that is also very cool. For those wondering, this is what it looks like:

  $ sudo apt install slop
  $ slop
       <selects an area on screen>
  1719x1403+1080+277
+2
machinestops1 year ago
hackernewds1 year ago

this is just cmd+shift+5 in a Mac OS

yjftsjthsd-h1 year ago

Is it? I thought that took a screenshot, not fed coordinates to a program (in this case a screen sharing program)

hackernewds1 year ago

Did you have any issues implementing?

zh31 year ago

You can do this with xzoom, which also allows magnification from x1 upwards - is there an advantage I'm missing here?

0cf8612b2e1e1 year ago

Can someone explain why this is still an unmet need within the current video conference platforms? Giant monitors have become increasingly common-especially for the developers who might be working on these tools.

simonmysun1 year ago

Maybe because a workaround with OBS isn't that difficult?

movedx1 year ago

You might have missed the point being made here.

We, as engineers, can't expect everyone to know what OBS is, download it, learn in, and use it every day to enable their ability to share a sub-section of their monitor (regardless of its size.)

We, as engineers, _are_ expected to make our software easier to use and feature rich. Adding this capability into Zoom or Meet, etc. is a reasonable thing to expect from a software company of note. And people _do_ know what Zoom and Meet are, download them, learn them, and use them every day. Why not implement the feature directly into the software they're already using?

wink1 year ago

Also which percentage of people taking part in meetings with screen sharing are even allowed to "just install" OBS? More for developers, much less for other office workers.

simonmysun1 year ago

I mean a possible alternative can be a huge resistance. Users knowing this should be feasible probably know OBS, and other users may never thought of this possibility.

I can't deny the effort the author here have done. It is very useful and convenient. I just gave an explanation of why this comes so late.

amelius1 year ago

Nice. This is the first time I read about creating a virtual monitor in X.

benjiweber1 year ago

This is brilliant. I've wanted this so many times and had to awkwardly switch between window being shared instead.

fweimer1 year ago

I wouldn't mind switching between windows if I could use the GNOME Activities overview for that. But maybe that is not possible because there is no way to communicate the change in stream size if the windows have different sizes?

TacticalCoder1 year ago

That s very cool... Speaking of which: any easy way to allow two people, both on X, to both share and interact (keyboard and mouse) with a common X window?

The app that we d like to share and both control is a browser (running on a machine on our LAN) so a browser extension would work too I guess.

bee_rider1 year ago

I think there was some way to do that with existing tools. I forget the details because I only threw it together as a bit of fun novelty. I think the terms to google are x2x and multiseat though, at least to start your search…

patrakov1 year ago

My preferred solution for that would be a VNC server (so that it shares the whole screen) installed in a VM.

tcsenpai1 year ago

This is surely useful right now. I wonder what will happens to all the nice X11 tools once Wayland (hopefully soon) will be the golden standard. There are options to enable X11 behaviors in Wayland but I guess that is just a fallback to the insecure implementation.

alanjames001 year ago

I've looking for something like this for quite sometime. It's simple, clean and elegant.

udev40961 year ago

This is only helpful if you are using a desktop environment. What about window managers like i3?

hamdouni1 year ago

I'm not sure about i3 but you can have floating windows in DWM and move them to the targeted area with the mouse

brazzledazzle1 year ago

This is so cool. Seeing the word splitbrain definitely gave me a moment of dreaded recollection though, not gonna lie. Not a fun place to be when you're dealing with databases and clusters.

procparam1 year ago

I've always wanted something like this, but for i3 workspaces. Something like "share workspace 2." Anyone know how to accomplish this?

tincholio1 year ago

That only works if the workspace is on an actual screen, because it's otherwise not rendered. Maybe the virtual monitor solution via xrandr would work for that, but I haven't yet tried that.

attah_1 year ago

I was just about to go looking for something like this! I'll look so pro on the meeting tomorrow :)

shmerl1 year ago

I'm waiting for ffmpeg to implement pipewire screen grab so it could work on Wayland.

yazzku1 year ago

Can you not use std::condition_variable to avoid the active waiting of the signal?

listeria1 year ago

I don't know if std::condition_variable is async-signal safe, but an easy fix is to replace the sleeping with pause(2). There's also sigwait(3), which wouldn't need a signal handler.

quotemstr1 year ago

signalfd or ppoll or a million other options

snowe20101 year ago

Dang. I need this for Mac. I’ve been wishing I had exactly this for years.

_joel1 year ago

Wasn't the same thing posted for MacOS a few days back, can't recall the name? Looking at the time on the repo makes me think the author pushed after seeing people requesting something similar for Linux.

edit: Here you go https://github.com/Stengo/DeskPad

joombaga1 year ago

That's not quite the same. With DeskPad you have to move the window to the virtual monitor. clipscreen allows you to select a portion of your screen without moving any windows.

zczc1 year ago

DeskPad was discussed there: https://news.ycombinator.com/item?id=41800602 And for Windows there is RegionToShare https://github.com/tom-englert/RegionToShare

thefreeman1 year ago

I use "Advanced Screen Share" for this purpose. it has a one time purchase if you want to remove a small overlay but it gets the job done and is installable through the app store.

iknowstuff1 year ago

you can just share multiple windows on the newest macOS and they will ne nicely arranged for viewers. You can even add the presenter thing to show your face next to them.

ho_schi1 year ago

Neat. Now I want for Wayland. Don’t use X11 for some years.

singpolyma31 year ago

Never to late to upgrade to X11 :)

TZubiri1 year ago

[flagged]

ragebol1 year ago

As per the blog post, only Zoom allows to stream a selected area of the screen. What other proprietary SW can do this?

With this tool, they should all be able to

splitbrain1 year ago

I'm not sure what you are referring to. What's the proprietary solution you're suggesting here?

squilliam1 year ago

OBS has had the ability to do this for quite some time

ceooflinux1 year ago

[flagged]