1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
#import <Cocoa/Cocoa.h>
@implementation Squeak
+ (void) initialize
{
NSMutableDictionary *dict;
NSUserDefaults *defaults;
defaults= [NSUserDefaults standardUserDefaults];
dict= [NSMutableDictionary dictionary];
[dict setObject: @"YES" forKey: @"AppleDockIconEnabled"];
[defaults registerDefaults: dict];
}
static char *documentName= 0;
-(BOOL) application: (NSApplication *) theApplication
openFile: (NSString *) filename
{
if (fromFinder)
documentName= strdup([filename cString]);
return YES;
}
-(void) applicationDidFinishLaunching: (NSNotification *)note
{
int fds[2];
// this saves an awful lot of tedious mutex contention (and besides
// is essentially free, since there's no way to avoid writing a
// socket to inform aio of the availability of the event)
#if 0
if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) != 0)
{
perror("socketpair");
exit(1);
}
osXfd= fds[0];
stXfd= fds[1];
#else
if (pipe(fds))
{
perror("pipe");
exit(1);
}
stXfd= fds[0];
osXfd= fds[1];
#endif
aioEnable(stXfd, 0, 0);
aioHandle(stXfd, evtHandler, AIO_RX);
#if (!USE_SPINLOCK)
{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
# ifndef NDEBUG
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
# endif
if (pthread_mutex_init(&displayMx, &attr))
{
perror("pthread_mutex_init");
exit(1);
}
pthread_mutexattr_destroy(&attr);
}
#endif
if (fromFinder)
{
char *ptr= 0;
strncpy(resourcePath, argVec[0], sizeof(resourcePath)); // .app/Contents/MacOS/squeak
if ((ptr= strrchr(resourcePath, '/')))
{
*ptr= '\0'; // .app/Contents/MacOS
if ((ptr= strrchr(resourcePath, '/')))
{
*ptr= '\0'; // .app/Contents
strcpy(ptr, "/Resources/"); // .app/Contents/Resources/
}
else
resourcePath[0]= '\0';
}
else
resourcePath[0]= '\0';
}
imgInit();
// vmPath has been set to the realpath() of the VM, which might be
// bogus (since the more cultivated OS X user will make it a symlink
// to /usr/local/bin/squeak). put it back where it should be. note
// that we would also like to pick up the sources file from
// resourcePath/SqueakV*.sources, but I guess the stupid image is
// going to look for it in the vmPath. ho hum.
{
char *ptr;
assert(strlen(argVec[0]) < MAXPATHLEN);
strcpy(vmPath, argVec[0]);
if ((ptr= strrchr(vmPath, '/')))
ptr[1]= '\0';
}
setUpDisplay();
printf("fullScreenFlag %d\n", getFullScreenFlag());
setUpWindow(fullscreen= getFullScreenFlag());
[NSThread
detachNewThreadSelector: @selector(interpret:)
toTarget: self
withObject: nil];
}
- (void) interpret: (id)context
{
[[NSAutoreleasePool alloc] init]; // running in new thread
interpret();
(void)recordMouseEvent;
(void)recordKeyboardEvent;
(void)recordDragEvent;
}
- (void) applicationDidChangeScreenParameters: (NSNotification *)note
{
//xxx this one might be tricky in the absence of appWillChangeScreenParams:
fprintf(stderr, "\nDISPLAY PARAMETERS CHANGED\n\n");
// lock(display);
pixWidth= pixHeight= pixDepth= 0;
setUpDisplay();
//setUpWindow(getFullScreenFlag());
updatePix();
// unlock(display);
//setUpMenus();
fullDisplayUpdate();
}
- (void) unhideAllApplications: (id)sender
{
[super unhideAllApplications: sender];
[win orderFront: self]; // so that unhinding once more will reveal the Sq window
}
- (BOOL) windowShouldClose: (id)sender
{
return NO;
}
- (void) terminate: (id)sender
{
[super terminate: sender];
exit(0);
}
- (void) maybeTerminate: (id)sender
{
switch (NSRunAlertPanel(@"Really quit?",
@"All changes since your last save will be lost.\n\nIf you want to save your changes, press `Cancel' and then choose `save and quit' from the background menu in the Squeak window.",
@"Quit",
@"Cancel",
nil))
{
case NSAlertDefaultReturn: [self terminate: self];
}
}
- (void) performAbout: (id)sender
{
extern char *getVersionInfo(int verbose);
char *info= getVersionInfo(1);
NSPanel *panel= NSGetInformationalAlertPanel(@"About Squeak",
@"%s",
@"Dismiss",
nil,
nil,
info);
NSRect frame= [panel frame];
frame.size.width *= 1.5;
[panel setFrame: frame display: NO];
[NSApp runModalForWindow: panel];
[panel close];
free(info);
}
//xxx why does rebuilding the menu lose boldface on the Apple menu item???
- (void) performEnableKeys: (id)sender { cmdKeys= 1; setUpMenus(); }
- (void) performDisableKeys: (id)sender { cmdKeys= 0; setUpMenus(); }
- (void) windowWillMove: (NSNotification *)note
{
//xxx FIXME SOON: there are other ways to enter this (and ways other than
// noteEvent to escape from it)
inModalLoop= 1;
}
- (NSSize) windowWillResize: (NSWindow *)sender toSize: (NSSize)size
{
return glActive ? [sender frame].size : size;
}
- (void) windowDidResize: (NSNotification *)note
{
reframeRenderers();
}
-(void) sendEvent: (NSEvent *)event
{
int type= [event type];
NSPoint loc= [event locationInWindow];
NSWindow *evtWin= [event window];
#if 0
NSPoint loc= (fullscreen
? [NSEvent mouseLocation] //xxx should use deltas
: [event locationInWindow]);
#endif
if (evtWin && ((NSWindow *)win != [event window]))
{
[super sendEvent: event];
return;
}
switch (type)
{
# define down buttonState |= qz2sqButton([event buttonNumber])
# define move
# define up buttonState &= ~qz2sqButton([event buttonNumber])
# define recordEvent(delta) \
if (fullscreen || NSPointInRect(loc, [view frame])) \
{ \
noteMousePoint(loc); \
delta; \
modifierState= qz2sqModifiers([event modifierFlags]); \
noteMouseEvent(); \
} \
else \
[super sendEvent: event] // don't track outside window
case NSLeftMouseDown: case NSOtherMouseDown: case NSRightMouseDown:
if ((!active) || NSPointInRect(loc, resizeRect))
[super sendEvent: event]; // first click, or start resize
else
recordEvent(down);
break;
case NSLeftMouseDragged: case NSRightMouseDragged: case NSOtherMouseDragged:
if (!(buttonState & qz2sqButton([event buttonNumber])))
{
[super sendEvent: event]; // already tracking window move
break;
}
// fall through...
case NSMouseMoved:
recordEvent(move);
break;
case NSLeftMouseUp: case NSOtherMouseUp: case NSRightMouseUp:
recordEvent(up);
break;
# undef recordEvent
# undef down
# undef move
# undef up
case NSKeyDown:
{
int keyCode;
modifierState= qz2sqModifiers([event modifierFlags]);
keyCode= [view composeKeyDown: event]; //qz2sqKey(event);
if (keyCode >= 0)
{
if (cmdKeys)
{
if ((modifierState == CommandKeyBit) || (modifierState == CommandKeyBit + ShiftKeyBit))
switch (keyCode)
{
case '?': [NSApp showHelp: self]; keyCode= -1; break;
case 'h': [NSApp hide: self]; keyCode= -1; break;
case 'k': [NSApp performDisableKeys: self]; keyCode= -1; break;
case 'm': [win performMiniaturize: self]; keyCode= -1; break;
case 'q': [NSApp maybeTerminate: self]; keyCode= -1; break;
}
else if (modifierState == CommandKeyBit + OptionKeyBit)
switch (keyCode)
{
case 'h': [NSApp hideOtherApplications: self]; keyCode= -1; break;
}
}
if (keyCode >= 0)
{
if (![event isARepeat])
noteKeyboardEvent(keyCode, EventKeyDown, modifierState);
noteKeyboardEvent(keyCode, EventKeyChar, modifierState);
recordKeystroke(keyCode); /* DEPRECATED */
}
else // key up not interesting
[view composeKeyUp: event];
}
}
break;
case NSKeyUp:
{
int keyCode;
modifierState= qz2sqModifiers([event modifierFlags]);
keyCode= [view composeKeyUp: event]; //qz2sqKey(event);
if (keyCode >= 0)
{
noteKeyboardEvent(keyCode, EventKeyUp, modifierState);
//accentMap= 0;
}
}
break;
case NSScrollWheel:
{
int keyCode= ([event deltaY] >= 0.0) ? 30 : 31;
noteKeyboardEvent(keyCode, EventKeyDown, modifierState);
noteKeyboardEvent(keyCode, EventKeyChar, modifierState);
noteKeyboardEvent(keyCode, EventKeyUp, modifierState);
}
break;
case NSAppKitDefined:
switch ([event subtype])
{
case NSApplicationActivatedEventType:
active= 1;
break;
case NSApplicationDeactivatedEventType:
active= 0;
break;
// case NSScreenChangedEventType: //xxx this means the window
// changed to a different physical screen, which is useless
// info (we'd far rather be informed that the current screen's
// depth has changed)
}
//dprintf(("AppKitDefinedEvent subtype %d\n", [event subtype]));
[super sendEvent: event];
break;
// case NSFlagsChanged:
// case NSApplicationDefined: break;
// case NSPeriodic: break;
// case NSCursorUpdate: break;
default: // almost always NSSystemDefined
//dprintf(("Event type %d subtype %d\n", [event type], [event subtype]));
[super sendEvent: event];
}
}
@end // Squeak
|