Update charmapTTY.py
This commit is contained in:
parent
6173fe8a4d
commit
629e90938c
@ -1,65 +1,55 @@
|
|||||||
#!/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
|
charmap = {}
|
||||||
|
def updateCharMap(screen):
|
||||||
|
ttyno = '4'
|
||||||
|
tty = open('/dev/tty' + screen, 'rb')
|
||||||
ttyno = 4
|
GIO_UNIMAP = 0x4B66
|
||||||
tty = open('/dev/tty%d' % ttyno, 'rb')
|
VT_GETHIFONTMASK = 0x560D
|
||||||
vcs = open('/dev/vcsa%d' % ttyno, 'rb')
|
himask = array("H", (0,))
|
||||||
|
ioctl(tty, VT_GETHIFONTMASK, himask)
|
||||||
head = vcs.read(4)
|
hichar, = struct.unpack_from("@H", himask)
|
||||||
rows = int(head[0])
|
sz = 512
|
||||||
cols = int(head[1])
|
line = ''
|
||||||
|
while True:
|
||||||
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:
|
try:
|
||||||
unipairs = array("H", [0]*(2*sz))
|
unipairs = array("H", [0]*(2*sz))
|
||||||
unimapdesc = array("B", struct.pack("@HP", sz, unipairs.buffer_info()[0]))
|
unimapdesc = array("B", struct.pack("@HP", sz, unipairs.buffer_info()[0]))
|
||||||
fcntl.ioctl(tty.fileno(), GIO_UNIMAP, unimapdesc)
|
ioctl(tty.fileno(), GIO_UNIMAP, unimapdesc)
|
||||||
break
|
break
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
if e.errno != errno.ENOMEM:
|
if e.errno != errno.ENOMEM:
|
||||||
raise
|
raise
|
||||||
sz *= 2
|
sz *= 2
|
||||||
|
tty.close()
|
||||||
tty.close()
|
ncodes, = struct.unpack_from("@H", unimapdesc)
|
||||||
|
utable = struct.unpack_from("@%dH" % (2*ncodes), unipairs)
|
||||||
ncodes, = struct.unpack_from("@H", unimapdesc)
|
for u, b in zip(utable[::2], utable[1::2]):
|
||||||
utable = struct.unpack_from("@%dH" % (2*ncodes), unipairs)
|
|
||||||
|
|
||||||
charmap = {}
|
|
||||||
for u, b in zip(utable[::2], utable[1::2]):
|
|
||||||
if charmap.get(b) is None:
|
if charmap.get(b) is None:
|
||||||
charmap[b] = u
|
charmap[b] = chr(u)
|
||||||
|
|
||||||
allText = []
|
|
||||||
allAttrib = []
|
|
||||||
|
|
||||||
for y in range(rows):
|
def autoDecodeVCSA(allData):
|
||||||
|
allText = []
|
||||||
|
allAttrib = []
|
||||||
|
print(time.time() -s )
|
||||||
|
for y in range(rows):
|
||||||
lineText = ''
|
lineText = ''
|
||||||
lineAttrib = []
|
lineAttrib = []
|
||||||
|
i = 0
|
||||||
for x in range(cols):
|
for x in range(cols):
|
||||||
data = vcs.read(2)
|
data = allData[i: i + 2]
|
||||||
|
if data == b' \x07':
|
||||||
|
attr = 7
|
||||||
|
ink = 7
|
||||||
|
paper = 0
|
||||||
|
lineAttrib.append(7)
|
||||||
|
lineText += ' '
|
||||||
|
continue
|
||||||
(sh,) = struct.unpack("=H", data)
|
(sh,) = struct.unpack("=H", data)
|
||||||
attr = (sh >> 8) & 0xFF
|
attr = (sh >> 8) & 0xFF
|
||||||
ch = sh & 0xFF
|
ch = sh & 0xFF
|
||||||
@ -72,10 +62,23 @@ for y in range(rows):
|
|||||||
# print(ink,paper)
|
# print(ink,paper)
|
||||||
if sh & hichar:
|
if sh & hichar:
|
||||||
ch |= 0x100
|
ch |= 0x100
|
||||||
lineText += chr(charmap.get(ch, u'?'))
|
try:
|
||||||
|
lineText += charmap[ch]
|
||||||
|
except:
|
||||||
|
lineText += chr('?')
|
||||||
|
i += 2
|
||||||
allText.append(lineText)
|
allText.append(lineText)
|
||||||
allAttrib.append(lineAttrib)
|
allAttrib.append(lineAttrib)
|
||||||
|
return allText, allAttrib
|
||||||
|
|
||||||
|
def m():
|
||||||
|
s = time.time()
|
||||||
|
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 )
|
||||||
|
|
||||||
#print(allText)
|
|
||||||
#print(allAttrib)
|
|
||||||
print(time.time() -s)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user