OSX - How to tell if cursor is visible - macos

I understand that this question has already been answered:
Detect if cursor is hidden on Mac OS X
But at this point, the function "CGCursorIsVisible" has been deprecated. According to the help literature, there is no replacement.
https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/Quartz_Services_Ref/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/c/func/CGCursorIsVisible
When using the hide and show functions (CGDisplayHideCursor / CGDisplayShowCursor), no reference to the cursor's visibility is returned. Although a show / hide counter is maintained by quartz, there's no way of accessing that value.
So, how do we find out if the cursor is visible or not?

I ran into the very same problem and could not find a valid answer to this problem.
I solved it by implementing a counter which is set to 0 when my application starts. I increase it everytime I hide the cursor and then I can unhide while the _counter != 0.
Your Mac seems to have an own counter for each application... So if your application is not in foreground and it uses the [NSCursor hide] command, it will still be visible but become invisible, when your app becomes keyWindow again.

I know it is a little bit late, but I recently stumbled upon a similar issue. One way to solve this problem is to push an empty cursor onto the cursor stack instead of calling NSCursor.hide() like this:
let myCursor = NSCursor.init(image: NSImage.init(size: NSSize(width: 1, height: 1)), hotSpot: NSPoint(x: 0, y: 0))
myCursor.set()
At a later point in time you can then just push the normal arrow cursor with NSCursor.arrow.set() or NScursor.arrow.push().
This way you don't have the issue of keeping track of the hide/unhide count.
Theoretically you should also be able to check the reference of NSCursor.current against myCursor and see if you still have the invisible cursor (I have not checked this though).

Related

PTVISIBLE always returns true when relevant point obscured by another window

I am trying to ascertain whether the mouse cursor is over a specific window and whether there are any other windows obscuring that window at that specific point. The relevant point is obtained in screen coordinates by way of a mouse hook. I then use the ptVisible function to determine this. My code is:
DC := GetDC(wnd);
try
Result := PtVisible(DC, pt.X, pt.Y);
finally
ReleaseDC(wnd, DC);
end;
This always returns false even when there is nothing obscuring the window represented by the wnd handle.
I found very little on the web as to proper use of ptVisible. Can anyone advise if I am using it incorrectly?
You are not using it correctly. From the docs page, PtVisible() "determines whether the specified point is within the clipping region of a device context." This has nothing to do with mouse location or location of a pixel within a window. This has specific uses to check whether the point is within the clipping region of a graphics device context.
If you want to check whether the mouse is within a certain window, you might want to try checking if your mouse coordinate is within GetWindowRect(). And then to check whether or not any windows overlap, you'd want to EnumWindows() and then IntersectRect()

How to detect current system cursor on OSX?

Is there a way to know what the current system cursor on mac/ OSX is ? I know we can get current cursor using [NSCursor currentSystemCursor] call. But this does not tell us what the current cursor is. The check
if ([[NSCursor currentSystemCursor] isEqual:[NSCursor arrowCursor]]) always fails. Even the check if ([[NSCursor currentSystemCursor] isEqual:[NSCursor currentSystemCursor]]) always returns zero.
I have gone through similar question How to get current type of mouse cursor in Mac OS X? but did not find any way of doing it there.
Any help on this is appreciated. Thanks in advance.
Unfortunately not.
What you can do is get the cursor image and the compare that image to the image of a known cursor (arrow, I-beam, etc).
However many apps supply slight variations of each cursor so your check can never be exhaustive unless you keep updating the app to know about more and more cursor variation.
You can even build an algorithm to analyse the cursor image to heuristically categorise it.
It sounds hacky, I know. But it does work. Kinda.
I was able to get this to work using double equals.
ex:
[NSCursor currentCursor] == [NSCursor arrowCursor]
returns the result I'm expecting. It's annoying that you can't just query the type but at least this worked to set conditional breakpoints and stuff like that.
Hope that helps.

SDL2 relative mouse mode reporting motion when mouse has not been moved

