movewin.py 11 KB

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