Skip to content

Commit 9b0c9e2

Browse files
committed
First attempts to adding linux support. It's broken.
1 parent 19c0be9 commit 9b0c9e2

File tree

11 files changed

+1217
-35
lines changed

11 files changed

+1217
-35
lines changed

FingerControl.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@
44

55

66
import math
7-
import sys
8-
if sys.platform == "darwin":
9-
import OSX.Leap as Leap
10-
else:
11-
import Windows.Leap as Leap
12-
import Geometry
7+
from leap import Leap, Mouse
138
from MiscFunctions import *
149

1510

Geometry.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33

44

55
import math
6-
import sys
7-
if sys.platform == "darwin":
8-
import OSX.Leap as Leap
9-
else:
10-
import Windows.Leap as Leap
6+
from leap import Leap
117

128

139
def to_vector(leap_vector):

Linux/Leap.py

Lines changed: 1074 additions & 0 deletions
Large diffs are not rendered by default.

Linux/LeapPython.so

8.3 MB
Binary file not shown.

Linux/Mouse.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
from pymouse import PyMouse
2+
mouse = PyMouse()
3+
4+
def AbsoluteMouseMove(posx,posy):
5+
print 'move to', posx, posy
6+
mouse.move(int(posx), int(posy))
7+
8+
def AbsoluteMouseClick(posx,posy):
9+
print 'click on ', posx, posy
10+
mouse.click(posx, posy)
11+
12+
def AbsoluteMouseClickDown(posx, posy):
13+
pass
14+
15+
def AbsoluteMouseClickUp(posx, posy):
16+
pass
17+
18+
def AbsoluteMouseDrag(posx, posy): #Only relevant in OS X(?)
19+
mouse.move(posx, posy)
20+
21+
def AbsoluteMouseRightClick(posx,posy):
22+
mouse.click(posx, posy, button=2)
23+
24+
def RelativeMouseScroll(x_movement, y_movement): #Movements should be no larger than +- 10
25+
mouse.click(x_movement, y_movement, button=3)
26+
27+
def GetDisplayWidth():
28+
return mouse.screen_size()[0]
29+
30+
def GetDisplayHeight():
31+
return mouse.screen_size()[1]
32+
33+
34+
#A cursor that does commands based on absolute position (good for finger pointing)
35+
class absolute_cursor(object):
36+
def __init__(self):
37+
self.x_max = GetDisplayWidth() - 1
38+
self.y_max = GetDisplayHeight() - 1
39+
self.left_button_pressed = False
40+
self.x = 0
41+
self.y = 0
42+
43+
def move(self, posx, posy): #Move to coordinates
44+
self.x = posx
45+
self.y = posy
46+
if self.x > self.x_max:
47+
self.x = self.x_max
48+
if self.y > self.y_max:
49+
self.y = self.y_max
50+
if self.x < 0.0:
51+
self.x = 0.0
52+
if self.y < 0.0:
53+
self.y = 0.0
54+
if self.left_button_pressed: #We are dragging
55+
AbsoluteMouseDrag(self.x, self.y)
56+
else: #We are not dragging
57+
AbsoluteMouseMove(self.x, self.y)
58+
59+
def click(self, posx=None, posy=None): #Click at coordinates (current coordinates by default)
60+
if posx == None:
61+
posx = self.x
62+
if posy == None:
63+
posy = self.y
64+
AbsoluteMouseClick(posx, posy)
65+
66+
def set_left_button_pressed(self, boolean_button): #Set the state of the left button
67+
if boolean_button == True: #Pressed
68+
self.click_down()
69+
else: #Not pressed
70+
self.click_up()
71+
72+
def click_down(self, posx=None, posy=None):
73+
if posx == None:
74+
posx = self.x
75+
if posy == None:
76+
posy = self.y
77+
AbsoluteMouseClickDown(posx, posy)
78+
self.left_button_pressed = True
79+
80+
def click_up(self, posx=None, posy=None):
81+
if posx == None:
82+
posx = self.x
83+
if posy == None:
84+
posy = self.y
85+
AbsoluteMouseClickUp(posx, posy)
86+
self.left_button_pressed = False
87+
88+
def rightClick(self, posx=None, posy=None):
89+
if posx == None:
90+
posx = self.x
91+
if posy == None:
92+
posy = self.y
93+
AbsoluteMouseRightClick(posx, posy)
94+
95+
def scroll(self, x_movement, y_movement):
96+
RelativeMouseScroll(x_movement, y_movement)
97+
98+
99+
#Allows for relative movement instead of absolute movement. This implementation is not a "true" relative mouse,
100+
#but is really just a relative wrapper for an absolute mouse. Not the best way to do it, but I need to
101+
#figure out how to send raw "mouse moved _this amount_" events. This class is (as of writing) untested.
102+
#It's only here in case someone else wants to figure out how to do this properly on OS X.
103+
#I will be "actually" implementing this on Windows shortly. OSX TBD.
104+
class relative_cursor(absolute_cursor):
105+
def __init__(self):
106+
absolute_cursor.__init__(self)
107+
108+
def move(self, x_amt, y_amt):
109+
self.x = self.x + x_amt
110+
self.y = self.y + y_amt
111+
if self.x > self.x_max:
112+
self.x = self.x_max
113+
if self.y > self.y_max:
114+
self.y = self.y_max
115+
if self.x < 0.0:
116+
self.x = 0.0
117+
if self.y < 0.0:
118+
self.y = 0.0
119+
if self.left_button_pressed: #We are dragging
120+
AbsoluteMouseDrag(self.x, self.y)
121+
else: #We are not dragging
122+
AbsoluteMouseMove(self.x, self.y)

