Feeds

Fresh effects for Cocoa apps

One file, many identities

Providing a secure and efficient Helpdesk

Mac Secrets I've decided to delve into Apple's CoreGraphics framework, to explore some of its undocumented features. Using these routines, you can add all sorts of wondrous effects to your Cocoa applications.

I ought to begin by saying that I'm claiming no originality for the undocumented nuggets. If you trawl Google, you'll find similar revelations of CoreGraphics naughtiness scattered all over the net. The key to understanding what functionality is available is a header file called CGSPrivate.h. This file is the work of many individuals, and it attempts to elucidate some of the undocumented routines and data types contained inside the framework.

You should also be aware that there are many different versions of this file, with subtle naming differences between them. For example, a connection between your application and the window server is variously referred to as a CGSConnection, a CGSConnectionID and a CGSConnectionRef. Confused? You will be.

In an effort to cut through the madness, I've included a fairly recent version of CGSPrivate.h as part of this month's project. As well as being relatively non-verbose - it uses the first of the three naming styles given above - it also makes a big effort to divide the various routines up into functional areas, and it includes some later additions such as CGSAddWindowFilter, about which more later.

I've made a few very minor changes to the file, all identified by my initials, DSJ. Despite being a true-blue Brit, I long ago gave up the fight regarding how to spell "colour", when programming! I've also added a new typedef to make clear when a transition handle should be supplied.

With the above in place, I created a new NSWindow category called CGSSecrets in order to demonstrate some of the functionality available through these undocumented CoreGraphics calls.

I should point out that in general I think it's easier to do this through a category rather than subclassing NSWindow. For one thing, the code is more immediately reusable, and for another, it can be applied to any window in your application. Moreover, you don't have to start introducing custom class names to Interface Builder. On the negative side, however, it does mean that you can't add new instance variables.

The category method below produces a window flipping effect that should be familiar to anyone who's used one of the many Dashboard widgets. As you'll know, many widgets have a little info button - usually in the lower, right corner - that, when clicked, causes the widget's window to flip over to reveal user-settable preferences or other information.

- (void) flipWithDuration: (float) duration forwards: (BOOL) forwards
{
  CGSTransitionSpec spec;
  CGSTransitionHandle transitionHandle;
  CGSConnection cid = CGSDefaultConnection;
        
  spec.type = CGSFlip;
  spec.option = 0x80 | (forwards ? 2 : 1);
  spec.wid = [self windowNumber];
  spec.backColor = nil;
        
  transitionHandle = -1;
  CGSNewTransition (cid, &spec, &transitionHandle);
  CGSInvokeTransition (cid, transitionHandle, duration);
  [[NSRunLoop currentRunLoop] runUntilDate: 
        [NSDate dateWithTimeIntervalSinceNow: duration]];
  CGSReleaseTransition (cid, transitionHandle);
}

I've seen Cocoa code fragments that attempt to do something similar without using the undocumented CoreGraphics routines but - in general - they don't perform well, especially if a complex graphic is on the window being flipped. This is a strong argument for Apple listening to the various enhancement requests and making more of this stuff official.

The flipWithDuration:forwards: routine takes a float specifying the animation duration - half a second seems about right - and a direction so that you can flip one way, and then flip the other way when returning to the original view.

The code works by using the undocumented CGSNewTransition routine to create a new transition according to the specification provided in the CGSTransitionSpec record.

The key thing here is the spec.type field, which is set to CGSFlip for a horizontal flip transition. Setting it to CGSWarpSwitch will give you a vortex-like transition, whereas CGSCube will give you the familiar cube transition - as when swapping from one user to another with fast user switching enabled.

Fliipping a complex graphic

Flipping a complex graphic doesn’t mean lost performance with the CoreGraphics routine

Incidentally, if you want to include the entire desktop - rather than just your application window - in the transition, then you can set spec.wid to zero. Typically, you wouldn't want to do that, but the paradigm obviously makes sense for user switching. Also bear in mind that, with a multi-display configuration, setting wid to zero will apply the transition to all displays individually. I'm not aware of any call that would make CoreGraphics treat the entire desktop surface as a single transition.

The demo program shows how to use a CoreGraphics transition and demonstrates an undocumented method of NSApplication called speakString:. Just pass any string to this method, and the speech synthesizer will produce it using the default voice.

Finally, the demo program also shows another interesting CoreGraphics routine called CGDisplayVisualBell. This is nothing to do with displaying visual bell symbols on the screen. Rather, it's a routine that attempts to apply a bell-shaped luminosity curve to the screen with respect to time. It simulates the rise, sustain and fall time of an old-style flash gun, and is useful when taking a photo, or producing some similar effect. Source code of the demo application can be downloaded from here.

More exploration of CoreGraphics next time. ®

Internet Security Threat Report 2014

More from The Register

next story
Microsoft on the Threshold of a new name for Windows next week
Rebranded OS reportedly set to be flung open by Redmond
Apple: SO sorry for the iOS 8.0.1 UPDATE BUNGLE HORROR
Apple kills 'upgrade'. Hey, Microsoft. You sure you want to be like these guys?
SMASH the Bash bug! Apple and Red Hat scramble for patch batches
'Applying multiple security updates is extremely difficult'
Business is back, baby! Hasta la VISTA, Win 8... Oh, yeah, Windows 9
Forget touchscreen millennials, Microsoft goes for mouse crowd
ARM gives Internet of Things a piece of its mind – the Cortex-M7
32-bit core packs some DSP for VIP IoT CPU LOL
Lotus Notes inventor Ozzie invents app to talk to people on your phone
Imagine that. Startup floats with voice collab app for Win iPhone
prev story

Whitepapers

Providing a secure and efficient Helpdesk
A single remote control platform for user support is be key to providing an efficient helpdesk. Retain full control over the way in which screen and keystroke data is transmitted.
Intelligent flash storage arrays
Tegile Intelligent Storage Arrays with IntelliFlash helps IT boost storage utilization and effciency while delivering unmatched storage savings and performance.
Beginner's guide to SSL certificates
De-mystify the technology involved and give you the information you need to make the best decision when considering your online security options.
Security for virtualized datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.
Secure remote control for conventional and virtual desktops
Balancing user privacy and privileged access, in accordance with compliance frameworks and legislation. Evaluating any potential remote control choice.