You are not logged in.

#1 2019-08-31 17:28:40

M5Hc6UsQ
Member
Registered: 2018-02-26
Posts: 33

[SOLVED] How to disable the middle mouse paste behavior?

How can I disable the middle mouse paste behavior without deactivating the middle click entirely? This behavior is just a pain on a thinkpad because the middle mouse key is also used for scrolling which results in many unwanted pasted text. I know that I can disable the button with xinput  set-button-map but I'd like to keep the functionality in the browser for example (opens a link in a new tab). Is this possible or is the only solution to disable the button entirely?

Last edited by M5Hc6UsQ (2019-09-07 08:36:45)

Offline

#2 2019-08-31 17:52:35

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,446
Website

Re: [SOLVED] How to disable the middle mouse paste behavior?

You could have a program in the background that puts an empty string in the primary selection and simply reclaims the primary selection every time another program tries to claim it.  But this would prevent any use of the primary selection (e.g., shift+ins in some programs).

EDIT: or slightly simpler, just set the selection to None every time it is claimed.

Last edited by Trilby (2019-08-31 17:53:54)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2019-08-31 23:17:18

Alborix
Member
Registered: 2019-08-31
Posts: 2

Re: [SOLVED] How to disable the middle mouse paste behavior?

I haven't tested this myself OP but I did a search out of curiosity and this seems like a pretty good solution. It's a few years old but there's a reply to it from relatively recently indicating it still works: https://unix.stackexchange.com/a/277488

Last edited by Alborix (2019-08-31 23:17:29)

Offline

#4 2019-09-01 07:40:58

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 72,286

Re: [SOLVED] How to disable the middle mouse paste behavior?

This approach claims the primary selection for xbindkeys and drops it by killing and restarting xbindkeys when you release the MMB, then also fakes a MMB click (w/ now an empty primary selection) what makes it a very clumsy version of Trilby's suggestion, though acting on the mouse is better.

