You are not logged in.

#1 2020-07-16 20:31:40

qurn
Member
Registered: 2017-10-13
Posts: 21

[closed] Change cursor style instantly for all applications

Is there a way to change the cursor style while the WM and applications keep running?

I want to change my cursor to indicate the state my QMK keyboard is currently in.
My keyboard would send a key combination that executes the command to change the cursor.

This could be a change from white to black theme or a change from left_ptr to right_ptr.
I am running DWM via a .xinitrc.

Last edited by qurn (2020-11-12 22:34:13)

Offline

#2 2020-10-04 02:49:10

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

Re: [closed] Change cursor style instantly for all applications

Use xsetroot, or (from DWM's C code) XDefineCursor.  I have no idea what all that cursor grabbing code is for that you put in the dwm thread - that has nothing to do with changing the cursor that is displayed.  If you just want to change / invert the colors, xsetroot can do this as well, or from DWM's code use either XSetForeground and XSetBackground, or XChangeWindowAttributes for setting both colors at once.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2020-10-04 03:15:10

solskog
Member
Registered: 2020-09-05
Posts: 407

Re: [closed] Change cursor style instantly for all applications

I would combine a keybinding with an external shell script. That way I don't have to recompile DWM too often.

Offline

#4 2020-10-22 20:27:13

qurn
Member
Registered: 2017-10-13
Posts: 21

Re: [closed] Change cursor style instantly for all applications

I'm sorry to say I just can't get it to work, because some second/third argument is wrong or some code around it (I tried XDefineCursor, XSetBackground, XChangeWindowAttributes, and ofc. search engines for solutions).

Eg. xsetroot -cursor XC_left_ptr does not work instantly on all windows but only on the new spawned ones and the background. The idea is to have something open (firefox) press something and the cursor changes there.

I don't mind recompiling DWM at all, but would also be open for a script solution.

Offline

#5 2020-10-22 20:34:08

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

Re: [closed] Change cursor style instantly for all applications

qurn wrote:

I'm sorry to say I just can't get it to work, because some second/third argument is wrong or some code around it (I tried XDefineCursor, XSetBackground, XChangeWindowAttributes ...

Post the actual code you've used if you want help troubleshooting it.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#6 2020-10-22 21:06:21

qurn
Member
Registered: 2017-10-13
Posts: 21

Re: [closed] Change cursor style instantly for all applications

Lets try the XChangeWindowAttributes attempt:

void
point_left(void) {
	cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr); // normal is now left
	XSetWindowAttributes wa;
	wa.cursor = cursor[CurNormal]->cursor;
	XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa); // bitmask as used in setup(void)
}

and right similar
Compiles without warning but crashes on execution.

Offline

#7 2020-10-22 21:09:16

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

Re: [closed] Change cursor style instantly for all applications

Ah, you're trying to do it within dwm.  Assuming all else is in order, one thing that stands out is that you are changing the EventMask of the root window which would likely have the effect of making dwm ignore all window events.  Remove CWEventMask.

Note, I'm also not familiar with dwm's cursor array, so perhaps there are other issues there.

Last edited by Trilby (2020-10-22 21:10:13)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#8 2020-10-22 21:19:04

qurn
Member
Registered: 2017-10-13
Posts: 21

Re: [closed] Change cursor style instantly for all applications

ok that works for the root window

Offline

#9 2020-10-22 21:41:49

qurn
Member
Registered: 2017-10-13
Posts: 21

Re: [closed] Change cursor style instantly for all applications

That leaves the other open windows and what i dont understand is that new spawned windows dont get the new fliped pointer. I defined cursor[CurNormal] = drw_cur_create(drw, XC_right_ptr);

Offline

#10 2020-10-22 22:54:24

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

Re: [closed] Change cursor style instantly for all applications

