add old browse.py script from Korhal
authorColin Patrick Mccabe <cmccabe@alumni.cmu.edu>
Sat, 15 Jun 2013 23:37:09 +0000 (16:37 -0700)
committerColin Patrick Mccabe <cmccabe@alumni.cmu.edu>
Sat, 15 Jun 2013 23:38:42 +0000 (16:38 -0700)
Signed-off-by: Colin McCabe <cmccabe@alumni.cmu.edu>

browse.py [new file with mode: 0755]

diff --git a/browse.py b/browse.py
new file mode 100755 (executable)
index 0000000..1a0d7ce
--- /dev/null
+++ b/browse.py
@@ -0,0 +1,350 @@
+#!/usr/bin/env python
+import curses
+import dircache
+import os
+import string
+import time
+import sys
+import traceback
+
+def MyColor(str):
+    if (str == "BLACK"):
+       return 0;
+    if (str == "BLUE"):
+       return 1;
+    if (str == "GREEN"):
+       return 2;
+    if (str == "CYAN"):
+       return 3;
+    if (str == "RED"):
+       return 4;
+    if (str == "MAGENTA"):
+       return 5;
+    if (str == "YELLOW"):
+       return 6;
+    if (str == "WHITE"):
+       return 7;
+    if (str == "BLACK_GREEN"):
+       return 8;
+    if (str == "BLACK_CYAN"):
+       return 9;
+    if (str == "BLACK_RED"):
+       return 10;
+
+def GetFileTy(dirname, fileName):
+    if (dirname == '/kroot/media_unmount'): 
+       return 'umount_dir'
+    elif (os.path.isdir(dirname + fileName)): 
+       return 'dir'
+    else:
+       periodNum = string.rfind(fileName, '.')
+       if (periodNum < 0):
+           return 'notype'
+       extension = fileName[periodNum:].lower()
+       if extension == '.sh': return 'script'
+       elif extension == '.mp3': return 'music'
+       elif extension == '.mov': return 'movie'
+       elif extension == '.iso': return 'movieiso'
+       elif extension == '.divx': return 'movie'
+       elif extension == '.mpg': return 'movie'
+       elif extension == '.mp4': return 'movie'
+       elif extension == '.vob': return 'movie'
+       elif extension == '.avi': return 'movie'
+       elif extension == '.nes': return 'nes'
+       elif extension == '.smc': return 'snes'
+       elif extension == '.wav': return 'music' 
+    return ''
+
+def UseFile(filePath, fileTy):
+    StopCurses()
+    if fileTy == "script":
+       os.spawnl(os.P_WAIT, filePath, filePath)
+    elif fileTy == "music":
+       os.spawnl(os.P_NOWAIT, "/usr/bin/xmms", "/usr/bin/xmms", filePath)
+    elif fileTy == "movie":
+       os.spawnl(os.P_WAIT, "/usr/bin/aumix", "/usr/bin/aumix", "-v", "85");
+       os.spawnl(os.P_WAIT, "/usr/bin/aumix", "/usr/bin/aumix", "-w", "89");
+       os.spawnl(os.P_WAIT, "/usr/local/bin/mplayer", "/usr/local/bin/mplayer", filePath);
+    elif fileTy == "movieiso":
+       os.spawnl(os.P_WAIT, "/usr/local/bin/mplayer", "/usr/local/bin/mplayer", filePath);
+    elif fileTy == "nes":
+       os.spawnl(os.P_WAIT, "/usr/bin/aumix", "/usr/bin/aumix", "-v", "65");
+       os.spawnl(os.P_WAIT, "/usr/bin/aumix", "/usr/bin/aumix", "-w", "65");
+       #start ines
+       os.spawnl(os.P_NOWAIT, ines_path, ines_path,\
+"-ntsc", "-uperiod", "1", "-sync", "60", filePath) #"-nosound", 
+       #start the program that converts joystick presses to keypresses
+       os.spawnl(os.P_WAIT, joy2keyy_path, joy2keyy_path, "iNES Unix/X 3.0") 
+    elif fileTy == "snes":
+       os.spawnl(os.P_WAIT, "/usr/bin/aumix", "/usr/bin/aumix", "-v", "65");
+       os.spawnl(os.P_WAIT, "/usr/bin/aumix", "/usr/bin/aumix", "-w", "65");
+       os.spawnl(os.P_WAIT, "/usr/bin/snes9x", "/usr/bin/snes9x", \
+                  "-no",\
+"-joydev1", "/dev/input/js0",\
+"-joymap1", "1", "0", "3", "2", "10", "11", "5", "4",\
+"-joydev2", "/dev/input/js1",\
+"-joymap2", "1", "0", "3", "2", "10", "11", "5", "4",\
+filePath); #'"' +filePath + '"')
+#"-joymap1", "1", "2", "0", "3", "10", "11", "5", "4",\
+#"-joymap2", "1", "2", "0", "3", "10", "11", "5", "4",\
+    InitCurses()
+    stdscr.getch()
+    return
+
+def StopCurses():
+    #Stop the program
+    curses.nocbreak(); stdscr.keypad(0); curses.echo()
+    curses.endwin()
+    return
+
+
+def InitCurses():
+    global stdscr
+    stdscr = curses.initscr()
+    curses.noecho()
+    curses.cbreak()
+    stdscr.keypad(1)
+    curses.start_color()
+
+    curses.init_pair(1,curses.COLOR_BLACK,   curses.COLOR_BLACK)
+    curses.init_pair(1,curses.COLOR_BLUE,    curses.COLOR_BLACK)
+    curses.init_pair(2,curses.COLOR_GREEN,   curses.COLOR_BLACK)
+    curses.init_pair(3,curses.COLOR_CYAN,    curses.COLOR_BLACK)
+    curses.init_pair(4,curses.COLOR_RED,     curses.COLOR_BLACK)
+    curses.init_pair(5,curses.COLOR_MAGENTA, curses.COLOR_BLACK)
+    curses.init_pair(6,curses.COLOR_YELLOW,  curses.COLOR_BLACK)
+    curses.init_pair(7,curses.COLOR_WHITE,   curses.COLOR_BLACK)
+    curses.init_pair(12,curses.COLOR_BLACK,   curses.COLOR_GREEN)
+    curses.init_pair(13,curses.COLOR_BLACK,   curses.COLOR_CYAN)
+    curses.init_pair(14,curses.COLOR_BLACK,   curses.COLOR_RED)
+    return
+
+try:
+    #consts
+    global mplayer_path
+    global ines_path
+    global joy2key_path
+
+    #globals
+    global sleeping
+    global stdscr
+    global highNum 
+    global curdir
+    global boostX, topFileNum 
+    global numSongsOnScreen 
+
+    global TRUE
+    TRUE = 1
+    global FALSE
+    FALSE = 0
+
+    #curses initialization
+    InitCurses()
+
+    #consts
+    mplayer_path = "/usr/bin/mplayer"
+    ines_path = "/usr/bin/ines"
+    joy2keyy_path ="/home/tvman/bin/joy2keyy" 
+
+    #variable initialization
+    highNum = 0
+    boostX, topFileNum = (0,0)
+    maxY, maxX = stdscr.getmaxyx()
+    numSongsOnScreen = maxY - 2 - 2
+    sleepingPid = -1
+    sleepingFile = ""
+    curdir = "/b/"
+
+#    ShutDown();
+#    print numSongsOnScreen;
+#    sys.exit(0);
+
+    #pad = curses.newpad(100, 100)
+
+    #These loops fill the pad with letters; this is
+    #explained in the next section
+#    for y in range(0, 100):
+#      for x in range(0, 100):
+#          try: stdscr.addch(y,x, ord('a') + (x*x+y*y) % 26 )
+#          except curses.error: pass
+
+#    s = curses.newwin(5,10,2,1)
+#    s.box()
+#    s.refresh();
+
+    def Redraw():
+       y = 0
+       stdscr.clear()
+       stdscr.addstr(0, 0, "*" * (maxX - 1))
+       stdscr.addstr(1, 0, " " * 32 + "Welcome to Korhal")
+       stdscr.addstr(2, 0, "*" * (maxX - 1))
+       stdscr.addstr(maxY - 1, 0, "*" * (maxX - 1))
+       for f in dircache.listdir(curdir)[topFileNum:]:
+           if (y + topFileNum == highNum):
+               stdscr.addstr(2+y+1, 1, f[:maxX-1], curses.A_STANDOUT)
+           else:
+               fileTy = GetFileTy(curdir, f)
+               if (fileTy == "script"): color = "RED"
+               elif (fileTy == "mp3"): color = "GREEN"
+               elif (fileTy == "dir"): color = "BLUE"
+               elif (fileTy == "movie"): color = "YELLOW"
+               elif (fileTy == "movieiso"): color = "YELLOW"
+               elif (fileTy == "nes"): color = "MAGENTA"
+               elif (fileTy == "snes"): color = "MAGENTA"
+               else: color = "WHITE"
+               stdscr.addstr(2+y+1, 1, f[:maxX-1], \
+                   curses.color_pair(MyColor(color)))
+           y = y + 1;
+           if (y == numSongsOnScreen):
+               break
+       return
+    
+    def MoveUp(i):
+       global highNum
+       global topFileNum 
+
+       highNum = highNum - i
+       if (highNum < 0):
+           highNum = 0
+       if (highNum < topFileNum):
+           topFileNum = highNum
+       return
+
+    def MoveDown(i):
+       global highNum
+       global topFileNum 
+
+       highNum = highNum + i
+       if highNum >= len(dircache.listdir(curdir)):
+           highNum = len(dircache.listdir(curdir)) - 1
+       if (highNum >= topFileNum + numSongsOnScreen):
+           topFileNum = highNum - (numSongsOnScreen - 1)
+       return
+
+#    def Play(s):
+#      global stdscr
+#      global sleepingPid
+#      global sleepingFile
+#
+#      stdscr.clear()
+#      stdscr.addstr(int(maxY / 2), 1, "Now Playing \"" + s + "\"")
+#
+#      if (sleepingFile == s):
+#          #if this is the player process we put to sleep earlier,
+#          #wake it up
+#          os.spawnl(os.P_WAIT, '/usr/bin/kill', '/usr/bin/kill', 
+#                      '-s', 'SIGCONT', str(sleepingPid))
+#          pid = sleepingPid
+#      else:
+#          #kill any lingering sleeping player that might exist
+#          if (sleepingPid != -1):
+#              os.spawnl(os.P_WAIT, '/usr/bin/kill', '/usr/bin/kill', 
+#                      '-s', 'SIGCONT', str(sleepingPid))
+#              os.spawnl(os.P_WAIT, '/usr/bin/kill', '/usr/bin/kill', 
+#                      '-s', 'SIGTERM', str(sleepingPid))
+#          try:
+#              os.wait()
+#          except:
+#              pass
+#
+#          #create new player
+#          pid = os.fork()
+#          if (pid == 0):
+#              #curses.endwin()
+#              os.execl(mpg123path, mpg123path, "-o", "esd", s)
+#              #stdscr = curses.initscr()
+#          
+#      sleepingPid = -1
+#      sleepingFile = ""
+#      while 1:
+#          c = stdscr.getch()
+#          #stdscr.clear()
+#          try:
+#              (_,waitret) = os.waitpid(pid, os.WNOHANG)
+#          except:
+#              return c
+#          #if (os.WIFEXITED(waitret) or os.WIFSIGNALED(waitret)):
+#          if (waitret != 0):
+#              #Mpg123 process has exited; break from this loop 
+#              #and treat this keypress as if it had happened in the main
+#              #input loop.
+#              return c
+#          if (c == ord('q')):
+#              os.spawnl(os.P_WAIT, '/usr/bin/kill', '/usr/bin/kill', str(pid))
+#              try:
+#                  os.wait()
+#              except:
+#                  pass
+#              #stdscr.addstr(int(maxY / 2), 1, "/usr/bin/kill"+str(pid))
+#              #os.spawnl('/usr/bin/kill', '/usr/bin/kill', str(pid))
+#              return -1
+#          elif (c == ord(' ')):
+#              sleepingPid = pid
+#              sleepingFile = s
+#              os.spawnl(os.P_WAIT, '/usr/bin/kill', '/usr/bin/kill',
+#                          '-s', 'SIGSTOP', str(sleepingPid))
+#              return -1
+
+    #Displays a section of the pad in the middle of the screen
+    #pad.refresh( 0,0, 5,5, 20,75)
+#    dirl = dircache.listdir(curdir)
+#    stdscr.addstr(1, 1, curdir + dirl[2], curses.A_STANDOUT)
+
+    #wait for user input
+    while 1:
+       Redraw()
+       nextc = stdscr.getch()
+       #if nextc == ord('q'):
+           #StopCurses()
+           #break
+       if nextc == curses.KEY_UP:
+           MoveUp(1)
+       elif nextc == curses.KEY_DOWN:
+           MoveDown(1)
+       elif nextc == curses.KEY_PPAGE:
+           MoveUp(numSongsOnScreen)
+       elif nextc == curses.KEY_NPAGE:
+           MoveDown(numSongsOnScreen)
+       elif nextc == ord(' '):
+           MoveDown(int(numSongsOnScreen / 2))
+       elif nextc == ord('a'):
+           fileName = dircache.listdir(curdir)[highNum]
+           path = curdir + fileName
+           UseFile(path, "music")
+       elif nextc == 10 or nextc == curses.KEY_RIGHT:
+           #Special case: if the current directory contains no items,
+           #do nothing.
+           if (dircache.listdir(curdir) != []):
+               fileName = dircache.listdir(curdir)[highNum]
+               path = curdir + fileName
+               fileTy = GetFileTy(curdir, fileName)
+               if (fileTy == 'umount_dir'):
+                   os.execl("umount", "umount", path)
+                   os.execl("eject", "eject", path)
+               elif (fileTy == 'dir'):
+                   curdir = path + "/"
+                   highNum = 0
+                   topFileNum = 0
+               else:
+                   UseFile(path, fileTy)
+
+       elif nextc == curses.KEY_LEFT:
+           highSlash = string.rfind(curdir[:-1], "/")
+           if (highSlash > 0):
+               ncurdir = curdir[0:highSlash+1]
+           else:
+               ncurdir = "/"
+
+           #Only really change the directory if we haven't 
+           #stepped out of the kroot area
+           hasRoot = string.find(ncurdir, "/b/")
+           if (hasRoot >= 0):
+               curdir = ncurdir
+
+           highNum = 0
+           topFileNum = 0
+           nextc = -1
+
+except:
+    StopCurses()
+    traceback.print_exc()