I'm new to SDL2, so pardon any ignorance, but I am experiencing strange results when using relative mouse mode in SDL2.0.3. When I do SDL_SetRelativeMouseMode(SDL_bool::SDL_true), the cursor is hidden as expected. Inside the event loop, I check for windowEvent.type == SDL_MOUSEMOTION and then use windowEvent.motion.xrel/yrel xrel and yrel report values from -4 to 4 when the mouse is not even moving! Furthermore, actually moving my mouse does not seem to correlate whatsoever to the xrel and yrel's being reported.
Should I be doing this differently?
I have the exact same problem on my computer, but using Uint32 SDL_GetRelativeMouseState(int* x,int* y) instead works fine.

Cocoa: How to send proper mouse delta events?

I am making an application to control the mouse using an analog stick from a gamepad for Mac OS X (10.7.3).
Currently it is working pretty well, I can control the cursor in desktop and most games. But in Team Fortress 2, I cant control the aim, but can control the cursor in the menu. Mouse wheel and clicks works everywhere.
Another strange thing is that when I move the real mouse, the aim "jumps" the traveled distance from my "virtual mouse" before aiming normally, so it somewhat is receiving the events.
The game option "Raw mouse input" is disabled (I think it is not even supported in osx). And a similar application can controle the aim successfully.
I suspect the game is looking for "delta movement" or "relative movement" events or anything similar, but my code sets the position of the cursor using absolute positions. Not sute how to change this.
Here is the snippet of code used to send mouseMoved events:
EDIT: Crappy code removed!
.
EDIT:
Also, because I did this way, I need to check the screen bounds manually to prevent the cursor going Crazy. So in multi-screen setups, and when the user change the resolution, it gets worst. Would be so much better if I can just send the amount of movement and let the OS take care of constraining the cursor.
.
The question is:
I am doing the mouse move events the wrong way?
How can I fix this?
EDIT2:
So, that was just a stupid bug, sorry =P
I just forgot to call CGSetIntegerValueField(kCGEventMouseDeltaX, ...) (and Y) with the delta values... =P
Finally got this working, after just a few short hours of Googling and experimenting :) Looks like Rodrigo was trying to say that you have to call CGEventSetIntegerValueField on kCGMouseEventDeltaX and kCGMouseEventDeltaY on the mouse event after constructing it.
My code, using PyObjC:
from Quartz.CoreGraphics import (
CGEventCreate,
CGEventCreateMouseEvent,
CGEventGetLocation,
CGEventPost,
CGEventSetIntegerValueField,
kCGEventMouseMoved,
kCGHIDEventTap,
kCGMouseEventDeltaX,
kCGMouseEventDeltaY,
)
def mouse_delta(delta_x, delta_y):
"""
Send a MouseMoved event with the given deltaX and deltaY attributes,
but without changing the cursor location.
"""
# Might be a better way to get the current mouse location
x, y = CGEventGetLocation(CGEventCreate(None))
# The `mouseButton` parameter to CGEventCreateMouseEvent is ignored
# unless `mouseType` is kCGEventOtherMouse{Down,Dragged,Up},
# so let's just pass the value 0
event = CGEventCreateMouseEvent(None, kCGEventMouseMoved, (x, y), 0)
CGEventSetIntegerValueField(event, kCGMouseEventDeltaX, delta_x)
CGEventSetIntegerValueField(event, kCGMouseEventDeltaY, delta_y)
return CGEventPost(kCGHIDEventTap, event)

How to create an invisible X11 window for GPGPU?

Is it possible to create an invisible X window? For initialization of an OpenGL ES 2.0 context, one has to create a X window manually, but I can't find a way to make it invisible. Since I'm only doing GPGPU I don't need an output window. In fact, it is rather annoying in my case.
I'm aware of a solution from an earlier question, where it has been pointed out to use InputOnly in XCreateWindow(). This, however, leads to the X error GLXBadDrawable. Probably because EGL requires the window to respond to graphics request. Is there another way? Maybe create it minimized? But I can't find anything on that either. Also setting the window's size really small doesn't help, since it always occupies the whole screen on my device (Nokia N9).
When you create an X window, it is created unmapped, so what about creating an InputOutput window and leaving it unmapped? Another option would be (if the window must stay mapped), to move it out of the screen.

Resources