Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get events from OS

I work on windows but I am stuck here on Mac. I have the Canon SDK and have built a JNA wrapper over it. It works well on windows and need some help with Mac. In the sdk, there is a function where one can register a callback function. Basically when an event occurs in camera, it calls the callback function.

On windows, after registering, I need to use User32 to get the event and to dispatch the event by:

private static final User32 lib = User32.INSTANCE;
boolean hasMessage = lib.PeekMessage( msg, null, 0, 0, 1 ); // peek and remove
if( hasMessage ){
    lib.TranslateMessage( msg ); 
    lib.DispatchMessage( msg ); //message gets dispatched and hence the callback function is called
}

In the api, I do not find a similar class in Mac. How do I go about this one??

PS: The JNA api for unix is extensive and I could not figure out what to look for. The reference might help

like image 912
Jatin Avatar asked Mar 05 '13 05:03

Jatin


1 Answers

This solution is using the Cocoa framework. Cocoa is deprecated and I am not aware of any other alternative solution. But the below works like charm.

Finally I found the solution using Carbon framework. Here is my MCarbon interface which defines calls I need.

  public interface MCarbon extends Library {
  MCarbon INSTANCE = (MCarbon) Native.loadLibrary("Carbon", MCarbon.class);
  Pointer GetCurrentEventQueue();
  int SendEventToEventTarget(Pointer inEvent, Pointer intarget);
  int RemoveEventFromQueue(Pointer inQueue, Pointer inEvent);
  void ReleaseEvent(Pointer inEvent);
  Pointer AcquireFirstMatchingEventInQueue(Pointer inQueue,NativeLong inNumTypes,EventTypeSpec[] inList, NativeLong inOptions);
  //... so on
  }

The solution to the problem is solved using the below function:

 NativeLong ReceiveNextEvent(NativeLong inNumTypes, EventTypeSpec[] inList, double inTimeout, byte inPullEvent, Pointer outEvent);

This does the job. As per documentation -

This routine tries to fetch the next event of a specified type.
If no events in the event queue match, this routine will run the
current event loop until an event that matches arrives, or the
timeout expires. Except for timers firing, your application is
blocked waiting for events to arrive when inside this function.

Also if not ReceiveNextEvent, then other functions as mentioned in MCarbon class above would be useful.

I think Carbon framework documentation would give more insights and flexibilities to solve the problem. Apart from Carbon, in forums people have mentioned about solving using Cocoa, but none I am aware of.

Edit: Thanks to technomarge, more information here

like image 185
Jatin Avatar answered Nov 13 '22 21:11

Jatin