Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

cannot initialize a variable of type 'CFDictionaryRef'

+3
−1

Background story: I'm on mac OS 11.6 and would like to access an identifier for the current space/desktop from the command line. I found a helpful article at https://ianyh.com/blog/identifying-spaces-in-mac-os-x/ which describes a strategy to compare the output of defaults read com.apple.spaces with the list of windows on the current space. I don't really know how to puzzle together all the code fragments from this site, so I kept searching and found this implementation: https://gist.github.com/camillol/1129406 :

#include <unistd.h>
#include <CoreServices/CoreServices.h>
#include <ApplicationServices/ApplicationServices.h>

typedef int CGSConnection;
extern OSStatus CGSGetWorkspace(const CGSConnection cid, int *workspace);
extern OSStatus CGSSetWorkspace(const CGSConnection cid, int workspace);
extern CGSConnection _CGSDefaultConnection(void);

int get_space_id(void);
void set_space_by_index(int space);

int get_space_id(void)
{
    int space;
    CFArrayRef windows = CGWindowListCopyWindowInfo( kCGWindowListOptionOnScreenOnly, kCGNullWindowID );
    CFIndex i, n;
    
    for (i = 0, n = CFArrayGetCount(windows); i < n; i++) {
        CFDictionaryRef windict = CFArrayGetValueAtIndex(windows, i);
        CFNumberRef spacenum = CFDictionaryGetValue(windict, kCGWindowWorkspace);
        if (spacenum) {
            CFNumberGetValue(spacenum,  kCFNumberIntType, &space);
            return space;
        }
    }
    return -1;
}

void set_space_by_index(int space)
{
    CFNotificationCenterRef nc = CFNotificationCenterGetDistributedCenter();
    CFStringRef numstr = CFStringCreateWithFormat(NULL, nil, CFSTR("%d"), space);
    CFNotificationCenterPostNotification(nc, CFSTR("com.apple.switchSpaces"), numstr, NULL, TRUE);
}

int main (int argc, char * const argv[])
{
    int useCGS = 0, ch, space;
    
    while ((ch = getopt(argc, argv, "-c")) != -1) {
        switch (ch) {
            case 'c':
                useCGS = 1;
                break;
        }
    }
    argc -= optind;
    argv += optind;
    
    if (argc > 0) {
        space = atoi(argv[0]);
        if (useCGS) CGSSetWorkspace(_CGSDefaultConnection(), space);
        else set_space_by_index(space);
    } else {
        if (useCGS) CGSGetWorkspace(_CGSDefaultConnection(), &space);
        else space = get_space_id();
        printf("%d\n", space);
    }
    
    return 0;
}

I tried to compile it with clang++ spaceutil.c, but get the following error:

clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated [-Wdeprecated]
spaceutil.c:20:25: error: cannot initialize a variable of type 'CFDictionaryRef' (aka 'const __CFDictionary *') with an rvalue of type 'const void *'
        CFDictionaryRef windict = CFArrayGetValueAtIndex(windows, i);
                        ^         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
spaceutil.c:21:21: error: cannot initialize a variable of type 'CFNumberRef' (aka 'const __CFNumber *') with an rvalue of type 'const void *'
        CFNumberRef spacenum = CFDictionaryGetValue(windict, kCGWindowWorkspace);
                    ^          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
spaceutil.c:21:62: warning: 'kCGWindowWorkspace' is deprecated: first deprecated in macOS 10.8 - No longer supported [-Wdeprecated-declarations]
        CFNumberRef spacenum = CFDictionaryGetValue(windict, kCGWindowWorkspace);
                                                             ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/CoreGraphics.framework/Headers/CGWindow.h:106:30: note: 'kCGWindowWorkspace' has been explicitly marked deprecated here
CG_EXTERN const CFStringRef  kCGWindowWorkspace
                             ^
1 warning and 2 errors generated.

My clang++ version is

clang++ --version
Apple clang version 13.0.0 (clang-1300.0.29.30)
Target: arm64-apple-darwin20.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Any pointers how to fix this error?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

1 answer

+1
−0

Instead of reinventing the wheel, I would use SpaceInfo, a nice little command line tool to retrieve the current space number.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »