#!/usr/bin/env python #KLL syscall_test.py # syscall2.py take command line parameter import pygame from pygame.locals import * import sys import os import subprocess import getopt mytitle="syscall2.py KLL" WINDOWWIDTH = 380 # size of window's width in pixels WINDOWHEIGHT =240 # size of windows' height in pixels msgcall = 'aconnect -i -o -l' # show text msg="_" msgline = 50 # chars ipady = 10 ipadx = 10 # button ibuttonwide = 50 ibuttonhigh = 17 buttonmsg = "start" ipadbuttony = WINDOWHEIGHT - ibuttonhigh -10 ipadbuttonx = WINDOWWIDTH - ibuttonwide -10 unselborder = 1 selborder = 3 FPS = 20 # frames per second, the general speed of the program WHITE = (255, 255, 255) BLACK = ( 0, 0, 0) GREY = (125, 125, 125) PURPLE = (255, 0, 255) CYAN = ( 0, 255, 255) RED = (255, 0, 0) unselcol = GREY selcol = CYAN mousex = 0 # used to store x coordinate of mouse event mousey = 0 # used to store y coordinate of mouse event #______________________________________________________________________ def draw_button(): # START button bord= unselborder bcol = unselcol myfont = pygame.font.SysFont("monospace",16,bold=True) pygame.draw.rect(DISPLAYSURF, bcol,(ipadbuttonx-bord, ipadbuttony-bord,ibuttonwide+2*bord,ibuttonhigh+2*bord),bord) DISPLAYSURF.blit(myfont.render(buttonmsg,1,WHITE,BLACK),(ipadbuttonx,ipadbuttony)) #______________________________________________________________________ def sel_button(x, y): # START button under mouse boxRect = pygame.Rect(ipadbuttonx,ipadbuttony,ibuttonwide,ibuttonhigh) if boxRect.collidepoint(x, y): bord=selborder pygame.draw.rect(DISPLAYSURF,selcol, (ipadbuttonx - bord, ipadbuttony - bord, ibuttonwide + 2*bord,ibuttonhigh + 2*bord), bord) return True return False #______________________________________________________________________ def draw_msg(): myfont = pygame.font.SysFont("monospace",12,bold=True) # show syscall DISPLAYSURF.blit(myfont.render(msgcall,1,PURPLE,WHITE),(ipadx,0)) myfont = pygame.font.SysFont("monospace",10,bold=True) # show long text as a multiline box # if sysmsg is multiline ( NL.. ) need extra line wrap here nextline = myfont.get_linesize() y=ipady #print "line hight %s" % (nextline) #print "text chars %s" % (len(msg)) Tend = len(msg) Tstart = 0 Tstep = msgline # chars per line Tnextstep=Tstart Tlast = False NONL = False NL = "\n" while ( Tlast == False ) : Tthisstep=Tnextstep Tnextstep=Tnextstep+Tstep if Tnextstep > Tend: Tnextstep = Tend Tlast = True NONL = False if True : #while ( NONL == False ) : msgx=msg[Tthisstep:Tnextstep] whereisNL = msgx.find(NL) #print "chars: %s pos0: %s pos1: %s, posin: %s" % (Tend,Tthisstep,Tnextstep,whereisNL) if ( whereisNL == -1 ) : NONL = True else : Tnextstep = Tthisstep+whereisNL # reduce string to until NL msgx=msg[Tthisstep:Tnextstep] Tnextstep = Tnextstep+1 # skip NL Tlast = False # not finished if ( y < WINDOWHEIGHT - nextline ) : y=y+nextline txtcol = BLACK else : txtcol = RED DISPLAYSURF.blit(myfont.render(msgx,1,txtcol,WHITE),(ipadx,y)) #______________________________________________________________________ def sys_execute(): global msg,msgcall msg= "START" return_code = os.popen(msgcall).read() msg=return_code #______________________________________________________________________ def mymain(clinstr = "",executer=False): global DISPLAYSURF,msg,msgcall if ( clinstr <> "" ) : msgcall = clinstr pygame.init() FPSCLOCK = pygame.time.Clock() DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT)) pygame.display.set_caption(mytitle) DISPLAYSURF.fill(WHITE) draw_msg() pygame.display.update() FPSCLOCK.tick(FPS) mousex = 0 # used to store x coordinate of mouse event mousey = 0 # used to store y coordinate of mouse event while True: mouseClicked = False DISPLAYSURF.fill(WHITE) draw_msg() if ( executer == False ) : draw_button() else : sys_execute() # do it without wait for button click for event in pygame.event.get(): if event.type == QUIT: # close window[x] QUIT event pygame.display.quit() pygame.quit() sys.exit() elif event.type == MOUSEMOTION: mousex, mousey = event.pos elif event.type == MOUSEBUTTONDOWN: mousex, mousey = event.pos mouseClicked = True if ( sel_button(mousex, mousey) and ( executer == False )): if ( mouseClicked ) : # do START sys_execute() pass pygame.display.update() FPSCLOCK.tick(FPS) pygame.display.quit() pygame.quit() sys.exit() # _________________________________________________________________ def main(argv): clinstr = "" # clinstr like 'ls -la' executer=False try: opts, args = getopt.getopt(argv,"hi:e",["instr="]) except getopt.GetoptError: print 'usage: syscall2.py -h -i use like \'ls -la\' -e :not wait start button ' for opt, arg in opts: if opt == '-h' or opt == '--help': print 'usage: syscall2.py -h -i use like \'ls -la\' -e :not wait start button ' elif opt == '-i' or opt == '--instr': clinstr = arg #print "in: %s" % (clinstr) elif opt == '-e' or opt == '--exec': executer=True #print "exec" mymain(clinstr,executer) # _________________________________________________________________ if __name__ == '__main__': main(sys.argv[1:])