-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplastey.py
executable file
·554 lines (478 loc) · 23.2 KB
/
plastey.py
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#!/usr/bin/env python3
## INFO ########################################################################
## ##
## plastey ##
## ======= ##
## ##
## Oculus Rift + Leap Motion + Python 3 + C + Blender + Arch Linux ##
## Version: 0.2.2.090 (20150514) ##
## File: plastey.py ##
## ##
## For more information about the project, visit ##
## <http://plastey.kibu.hu>. ##
## Copyright (C) 2015 Peter Varo, Kitchen Budapest ##
## ##
## This program is free software: you can redistribute it and/or modify it ##
## under the terms of the GNU General Public License as published by the ##
## Free Software Foundation, either version 3 of the License, or ##
## (at your option) any later version. ##
## ##
## This program is distributed in the hope that it will be useful, but ##
## WITHOUT ANY WARRANTY; without even the implied warranty of ##
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ##
## See the GNU General Public License for more details. ##
## ##
## You should have received a copy of the GNU General Public License ##
## along with this program, most likely a file in the root directory, ##
## called 'LICENSE'. If not, see <http://www.gnu.org/licenses>. ##
## ##
######################################################################## INFO ##
# Import python modules
from queue import Queue
from os.path import isfile
from threading import Thread
from configparser import ConfigParser
from os.path import join, expanduser
from pickle import dump, HIGHEST_PROTOCOL
from os import makedirs, listdir, remove
from subprocess import Popen, PIPE, TimeoutExpired
from tkinter import (Tk,
Toplevel,
Label,
Entry,
Button,
Radiobutton,
StringVar,
BooleanVar,
TOP,
BOTH,
CENTER,
W as WEST,
E as EAST,
NW as NORTH_WEST)
# Import plastey modules
from comm_setup import setup, check, CommunicationSetupError
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Read configuration
config = ConfigParser()
with open('config.ini', encoding='utf-8') as file:
config.read_file(file)
# Module level constants
MODE_SINGLE_PLAYER = False
MODE_MULTI_PLAYER = True
BASE_OPENED_GEOMETRY = False
BASE_CLOSED_GEOMETRY = True
COMM_SOCKET_CLIENT = False
COMM_SOCKET_SERVER = True
COMM_THIS_HOST = config['Communication']['this_host']
COMM_THIS_PORT = config['Communication']['this_port']
COMM_OTHER_HOST = config['Communication']['other_host']
COMM_OTHER_PORT = config['Communication']['other_port']
ADDR_NO_ADDRESS = 'Address not bound.'
ADDR_HAVE_ADDRESS = 'Address bound.'
CONN_NOT_CONNECTED = 'Disconnected.'
CONN_CONNECTED = 'Connected.'
GUI_PAD_X = 16
GUI_PAD_Y = GUI_PAD_X
GUI_SECTION_PAD_X = 32
GUI_SECTION_PAD_Y = GUI_SECTION_PAD_X
DRAW_FULL_SCREEN = bool(eval(config['Render']['full_screen']))
DRAW_DISPLAY_X = int(config['Render']['display_x'])
DRAW_DISPLAY_Y = int(config['Render']['display_y'])
DRAW_RESOLUTION_X = int(config['Render']['resolution_x'])
DRAW_RESOLUTION_Y = int(config['Render']['resolution_y'])
FILE_TEMPORARY_FOLDER = expanduser(config['Internal']['temp_base_dir'])
FILE_PERMANENT_FOLDER = expanduser(config['Internal']['permanent_save_dir'])
FILE_AUTO_SAVE_FOLDER = expanduser(join(FILE_TEMPORARY_FOLDER,
config['Internal']['temp_auto_save_dir']))
FILE_TEMP_SAVE_FOLDER = expanduser(join(FILE_TEMPORARY_FOLDER,
config['Internal']['temp_save_folder']))
FILE_TEMP_STATE_FOLDER = expanduser(join(FILE_TEMPORARY_FOLDER,
config['Internal']['temp_states']))
FILE_TEMP_FEEDS_FOLDER = expanduser(join(FILE_TEMPORARY_FOLDER,
config['Internal']['temp_feedbacks']))
FILE_STATE_SHUT_DOWN = join(FILE_TEMP_STATE_FOLDER, config['Internal']['state_shut_down'])
FILE_STATE_RESTART = join(FILE_TEMP_STATE_FOLDER, config['Internal']['state_restart'])
FILE_STATE_RECOVER = join(FILE_TEMP_STATE_FOLDER, config['Internal']['state_recover_auto'])
FILE_STATE_DONE = join(FILE_TEMP_STATE_FOLDER, config['Internal']['state_done'])
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Set external module level constants
with open('WARNING', encoding='utf-8') as file:
WARN_TEXT = file.read()
#------------------------------------------------------------------------------#
class Report(Toplevel):
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def __init__(self, message, *args, **kwargs):
Toplevel.__init__(self, *args, **kwargs)
Label(master = self,
text = message).pack()
Button(master = self,
text = 'OK',
command = self.destroy)
#------------------------------------------------------------------------------#
class Password(Toplevel):
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
@property
def on_return(self):
return self._on_return
@on_return.setter
def on_return(self, value):
self._on_return = value
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def __init__(self, string_var, *args, **kwargs):
Toplevel.__init__(self, *args, **kwargs)
self.wm_title('SUDO Password')
Label(master = self,
text = 'Root password:').pack()
self._input = input = Entry(master = self,
show = '*')
def set_pass(*args, **kwargs):
string_var.set(input.get())
self.destroy()
input.bind('<KeyRelease-Return>', set_pass)
#input.pack(side=TOP, fill=BOTH, expand=True)
input.pack()
input.focus_set()
Button(master = self,
text = 'Cancel',
command = self.destroy).pack()
Button(master = self,
text = 'OK',
command = set_pass).pack()
#------------------------------------------------------------------------------#
class Plastey(Tk):
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
# Set window title
self.wm_title('Plastey Configurator')
# Create GUI driven variables
self._mode = BooleanVar()
self._base = BooleanVar()
self._comm = BooleanVar()
self._pass = StringVar()
self._addressed = StringVar()
self._connected = StringVar()
self._this_host = StringVar()
self._this_port = StringVar()
self._other_host = StringVar()
self._other_port = StringVar()
# Create GUI
self._build_gui()
# Set default values for GUI driven variables
self._mode.set(MODE_SINGLE_PLAYER)
self._base.set(BASE_OPENED_GEOMETRY)
self._comm.set(COMM_SOCKET_SERVER)
self._pass.set('')
self._addressed.set(ADDR_HAVE_ADDRESS if check(COMM_THIS_HOST) else ADDR_NO_ADDRESS)
self._connected.set(CONN_NOT_CONNECTED)
self._this_host.set(COMM_THIS_HOST)
self._this_port.set(COMM_THIS_PORT)
self._other_host.set(COMM_THIS_HOST)
self._other_port.set(COMM_OTHER_PORT)
# Follow changes on password
self._pass.trace('w', self._on_bind_address)
# Create folder structures if they don't exists yet
makedirs(FILE_TEMPORARY_FOLDER, exist_ok=True)
makedirs(FILE_PERMANENT_FOLDER, exist_ok=True)
makedirs(FILE_TEMP_SAVE_FOLDER, exist_ok=True)
makedirs(FILE_AUTO_SAVE_FOLDER, exist_ok=True)
#makedirs(FILE_TEMP_STATE_FOLDER, exist_ok=True)
#makedirs(FILE_TEMP_FEEDS_FOLDER, exist_ok=True)
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def _build_gui(self):
# Create GUI sections
row = 0
col = 0
# Warning text
Label(master = self,
text = WARN_TEXT,
anchor = WEST,
justify = CENTER).grid(row = row,
column = col,
sticky = NORTH_WEST,
rowspan = 16)
# Set column spacing
self.columnconfigure(index = col,
pad = GUI_SECTION_PAD_X)
row = 0
col += 1
# Game mode options
Label(master = self,
text = 'Game Mode:').grid(row = row,
column = col,
sticky = WEST)
row += 1
Radiobutton(master = self,
text = 'Single Player',
value = MODE_SINGLE_PLAYER,
variable = self._mode).grid(row = row,
column = col,
sticky = WEST)
row += 1
Radiobutton(master = self,
text = 'Multi Player',
value = MODE_MULTI_PLAYER,
variable = self._mode).grid(row = row,
column = col,
sticky = WEST)
row += 1
# Base object modes
Label(master = self,
text = 'Base Object:').grid(row = row,
column = col,
sticky = WEST)
row += 1
Radiobutton(master = self,
text = 'Plane mesh',
value = BASE_OPENED_GEOMETRY,
variable = self._base).grid(row = row,
column = col,
sticky = WEST)
row += 1
Radiobutton(master = self,
text = 'Sphere mesh',
value = BASE_CLOSED_GEOMETRY,
variable = self._base).grid(row = row,
column = col,
sticky = WEST)
row += 1
# Start oculus-daemon
Label(master = self,
text = 'Daemons:').grid(row = row,
column = col,
sticky = WEST)
row += 1
Button(master = self,
text = 'Start OVRD',
command = self._on_start_oculus_daemon).grid(row = row,
column = col,
sticky = WEST)
# Set column spacing
self.columnconfigure(index = col,
pad = GUI_SECTION_PAD_X)
row = 0
col += 1
# Multiplayer mode options
Label(master = self,
text = 'Multi Player Options:').grid(row = row,
column = col,
sticky = WEST,
columnspan = 2)
row += 1
Label(master = self,
text = 'This role:').grid(row = row,
column = col,
sticky = WEST)
Radiobutton(master = self,
text = 'Server',
value = COMM_SOCKET_SERVER,
variable = self._comm).grid(row = row,
column = col + 1,
sticky = WEST)
row += 1
Radiobutton(master = self,
text = 'Client',
value = COMM_SOCKET_CLIENT,
variable = self._comm).grid(row = row,
column = col + 1,
sticky = WEST)
row += 1
Label(master = self,
text = 'This host:').grid(row = row,
column = col,
sticky = WEST)
Entry(master = self,
textvariable = self._this_host).grid(row = row,
column = col + 1,
sticky = WEST)
row += 1
Label(master = self,
text = 'This port:').grid(row = row,
column = col,
sticky = WEST)
Entry(master = self,
textvariable = self._this_port).grid(row = row,
column = col + 1,
sticky = WEST)
row += 1
Label(master = self,
text = 'Other host:').grid(row = row,
column = col,
sticky = WEST)
Entry(master = self,
textvariable = self._other_host).grid(row = row,
column = col + 1,
sticky = WEST)
row += 1
Label(master = self,
text = 'Other port:').grid(row = row,
column = col,
sticky = WEST)
Entry(master = self,
textvariable = self._other_port).grid(row = row,
column = col + 1,
sticky = WEST)
row += 1
Button(master = self,
text = 'Bind address',
command = self._on_ask_password).grid(row = row,
column = col,
sticky = WEST + EAST)
Label(master = self,
textvariable = self._addressed).grid(row = row,
column = col + 1,
sticky = WEST)
row += 1
Button(master = self,
text = 'Connect machines',
command = self._on_bind_address).grid(row = row,
column = col,
sticky = WEST + EAST)
Label(master = self,
textvariable = self._connected).grid(row = row,
column = col + 1,
sticky = WEST)
# Set column spacing
self.columnconfigure(index = col + 1,
pad = GUI_SECTION_PAD_X)
row = 0
col += 2
# Controller buttons
Label(master = self,
text = 'Controllers:').grid(row = row,
column = col,
sticky = WEST)
row += 1
Button(master = self,
text = 'Start game',
command = self._on_start_game).grid(row = row,
column = col,
sticky = WEST + EAST)
row += 1
Button(master = self,
text = 'Restart game',
command = self._on_restart_game).grid(row = row,
column = col,
sticky = WEST + EAST)
row += 1
Button(master = self,
text = 'Stop game',
command = self._on_stop_game).grid(row = row,
column = col,
sticky = WEST + EAST)
row += 1
Button(master = self,
text = 'Save last mesh',
command = self._on_save_mesh).grid(row = row,
column = col,
sticky = WEST + EAST)
row += 1
Button(master = self,
text = 'Load last mesh',
command = self._on_load_mesh).grid(row = row,
column = col,
sticky = WEST + EAST)
row += 1
Button(master = self,
text = 'Save log file',
command = self._on_save_log).grid(row = row,
column = col,
sticky = WEST + EAST)
row += 1
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def _on_ask_password(self, *args, **kwargs):
# Create a password-dialog
self._dialog = Password(self._pass)
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def _on_start_oculus_daemon(self, *args, **kwargs):
print('starting daemon...')
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def _on_bind_address(self, *args, **kwargs):
# Check for status and if address is not bound
if not check(COMM_THIS_HOST):
# Bind address
try:
setup(self._this_host, user_pass=self._pass.get())
except CommunicationSetupError as exception:
Report(exception.error)
# Check status and report to user
self._addressed.set(ADDR_HAVE_ADDRESS if check(COMM_THIS_HOST)
else ADDR_NO_ADDRESS)
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def _on_connect(self, *args, **kwargs):
pass
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def _on_start_game(self, *args, **kwargs):
# HACK: Is this really the best option we have, to get into fullscreen,
# other than using the blender's fullscreen option, which will
# unfortunately resize the display's resolution??? :(
window_command = ['sleep 1']
window_command.append('wmctrl -r :ACTIVE: '
'-e 0,{},{},{},{}'.format(DRAW_DISPLAY_X,
DRAW_DISPLAY_Y,
DRAW_RESOLUTION_X,
DRAW_RESOLUTION_Y))
if DRAW_FULL_SCREEN:
window_command.append('wmctrl -r :ACTIVE: -b add,fullscreen')
Popen(args = ' && '.join(window_command),
shell = True,
stdin = PIPE,
stderr = PIPE,
universal_newlines=True)
# Store subprocess, for further communication
self._pipe = Popen(args = './plastey',
stdin = PIPE,
stdout = PIPE,
stderr = PIPE,
universal_newlines=True)
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def _on_restart_game(self, *args, **kwargs):
self._pipe.stdin.write('hello-world\n')
#if not self._locked:
# self._locked = True
# with open(FILE_STATE_RESTART, mode='w') as file:
# file.write('')
# Thread(name = 'feedbackd-restart',
# target = self._get_feedback_to_clean_up).start()
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def _on_stop_game(self, *args, **kwargs):
#if not self._locked:
# self._locked = True
# with open(FILE_STATE_SHUT_DOWN, mode='w') as file:
# file.write('')
# Thread(name = 'feedbackd-shutdown',
# target = self._get_feedback_to_clean_up).start()
self._pipe.communicate('hello-world\n')
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def _on_save_mesh(self, *args, **kwargs):
pass
#if self._pipe.poll():
# print('saving inside game')
#else:
# print('save last auto-saved')
#print('parent says: hey there, daemon!\n')
#self._pipe.stdin.write('hey there, daemon!\n')
#self._pipe.stdin.close()
#try:
# self._pipe.communicate('hey there, daemon!\n', timeout=0.1)
## Hack: since communicate waits for the subprocess to terminate, the
## timeout value is necessary, however, after the timeout, the app
## won't terminate either. One solution should be
## self._pipe.stdin.write instead of the communicate method, but
## unfortunately the process's stdin.read/input are not getting
## anything.. is it because the value set to shell=True?
#except TimeoutExpired:
# return
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def _on_load_mesh(self, *args, **kwargs):
pass
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def _on_save_log(self, *args, **kwargs):
pass
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def run(self):
self.mainloop()
#------------------------------------------------------------------------------#
if __name__ == '__main__':
Plastey().run()