clipnotify icon indicating copy to clipboard operation
clipnotify copied to clipboard

Discriminate between primary and clipboard notification

Open Twix53791 opened this issue 3 years ago • 3 comments

Hi, thanks for this little program quite useful ! I use it to automatically copy the primary selection to the clipboard when I select something :

while clipnotify; do
    xclip -sel p -o | xclip -sel c
done

But, this poses a problem when on an application like libreoffice I need to copy with ctrl+c directly to clipboard, as my script gonna erase this selection overwriting the clipboard with the primary selection. I use now a trick : storing the clipboard content in a variable, I can check if the new clipboard content is different than the stored one (a new clipboard selection is done) or not (a new primary selection is done).

while clipnotify; do
   if [[ $(xclip -sel c -o) == "$clipboard" ]]; then
      xclip -sel p -o | xclip -sel c
   fi
   clipboard=$(xclip -sel c -o)
done

But it would be more elegant to have the possibility to discriminate between primary and clipboard notification with clipnotify or something else. I would like a syntax like:

while clipnotify; do
    if the sel is primary then
done

Why not something like the bspwm daemon : bspc subscribe node_geometry | while read -a msg; do sending just a message with the name of the X selection (primary/clipboard...) ?

Twix53791 avatar May 25 '22 06:05 Twix53791

It's totally possible. For what it's worth, what we do in clipmenu is exactly that: just store the existing content temporarily and see if it matches.

cdown avatar May 25 '22 23:05 cdown

  while True:
    # clipnotify exits on a copy event
    original = os.popen("xclip -o -sel clip").read()
    os.popen("clipnotify").read()
    content = os.popen("xclip -o -sel clip").read()
    # Ignore alt clipboard event
    if content != original:
      get_items()
      add_item(content)

It's still a bit confusing if I want the copy event to trigger twice.

For instance if I copy "something" to my clipboard manager, but then I delete the item from the clipboard manager, and then I copy "something" again, it won't get added, because the variable trick will make it ignore it. I'm probably missing something silly though.

There should be a flag to make it only detect normal copy events.

madprops avatar Jul 05 '22 01:07 madprops

Hi, I turns back to this question these days, as I have got a problem using my small script copying automatically the primary selection to clipboard on a clipnotify event in emacs pdf-tools. In fact, if i copy something directly to the clipboard, of course my script erase it immediately copying the primary to the clipboard. From this issue I have got the idea to look at the clipnotify.c file and erase the line XFixesSelectSelectionInput(disp, root, clip, XFixesSetSelectionOwnerNotifyMask);, then recompile it. It works perfectly fine, now my clipnotify bin doesn't report an event when something is directly sent to the clipboard.

But, still... it would be nice to be able to discriminate between primary and clipboard event in clipnotify. My problem is I know almost nothing on c language and even less about the X11 functions... I tried to returns a different value depending on the XFixesSelectSelectionInput event, but it seems I can't understand how to get its results (if statement, variable...). It would be nice just to return a different value in case of the event is coming from the first XFixesSelectSelectionInput or the second one, as the return value of clipnotify can be called in a bash script with $?...

I saw also the interesting pull requests from @alexozer and @andreblanke, which allow to disable interactively the primary event or the clipboard event, but it is still not be to able to discriminate between the two selection events in a same program/script, for example from the same while loop... You have to run two different daemon for example to got distinct events from primary or clipboard.

Not really a big deal as I don't use it anyway, but I ll be glad to understand how to do it... Thanks for any clue!

Twix53791 avatar Aug 31 '22 08:08 Twix53791

Hello guys. If you want to make clipnotify only react to clipboard notification instead of primary, then change line 23 from

    XFixesSelectSelectionInput(disp, root, XA_PRIMARY, XFixesSetSelectionOwnerNotifyMask);

to

    XFixesSelectSelectionInput(disp, root, XA_SECONDARY, XFixesSetSelectionOwnerNotifyMask);

XA_SECONDARY is responsible for reacting to only clipboard events and not to selection too.

TCH68k avatar Nov 04 '22 11:11 TCH68k

I made a patch for it: http://oscomp.hu/depot/clipnotify.c.patch If you pass -ce as the first argument, then it'll only detect clipboard events and will ignore selections.

TCH68k avatar Nov 04 '22 13:11 TCH68k

I made this repo based on andreblanke's patch https://github.com/madprops/copyevent

madprops avatar Nov 04 '22 13:11 madprops

Didn't know there's an alternative, thx.

TCH68k avatar Nov 04 '22 13:11 TCH68k

Hey! Thanks for bumping this, I'd kind of forgotten this request. Sorry to have missed andreblanke's patch. It looks like GitHub didn't send me any e-mail for the ping from @madprops, sadly.

I have two patches for clipnotify queued for release. One of them is a slightly changed version of Andre's patch with backwards compatibility fixes. Please feel free to test: https://github.com/cdown/clipnotify/tree/cdown/2022-11-04/clipnotify_2

If all looks good, then I'll merge and release clipnotify 2.0.0. Thanks!

cdown avatar Nov 04 '22 18:11 cdown

Clipnotify 2.0 exits instantly without the loop argument, because XNextEvent() is inside that branch; it needs an else branch too with XNextEvent() within it. Patch is here: http://oscomp.hu/depot/clipnotify2.c.patch (But i guess, you can type it in faster than download and apply the patch.)

TCH68k avatar Nov 04 '22 22:11 TCH68k

Well, that will teach me to add "just one more commit since we're here" :-) Now fixed.

cdown avatar Nov 04 '22 22:11 cdown

094dd7e20a06eba80c2e6f27dee775106e0eeca9 is now on master and will be released soon as part of clipnotify 2.0. :-)

cdown avatar Nov 07 '22 00:11 cdown