Skip to content

Commit

Permalink
Merge pull request #203 from ceccopierangiolieugenio/Multithreaded_input
Browse files Browse the repository at this point in the history
Multithreaded input
  • Loading branch information
ceccopierangiolieugenio authored Nov 21, 2023
2 parents 7e7c697 + 8a7fb6f commit 3abf776
Show file tree
Hide file tree
Showing 11 changed files with 1,031 additions and 591 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ deploySandbox:

git checkout gh-pages
cp tmp/sandbox/*.html sandbox
cp -r tmp/sandbox/js sandbox

git submodule foreach git pull
git add sandbox
Expand Down
163 changes: 2 additions & 161 deletions TermTk/TTkCore/TTkTerm/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,164 +20,5 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

__all__ = ['TTkInput']

import re
from time import time

import platform

from ..drivers import TTkInputDriver

from TermTk.TTkCore.log import TTkLog
from TermTk.TTkCore.constant import TTkK
from TermTk.TTkCore.signal import pyTTkSignal
from TermTk.TTkCore.TTkTerm.term import TTkTerm
from TermTk.TTkCore.TTkTerm.inputkey import TTkKeyEvent
from TermTk.TTkCore.TTkTerm.inputmouse import TTkMouseEvent


class TTkInput:
inputEvent = pyTTkSignal(TTkKeyEvent, TTkMouseEvent)
pasteEvent = pyTTkSignal(str)
_pasteBuffer = ""
_bracketedPaste = False
_readInput = None
_leftLastTime = 0
_midLastTime = 0
_rightLastTime = 0
_leftTap = 0
_midTap = 0
_rightTap = 0
_mouse_re = re.compile(r"\033\[<(\d+);(\d+);(\d+)([mM])")

class Mouse(int):
ON = 0x01
DIRECT = 0x02

@staticmethod
def init(mouse:bool=False, directMouse:bool=False) -> None:
TTkInput._readInput = TTkInputDriver()
TTkTerm.setMouse(mouse, directMouse)

@staticmethod
def close() -> None:
TTkTerm.setMouse(False, False)
if TTkInput._readInput:
TTkInput._readInput.close()

@staticmethod
def stop() -> None:
pass

@staticmethod
def cont() -> None:
if TTkInput._readInput:
TTkInput._readInput.cont()

@staticmethod
def start() -> None:
for stdinRead in TTkInput._readInput.read():
TTkInput.key_process(stdinRead)
TTkLog.debug("Close TTkInput")

@staticmethod
def key_process(stdinRead:str) -> None:
if TTkInput._bracketedPaste:
if stdinRead.endswith("\033[201~"):
TTkInput._pasteBuffer += stdinRead[:-6]
TTkInput._bracketedPaste = False
# due to the CRNL methos (don't ask me why) the terminal
# is substituting all the \n with \r
TTkInput.pasteEvent.emit(TTkInput._pasteBuffer.replace('\r','\n'))
TTkInput._pasteBuffer = ""
else:
TTkInput._pasteBuffer += stdinRead
return

mevt,kevt = None, None

if not stdinRead.startswith("\033[<"):
# Key Event
kevt = TTkKeyEvent.parse(stdinRead)
else:
# Mouse Event
m = TTkInput._mouse_re.match(stdinRead)
if not m:
# TODO: Return Error
hex = [f"0x{ord(x):02x}" for x in stdinRead]
TTkLog.error("UNHANDLED (mouse): "+stdinRead.replace("\033","<ESC>") + " - "+",".join(hex))
return None, None
code = int(m.group(1))
x = int(m.group(2))-1
y = int(m.group(3))-1
state = m.group(4)
key = TTkMouseEvent.NoButton
evt = TTkMouseEvent.Move
tap = 0

def _checkTap(lastTime, tap):
if state=="M":
t = time()
if (t-lastTime) < 0.4:
return t, tap+1
else:
return t, 1
return lastTime, tap

mod = TTkK.NoModifier
if code & 0x10:
code &= ~0x10
mod |= TTkK.ControlModifier
if code & 0x08:
code &= ~0x08
mod |= TTkK.AltModifier

if code == 0x00:
TTkInput._leftLastTime, TTkInput._leftTap = _checkTap(TTkInput._leftLastTime, TTkInput._leftTap)
tap = TTkInput._leftTap
key = TTkMouseEvent.LeftButton
evt = TTkMouseEvent.Press if state=="M" else TTkMouseEvent.Release
elif code == 0x01:
TTkInput._midLastTime, TTkInput._midTap = _checkTap(TTkInput._midLastTime, TTkInput._midTap)
tap = TTkInput._midTap
key = TTkMouseEvent.MidButton
evt = TTkMouseEvent.Press if state=="M" else TTkMouseEvent.Release
elif code == 0x02:
TTkInput._rightLastTime, TTkInput._rightTap = _checkTap(TTkInput._rightLastTime, TTkInput._rightTap)
tap = TTkInput._rightTap
key = TTkMouseEvent.RightButton
evt = TTkMouseEvent.Press if state=="M" else TTkMouseEvent.Release
elif code == 0x20:
key = TTkMouseEvent.LeftButton
evt = TTkMouseEvent.Drag
elif code == 0x21:
key = TTkMouseEvent.MidButton
evt = TTkMouseEvent.Drag
elif code == 0x22:
key = TTkMouseEvent.RightButton
evt = TTkMouseEvent.Drag
elif code == 0x40:
key = TTkMouseEvent.Wheel
evt = TTkMouseEvent.Up
elif code == 0x41:
key = TTkMouseEvent.Wheel
evt = TTkMouseEvent.Down
elif code == 0x23:
evt = TTkMouseEvent.Move
elif code == 0x27:
mod |= TTkK.ShiftModifier
evt = TTkMouseEvent.Move

mevt = TTkMouseEvent(x, y, key, evt, mod, tap, m.group(0).replace("\033", "<ESC>"))
if kevt or mevt:
TTkInput.inputEvent.emit(kevt, mevt)
return

if stdinRead.startswith("\033[200~"):
TTkInput._pasteBuffer = stdinRead[6:]
TTkInput._bracketedPaste = True
return

hex = [f"0x{ord(x):02x}" for x in stdinRead]
TTkLog.error("UNHANDLED: "+stdinRead.replace("\033","<ESC>") + " - "+",".join(hex))
# from .input_mono import *
from .input_thread import *
183 changes: 183 additions & 0 deletions TermTk/TTkCore/TTkTerm/input_mono.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
# MIT License
#
# Copyright (c) 2021 Eugenio Parodi <ceccopierangiolieugenio AT googlemail DOT com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

__all__ = ['TTkInput']

import re
from time import time

import platform

from ..drivers import TTkInputDriver

from TermTk.TTkCore.log import TTkLog
from TermTk.TTkCore.constant import TTkK
from TermTk.TTkCore.signal import pyTTkSignal
from TermTk.TTkCore.TTkTerm.term import TTkTerm
from TermTk.TTkCore.TTkTerm.inputkey import TTkKeyEvent
from TermTk.TTkCore.TTkTerm.inputmouse import TTkMouseEvent


class TTkInput:
inputEvent = pyTTkSignal(TTkKeyEvent, TTkMouseEvent)
pasteEvent = pyTTkSignal(str)
_pasteBuffer = ""
_bracketedPaste = False
_readInput = None
_leftLastTime = 0
_midLastTime = 0
_rightLastTime = 0
_leftTap = 0
_midTap = 0
_rightTap = 0
_mouse_re = re.compile(r"\033\[<(\d+);(\d+);(\d+)([mM])")

class Mouse(int):
ON = 0x01
DIRECT = 0x02

@staticmethod
def init(mouse:bool=False, directMouse:bool=False) -> None:
TTkInput._readInput = TTkInputDriver()
TTkTerm.setMouse(mouse, directMouse)

@staticmethod
def close() -> None:
TTkTerm.setMouse(False, False)
if TTkInput._readInput:
TTkInput._readInput.close()

@staticmethod
def stop() -> None:
pass

@staticmethod
def cont() -> None:
if TTkInput._readInput:
TTkInput._readInput.cont()

@staticmethod
def start() -> None:
for stdinRead in TTkInput._readInput.read():
TTkInput.key_process(stdinRead)
TTkLog.debug("Close TTkInput")

@staticmethod
def key_process(stdinRead:str) -> None:
if TTkInput._bracketedPaste:
if stdinRead.endswith("\033[201~"):
TTkInput._pasteBuffer += stdinRead[:-6]
TTkInput._bracketedPaste = False
# due to the CRNL methos (don't ask me why) the terminal
# is substituting all the \n with \r
TTkInput.pasteEvent.emit(TTkInput._pasteBuffer.replace('\r','\n'))
TTkInput._pasteBuffer = ""
else:
TTkInput._pasteBuffer += stdinRead
return

mevt,kevt = None, None

if not stdinRead.startswith("\033[<"):
# Key Event
kevt = TTkKeyEvent.parse(stdinRead)
else:
# Mouse Event
m = TTkInput._mouse_re.match(stdinRead)
if not m:
# TODO: Return Error
hex = [f"0x{ord(x):02x}" for x in stdinRead]
TTkLog.error("UNHANDLED (mouse): "+stdinRead.replace("\033","<ESC>") + " - "+",".join(hex))
return None, None
code = int(m.group(1))
x = int(m.group(2))-1
y = int(m.group(3))-1
state = m.group(4)
key = TTkMouseEvent.NoButton
evt = TTkMouseEvent.Move
tap = 0

def _checkTap(lastTime, tap):
if state=="M":
t = time()
if (t-lastTime) < 0.4:
return t, tap+1
else:
return t, 1
return lastTime, tap

mod = TTkK.NoModifier
if code & 0x10:
code &= ~0x10
mod |= TTkK.ControlModifier
if code & 0x08:
code &= ~0x08
mod |= TTkK.AltModifier

if code == 0x00:
TTkInput._leftLastTime, TTkInput._leftTap = _checkTap(TTkInput._leftLastTime, TTkInput._leftTap)
tap = TTkInput._leftTap
key = TTkMouseEvent.LeftButton
evt = TTkMouseEvent.Press if state=="M" else TTkMouseEvent.Release
elif code == 0x01:
TTkInput._midLastTime, TTkInput._midTap = _checkTap(TTkInput._midLastTime, TTkInput._midTap)
tap = TTkInput._midTap
key = TTkMouseEvent.MidButton
evt = TTkMouseEvent.Press if state=="M" else TTkMouseEvent.Release
elif code == 0x02:
TTkInput._rightLastTime, TTkInput._rightTap = _checkTap(TTkInput._rightLastTime, TTkInput._rightTap)
tap = TTkInput._rightTap
key = TTkMouseEvent.RightButton
evt = TTkMouseEvent.Press if state=="M" else TTkMouseEvent.Release
elif code == 0x20:
key = TTkMouseEvent.LeftButton
evt = TTkMouseEvent.Drag
elif code == 0x21:
key = TTkMouseEvent.MidButton
evt = TTkMouseEvent.Drag
elif code == 0x22:
key = TTkMouseEvent.RightButton
evt = TTkMouseEvent.Drag
elif code == 0x40:
key = TTkMouseEvent.Wheel
evt = TTkMouseEvent.Up
elif code == 0x41:
key = TTkMouseEvent.Wheel
evt = TTkMouseEvent.Down
elif code == 0x23:
evt = TTkMouseEvent.Move
elif code == 0x27:
mod |= TTkK.ShiftModifier
evt = TTkMouseEvent.Move

mevt = TTkMouseEvent(x, y, key, evt, mod, tap, m.group(0).replace("\033", "<ESC>"))
if kevt or mevt:
TTkInput.inputEvent.emit(kevt, mevt)
return

if stdinRead.startswith("\033[200~"):
TTkInput._pasteBuffer = stdinRead[6:]
TTkInput._bracketedPaste = True
return

hex = [f"0x{ord(x):02x}" for x in stdinRead]
TTkLog.error("UNHANDLED: "+stdinRead.replace("\033","<ESC>") + " - "+",".join(hex))
Loading

0 comments on commit 3abf776

Please sign in to comment.