Skip to content

Commit

Permalink
Added TouchPad area
Browse files Browse the repository at this point in the history
  • Loading branch information
pardo committed Oct 23, 2016
1 parent e5a845a commit f105f63
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 33 deletions.
36 changes: 30 additions & 6 deletions pysenteishon/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
MACOS_CMD = "/usr/bin/osascript -e 'tell application \"System Events\" to key code {}'"
else:
from pykeyboard import PyKeyboard
k = PyKeyboard()
keyboard = PyKeyboard()
from pymouse import PyMouse
mouse = PyMouse()

DEFAULT_PORT = 5000

Expand All @@ -25,6 +27,28 @@ def index(self):
"""Redirect to index.html"""
raise cherrypy.HTTPRedirect("/index.html")

@cherrypy.expose
def mouse_move(self, offset_x=0, offset_y=0, *args, **kwargs):
if ON_MACOS:
#TODO find out how to do this
pass
else:
x, y = mouse.position()
mouse.move(
x+int(offset_x),
y+int(offset_y)
)
return ""

@cherrypy.expose
def click(self, *args, **kwargs):
if ON_MACOS:
#TODO find out how to do this
pass
else:
mouse.click(*mouse.position())
return ""

@cherrypy.expose
@cherrypy.tools.json_out()
def press(self, key=None, *args, **kwargs):
Expand All @@ -49,7 +73,7 @@ def press(self, key=None, *args, **kwargs):
if ON_MACOS:
subprocess.call(MACOS_CMD.format(key_to_tap), shell=True)
else:
k.tap_key(key_to_tap)
keyboard.tap_key(key_to_tap)
return {"key-pressed": True, "key": key}
return {"key-pressed": False, "key": None}

Expand Down Expand Up @@ -80,10 +104,10 @@ def get_key(self, path):
}
else:
keys = {
'up': k.up_key,
'down': k.down_key,
'left': k.left_key,
'right': k.right_key,
'up': keyboard.up_key,
'down': keyboard.down_key,
'left': keyboard.left_key,
'right': keyboard.right_key,
}
return keys.get(path)

Expand Down
14 changes: 10 additions & 4 deletions pysenteishon/static/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,14 @@ body {
color: #FFE480;
}

.display {
width: 80%;
background: white;
color: black;
.toucharea{
height: 50vh;
max-height: 50vw;
background: repeating-linear-gradient(
-45deg,
rgba(34, 34, 34, 0.15),
rgba(34, 34, 34, 0.15) 10px,
#5784ab 10px,
#5784ab 20px
);
}
15 changes: 11 additions & 4 deletions pysenteishon/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,20 @@ <h4 class="modal-title" id="network-settings-modal-label"><i class="fa fa-wifi">

</div>

<div class="row box">
<div class="boox col-xs-12">
<input type="button" id="toggle" class="display btn-primary" value="Keep display ON (click me)" />
<div id="touchpad">
<div class="row box">
<div class="col-xs-12">
<p>Touchpad</p>
<div class="toucharea"></div>
</div>
</div>
</div>


<div class="row">
<div class="box col-xs-12">
<input type="button" id="toggle" class="btn btn-default" value="Keep screen ON (click me)" />
</div>
</div>

</div>

Expand Down
102 changes: 83 additions & 19 deletions pysenteishon/static/js/pysenteishon.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,40 +120,104 @@ function NetworkInfo ($el) {

function Swipe ($el) {
this.$el = $el;
this.doDoing = false;

this.do = function(key) {press(key)};

this.$el.swipe({
swipe: function(event, direction, distance, duration, fingerCount, fingerData) {
// FIXME: "this.do" doesn't work here and it should :)
press(direction);
},
fingers: 1,
fingers: 1
});
}

function TouchPad ($touchpad) {
var promise = $.Deferred().promise();
function mouseMove(offset_x, offset_y){
if (this.moving) {
return promise
}
this.moving = true;
return $.ajax({
url: "/mouse_move",
data: {
"offset_x": parseInt(offset_x),
"offset_y": parseInt(offset_y)
},
cache: false
}).always(function () {
this.moving = false;
}.bind(this));
}

function mouseClick (){
if (this.doing) { return }
this.doing = true;
return $.ajax({
url: "/click",
cache: false
}).always(function () {
this.doing = false;
}.bind(this));
}

var start_posx = 0;
var start_posy = 0;
var current_posx = 0;
var current_posy = 0;

$touchpad.on("touchstart", function(e){
start_posx = e.originalEvent.touches[0].pageX;
start_posy = e.originalEvent.touches[0].pageY;
current_posx = start_posx;
current_posy = start_posy;
});

$touchpad.on("touchend", function(e){
if ((current_posx - start_posx + current_posx - start_posx) == 0) {
mouseClick()
} else {
mouseMove(
current_posx - start_posx,
current_posy - start_posy
)
}
});

$touchpad.on("touchmove", function(e){
e.preventDefault(); // prevent scroll on mobile screens
current_posx = e.originalEvent.touches[0].pageX;
current_posy = e.originalEvent.touches[0].pageY;
var last_posx = current_posx;
var last_posy = current_posy;
mouseMove(
current_posx - start_posx,
current_posy - start_posy
).done(function(){
start_posx = last_posx;
start_posy = last_posy;
})
});
}

$(function () {
var chronometer = new Chronometer($('#chronometer'));
var slider = new Slider($('#slider'));
var networkInfo = new NetworkInfo($('#network-info'));
var swipe = new Swipe($('#container'));
var swipe = new Swipe($('#slider'));
var touchPad = new TouchPad($('#touchpad'));

var noSleep = new NoSleep();

var wakeLockEnabled = false;
var toggleEl = document.querySelector("#toggle");
var wakeLockEnabled = false;
toggleEl.addEventListener('click', function() {
if (!wakeLockEnabled) {
noSleep.enable(); // keep the screen on!
alert('display will be ON');
wakeLockEnabled = true;
toggleEl.value = "I will keep display ON";
} else {
noSleep.disable(); // let the screen turn off.
alert('display will be off');
wakeLockEnabled = false;
toggleEl.value = "Display will turn off auto";
}
if (!wakeLockEnabled) {
noSleep.enable(); // keep the screen on!
wakeLockEnabled = true;
toggleEl.value = "Screen won't turn off";
} else {
noSleep.disable(); // let the screen turn off.
wakeLockEnabled = false;
toggleEl.value = "Screen will turn off";
}
}, false);

});

0 comments on commit f105f63

Please sign in to comment.