In the background
MyWi runs in the background as a faceless application (referred to by Apple's documentation, and heretofore as an "agent application") and brings up a minimal user interface when a hot key is typed. In order to have a good user experience, the UI absolutely has to come forward immediately. When using development builds of MyWi myself I was often having to wait for the window to appear, especially when all of my physical memory was in use. During the day, I'm a java developer for a small software company. In addition to eclipse, safari, mail, itunes, ical etc, I also have four additional java virtual machines running our application(s). Let's just say that this does not leave a lot of memory lying around to keep my agent out of the swap file. I was hoping that there was some way to keep my agent resident in physical memory, and lo and behold, there is. Daniel Waylonis pointed out the following thread to me from the core audio list in which the task_wire mach call is described. It was very easy to implement like so:
#import <mach/vm_map.h>
...
//stay resident in memory
vm_map_t me = mach_task_self();
kern_return_t code = task_wire(me, true);
if (code == KERN_INVALID_HOST) {
NSLog(@"vm_wire returned KERN_INVALID_HOST.\n");
} else if (code) {
NSLog(@"task_wire error code = %d.\n", code);
} else {
NSLog(@"task_wire(..., true) succeeded.\n");
}
Now the UI usually comes up immediately. The first time I hit the hot key in the morning, it still takes a while to present the UI and I'll have to do some additional analysis to figure out why.