movewin.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 = winfo2(name="SDL-")
  113. lines.extend( winfo2(name="TK-"))
  114. lines.extend( winfo2(name="EXEC-BTN"))
  115. out_lines=[]
  116. for line in lines:
  117. t=line[2].split()
  118. for k in t:
  119. k = k.replace(" ", "_")
  120. if "SDL-" in k or "TK-" in k or "EXEC-BTN" in k:
  121. s=line[-2]
  122. p=line[-1]
  123. # info: b x h + x + y
  124. out = [1,k, s[0],s[1],p[0],p[1] ]
  125. out_lines.append(out)
  126. return out_lines
  127. def load_all_sdl(title="X"):
  128. fname = SHOW_PATH + "/gui-sdl.txt"
  129. if os.path.isfile(fname):
  130. f=open(fname,"r")
  131. lines = f.readlines()
  132. f.close()
  133. print(" load_all_sdl fname:",fname)
  134. for line in lines:
  135. if title in line:
  136. return json.loads(line)
  137. def _start_sub(cmd,name,mute=0):
  138. r = os.popen(cmd)
  139. while 1:
  140. #print(dir(r))
  141. line = r.readline()
  142. if not line:
  143. break
  144. line = line.strip()
  145. if mute == 0:
  146. cprint(name,":",[line],color="blue")
  147. cprint("EXIT:",name,cmd)
  148. #BrokenPipeError: [Errno 32] Broken pipe
  149. def start_sub(cmd,name="<PROCESS>",mute=0):
  150. cprint(" start_sub",cmd,name,"mute-stdout:",mute,color="green")
  151. thread.start_new_thread(_start_sub,(cmd,name,mute)) # SERVER
  152. def startup_all_sdl():
  153. #print()
  154. #print("-> def",inspect.currentframe().f_code.co_name,"-"*10)
  155. fname = SHOW_PATH + "/gui-sdl.txt"
  156. if os.path.isfile(fname):
  157. f=open(fname,"r")
  158. xlines = f.readlines()
  159. f.close()
  160. print()
  161. cprint("startup_all_sdl() ",fname,color="yellow")
  162. for line in xlines:
  163. line = line.strip()
  164. if line.startswith("#-- history"):
  165. break
  166. elif line.startswith("#"):
  167. continue
  168. if not line:
  169. continue
  170. else:
  171. #print(" line >> ",[line])
  172. try:
  173. line = json.loads(line)
  174. cmd = "python3 /opt/LibreLight/Xdesk/tksdl/{}"
  175. if line[1] == "SDL-MIDI":
  176. cmd=cmd.format("midi.py")
  177. #r=os.popen(cmd)
  178. start_sub(cmd,"SDL-MIDI",mute=1)
  179. elif line[1] == "SDL-DMX":
  180. cmd=cmd.format("dmx.py")
  181. #os.popen(cmd)
  182. start_sub(cmd,"SDL-DMX",mute=1)
  183. elif line[1] == "SDL-FIX-LIST":
  184. cmd=cmd.format("fix.py")
  185. #r=os.popen(cmd)
  186. start_sub(cmd,"SDL-FIX",mute=1)
  187. elif line[1] == "EXEC-BTN":
  188. cmd = "python3 /opt/LibreLight/Xdesk/tkgui/{}"
  189. cmd=cmd.format("EXEC-BTN.py")
  190. #r=os.popen(cmd)
  191. start_sub(cmd,"EXEC-BTN",mute=1)
  192. except json.decoder.JSONDecodeError as e:
  193. cprint("ERR",e,color="red")
  194. time.sleep(0.3)
  195. print()
  196. def store_all_sdl():
  197. #print()
  198. #print("-> def",inspect.currentframe().f_code.co_name,"-"*10)
  199. error = 0
  200. fname = SHOW_PATH + "/gui-sdl.txt"
  201. in_lines = []
  202. if os.path.isfile(fname):
  203. f=open(fname,"r")
  204. xlines = f.readlines()
  205. f.close()
  206. #print(" store_all_sdl fname",fname)
  207. for line in xlines:
  208. line = line.strip()
  209. if not line.startswith("#") and line:
  210. in_lines.append(line)
  211. #in_lines.append('[0,"xx aa",0,0,0,0]')
  212. #for line in in_lines:
  213. # print(" R:",[line])
  214. lines = get_store_sdl_line()
  215. ap_line = []
  216. pop = []
  217. for line in lines:
  218. ok = 0
  219. iline = ""
  220. for j,iline in enumerate(in_lines):
  221. if line[1] in iline:
  222. if j not in pop:
  223. pop.append(j)
  224. #print(" del ",j,line)
  225. for i in pop[::-1]:
  226. try:
  227. in_lines.pop(i)
  228. except Exception as e:
  229. cprint(" ERR:",e,color="red")
  230. error += 0
  231. temp = {}
  232. for i in in_lines:
  233. k = json.loads(i)[1]
  234. if k not in temp:
  235. temp[k] = i
  236. f=open(fname,"w")
  237. f.write("#"+json.dumps(["on","title","w","h","x","y"])+"\n")
  238. for line in lines:
  239. f.write(json.dumps(line)+"\n")
  240. f.write("\n")
  241. f.write("#-- history \n")
  242. for k,line in temp.items(): #in_lines:
  243. #print("+++>",line)
  244. f.write(line+"\n")
  245. f.write("\n")
  246. f.close()
  247. if not error:
  248. cprint(" store_all_sdl OK fname:",fname,color="green")
  249. return 1
  250. cprint(" store_all_sdl FAIL fname:",fname,color="red")
  251. def movewin(_id="0xWinId",x=None,y=None):
  252. print()
  253. #print("-> def",inspect.currentframe().f_code.co_name,"-"*10)
  254. cmd="xdotool windowmove {} {} {}".format(_id,x,y)
  255. print("movewin.movewin:",cmd)
  256. return cmd
  257. def sizewin(_id="0xWinId",x=None,y=None):
  258. print()
  259. #print("-> def",inspect.currentframe().f_code.co_name,"-"*10)
  260. cmd="xdotool windowsize {} {} {}".format(_id,x,y)
  261. return cmd
  262. def activate(_id="0xWinId"):
  263. cmd="xdotool windowactivate {}".format(_id)
  264. return cmd
  265. def system(cmd):
  266. print(cmd)
  267. os.system(cmd)
  268. def search_process(_file_path,exact=1):
  269. print("search_process",_file_path)
  270. pids = psutil.pids()
  271. count = 0
  272. out = []
  273. for pid in pids:
  274. try:
  275. p = psutil.Process(pid)
  276. except psutil.NoSuchProcess:
  277. break
  278. ps = p.cmdline()
  279. if len(ps) < 2:
  280. continue
  281. if "python" not in ps[0]:
  282. continue
  283. #print(" ",[ps[1]])
  284. #print("exact_search",exact)
  285. if exact:
  286. if str(_file_path) == str(ps[1]):
  287. print(ps)
  288. count += 1
  289. out.append(pid)
  290. else:
  291. if str(_file_path) in str(ps[1]):
  292. print(ps)
  293. count += 1
  294. out.append(pid)
  295. print("search_process",count)
  296. return out
  297. def process_kill(path):
  298. pids = search_process(path,exact=0)
  299. for pid in pids:
  300. print("process_kill:",[path,pid])
  301. cmd="kill -kill {} ".format(pid)
  302. os.system(cmd)
  303. time.sleep(0.2)
  304. #for pid in pids:
  305. # print("process_kill:",path,pid)
  306. # p = psutil.Process(pid)
  307. # #p.name()
  308. # #p.cmdline()
  309. # p.terminate()
  310. # p.wait()
  311. print("process_kill OK")
  312. def get_lineno():
  313. callerframerecord = inspect.stack()[1] # 0 represents this line
  314. # 1 represents line at caller
  315. frame = callerframerecord[0]
  316. info = inspect.getframeinfo(frame)
  317. #print(info.filename) # __FILE__ -> Test.py
  318. #print(info.function) # __FUNCTION__ -> Main
  319. #print(info.lineno) # __LINE__ -> 13
  320. return info.lineno
  321. if __name__ == "__main__":
  322. print("# python3 movewin.py window-title x y")
  323. print("# python3 movewin.py COMMA 723 943")
  324. import random
  325. a=random.randint(100,400)
  326. b=random.randint(100,400)
  327. search = "ASD"
  328. try:
  329. search = sys.argv[1]
  330. search = search.replace("'","")
  331. except:pass
  332. try:
  333. a = sys.argv[2]
  334. except:pass
  335. try:
  336. b = sys.argv[3]
  337. except:pass
  338. _ids = winfo(search)
  339. for _id in _ids:
  340. c1 = sizewin(_id,a,b)
  341. c2 = movewin(_id,a,b)
  342. c3 = activate(_id)
  343. system(c1)
  344. time.sleep(0.1)
  345. system(c2)
  346. time.sleep(0.1)
  347. system(c3)
  348. def check_is_started(CAPTION,_file_path,sleep=0):
  349. if sleep:
  350. time.sleep(sleep)
  351. pids = search_process(_file_path)
  352. if len(pids) >= 2:
  353. search = CAPTION[:]
  354. _ids = winfo(search)
  355. for _id in _ids:
  356. c3 = activate(_id)
  357. print()
  358. print(" check_is_started CMD:",c3)
  359. print(" EXIT",_file_path,CAPTION)
  360. os.system(c3)
  361. time.sleep(1)
  362. print(" EXIT",_file_path,CAPTION)
  363. print()
  364. sys.exit()