FX3.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #!/usr/bin/python3
  2. import json
  3. import time
  4. import sys
  5. import os
  6. import tkinter as tk
  7. import traceback
  8. import _thread as thread
  9. sys.path.insert(0,"/opt/LibreLight/Xdesk/")
  10. gui=None
  11. INIT_OK = 1
  12. IS_GUI = 0
  13. import __main__ as MAIN
  14. import lib.meta as meta
  15. import tool.git as git
  16. CAPTION = "FX3"
  17. title = CAPTION
  18. title += git.get_all()
  19. from lib.cprint import cprint
  20. import lib.libtk as libtk
  21. import lib.libtk2 as libtk2
  22. import lib.zchat as chat
  23. import lib.mytklib as mytklib
  24. import lib.tkevent as tkevent
  25. #import tkgui.draw as draw
  26. import tool.movewin as movewin
  27. movewin.check_is_started("FX3","/opt/LibreLight/Xdesk/tkgui/FX3.py")
  28. def _add_space(frame,r,c):
  29. b = tk.Canvas(frame,bg="black", height=2,bd=0,width=6,highlightthickness=0) #,bd="black")
  30. b.grid(row=r, column=c, sticky=tk.W+tk.E)
  31. r+=1
  32. c=0
  33. return r,c
  34. def _draw_fx3(frame,c,r,gui,mode="FX"):
  35. ct = MAIN.meta.fx3 #moves
  36. prm = MAIN.meta.fx3_prm
  37. for comm in ct.labels:
  38. if comm == "\n\n":
  39. b = tk.Label(frame,bg="black", text="-",font=space_font)
  40. b.grid(row=r, column=c,pady=0,padx=0, sticky=tk.W+tk.E)
  41. c=0
  42. r+=1
  43. continue
  44. if comm == "\n":
  45. c=0
  46. r+=1
  47. continue
  48. v=0
  49. b = tk.Button(frame,bg="lightgrey", text=str(comm),width=6,height=1)
  50. ct.elem[comm] = b
  51. ct.val[comm] = 0
  52. def XXX(event=None,**data):
  53. print("XXX",event,data)
  54. #b.bind("<Button>",XXX) #tkevent.tk_event_fx(fix=0,elem=b,attr=comm,data=gui,mode=mode).cb)
  55. print(":",[comm])
  56. b.bind("<Button>",tkevent.tk_event_fx(fix=0,elem=b,attr=comm,data=gui,mode=mode).cb)
  57. if comm == "xREC-FX":
  58. b["bg"] = "grey"
  59. elif comm == "xFX OFF":
  60. b["bg"] = "magenta"
  61. elif comm[:3] == "xFX:" or comm[:3] == "x2D:":
  62. b["text"] = comm
  63. b["bg"] = "#ffbf00"
  64. if comm == "PLAY":
  65. b["bg"] = "orange"
  66. b.config(activebackground="#dd0")
  67. if comm in MAIN.meta.fx3_grid:
  68. fx_func = MAIN.meta.fx3_grid[comm]
  69. fx_func.data["wrap"] = 1
  70. #fx_func.inc()
  71. v=fx_func.val()
  72. print(fx_func,v)
  73. b["text"] = v
  74. if comm.startswith("TYPE:"):
  75. b["bg"] = "orange"
  76. b.config(activebackground="#dd0")
  77. for a in ["SPEED:","SIZE:","START:","OFFSET:"]:
  78. if comm.startswith(a):
  79. b["bg"] = "lightgreen"
  80. b.config(activebackground="#0d0")
  81. if comm:
  82. b.grid(row=r, column=c, sticky=tk.W+tk.E)
  83. c+=1
  84. if c >=6:
  85. c=0
  86. r+=1
  87. c=0
  88. r+=1
  89. return c,r
  90. def _draw_fx(frame,c,r,gui,mode="FX"):
  91. ct = None
  92. prm = None
  93. if mode=="FX-MAIN":
  94. ct = MAIN.meta.fx_main
  95. prm = MAIN.meta.fx_prm_main
  96. for comm in ct.labels:
  97. if comm == "\n\n":
  98. b = tk.Label(frame,bg="black", text="-",font=space_font)
  99. b.grid(row=r, column=c,pady=0,padx=0, sticky=tk.W+tk.E)
  100. c=0
  101. r+=1
  102. continue
  103. if comm == "\n":
  104. c=0
  105. r+=1
  106. continue
  107. v=0
  108. b = tk.Button(frame,bg="lightgrey", text=str(comm),width=6,height=2)
  109. ct.elem[comm] = b
  110. ct.val[comm] = 0
  111. b.bind("<Button>",tkevent.tk_event_fx(fix=0,elem=b,attr=comm,data=gui,mode=mode).cb)
  112. if comm == "REC-FX":
  113. b["bg"] = "grey"
  114. elif comm == "FX OFF":
  115. b["bg"] = "magenta"
  116. b.grid(row=r, column=c, sticky=tk.W+tk.E)
  117. c+=1
  118. if c >=6:
  119. c=0
  120. r+=1
  121. c=0
  122. r+=1
  123. return c,r
  124. def draw_fx3(gui,xframe):
  125. frame_fx=xframe
  126. i=0
  127. c=0
  128. r=0
  129. frame = tk.Frame(frame_fx,bg="black")
  130. frame.pack(fill=tk.X, side=tk.TOP)
  131. # ------------------------------
  132. b = tk.Button(frame,bg="lightblue", text="FX3",width=6)
  133. b.grid(row=r, column=c, sticky=tk.W+tk.E)
  134. c+=1
  135. _add_space(frame,r,c)
  136. c,r = _draw_fx(frame,c,r,gui,mode="FX-MAIN")
  137. r,c=_add_space(frame,r,c)
  138. c,r = _draw_fx3(frame,c,r,gui,mode="FX-3")
  139. _global_short_key = 1
  140. root = None
  141. cmd_client = chat.Client(port=30003)
  142. try:
  143. import memcache
  144. mc = memcache.Client(['127.0.0.1:11211'], debug=0)
  145. #data = mc.get("MODES")
  146. except:
  147. mc = None
  148. class MANAGER(): # DUMMY
  149. def __init__(self,*arg,**args):
  150. print(self,"__init__",arg,args)
  151. def send(self,nr,*arg,**args): #val=None,xfade=None,event=None,button="",ptfade=None):
  152. if _global_key_lock:
  153. return
  154. print(self,"MASTER",nr,arg,args)
  155. btn_nr = nr
  156. v = args["val"]
  157. msg=json.dumps([{"event":"EXEC","EXEC":btn_nr+1,"VAL":v,"NR-KEY":btn_nr}]).encode("utf-8")
  158. msg=json.dumps([{"event":"XXX","YYY":1,"VAL":12}]).encode("utf-8")
  159. cprint("SEND MASTER._GO:",msg,color="green")
  160. cmd_client.send(msg)
  161. class Modes(): # DUMMY
  162. def __init__(self,*arg,**args):
  163. print("Modes.__init__",arg,args)
  164. self.modes = {}
  165. def val(self,*arg,**args):
  166. #print("Modes.val",arg,args)
  167. pass
  168. class Gui(): # DUMMY
  169. def __init__(self):
  170. self.elem_exec = []
  171. self.elem_meta = [None]*512
  172. self.old_btn_nr = -1
  173. self.METAS = []
  174. for i in range(512):
  175. self.METAS.append({})
  176. def _load_meta(self,nr):
  177. data = {}
  178. try:
  179. data = mc.get("EXEC-META-"+str(nr))
  180. data = json.loads(data)
  181. except Exception as e:
  182. print(" ER1R mc...",e)
  183. return data
  184. def _refresh_data(self,*arg,**args):
  185. start = time.time()
  186. METAS = []
  187. for i in range(512):
  188. nr = i #+1
  189. METAS.append( self._load_meta(nr) )
  190. self.METAS = METAS
  191. for nr,b in enumerate( self.elem_exec): #[nr]
  192. self._refresh_data_single(nr,b,METAS)
  193. print(__file__.split("/")[-1], "_refresh_data:",int((time.time()-start)*10)/10 )
  194. def _refresh_data_single(self,nr,b,METAS=None):
  195. start = time.time()
  196. no_meta = 0
  197. if not METAS:
  198. no_meta = 1
  199. try:
  200. data = mc.get("EXEC-META-"+str(nr)) #,json.dumps(index))
  201. data = json.loads(data)
  202. self.METAS[nr] = data #.append(data)
  203. #print(time.time())
  204. print(" _REFRESH_EXEC_SINGLE",nr,b,data["LABEL"])
  205. except Exception as e:
  206. print(" ER1R mc...",e)
  207. METAS = self.METAS
  208. manager = MANAGER()
  209. modes = Modes()
  210. gui = Gui()
  211. root = tk.Tk()
  212. root.maxsize(600,800)
  213. win_title="FX3"
  214. store = movewin.load_all_sdl(win_title)
  215. print(store)
  216. W=380
  217. H=550
  218. POS=[800,200]
  219. if store:
  220. W = store[-4]
  221. H = store[-3]
  222. POS=[store[-2],store[-1]]
  223. root.geometry('%dx%d+%d+%d' % (W, H, POS[0],POS[1]))
  224. root.tk_setPalette(background='#bbb', foreground='black', activeBackground='#aaa', activeForeground="black")
  225. defaultFont = tk.font.nametofont("TkDefaultFont")
  226. defaultFont.configure(family="FreeSans", size=10, weight="bold")
  227. # MAIN MENUE
  228. try:
  229. ico_path = "/opt/LibreLight/Xdesk/icon/"
  230. root.iconphoto(False, tk.PhotoImage(file=ico_path+"scribble.png"))
  231. except Exception as e:
  232. print(" Exception GUIWindowContainer.__init__",e)
  233. xframe = libtk.ScrollFrame(root,width=820,height=400,bd=1,bg="black",head=None,foot=None)
  234. draw_fx3(gui,xframe)
  235. root.title(title)
  236. root.bind("<Button>",libtk2.tk_event)#
  237. root.bind("<Key>",libtk2.tk_event)#,self.callback)
  238. root.bind("<KeyRelease>",libtk2.tk_event)#,self.callback)
  239. #root.bind("<FocusIn>",libtk2.tk_event)#, on_focus(self.args["title"],"In").cb)
  240. #root.bind("<FocusOut>",libtk2.tk_event)#, on_focus(self.args["title"],"Out").cb)
  241. _global_key_lock = 0
  242. def focus_in(event=None):
  243. _global_short_key = 0 # protect key-press-repeat
  244. cmd = "xset -display :0.0 r off"
  245. print("FOCUS_IN1", cmd)
  246. os.system(cmd)
  247. time.sleep(0.3)
  248. print("FOCUS_IN2", cmd)
  249. os.system(cmd)
  250. _global_short_key = 1 # protect key-press-repeat
  251. time.sleep(0.3)
  252. _global_key_lock = 0
  253. def focus_out(event=None):
  254. _global_key_lock = 1
  255. _global_short_key = 0
  256. cmd="xset -display :0.0 r rate 240 20"
  257. print("FOCUS_OUT", cmd)
  258. #os.system(cmd) # DISABLED
  259. root.bind("<FocusIn>", focus_in)
  260. root.bind("<FocusOut>", focus_out)
  261. def _refr_loop():
  262. time.sleep(3)
  263. while 1:
  264. try:
  265. gui._refresh_data()
  266. time.sleep(3)
  267. except Exception as e:
  268. print(" ER5R refr_loop()...",e)
  269. time.sleep(3)
  270. time.sleep(0.1)
  271. def _refr_loop2():
  272. time.sleep(3)
  273. while 1:
  274. try:
  275. global root,title
  276. data = mc.get("MODES")
  277. title2 = title +" "+str(data)
  278. data = json.loads(data)
  279. #print("MODES",data)
  280. modes.modes = data
  281. if root:
  282. root.title(title2)
  283. if "S-KEY" in data:
  284. _global_short_key = 0
  285. if data["S-KEY"]:
  286. _global_short_key = 1
  287. for k in data:
  288. #print("title",k)
  289. for kk in ["RESTART","PRO","EASY","EXIT"]:
  290. if kk in str(k):
  291. print("RESTART !!! EXIT !!")
  292. time.sleep(0.1)
  293. root.quit()
  294. #sys.exit()
  295. except Exception as e:
  296. print(" ER7R mc...",e)
  297. time.sleep(3)
  298. time.sleep(0.1)
  299. thread.start_new_thread(_refr_loop,())
  300. thread.start_new_thread(_refr_loop2,())
  301. root.mainloop()