When windows are initially mapped they inherit the cursor pointer from the root window (if they don't explicitly set it themselves).  I've never really messed with cursors much at run time to see the distinction you are noting that apparently it is inherited effectively by value rather than by reference (so changes to the root window don't propgate to previously mapped windows).

So if you do need to change it, you will need to change it on the root window and all currently mapped top-level windows.  Luckily, dwm already keeps a list of these, so just add a loop to your code that loops through the list of windows (it used to be "clients", now I think it's m->clients), and run the same XChangeWindowAttributes on each one.  E.g.,:

...
	wa.cursor = cursor[CurNormal]->cursor;
	XChangeWindowAttributes(dpy, root, CWCursor, &wa);
	for (c = m->clients; c; c = c->next)
		XChangeWindowAttributes(dpy, c->win, CWCursor, &wa);
}

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#11 2020-10-22 23:20:39

robson75
Member
From: Poland
Registered: 2020-06-28
Posts: 144

Re: [closed] Change cursor style instantly for all applications

If you want the cursor theme to be displayed throughout the system, you must enter /usr/share/icons/default and edit the index.theme file by renaming the cursor theme. For example, I have this entry

[icon theme]
Inherits=Oxygen_Gray

Arch Linux Xfce - 64Bit Linux User #621110

Offline

#12 2020-10-23 05:37:56

qurn
Member
Registered: 2017-10-13
Posts: 21

Re: [closed] Change cursor style instantly for all applications

Almost there! I added update for the status bar:

 
void
point_right(void) {
	Client *c;
	Monitor *m;
	XSetWindowAttributes wa;
	cursor[CurNormal] = drw_cur_create(drw, XC_right_ptr);
	wa.cursor = cursor[CurNormal]->cursor;
	XChangeWindowAttributes(dpy, root, CWCursor, &wa);
	for (m = mons; m; m = m->next) {
		for (c = m->clients; c; c = c->next)
			XChangeWindowAttributes(dpy, c->win, CWCursor, &wa);
		XChangeWindowAttributes(dpy, m->barwin, CWCursor, &wa);
	}
}

So some applications behave a bit different now:

nemo, alacritty - work flawless

firefox - cursor change happens only when window is focused while execution

qutebrowser, kolourpaint - starts at spawn with the left cursor

robson75: the cursors are selected all within one theme

Offline

#13 2020-10-24 20:52:14

qurn
Member
Registered: 2017-10-13
Posts: 21

Re: [closed] Change cursor style instantly for all applications

I put the code to assign the new normal pointer to focus() and now more cases are covered.

void
focus(Client *c)
{
	if (!c || !ISVISIBLE(c))
		for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
	if (selmon->sel && selmon->sel != c)
		unfocus(selmon->sel, 0);
	if (c) {
		if (c->mon != selmon)
			selmon = c->mon;
		if (c->isurgent)
			seturgent(c, 0);
		detachstack(c);
		attachstack(c);
		grabbuttons(c, 1);
		XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColBorder].pixel);
		setfocus(c);
		// code for pointer update
		XSetWindowAttributes wa;
		wa.cursor = cursor[CurNormal]->cursor;
		XChangeWindowAttributes(dpy, c->win, CWCursor, &wa);
		// code for pointer update end
	} else {
		XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
		XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
	}
	selmon->sel = c;
	drawbars();
}

What remains is firefox and new started terminal application, like vim.
I also noticed that vim and firefox even start with a pointer pointing left when the right one is assigned at setup().

I think now I need to find one proper spot to insert this code again to cover the last remaining cases.

Offline

#14 2020-11-12 22:33:30

qurn
Member
Registered: 2017-10-13
Posts: 21

Re: [closed] Change cursor style instantly for all applications

I think those applications ask specifically for the left pointing cursor instead of the default. I given up on this and made a c program that indicates the state with a colored circle.

https://gitlab.com/qurn/cursor_indicator

Offline

#15 2020-11-12 22:40:01

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: [closed] Change cursor style instantly for all applications

Please edit your first post to use [SOLVED] instead of [CLOSED]...
https://wiki.archlinux.org/index.php/Co … ow_to_post


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#16 2020-11-12 23:38:41

qurn
Member
Registered: 2017-10-13
Posts: 21

Re: [closed] Change cursor style instantly for all applications

But its not solved.. I've resigned on the described topic. I didn't use solved, to not mislead others looking for this.
Should I still mark it as Solved?

Offline

#17 2020-11-13 00:12:59

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

Re: [closed] Change cursor style instantly for all applications

Interesting code - I'd strongly suggest you take all of the window / gc creation and display connection out of the loop - it will be *much* smoother.  Just create the window with the content you want and have an inner loop with just the following:

        XSelectInput(dpy, root, PointerMotionMask);
        XEvent ev;
        XMotionEvent *e;
        while (!XNextEvent(dpy, &ev)) {
                if (ev.type != MotionNotify) continue;
                e = &ev.xmotion;
                XMoveWindow(dpy, win, e->x, e->y);
                XRaiseWindow(dpy, win);
        }

Also note that the NET_WM stuff will not work with dwm.

Last edited by Trilby (2020-11-13 00:17:46)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

Board footer

Powered by FluxBB