### Create and Configure CGS Windows in Objective-C Source: https://context7.com/nuikit/cgsinternal/llms.txt Provides examples for creating, configuring, and manipulating CGS windows. This includes setting window bounds, title, transparency, level, tags, shadow properties, and applying transformations. Proper release of window resources is essential. ```c #include "CGSInternal.h" CGSConnectionID cid = CGSMainConnectionID(); // Create a region for the window bounds CGRect bounds = CGRectMake(0, 0, 800, 600); CGSRegionRef region; CGSNewRegionWithRect(&bounds, ®ion); // Create a new window CGWindowID windowID; CGError error = CGSNewWindow(cid, kCGSBackingBuffered, 100, 100, region, &windowID); if (error == kCGErrorSuccess) { // Set the window title (used by Spaces) CGSSetWindowTitle(cid, windowID, CFSTR("My Window")); // Set window transparency CGSSetWindowAlpha(cid, windowID, 0.9f); // Set window level (floating, normal, etc.) CGSSetWindowLevel(cid, windowID, kCGFloatingWindowLevel); // Configure window tags for special behavior CGSWindowTagBit tags[2] = { kCGSDisableShadowTagBit | kCGSOnAllWorkspacesTagBit, 0 }; CGSSetWindowTags(cid, windowID, tags, kCGSRealMaximumTagSize); // Make the window opaque or transparent CGSSetWindowOpacity(cid, windowID, false); // transparent background // Set window background color (for autofill) CGSSetWindowAutofillColor(cid, windowID, 0.2f, 0.2f, 0.2f); CGSSetWindowAutofill(cid, windowID, true); // Apply an affine transform to the window CGAffineTransform transform = CGAffineTransformMakeScale(0.9, 0.9); CGSSetWindowTransform(cid, windowID, transform); // Configure window shadow CGSSetWindowShadowParameters(cid, windowID, 10.0f, 0.5f, 0, 10); // Order the window on screen CGSOrderWindow(cid, windowID, kCGSOrderAbove, 0); // Move the window CGPoint newOrigin = CGPointMake(200, 200); CGSMoveWindow(cid, windowID, &newOrigin); // Get window's screen rect CGRect screenRect; CGSGetScreenRectForWindow(cid, windowID, &screenRect); printf("Window at: %.0f, %.0f (%.0fx%.0f)\n", screenRect.origin.x, screenRect.origin.y, screenRect.size.width, screenRect.size.height); // Release the window when done CGSReleaseWindow(cid, windowID); } CGSReleaseRegion(region); ``` -------------------------------- ### Configuring Display Accessibility Features Source: https://context7.com/nuikit/cgsinternal/llms.txt Provides examples for querying and modifying display accessibility settings including zoom status, color inversion, grayscale mode, and display contrast levels. ```c #include "CGSInternal.h" CGSConnectionID cid = CGSMainConnectionID(); bool isZoomed; CGSIsZoomed(cid, &isZoomed); bool inverted = CGDisplayUsesInvertedPolarity(); CGDisplaySetInvertedPolarity(!inverted); CGDisplayForceToGray(true); CGSSetDisplayContrast(1.5f); ``` -------------------------------- ### Perform Geometric Region Operations Source: https://context7.com/nuikit/cgsinternal/llms.txt Provides examples for creating, modifying, and querying geometric regions. These operations include boolean logic like union, difference, and XOR, as well as point-in-region testing. ```c #include "CGSInternal.h" CGRect rect = CGRectMake(0, 0, 100, 100); CGSRegionRef region; CGSNewRegionWithRect(&rect, ®ion); CGSRegionRef unionRegion; CGSUnionRegion(region, multiRegion, &unionRegion); bool pointInRegion = CGSPointInRegion(region, &testPoint); CGSReleaseRegion(region); ``` -------------------------------- ### Manage macOS Spaces and Windows with CGSSpace.h Source: https://context7.com/nuikit/cgsinternal/llms.txt This C code snippet demonstrates various functionalities for managing macOS virtual desktops (Spaces) and windows. It covers retrieving active and all spaces, getting space types and names, creating new spaces, setting space names, associating windows with spaces, adding/removing windows from spaces, checking space management modes, switching spaces, and destroying spaces. It requires the CGSInternal.h header and interacts with the CoreGraphics framework. ```c #include "CGSInternal.h" CGSConnectionID cid = CGSMainConnectionID(); // Get the currently active space CGSSpaceID activeSpace = CGSGetActiveSpace(cid); printf("Active space ID: %zu\n", activeSpace); // Get all spaces CFArrayRef allSpaces = CGSCopySpaces(cid, kCGSAllSpacesMask); CFIndex spaceCount = CFArrayGetCount(allSpaces); printf("Total spaces: %ld\n", spaceCount); for (CFIndex i = 0; i < spaceCount; i++) { CFNumberRef spaceNumber = CFArrayGetValueAtIndex(allSpaces, i); CGSSpaceID sid; CFNumberGetValue(spaceNumber, kCFNumberLongType, &sid); // Get space type CGSSpaceType type = CGSSpaceGetType(cid, sid); const char *typeName = (type == CGSSpaceTypeUser) ? "User" : (type == CGSSpaceTypeFullscreen) ? "Fullscreen" : "System"; // Get space name CFStringRef name = CGSSpaceCopyName(cid, sid); printf("Space %zu: type=%s, name=%s\n", sid, typeName, name ? CFStringGetCStringPtr(name, kCFStringEncodingUTF8) : "(null)"); if (name) CFRelease(name); } CFRelease(allSpaces); // Create a new space CFMutableDictionaryRef options = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFNumberRef typeNum = CFNumberCreate(NULL, kCFNumberIntType, &(int){CGSSpaceTypeUser}); CFDictionarySetValue(options, CFSTR("type"), typeNum); CGSSpaceID newSpace = CGSSpaceCreate(cid, NULL, options); CFRelease(typeNum); CFRelease(options); // Set space name CGSSpaceSetName(cid, newSpace, CFSTR("My Custom Space")); // Get spaces containing specific windows CFMutableArrayRef windowIDs = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); CFNumberRef winNum = CFNumberCreate(NULL, kCFNumberIntType, &(int){12345}); CFArrayAppendValue(windowIDs, winNum); CFArrayRef spacesForWindows = CGSCopySpacesForWindows(cid, kCGSAllSpacesMask, windowIDs); CFRelease(winNum); CFRelease(windowIDs); // Add windows to a space CFMutableArrayRef windows = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); CFMutableArrayRef spaces = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); // ... populate arrays ... CGSAddWindowsToSpaces(cid, windows, spaces); // Remove windows from a space CGSRemoveWindowsFromSpaces(cid, windows, spaces); CFRelease(windows); CFRelease(spaces); // Get space management mode (separate spaces per display) CGSSpaceManagementMode mode = CGSGetSpaceManagementMode(cid); printf("Space management: %s\n", mode == kCGSPackagesSpaceManagementModePerDesktop ? "Per Desktop" : "Single"); // Switch to a different space CGSManagedDisplaySetCurrentSpace(cid, kCGSPackagesMainDisplayIdentifier, newSpace); // Destroy the space when done CGSSpaceDestroy(cid, newSpace); ``` -------------------------------- ### Control Cursor Appearance and Position with CGSCursor.h Source: https://context7.com/nuikit/cgsinternal/llms.txt Programmatically control cursor appearance, position, and register custom cursors using the CGSCursor.h internal API. This includes getting cursor location, moving the cursor, hiding/showing it, adjusting scale for accessibility, and registering custom cursor images. ```Objective-C #include "CGSInternal.h" CGSConnectionID cid = CGSMainConnectionID(); // Get current cursor location pot cursorPos; CGSGetCurrentCursorLocation(cid, &cursorPos); printf("Cursor at: %.0f, %.0f\n", cursorPos.x, cursorPos.y); // Move the cursor to a new position CGSWarpCursorPosition(cid, 500.0f, 500.0f); // Hide/show cursor CGSHideCursor(cid); // ... do something ... CGSShowCursor(cid); // Temporarily hide cursor until mouse moves CGSObscureCursor(cid); // Get/set cursor scale (for accessibility) CGFloat scale; CGSGetCursorScale(cid, &scale); printf("Current cursor scale: %.1f\n", scale); // Make cursor larger (max 4.0 via System Preferences) CGSSetCursorScale(cid, 2.0f); // Get the current cursor seed (changes when cursor updates) int seed = CGSCurrentCursorSeed(); printf("Cursor seed: %d\n", seed); // Register a custom cursor CGImageRef cursorImage = /* create or load image */; CFMutableArrayRef imageArray = CFArrayCreateMutable(NULL, 1, &kCFTypeArrayCallBacks); CFArrayAppendValue(imageArray, cursorImage); int cursorSeed; CGError error = CGSRegisterCursorWithImages( cid, "com.myapp.customcursor", // cursor name true, // set globally true, // instantly 1, // frame count imageArray, // images CGSizeMake(16, 16), // cursor size CGPointMake(0, 0), // hotspot &cursorSeed, // output seed CGRectMake(0, 0, 16, 16), // bounds 0.0, // frame duration (for animated) 0 // repeat count ); CFRelease(imageArray); // Get registered cursor properties CGSize imageSize; CGPoint hotSpot; NSUInteger frameCount; CGFloat frameDuration; CFArrayRef images; CGSCopyRegisteredCursorImages(cid, "com.myapp.customcursor", &imageSize, &hotSpot, &frameCount, &frameDuration, &images); // Force the wait cursor (spinning beach ball) - use with caution! // CGSForceWaitCursorActive(cid, true); // CGSForceWaitCursorActive(cid, false); ``` -------------------------------- ### Implement Window Transitions with CGS Source: https://context7.com/nuikit/cgsinternal/llms.txt Demonstrates how to define and execute visual window transitions like cube rotations, fades, and slides. It requires a valid CGS connection and window ID to trigger the animation. ```c #include "CGSInternal.h" CGSConnectionID cid = CGSMainConnectionID(); CGWindowID windowID = /* your window */; CGSTransitionSpec spec; memset(&spec, 0, sizeof(spec)); spec.version = 0; spec.type = kCGSTransitionCube; spec.options = kCGSTransitionDirectionRight | kCGSTransitionFlagTransparent; spec.wid = windowID; spec.backColor = NULL; CGSTransitionID transition; CGError error = CGSNewTransition(cid, &spec, &transition); if (error == kCGErrorSuccess) { CGSInvokeTransition(cid, transition, 0.5); CGSReleaseTransition(cid, transition); } ``` -------------------------------- ### Query and Manipulate Windows using CGSInternal Source: https://context7.com/nuikit/cgsinternal/llms.txt Demonstrates how to retrieve a list of windows for a connection, query individual window properties such as level, alpha, and bounds, and perform batch operations like alpha animation on on-screen windows. ```c #include "CGSInternal.h" CGSConnectionID cid = CGSMainConnectionID(); int windowCount; CGSGetWindowCount(cid, cid, &windowCount); printf("Total windows: %d\n", windowCount); CGWindowID *windowList = malloc(sizeof(CGWindowID) * windowCount); int actualCount; CGSGetWindowList(cid, cid, windowCount, windowList, &actualCount); for (int i = 0; i < actualCount; i++) { CGWindowID wid = windowList[i]; CGWindowLevel level; CGSGetWindowLevel(cid, wid, &level); CGFloat alpha; CGSGetWindowAlpha(cid, wid, &alpha); CGRect bounds; CGSGetScreenRectForWindow(cid, wid, &bounds); printf("Window %d: level=%d, alpha=%.2f, bounds=(%.0f,%.0f,%.0f,%.0f)\n", wid, level, alpha, bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height); } int onScreenCount; CGSGetOnScreenWindowCount(cid, cid, &onScreenCount); CGWindowID *onScreenList = malloc(sizeof(CGWindowID) * onScreenCount); CGSGetOnScreenWindowList(cid, cid, onScreenCount, onScreenList, &actualCount); CGFloat targetAlpha = 0.5f; CGFloat duration = 0.3f; CGSSetWindowListAlpha(cid, onScreenList, onScreenCount, targetAlpha, duration); free(windowList); free(onScreenList); ``` -------------------------------- ### Window Creation and Configuration (CGSWindow.h) Source: https://context7.com/nuikit/cgsinternal/llms.txt APIs for creating and manipulating CGS windows, including setting bounds, title, alpha, level, tags, and transformations. ```APIDOC ## Window Creation and Configuration (CGSWindow.h) ### Description The window API allows creation and manipulation of CGS windows with fine-grained control over appearance, behavior, and positioning. ### Method Various C functions ### Endpoint N/A (Internal C API) ### Parameters N/A ### Request Example ```c #include "CGSInternal.h" CGSConnectionID cid = CGSMainConnectionID(); // Create a region for the window bounds CGRect bounds = CGRectMake(0, 0, 800, 600); CGSRegionRef region; CGSNewRegionWithRect(&bounds, ®ion); // Create a new window CGWindowID windowID; CGError error = CGSNewWindow(cid, kCGSBackingBuffered, 100, 100, region, &windowID); if (error == kCGErrorSuccess) { // Set the window title (used by Spaces) CGSSetWindowTitle(cid, windowID, CFSTR("My Window")); // Set window transparency CGSSetWindowAlpha(cid, windowID, 0.9f); // Set window level (floating, normal, etc.) CGSSetWindowLevel(cid, windowID, kCGFloatingWindowLevel); // Configure window tags for special behavior CGSWindowTagBit tags[2] = { kCGSDisableShadowTagBit | kCGSOnAllWorkspacesTagBit, 0 }; CGSSetWindowTags(cid, windowID, tags, kCGSRealMaximumTagSize); // Make the window opaque or transparent CGSSetWindowOpacity(cid, windowID, false); // transparent background // Set window background color (for autofill) CGSSetWindowAutofillColor(cid, windowID, 0.2f, 0.2f, 0.2f); CGSSetWindowAutofill(cid, windowID, true); // Apply an affine transform to the window CGAffineTransform transform = CGAffineTransformMakeScale(0.9, 0.9); CGSSetWindowTransform(cid, windowID, transform); // Configure window shadow CGSSetWindowShadowParameters(cid, windowID, 10.0f, 0.5f, 0, 10); // Order the window on screen CGSOrderWindow(cid, windowID, kCGSOrderAbove, 0); // Move the window CGPoint newOrigin = CGPointMake(200, 200); CGSMoveWindow(cid, windowID, &newOrigin); // Get window's screen rect CGRect screenRect; CGSGetScreenRectForWindow(cid, windowID, &screenRect); printf("Window at: %.0f, %.0f (%.0fx%.0f)\n", screenRect.origin.x, screenRect.origin.y, screenRect.size.width, screenRect.size.height); // Release the window when done CGSReleaseWindow(cid, windowID); } CGSReleaseRegion(region); ``` ### Response N/A (C function return values and side effects) ### Response Example N/A ``` -------------------------------- ### Manage CGS Connections in Objective-C Source: https://context7.com/nuikit/cgsinternal/llms.txt Demonstrates how to establish, manage, and query connections to the Window Server using CGS APIs. This includes obtaining connection IDs, setting properties, and checking system states like menu bar visibility. It's crucial to release connections when no longer needed. ```c #include "CGSInternal.h" // Get the main connection for the current process CGSConnectionID cid = CGSMainConnectionID(); // Create a new connection to the Window Server CGSConnectionID newConnection; CGError error = CGSNewConnection(0, &newConnection); if (error == kCGErrorSuccess) { // Use the new connection // Get the PID of the process owning this connection pid_t pid; CGSConnectionGetPID(newConnection, &pid); printf("Connection owned by PID: %d\n", pid); // Set a connection property CFStringRef key = CFSTR("MyAppKey"); CFStringRef value = CFSTR("MyAppValue"); CGSSetConnectionProperty(cid, newConnection, key, value); // Read the property back CFTypeRef readValue; CGS_copyConnectionProperty(cid, newConnection, key, &readValue); // Release the connection when done CGSReleaseConnection(newConnection); } // Check if the menu bar exists (useful during system transitions) bool menuBarExists = CGSMenuBarExists(cid); // Disable/enable screen updates for batched operations CGSDisableUpdate(cid); // Perform multiple window operations... CGSReenableUpdate(cid); ``` -------------------------------- ### Manage Displays and Configurations using CGSInternal Source: https://context7.com/nuikit/cgsinternal/llms.txt Shows how to access display properties, enumerate available display modes, and apply configuration changes to displays using transaction-based APIs. ```c #include "CGSInternal.h" CGSConnectionID cid = CGSMainConnectionID(); CGDirectDisplayID mainDisplay = CGSMainDisplayID(); uint32_t displayCount = CGSGetNumberOfDisplays(); CGRect displayBounds; CGSGetDisplayBounds(mainDisplay, &displayBounds); int numModes; CGSGetNumberOfDisplayModes(mainDisplay, &numModes); for (int i = 0; i < numModes; i++) { CGSDisplayModeDescription desc; CGSGetDisplayModeDescriptionOfLength(mainDisplay, i, &desc, sizeof(desc)); printf("Mode %d: %ux%u @ %.0fHz (scale: %.1f)\n", i, desc.width, desc.height, (float)desc.freq, desc.scale); } CGDisplayConfigRef config; CGSBeginDisplayConfiguration(&config); CGSConfigureDisplayMode(config, mainDisplay, 0); CGSCompleteDisplayConfiguration(config); ``` -------------------------------- ### Manage Login Sessions and Retrieve Session Info (C) Source: https://context7.com/nuikit/cgsinternal/llms.txt This C code snippet demonstrates how to manage login sessions using CGSSession functions. It retrieves current session information such as session ID, username, and console status, and also lists all available sessions for fast user switching. Proper resource management by releasing obtained dictionaries and arrays is crucial. ```c #include "CGSInternal.h" // Get current session information CFDictionaryRef sessionInfo = CGSCopyCurrentSessionDictionary(); if (sessionInfo) { // Extract session details CFNumberRef sessionID = CFDictionaryGetValue(sessionInfo, CFSTR("kCGSSessionIDKey")); CFStringRef userName = CFDictionaryGetValue(sessionInfo, CFSTR("kCGSSessionUserNameKey")); CFBooleanRef onConsole = CFDictionaryGetValue(sessionInfo, CFSTR("kCGSSessionOnConsoleKey")); CFNumberRef userID = CFDictionaryGetValue(sessionInfo, CFSTR("kCGSSessionUserIDKey")); int sid, uid; CFNumberGetValue(sessionID, kCFNumberIntType, &sid); CFNumberGetValue(userID, kCFNumberIntType, &uid); char name[256]; CFStringGetCString(userName, name, sizeof(name), kCFStringEncodingUTF8); printf("Session ID: %d\n", sid); printf("User: %s (UID: %d)\n", name, uid); printf("On console: %s\n", CFBooleanGetValue(onConsole) ? "yes" : "no"); CFRelease(sessionInfo); } // Get list of all sessions (for fast user switching) CFArrayRef sessions = CGSCopySessionList(); if (sessions) { CFIndex count = CFArrayGetCount(sessions); printf("Total sessions: %ld\n", count); for (CFIndex i = 0; i < count; i++) { CFDictionaryRef session = CFArrayGetValueAtIndex(sessions, i); CFStringRef user = CFDictionaryGetValue(session, CFSTR("kCGSSessionUserNameKey")); if (user) { char userName[256]; CFStringGetCString(user, userName, sizeof(userName), kCFStringEncodingUTF8); printf(" Session %ld: %s\n", i, userName); } } CFRelease(sessions); } // Create a new login session (switches to LoginWindow) // WARNING: This performs fast user switch! // CGSSessionID newSession; // CGSCreateLoginSession(&newSession); // Release a session // CGSReleaseSession(newSession); ``` -------------------------------- ### Connection Management (CGSConnection.h) Source: https://context7.com/nuikit/cgsinternal/llms.txt APIs for managing connections to the Window Server, including obtaining connection IDs, creating new connections, and setting/getting connection properties. ```APIDOC ## Connection Management (CGSConnection.h) ### Description The connection API manages the application's connection to the Window Server. Every macOS application gets a connection ID through which it can manipulate windows, receive events, and interact with the display system. ### Method Various C functions ### Endpoint N/A (Internal C API) ### Parameters N/A ### Request Example ```c #include "CGSInternal.h" // Get the main connection for the current process CGSConnectionID cid = CGSMainConnectionID(); // Create a new connection to the Window Server CGSConnectionID newConnection; CGError error = CGSNewConnection(0, &newConnection); if (error == kCGErrorSuccess) { // Use the new connection // Get the PID of the process owning this connection pid_t pid; CGSConnectionGetPID(newConnection, &pid); printf("Connection owned by PID: %d\n", pid); // Set a connection property CFStringRef key = CFSTR("MyAppKey"); CFStringRef value = CFSTR("MyAppValue"); CGSSetConnectionProperty(cid, newConnection, key, value); // Read the property back CFTypeRef readValue; CGSCopyConnectionProperty(cid, newConnection, key, &readValue); // Release the connection when done CGSReleaseConnection(newConnection); } // Check if the menu bar exists (useful during system transitions) bool menuBarExists = CGSMenuBarExists(cid); // Disable/enable screen updates for batched operations CGSDisableUpdate(cid); // Perform multiple window operations... CGSReenableUpdate(cid); ``` ### Response N/A (C function return values and side effects) ### Response Example N/A ``` -------------------------------- ### Window Transitions API Source: https://context7.com/nuikit/cgsinternal/llms.txt Functions for creating and invoking visual transitions and animations on windows using CGSInternal. ```APIDOC ## CGSNewTransition ### Description Creates a new transition specification for a window or screen. ### Method C-Function ### Parameters - **cid** (CGSConnectionID) - Required - The current connection ID. - **spec** (CGSTransitionSpec*) - Required - Pointer to the transition configuration. - **transition** (CGSTransitionID*) - Required - Pointer to store the resulting transition ID. ### Request Example CGSTransitionSpec spec = { .type = kCGSTransitionCube, .wid = windowID }; CGSNewTransition(cid, &spec, &transition); ## CGSInvokeTransition ### Description Triggers the specified transition with a defined duration. ### Method C-Function ### Parameters - **cid** (CGSConnectionID) - Required - The current connection ID. - **transition** (CGSTransitionID) - Required - The ID of the transition to invoke. - **duration** (float) - Required - Duration in seconds. ### Request Example CGSInvokeTransition(cid, transition, 0.5); ``` -------------------------------- ### Registering System and Connection Event Notifications in CGSInternal Source: https://context7.com/nuikit/cgsinternal/llms.txt Demonstrates how to register callbacks for system-wide and connection-specific events using CGSRegisterNotifyProc and CGSRegisterConnectionNotifyProc. It also covers managing secure input states and checking application responsiveness. ```c #include "CGSInternal.h" CGSConnectionID cid = CGSMainConnectionID(); void systemNotifyCallback(CGSEventType type, void *data, unsigned int dataLength, void *userData) { switch (type) { case kCGSWindowDidCreate: printf("Window created\n"); break; case kCGSWorkspaceDidChange: printf("Workspace changed\n"); break; case kCGSDisplayDidReconfigure: printf("Display reconfigured\n"); break; default: printf("Event: %u\n", type); } } CGSRegisterNotifyProc(systemNotifyCallback, kCGSWindowDidCreate, NULL); void connectionNotifyCallback(CGSEventType type, CGSNotificationData data, size_t dataLength, CGSNotificationArg userData, CGSConnectionID cid) { printf("Connection event: %u on connection %d\n", type, cid); } CGSRegisterConnectionNotifyProc(cid, connectionNotifyCallback, kCGSEventNotificationMouseMoved, NULL); CGSSetSecureEventInput(cid, true); ProcessSerialNumber psn = { 0, kCurrentProcess }; bool isUnresponsive = CGSEventIsAppUnresponsive(cid, &psn); ``` -------------------------------- ### Session Management (CGSSession.h) Source: https://context7.com/nuikit/cgsinternal/llms.txt This section details how to manage login sessions and retrieve session information using the CGSInternal library. ```APIDOC ## Session Management (CGSSession.h) Manage login sessions and retrieve session information. ### Get Current Session Information Retrieves and prints details of the current user session. - **Method**: N/A (Function calls) - **Endpoint**: N/A ### Request Example ```c #include "CGSInternal.h" // Get current session information CFDictionaryRef sessionInfo = CGSCopyCurrentSessionDictionary(); if (sessionInfo) { // Extract session details CFNumberRef sessionID = CFDictionaryGetValue(sessionInfo, CFSTR("kCGSSessionIDKey")); CFStringRef userName = CFDictionaryGetValue(sessionInfo, CFSTR("kCGSSessionUserNameKey")); CFBooleanRef onConsole = CFDictionaryGetValue(sessionInfo, CFSTR("kCGSSessionOnConsoleKey")); CFNumberRef userID = CFDictionaryGetValue(sessionInfo, CFSTR("kCGSSessionUserIDKey")); int sid, uid; CFNumberGetValue(sessionID, kCFNumberIntType, &sid); CFNumberGetValue(userID, kCFNumberIntType, &uid); char name[256]; CFStringGetCString(userName, name, sizeof(name), kCFStringEncodingUTF8); printf("Session ID: %d\n", sid); printf("User: %s (UID: %d)\n", name, uid); printf("On console: %s\n", CFBooleanGetValue(onConsole) ? "yes" : "no"); CFRelease(sessionInfo); } ``` ### Response Prints session details to standard output. - **Session ID** (int) - The unique identifier for the session. - **User** (string) - The username associated with the session. - **UID** (int) - The user ID associated with the session. - **On console** (string) - Indicates if the session is currently active on the console ('yes' or 'no'). ### Get List of All Sessions Retrieves a list of all active user sessions, typically used for fast user switching. - **Method**: N/A (Function calls) - **Endpoint**: N/A ### Request Example ```c #include "CGSInternal.h" // Get list of all sessions (for fast user switching) CFArrayRef sessions = CGSCopySessionList(); if (sessions) { CFIndex count = CFArrayGetCount(sessions); printf("Total sessions: %ld\n", count); for (CFIndex i = 0; i < count; i++) { CFDictionaryRef session = CFArrayGetValueAtIndex(sessions, i); CFStringRef user = CFDictionaryGetValue(session, CFSTR("kCGSSessionUserNameKey")); if (user) { char userName[256]; CFStringGetCString(user, userName, sizeof(userName), kCFStringEncodingUTF8); printf(" Session %ld: %s\n", i, userName); } } CFRelease(sessions); } ``` ### Response Prints the total number of sessions and a list of users for each session to standard output. - **Total sessions** (integer) - The total count of active user sessions. - **Session [index]** (string) - The username for the session at the given index. ### Create New Login Session (Deprecated/Commented Out) This function is commented out in the provided code. It was intended to create a new login session, which would perform a fast user switch to the Login Window. - **WARNING**: This performs fast user switch! ### Release Session (Deprecated/Commented Out) This function is commented out in the provided code. It was intended to release a session resource. ``` -------------------------------- ### Spaces Management API Source: https://context7.com/nuikit/cgsinternal/llms.txt This section details the functions available for managing macOS Spaces, including retrieving space information, creating new spaces, manipulating windows within spaces, and switching between spaces. ```APIDOC ## Spaces Management (CGSSpace.h) Control macOS Spaces (virtual desktops) and manage windows across spaces. ### Description This API provides functions to interact with and manage macOS virtual desktops, referred to as Spaces. It allows for retrieving information about existing spaces, creating new ones, associating windows with specific spaces, and switching between them. ### Method N/A (This is a C API, not a REST API) ### Endpoint N/A (This is a C API, not a REST API) ### Functions #### Get Active Space - **`CGSGetActiveSpace(CGSConnectionID cid)`**: Retrieves the ID of the currently active space. #### Get All Spaces - **`CGSCopySpaces(CGSConnectionID cid, CGSSpaceMask mask)`**: Returns an array of all spaces matching the specified mask. #### Get Space Type - **`CGSSpaceGetType(CGSConnectionID cid, CGSSpaceID sid)`**: Returns the type of a given space (User, Fullscreen, System). #### Get Space Name - **`CGSSpaceCopyName(CGSConnectionID cid, CGSSpaceID sid)`**: Returns the name of a given space. #### Create Space - **`CGSSpaceCreate(CGSConnectionID cid, CFDictionaryRef options)`**: Creates a new space with the specified options. #### Set Space Name - **`CGSSpaceSetName(CGSConnectionID cid, CGSSpaceID sid, CFStringRef name)`**: Sets the name of a given space. #### Get Spaces for Windows - **`CGSCopySpacesForWindows(CGSConnectionID cid, CGSSpaceMask mask, CFArrayRef windowIDs)`**: Returns an array of spaces that contain the specified windows. #### Add Windows to Spaces - **`CGSAddWindowsToSpaces(CGSConnectionID cid, CFArrayRef windows, CFArrayRef spaces)`**: Adds the specified windows to the specified spaces. #### Remove Windows from Spaces - **`CGSRemoveWindowsFromSpaces(CGSConnectionID cid, CFArrayRef windows, CFArrayRef spaces)`**: Removes the specified windows from the specified spaces. #### Get Space Management Mode - **`CGSGetSpaceManagementMode(CGSConnectionID cid)`**: Retrieves the current space management mode (e.g., per-desktop). #### Switch Space - **`CGSManagedDisplaySetCurrentSpace(CGSConnectionID cid, CGDisplayID display, CGSSpaceID sid)`**: Switches the current space for a given display. #### Destroy Space - **`CGSSpaceDestroy(CGSConnectionID cid, CGSSpaceID sid)`**: Destroys a given space. ### Example Usage (C) ```c #include "CGSInternal.h" CGSConnectionID cid = CGSMainConnectionID(); // Get the currently active space CGSSpaceID activeSpace = CGSGetActiveSpace(cid); printf("Active space ID: %zu\n", activeSpace); // Get all spaces CFArrayRef allSpaces = CGSCopySpaces(cid, kCGSAllSpacesMask); CFIndex spaceCount = CFArrayGetCount(allSpaces); printf("Total spaces: %ld\n", spaceCount); for (CFIndex i = 0; i < spaceCount; i++) { CFNumberRef spaceNumber = CFArrayGetValueAtIndex(allSpaces, i); CGSSpaceID sid; CFNumberGetValue(spaceNumber, kCFNumberLongType, &sid); // Get space type CGSSpaceType type = CGSSpaceGetType(cid, sid); const char *typeName = (type == CGSSpaceTypeUser) ? "User" : (type == CGSSpaceTypeFullscreen) ? "Fullscreen" : "System"; // Get space name CFStringRef name = CGSSpaceCopyName(cid, sid); printf("Space %zu: type=%s, name=%s\n", sid, typeName, name ? CFStringGetCStringPtr(name, kCFStringEncodingUTF8) : "(null)"); if (name) CFRelease(name); } CFRelease(allSpaces); // Create a new space CFMutableDictionaryRef options = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFNumberRef typeNum = CFNumberCreate(NULL, kCFNumberIntType, &(int){CGSSpaceTypeUser}); CFDictionarySetValue(options, CFSTR("type"), typeNum); CGSSpaceID newSpace = CGSSpaceCreate(cid, NULL, options); CFRelease(typeNum); CFRelease(options); // Set space name CGSSpaceSetName(cid, newSpace, CFSTR("My Custom Space")); // Get spaces containing specific windows CFMutableArrayRef windowIDs = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); CFNumberRef winNum = CFNumberCreate(NULL, kCFNumberIntType, &(int){12345}); CFArrayAppendValue(windowIDs, winNum); CFArrayRef spacesForWindows = CGSCopySpacesForWindows(cid, kCGSAllSpacesMask, windowIDs); CFRelease(winNum); CFRelease(windowIDs); // Add windows to a space CFMutableArrayRef windows = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); CFMutableArrayRef spaces = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); // ... populate arrays ... CGSAddWindowsToSpaces(cid, windows, spaces); // Remove windows from a space CGSRemoveWindowsFromSpaces(cid, windows, spaces); CFRelease(windows); CFRelease(spaces); // Get space management mode (separate spaces per display) CGSSpaceManagementMode mode = CGSGetSpaceManagementMode(cid); printf("Space management: %s\n", mode == kCGSPackagesSpaceManagementModePerDesktop ? "Per Desktop" : "Single"); // Switch to a different space CGSManagedDisplaySetCurrentSpace(cid, kCGSPackagesMainDisplayIdentifier, newSpace); // Destroy the space when done CGSSpaceDestroy(cid, newSpace); ``` ``` -------------------------------- ### Manage System-Wide Hot Keys with CGSHotKeys.h Source: https://context7.com/nuikit/cgsinternal/llms.txt Register and control system-wide hot keys and keyboard shortcuts using the CGSHotKeys.h internal API. This includes checking, enabling/disabling symbolic and custom hot keys, retrieving their configurations, and setting the global hot key operating mode. ```Objective-C #include "CGSInternal.h" CGSConnectionID cid = CGSMainConnectionID(); // Check if a symbolic hot key is enabled bool screenshotEnabled = CGSIsSymbolicHotKeyEnabled(kCGSHotKeyScreenshot); printf("Screenshot hotkey enabled: %s\n", screenshotEnabled ? "yes" : "no"); // Disable/enable a symbolic hot key CGSSetSymbolicHotKeyEnabled(kCGSHotKeyScreenshot, false); // ... do something ... CGSSetSymbolicHotKeyEnabled(kCGSHotKeyScreenshot, true); // Get hot key configuration unichar keyEquivalent, virtualKeyCode; CGSModifierFlags modifiers; CGSGetSymbolicHotKeyValue(kCGSHotKeyScreenshot, &keyEquivalent, &virtualKeyCode, &modifiers); printf("Screenshot: key=%c, modifiers=%u\n", keyEquivalent, modifiers); // Get/set global hot key operating mode CGSGlobalHotKeyOperatingMode mode; CGSGetGlobalHotKeyOperatingMode(cid, &mode); // Disable all hot keys for this app (useful for games, kiosk mode) CGSSetGlobalHotKeyOperatingMode(cid, kCGSGlobalHotKeyDisable); // Disable hot keys except Universal Access CGSSetGlobalHotKeyOperatingMode(cid, kCGSGlobalHotKeyDisableAllButUniversalAccess); // Re-enable hot keys CGSSetGlobalHotKeyOperatingMode(cid, kCGSGlobalHotKeyEnable); // Register a custom hot key (unique ID 1000+) int hotKeyUID = 1000; unichar key = 'A'; CGSModifierFlags flags = kCGSCommandKeyMask | kCGSShiftKeyMask; CGSSetHotKey(cid, hotKeyUID, 0, key, flags); // Check if custom hot key is enabled BOOL isEnabled = CGSIsHotKeyEnabled(cid, hotKeyUID); // Enable/disable custom hot key CGSSetHotKeyEnabled(cid, hotKeyUID, true); // Get custom hot key configuration unichar options, retrievedKey; CGSModifierFlags retrievedFlags; CGSGetHotKey(cid, hotKeyUID, &options, &retrievedKey, &retrievedFlags); // Remove custom hot key CGSRemoveHotKey(cid, hotKeyUID); ``` -------------------------------- ### Hot Keys Management API Source: https://context7.com/nuikit/cgsinternal/llms.txt APIs for registering and controlling system-wide hot keys and keyboard shortcuts. ```APIDOC ## Hot Keys Management (CGSHotKeys.h) Register and control system-wide hot keys and keyboard shortcuts. ### Description Allows checking the status of symbolic hot keys, enabling or disabling them, retrieving their configurations, and registering/managing custom hot keys with specific key combinations and modifiers. ### Method Various (CGS functions) ### Endpoint N/A (Internal CGS functions) ### Parameters N/A for general description, refer to individual function calls. ### Request Example ```c #include "CGSInternal.h" CGSConnectionID cid = CGSMainConnectionID(); // Check if a symbolic hot key is enabled bool screenshotEnabled = CGSIsSymbolicHotKeyEnabled(kCGSHotKeyScreenshot); printf("Screenshot hotkey enabled: %s\n", screenshotEnabled ? "yes" : "no"); // Disable/enable a symbolic hot key CGSSetSymbolicHotKeyEnabled(kCGSHotKeyScreenshot, false); // ... do something ... CGSSetSymbolicHotKeyEnabled(kCGSHotKeyScreenshot, true); // Get hot key configuration unichar keyEquivalent, virtualKeyCode; CGSModifierFlags modifiers; CGSGetSymbolicHotKeyValue(kCGSHotKeyScreenshot, &keyEquivalent, &virtualKeyCode, &modifiers); printf("Screenshot: key=%c, modifiers=%u\n", keyEquivalent, modifiers); // Get/set global hot key operating mode CGSGlobalHotKeyOperatingMode mode; CGSGetGlobalHotKeyOperatingMode(cid, &mode); // Disable all hot keys for this app (useful for games, kiosk mode) CGSSetGlobalHotKeyOperatingMode(cid, kCGSGlobalHotKeyDisable); // Disable hot keys except Universal Access CGSSetGlobalHotKeyOperatingMode(cid, kCGSGlobalHotKeyDisableAllButUniversalAccess); // Re-enable hot keys CGSSetGlobalHotKeyOperatingMode(cid, kCGSGlobalHotKeyEnable); // Register a custom hot key (unique ID 1000+) int hotKeyUID = 1000; unichar key = 'A'; CGSModifierFlags flags = kCGSCommandKeyMask | kCGSShiftKeyMask; CGSSetHotKey(cid, hotKeyUID, 0, key, flags); // Check if custom hot key is enabled BOOL isEnabled = CGSIsHotKeyEnabled(cid, hotKeyUID); // Enable/disable custom hot key CGSSetHotKeyEnabled(cid, hotKeyUID, true); // Get custom hot key configuration unichar options, retrievedKey; CGSModifierFlags retrievedFlags; CGSGetHotKey(cid, hotKeyUID, &options, &retrievedKey, &retrievedFlags); // Remove custom hot key CGSRemoveHotKey(cid, hotKeyUID); ``` ### Response N/A (Direct function calls with return values or output parameters) ``` -------------------------------- ### Cursor Control API Source: https://context7.com/nuikit/cgsinternal/llms.txt APIs for controlling cursor appearance, position, and custom cursor registration. ```APIDOC ## Cursor Control (CGSCursor.h) Programmatically control cursor appearance, position, and custom cursor registration. ### Description Provides functions to get and set the cursor's location, hide or show it, obscure it until the mouse moves, adjust its scale for accessibility, and register custom cursor images. ### Method Various (CGS functions) ### Endpoint N/A (Internal CGS functions) ### Parameters N/A for general description, refer to individual function calls. ### Request Example ```c #include "CGSInternal.h" CGSConnectionID cid = CGSMainConnectionID(); // Get current cursor location CGPoint cursorPos; CGSGetCurrentCursorLocation(cid, &cursorPos); printf("Cursor at: %.0f, %.0f\n", cursorPos.x, cursorPos.y); // Move the cursor to a new position CGSWarpCursorPosition(cid, 500.0f, 500.0f); // Hide/show cursor CGSHideCursor(cid); // ... do something ... CGSShowCursor(cid); // Temporarily hide cursor until mouse moves CGSObscureCursor(cid); // Get/set cursor scale (for accessibility) CGFloat scale; CGSGetCursorScale(cid, &scale); printf("Current cursor scale: %.1f\n", scale); // Make cursor larger (max 4.0 via System Preferences) CGSSetCursorScale(cid, 2.0f); // Get the current cursor seed (changes when cursor updates) int seed = CGSCurrentCursorSeed(); printf("Cursor seed: %d\n", seed); // Register a custom cursor CGImageRef cursorImage = /* create or load image */; CFMutableArrayRef imageArray = CFArrayCreateMutable(NULL, 1, &kCFTypeArrayCallBacks); CFArrayAppendValue(imageArray, cursorImage); int cursorSeed; CGError error = CGSRegisterCursorWithImages( cid, "com.myapp.customcursor", // cursor name true, // set globally true, // instantly 1, // frame count imageArray, // images CGSizeMake(16, 16), // cursor size CGPointMake(0, 0), // hotspot &cursorSeed, // output seed CGRectMake(0, 0, 16, 16), // bounds 0.0, // frame duration (for animated) 0 // repeat count ); CFRelease(imageArray); // Get registered cursor properties CGSize imageSize; CGPoint hotSpot; NSUInteger frameCount; CGFloat frameDuration; CFArrayRef images; CGSCopyRegisteredCursorImages(cid, "com.myapp.customcursor", &imageSize, &hotSpot, &frameCount, &frameDuration, &images); // Force the wait cursor (spinning beach ball) - use with caution! // CGSForceWaitCursorActive(cid, true); // CGSForceWaitCursorActive(cid, false); ``` ### Response N/A (Direct function calls with return values or output parameters) ``` -------------------------------- ### Region Operations API Source: https://context7.com/nuikit/cgsinternal/llms.txt Functions for creating, manipulating, and querying geometric regions used for window shapes and hit testing. ```APIDOC ## CGSNewRegionWithRect ### Description Creates a new geometric region from a single rectangle. ### Method C-Function ### Parameters - **rect** (CGRect*) - Required - The rectangle defining the region. - **region** (CGSRegionRef*) - Required - Pointer to store the created region reference. ## CGSUnionRegion ### Description Calculates the union of two regions. ### Method C-Function ### Parameters - **region1** (CGSRegionRef) - Required - First region. - **region2** (CGSRegionRef) - Required - Second region. - **result** (CGSRegionRef*) - Required - Pointer to store the resulting union region. ## CGSPointInRegion ### Description Tests if a point is contained within a specific region. ### Method C-Function ### Parameters - **region** (CGSRegionRef) - Required - The region to test against. - **point** (CGPoint*) - Required - The point to check. ### Response - **bool** - Returns true if the point is within the region. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.