Skip to content

Commit

Permalink
Merge pull request #25 from umjammer/0.8.9
Browse files Browse the repository at this point in the history
0.8.9
  • Loading branch information
umjammer authored Mar 24, 2024
2 parents 66354ba + 2749f81 commit 7d1a90a
Show file tree
Hide file tree
Showing 14 changed files with 592 additions and 196 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.rococoa</groupId>
<artifactId>rococoa-parent</artifactId>
<version>0.8.8</version>
<version>0.8.9</version>
<packaging>pom</packaging>

<modules>
Expand Down
2 changes: 1 addition & 1 deletion rococoa-cocoa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<parent>
<groupId>org.rococoa</groupId>
<artifactId>rococoa-parent</artifactId>
<version>0.8.8</version>
<version>0.8.9</version>
</parent>

<name>Rococoa Cocoa Mappings</name>
Expand Down
382 changes: 259 additions & 123 deletions rococoa-cocoa/src/main/java/org/rococoa/carbon/CarbonCoreLibrary.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import vavi.util.Debug;
import vavi.util.StringUtil;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.rococoa.carbon.CarbonCoreLibrary.kTISPropertyUnicodeKeyLayoutData;
import static org.rococoa.carbon.CarbonCoreLibrary.library;

Expand All @@ -27,16 +28,18 @@ void test1() throws Exception {

@Test
void test2() throws Exception {
Pointer s = library.TISCopyCurrentKeyboardInputSource();
Pointer s = library.TISCopyCurrentKeyboardLayoutInputSource();
Debug.println("TISCopyCurrentKeyboardInputSource: " + s);
Pointer p = library.TISGetInputSourceProperty(s, kTISPropertyUnicodeKeyLayoutData);
Debug.println("TISGetInputSourceProperty: " + p); // TODO null
Debug.println("TISGetInputSourceProperty: " + p);
assertNotNull(p);
}

@Test
void test3() throws Exception {
PointerByReference pp = new PointerByReference(CarbonCoreLibrary.NATIVE_LIBRARY.getGlobalVariableAddress("kTISPropertyUnicodeKeyLayoutData"));
Pointer p = pp.getValue().getPointer(0);
assertNotNull(p);
Debug.printf("pointer: %s", p);
Debug.printf("pointer: %s", new CFStringRef(p));
byte[] bytes = new byte[32];
Expand All @@ -49,9 +52,10 @@ void test4() throws Exception {
PointerByReference pp = new PointerByReference(CarbonCoreLibrary.NATIVE_LIBRARY.getGlobalVariableAddress("kTISPropertyUnicodeKeyLayoutData"));
Pointer kTISPropertyUnicodeKeyLayoutData = pp.getValue().getPointer(0);
Debug.println("kTISPropertyUnicodeKeyLayoutData: " + kTISPropertyUnicodeKeyLayoutData);
Pointer s = library.TISCopyCurrentKeyboardInputSource();
Pointer s = library.TISCopyCurrentKeyboardLayoutInputSource();
Debug.println("TISCopyCurrentKeyboardInputSource: " + s);
Pointer p = library.TISGetInputSourceProperty(s, new CFStringRef(kTISPropertyUnicodeKeyLayoutData));
Debug.println("TISGetInputSourceProperty: " + p); // TODO null
Debug.println("TISGetInputSourceProperty: " + p);
assertNotNull(p);
}
}
2 changes: 1 addition & 1 deletion rococoa-contrib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<parent>
<groupId>org.rococoa</groupId>
<artifactId>rococoa-parent</artifactId>
<version>0.8.8</version>
<version>0.8.9</version>
</parent>

<name>Rococoa Contrib</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@

package org.rococoa.cocoa.coregraphics;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import com.sun.jna.Callback;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.platform.mac.CoreFoundation.CFArrayRef;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.NativeLongByReference;
import com.sun.jna.ptr.ShortByReference;
import org.rococoa.carbon.CarbonCoreLibrary;
import org.rococoa.cocoa.CFIndex;
import org.rococoa.cocoa.CGFloat;
Expand Down Expand Up @@ -188,6 +190,9 @@ interface CGRectEdge {

// enum CGEventFlags
long kCGEventFlagMaskCommand = NX_COMMANDMASK;
long kCGEventFlagMaskAlternate = NX_ALTERNATEMASK;
long kCGEventFlagMaskControl = NX_CONTROLMASK;
long kCGEventFlagMaskShift = NX_SHIFTMASK;

long kCGEventMaskForAllEvents = 0xFFFF_FFFF_FFFF_FFFFL;

Expand Down Expand Up @@ -262,7 +267,19 @@ interface CGEventTapCallBack extends Callback {
/** Returns a Quartz event source created with a specified source state. */
Pointer /* CGEventSourceRef */ CGEventSourceCreate(int /* CGEventSourceStateID */ stateID);

// CGKeyCode
int kCGMouseEventDeltaX = 4;
int kCGMouseEventDeltaY = 5;

/* Sets the integer value of a field in a Quartz event. */
void CGEventSetIntegerValueField(Pointer /* CGEventRef */ event, int /* CGEventField */ field, long value);

int kCGWindowListOptionOnScreenOnly = 1 << 0;
int kCGNullWindowID = 0;

/** Generates and returns information about the selected windows in the current user session. */
CFArrayRef CGWindowListCopyWindowInfo(int /* CGWindowListOption */ option, int /* CGWindowID */ relativeToWindow);

//#region CGKeyCode

/**
* Returns string representation of key, if it is printable.
Expand All @@ -271,10 +288,10 @@ interface CGEventTapCallBack extends Callback {
* @see "https://stackoverflow.com/a/1971027"
*/
private static CFStringRef createStringForKey(char /* CGKeyCode */ keyCode) {
Pointer /* TISInputSourceRef */ currentKeyboard = CarbonCoreLibrary.library.TISCopyCurrentKeyboardInputSource();
logger.fine("currentKeyboard: " + currentKeyboard);// + ", " + kTISPropertyUnicodeKeyLayoutData);
Pointer /* TISInputSourceRef */ currentKeyboard = CarbonCoreLibrary.library.TISCopyCurrentKeyboardLayoutInputSource(); // must be *Layout*
logger.finest("currentKeyboard: " + currentKeyboard);// + ", " + kTISPropertyUnicodeKeyLayoutData);
Pointer /* CFDataRef */ layoutData = CarbonCoreLibrary.library.TISGetInputSourceProperty(currentKeyboard, CFStringRef.toCFString("TISPropertyUnicodeKeyLayoutData"));
logger.finer("layoutData: " + layoutData);
logger.finest("layoutData: " + layoutData);
Pointer /* UCKeyboardLayout */ keyboardLayout = CoreFoundation.library.CFDataGetBytePtr(layoutData);

IntByReference keysDown = new IntByReference(0);
Expand All @@ -296,48 +313,37 @@ private static CFStringRef createStringForKey(char /* CGKeyCode */ keyCode) {
return CoreFoundation.library.CFStringCreateWithCharacters(kCFAllocatorDefault, chars, CFIndex.of(1));
}

/** key code, char map */
Map <String, Character> charToCodeDict = new HashMap<>(128);

/**
* Returns key code for given character via the above function, or Character.MAX_VALUE (UINT16_MAX)
* on error.
* Returns key code for given character via the above function.
*/
static char /* CGKeyCode */ keyCodeForChar(char c) {
Pointer /* CFMutableDictionaryRef */ charToCodeDict = null;
char[] /* UniChar */ character = new char[] { c };

// Generate table of keycodes and characters.
if (charToCodeDict == null) {
charToCodeDict = CoreFoundation.library.CFDictionaryCreateMutable(kCFAllocatorDefault,
new NativeLong(0), // must be 0 ???
CoreFoundation.library.kCFCopyStringDictionaryKeyCallBacks,
CoreFoundation.library.kCFTypeDictionaryValueCallBacks);
if (charToCodeDict == null) throw new IllegalStateException("cannot careate CFDictionaryCreateMutable");

if (charToCodeDict.isEmpty()) {
/* Loop through every keycode (0 - 127) to find its current mapping. */
for (char i = 0; i < 128; i++) {
CFStringRef string = createStringForKey(/* CGKeyCode */ i);
logger.finest("key: " + (int) i + ", 0x" + Integer.toHexString(i) + ", string: " + string + (string != null && !string.toString().isEmpty() ? ", 0x" + Integer.toHexString(string.toString().charAt(0)) : "null"));
if (string != null) {
IntByReference iRef = new IntByReference(i);
CoreFoundation.library.CFDictionaryAddValue(charToCodeDict, string, iRef.getPointer());
charToCodeDict.put(string.toString(), i);
CoreFoundation.library.CFRelease(string);
}
}
}

char[] /* UniChar */ character = new char[] { c };
CFStringRef charStr = CoreFoundation.library.CFStringCreateWithCharacters(kCFAllocatorDefault, character, CFIndex.of(1));

/* Our values may be NULL (0), so we need to use this function. */
char code;
ShortByReference /* CGKeyCode */ codeRef = new ShortByReference();
if (!CoreFoundation.library.CFDictionaryGetValueIfPresent(charToCodeDict, charStr.getPointer(), codeRef)) {
code = Character.MAX_VALUE;
} else {
code = (char) codeRef.getValue();
}

char code = charToCodeDict.getOrDefault(charStr.toString(), Character.MAX_VALUE);
CoreFoundation.library.CFRelease(charStr);

return code;
}

//#endregion

/** Returns a new Quartz keyboard event. */
Pointer /* CGEventRef */ CGEventCreateKeyboardEvent(Pointer /* CGEventSourceRef */ source, char /* CGKeyCode */ virtualKey, boolean keyDown);

Expand All @@ -353,6 +359,9 @@ private static CFStringRef createStringForKey(char /* CGKeyCode */ keyCode) {
// /** Returns a point with the specified coordinates. */
// /* inline */ CGPoint CGPointMake(CGFloat x, CGFloat y);

/** Sets the event type of a Quartz event (left mouse down, for example). */
void CGEventSetType(Pointer /* CGEventRef */ event, int /* CGEventType */ type);

// CGMouseButton
int kCGMouseButtonLeft = 0;
int kCGMouseButtonRight = 1;
Expand All @@ -373,4 +382,10 @@ private static CFStringRef createStringForKey(char /* CGKeyCode */ keyCode) {

/** Returns a new Quartz mouse event. */
Pointer /* CGEventRef */ CGEventCreateMouseEvent(Pointer /* CGEventSourceRef */ source, int /* CGEventType */ mouseType, CGPoint mouseCursorPosition, int /* CGMouseButton */ mouseButton);

int kCGScrollEventUnitPixel = 0;
int kCGScrollEventUnitLine = 1;

/** Returns a new Quartz scrolling event. */
Pointer /* CGEventRef */ CGEventCreateScrollWheelEvent2(Pointer /* CGEventSourceRef */ source, int /* CGScrollEventUnit */ units, int wheelCount, int wheel1, int wheel2, int wheel3);
}
Loading

0 comments on commit 7d1a90a

Please sign in to comment.