| @@ -41,7 +41,7 @@ Memimage *gscreen; · 40 unmodified lines |
| 41 | 41 | |
| 42 | 42 | static int readybit; |
| 43 | 43 | static Rendez rend; |
| 44 | | −static int triedscreen; |
| 44 | +static int triedscreen; |
| 45 | 45 | |
| 46 | 46 | /// |
| 47 | 47 | // menu |
| @@ -64,6 +64,12 @@ static CGRect bounds; · 16 unmodified lines |
| 64 | 64 | static PasteboardRef appleclip; |
| 65 | 65 | static _Rect winRect; |
| 66 | 66 | |
| 67 | +static Boolean altPressed = false; |
| 68 | +static Boolean button2 = false; |
| 69 | +static Boolean button3 = false; |
| 70 | + |
| 71 | +static Boolean needflush = false; |
| 72 | + |
| 67 | 73 | |
| 68 | 74 | static int |
| 69 | 75 | isready(void*a) |
| @@ -73,7 +79,11 @@ isready(void*a) · 3 unmodified lines |
| 73 | 79 | |
| 74 | 80 | CGContextRef QuartzContext; |
| 75 | 81 | |
| 82 | +static OSStatus MainWindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData); |
| 83 | +static OSStatus MainWindowCommandHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData); |
| 84 | + |
| 76 | 85 | static void winproc(void *a); |
| 86 | +static void flushproc(void *a); |
| 77 | 87 | |
| 78 | 88 | void |
| 79 | 89 | screeninit(void) |
| @@ -108,12 +118,10 @@ screeninit(void) · 28 unmodified lines |
| 108 | 118 | dataProviderRef, 0, 0, kCGRenderingIntentDefault); |
| 109 | 119 | |
| 110 | 120 | kproc("osxscreen", winproc, nil, 0); |
| 121 | + kproc("osxflush", flushproc, nil, 0); |
| 111 | 122 | Sleep(&rend, isready, nil); |
| 112 | 123 | } |
| 113 | 124 | |
| 114 | | −static OSStatus MainWindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData); |
| 115 | | −static OSStatus MainWindowCommandHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData); |
| 116 | | − |
| 117 | 125 | void |
| 118 | 126 | window_resized(void) |
| 119 | 127 | { |
| @@ -122,6 +130,21 @@ window_resized(void) · 2 unmodified lines |
| 122 | 130 | bounds = CGRectMake(0, 0, winRect.right-winRect.left, winRect.bottom - winRect.top); |
| 123 | 131 | } |
| 124 | 132 | |
| 133 | +static void |
| 134 | +flushproc(void *a) |
| 135 | +{ |
| 136 | + for(;;) { |
| 137 | + if(needflush) { |
| 138 | + drawqlock(); |
| 139 | + QDBeginCGContext(GetWindowPort(theWindow), &context); |
| 140 | + CGContextFlush(context); |
| 141 | + QDEndCGContext(GetWindowPort(theWindow), &context); |
| 142 | + needflush = false; |
| 143 | + drawqunlock(); |
| 144 | + } |
| 145 | + usleep(33333); |
| 146 | + } |
| 147 | +} |
| 125 | 148 | |
| 126 | 149 | static void |
| 127 | 150 | winproc(void *a) |
| @@ -208,7 +231,6 @@ winproc(void *a) · 80 unmodified lines |
| 208 | 231 | ShowMenuBar(); |
| 209 | 232 | window_resized(); |
| 210 | 233 | SelectWindow(theWindow); |
| 211 | | −// terminit(); |
| 212 | 234 | // Run the event loop |
| 213 | 235 | readybit = 1; |
| 214 | 236 | Wakeup(&rend); |
| @@ -273,7 +295,6 @@ convert_key(UInt32 key, UInt32 charcode) · 58 unmodified lines |
| 273 | 295 | void |
| 274 | 296 | sendbuttons(int b, int x, int y) |
| 275 | 297 | { |
| 276 | | − /* TO DO: add special keyboard characters as mouse buttons */ |
| 277 | 298 | mousetrack(b, x, y, 0); |
| 278 | 299 | } |
| 279 | 300 | |
| @@ -306,7 +327,9 @@ full_screen(void) · 26 unmodified lines |
| 306 | 327 | BeginFullScreen(&fullScreenRestore, 0, 0, 0, &theWindow, 0, 0); |
| 307 | 328 | amFullScreen = 1; |
| 308 | 329 | window_resized(); |
| 309 | | − Rectangle rect = { { 0, 0 }, { bounds.size.width, bounds.size.height} }; |
| 330 | + Rectangle rect = { { 0, 0 }, |
| 331 | + { bounds.size.width, |
| 332 | + bounds.size.height} }; |
| 310 | 333 | drawqlock(); |
| 311 | 334 | flushmemscreen(rect); |
| 312 | 335 | drawqunlock(); |
| @@ -320,6 +343,10 @@ MainWindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *us · 7 unmodified lines |
| 320 | 343 | result = CallNextEventHandler(nextHandler, event); |
| 321 | 344 | UInt32 class = GetEventClass (event); |
| 322 | 345 | UInt32 kind = GetEventKind (event); |
| 346 | + static uint32_t mousebuttons = 0; // bitmask of buttons currently down |
| 347 | + static uint32_t mouseX = 0; |
| 348 | + static uint32_t mouseY = 0; |
| 349 | + |
| 323 | 350 | if(class == kEventClassKeyboard) { |
| 324 | 351 | char macCharCodes; |
| 325 | 352 | UInt32 macKeyCode; |
| @@ -331,24 +358,69 @@ MainWindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *us · 5 unmodified lines |
| 331 | 358 | sizeof(macKeyCode), NULL, &macKeyCode); |
| 332 | 359 | GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, |
| 333 | 360 | sizeof(macKeyModifiers), NULL, &macKeyModifiers); |
| 334 | | − switch(kind) { |
| 361 | + switch(kind) { |
| 335 | 362 | case kEventRawKeyModifiersChanged: |
| 336 | | − if (macKeyModifiers == 0x1800) leave_full_screen(); |
| 337 | | − break; |
| 338 | | − case kEventRawKeyDown: |
| 339 | | − case kEventRawKeyRepeat: { |
| 340 | | − if(macKeyModifiers != 256) { |
| 341 | | − if (kind == kEventRawKeyRepeat || kind == kEventRawKeyDown) { |
| 342 | | − int key = convert_key(macKeyCode, macCharCodes); |
| 343 | | − if(key != -1) |
| 344 | | − gkbdputc(gkbdq, key); |
| 345 | | −if(0 && key != -1)fprint(2, "[%C]", key); |
| 363 | + if (macKeyModifiers == (controlKey | optionKey)) leave_full_screen(); |
| 364 | + |
| 365 | + switch(macKeyModifiers & (optionKey | cmdKey)) { |
| 366 | + case (optionKey | cmdKey): |
| 367 | + /* due to chording we need to handle the case when both |
| 368 | + * modifier keys are pressed at the same time. |
| 369 | + * currently it's only 2-3 snarf and the 3-2 noop |
| 370 | + */ |
| 371 | + altPressed = true; |
| 372 | + if(mousebuttons & 1 || mousebuttons & 2 || mousebuttons & 4) { |
| 373 | + mousebuttons |= 2; /* set button 2 */ |
| 374 | + mousebuttons |= 4; /* set button 3 */ |
| 375 | + button2 = true; |
| 376 | + button3 = true; |
| 377 | + sendbuttons(mousebuttons, mouseX, mouseY); |
| 378 | + } |
| 379 | + break; |
| 380 | + case optionKey: |
| 381 | + altPressed = true; |
| 382 | + if(mousebuttons & 1 || mousebuttons & 4) { |
| 383 | + mousebuttons |= 2; /* set button 2 */ |
| 384 | + button2 = true; |
| 385 | + sendbuttons(mousebuttons, mouseX, mouseY); |
| 346 | 386 | } |
| 387 | + break; |
| 388 | + case cmdKey: |
| 389 | + if(mousebuttons & 1 || mousebuttons & 2) { |
| 390 | + mousebuttons |= 4; /* set button 3 */ |
| 391 | + button3 = true; |
| 392 | + sendbuttons(mousebuttons, mouseX, mouseY); |
| 393 | + } |
| 394 | + break; |
| 395 | + case 0: |
| 396 | + default: |
| 397 | + if(button2 || button3) { |
| 398 | + if(button2) { |
| 399 | + mousebuttons &= ~2; /* clear button 2 */ |
| 400 | + button2 = false; |
| 401 | + altPressed = false; |
| 402 | + } |
| 403 | + if(button3) { |
| 404 | + mousebuttons &= ~4; /* clear button 3 */ |
| 405 | + button3 = false; |
| 406 | + } |
| 407 | + sendbuttons(mousebuttons, mouseX, mouseY); |
| 408 | + } |
| 409 | + if(altPressed) { |
| 410 | + gkbdputc(gkbdq, Kalt); |
| 411 | + altPressed = false; |
| 412 | + } |
| 413 | + break; |
| 347 | 414 | } |
| 348 | | − else |
| 415 | + break; |
| 416 | + case kEventRawKeyDown: |
| 417 | + case kEventRawKeyRepeat: |
| 418 | + if(macKeyModifiers != cmdKey) { |
| 419 | + int key = convert_key(macKeyCode, macCharCodes); |
| 420 | + if (key != -1) gkbdputc(gkbdq, key); |
| 421 | + } else |
| 349 | 422 | result = eventNotHandledErr; |
| 350 | 423 | break; |
| 351 | | − } |
| 352 | 424 | default: |
| 353 | 425 | break; |
| 354 | 426 | } |
| @@ -359,35 +431,45 @@ if(0 && key != -1)fprint(2, "[%C]", key); · 4 unmodified lines |
| 359 | 431 | GetEventParameter(event, kEventParamMouseLocation, typeQDPoint, |
| 360 | 432 | 0, sizeof mousePos, 0, &mousePos); |
| 361 | 433 | |
| 362 | | − static uint32_t mousebuttons = 0; // bitmask of buttons currently down |
| 363 | | − |
| 364 | | − switch (kind) { |
| 365 | | − case kEventMouseWheelMoved: { |
| 366 | | − int32_t wheeldelta; |
| 367 | | − GetEventParameter(event,kEventParamMouseWheelDelta,typeLongInteger, |
| 368 | | − 0,sizeof(EventMouseButton), 0, &wheeldelta); |
| 369 | | − sendbuttons((int16_t)wheeldelta>0 ? 8 : 16, |
| 370 | | − mousePos.h - winRect.left, |
| 371 | | − mousePos.v - winRect.top); |
| 434 | + switch(kind) { |
| 435 | + case kEventMouseWheelMoved: |
| 436 | + { |
| 437 | + int32_t wheeldelta; |
| 438 | + GetEventParameter(event,kEventParamMouseWheelDelta,typeSInt32, |
| 439 | + 0,sizeof(wheeldelta), 0, &wheeldelta); |
| 440 | + mouseX = mousePos.h - winRect.left; |
| 441 | + mouseY = mousePos.v - winRect.top; |
| 442 | + sendbuttons(wheeldelta>0 ? 8 : 16, mouseX, mouseY); |
| 372 | 443 | break; |
| 373 | 444 | } |
| 374 | 445 | case kEventMouseUp: |
| 375 | | − case kEventMouseDown: { |
| 446 | + case kEventMouseDown: |
| 447 | + { |
| 376 | 448 | uint32_t buttons; |
| 377 | | − GetEventParameter(event, kEventParamMouseChord, |
| 378 | | − typeUInt32, 0, sizeof buttons, 0, &buttons); |
| 379 | | − mousebuttons = (buttons & 1) |
| 380 | | − | ((buttons & 2)<<1) |
| 381 | | − | ((buttons & 4)>>1); |
| 449 | + uint32_t modifiers; |
| 450 | + GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, |
| 451 | + 0, sizeof(modifiers), 0, &modifiers); |
| 452 | + GetEventParameter(event, kEventParamMouseChord, typeUInt32, |
| 453 | + 0, sizeof buttons, 0, &buttons); |
| 454 | + /* simulate other buttons via alt/apple key. like x11 */ |
| 455 | + if(modifiers & optionKey) { |
| 456 | + mousebuttons = ((buttons & 1) ? 2 : 0); |
| 457 | + altPressed = false; |
| 458 | + } else if(modifiers & cmdKey) |
| 459 | + mousebuttons = ((buttons & 1) ? 4 : 0); |
| 460 | + else |
| 461 | + mousebuttons = (buttons & 1); |
| 462 | + |
| 463 | + mousebuttons |= ((buttons & 2)<<1); |
| 464 | + mousebuttons |= ((buttons & 4)>>1); |
| 465 | + |
| 382 | 466 | } /* Fallthrough */ |
| 383 | 467 | case kEventMouseMoved: |
| 384 | | − case kEventMouseDragged: { |
| 385 | | − sendbuttons(mousebuttons, |
| 386 | | − mousePos.h - winRect.left, |
| 387 | | − mousePos.v - winRect.top); |
| 388 | | − } |
| 389 | | − break; |
| 390 | | − |
| 468 | + case kEventMouseDragged: |
| 469 | + mouseX = mousePos.h - winRect.left; |
| 470 | + mouseY = mousePos.v - winRect.top; |
| 471 | + sendbuttons(mousebuttons, mouseX, mouseY); |
| 472 | + break; |
| 391 | 473 | default: |
| 392 | 474 | result = eventNotHandledErr; |
| 393 | 475 | break; |
| @@ -396,6 +478,7 @@ if(0 && key != -1)fprint(2, "[%C]", key); · 2 unmodified lines |
| 396 | 478 | return result; |
| 397 | 479 | } |
| 398 | 480 | |
| 481 | + |
| 399 | 482 | //default window command handler (from menus) |
| 400 | 483 | static OSStatus |
| 401 | 484 | MainWindowCommandHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData) |
| @@ -406,12 +489,12 @@ MainWindowCommandHandler(EventHandlerCallRef nextHandler, EventRef event, void * · 4 unmodified lines |
| 406 | 489 | |
| 407 | 490 | result = CallNextEventHandler(nextHandler, event); |
| 408 | 491 | |
| 409 | | − if(class == kEventClassCommand){ |
| 492 | + if(class == kEventClassCommand) { |
| 410 | 493 | HICommand theHICommand; |
| 411 | 494 | GetEventParameter(event, kEventParamDirectObject, typeHICommand, |
| 412 | 495 | NULL, sizeof(HICommand), NULL, &theHICommand); |
| 413 | 496 | |
| 414 | | − switch(theHICommand.commandID){ |
| 497 | + switch(theHICommand.commandID) { |
| 415 | 498 | case kHICommandQuit: |
| 416 | 499 | cleanexit(0); |
| 417 | 500 | break; |
| @@ -424,7 +507,7 @@ MainWindowCommandHandler(EventHandlerCallRef nextHandler, EventRef event, void * · 6 unmodified lines |
| 424 | 507 | result = eventNotHandledErr; |
| 425 | 508 | break; |
| 426 | 509 | } |
| 427 | | − } else if(class == kEventClassWindow){ |
| 510 | + } else if(class == kEventClassWindow) { |
| 428 | 511 | WindowRef window; |
| 429 | 512 | _Rect rectPort = {0,0,0,0}; |
| 430 | 513 | |
| @@ -434,7 +517,7 @@ MainWindowCommandHandler(EventHandlerCallRef nextHandler, EventRef event, void * · 3 unmodified lines |
| 434 | 517 | if(window) |
| 435 | 518 | GetPortBounds(GetWindowPort(window), &rectPort); |
| 436 | 519 | |
| 437 | | − switch (kind){ |
| 520 | + switch(kind) { |
| 438 | 521 | case kEventWindowClosed: |
| 439 | 522 | theWindow = NULL; |
| 440 | 523 | cleanexit(0); // only one window |
| @@ -485,15 +568,16 @@ flushmemscreen(Rectangle r) · 44 unmodified lines |
| 485 | 568 | // Drawing the sub-image is relative to the window. |
| 486 | 569 | rbounds.origin.y = winRect.bottom - winRect.top - r.min.y - rbounds.size.height; |
| 487 | 570 | CGContextDrawImage(context, rbounds, subimg); |
| 488 | | − CGContextFlush(context); |
| 489 | 571 | CGImageRelease(subimg); |
| 490 | 572 | QDEndCGContext(GetWindowPort(theWindow), &context); |
| 573 | + |
| 574 | + needflush = true; |
| 491 | 575 | } |
| 492 | 576 | |
| 493 | 577 | uchar* |
| 494 | 578 | attachscreen(Rectangle *r, ulong *chan, int *depth, int *width, int *softscreen) |
| 495 | | −{ |
| 496 | | − if(!triedscreen){ |
| 579 | +{ |
| 580 | + if(!triedscreen) { |
| 497 | 581 | triedscreen = 1; |
| 498 | 582 | screeninit(); /* TO DO: call this elsewhere? */ |
| 499 | 583 | } |
| @@ -527,7 +611,6 @@ enum{ · 27 unmodified lines |
| 527 | 611 | SnarfSize= 100*1024 |
| 528 | 612 | }; |
| 529 | 613 | |
| 530 | | − |
| 531 | 614 | static char snarf[3*SnarfSize+1]; |
| 532 | 615 | static Rune rsnarf[SnarfSize+1]; |
| 533 | 616 | |