Skip to content

Commit

Permalink
Switch only if vt number from stdin different than current vt, for Op…
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumezin committed Jul 28, 2020
1 parent 037591c commit bec9494
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions roberta/lab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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):
Expand Down

0 comments on commit bec9494

Please sign in to comment.