movewin.py 9.2 KB

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