Skip to content

Commit

Permalink
get stuff to work on OSX
Browse files Browse the repository at this point in the history
  • Loading branch information
smiley22 committed Jun 11, 2019
1 parent 768170c commit 390e492
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
8 changes: 4 additions & 4 deletions BetterMouseYoke/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Additionally, a magenta text indicator is displayed in the upper-left corner of
</a>
</p>

While in mouse yoke mode, press and hold the left mouse button to switch to rudder mode. The cursor will change to a <img src="rudder-mode.png?raw=true"> symbol and the magenta text indicator will read *Mouse Rudder Control*. Two green little bars will appear to the left and to the right of the cursor that indicate the mouse movement range for full rudder deflection.
While in mouse yoke mode, press and hold the left mouse button to switch to rudder mode. The cursor will change to a <img src="rudder-mode.png?raw=true"> symbol and the magenta text indicator will read *Mouse Rudder Control*. Two green little bars will appear to the left and to the right of the cursor that indicate the mouse movement range for rudder deflection.

<p align="center">
<a href="image-2.png?raw=true" target="_blank">
Expand Down Expand Up @@ -63,11 +63,11 @@ set_pos = 1
# this to 0 to disable.
change_cursor = 1
# Defines the mouse movement range for rudder deflection in rudder mode.
# In other words, the distance between the two green vertical bars on the
# screen.
# In other words, the greater the value, the greater the distance between
# the two green vertical bars on the screen.
rudder_deflection_distance = 100
# The speed (in units per second) with which the rudder returns to the
# neutral position when exiting rudder mode.
# neutral position after exiting rudder mode.
# The default value of 2.0 means that it takes the rudder half a second
# to return to neutral from full left or right deflection.
rudder_return_speed = 2.0
Expand Down
36 changes: 22 additions & 14 deletions BetterMouseYoke/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ float loop_cb(float last_call, float last_loop, int count, void *ref) {
return 0;
}
int m_x, m_y;
XPLMGetMouseLocationGlobal(&m_x, &m_y);
get_cursor_pos(&m_x, &m_y);
if (controlling_rudder(&m_x, &m_y)) {
int dist = min(max(m_x - cursor_pos[0], -rudder_defl_dist),
rudder_defl_dist);
Expand Down Expand Up @@ -278,8 +278,7 @@ int left_mouse_down() {
/* Most significant bit is set if button is being held. */
return GetAsyncKeyState(VK_LBUTTON) >> 15;
#elif APL
/* FIXME: Not sure if we can always call this or if it's only valid in
the context of an event? */
/* Apparently you can use this also outside of the context of an event. */
return CGEventSourceButtonState(
kCGEventSourceStateCombinedSessionState, kCGMouseButtonLeft);
#endif
Expand Down Expand Up @@ -310,6 +309,21 @@ int controlling_rudder(int *x, int *y) {
return rudder_control;
}

void get_cursor_pos(int *x, int *y) {
#ifdef APL
/* On OSX, XPLMGetMouseLocationGlobal still returns old cursor location after
setting its position for whatever reason, so we query the cursor position
ourselves. */
CGEventRef ev = CGEventCreate(NULL);
CGPoint pt = CGEventGetLocation(ev);
CFRelease(ev);
*x = pt.x;
*y = screen_height - pt.y;
#else
XPLMGetMouseLocationGlobal(x, y);
#endif
}

void set_cursor_from_yoke() {
set_cursor_pos(
0.5 * screen_width * (XPLMGetDataf(yoke_roll_ratio) + 1),
Expand All @@ -327,21 +341,15 @@ void set_cursor_pos(int x, int y) {
ClientToScreen(xp_hwnd, &pt);
SetCursorPos(pt.x, pt.y);
#elif APL
/* FIXME: need to test if this actually works */
CGPoint pt = {
.x = x,
.y = screen_height - y
};
CGWarpMouseCursorPosition(pt);
/*
CGWarpMouseCursorPosition(CGPoint p)
Moves the mouse cursor to a specified point relative to the
display origin (the upper-left corner of the display).
CGDisplayMoveCursorToPoint(CGDirectDisplayID id, CGPoint p)
- CGMainDisplayID()
- kCGDirectMainDisplay
*/
/* CGWarpMouseCursorPosition and CGDisplayMoveCursorToPoint don't generate a mouse
movement event so they're not a good fit here. */
CGEventRef ev = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved, pt, 0);
CGEventPost(kCGHIDEventTap, ev);
CFRelease(ev);
#endif
}

Expand Down
1 change: 1 addition & 0 deletions BetterMouseYoke/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ typedef enum {
int toggle_yoke_control_cb(XPLMCommandRef cmd, XPLMCommandPhase phase, void *ref);
int draw_cb(XPLMDrawingPhase phase, int before, void *ref);
float loop_cb(float last_call, float last_loop, int count, void *ref);
void get_cursor_pos(int *x, int *y);
void set_cursor_from_yoke();
void set_cursor_pos(int x, int y);
void set_cursor_bmp(cursor_t cursor);
Expand Down
6 changes: 4 additions & 2 deletions Util/ini.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ void ini_gets(const char *name, char *buf, int size, const char *def) {
GetPrivateProfileStringA(INI_SECT_NAME, name, def, buf, size, f);
#else
FILE *fp = fopen(f, "r");
if (!fp)
return 0;
if (!fp) {
strcpy(buf, def);
return;
}
char line[128];
while (fgets(line, sizeof(line), fp)) {
char *p = line;
Expand Down

0 comments on commit 390e492

Please sign in to comment.