Linux/__init__.py

Whitespace-only changes.

Linux/libLeap.so

2.59 MB
Binary file not shown.

MiscFunctions.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@
44

55

66
import math
7-
import sys
8-
if sys.platform == "darwin":
9-
import OSX.Leap as Leap
10-
else:
11-
import Windows.Leap as Leap
7+
from leap import Leap
128
import Geometry
139

1410
#Smooths the mouse's position
15-
class mouse_position_smoother(object):
11+
class mouse_position_smoother(object):
1612
def __init__(self, smooth_aggressiveness, smooth_falloff):
1713
#Input validation
1814
if smooth_aggressiveness < 1:
@@ -80,7 +76,7 @@ def signal(self, signal_value):
8076
if self.state_counters[i] < 0: self.state_counters[i] = 0
8177
if self.state_counters[i] >= self.debounce_time: #Confirmed new state at index i
8278
self.state_counters[i] = self.debounce_time
83-
for x in range(0,len(self.state_counters)):
79+
for x in range(0,len(self.state_counters)):
8480
if x is not i: self.state_counters[x] = 0 #Zero out all other state counters
8581
self.state = i #Save the new state
8682
return self.state

PalmControl.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44

55

66
import math
7-
import sys
8-
if sys.platform == "darwin":
9-
import OSX.Leap as Leap
10-
else:
11-
import Windows.Leap as Leap
7+
from leap import Leap, Mouse
128
import Geometry
139
from MiscFunctions import *
1410

@@ -42,7 +38,7 @@ def on_frame(self, controller):
4238
rightmost_hand = max(frame.hands, key=lambda hand: hand.palm_position.x) #Get rightmost hand
4339
leftmost_hand = min(frame.hands, key=lambda hand: hand.palm_position.x) #Get leftmost hand
4440
self.do_gesture_recognition(leftmost_hand, rightmost_hand) #This will run with >1 hands in frame
45-
41+
4642
def do_mouse_stuff(self, hand): #Take a hand and use it as a mouse
4743
hand_normal_direction = Geometry.to_vector(hand.palm_normal)
4844
hand_direction = Geometry.to_vector(hand.direction)
@@ -58,7 +54,7 @@ def do_gesture_recognition(self, gesture_hand, mouse_hand):
5854
if len(gesture_hand.fingers) == 2: #Two open fingers on gesture hand (scroll mode)
5955
self.gesture_debouncer.signal(2) #Tell the debouncer we've seen this gesture
6056
elif len(gesture_hand.fingers) == 1: #One open finger on gesture hand (click down)
61-
self.gesture_debouncer.signal(1)
57+
self.gesture_debouncer.signal(1)
6258
else: #No open fingers or 3+ open fingers (click up/no action)
6359
self.gesture_debouncer.signal(0)
6460
#Now that we've told the debouncer what we *think* the current gesture is, we must act
@@ -72,7 +68,7 @@ def do_gesture_recognition(self, gesture_hand, mouse_hand):
7268
self.do_mouse_stuff(mouse_hand) #We may want to click and drag
7369
elif self.gesture_debouncer.state == 0: #Move cursor mode
7470
if self.cursor.left_button_pressed: self.cursor.click_up() #Click up (if needed)
75-
self.do_mouse_stuff(mouse_hand)
71+
self.do_mouse_stuff(mouse_hand)
7672

7773
def velocity_to_scroll_amount(self, velocity): #Converts a finger velocity to a scroll velocity
7874
#The following algorithm was designed to reflect what I think is a comfortable

PyLeapMouse.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
#William Yager
22
#Leap Python mouse controller POC
3-
4-
53
import sys
6-
if sys.platform == "darwin":
7-
import OSX.Leap as Leap
8-
import OSX.Mouse as Mouse
9-
else:
10-
import Windows.Leap as Leap
11-
import Windows.Mouse as Mouse
4+
from leap import Leap, Mouse
125
from PalmControl import Palm_Control_Listener #For palm-tilt based control
136
from FingerControl import Finger_Control_Listener #For finger-pointing control
147

@@ -63,4 +56,4 @@ def main():
6356
#Remove the sample listener when done
6457
controller.remove_listener(listener)
6558

66-
main()
59+
main()

leap.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import sys
2+
if sys.platform == "darwin":
3+
import OSX.Leap as Leap
4+
import OSX.Mouse as Mouse
5+
elif 'linux' in sys.platform:
6+
import Linux.Leap as Leap
7+
import Linux.Mouse as Mouse
8+
else:
9+
import Windows.Leap as Leap
10+
import Windows.Mouse as Mouse

0 commit comments

Comments
 (0)