diff --git a/TermTk/TTkWidgets/TTkTerminal/terminal.py b/TermTk/TTkWidgets/TTkTerminal/terminal.py index 8101bdcc..17b02bed 100644 --- a/TermTk/TTkWidgets/TTkTerminal/terminal.py +++ b/TermTk/TTkWidgets/TTkTerminal/terminal.py @@ -64,8 +64,10 @@ class TTkTerminal(TTkWidget): @dataclass class _Mouse(): - tracking: bool = False - sgrMode: bool = False + reportPress: bool = False + reportDrag: bool = False + reportMove: bool = False + sgrMode: bool = False __slots__ = ('_shell', '_fd', '_inout', '_proc', '_quit_pipe', '_mode_normal' '_buffer_lines', '_buffer_screen', '_keyboard', '_mouse', @@ -399,7 +401,12 @@ class TTkTerminal(TTkWidget): # report position in pixels rather than character cells. def _sendMouse(self, evt): - if not self._mouse.tracking: return True + if not self._mouse.reportPress: + return True + if ( not self._mouse.reportDrag and + evt.evt in (TTkK.Drag, TTkK.Move)): + return True + x,y = evt.x+1, evt.y+1 if self._mouse.sgrMode: k = { @@ -528,6 +535,21 @@ class TTkTerminal(TTkWidget): def _CSI_DEC_SR_25_DECTCEM(self, s): self.enableWidgetCursor(enable=s) + # CSI ? Pm h + # DEC Private Mode Set (DECSET). + # Ps = 1 0 0 0 ⇒ Send Mouse X & Y on button press and + # release. See the section Mouse Tracking. This is the X11 + # xterm mouse protocol. + # CSI ? Pm l + # DEC Private Mode Reset (DECRST). + # Ps = 1 0 0 0 ⇒ Don't send Mouse X & Y on button press and + # release. See the section Mouse Tracking. + def _CSI_DEC_SR_1000(self, s): + self._mouse.reportPress = s + self._mouse.reportDrag = False + self._mouse.reportMove = False + TTkLog.info(f"1002 Mouse Tracking {s=}") + # CSI ? Pm h # DEC Private Mode Set (DECSET). # Ps = 1 0 0 2 ⇒ Use Cell Motion Mouse Tracking, xterm. See @@ -537,7 +559,9 @@ class TTkTerminal(TTkWidget): # Ps = 1 0 0 2 ⇒ Don't use Cell Motion Mouse Tracking, # xterm. See the section Button-event tracking. def _CSI_DEC_SR_1002(self, s): - self._mouse.tracking = s + self._mouse.reportPress = s + self._mouse.reportDrag = s + self._mouse.reportMove = False TTkLog.info(f"1002 Mouse Tracking {s=}") # CSI ? Pm h @@ -614,6 +638,7 @@ class TTkTerminal(TTkWidget): _CSI_DEC_SET_RST_MAP = { 1 : _CSI_DEC_SR_1_DECCKM, 25 : _CSI_DEC_SR_25_DECTCEM, + 1000: _CSI_DEC_SR_1000, 1002: _CSI_DEC_SR_1002, 1006: _CSI_DEC_SR_1006, 1015: _CSI_DEC_SR_1015, diff --git a/TermTk/TTkWidgets/TTkTerminal/terminal_normal.py b/TermTk/TTkWidgets/TTkTerminal/terminal_normal.py index 6fd14043..f8e39a13 100644 --- a/TermTk/TTkWidgets/TTkTerminal/terminal_normal.py +++ b/TermTk/TTkWidgets/TTkTerminal/terminal_normal.py @@ -20,6 +20,8 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +import collections + from TermTk.TTkCore.canvas import TTkCanvas from TermTk.TTkCore.color import TTkColor @@ -41,7 +43,8 @@ from TermTk.TTkWidgets.widget import TTkWidget class _TTkTerminalNormalScreen(): __slots__ = ('_lines', '_terminalCursor', '_w', '_h', '_bufferSize', '_color') def __init__(self, w=80, h=24, bufferSize=200, color=TTkColor.RST) -> None: - self._lines = [TTkString()] + self._lines = collections.deque(maxlen=bufferSize) + self._lines.append(TTkString()) self._terminalCursor = (0,0) self._w = w self._h = h @@ -83,7 +86,7 @@ class _TTkTerminalNormalScreen(): y += 1 if y >= len(self._lines): self._lines.append(TTkString()) - self._lines = self._lines[-self._bufferSize:] + # self._lines = self._lines[-self._bufferSize:] self._terminalCursor = (x,y) = (0,min(self._bufferSize-1, y)) ls = l.split('\r') for ii,ll in enumerate(ls): @@ -98,8 +101,12 @@ class _TTkTerminalNormalScreen(): def paintEvent(self, canvas:TTkCanvas, w:int, h:int, ox:int=0, oy:int=0) -> None: canvas.drawText(text="NORMAL Screen", pos=(0,0), width=w) - for y,l in enumerate(self._lines[-h:]): - canvas.drawTTkString(text=l, pos=(0,y), width=w) + # for y,l in enumerate(self._lines[-h:]): + # canvas.drawTTkString(text=l, pos=(0,y), width=w) + di = max(0,len(self._lines)-h) + for i in range(di,len(self._lines)): + canvas.drawTTkString(text=self._lines[i], pos=(0,i-di), width=w) + diff --git a/tests/test.input.raw.py b/tests/test.input.raw.py index e5c7abb8..b47ec51d 100755 --- a/tests/test.input.raw.py +++ b/tests/test.input.raw.py @@ -37,13 +37,15 @@ print("Retrieve Keyboard, Mouse press/drag/wheel Events") print("Press q or to exit") # Reset +TTkTerm.push("\033[?1000l") TTkTerm.push("\033[?1002l") TTkTerm.push("\033[?1015l") TTkTerm.push("\033[?1006l") -TTkTerm.push("\033[?1002h") +TTkTerm.push("\033[?1000h") +# TTkTerm.push("\033[?1002h") # TTkTerm.push("\033[?1006h") -TTkTerm.push("\033[?1015h") +# TTkTerm.push("\033[?1015h") # TTkTerm.push(TTkTerm.Mouse.ON) # TTkTerm.push(TTkTerm.Mouse.DIRECT_ON) TTkTerm.setEcho(False) @@ -99,6 +101,7 @@ try: print(f"{stdinRead=}") finally: # Reset + TTkTerm.push("\033[?1000l") TTkTerm.push("\033[?1002l") TTkTerm.push("\033[?1015l") TTkTerm.push("\033[?1006l") diff --git a/tests/test.pty.006.terminal.03.py b/tests/test.pty.006.terminal.03.py new file mode 100755 index 00000000..a7d48b55 --- /dev/null +++ b/tests/test.pty.006.terminal.03.py @@ -0,0 +1,97 @@ +#!/usr/bin/env python3 + +# MIT License +# +# Copyright (c) 2023 Eugenio Parodi +# +# 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. + +# This test is based on: +# pyte - In memory VTXXX-compatible terminal emulator. +# Terminal Emulator Example +# https://github.com/selectel/pyte/blob/master/examples/terminal_emulator.py +# +# pty — Pseudo-terminal utilities¶ +# https://docs.python.org/3/library/pty.html#example +# +# Using a pseudo-terminal to interact with interactive Python in a subprocess +# by Thomas Billinger +# https://gist.github.com/thomasballinger/7979808 +# +# Run interactive Bash with popen and a dedicated TTY Python +# https://stackoverflow.com/questions/41542960/run-interactive-bash-with-popen-and-a-dedicated-tty-python + +import os +import pty +import sys +import threading +import argparse +from select import select + +sys.path.append(os.path.join(sys.path[0],'..')) +import TermTk as ttk + +parser = argparse.ArgumentParser() +parser.add_argument('-d', help='Debug (Add LogViewer Panel)', action='store_true') +args = parser.parse_args() + +# ttk.TTkLog.use_default_file_logging() +root = ttk.TTk(layout=ttk.TTkGridLayout()) + +split = ttk.TTkSplitter(parent=root, orientation=ttk.TTkK.VERTICAL) + +split.addItem(top := ttk.TTkLayout()) + +if args.d: + split.addWidget(ttk.TTkLogViewer(follow=False ), title='Log', size=20) + +quitBtn = ttk.TTkButton(text="QUIT", border=True) +quitBtn.clicked.connect(ttk.TTkHelper.quit) + +cb_c = ttk.TTkCheckbox(pos=(0,3),size=(20,1), text="CTRL-C (VINTR) ", checked=ttk.TTkK.Checked) +cb_s = ttk.TTkCheckbox(pos=(0,4),size=(20,1), text="CTRL-S (VSTOP) ", checked=ttk.TTkK.Checked) +cb_z = ttk.TTkCheckbox(pos=(0,5),size=(20,1), text="CTRL-Z (VSUSP) ", checked=ttk.TTkK.Checked) +cb_q = ttk.TTkCheckbox(pos=(0,6),size=(20,1), text="CTRL-Q (VSTART)", checked=ttk.TTkK.Checked) + +cb_c.stateChanged.connect(lambda x: ttk.TTkTerm.setSigmask(ttk.TTkTerm.Sigmask.CTRL_C,x==ttk.TTkK.Checked)) +cb_s.stateChanged.connect(lambda x: ttk.TTkTerm.setSigmask(ttk.TTkTerm.Sigmask.CTRL_S,x==ttk.TTkK.Checked)) +cb_z.stateChanged.connect(lambda x: ttk.TTkTerm.setSigmask(ttk.TTkTerm.Sigmask.CTRL_Z,x==ttk.TTkK.Checked)) +cb_q.stateChanged.connect(lambda x: ttk.TTkTerm.setSigmask(ttk.TTkTerm.Sigmask.CTRL_Q,x==ttk.TTkK.Checked)) + +win1 = ttk.TTkWindow(pos=(90,5), size=(70,15), title="Terminallo n.1", border=True, layout=ttk.TTkVBoxLayout(), flags = ttk.TTkK.WindowFlag.WindowMinMaxButtonsHint) +term1 = ttk.TTkTerminal(parent=win1) +term1.runShell() + +win2 = ttk.TTkWindow(pos=(0,0), size=(150,30), title="Terminallo n.2", border=True, layout=ttk.TTkVBoxLayout(), flags = ttk.TTkK.WindowFlag.WindowMinMaxButtonsHint) +term2 = ttk.TTkTerminal(parent=win2) +term2.runShell() + +win3 = ttk.TTkWindow(pos=(92,8), size=(70,15), title="Terminallo n.3", border=True, layout=ttk.TTkVBoxLayout(), flags = ttk.TTkK.WindowFlag.WindowMinMaxButtonsHint) +term3 = ttk.TTkTerminal(parent=win3) +term3.runShell() + +win4 = ttk.TTkWindow(pos=(94,11), size=(70,15), title="Terminallo n.4", border=True, layout=ttk.TTkVBoxLayout(), flags = ttk.TTkK.WindowFlag.WindowMinMaxButtonsHint) +term4 = ttk.TTkTerminal(parent=win4) +term4.runShell() + +top.addWidgets([quitBtn, cb_c, cb_s, cb_z, cb_q, win1, win2, win3, win4]) + +term2.setFocus() + +root.mainloop() \ No newline at end of file