movewin.py 10 KB

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