-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyMain.py
More file actions
173 lines (140 loc) · 7.43 KB
/
PyMain.py
File metadata and controls
173 lines (140 loc) · 7.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import json
import os
import re
import shutil
import sys
import tempfile
import time
import pygame
import subprocess
from pgu import gui
from pygame.locals import *
from Constants import *
from Logger import logger
from pgu.gui import Theme
from surfaces.LeftMenu import LeftMenu
from surfaces.MainArea import MainArea
from surfaces.StatusBar import StatusBar
from surfaces.TopMenu import TopMenu
from utils import AssetManager
from utils import FileSystemHelper
if not pygame.font:
print 'Warning, fonts disabled'
if not pygame.mixer:
print 'Warning, sound disabled'
BASE_COMMAND = "/usr/bin/xinit /usr/bin/dbus-launch --exit-with-session %s -- :0 -nolisten tcp vt7"
class PyMain(object):
def __init__(self, configFile):
pygame.init()
logger.info("Using driver : " + pygame.display.get_driver())
# self.disableMouse()
self.initSurfaces()
self.initJoysticks()
self.temporaryFile = os.path.join(tempfile.gettempdir(), 'thinlauncher.tmp')
self.jsondata = json.load(open(FileSystemHelper.findConfig(configFile), 'rb'))
self.backgroundColor = eval(self.jsondata['backgroundColor'])
if 'backgroundImage' in self.jsondata:
self.backgroundImage = AssetManager.load_image(self.jsondata['backgroundImage'])
self.backgroundImage = pygame.transform.smoothscale(self.backgroundImage, (self.screen.get_width(), self.screen.get_height()))
else:
self.backgroundImage = None
self.topMenuSurface.init(self.jsondata['menus'])
self.setTopSelected(0)
self.setLeftSelected(0)
self.redraw()
def disableMouse(self):
devices = subprocess.check_output(["xinput"]).splitlines()
for line in devices:
logger.debug(line)
if re.search("(master|slave)\s*pointer", line):
deviceID = re.findall("id=([0-9]+)", line)[0]
logger.info("Trying to disable device %s" % deviceID)
subprocess.call(["xinput", "disable", deviceID])
def initSurfaces(self):
self.screen = pygame.display.set_mode((SCREEN_RES_X, SCREEN_RES_Y), SRCALPHA)
screenWidth = self.screen.get_width()
screenHeight = self.screen.get_height()
self.topMenuSurface = TopMenu(screenWidth, TOP_MENU_HEIGHT)
self.leftMenuSurface = LeftMenu(LEFT_MENU_WIDTH, screenHeight - TOP_MENU_HEIGHT - STATUS_BAR_HEIGHT)
self.statusBarSurface = StatusBar(screenWidth, STATUS_BAR_HEIGHT)
self.mainAreaSurface = MainArea(screenWidth - LEFT_MENU_WIDTH, screenHeight - TOP_MENU_HEIGHT - STATUS_BAR_HEIGHT)
logger.info("Screen created with resolution of %dx%d" % (screenWidth, screenHeight))
logger.info("TopMenu created with resolution of %dx%d" % (self.topMenuSurface.get_width(), self.topMenuSurface.get_height()))
logger.info("LeftMenu created with resolution of %dx%d" % (self.leftMenuSurface.get_width(), self.leftMenuSurface.get_height()))
logger.info("StatusBar created with resolution of %dx%d" % (self.statusBarSurface.get_width(), self.statusBarSurface.get_height()))
logger.info("MainArea created with resolution of %dx%d" % (self.mainAreaSurface.get_width(), self.mainAreaSurface.get_height()))
def initJoysticks(self):
for i in range(pygame.joystick.get_count()):
joystick = pygame.joystick.Joystick(i)
joystick.init()
logger.info("Found joystick %s" % (joystick.get_name(),))
def redraw(self):
# TODO : The whole concept of only partially redrawing is bad.
# TODO : Should try to figure out why redrawing is so expensive
self.screen.fill(self.backgroundColor)
if self.backgroundImage:
self.screen.blit(self.backgroundImage, self.backgroundImage.get_rect())
self.topMenuSurface.redraw(self.screen, 0, 0)
self.leftMenuSurface.redraw(self.screen, 0, TOP_MENU_HEIGHT)
self.statusBarSurface.redraw(self.screen, 0, TOP_MENU_HEIGHT + self.leftMenuSurface.get_height())
self.mainAreaSurface.redraw(self.screen, LEFT_MENU_WIDTH, TOP_MENU_HEIGHT)
pygame.display.flip()
def setTopSelected(self, index):
self.topMenuSurface.setSelected(index)
self.leftMenuSurface.init(self.jsondata['menus'][index]['entries'])
self.setLeftSelected(0)
def setLeftSelected(self, index):
self.leftMenuSurface.setSelected(index)
self.mainAreaSurface.init(self.leftMenuSurface.data[index])
def writeCommand(self, entry, command):
ff = open(self.temporaryFile, 'wb')
ff.write(command)
ff.close()
logger.info("Launching %s with command %s" % (entry['name'], command))
def loop(self):
while 1:
event = pygame.event.wait()
# if (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE) \
# or (event.type == pygame.JOYBUTTONDOWN and event.button == 1):
if (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE) \
or (event.type == pygame.QUIT):
if os.path.exists(self.temporaryFile):
os.remove(self.temporaryFile)
sys.exit(0)
elif (event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT) \
or (event.type == pygame.JOYBUTTONDOWN and event.button == 11):
topMenuIndex = (self.topMenuSurface.getSelected() - 1) % len(self.topMenuSurface.buttons)
self.setTopSelected(topMenuIndex)
self.redraw()
elif (event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT) \
or (event.type == pygame.JOYBUTTONDOWN and event.button == 12):
topMenuIndex = (self.topMenuSurface.getSelected() + 1) % len(self.topMenuSurface.buttons)
self.setTopSelected(topMenuIndex)
self.redraw()
elif (event.type == pygame.KEYDOWN and event.key == pygame.K_UP) \
or (event.type == pygame.JOYBUTTONDOWN and event.button == 13):
leftMenuIndex = (self.leftMenuSurface.getSelected() - 1) % len(self.leftMenuSurface.buttons)
self.setLeftSelected(leftMenuIndex)
self.redraw()
elif (event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN) \
or (event.type == pygame.JOYBUTTONDOWN and event.button == 14):
leftMenuIndex = (self.leftMenuSurface.getSelected() + 1) % len(self.leftMenuSurface.buttons)
self.setLeftSelected(leftMenuIndex)
self.redraw()
elif (event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN) \
or (event.type == pygame.JOYBUTTONDOWN and event.button == 0):
entry = self.leftMenuSurface.data[self.leftMenuSurface.getSelected()]
# We run a command if there is one
if 'command' in entry:
command = entry['command']
if command[0] == '#':
self.writeCommand(entry, command[1:])
sys.exit(0)
elif command[0] == '$':
subprocess.call(command[1:], shell=True)
else:
self.writeCommand(entry, BASE_COMMAND % command)
sys.exit(0)
# If we have a main area GUI, we should activate it and start working on it
if 'mainAreaGUI' in entry:
pass