Guest User

Untitled

a guest
Dec 11th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.28 KB | None | 0 0
  1. # To change this template, choose Tools | Templates
  2. # and open the template in the editor.
  3.  
  4. __author__ = "Bob"
  5. __date__ = "$Apr 30, 2011 11:03:09 AM$"
  6. import os
  7.  
  8. import ctypes
  9. from ctypes import wintypes
  10.  
  11. import _thread
  12. import time
  13.  
  14. import win32gui
  15.  
  16. import hotkeyhelper
  17. import windowhelper
  18.  
  19. from tiler import Tiler
  20.  
  21. from win32con import GW_OWNER
  22. from win32con import MOD_ALT
  23. from win32con import MOD_SHIFT
  24. from win32con import WM_CLOSE
  25. from win32con import WM_HOTKEY
  26. from win32con import SW_SHOWNORMAL
  27. from win32con import SW_MINIMIZE
  28.  
  29. def callback (window, resultList):
  30.  
  31. #if the window is visible
  32. if win32gui.IsWindowVisible(window):
  33. #if the window doesn't have an owner(pop-ups)
  34. if not win32gui.GetWindow(window, GW_OWNER):
  35. #if the window command == show
  36. if win32gui.GetWindowPlacement(window)[1] == SW_SHOWNORMAL:
  37. #if the class is not empty
  38. if win32gui.GetClassName(window):
  39. #grab window
  40. resultList.append(window)
  41. return True
  42.  
  43. def validate_ignorelist(window):
  44.  
  45. ignore = (#Windows classnames that I like tiled
  46. #Keep this list to a minimum! the smaller it is the better the performance
  47. "Progman" #windows shell
  48. , "ComboLBox" #The class for the list box contained in a combo box.
  49. , "DDEMLEvent" #The class for Dynamic Data Exchange Management Library (DDEML) events.
  50. , "Message" #The class for a message-only window.
  51. , "#32768" #The class for a menu.
  52. , "#32769" #The class for the desktop window.
  53. , "#32770" #The class for a dialog box.
  54. , "#32771" #The class for the task switch window.
  55. , "#32772" #The class for icon titles.
  56. , "#32774" #The class for titles on close in command prompts.
  57.  
  58. , "Button" #The class for a button.
  59. , "ComboBox" #he class for a combo box.
  60. , "Edit" #The class for an edit control.
  61. , "ListBox" #The class for a list box.
  62. , "MDIClient" #The class for an MDI client window.
  63. , "ScrollBar" #The class for a scroll bar.
  64. , "Static" #The class for a static control.
  65.  
  66. , "ClockTooltipWindow" #the clock tooltip
  67. , "tooltips_class32" #tooltip in explorer
  68.  
  69. , "DV2ControlHost" #start menu
  70. , "BaseBar" #shutdown options
  71. , "SynTrackCursorWindowClass" #no idea
  72. , "Shell_TrayWnd" #console extra handler
  73. , "TaskListThumbnailWnd" #windows 7 thumbnails
  74.  
  75. , "WorkerW" #popped up after opening powerpoint?
  76. , "screenClass" #powerpoint fullscreen
  77.  
  78. , "gdkWindowTempShadow" #pidgin contact details
  79. , "gdkWindowTemp" #pidgin contact details
  80.  
  81. , "ShockwaveFlashFullScreen" #fullscreen flash (video)
  82.  
  83. , "SynShapedFrameWindowClass" #firefox scrolling, god knows why
  84. , "MozillaDropShadowWindowClass"#firefox shadowwindow
  85.  
  86. )
  87.  
  88. if win32gui.GetClassName(window) not in ignore:
  89. return True
  90.  
  91. return False
  92.  
  93.  
  94. def listen_to_hotkeys():
  95.  
  96. byref = ctypes.byref
  97. user32 = ctypes.windll.user32
  98.  
  99. hotkeys = {1: (MOD_ALT, ord("L"))
  100. , 2: (MOD_ALT, ord("H"))
  101. , 3: (MOD_ALT + MOD_SHIFT, ord("J"))
  102. , 4: (MOD_ALT + MOD_SHIFT, ord("K"))
  103. , 5: (MOD_ALT, ord("J"))
  104. , 6: (MOD_ALT, ord("K"))
  105. , 7: (MOD_ALT + MOD_SHIFT, ord("C"))
  106. , 8: (MOD_ALT + MOD_SHIFT, ord("V"))
  107. , 9: (MOD_ALT + MOD_SHIFT, ord("L"))
  108. , 10: (MOD_ALT + MOD_SHIFT, ord("H"))
  109. , 11: (MOD_ALT + MOD_SHIFT, ord("Q"))
  110. }
  111.  
  112. hotkeyhandlers = {1: handler_alt_L
  113. , 2: handler_alt_H
  114. , 3: handler_shift_alt_J
  115. , 4: handler_shift_alt_K
  116. , 5: handler_alt_J
  117. , 6: handler_alt_K
  118. , 7: handler_shift_alt_C
  119. , 8: handler_shift_alt_V
  120. , 9: handler_shift_alt_L
  121. , 10: handler_shift_alt_H
  122. , 11: handler_shift_alt_Q
  123. }
  124.  
  125. try:
  126. #register the hotkeys
  127. hotkeyhelper.register_hotkeys(hotkeys)
  128.  
  129. #define msg
  130. msg = wintypes.MSG()
  131.  
  132. #wait for a message
  133. while user32.GetMessageA(byref(msg), None, 0, 0) != 0:
  134. #check if message is a hotkey and if we have it registered
  135. if msg.message == WM_HOTKEY and msg.wParam in hotkeyhandlers.keys():
  136. #execute the hotkey handler in a different thread (smooth actions when hotkey is rapidly pressed)
  137. _thread.start_new_thread(hotkeyhandlers.get(msg.wParam), ())
  138.  
  139. #user32.TranslateMessage(byref(msg))
  140. #user32.DispatchMessageA(byref(msg))
  141. finally:
  142. #unregister the hotkeys
  143. hotkeyhelper.unregister_hotkeys(hotkeys)
  144.  
  145. def handler_alt_L():
  146.  
  147. global tiler, threadlock
  148.  
  149. threadlock.acquire()
  150. tiler.masterareaWidth += 100
  151. threadlock.release()
  152. print ("master area += 100")
  153. tiler.tile_windows()
  154.  
  155. def handler_alt_H():
  156.  
  157. global tiler, threadlock
  158.  
  159. threadlock.acquire()
  160. tiler.masterareaWidth -= 100
  161. threadlock.release()
  162. print ("master area -= 100")
  163. tiler.tile_windows()
  164.  
  165. def handler_shift_alt_J():
  166.  
  167. global tiler, threadlock
  168.  
  169. #only grab and move the window if it is in the tiler
  170. if win32gui.GetForegroundWindow() in tiler.windows:
  171. i = tiler.windows.index(windowhelper.get_focused_window())
  172. if i >= len(tiler.windows) - 1:
  173. threadlock.acquire()
  174. tiler.windows[0], tiler.windows[1:] = tiler.windows[i], tiler.windows[:i]
  175. threadlock.release()
  176. else:
  177. threadlock.acquire()
  178. tiler.windows[i], tiler.windows[i + 1] = tiler.windows[i + 1], tiler.windows[i]
  179. threadlock.release()
  180.  
  181. print ("change order down")
  182. tiler.tile_windows()
  183.  
  184. def handler_shift_alt_K():
  185.  
  186. global tiler, threadlock
  187.  
  188. #only grab and move the window if it is in the tiler
  189. if win32gui.GetForegroundWindow() in tiler.windows:
  190. i = tiler.windows.index(windowhelper.get_focused_window())
  191. if i <= 0:
  192. threadlock.acquire()
  193. j = len(tiler.windows) - 1
  194. tiler.windows[j], tiler.windows[:j] = tiler.windows[0], tiler.windows[1:]
  195. threadlock.release()
  196. else:
  197. threadlock.acquire()
  198. j = i - 1
  199. tiler.windows[i], tiler.windows[j] = tiler.windows[j], tiler.windows[i]
  200. threadlock.release()
  201.  
  202. print ("change order up")
  203. tiler.tile_windows()
  204.  
  205. def handler_alt_J():
  206.  
  207. global tiler
  208.  
  209. #only grab and move the focus if it is in the tiler
  210. if win32gui.GetForegroundWindow() in tiler.windows:
  211. i = tiler.windows.index(windowhelper.get_focused_window()) + 1
  212. if i >= len(tiler.windows):
  213. i = 0
  214.  
  215. windowhelper.focus(tiler.windows[i])
  216. windowhelper.set_cursor_window(tiler.windows[i])
  217.  
  218. print ("change focus down")
  219.  
  220. def handler_alt_K():
  221.  
  222. global tiler
  223.  
  224. #only grab and move the focus if it is in the tiler
  225. if win32gui.GetForegroundWindow() in tiler.windows:
  226. i = tiler.windows.index(windowhelper.get_focused_window()) - 1
  227. if i < 0:
  228. i = len(tiler.windows) - 1
  229.  
  230. windowhelper.focus(tiler.windows[i])
  231. windowhelper.set_cursor_window(tiler.windows[i])
  232.  
  233. print ("change focus up")
  234.  
  235. def handler_shift_alt_C():
  236.  
  237. global tiler, threadlock, oldAmount
  238.  
  239. window = windowhelper.get_focused_window()
  240.  
  241. #only remove the window from the tiler if it is in the tiler
  242. if win32gui.GetForegroundWindow() in tiler.windows:
  243. threadlock.acquire()
  244. tiler.windows.remove(window)
  245. threadlock.release()
  246.  
  247. win32gui.SendMessage(window, WM_CLOSE, 0, 0)
  248.  
  249. threadlock.acquire()
  250. oldAmount -= 1
  251. threadlock.release()
  252.  
  253. tiler.tile_windows()
  254.  
  255. def handler_shift_alt_V():
  256.  
  257. global tiler, threadlock, oldAmount
  258.  
  259. window = windowhelper.get_focused_window()
  260.  
  261. #only remove the window from the tiler if it is in the tiler
  262. if win32gui.GetForegroundWindow() in tiler.windows:
  263.  
  264. threadlock.acquire()
  265. tiler.windows.remove(window)
  266. threadlock.release()
  267.  
  268. win32gui.ShowWindow(window, SW_MINIMIZE)
  269.  
  270. threadlock.acquire()
  271. oldAmount -= 1
  272. threadlock.release()
  273.  
  274. tiler.tile_windows()
  275.  
  276. def handler_shift_alt_L():
  277.  
  278. global tiler, threadlock
  279.  
  280. if tiler.masterareaSize > 1:
  281. threadlock.acquire()
  282. tiler.masterareaSize -= 1
  283. threadlock.release()
  284.  
  285. print ("masterarea size -= 1")
  286. tiler.tile_windows()
  287.  
  288. def handler_shift_alt_H():
  289.  
  290. global tiler, threadlock
  291.  
  292. if tiler.masterareaSize < len(tiler.windows):
  293. threadlock.acquire()
  294. tiler.masterareaSize += 1
  295. threadlock.release()
  296.  
  297. print ("masterarea size += 1")
  298. tiler.tile_windows()
  299.  
  300. def handler_shift_alt_Q():
  301.  
  302. os._exit(0)
  303.  
  304. if __name__ == "__main__":
  305.  
  306. #initialization
  307. threadlock = _thread.allocate_lock()
  308.  
  309. windows = []
  310. tiler = Tiler()
  311.  
  312. #hotkeys
  313. _thread.start_new_thread(listen_to_hotkeys, ())
  314.  
  315. #initial tile
  316. win32gui.EnumWindows(callback, windows)
  317.  
  318. for window in windows:
  319. if validate_ignorelist(window):
  320. tiler.windows.append(window)
  321.  
  322. #tile
  323. tiler.tile_windows()
  324.  
  325. for window in tiler.windows:
  326. print (win32gui.GetClassName(window))
  327.  
  328. #set initial control values
  329. oldAmount = len(windows)
  330. oldTiledWindows = set(tiler.windows)
  331.  
  332. while True:
  333. time.sleep(0.1)
  334.  
  335. windows = []
  336. reTile = False
  337.  
  338. win32gui.EnumWindows(callback, windows)
  339.  
  340. if len(windows) > oldAmount:
  341. for window in windows:
  342. if window not in tiler.windows:
  343. if validate_ignorelist(window):
  344. tiler.windows.append(window)
  345. reTile = True
  346. print ("Add handle: ", window, win32gui.GetClassName(window))
  347.  
  348. elif oldAmount > len(windows):
  349. for window in (oldTiledWindows - set(windows)):
  350. if window in tiler.windows:
  351. tiler.windows.remove(window)
  352. reTile = True
  353. print ("Remove handle: ", window)
  354.  
  355. if reTile:
  356. _thread.start_new_thread(tiler.tile_windows, ())
  357. oldAmount = len(windows)
  358. oldTiledWindows = set(tiler.windows)
Add Comment
Please, Sign In to add comment