Igor Delovski Board Forum Index Igor Delovski Board
My Own Personal Slashdot!
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

NSEvent & NSRunLoop

 
Post new topic   Reply to topic    Igor Delovski Board Forum Index -> Mac Cocoa
Mac Cocoa  
Author Message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3031
Location: Europe

PostPosted: Wed Sep 22, 2021 5:37 pm    Post subject: NSEvent & NSRunLoop Reply with quote

so - Cocoa: how to run a modal window while performing a background task?

"... if you must use a modal dialog, you can make the main run loop run by
giving it some time while the modal dialog is open:"

Code:
NSModalSession session = [NSApp beginModalSessionForWindow:[self window]];
int result = NSRunContinuesResponse;

while (result == NSRunContinuesResponse)
{
    //run the modal session
    //once the modal window finishes, it will return a different result and break out of the loop
    result = [NSApp runModalSession:session];

    //this gives the main run loop some time so your other code processes
    [[NSRunLoop currentRunLoop] limitDateForMode:NSDefaultRunLoopMode];

    //do some other non-intensive task if necessary
}

[NSApp endModalSession:session];


edit - see: https://github.com/wxWidgets/wxWidgets/blob/1c160557132f76423dbc0acb3a01b1f64f016dd1/src/osx/cocoa/evtloop.mm#L205

Code:
NSInteger response = [NSApp runModalSession:(NSModalSession)m_modalSession];


Last edited by Ike on Sun Jul 02, 2023 9:58 pm; edited 1 time in total
Back to top
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3031
Location: Europe

PostPosted: Wed Sep 22, 2021 5:39 pm    Post subject: Reply with quote

so - Best way to make NSRunLoop wait for a flag to be set?

Code:
NSDate *loopUntil = [NSDate dateWithTimeIntervalSinceNow:0.1];
while (webViewIsLoading && [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate:loopUntil])
  loopUntil = [NSDate dateWithTimeIntervalSinceNow:0.1];


"With your distantFuture timeout, the runloop will wait foreever until it
processes an event. So when it returns to you, it has just processed an event
of some kind."
Back to top
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3031
Location: Europe

PostPosted: Thu Sep 23, 2021 12:45 pm    Post subject: Reply with quote

so - What's the modern equivalent of GetNextEvent in Cocoa?

"I suppose the nearest thing to a Cocoa equivalent would be -[NSApplication
nextEventMatchingMask:untilDate:inMode:dequeue:].

The preferred way of handing events is the don't call us, we'll call you
scheme, where the app calls NSApplicationMain or RunApplicationEventLoop
and events are dispatched to you."
Back to top
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3031
Location: Europe

PostPosted: Thu Sep 23, 2021 12:54 pm    Post subject: Reply with quote

so - Cocoa message loop? (vs. windows message loop)

Code:
bool quit = false;

while (!quit)   {
   NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:nil inMode:NSDefaultRunLoopMode dequeue:YES];
   switch([(NSEvent *)event type])  {
      case NSKeyDown:
         quit = true;
         break;
      default:
         [NSApp sendEvent:event];
         break;
   }
   [event release];
}


"But beware... check out the Activity Monitor. You will see that your
application is using 100% CPU.

...

You could use a Core Foundation runloop instead, and have event sources
for your threads so that the run loop will wake up and call processEvents()
methods. This is an improvement over your polling code, since the run loop
will let the thread sleep if there are no events waiting.

See CFRunLoopSourceCreate(), CFRunLoopSourceSignal() and CFRunLoop-
WakeUp() for more information."
Back to top
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3031
Location: Europe

PostPosted: Sat Oct 09, 2021 8:20 pm    Post subject: Reply with quote

Handmade Hero - Main game loop on OS X

"So my question is: How do you properly create a game run loop on OS X?

I found 'the windows way' on OS X online and merged it with my code."
Back to top
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3031
Location: Europe

PostPosted: Fri Oct 15, 2021 8:40 pm    Post subject: Reply with quote

so - Run loop doesn't proceed events

"You are assuming that running the NSRunLoop in the NSDefaultRunMode
processes user input events such as key presses or mouse clicks. I don't think
that's the case."
Back to top
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3031
Location: Europe

PostPosted: Wed Oct 27, 2021 7:28 am    Post subject: Reply with quote

gitlab - 6502js But C

"For programmers, the OS layer contains an example on how to use X11,
Cocoa/Quartz2D or Win32 to draw stuff without a library like SDL2. See for
programmers for more info.

windows.c, linux.c, and mac6502/mac6502/mac.m contain working examples
of how to create a window, receive user input and draw rects using Win32, X11
(via XCB/XKBCommon) and Cocoa/Quartz2D.

