From cde0bbb2d3b34b88a268fa7e0b60c9640deb2021 Mon Sep 17 00:00:00 2001 From: Guillaume Zin Date: Sun, 26 Jul 2020 18:23:58 +0200 Subject: [PATCH 1/5] Use dbus to switch VT, should help with https://github.com/OpenRoberta/robertalab-ev3dev/issues/72 on stretch --- roberta/lab.py | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/roberta/lab.py b/roberta/lab.py index 45dce0a..b0e9871 100644 --- a/roberta/lab.py +++ b/roberta/lab.py @@ -33,7 +33,8 @@ # TRUE: use a new token per reconnect # FALSE: try keep using the token for as long as possible # (needs robertalab > 1.4 or develop branch) -TOKEN_PER_SESSION = True +#TOKEN_PER_SESSION = True +TOKEN_PER_SESSION = False # helpers @@ -153,20 +154,31 @@ def status(self, status): class GfxMode(object): def __init__(self): - self.tty_name = os.ttyname(sys.stdin.fileno()) + self.previous_tty_num = 3 + self.previous_tty_name = "/dev/tty" + str(self.previous_tty_num) + self.tty_num = 5 + self.tty_name = "/dev/tty" + str(self.tty_num) + try: + self.bus = dbus.SystemBus() + self.devobj = self.bus.get_object('org.freedesktop.login1', '/org/freedesktop/login1/seat/seat0') + self.devprop = dbus.Interface(self.devobj, "org.freedesktop.DBus.Properties") + self.dev = dbus.Interface(self.devobj, 'org.freedesktop.login1.Seat') + except: + logger.exception('cannot open dbus interface for org.freedesktop.login1.Seat') def __enter__(self): - logger.info('running on tty: %s', self.tty_name) - with open(self.tty_name, 'r') as tty: - # KDSETMODE = 0x4B3A, GRAPHICS = 0x01 - ioctl(tty, 0x4B3A, 0x01) + try: + self.dev.SwitchTo(self.tty_num) + logger.info('running on tty: %s', self.tty_name) + except: + logger.exception('cannot switch to: %s', self.tty_name) def __exit__(self, type, value, traceback): - with open(self.tty_name, 'w') as tty: - # KDSETMODE = 0x4B3A, TEXT = 0x00 - ioctl(tty, 0x4B3A, 0x00) - # send Ctrl-L to tty to clear - tty.write('\033c') + try: + self.dev.SwitchTo(self.previous_tty_num) + logger.info('running on tty: %s', self.previous_tty_name) + except: + logger.exception('cannot switch back to: %s', self.previous_tty_name) class AbortHandler(threading.Thread): From 649f184bdd464d4df97ac71926dc6d490eb35e3e Mon Sep 17 00:00:00 2001 From: Guillaume Zin Date: Sun, 26 Jul 2020 23:25:34 +0200 Subject: [PATCH 2/5] Keep ioctl to VT when correcting https://github.com/OpenRoberta/robertalab-ev3dev/issues/72 --- roberta/lab.py | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/roberta/lab.py b/roberta/lab.py index b0e9871..0650ec6 100644 --- a/roberta/lab.py +++ b/roberta/lab.py @@ -154,29 +154,41 @@ def status(self, status): class GfxMode(object): def __init__(self): + self.tty_name = os.ttyname(sys.stdin.fileno()) + # get digit from string + self.tty_num = int(list(filter(str.isdigit, self.tty_name))[0]) + # TODO: get tty number from calling program as parameter self.previous_tty_num = 3 - self.previous_tty_name = "/dev/tty" + str(self.previous_tty_num) - self.tty_num = 5 - self.tty_name = "/dev/tty" + str(self.tty_num) + self.previous_tty_name = '/dev/tty' + str(self.previous_tty_num) try: - self.bus = dbus.SystemBus() - self.devobj = self.bus.get_object('org.freedesktop.login1', '/org/freedesktop/login1/seat/seat0') - self.devprop = dbus.Interface(self.devobj, "org.freedesktop.DBus.Properties") - self.dev = dbus.Interface(self.devobj, 'org.freedesktop.login1.Seat') + bus = dbus.SystemBus() + seat_obj = bus.get_object('org.freedesktop.login1', '/org/freedesktop/login1/seat/seat0') + seat_props = dbus.Interface(seat_obj, 'org.freedesktop.DBus.Properties') + self.seat_methods = dbus.Interface(seat_obj, 'org.freedesktop.login1.Seat') except: logger.exception('cannot open dbus interface for org.freedesktop.login1.Seat') def __enter__(self): + logger.info('running on tty: %s', self.tty_name) + # really useful ? Can block if permission is not set correctly on /dev/ttyx + with open(self.tty_name, 'r') as tty: + # KDSETMODE = 0x4B3A, GRAPHICS = 0x01 + ioctl(tty, 0x4B3A, 0x01) try: - self.dev.SwitchTo(self.tty_num) - logger.info('running on tty: %s', self.tty_name) + self.seat_methods.SwitchTo(self.tty_num) except: logger.exception('cannot switch to: %s', self.tty_name) def __exit__(self, type, value, traceback): + logger.info('switching back to tty: %s', self.previous_tty_name) + # really useful ? Can block if permission is not set correctly on /dev/ttyx + with open(self.tty_name, 'w') as tty: + # KDSETMODE = 0x4B3A, TEXT = 0x00 + ioctl(tty, 0x4B3A, 0x00) + # send Ctrl-L to tty to clear + tty.write('\033c') try: - self.dev.SwitchTo(self.previous_tty_num) - logger.info('running on tty: %s', self.previous_tty_name) + self.seat_methods.SwitchTo(self.previous_tty_num) except: logger.exception('cannot switch back to: %s', self.previous_tty_name) From 037591cba637a7fe46b3f8b481f7742aa83e7b31 Mon Sep 17 00:00:00 2001 From: Guillaume Zin Date: Mon, 27 Jul 2020 12:26:56 +0200 Subject: [PATCH 3/5] Getting current vt number from fgconsole, for https://github.com/OpenRoberta/robertalab-ev3dev/issues/72 issue --- roberta/lab.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/roberta/lab.py b/roberta/lab.py index 0650ec6..847e768 100644 --- a/roberta/lab.py +++ b/roberta/lab.py @@ -157,13 +157,17 @@ def __init__(self): self.tty_name = os.ttyname(sys.stdin.fileno()) # get digit from string self.tty_num = int(list(filter(str.isdigit, self.tty_name))[0]) - # TODO: get tty number from calling program as parameter - self.previous_tty_num = 3 + try: + stream = os.popen('fgconsole') + self.previous_tty_num = int(stream.readline()) + logger.info('current vt number is: %d', self.previous_tty_num) + except: + logger.exception('cannot read current vt number from fgconsole command, setting 3 by default') + self.previous_tty_num = 3 self.previous_tty_name = '/dev/tty' + str(self.previous_tty_num) try: bus = dbus.SystemBus() seat_obj = bus.get_object('org.freedesktop.login1', '/org/freedesktop/login1/seat/seat0') - seat_props = dbus.Interface(seat_obj, 'org.freedesktop.DBus.Properties') self.seat_methods = dbus.Interface(seat_obj, 'org.freedesktop.login1.Seat') except: logger.exception('cannot open dbus interface for org.freedesktop.login1.Seat') From bec9494edb2df3054e8895427168a88565c5b7d6 Mon Sep 17 00:00:00 2001 From: Guillaume Zin Date: Tue, 28 Jul 2020 15:26:11 +0200 Subject: [PATCH 4/5] Switch only if vt number from stdin different than current vt, for https://github.com/OpenRoberta/robertalab-ev3dev/issues/72 --- roberta/lab.py | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/roberta/lab.py b/roberta/lab.py index 847e768..fbd654b 100644 --- a/roberta/lab.py +++ b/roberta/lab.py @@ -33,8 +33,7 @@ # TRUE: use a new token per reconnect # FALSE: try keep using the token for as long as possible # (needs robertalab > 1.4 or develop branch) -#TOKEN_PER_SESSION = True -TOKEN_PER_SESSION = False +TOKEN_PER_SESSION = True # helpers @@ -154,16 +153,18 @@ def status(self, status): class GfxMode(object): def __init__(self): + # get target vt from stdin, if vt number given by stdin is different than active vt number, we will have to switch then switch back self.tty_name = os.ttyname(sys.stdin.fileno()) # get digit from string self.tty_num = int(list(filter(str.isdigit, self.tty_name))[0]) try: + # ugly hack to get current vt number, I didn't find any dbus properties to get it properly stream = os.popen('fgconsole') self.previous_tty_num = int(stream.readline()) - logger.info('current vt number is: %d', self.previous_tty_num) + logger.info('current virtual terminal number is: %d', self.previous_tty_num) except: - logger.exception('cannot read current vt number from fgconsole command, setting 3 by default') - self.previous_tty_num = 3 + self.previous_tty_num = self.tty_num + logger.exception('cannot read current virtual terminal number from fgconsole command, setting to %d', self.previous_tty_num) self.previous_tty_name = '/dev/tty' + str(self.previous_tty_num) try: bus = dbus.SystemBus() @@ -173,28 +174,32 @@ def __init__(self): logger.exception('cannot open dbus interface for org.freedesktop.login1.Seat') def __enter__(self): - logger.info('running on tty: %s', self.tty_name) # really useful ? Can block if permission is not set correctly on /dev/ttyx with open(self.tty_name, 'r') as tty: # KDSETMODE = 0x4B3A, GRAPHICS = 0x01 ioctl(tty, 0x4B3A, 0x01) - try: - self.seat_methods.SwitchTo(self.tty_num) - except: - logger.exception('cannot switch to: %s', self.tty_name) + # change vt if not already done by caller + if self.tty_name != self.previous_tty_num: + logger.info('switching to tty: %s', self.tty_name) + try: + self.seat_methods.SwitchTo(self.tty_num) + except: + logger.exception('cannot switch to: %s', self.tty_name) def __exit__(self, type, value, traceback): - logger.info('switching back to tty: %s', self.previous_tty_name) # really useful ? Can block if permission is not set correctly on /dev/ttyx with open(self.tty_name, 'w') as tty: # KDSETMODE = 0x4B3A, TEXT = 0x00 ioctl(tty, 0x4B3A, 0x00) # send Ctrl-L to tty to clear tty.write('\033c') - try: - self.seat_methods.SwitchTo(self.previous_tty_num) - except: - logger.exception('cannot switch back to: %s', self.previous_tty_name) + # change vt if not managed by caller + if self.tty_name != self.previous_tty_num: + logger.info('switching back to tty: %s', self.previous_tty_name) + try: + self.seat_methods.SwitchTo(self.previous_tty_num) + except: + logger.exception('cannot switch back to: %s', self.previous_tty_name) class AbortHandler(threading.Thread): From 5ade723193d997083eac08721de1b1cd51141e04 Mon Sep 17 00:00:00 2001 From: Guillaume Zin Date: Tue, 28 Jul 2020 15:30:38 +0200 Subject: [PATCH 5/5] Add log message if not switching VT --- roberta/lab.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/roberta/lab.py b/roberta/lab.py index fbd654b..22978e6 100644 --- a/roberta/lab.py +++ b/roberta/lab.py @@ -185,6 +185,8 @@ def __enter__(self): self.seat_methods.SwitchTo(self.tty_num) except: logger.exception('cannot switch to: %s', self.tty_name) + else: + logger.info('running on tty: %s', self.tty_name) def __exit__(self, type, value, traceback): # really useful ? Can block if permission is not set correctly on /dev/ttyx