From d995d32d12fcbea933965ff0f8394a5d1aba5c7e Mon Sep 17 00:00:00 2001 From: ville rantanen Date: Tue, 30 Jul 2013 13:40:43 +0300 Subject: [PATCH] VNC scripts --- vncdisp | 161 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ vnckill | 8 +++ vncs | 9 ++++ 3 files changed, 178 insertions(+) create mode 100755 vncdisp create mode 100755 vnckill create mode 100755 vncs diff --git a/vncdisp b/vncdisp new file mode 100755 index 0000000..f9c4800 --- /dev/null +++ b/vncdisp @@ -0,0 +1,161 @@ +#!/usr/bin/python + +import os +import sys +import subprocess +import termios +import tty +import readline +import math + +readline.parse_and_bind('tab: complete') +readline.parse_and_bind('set editing-mode vi') + +def termsize(): + rows, columns = os.popen('stty size', 'r').read().split() + return (int(rows),int(columns)) + +class bc: + MAG = '\033[95m' + BLU = '\033[94m' + GRE = '\033[92m' + YEL = '\033[93m' + RED = '\033[91m' + CYA = '\033[96m' + WHI = '\033[1m' + END = '\033[0m' + CLR = '\033[2J' + + def disable(self): + self.MAG = '' + self.BLU = '' + self.GRE = '' + self.YEL = '' + self.RED = '' + self.END = '' + self.CLR = '' + self.CYA = '' + self.WHI = '' + + def pos(self,y,x): + return "\033["+str(y)+";"+str(x)+"H" + +class getch: + def __init__(self): + import sys, tty, termios + + def get(self): + + fd = sys.stdin.fileno() + old_settings = termios.tcgetattr(fd) + try: + tty.setraw(sys.stdin.fileno()) + ch = sys.stdin.read(1) + finally: + termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) + return ch + + +def ichr(i): + ''' convert integer to 1-9, a-z, A-Z, omitting x ''' + + if i < 10: + return str(i) + i=i + 87 + if i>119: + i=i+1 + if i>122: + i=i-122+64 + return chr(i) + +def drawmenu(entries): + maxrows,maxcolumns = termsize() + maxrows-=5 + maxcolumns-=10 + twocol=False + co=bc() + helptext="" + + print(co.END+co.CLR+co.pos(1,3)+co.YEL+'XRandrMenu x:exit '+helptext+co.END) + if len(entries)>20: + twocol=True + maxrows=int(math.ceil(min(maxrows/2.0, len(entries)/2.0))) + maxcolumns=int(math.ceil(maxcolumns/2.0)) + r=1 + for e in range(len(entries)): + if r>maxrows: + break + printline=entries[e][2] + if len(printline)>maxcolumns: + printline=printline[:maxcolumns]+"..." + print(co.WHI+entries[e][0]+co.END+' '+entries[e][3]+printline+co.END) + r=1+r + if twocol: + r=1 + for e in range(e,len(entries)): + if r>maxrows: + break + printline=entries[e][2] + if len(printline)>maxcolumns: + printline=printline[:maxcolumns]+"..." + print(co.pos(r+1,maxcolumns)+'| '+co.WHI+entries[e][0]+co.END+' '+entries[e][3]+printline+co.END) + r=1+r + print(co.pos(maxrows+2,0)) + +def read_displays(): + proc = subprocess.Popen('xrandr', stdout=subprocess.PIPE) + output = proc.stdout.read() + entries=[] + connected=False + for line in output.split('\n'): + try: + idx=int(line[1:2]) + entries.append([line[1:3], line[4:]]) + except: + pass + return entries + + +def append_index(entries,offset=0,color=None): + e=1+offset + for el in range(len(entries)): + entries[el]=[ichr(e), entries[el][0], entries[el][1], color] + e=e+1 + return entries + +def launch(key,entries,args=""): + ''' launch the given resolution ''' + idx=[i for i in range(len(entries)) if entries[i][0]==key][0] + command_str=['xrandr','--size',entries[idx][1]] + try: + subprocess.call(command_str) + except: + print('Unable to run:') + print(command_str) + + +def initialize(): + entries=read_displays() + entries=append_index(entries,offset=0,color=bc.CYA) + return entries + +def main(): + entries=initialize() + print(entries) + ch=getch() + drawmenu(entries) + while True: + inkey=ord(ch.get()) + #print('-'+str((inkey))+'-') + if inkey in [120,27,3,24,4]: + sys.exit(0) + if inkey==46: # . + entries=initialize() + if chr(inkey) in [x[0] for x in entries]: + launch(chr(inkey),entries) + entries=initialize() + drawmenu(entries) + +main() + + diff --git a/vnckill b/vnckill new file mode 100755 index 0000000..b765f78 --- /dev/null +++ b/vnckill @@ -0,0 +1,8 @@ +#!/bin/bash + +dn=$( echo $UID | sed 's/.*\(....\)$/\1/' ) + +vncserver -kill :$dn +rm -f /tmp/.X${dn}-lock +rm -f /tmp/.X11-unix/X${dn} + diff --git a/vncs b/vncs new file mode 100755 index 0000000..c5fcbc9 --- /dev/null +++ b/vncs @@ -0,0 +1,9 @@ +#!/bin/bash + +dn=$( echo $UID | sed 's/.*\(....\)$/\1/' ) + +vncserver :$dn -depth 24 -geometry 1024x768 -geometry 800x600 -geometry 1366x768 -geometry 1280x1024 -geometry 1680x1050 -geometry 1280x720 +echo "Kill with vnckill" +echo "use vncdisp to change resolution" +echo Your VNC connection port is $HOSTNAME:$(( 5900 + $dn )) +