Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Globally hiding cursor (from background app)

I want to hide the cursor from a statusbar app and i've done some research. It seems as though the solution to this problem was found a while ago:

Globally hide mouse cursor in Cocoa/Carbon? or http://lists.apple.com/archives/carbon-dev/2006/Jan/msg00555.html

But the code that is referred to will not compile. Do any of you guys know either how to make the code compile (by importing some old API or something) or another way of achieving this (some kind of hack)?

(I know it is generally a bad idea to hide the cursor from a background app, but i making an app where this functionality is pretty essential)

Edit:

Here's the old hack, that doesn't work anymore.

long sysVers = GetSystemVersion();

// This trick doesn't work on 10.1 
if (sysVers >= 0x1020)
{
    void CGSSetConnectionProperty(int, int, int, int);
    int CGSCreateCString(char *);
    int CGSCreateBoolean(BOOL);
    int _CGSDefaultConnection();
    void CGSReleaseObj(int);
    int propertyString, boolVal;

    // Hack to make background cursor setting work
    propertyString = CGSCreateCString("SetsCursorInBackground");
    boolVal = CGSCreateBoolean(TRUE);
    CGSSetConnectionProperty(_CGSDefaultConnection(), _CGSDefaultConnection(), propertyString, boolVal);
    CGSReleaseObj(propertyString);
    CGSReleaseObj(boolVal);
}

It gives me 4 errors:

"_CGSCreateBoolean", referenced from: -[MyClass myMethod] in MyClass.o

"_GetSystemVersion", referenced from: -[MyClass myMethod] in MyClass.o

"_CGSCreateCString", referenced from: -[MyClass myMethod] in MyClass.o

"_CGSReleaseObj", referenced from: -[MyClass myMethod] in MyClass.o

like image 362
Sorig Avatar asked Oct 07 '10 21:10

Sorig


1 Answers

You need to link against the Application Services framework to get rid of the linker errors.

Here's a complete example of the hack (updated to use Core Foundation):

cat >t.c<<EOF
#include <ApplicationServices/ApplicationServices.h>

int main(void)
{
    void CGSSetConnectionProperty(int, int, CFStringRef, CFBooleanRef);
    int _CGSDefaultConnection();
    CFStringRef propertyString;

    // Hack to make background cursor setting work
    propertyString = CFStringCreateWithCString(NULL, "SetsCursorInBackground", kCFStringEncodingUTF8);
    CGSSetConnectionProperty(_CGSDefaultConnection(), _CGSDefaultConnection(), propertyString, kCFBooleanTrue);
    CFRelease(propertyString);
    // Hide the cursor and wait
    CGDisplayHideCursor(kCGDirectMainDisplay);
    pause();
    return 0;
}
EOF
gcc -framework ApplicationServices t.c
./a.out

On Mac OS 10.5 this hides the cursor until the program is interrupted. However, performing any window server or dock tasks shows the cursor.

like image 111
ejsherry Avatar answered Sep 19 '22 00:09

ejsherry