What one could do instead was to listen to button 2 presses, copy the primary selection owner, set it to None and restore it after™ (ie. briefly sleep) the release to maintain the primary selection buffer (though I could not name applications that you the IBM shortcuts for th primary selection off the top of my head.

The nasty thing will be to read the input events, either you (passively) grab the pointer (meh) or you try to XSelectInput on *all* windows (uahhh) or you use XI2 (which is pointer mapping agnostic, but you can isolate the input device - nice - and a parameter could define which button to listen to) - I'd be inclined to use XI2.

Edit: clarification what you'd copy.

Last edited by seth (2019-09-01 07:41:55)

Offline

#5 2019-09-01 20:16:30

Swiggles
Member
Registered: 2014-08-02
Posts: 266

Re: [SOLVED] How to disable the middle mouse paste behavior?

If you are using a gnome based desktop you can use the gnome-tweak-tool to disable it. There is an option for "Mouse and Keyboard -> Middle click paste" or you could run this command, it does the same:

gsettings set org.gnome.desktop.interface gtk-enable-primary-paste false

Offline

#6 2019-09-03 12:29:11

M5Hc6UsQ
Member
Registered: 2018-02-26
Posts: 33

Re: [SOLVED] How to disable the middle mouse paste behavior?

@Swiggles I'm not using Gnome and I guess it would only work for Gnome applications.

@Alborix This solution actually works. Don't know how I missed that. Thanks.

@Trilby @seth Is there a tool which does what you are proposing? I have no experience with xorg therefore writing my own tool would be time consuming. For now I'll stick to the solution from above even if it's not the most elegant.

Offline

#7 2019-09-03 16:51:29

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,446
Website

Re: [SOLVED] How to disable the middle mouse paste behavior?

This would implement my plan:

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xfixes.h>

int main() {
	Display *dpy;
	Window root;
	int ev_base, err_base;
	XEvent ev;
	if (!(dpy=XOpenDisplay(0x0)))
		return 1;
	root = DefaultRootWindow(dpy);
	XFixesQueryExtension(dpy, &ev_base, &err_base);
	XFixesSelectSelectionInput(dpy, root, XA_PRIMARY, XFixesSetSelectionOwnerNotifyMask);
	while (!XNextEvent(dpy, &ev))
		if (ev.type == ev_base + XFixesSelectionNotify)
			XSetSelectionOwner(dpy, XA_PRIMARY, None, CurrentTime);
	return 0;
}

Compile with `gcc -o nosel <filename.c> -lX11 -lXfixes` where "filename.c" is whatever you named the file above.  Then run "nosel" in the background.  It will do nothing until some program claims the primary selection, then it will clear the selection owner.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#8 2019-09-05 18:35:46

M5Hc6UsQ
Member
Registered: 2018-02-26
Posts: 33

Re: [SOLVED] How to disable the middle mouse paste behavior?

Just tried your solution. The CPU usage goes up to 100% if I select some text while the tool is running.

Last edited by M5Hc6UsQ (2019-09-05 18:36:00)

Offline

#9 2019-09-05 18:46:04

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 72,286

Re: [SOLVED] How to disable the middle mouse paste behavior?

Hotfix

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xfixes.h>

int main() {
	Display *dpy;
	Window root;
	int ev_base, err_base;
	XEvent ev;
	if (!(dpy=XOpenDisplay(0x0)))
		return 1;
	root = DefaultRootWindow(dpy);
	XFixesQueryExtension(dpy, &ev_base, &err_base);
	XFixesSelectSelectionInput(dpy, root, XA_PRIMARY, XFixesSetSelectionOwnerNotifyMask);
    int ignore = 0;
	while (!XNextEvent(dpy, &ev)) {
		if (ev.type == ev_base + XFixesSelectionNotify) {
            if (ignore) {
                ignore = 0;
            } else {
                ignore = 1;
                XSetSelectionOwner(dpy, XA_PRIMARY, None, CurrentTime);
            }
        }
    }
	return 0;
}

Offline

#10 2019-09-05 19:09:19

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,446
Website

Re: [SOLVED] How to disable the middle mouse paste behavior?

I was wondering if that would happen, for some reason it worked fine for me.  A potentially cleaner solution would be to add an `XSync(dpy, True);` after the XSetSelectionOwner call.  I can't test whether this is sufficient, though, as I don't experience the race condition that it would fix.

edit: Flush -> Sync

Last edited by Trilby (2019-09-06 17:53:24)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#11 2019-09-05 19:22:03

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 72,286

Offline

#12 2019-09-06 17:14:28

M5Hc6UsQ
Member
Registered: 2018-02-26
Posts: 33

Re: [SOLVED] How to disable the middle mouse paste behavior?

@ Trilby
I guess you mean `XFlush(dpy)`, the function only takes one parameter. But there was no difference. CPU usage still goes high.

The fix from @seth works, but unfortunately I'm not able to select any text while the tool is running.

Offline

#13 2019-09-06 17:54:56

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,446
Website

Re: [SOLVED] How to disable the middle mouse paste behavior?

Sorry, I meant XSync, but accidentally typed the wrong one.

But yes, as noted in post #2, this would prevent any use of the primary selection.  If you don't want the middle click paste, do you use shift+insert in some programs, or how do you want to use the primary selection?


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#14 2019-09-06 20:54:05

M5Hc6UsQ
Member
Registered: 2018-02-26
Posts: 33

Re: [SOLVED] How to disable the middle mouse paste behavior?

I just want to disable the middle click paste. I still want to be able to select text to use it with the clipboard for example.

Offline

#15 2019-09-06 22:52:27

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,446
Website

Re: [SOLVED] How to disable the middle mouse paste behavior?

Running the above code would prevent any use of the primary selection.  But the clipboard selection will still work just fine.  I just ran 'nosel' and indeed cannot get anything into the primary selection (after selecting text `xsel` returns an empty string and middle-click does not paste anything) but I can still select text and <Ctrl>+C and <Ctrl>+V in programs that support it; further after a select and <Ctrl>+C `xsel -b` does return the selected text even with 'nosel' running.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#16 2019-09-07 06:02:10

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 72,286

Re: [SOLVED] How to disable the middle mouse paste behavior?

I assume what he means is that some toolkit just aborts/restarts the selection process when the primary buffer is taken and it's likely also weird enough to claim the buffer on motion while the button is still down?

Offline

#17 2019-09-07 08:14:33

M5Hc6UsQ
Member
Registered: 2018-02-26
Posts: 33

Re: [SOLVED] How to disable the middle mouse paste behavior?

Trilby wrote:

[..] but I can still select text and <Ctrl>+C and <Ctrl>+V in programs that support it [...]

Doesn't work for me. The selection is either reset after releasing the mouse button or instantly while I'm still selecting text depending on the application.

I guess I'll stick with the `xbindkeys` solution everything else seems to be trial and error. Thanks for your help!

Last edited by M5Hc6UsQ (2019-09-07 08:16:55)

Offline

#18 2019-09-07 12:35:54

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,446
Website

Re: [SOLVED] How to disable the middle mouse paste behavior?

seth wrote:

... some toolkit ...

Perhaps.  Yet another reason for me to avoid that toolkit.  Sorry it didn't work for you - it works at least within qutebrowser which uses qt5.  That's the only toolkit-using program I have.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#19 2019-09-07 18:20:05

M5Hc6UsQ
Member
Registered: 2018-02-26
Posts: 33

Re: [SOLVED] How to disable the middle mouse paste behavior?

I've tested it with xfce4-terminal and mousepad.

Offline

Board footer

Powered by FluxBB