-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpp_displaymanager.py
1332 lines (1062 loc) · 53.5 KB
/
pp_displaymanager.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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env python3
import os
import sys
import subprocess
#from subprocess import run,call
from tkinter import Tk, Canvas,Toplevel,NW,Scrollbar,RIGHT,Y,LEFT,BOTH,TOP
import copy
import configparser
class DisplayManager(object):
# DSI1 0 - MainLCD - official DSI touchscreen
# 1 - - Auxilliary LCD ?whats this
# HDMI0 2 - HDMI0 - HDMI port 0
# A/V 3 - Composite - TV
# 4 - - Force LCD
# 5 - - Force TV
# 6 - - Force non-default display
# 7 - HDMI1 - HDMI Port 1
# 8 -
debug = False
display_map = {'DSI0':0,'HDMI0':2,'HDMI':2,'A/V':3,'HDMI1':7 } # lookup display Id by display name e.g. HDMI1>7
display_reverse_map = {0:'DSI0',2:'HDMI0',3:'A/V',7:'HDMI1' } # lookup display name by Id e.g. 2>HDMI0
vlc_display_name_map = {'DSI0':'DSI-1','HDMI':'HDMI-1','HDMI0':'HDMI-1','A/V':'A/V','HDMI1':'HDMI-2' }
# Class Variables
# obtained from tvservice for model 3 or randr for model 4
numdisplays=0
displays=[] # list of dispay Id's e.g.[2,7]
# tv service parameters by Display Id
# width and height from tvservice does not take into account rotation
# tvservice needs to be main source of info for model 3. For 4 it is just for info as its use is deprecated
tv_num_displays = 0
tv_displays = []
tv_display_width = {}
tv_display_height = {}
# randr paramters by randr name (HDMI-1 etc)
randr_num_displays = 0 # should be the same as tvservice
randr_displays = []
randr_rotation = {}
randr_width = {}
rand_height = {}
randr_x = {}
randr_y = {}
# dimensions of the real displays obtained from tvservice and randr by display_id (2,7
real_display_width={}
real_display_height={}
real_display_x = {}
real_display_y = {}
real_display_rotation={}
overlap = '' #are the displays overlapping also above/sid by side
# dimensions modified by fake in display.cfg, used to create windows
fake_display_width={}
fake_display_height={}
# dimensions of the window in non-fullscreen mode (as modified by non-full window width/height)
window_width=dict()
window_height=dict()
# canvas parameters by Display Id
canvas_obj=dict() # Tkinter widget
canvas_width=dict()
canvas_height=dict()
# touch matrix by display id
rotation_x_offset={}
rotation_y_offset={}
touch_matrix={}
#called by all classes using DisplayManager
def __init__(self):
return
# ***********************************************
# Methods for rest of Pi Presents
# ************************************************
def model_of_pi(self):
return DisplayManager.pi_model
def id_of_display(self,display_name):
if display_name not in DisplayManager.display_map:
return 'error','Display Name not known: '+ display_name,-1
display_id = DisplayManager.display_map[display_name]
if display_id not in DisplayManager.displays:
return 'error','Display not connected: '+ display_name,-1
return 'normal','',display_id
def id_of_canvas(self,display_name):
if display_name not in DisplayManager.display_map:
return 'error','Display Name not known '+ display_name,-1,-1
display_id = DisplayManager.display_map[display_name]
if display_id not in DisplayManager.canvas_obj:
return 'error','Display not connected (no canvas): '+ display_name,-1,-1
return 'normal','',display_id,DisplayManager.canvas_obj[display_id]
def name_of_display(self,display_id):
return DisplayManager.display_reverse_map[display_id]
def has_canvas(self,display_id):
if display_id not in DisplayManager.canvas_obj:
return False
else:
return True
def canvas_widget(self,display_id):
return DisplayManager.canvas_obj[display_id]
def canvas_dimensions(self,display_id):
return DisplayManager.canvas_width[display_id],DisplayManager.canvas_height[display_id]
def display_dimensions(self,display_id):
return DisplayManager.fake_display_width[display_id],DisplayManager.fake_display_height[display_id]
def real_display_dimensions(self,display_id):
return DisplayManager.real_display_width[display_id],DisplayManager.real_display_height[display_id]
def real_display_position(self,display_id):
return DisplayManager.real_display_x[display_id],DisplayManager.real_display_y[display_id]
def real_display_orientation(self,display_id):
return DisplayManager.real_display_rotation[display_id]
def orientation_offset(self,display_id):
return DisplayManager.rotation_x_offset[display_id],DisplayManager.rotation_y_offset[display_id]
def touch_matrix_for(self,display_id):
matrix=DisplayManager.touch_matrix[display_id]
# convert to strings array
cstr=['','','','','','','','','']
i=0
while i <9:
cstr[i]= '{:f}'.format(matrix[i])
i+=1
chunks=self.chunks(cstr,3)
ms='Matrix:\n '
for chunk in chunks:
ms=ms+ ' '.join(chunk)
ms +='\n '
return matrix,ms
# ***********************************************
# Initialize displays at start
# ************************************************
# called by pipresents.py only when PP starts
def init(self,options,close_callback,pp_dir,debug):
DisplayManager.debug=debug
# read display.cfg
self.read_config(pp_dir)
# get model of Pi. Only interested if it is 4 or less
self.model=self.pi_model()
DisplayManager.pi_model=self.model
self.print_info()
if self.model == 4:
# find connected displays from randr and get their parameters
status,message=self.find_randr_displays()
if status=='error':
return status,message,None
# find connected displays from tvservice and get their parameters
status,message=self.find_tv_displays()
if status=='error':
return status,message,None
# process Randr displays to display_id
status,message=self.process_displays_model4()
if status=='error':
return status,message,None
else:
# find connected displays from tvservice and get their parameters
status,message=self.find_tv_displays()
if status=='error':
return status,message,None
# process tvservice displays to display_id and add missing paramters from displau options
status,message=self.process_displays_model123()
if status=='error':
return status,message,None
# compute display_width, display_height accounting for --screensize option
status,message=self.do_fake_display()
if status=='error':
return status,message,None
# Have now got all the required information
# setup backlight for touchscreen if connected
status,message=self.init_backlight()
if status=='error':
return status,message,None
# setup the touch input for touchscreens
status,message=self.init_touch()
if status=='error':
return status,message,None
# set up Tkinter windows
status,message,root=self.init_tk(options,close_callback)
if status=='error':
return status,message,None
return status,message,root
def terminate(self):
self.terminate_backlight()
# ***********************************************
# Get information about displays
# ************************************************
def find_tv_displays(self):
DisplayManager.tv_num_displays=0
DisplayManager.tv_displays=[]
DisplayManager.tv_display_width=dict()
DisplayManager.tv_display_height=dict()
# get number of displays and ther Display ID's from tvservice
l_reply=subprocess.run(['tvservice','-l'],stdout=subprocess.PIPE)
l_reply_list=l_reply.stdout.decode('utf-8').split('\n')
DisplayManager.tv_num_displays=int(l_reply_list[0].split(' ')[0])
for line in range(1,DisplayManager.tv_num_displays+1):
disp_list=l_reply_list[line].split(' ')
disp_id=disp_list[2][:1]
if int(disp_id) not in DisplayManager.display_reverse_map:
return 'error','Display Id not known: '+ l_reply_list[line]
DisplayManager.tv_displays.append(int(disp_id))
# get dimensions of this display from tvservice
command=['tvservice','-s','-v'+ disp_id]
s_reply=subprocess.run(command,stdout=subprocess.PIPE)
s_reply_list=s_reply.stdout.decode('utf-8').split(',')
s_tt_list=s_reply_list[1].strip().split(' ')
s_dim_list=s_tt_list[0].split('x')
# get real display width and height
DisplayManager.tv_display_width[int(disp_id)]=int(s_dim_list[0])
DisplayManager.tv_display_height[int(disp_id)]=int(s_dim_list[1])
self.print_tv()
return 'normal',''
def find_randr_displays(self):
# clear dicts to be used
DisplayManager.randr_num_displays = 0 # should be the same as tvservice
DisplayManager.randr_displays = [] # If there are 2 HDMI then HDMI0 is the first
DisplayManager.randr_rotation = dict()
DisplayManager.randr_width = dict()
DisplayManager.randr_height = dict()
DisplayManager.randr_x = dict()
DisplayManager.randr_y = dict()
#execute xrandr command
output = subprocess.check_output(["xrandr"]).decode("utf-8").splitlines()
for l in output:
if ' connected ' in l:
fields = l.split()
name= fields[0]
DisplayManager.randr_displays.append(name)
DisplayManager.randr_num_displays +=1
if 'primary' in l:
whxy_field=3
else:
whxy_field=2
whxy=fields[whxy_field]
rotation = fields[whxy_field+1]
if rotation[0]=='(':
rotation = rotation[1:]
wh=whxy.split('+')[0]
w=wh.split('x')[0]
h=wh.split('x')[1]
xy=whxy.split('+')
x=xy[1]
y=xy[2]
DisplayManager.randr_width[name]=int(w)
DisplayManager.randr_height[name]=int(h)
DisplayManager.randr_x[name]=int(x)
DisplayManager.randr_y[name]=int(y)
DisplayManager.randr_rotation[name]=rotation
self.print_randr()
return 'normal',''
def process_displays_model123(self):
"""
For model 1,2,3 randr gives no more information than tvservice
so just copy stuff from tvservice
However tvservice dos not supply rotation so this has to be got from display.cfg
"""
# init class variables
DisplayManager.displays=[]
DisplayManager.num_displays=0
DisplayManager.real_display_width=dict()
DisplayManager.real_display_height=dict()
DisplayManager.real_display_x=dict()
DisplayManager.real_display_y=dict()
DisplayManager.real_display_rotation = dict()
#obtain real from tv and display.cfg
DisplayManager.displays=DisplayManager.tv_displays
DisplayManager.num_displays= DisplayManager.tv_num_displays
for did in DisplayManager.displays:
DisplayManager.real_display_x[did]=0 #only one display so x and y are 0
DisplayManager.real_display_y[did]=0
status,message,rotation= self.get_rotation(did)
if status =='error':
return status,message
if status == 'null':
rotation = 'normal'
DisplayManager.real_display_rotation[did]=rotation
width=DisplayManager.tv_display_width[did]
height=DisplayManager.tv_display_height[did]
if rotation in ('right','left'):
width,height=height,width
DisplayManager.real_display_width[did] = width
DisplayManager.real_display_height[did] = height
self.print_real()
return 'normal',''
def process_displays_model4(self):
"""
we need to have all parameters referenced to the display_id as display_id is used by omxplayer etc.
however for model 4:
x position and y position are provided only by xrandr
display width and height are swappped for rotated displays by xrandr
display rotation is provided only by xrandr
and
xrandr does not reference display by display_id but by DSI-1 HDMI-1 HDMI-2
The translation is not constant:
DSI-1 always equates to display_id 0
but HDMI-1 and HDMI-2 translation depends on the number of HDMI monitors
DSI-1 only: > [0]
* Composite-1 only > [3]
HDMI-1 only: > [2] (plugged into HDMI0 port)
HDMI-2 > [] does not happen because 7 not in randr list - report error
* DSI-1 + Composite-1 > [0 + 3] not tested
DSI-1 + HDMI-1: [0 + 2]
DSI-1 + HDMI-2: [0] only because HDMI-2 cannot be the only HDMI monitor
HDMI-1 + HDMI-2: [2,7] assume HDMI0 port is always first in the list
"""
DisplayManager.displays=[]
DisplayManager.num_displays=0
DisplayManager.real_display_width=dict()
DisplayManager.real_display_height=dict()
DisplayManager.real_display_x=dict()
DisplayManager.real_display_y=dict()
DisplayManager.real_display_rotation = dict()
# translate rand r display names into display_id's
if DisplayManager.randr_num_displays == 1:
if DisplayManager.randr_displays[0] == 'HDMI-2':
return 'error','HDMI-2 cannot be the only display'
DisplayManager.num_displays = 1
if DisplayManager.randr_displays[0] == 'DSI-1':
DisplayManager.displays.append(DisplayManager.display_map['DSI0'])
elif DisplayManager.randr_displays[0] == 'HDMI-1':
DisplayManager.displays.append(DisplayManager.display_map['HDMI0'])
elif DisplayManager.randr_displays[0] == 'Composite-1':
DisplayManager.displays.append(DisplayManager.display_map['A/V'])
else:
return 'error','xrand r display not recognised '+ DisplayManager.randr_displays[0]
else:
if 'DSI-1' in DisplayManager.randr_displays and 'HDMI-1' in DisplayManager.randr_displays:
DisplayManager.displays.append(DisplayManager.display_map['DSI0'])
DisplayManager.displays.append(DisplayManager.display_map['HDMI0'])
DisplayManager.num_displays = 2
elif 'HDMI-1' in DisplayManager.randr_displays and 'HDMI-2' in DisplayManager.randr_displays:
DisplayManager.displays.append(DisplayManager.display_map['HDMI0'])
DisplayManager.displays.append(DisplayManager.display_map['HDMI1'])
DisplayManager.num_displays = 2
elif 'DSI-1' in DisplayManager.randr_displays and 'HDMI-2' in DisplayManager.randr_displays:
DisplayManager.displays.append(DisplayManager.display_map['DSI0'])
DisplayManager.num_displays = 1
# copy display dimensions from randr arrays to real arrays
if DisplayManager.num_displays==1:
dname1=DisplayManager.randr_displays[0]
did1=DisplayManager.displays[0]
if did1 == 7:
return 'error','single HDMI display must be in port HDMI0'
DisplayManager.real_display_width[did1]= DisplayManager.randr_width[dname1]
DisplayManager.real_display_height[did1] = DisplayManager.randr_height[dname1]
DisplayManager.real_display_x[did1]= DisplayManager.randr_x[dname1]
DisplayManager.real_display_y[did1] = DisplayManager.randr_y[dname1]
DisplayManager.real_display_rotation[did1] = DisplayManager.randr_rotation[dname1]
if DisplayManager.num_displays==2:
dname1=DisplayManager.randr_displays[0]
did1=DisplayManager.displays[0]
dname2=DisplayManager.randr_displays[1]
did2=DisplayManager.displays[1]
if did2 == 7 and did1 !=2:
return 'error','single HDMI display must be in port HDMI0'
DisplayManager.real_display_width[did1]= DisplayManager.randr_width[dname1]
DisplayManager.real_display_height[did1] = DisplayManager.randr_height[dname1]
DisplayManager.real_display_x[did1]= DisplayManager.randr_x[dname1]
DisplayManager.real_display_y[did1] = DisplayManager.randr_y[dname1]
DisplayManager.real_display_rotation[did1] = DisplayManager.randr_rotation[dname1]
DisplayManager.real_display_width[did2]= DisplayManager.randr_width[dname2]
DisplayManager.real_display_height[did2] = DisplayManager.randr_height[dname2]
DisplayManager.real_display_x[did2]= DisplayManager.randr_x[dname2]
DisplayManager.real_display_y[did2] = DisplayManager.randr_y[dname2]
DisplayManager.real_display_rotation[did2] = DisplayManager.randr_rotation[dname2]
if DisplayManager.num_displays == 2:
id0=DisplayManager.displays[0]
id1=DisplayManager.displays[1]
if DisplayManager.real_display_x[id0] == DisplayManager.real_display_x[id1]\
and DisplayManager.real_display_y[id0] == DisplayManager.real_display_y[id1]:
DisplayManager.overlap='on-top'
elif DisplayManager.real_display_x[id0] == DisplayManager.real_display_x[id1]:
DisplayManager.overlap='above'
else:
DisplayManager.overlap='side-by-side'
self.print_real()
return 'normal',''
def do_fake_display(self):
DisplayManager.fake_display_width=dict()
DisplayManager.fake_display_height=dict()
for did in DisplayManager.displays:
reason,message,fake_width,fake_height=self.get_fake_dimensions(DisplayManager.display_reverse_map[did])
if reason =='error':
return 'error',message
if reason == 'null':
DisplayManager.fake_display_width[did]=DisplayManager.real_display_width[did]
DisplayManager.fake_display_height[did]=DisplayManager.real_display_height[did]
else:
DisplayManager.fake_display_width[did] = fake_width
DisplayManager.fake_display_height[did] = fake_height
self.print_fake()
return 'normal',''
# ***********************************************
# Set up Tkinter windows and canvases.
# ************************************************
def init_tk(self,options,close_callback):
# clear class variables
DisplayManager.window_width=dict()
DisplayManager.window_height=dict()
DisplayManager.canvas_obj=dict()
DisplayManager.canvas_width=dict()
DisplayManager.canvas_height=dict()
# get the display to be called Tk
if len(DisplayManager.displays)==0:
return 'error','No displays connected',None
# primary is the display_id that is to be Tk root
# primary display needs to be 0 if DSI0 is used otherwise Tkinter crashes
# set to 2 if HDMI0 and HDMI1 as HDMI0 is the main dislay
# primary_id is assigned to Tk() main window
# develop_id is windowed if not fullscreen
if self.model<4:
# model < 4
if DisplayManager.num_displays>1:
# DSI0 and HDMI0 connected. HDMI0 is in tvservice -l but useless for other than omxplayer output
primary_id=0
self.develop_id=0
else:
#one display which could be DSI0 or HDMI0
primary_id=DisplayManager.displays[0]
self.develop_id=primary_id
else:
# Model 4
if len(DisplayManager.displays)==1:
# single display either DSI0 or HDMI0
primary_id=DisplayManager.displays[0]
self.develop_id = primary_id
elif 0 in DisplayManager.displays and 2 in DisplayManager.displays:
# DSI0 and HDMI0. Make HDMI0 the windowed display as best for developing.
primary_id=0 # tk falls over if 0 is not the primary display.
self.develop_id=2 # 2 is HDMI so best for developing
elif 2 in DisplayManager.displays and 7 in DisplayManager.displays:
# HDMI0 and HDMI1
primary_id=2
self.develop_id=2
# setup Tk windows/canvases for all connected displays
for this_id in DisplayManager.displays:
# HDMI0 is not useable as a Tk display if there are 2 displays on model<4
if self.model <4 and DisplayManager.num_displays>1 and this_id !=0:
continue
# print (this_id, self.develop_id)
if this_id == primary_id:
tk_window=Tk()
root=tk_window
else:
tk_window=Toplevel()
tk_window.title('Pi Presents - ' + DisplayManager.display_reverse_map[this_id])
tk_window.iconname('Pi Presents')
tk_window.config(bg='black')
# set window dimensions and decorations
# make develop_id screen windowed
if options['fullscreen'] is False and this_id == self.develop_id:
status,message,x,y,w_scale,h_scale=self.get_develop_window(DisplayManager.display_reverse_map[this_id])
if status != 'normal':
return 'error',message,None
window_width=DisplayManager.real_display_width[this_id]*w_scale
window_height= DisplayManager.real_display_height[this_id]*h_scale
window_x=DisplayManager.real_display_x[self.develop_id] + x
window_y= DisplayManager.real_display_y[self.develop_id] + y
# print ('Window Position not FS', this_id,window_x,window_y)
tk_window.geometry("%dx%d%+d%+d" % (window_width,window_height,window_x,window_y))
else:
# fullscreen for all displays that are not develop_id
window_width=DisplayManager.fake_display_width[this_id]
# krt changed
window_height=DisplayManager.fake_display_height[this_id]
window_x=DisplayManager.real_display_x[this_id]
window_y=DisplayManager.real_display_y[this_id]
tk_window.attributes('-fullscreen', True)
if options['nounclutter'] is False:
# print ('set unclutter')
os.system('unclutter > /dev/null 2>&1 &')
# print ('Window Position FS', this_id, window_x,window_y,window_width,window_height)
tk_window.geometry("%dx%d%+d%+d" % (window_width,window_height,window_x,window_y))
tk_window.attributes('-zoomed','1')
DisplayManager.window_width[this_id]=window_width
DisplayManager.window_height[this_id]=window_height
# define response to main window closing.
tk_window.protocol ("WM_DELETE_WINDOW", close_callback)
# setup a canvas onto which will be drawn the images or text
# canvas covers the whole screen whatever the size of the window
canvas_height=DisplayManager.fake_display_height[this_id]
canvas_width=DisplayManager.fake_display_width[this_id]
if options['fullscreen'] is False:
##scrollbar = Scrollbar(tk_window)
#scrollbar.pack(side=RIGHT, fill=Y)
tk_canvas = Canvas(tk_window, bg='black')
#tk_canvas = Canvas(tk_window, bg='blue',yscrollcommand=scrollbar.set)
tk_canvas.config(height=canvas_height,
width=canvas_width,
highlightcolor='yellow',
highlightthickness=1)
#tk_canvas.pack(anchor=NW,fill=Y)
#scrollbar.config(command=tk_canvas.yview)
tk_canvas.place(x=0,y=0)
else:
tk_canvas = Canvas(tk_window, bg='black')
tk_canvas.config(height=canvas_height,
width=canvas_width,
highlightthickness=0,
highlightcolor='yellow')
tk_canvas.place(x=0,y=0)
# tk_canvas.config(bg='black')
DisplayManager.canvas_obj[this_id]=tk_canvas
DisplayManager.canvas_width[this_id]=canvas_width
DisplayManager.canvas_height[this_id]=canvas_height
tk_window.focus_set()
tk_canvas.focus_set()
self.print_tk()
return 'normal','',root
def print_info(self):
if DisplayManager.debug is True:
print ('\nMaps:',DisplayManager.display_map,DisplayManager.display_reverse_map)
print ('Pi Model:',self.model)
def print_tv(self):
if DisplayManager.debug is True:
print ('\nNumber of Displays - tvservice:',DisplayManager.tv_num_displays)
print ('Displays Connected - tvservice:',DisplayManager.tv_displays)
print ('Display Dimensions - tvservice:',DisplayManager.tv_display_width,DisplayManager.tv_display_height)
def print_randr(self):
if DisplayManager.debug is True:
print ('\nNumber of Displays - randr:',DisplayManager.randr_num_displays)
print ('Displays Connected- randr:',DisplayManager.randr_displays)
print ('Display Dimensions - randr:',DisplayManager.randr_width,DisplayManager.randr_height)
print ('Display Position - randr:',DisplayManager.randr_x,DisplayManager.randr_y)
print ('Display Rotation - randr:',DisplayManager.randr_rotation)
def print_real(self):
if DisplayManager.debug is True:
print ('\nNumber of Displays - real:',DisplayManager.num_displays)
print ('Displays Connected- real:',DisplayManager.displays)
print ('Display Dimensions - real:',DisplayManager.real_display_width,DisplayManager.real_display_height)
print ('Display Position - real:',DisplayManager.real_display_x,DisplayManager.real_display_y)
print ('Display Rotation - real:',DisplayManager.real_display_rotation)
def print_fake(self):
if DisplayManager.debug is True:
print ('\nDisplay Dimensions - fake:',DisplayManager.fake_display_width,DisplayManager.fake_display_height)
def print_tk(self):
if DisplayManager.debug is True:
print ('\nDevelopment Display:',self.develop_id)
print ('Window Dimensions - non-full:',DisplayManager.window_width,DisplayManager.window_height)
print ('Canvas Widget:',DisplayManager.canvas_obj)
print ('Canvas Dimensions:',DisplayManager.canvas_width,DisplayManager.canvas_height,'\n\n')
# ***********************************************
# Touchscreen Calibration
# ************************************************
def init_touch(self):
# enable display debug output to terminal
self.debug=DisplayManager.debug
total_width=0
total_height=0
for display_id in DisplayManager.displays:
# for model 3 miss out id = 2 or 3 if touchsreen is connected as tvservice includes HDMI0 even if useless
if self.model <4 and DisplayManager.num_displays>1 and display_id !=0:
continue
if DisplayManager.overlap == 'on-top':
return 'error','The two monitors must not overlap in Screen Config Utility'
elif DisplayManager.overlap == 'above':
total_height += DisplayManager.real_display_height[display_id]
total_width=max(total_width,DisplayManager.real_display_width[display_id])
else:
total_width += DisplayManager.real_display_width[display_id]
total_height=max(total_height,DisplayManager.real_display_height[display_id])
for display_id in DisplayManager.displays:
rotation=DisplayManager.real_display_rotation[display_id]
width=DisplayManager.real_display_width[display_id]
height=DisplayManager.real_display_height[display_id]
x_position=DisplayManager.real_display_x[display_id]
y_position=DisplayManager.real_display_y[display_id]
# print (display_id)
DisplayManager.touch_matrix[display_id],coords_str \
=self.calc_coords(display_id,rotation,width,height,x_position,y_position,total_width,total_height,'',self.debug)
status,message,driver_name=self.get_driver_name(display_id)
if status =='error':
return status,message
if status =='null':
if self.debug:
print ('Touch driver not defined for '+ str(display_id))
if status == 'normal':
status,message=self.send_xinput(display_id,coords_str,driver_name)
if status == 'error':
return 'error','Touch driver is '+ message
return 'normal',''
def calc_coords(self,display_id,rotation,width,height,x_position,y_position,total_width,total_height,title,debug):
# if display is rotated the origin moves, offset is how the ORIGIN moves
# Note: left and right reversed from how you might draw them
rotation_x_offset ={'normal':0,
'left':width, # !! display rotated clockwise
'inverted':width,
'right':0, # !! display rotated anti-clock
}
rotation_y_offset ={'normal':0,
'left':0,
'inverted':height,
'right':height,
}
# calling function has swapped width and height for rotated monitos
sx = width/total_width
sy = height/total_height
tx=(rotation_x_offset[rotation]+ x_position)/total_width
ty=(rotation_y_offset[rotation]+ y_position)/total_height
DisplayManager.rotation_x_offset[display_id]=rotation_x_offset[rotation]
DisplayManager.rotation_y_offset[display_id]=rotation_y_offset[rotation]
if debug is True:
print ('\n---------'+ title + '-------------')
print ('width rot-ofxset x-position total-width:',width,rotation_x_offset[rotation],x_position,total_width)
print ('height rot-ofxset y-position total-height:',height,rotation_y_offset[rotation],y_position,total_height)
c,cstr=self.matrix(DisplayManager.display_reverse_map[display_id],rotation,sx,sy,tx,ty,'',debug)
return c,cstr
def chunks(self,lst, n):
"""Yield successive n-sized chunks from lst."""
for i in range(0, len(lst), n):
yield lst[i:i + n]
def matrix(self,position,rotation,sx,sy,tx,ty,text,debug):
# base has c[2] and c[5] set to include the offset of the origin due to rotation, however these
# are not used by this code as the coords are given by tx and ty
self.base={
'normal': [1, 0, 0,
0, 1, 0,
0 ,0 ,1],
'left': [0, -1, 1, #90 degrees
1, 0, 0,
0 ,0 ,1],
'inverted':[-1, 0, 1,
0, -1, 1,
0 ,0 ,1],
'right': [0, 1, 0, #270 degrees
-1, 0, 1,
0 ,0 ,1]}
rb = self.base[rotation]
# deepcopy the rotated template
c=copy.deepcopy(rb)
#print (c)
c[0] = sx * c[0]
c[1] = sx * c[1]
c[2] = tx
c[3] = sy * c[3]
c[4] = sy * c[4]
c[5] = ty
# convert to strings array
cstr=['','','','','','','','','']
i=0
while i <9:
cstr[i]= '{:f}'.format(c[i])
i+=1
chunks=self.chunks(cstr,3)
if debug is True:
print ('\n'+ position +' Monitor, Rotation = ' + rotation)
if text!='': print('\n'+text)
for chunk in chunks:
print (' '+' '.join(chunk))
return c,cstr
def send_xinput(self,display_id,coords_str,driver_name):
self.xinput_template=['xinput', 'set-prop', '', '--type=float', '"Coordinate Transformation Matrix"']
if driver_name !='':
# send command only if a display driver is present
xinput_command=copy.deepcopy(self.xinput_template)
xinput_command[2]=driver_name
xinput_command += coords_str
xinput_str=' '.join(xinput_command)
if self.debug:
print ('\nxinput call for ' + DisplayManager.display_reverse_map[display_id] + ':\n '+' '+xinput_str+'\n')
proc=subprocess.Popen(xinput_str, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
out,err=proc.communicate(xinput_str)
#strip newline
err= err[0:-1]
if len(err)==0:
return 'normal',''
else:
return 'error',err.decode('utf-8')+'|'
# ***********************************************
# Determine model of Pi - 1,2,3,4
# ************************************************
## awk '/^Revision/ {sub("^1000", "", $3); print $3}' /proc/cpuinfo
def pi_model(self):
command=['cat', '/proc/device-tree/model']
l_reply=subprocess.run(command,stdout=subprocess.PIPE)
l_reply_list=l_reply.stdout.decode('utf-8').split(' ')
if l_reply_list[2] == 'Zero':
return 0
elif l_reply_list[2] == 'Model':
return 1
else:
return int(l_reply_list[2])
# ***********************************************
# Read and process configuration data
# ************************************************
# read display.cfg
def read_config(self,pp_dir):
filename=pp_dir+os.sep+'pp_config'+os.sep+'pp_display.cfg'
if os.path.exists(filename):
DisplayManager.config = configparser.ConfigParser(inline_comment_prefixes = (';',))
DisplayManager.config.read(filename)
return 'normal','display.cfg read'
else:
return 'error',"Failed to find display.cfg at "+ filename
def displays_in_config(self):
return DisplayManager.config.sections()
def display_in_config(self,section):
return DisplayManager.config.has_section(section)
def get_item_in_config(self,section,item):
return DisplayManager.config.get(section,item)
def item_in_config(self,section,item):
return DisplayManager.config.has_option(section,item)
def get_rotation(self,did):
if not self.display_in_config(DisplayManager.display_reverse_map[did]):
return 'error','display not in display.cfg '+ DisplayManager.display_reverse_map[did],0,0
if not self.item_in_config(DisplayManager.display_reverse_map[did],'rotation-1-2-3'):
return 'null','',''
rot_text=self.get_item_in_config(DisplayManager.display_reverse_map[did],'rotation-1-2-3')
if rot_text=='':
return 'null','',''
if rot_text not in ('normal','right','inverted','left'):
return 'error','rotation not understood in display.cfg '+rot_text,''
return 'normal','',rot_text
def get_fake_dimensions(self,dname):
if not self.display_in_config(dname):
return 'error','display not in display.cfg '+ dname,0,0
if not self.item_in_config(dname,'fake-dimensions'):
return 'null','',0,0
size_text=self.get_item_in_config(dname,'fake-dimensions')
if size_text=='':
return 'null','',0,0
fields=size_text.split('*')
if len(fields)!=2:
return 'error','do not understand fake-dimensions in display.cfg for '+dname,0,0
elif fields[0].isdigit() is False or fields[1].isdigit() is False:
return 'error','fake dimensions are not positive integers in display.cfg for '+dname,0,0
else:
return 'normal','',int(fields[0]),int(fields[1])
def get_develop_window(self,dname):
if not self.display_in_config(dname):
return 'error','display not in display.cfg '+ dname,0,0
if not self.item_in_config(dname,'develop-window'):
return 'normal','',0,0,0.45,0.7
size_text=self.get_item_in_config(dname,'develop-window')
if size_text=='':
return 'normal','',0,0,0.45,0.7
if '+' in size_text:
# parse x+y+width*height
fields=size_text.split('+')
if len(fields) != 3:
return 'error','Do not understand Display Window in display.cfg for '+dname,0,0,0,0
dimensions=fields[2].split('*')
if len(dimensions)!=2:
return 'error','Do not understand Display Window in display.cfg for '+dname,0,0,0,0
if not fields[0].isdigit():
return 'error','x is not a positive decimal in display.cfg for '+dname,0,0,0,0
else:
x=float(fields[0])
if not fields[1].isdigit():
return 'error','y is not a positive decimal in display.cfg for '+dname,0,0,0,0
else:
y=float(fields[1])
if not self.is_scale(dimensions[0]):
return 'error','width1 is not a positive decimal in display.cfg for '+dname,0,0,0,0
else:
width=float(dimensions[0])
if not self.is_scale(dimensions[1]):
return 'error','height is not a positive decimal in display.cfg for '+dname,0,0,0,0
else:
height=float(dimensions[1])
return 'normal','',x,y,width,height
def is_scale(self,s):
try:
sf=float(s)
if sf > 0.0 and sf <=1:
return True
else:
return False
except ValueError:
return False
def get_driver_name(self,display_id):
if not self.display_in_config(DisplayManager.display_reverse_map[display_id]):
return 'error','display not in display.cfg '+ dname,''
if not self.item_in_config(DisplayManager.display_reverse_map[display_id],'touch-driver'):
return 'error','touch driver not in display.cfg for '+ DisplayManager.display_reverse_map[display_id],''
driver_name=self.get_item_in_config(DisplayManager.display_reverse_map[display_id],'touch-driver')
driver_name=driver_name.strip()
if len(driver_name)==0:
return 'null','',driver_name
if len(driver_name)<2 or driver_name[0] !='"' or driver_name[-1] != '"':
return 'error','driver-name must begin and end with ": '+driver_name,''
inside = driver_name.strip('"')
empty = inside.strip()
if len(empty)==0:
return 'null','',driver_name
return 'normal','',driver_name
# ***********************************************
# HDMI Monitor Commands for DSI and HDMI
# ************************************************
def handle_monitor_command(self,args):
#fields=command_text.split()
#args = fields[1:]
# print ('args',args)
if len(args) == 0:
return 'error','no arguments for monitor command'
if len (args) == 2:
command = args[0]
display= args[1].upper()
if display not in DisplayManager.display_map:
return 'error', 'Monitor Command - Display not known: '+ display
display_num=DisplayManager.display_map[display]
if display_num not in DisplayManager.displays:
return 'error', 'Monitor Command - Display not connected: '+ display
display_ref=str(display_num)
else:
command= args[0]