libtk2.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import __main__ as MAIN
  2. import json
  3. import sys
  4. import time
  5. _file_path = "/opt/LibreLight/Xdesk/"
  6. sys.path.insert(0,"/opt/LibreLight/Xdesk/")
  7. from lib.cprint import cprint
  8. import lib.libtk as libtk
  9. def serialize_event(event):
  10. data = {}
  11. for k in dir(event):
  12. if k.startswith("_"):
  13. continue
  14. v = event.__getattribute__(k)
  15. if v == '??':
  16. continue
  17. if type(v) not in [int,str,float]:
  18. continue
  19. data[k] = v
  20. data["event"] = str(event).split()[0][1:]
  21. if "state" in data:
  22. del data["state"]
  23. if "time" in data:
  24. del data["time"]
  25. if "serial" in data:
  26. del data["serial"]
  27. keys = list(data.keys())
  28. keys.sort()
  29. data2={}
  30. for k in keys:
  31. data2[k] = data[k]
  32. return data2
  33. Control_L = 0
  34. Alt_L = 0
  35. def tk_event(event,data={}):
  36. #print("tk_event",event,data)
  37. global Control_L,Alt_L
  38. if MAIN._global_key_lock:
  39. return
  40. #print(" ",dir(event)) #.dict())
  41. data = serialize_event(event)
  42. if 'keysym' in data:
  43. keysym = data["keysym"]
  44. if keysym == 'Control_L':
  45. if "Press" in data["event"]:
  46. Control_L = 1
  47. if "Release" in data["event"]:
  48. Control_L = 0
  49. if keysym == 'Alt_L':
  50. if "Press" in data["event"]:
  51. Alt_L = 1
  52. if "Release" in data["event"]:
  53. Alt_L = 0
  54. data["Alt_L"] = Alt_L
  55. data["Control_L"] = Control_L
  56. print("tk_event",data)
  57. ok=0
  58. # CONTROL + KEY
  59. key_code = {"s":"SAVE\nSHOW","c":"RESTART" }
  60. if 'keysym' in data:
  61. keysym = data["keysym"]
  62. if keysym in key_code:
  63. if "Press" in data['event'] and data["Control_L"]:
  64. MOD = key_code[keysym]
  65. msg=json.dumps([{"event":MOD}]).encode("utf-8")
  66. cprint("SEND tk_event",msg,color="green")
  67. MAIN.cmd_client.send(msg)
  68. if MOD in ["RESTART"]:
  69. time.sleep(2)
  70. exit()
  71. ok = 1
  72. if ok:
  73. return
  74. # NORMAL KEY
  75. key_code = {"r":"REC","x":"REC-FX","e":"EDIT","c":"CFG-BTN"
  76. ,"m":"MOVE","Delete":"DEL","End":"FX-OFF"
  77. ,"Escape":"ESC","s":"SELECT","f":"FLASH"
  78. ,"C":"COPY","d":"DEL","b":"BLIND","l":"LABEL"
  79. }
  80. if 'keysym' in data:
  81. keysym = data["keysym"]
  82. if keysym in key_code:
  83. if "Press" in data['event']:
  84. MOD = key_code[keysym]
  85. msg=json.dumps([{"event":MOD}]).encode("utf-8")
  86. cprint("SEND tk_event",msg,color="green")
  87. MAIN.cmd_client.send(msg)
  88. ok = 1
  89. cprint("OK",ok)
  90. if not ok:
  91. libtk.tk_keyboard_callback(event,data=data)