If you need, for example, a working example of how to use Cocoa/Quartz2D
with a custom game loop, how to get input via XCB/X11 and XKBCommon, or
how to deal with multiple OSes in general, I hope my code can help you out,
because that information was hard to find."
Back to top
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3031
Location: Europe

PostPosted: Sun Dec 05, 2021 1:12 pm    Post subject: Reply with quote

adc - Handling Mouse Events

"Mouse events are dispatched by an NSWindow object to the NSView object
over which the event occurred.
Subsequent key events are dispatched to that view object if it accepts first
responder status."


Example: Cropped Image
Back to top
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3031
Location: Europe

PostPosted: Tue Sep 27, 2022 10:17 pm    Post subject: Reply with quote

adc - startPeriodicEventsAfterDelay:withPeriod:

"This method is typically used in a modal loop while tracking mouse-dragged
events.
+stopPeriodicEvents - Stops generating periodic events for the current thread
and discards any periodic events remaining in the queue."
Back to top
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3031
Location: Europe

PostPosted: Mon May 15, 2023 6:20 pm    Post subject: Reply with quote

fltk.org - Fast Light Toolkit

"FLTK (pronounced 'fulltick') is a cross-platform C++ GUI toolkit for
UNIX/Linux (X11), Microsoft Windows, and MacOS X. FLTK provides
modern GUI functionality without the bloat and supports 3D graphics
via OpenGL and its built-in GLUT emulation."


https://github.com/fltk/fltk/blob/master/src/Fl_cocoa.mm

Code:
- (BOOL)process_keydown:(NSEvent*)theEvent;
- (id)initWithFrame:(NSRect)frameRect;
- (void)drawRect:(NSRect)rect;
- (BOOL)acceptsFirstResponder;
- (BOOL)acceptsFirstMouse:(NSEvent*)theEvent;
- (void)resetCursorRects;
- (BOOL)performKeyEquivalent:(NSEvent*)theEvent;
- (void)mouseUp:(NSEvent *)theEvent;
- (void)rightMouseUp:(NSEvent *)theEvent;
- (void)otherMouseUp:(NSEvent *)theEvent;
- (void)mouseDown:(NSEvent *)theEvent;
- (void)rightMouseDown:(NSEvent *)theEvent;
- (void)otherMouseDown:(NSEvent *)theEvent;
- (void)mouseMoved:(NSEvent *)theEvent;
- (void)mouseEntered:(NSEvent *)theEvent;
- (void)mouseExited:(NSEvent *)theEvent;
- (void)mouseDragged:(NSEvent *)theEvent;
- (void)rightMouseDragged:(NSEvent *)theEvent;
- (void)otherMouseDragged:(NSEvent *)theEvent;
- (void)scrollWheel:(NSEvent *)theEvent;
- (void)magnifyWithEvent:(NSEvent *)theEvent;
- (void)keyDown:(NSEvent *)theEvent;
- (void)keyUp:(NSEvent *)theEvent;
- (void)flagsChanged:(NSEvent *)theEvent;
Back to top
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3031
Location: Europe

PostPosted: Mon May 15, 2023 6:44 pm    Post subject: Reply with quote

tcltk/tk - macosx

https://github.com/tcltk/tk/blob/main/macosx/tkMacOSXWindowEvent.c

Code:
/*
 * This keyDown method does nothing, which is a huge improvement over the
 * default keyDown method which beeps every time a key is pressed.
 */

- (void) keyDown: (NSEvent *) theEvent
{
    (void)theEvent;

#ifdef TK_MAC_DEBUG_EVENTS
    TKLog(@"-[%@(%p) %s] %@", [self class], self, sel_getName(_cmd), theEvent);
#endif
}


https://github.com/tcltk/tk/blob/main/macosx/tkMacOSXKeyEvent.c

Code:
if (modifiers) {
   state = (modifiers & NSAlphaShiftKeyMask ? LockMask    : 0) |
           (modifiers & NSShiftKeyMask      ? ShiftMask   : 0) |
           (modifiers & NSControlKeyMask    ? ControlMask : 0) |
           (modifiers & NSCommandKeyMask    ? Mod1Mask    : 0) |
           (modifiers & NSAlternateKeyMask  ? Mod2Mask    : 0) |
           (modifiers & NSNumericPadKeyMask ? Mod3Mask    : 0) |
           (modifiers & NSFunctionKeyMask   ? Mod4Mask    : 0) ;
    }
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Igor Delovski Board Forum Index -> Mac Cocoa All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Delovski.hr
Powered by php-B.B. © 2001, 2005 php-B.B. Group