movewin.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. #!/usr/bin/python3
  2. import os
  3. import time
  4. import sys
  5. sys.path.insert(0,"/opt/LibreLight/Xdesk/")
  6. import psutil
  7. import json
  8. import inspect
  9. import _thread as thread
  10. import lib.showlib as showlib
  11. from lib.cprint import cprint
  12. SHOW_PATH = showlib.current_show_path() #SHOW_PATH
  13. # python3 movewin.py window-title x y
  14. # python3 movewin.py COMMA 723 943
  15. class Control():
  16. def __init__(self):
  17. self.title = "WinfoWinName"
  18. self.winid = ""
  19. def winfo(self):
  20. winid = winfo(self.title)
  21. if type(winid) == list:
  22. if len(winid) >= 1:
  23. self.winid = winid[0]
  24. def winfo2(self):
  25. winid = winfo2(self.title)
  26. #if type(winid) == list:
  27. # if len(winid) >= 1:
  28. # self.winid = winid[0]
  29. def move(self,x=None,y=None):
  30. if self.winid:
  31. cmd=movewin(_id=self.winid,x=x,y=y)
  32. print("movewin.move:",cmd)
  33. system(cmd)
  34. def size(self,x=None,y=None):
  35. if self.winid:
  36. cmd = sizewin(_id=self.winid,x=x,y=y)
  37. system(cmd)
  38. def activate(self):
  39. if self.winid:
  40. cmd=activate(_id=self.winid)
  41. system(cmd)
  42. def winfo(name="WinfoWinName"):
  43. search = name
  44. cmd = "xwininfo -root -children -all | grep '{}'"
  45. cmd = cmd.format(search)
  46. print(cmd)
  47. r = os.popen(cmd)
  48. lines = r.readlines()
  49. _id = [] #"xxxx"
  50. if lines and lines[0]:
  51. _id.append( lines[0].split()[0] )
  52. print("ID:",_id)
  53. for line in lines:
  54. line = line.strip()
  55. print("-",line)
  56. return _id
  57. def parse_winfo_line(line):
  58. a1 = 0
  59. a2 = 0
  60. ps_name = ""
  61. ps_title = ""
  62. ps_id = ""
  63. ps_pos = ""
  64. ps_size = ""
  65. if line: # in lines:
  66. a1 = line.index(' ("')+2
  67. a2 = line.index('") ')+1
  68. ps_id = line.split()[0]
  69. ps_id = ps_id.replace(" ","")
  70. ps_pos = line.split()[-1]
  71. ps_size = line.split()[-2]
  72. ps_pos = ps_pos.replace("+"," ").replace("x"," ")
  73. ps_size = ps_size.replace("+"," ").replace("x"," ")
  74. ps_pos = ps_pos.strip().split()
  75. ps_size = ps_size.strip().split()
  76. for i in range(len(ps_pos)):
  77. ps_pos[i] = int(ps_pos[i])
  78. for i in range(len(ps_size)):
  79. ps_size[i] = int(ps_size[i])
  80. ps_pos[-2] -= ps_size[-2]
  81. ps_pos[-1] -= ps_size[-1]
  82. #ps_pos_x1 = int(ps_apos.split("+")[-2])
  83. #ps_pos_y1 = int(ps_pos.split("+")[-1])
  84. if a1 >= 0 and a2 >= 0:
  85. ps_name = line[a1:a2]
  86. line = line[:a1]+line[a2:]
  87. if ' "' in line and '":' in line:
  88. a1 = line.index(' "')+2
  89. a2 = line.index('":')
  90. ps_title = line[a1:a2]
  91. _line = [ps_id,ps_name,ps_title,ps_size,ps_pos]
  92. print(" ",_line)
  93. return _line
  94. def winfo2(name="WinfoWinName"):
  95. #print("--------------")
  96. search = name
  97. cmd = "xwininfo -root -children -all | grep '{}'"
  98. cmd = cmd.format(search)
  99. #print(cmd)
  100. r = os.popen(cmd)
  101. lines = r.readlines()
  102. _data = []
  103. for line in lines:
  104. a = parse_winfo_line(line)
  105. if a:
  106. _data.append(a)
  107. #print("--------------")
  108. return _data
  109. def get_store_sdl_line():
  110. #print()
  111. #print("-> def",inspect.currentframe().f_code.co_name,"-"*10)
  112. lines = []
  113. lines.extend( winfo2(name="SDL-"))
  114. lines.extend( winfo2(name="TK-"))
  115. lines.extend( winfo2(name="RAY-"))
  116. lines.extend( winfo2(name="R-"))
  117. lines.extend( winfo2(name="EXEC-BTN"))
  118. lines.extend( winfo2(name="FX3"))
  119. lines.extend( winfo2(name="EXEC-XWING"))
  120. out_lines=[]
  121. for line in lines:
  122. t=line[2].split()
  123. for k in t:
  124. k = k.replace(" ", "_")
  125. for j in ["SDL-","RAY-","R-","TK-","EXEC-BTN","FX3", "EXEC-XWING"]:
  126. if not k.startswith(j):
  127. continue
  128. s=line[-2]
  129. p=line[-1]
  130. # info: b x h + x + y
  131. out = [1,k, s[0],s[1],p[0],p[1] ]
  132. out_lines.append(out)
  133. return out_lines
  134. def load_all_sdl(title="X"):
  135. fname = SHOW_PATH + "/gui-sdl.txt"
  136. if not os.path.isfile(fname):
  137. return
  138. f=open(fname,"r")
  139. lines = f.readlines()
  140. f.close()
  141. print(" load_all_sdl fname:",fname)
  142. for line in lines:
  143. if title in line:
  144. return json.loads(line)
  145. def _start_sub(cmd,name,mute=0):
  146. r = os.popen(cmd)
  147. while 1:
  148. #print(dir(r))
  149. line = r.readline()
  150. if not line:
  151. break
  152. line = line.strip()
  153. if mute == 0:
  154. cprint(name,":",[line],color="blue")
  155. cprint("EXIT:",name,cmd)
  156. #BrokenPipeError: [Errno 32] Broken pipe
  157. def start_sub(cmd,name="<PROCESS>",mute=0):
  158. cprint(" start_sub",cmd,name,"mute-stdout:",mute,color="green")
  159. thread.start_new_thread(_start_sub,(cmd,name,mute)) # SERVER
  160. def startup_all_sdl():
  161. #print()
  162. #print("-> def",inspect.currentframe().f_code.co_name,"-"*10)
  163. fname = SHOW_PATH + "/gui-sdl.txt"
  164. if not os.path.isfile(fname):
  165. return
  166. f=open(fname,"r")
  167. xlines = f.readlines()
  168. f.close()
  169. print()
  170. cprint("startup_all_sdl() ",fname,color="yellow")
  171. xlines2 = []
  172. for line in xlines:
  173. # cleanup and info loop
  174. line = line.strip()
  175. if line.startswith("#-- history"):
  176. break
  177. if line.startswith("#"):
  178. continue
  179. if not line:
  180. continue
  181. xlines2.append(line)
  182. print(" line >> ",[line])
  183. # ------------------
  184. print()
  185. for line in xlines2:
  186. #print(" line >> ",[line])
  187. try:
  188. line = json.loads(line)
  189. except json.decoder.JSONDecodeError as e:
  190. cprint("ERR",e,color="red")
  191. continue
  192. cmd = "python3 /opt/LibreLight/Xdesk/tksdl/{}"
  193. if "RAY" in line[1]:
  194. cmd = "python3 /opt/LibreLight/Xdesk/tkray/{}"
  195. if "--easy" in sys.argv:
  196. if line[1] == "EXEC-BTN":
  197. cmd = "python3 /opt/LibreLight/Xdesk/tkgui/{}"
  198. cmd=cmd.format("EXEC-BTN.py")
  199. #r=os.popen(cmd)
  200. start_sub(cmd,"EXEC-BTN",mute=1)
  201. else:
  202. if line[1] == "SDL-MIDI":
  203. cmd=cmd.format("midi.py")
  204. #r=os.popen(cmd)
  205. start_sub(cmd,"SDL-MIDI",mute=1)
  206. elif line[1] == "SDL-DMX":
  207. cmd=cmd.format("dmx.py")
  208. #os.popen(cmd)
  209. start_sub(cmd,"SDL-DMX",mute=1)
  210. elif line[1] == "R-FIX-LIST":
  211. cmd=cmd.format("fix.py")
  212. #r=os.popen(cmd)
  213. start_sub(cmd,"R-FIX-LIST",mute=1)
  214. elif line[1] == "RAY-DMX":
  215. cmd=cmd.format("dmx.py")
  216. #r=os.popen(cmd)
  217. start_sub(cmd,"RAY-DMX",mute=1)
  218. elif line[1] == "SDL-FIX-LIST":
  219. cmd=cmd.format("fix.py")
  220. #r=os.popen(cmd)
  221. start_sub(cmd,"SDL-FIX",mute=1)
  222. elif line[1] == "FX3":
  223. cmd = "python3 /opt/LibreLight/Xdesk/tkgui/{}"
  224. cmd=cmd.format("FX3.py")
  225. #r=os.popen(cmd)
  226. start_sub(cmd,"FX3",mute=1)
  227. elif line[1] == "EXEC-BTN":
  228. cmd = "python3 /opt/LibreLight/Xdesk/tkgui/{}"
  229. cmd=cmd.format("EXEC-BTN.py")
  230. #r=os.popen(cmd)
  231. start_sub(cmd,"EXEC-BTN",mute=1)
  232. elif line[1] == "EXEC-XWING":
  233. cmd = "python3 /opt/LibreLight/Xdesk/tkgui/{}"
  234. cmd=cmd.format("EXEC-XWING.py")
  235. #r=os.popen(cmd)
  236. start_sub(cmd,"EXEC-XWING",mute=1)
  237. time.sleep(0.3)
  238. print()
  239. def store_all_sdl():
  240. #print()
  241. #print("-> def",inspect.currentframe().f_code.co_name,"-"*10)
  242. error = 0
  243. fname = SHOW_PATH + "/gui-sdl.txt"
  244. in_lines = []
  245. if os.path.isfile(fname):
  246. f=open(fname,"r")
  247. xlines = f.readlines()
  248. f.close()
  249. #print(" store_all_sdl fname",fname)
  250. for line in xlines:
  251. line = line.strip()
  252. if not line.startswith("#") and line:
  253. in_lines.append(line)
  254. #in_lines.append('[0,"xx aa",0,0,0,0]')
  255. #for line in in_lines:
  256. # print(" R:",[line])
  257. lines = get_store_sdl_line()
  258. ap_line = []
  259. pop = []
  260. for line in lines:
  261. ok = 0
  262. iline = ""
  263. for j,iline in enumerate(in_lines):
  264. if line[1] in iline:
  265. if j not in pop:
  266. pop.append(j)
  267. #print(" del ",j,line)
  268. for i in pop[::-1]:
  269. try:
  270. if len(in_lines) > i:
  271. in_lines.pop(i)
  272. else:
  273. print(" index",i,"not in",len(in_lines))
  274. except Exception as e:
  275. cprint(" ERR:",e,i,color="red")
  276. error += 0
  277. temp = {}
  278. for i in in_lines:
  279. k = json.loads(i)[1]
  280. if k not in temp:
  281. temp[k] = i
  282. f=open(fname,"w")
  283. f.write("#"+json.dumps(["on","title","w","h","x","y"])+"\n")
  284. for line in lines:
  285. print(" --",line)
  286. f.write(json.dumps(line)+"\n")
  287. f.write("\n")
  288. f.write("#-- history \n")
  289. for k,line in temp.items(): #in_lines:
  290. #print("+++>",line)
  291. print(" --",line)
  292. f.write(line+"\n")
  293. f.write("\n")
  294. f.close()
  295. if not error:
  296. cprint(" store_all_sdl OK fname:",fname,color="green")
  297. return 1
  298. cprint(" store_all_sdl FAIL fname:",fname,color="red")
  299. def movewin(_id="0xWinId",x=None,y=None):
  300. print()
  301. #print("-> def",inspect.currentframe().f_code.co_name,"-"*10)
  302. cmd="xdotool windowmove {} {} {}".format(_id,x,y)
  303. print("movewin.movewin:",cmd)
  304. return cmd
  305. def sizewin(_id="0xWinId",x=None,y=None):
  306. print()
  307. #print("-> def",inspect.currentframe().f_code.co_name,"-"*10)
  308. cmd="xdotool windowsize {} {} {}".format(_id,x,y)
  309. return cmd
  310. def activate(_id="0xWinId"):
  311. cmd="xdotool windowactivate {}".format(_id)
  312. return cmd
  313. def system(cmd):
  314. print(cmd)
  315. os.system(cmd)
  316. def search_process(_file_path,exact=1):
  317. print("search_process",_file_path)
  318. pids = psutil.pids()
  319. count = 0
  320. out = []
  321. for pid in pids:
  322. try:
  323. p = psutil.Process(pid)
  324. except psutil.NoSuchProcess:
  325. break
  326. ps = p.cmdline()
  327. if len(ps) < 2:
  328. continue
  329. if "python" not in ps[0]:
  330. continue
  331. #print(" ",[ps[1]])
  332. #print("exact_search",exact)
  333. if exact:
  334. if str(_file_path) == str(ps[1]):
  335. print(ps)
  336. count += 1
  337. out.append(pid)
  338. else:
  339. if str(_file_path) in str(ps[1]):
  340. print(ps)
  341. count += 1
  342. out.append(pid)
  343. print("search_process",count)
  344. return out
  345. def process_kill(path):
  346. pids = search_process(path,exact=0)
  347. for pid in pids:
  348. print("process_kill:",[path,pid])
  349. cmd="kill -kill {} ".format(pid)
  350. os.system(cmd)
  351. time.sleep(0.2)
  352. #for pid in pids:
  353. # print("process_kill:",path,pid)
  354. # p = psutil.Process(pid)
  355. # #p.name()
  356. # #p.cmdline()
  357. # p.terminate()
  358. # p.wait()
  359. print("process_kill OK")
  360. def get_lineno():
  361. callerframerecord = inspect.stack()[1] # 0 represents this line
  362. # 1 represents line at caller
  363. frame = callerframerecord[0]
  364. info = inspect.getframeinfo(frame)
  365. #print(info.filename) # __FILE__ -> Test.py
  366. #print(info.function) # __FUNCTION__ -> Main
  367. #print(info.lineno) # __LINE__ -> 13
  368. return info.lineno
  369. if __name__ == "__main__":
  370. print("# python3 movewin.py window-title x y")
  371. print("# python3 movewin.py COMMA 723 943")
  372. import random
  373. a=random.randint(100,400)
  374. b=random.randint(100,400)
  375. search = "ASD"
  376. try:
  377. search = sys.argv[1]
  378. search = search.replace("'","")
  379. except:pass
  380. try:
  381. a = sys.argv[2]
  382. except:pass
  383. try:
  384. b = sys.argv[3]
  385. except:pass
  386. _ids = winfo(search)
  387. for _id in _ids:
  388. c1 = sizewin(_id,a,b)
  389. c2 = movewin(_id,a,b)
  390. c3 = activate(_id)
  391. system(c1)
  392. time.sleep(0.1)
  393. system(c2)
  394. time.sleep(0.1)
  395. system(c3)
  396. def check_is_started(CAPTION,_file_path,sleep=0):
  397. if sleep:
  398. time.sleep(sleep)
  399. pids = search_process(_file_path)
  400. if len(pids) >= 2:
  401. search = CAPTION[:]
  402. _ids = winfo(search)
  403. for _id in _ids:
  404. c3 = activate(_id)
  405. print()
  406. print(" check_is_started CMD:",c3)
  407. print(" EXIT",_file_path,CAPTION)
  408. os.system(c3)
  409. time.sleep(1)
  410. print(" EXIT",_file_path,CAPTION)
  411. print()
  412. sys.exit()
  413. if __name__ == "__main__":
  414. w = Control()
  415. print(sys.argv)
  416. w.title = "gpk-update-viewer-org"
  417. w.title = "Paketaktualisierung\""
  418. if len(sys.argv) >= 2:
  419. w.title = sys.argv[1]
  420. w.winfo()
  421. w.move(10,10)