-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·668 lines (586 loc) · 24.2 KB
/
main.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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#===============================================================================
# Written by Rentouch 2012 - http://www.rentouch.ch
#===============================================================================
#global
import os
import time
import multiprocessing
import subprocess
from threading import Thread
import gtk
import dbus
import sqlite3
import dbus.service
import signal
import socket
import logging
from dbus.mainloop.glib import DBusGMainLoop
#initialize thread support in gtk
gtk.threads_init()
#gtk.gdk.threads_init()
"""
PBController
============
The PBController starts all the progs in a seperate process. Also it regulates
the the desktops of Kwin. To use PBCntroller successfully you have to use Kwin.
Else it will just be a little mess. -> Switching between different progs is not
possible. If you want to use this with compiz you have to write the addition
yourself.
!For kwin you have to install the tool WMIface first!
"""
#initialize logger
log = logging.getLogger('PBUpdater')
log.setLevel(logging.INFO)
hdlr = logging.FileHandler('data/logs/log.log')
consoleHandler = logging.StreamHandler()
formatter = logging.Formatter("%(asctime)s [%(levelname)-8s] %(module)s%(lineno)-3s %(message)s")
hdlr.setFormatter(formatter)
consoleHandler.setFormatter(formatter)
log.addHandler(hdlr)
log.addHandler(consoleHandler)
log.info("*-----------PBController started-----------*")
class Controller():
'''This is the controller class. It will start, stop progs and receive the
events when a prog is opened or closed. It is also handling the kwin desktop
switching when kwin is available. Without kwin only one prog can be open at
a time.
'''
progs={}
def __init__(self):
log.info("PBController has started")
#init dbus client
self.bus = dbus.SessionBus()
def open_prog(self, prog_id):
'''Open a particular prog.
If Kwin is not available it will just launch one prog at a time.
Also it will not do any desktop switching.
:param prog_id: id of prog which should be opened
'''
prog_id=int(prog_id)
if prog_id not in self.progs:
#if this is the first run since runtime make a new prog instance
self.progs[prog_id]=Prog(self, prog_id, self.get_path_from_db(prog_id),
tuio.create_new_port(), ignore_region=(0.9479166667,
0.9074074074,
0.9739583333,
0.9537037037))
#check if the prog is already running
if self.progs[prog_id].open:
self.switch_to_desktop(self.progs[prog_id].desktop)
tuio.add_port(self.progs[prog_id].TUIOport)
else:
if self.kwin_is_available():
#is a desktop free?
c=0
for prog in self.progs:
if self.progs[prog].desktop!=None:
c+=1
if c>= self.get_number_of_desktops()-1:
#all desktops are full
self.prog_opening_failed(prog_id, "NoFreeDesktop")
return False
#if it is not running, try to start it & start tuio for prog
tuio.add_port(self.progs[prog_id].TUIOport)
if self.progs[prog_id].start():
#if kwin is available
if self.kwin_is_available():
self.progs[prog_id].desktop=self.get_current_desktop()
self.prog_opened(prog_id)
else:
#if start fails: add pbase port again
tuio.add_port(3335)
self.prog_opening_failed(prog_id, "Start of Prog failed")
return False
def prog_opened(self, prog_id):
'''Call over DBUS to PBase&PBSwitch: prog_opened
:param prog_id: id of prog which just opened
'''
print "prog opened", prog_id
server=self.try_reach_dbus_PBase()
print "prog opened2", prog_id
if server:
print "prog opened2.1", prog_id
try:
print "prog opened2.2", prog_id
server.prog_opened(prog_id, dbus_interface = 'org.PB.PBase')
print "prog opened2.3", prog_id
except dbus.exceptions.DBusException, e:
print "prog opened2.4", prog_id
print "dbus exception from main from pbcontroller:", e
print "prog opened2.5", prog_id
print "prog opened3", prog_id
try:
server = self.bus.get_object('org.PB.PBSwitch', '/PBSwitch')
print "prog opened4", prog_id
try:
server.prog_opened(prog_id, dbus_interface = 'org.PB.PBSwitch')
except dbus.exceptions.DBusException, e:
print "in controller main", e
print "prog opened4", prog_id
except Exception, e:
print "prog opened5", prog_id
print e
def stop_prog(self, prog_id):
'''This function kills/stops a prog.
:param prog_id: prog_id of prog which should be killed/stopped
'''
PID=self.progs[prog_id].PID
log.info("kill prog:"+str(PID))
try:
os.killpg(PID, signal.SIGTERM)
except:
os.system("kill "+str(PID))
os.system("kill -9 "+str(PID))
return False
def stop_all_progs(self):
for prog in self.progs: self.stop_prog(prog)
for prog in self.progs: self.progs[prog].threading=False
def prog_closed(self, prog_id):
'''Call over DBUS to PBase: prog_closed
Stop TUIO port for this prog
Start TUIO for PBase
:param prog_id: id of prog which just closed
'''
prog_id=int(prog_id)
#set desktop of prog=None
self.progs[prog_id].desktop=None
#start tuio for pbase
tuio.add_port(3335)
server=self.try_reach_dbus_PBase()
if server:
try:
server.prog_closed(prog_id, dbus_interface = 'org.PB.PBase')
except dbus.exceptions.DBusException:
log.exception('failed to connect to pbase dbus')
try:
server = self.bus.get_object('org.PB.PBSwitch', '/PBSwitch')
try:
server.prog_closed(prog_id, dbus_interface = 'org.PB.PBSwitch')
except dbus.exceptions.DBusException:
log.exception('dbus exception while calling main')
except Exception:
log.exception('exception:')
return False
def prog_opening_failed(self, prog_id, reason):
server=self.try_reach_dbus_PBase()
if server:
try:
server.prog_opening_failed(prog_id, reason, dbus_interface = 'org.PB.PBase')
except dbus.exceptions.DBusException:
log.exception('dbus-exception')
def load_pbase(self):
'''Call over DBUS to PBase: load pbase
'''
server=self.try_reach_dbus_PBase()
if server:
try:
server.load(dbus_interface = 'org.PB.PBase')
except dbus.exceptions.DBusException:
log.exception('dbus-exception')
return False
def unload_pbase(self):
'''Call over DBUS to PBase: unload
'''
server=self.try_reach_dbus_PBase()
if server:
try:
server.unload(dbus_interface = 'org.PB.PBase')
except dbus.exceptions.DBusException:
log.exception('dbus-exception')
return False
def show_pbase(self):
'''This function shows PBase. In case kwin is available it will switch to the
next free desktop. If kwin is not available it will close the running prog.
'''
if self.kwin_is_available():
#check if no prog is on the current desktop: this means pbase is already open
for prog in self.progs:
if self.progs[prog].desktop==self.get_current_desktop():
self.switch_to_desktop(self.get_free_desktop())
#activate tuio
tuio.add_port(3335)
return
else:
#if kwin is not available, quit all runing progs
for prog in self.progs:
if self.progs[prog].open:
self.stop_prog(prog)
return
def get_open_progs(self):
'''This function returns all the open progs in a list.
:return progs: list with prog_id's
'''
prog_list=[]
for prog in self.progs:
if self.progs[prog].open: prog_list.append(prog)
if len(prog_list)==0: prog_list.append(False)
return prog_list
def get_free_desktop(self):
'''This function searches for the next free desktop and returns the number
of it.
:return desktop: int: the number of the free desktop
'''
#create a list with the number of desktops in it.
#example: desktops=[1,2,3,4] (four desktops)
desktops=[]
for desktop in range(self.get_number_of_desktops()):
desktops.append(desktop+1)
#fill the number in the list with None when a prog is there
#example: desktops[None,2,3,4] (On desktop 1 is a prog running)
for prog in self.progs:
if self.progs[prog].desktop!=None: #ignore progs which aren't active/ prog.desktop=None
desktops[desktops.index(self.progs[prog].desktop)]=None
for i in desktops:
if i != None: return i
def switch_to_desktop(self, desktop):
'''This function switches to the given desktop.
:param desktop: nuber of the desktop
'''
if self.kwin_is_available():
os.system("wmiface setCurrentDesktop "+str(desktop))
def get_current_desktop(self):
'''This function returns the number of the current desktop.
:return desktop: int, number of desktop
'''
if self.kwin_is_available():
proc = subprocess.Popen("wmiface currentDesktop", shell=True, stdout=subprocess.PIPE)
proc.wait()
for line in proc.stdout: currentDesktop=int(line.rstrip())
return int(currentDesktop)
def get_number_of_desktops(self):
'''This function returns the number of the current desktop.
:return desktop: int, number of desktop
'''
if self.kwin_is_available():
proc = subprocess.Popen("wmiface numberOfDesktops", shell=True, stdout=subprocess.PIPE)
proc.wait()
for line in proc.stdout: desktops=int(line.rstrip())
return int(desktops)
def shutdown(self, reaction='stop'):
'''This function calls shutdown on start over DBUS
This function also closes all running progs first.
:param stop: reboot=device reboot, stop=software stop,
shutdown=device shutdown
'''
self.stop_all_progs()
bus=dbus.SessionBus()
server = bus.get_object('org.PB.start', '/start')
try:
server.shutdown(reaction, dbus_interface = 'org.PB.start')
except:
print "PBController/start.py crashed or the dbus server of it. - last hope: killall python"
os.system("killall python")
return False
def kwin_is_available(self):
'''This function checks if kwin and wmiface is available or not by
calling the try calling its D-Bus
:return bool: returns True or False
'''
if self.cmd_exists("wmiface numberOfDesktops"):
try:
self.bus.get_object('org.kde.kwin', '/KWin')
return True
except:
return False
else:
return False
def cmd_exists(self, cmd):
'''checks if this command exists
'''
if len(os.popen(cmd).readlines())==0:
return False
else:
return True
def get_path_from_db(self, prog_id):
'''This function gets the path to the prog.
:param prog_id: id of the prog
'''
DB_connection = sqlite3.connect("../PBase/data/database.sqlite")
DB_cursor = DB_connection.cursor()
DB_cursor.execute("SELECT path FROM progs WHERE id = '%i'" %prog_id)
DB_connection.commit()
for row in DB_cursor: path=row[0]
DB_connection.close()
return path
def try_reach_dbus_PBase(self):
try:
server = self.bus.get_object('org.PB.PBase', '/PBase')
except dbus.exceptions.DBusException:
#log.exception('Can not reach dbus:')
return False
return server
class Prog():
'''The Prog class will be instanced for every prog that will be running.
:param controller: the controller object
:param prog_id: the ID of the prog
:param path: the path to the prog
:pram TUIOport: the port on which which the prog should listen to TUIO.
Once the prog instance is created, you should not change the port unless
you are sure that you know what you are doing. That is because the
TUIOController will multiplex to this port the next time this prog
gets opened too.
:prarm ignore_region: this is the region on which the close button
could be placed. This means on this region do not recognize any touches.
'''
def __init__(self, controller, prog_id, path, TUIOport, ignore_region):
self.controller=controller
self.prog_id=prog_id
self.path=path
self.TUIOport=TUIOport
self.ignore_region=ignore_region #(0.395833, 0.9537, 0.604166, 1.0)
self.open=False
self.desktop=None
self.PID=None #the PID will defined at runtime via the queue.
self.threading=True
def start(self, *kwargs):
'''Starting the prog
1. Create a queue for passing arguments between processes
2. Create and start a process out of the __process() function
3. Create and start a thread which is checking the activity of the process
4. Wait until we received the PID of the subprocess
:return: True if the prog is launched successfully.
'''
#check if the process is already running
if hasattr(self, "process"):
if self.process.is_alive():
return False
#check if path exists
if not os.path.isdir("../progs/"+self.path+"/"):
log.error('Path not found: '+"../progs/"+self.path+"/")
return False
#make queue for passing arguments between process & this class
q = multiprocessing.Queue()
#create and start the process
self.process=multiprocessing.Process(target=self.__process, args=(q,))
self.process.start()
#create the thread
t = Thread(target=self.__check_activity)
t.start()
#receive PID from queue
self.PID=q.get()
log.info("Prog opened with PID of: "+str(self.PID))
self.open=True
return True
def stop(self):
'''Stopping the prog / quit
This is done simply by calling "kill <PID>"
'''
os.system("kill "+str(self.PID))
def __process(self, q):
'''The function which will get transformed to the process.
This is a internal function. Don't EVER call this function on your own!
call it only via start()
'''
command=("python","main.py","-k","-p",
"test:tuio,0.0.0.0:"+str(self.TUIOport),"-c",
"postproc:ignore:["+str(self.ignore_region)+"]")
pr=subprocess.Popen(command, cwd="../progs/"+self.path+"/", stdout=subprocess.PIPE,
preexec_fn=os.setsid)
q.put(pr.pid) #put the PID thrugh the queue
pr.wait() #wait until the subprocess is finished
def __check_activity(self):
'''This function get transformed into the thread. It checks
every 0.1second if the process is still alive. When the prog get
closed or had a crash it isn't alive anymore. Is this the case,
then we can inform the controller object about that. Don't EVER
call this function on your own! call it only via start()
This thread will be stopped as soon: self.threading=False
'''
while self.threading:
time.sleep(0.2)
if not self.process.is_alive():
self.open=False
gtk.timeout_add(1, self.controller.prog_closed, self.prog_id)
break
class TUIOMultiplexer(object):
'''This class is does multiplex the TUIO to different ports.
There is a self.active_ports dict, in which all ports are stored that are active.
Also there is a self.ports list, in which all ports are stored where used since
runtime.
'''
active_ports={} #active ports
ports=[] #all ports which are used since runtime
stop_loop=False
def __init__(self, *kwagrs):
'''The init function will start the loop
Also it will add the first port to the loop (PBSwitch->3334)
'''
#always multiplex for PBSwitch 3334!
self.add_port(3334)
#start tuio for pbase
self.add_port(3335)
t = Thread(target=self.__loop)
t.start()
def __loop(self):
'''The main loop for multiplexing.
This function is launched as a thread from the init form this class.
Do not call this function on you own!
'''
hostin = "127.0.0.1"
portin = 3333
cont = True
while cont and not self.stop_loop:
try:
sin = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sin.bind((hostin,portin))
cont = False
except:
log.error("TUIOMultiplexer: Port error. retry...in 2sec")
time.sleep(2)
cont = True
data = 1
while data and not self.stop_loop:
data = sin.recv(1024*1024)
copy_active_ports=self.active_ports
try:
for i in copy_active_ports:
try:
self.active_ports[i].send(data)
except:
pass
except:
pass
def create_new_port(self):
'''Create a new port for a new prog.
This is needed if a prog is lunched the first time since runtime.
The function will return you the new port for.
:return port: Returns the port for this prog
'''
#get highest used port
high=max(port for port in self.ports)
log.info("new highest port: "+str(high+1))
#add new port that is +1 higher
self.ports.append(high+1)
return high+1
def add_port(self, port):
'''Add a port to the multiplexer
:param port: port to add
'''
#check if port is already in list
if not port in self.ports:
self.ports.append(port)
#close all ports, without the one that should added
ports_to_remove=[] #create list to avoid "dict changed size during iteration"
for porti in self.active_ports:
if porti!=3334: #if it is not the switch port
ports_to_remove.append(porti)
for porti in ports_to_remove: self.remove_port(porti)
#add port
if port not in self.active_ports:
log.info("START TUIO: "+str(port))
self.active_ports[port] = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.active_ports[port].connect(("127.0.0.1",port))
def remove_port(self, port):
'''Remove a port from the multiplexer
:param port: port to remove
'''
if port in self.active_ports:
log.info("STOP TUIO: "+str(port))
self.active_ports.pop(port)
tuio=TUIOMultiplexer()
def test():
log.info("TEST")
return [5,6]
class DBusServer(dbus.service.Object):
'''DBus server from PBController.
Reachable under session bus: org.PB.PBController (/PBController)
'''
def __init__(self, controller):
'''Initialize the session bus under:
org.PB.PBController
:param controller: controller object
'''
self.controller=controller
bus_name = dbus.service.BusName('org.PB.PBController', bus=dbus.SessionBus())
dbus.service.Object.__init__(self, bus_name, '/PBController')
@dbus.service.method('org.PB.PBController')
def open_prog(self, prog_id):
'''This function opens or switch to a prog.
:param prog_id: id of the prog which should be opened
'''
gtk.timeout_add(1, self.controller.open_prog, prog_id)
##############
#Komisches Problem mit Prints/ logging
#
#Die vermutung liegt nahe, dass sofern der Fehler im timeout_add passiert, dass es dan zum crash kommt
#denn auch die log die ich schreibe werden speziell dargestellt wenn durch timeout_add aufgerufen.
#
#UPDATE1:
#Das Problem liegt nicht (ausschliesslich) an timeout_add. Wird nicht über start.py gestarted sondern nur
#dieses file, dann kann über dbus debugger das problem nicht widerhohlt werden, jedoch wenn über start.py
#kann es auch über dbus debugger widerhohlt werden.
#
#UPDATE2:
#Das Problem liegt nicht an timeout_add, da auch ein direktes loggen probleme bereitet direkt inder Funktion
#Das Problem wird irgdnwo beim DBUS zu suchen sein, weniger im GTKloop
#
#UPDATE3:
#Durch Ausführen über start.py aber ohne die anderen softwares, funtkioniert es auch. Das Problem muss von den
#anderen kommen
#
#UPDATE4:
#wenn gleichzeitig switch.py gestarted wird herst das problem
#
#UPDATE5:
#Der call zu get_open_progs() ist der Hinderer
#
#UPDATE6:
#Problem ist wegg: KEINE ahnung warum!
#
##############
return
@dbus.service.method('org.PB.PBController')
def close_prog(self, prog_id):
'''This function closes a prog. After closing the PBase
will be showed.
:param prog_id: id of the prog which should be closed
'''
gtk.timeout_add(1,self.controller.stop_prog,prog_id)
return
@dbus.service.method('org.PB.PBController')
def get_open_progs(self):
'''This function returns a list with prog_id's from progs
which are open.
:return prog_list: list with prog id's
'''
return self.controller.get_open_progs()
@dbus.service.method('org.PB.PBController')
def load_pbase(self):
'''This function will load the PBase.
'''
gtk.timeout_add(1,self.controller.load_pbase)
return
@dbus.service.method('org.PB.PBController')
def unload_pbase(self):
'''This function will unload the PBase.
'''
gtk.timeout_add(1, self.controller.unload_pbase)
return
@dbus.service.method('org.PB.PBController')
def show_pbase(self):
'''This function will unload the PBase.
'''
gtk.timeout_add(1, self.controller.show_pbase)
return
@dbus.service.method('org.PB.PBController')
def shutdown(self, reaction='stop'):
'''This function will shutdown the device or
restart in case the argument reboot is True
:param reboot: Indicates if the device should reboot or shutdown
'''
gtk.timeout_add(1, self.controller.shutdown, reaction)
return
DBusGMainLoop(set_as_default=True)
controller=Controller()
myservice = DBusServer(controller)
try:
gtk.main()
except KeyboardInterrupt:
#stop everything safely
tuio.stop_loop=True
controller.stop_all_progs()
raise
except Exception, e:
print e