1 message in net.java.dev.jna.users[jna-users] JNA mappings for Mac OS-X...
FromSent OnAttachments
Alan SnyderJan 9, 2009 1:28 pm 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:[jna-users] JNA mappings for Mac OS-X - regestering a callback.Actions...
From:Alan Snyder (ala@tidebreaksys.com)
Date:Jan 9, 2009 1:28:11 pm
List:net.java.dev.jna.users

Does JNA have a set of definitions for the Mac OS X API, like it does for Win32 API?

I've been working on a project, and end up using Pointer for many of the Mac definitions, but it isn't quite working yet. Does anyone have know a better set of definitions for some of the APIs mentioned below.

Also what would be a good approach to construct a library like below.

---------------------------------------------------------------------

NOTE: Mac definitions are in /* */ and Java is below it.

public interface CGCarbon extends Library { CGCarbon INSTANCE = (CGCarbon) Native.loadLibrary("Carbon", CGCarbon.class);

/* CFMachPortRef CGEventTapCreate ( CGEventTapLocation tap, CGEventTapPlacement place, CGEventTapOptions options, CGEventMask eventsOfInterest, CGEventTapCallBack callback, void *refcon ); */ Pointer CGEventTapCreate(int tap, int place, int options, long eventsOfInterest, CGEventTapCallBack callback, Pointer refcon );

/* CGEventTapCallBack */ /* typedef CGEventRef (*CGEventTapCallBack) ( CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon ); */ public interface CGEventTapCallBack extends Callback { public Pointer callback(Pointer proxy, int type, Pointer event, Pointer refcon); }

/* CGEventGetIntegerValueField */ /* int64_t CGEventGetIntegerValueField ( CGEventRef event, CGEventField field ); */ long CGEventGetIntegerValueField(Pointer event,int field);

/* CFMackPortCreateRunLoopSource() */ /* CFRunLoopSourceRef CFMachPortCreateRunLoopSource( CFAllocatorRef allocator, CFMachPortRef port, CFIndex order ); */ Pointer CFMachPortCreateRunLoopSource( Pointer alwaysNull, Pointer machPortRef, int order);

/* CFRunLoopAddSource() */ /* void CFRunLoopAddSource( CFRunLoopRef rl, CFRunLoopSourceRef source, CFStringRef mode ); */ void CFRunLoopAddSource(Pointer rl, Pointer source, int mode);

/* CFRunLoopAddRemoveSource() */ /*void CFRunLoopRemoveSource ( CFRunLoopRef rl, CFRunLoopSourceRef source, CFStringRef mode ); */ void CFRunLoopRemoveSource(Pointer rl, Pointer source, int mode);

/* CFRunLoopGetCurrent */ /* CFRunLoopRef CFRunLoopGetCurrent() */ Pointer CFRunLoopGetCurrent();

final int kCGEventNull = 0; final int kCGEventLeftMouseDown = 1; final int kCGEventLeftMouseUp = 2; final int kCGEventRightMouseDown = 3; final int kCGEventRightMouseUp = 4; final int kCGEventMouseMoved = 5; final int kCGEventLeftMouseDragged = 6; final int kCGEventRightMouseDragged = 7;

final int kCGEventScrollWheel = 22;

//integer constants below here. final int kCGEventKeyDown = 10; final int kCGEventKeyUp = 11; final int kCGEventFlagsChanged = 12;

}

----------------------- Below is an example that works in C code.

-----------------

---- snip ---- //register the callback. CFMachPortRef portRef = CGEventTapCreate( kCGAnnotatedSessionEventTap, kCGHeadInsertEventTap, 0, mask, globalEventTapCallback, NULL ); cfrls = CFMachPortCreateRunLoopSource(NULL, portRef, 0); CFRunLoopAddSource(CFRunLoopGetCurrent(), cfrls, kCFRunLoopCommonModes); ---- snip ----

//the callback static CGEventRef globalEventTapCallback( CGEventTapProxy proxy, CGEventType type, CGEventRef inEvent, void* inRefcon ) { int64_t keycode = CGEventGetIntegerValueField(inEvent,kCGKeyboardEventKeycode); int64_t keyAutoRepeat = CGEventGetIntegerValueField(inEvent,kCGKeyboardEventAutorepeat); printf("key=0x%x, type=0x%x, code=0x%x, autorepeat=0x%x\n",keyId,type,keycode,keyAutoRepeat); keyId++; if(bSuppress){ return NULL; } return inEvent; }//globalEventTapCallback

----------------------------- But the JNA translation using the Library above does not -------------------------

---- snip ---- CGEventTapCallBack keyCallback = new KeyCallback();

Pointer portRef = lib.CGEventTapCreate(lib.kCGAnnotatedSessionEventTap, lib.kCGHeadInsertEventTap, 0, eventsOfInterest, keyCallback, Pointer.NULL);

Pointer cfrls = lib.CFMachPortCreateRunLoopSource(Pointer.NULL, portRef,0); lib.CFRunLoopAddSource( lib.CFRunLoopGetCurrent() , cfrls, lib.kCFRunLoopCommonModes ); ---- snip ----

public class KeyCallback implements CGEventTapCallBack{

public Pointer callback(Pointer proxy, int type, Pointer event, Pointer refcon){

long code = lib.CGEventGetIntegerValueField(event, type);

log.info("osx-callback code="+code);

if( state.isSuppressed() ){ return null; }else{ return event; } } }//class CallbackListener