Update charmapTTY.py

This commit is contained in:
chrys87 2017-07-14 13:14:04 +02:00 committed by GitHub
parent 6173fe8a4d
commit 629e90938c

View File

@ -1,81 +1,84 @@
#!/bin/python3 import time
#attrib: from fcntl import ioctl
#http://rampex.ihep.su/Linux/linux_howto/html/tutorials/mini/Colour-ls-6.html
#0 = black, 1 = blue, 2 = green, 3 = cyan, 4 = red, 5 = purple, 6 = brown/yellow, 7 = white.
#https://github.com/jwilk/vcsapeek/blob/master/linuxvt.py
#blink = 5 if attr & 1 else 0
#bold = 1 if attr & 16 else 0
import fcntl
from array import array from array import array
import struct import struct
import errno import errno
import sys import sys
import time
ttyno = 4
tty = open('/dev/tty%d' % ttyno, 'rb')
vcs = open('/dev/vcsa%d' % ttyno, 'rb')
head = vcs.read(4)
rows = int(head[0])
cols = int(head[1])
s = time.time()
GIO_UNIMAP = 0x4B66
VT_GETHIFONTMASK = 0x560D
himask = array("H", (0,))
fcntl.ioctl(tty, VT_GETHIFONTMASK, himask)
hichar, = struct.unpack_from("@H", himask)
sz = 512
line = ''
while True:
try:
unipairs = array("H", [0]*(2*sz))
unimapdesc = array("B", struct.pack("@HP", sz, unipairs.buffer_info()[0]))
fcntl.ioctl(tty.fileno(), GIO_UNIMAP, unimapdesc)
break
except IOError as e:
if e.errno != errno.ENOMEM:
raise
sz *= 2
tty.close()
ncodes, = struct.unpack_from("@H", unimapdesc)
utable = struct.unpack_from("@%dH" % (2*ncodes), unipairs)
charmap = {} charmap = {}
for u, b in zip(utable[::2], utable[1::2]): def updateCharMap(screen):
if charmap.get(b) is None: ttyno = '4'
charmap[b] = u tty = open('/dev/tty' + screen, 'rb')
GIO_UNIMAP = 0x4B66
VT_GETHIFONTMASK = 0x560D
himask = array("H", (0,))
ioctl(tty, VT_GETHIFONTMASK, himask)
hichar, = struct.unpack_from("@H", himask)
sz = 512
line = ''
while True:
try:
unipairs = array("H", [0]*(2*sz))
unimapdesc = array("B", struct.pack("@HP", sz, unipairs.buffer_info()[0]))
ioctl(tty.fileno(), GIO_UNIMAP, unimapdesc)
break
except IOError as e:
if e.errno != errno.ENOMEM:
raise
sz *= 2
tty.close()
ncodes, = struct.unpack_from("@H", unimapdesc)
utable = struct.unpack_from("@%dH" % (2*ncodes), unipairs)
for u, b in zip(utable[::2], utable[1::2]):
if charmap.get(b) is None:
charmap[b] = chr(u)
allText = []
allAttrib = []
for y in range(rows): def autoDecodeVCSA(allData):
lineText = '' allText = []
lineAttrib = [] allAttrib = []
for x in range(cols): print(time.time() -s )
data = vcs.read(2) for y in range(rows):
(sh,) = struct.unpack("=H", data) lineText = ''
attr = (sh >> 8) & 0xFF lineAttrib = []
ch = sh & 0xFF i = 0
if hichar == 0x100: for x in range(cols):
attr >>= 1 data = allData[i: i + 2]
lineAttrib.append(attr) if data == b' \x07':
ink = attr & 0x0F attr = 7
paper = (attr>>4) & 0x0F ink = 7
#if (ink != 7) or (paper != 0): paper = 0
# print(ink,paper) lineAttrib.append(7)
if sh & hichar: lineText += ' '
ch |= 0x100 continue
lineText += chr(charmap.get(ch, u'?')) (sh,) = struct.unpack("=H", data)
allText.append(lineText) attr = (sh >> 8) & 0xFF
allAttrib.append(lineAttrib) ch = sh & 0xFF
if hichar == 0x100:
attr >>= 1
lineAttrib.append(attr)
ink = attr & 0x0F
paper = (attr>>4) & 0x0F
#if (ink != 7) or (paper != 0):
# print(ink,paper)
if sh & hichar:
ch |= 0x100
try:
lineText += charmap[ch]
except:
lineText += chr('?')
i += 2
allText.append(lineText)
allAttrib.append(lineAttrib)
return allText, allAttrib
#print(allText) def m():
#print(allAttrib) s = time.time()
print(time.time() -s) updateCharMap('4')
print(time.time() -s )
vcsa = open('/dev/vcsa' + '4', 'rb')
head = vcsa.read(4)
rows = int(head[0])
cols = int(head[1])
text, attrib = autoDecodeVCSA(vcsa.read())
print(time.time() -s )