Expanded my entry in contributors.txt.
[fpdb-dooglus.git] / pyfpdb / WinTables.py
blob5a89857e14e3cd609128fb9aced8dc0e7afacf10
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 """Routines for detecting and handling poker client windows for MS Windows.
4 """
5 # Copyright 2008 - 2010, Ray E. Barker
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 ########################################################################
23 import L10n
24 _ = L10n.get_translation()
26 # Standard Library modules
27 import re
29 import logging
30 # logging has been set up in fpdb.py or HUD_main.py, use their settings:
31 log = logging.getLogger("hud")
33 # pyGTK modules
34 import pygtk
35 import gtk
37 # Other Library modules
38 import win32gui
39 import win32process
40 import win32api
41 import win32con
42 import win32security
44 # FreePokerTools modules
45 from TableWindow import Table_Window
47 # We don't know the border width or title bar height
48 # so we guess here. We can probably get these from a windows call.
49 b_width = 3
50 tb_height = 29
53 class Table(Table_Window):
55 def find_table_parameters(self):
56 """Finds poker client window with the given table name."""
57 titles = {}
58 win32gui.EnumWindows(win_enum_handler, titles)
59 for hwnd in titles:
60 if titles[hwnd] == "":
61 continue
62 if re.search(self.search_string, titles[hwnd], re.I):
63 if self.check_bad_words(titles[hwnd]):
64 continue
65 # if window not visible, probably not a table
66 if not win32gui.IsWindowVisible(hwnd):
67 continue
68 # if window is a child of another window, probably not a table
69 if win32gui.GetParent(hwnd) != 0:
70 continue
71 HasNoOwner = win32gui.GetWindow(hwnd, win32con.GW_OWNER) == 0
72 WindowStyle = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
73 if HasNoOwner and WindowStyle & win32con.WS_EX_TOOLWINDOW != 0:
74 continue
75 if not HasNoOwner and WindowStyle & win32con.WS_EX_APPWINDOW == 0:
76 continue
78 self.window = hwnd
79 break
81 try:
82 if self.window == None:
83 log.error(_("Window %s not found. Skipping.") % self.search_string)
84 return None
85 except AttributeError:
86 log.error(_("self.window doesn't exist? why?"))
87 return None
89 self.title = titles[hwnd]
90 self.hud = None
91 self.number = hwnd
92 if self.gdkhandle is not None:
93 try: # Windows likes this here - Linux doesn't
94 self.gdkhandle = gtk.gdk.window_foreign_new(self.number)
95 except AttributeError:
96 pass
98 def get_geometry(self):
99 try:
100 if win32gui.IsWindow(self.number):
101 (x, y, width, height) = win32gui.GetWindowRect(self.number)
102 # this apparently returns x = far left side of window, width = far right side of window, y = top of window, height = bottom of window
103 # so apparently we have to subtract x from "width" to get actual width, and y from "height" to get actual height ?
104 # it definitely gives slightly different results than the GTK code that does the same thing.
105 #print "x=", x, "y=", y, "width=", width, "height=", height
106 width = width - x
107 height = height - y
108 return {
109 'x' : int(x) + b_width,
110 'y' : int(y) + tb_height,
111 'height' : int(height) - y,
112 'width' : int(width) - x
114 except AttributeError:
115 return None
117 def get_window_title(self):
118 try: # after window is destroyed, self.window = attribute error
119 return win32gui.GetWindowText(self.window)
120 except AttributeError:
121 return ""
123 # def get_nt_exe(self, hwnd):
124 # """Finds the name of the executable that the given window handle belongs to."""
126 # # Request privileges to enable "debug process", so we can later use PROCESS_VM_READ, retardedly required to GetModuleFileNameEx()
127 # priv_flags = win32security.TOKEN_ADJUST_PRIVILEGES | win32security.TOKEN_QUERY
128 # hToken = win32security.OpenProcessToken (win32api.GetCurrentProcess(), priv_flags)
129 # # enable "debug process"
130 # privilege_id = win32security.LookupPrivilegeValue (None, win32security.SE_DEBUG_NAME)
131 # old_privs = win32security.AdjustTokenPrivileges (hToken, 0, [(privilege_id, win32security.SE_PRIVILEGE_ENABLED)])
133 # # Open the process, and query it's filename
134 # processid = win32process.GetWindowThreadProcessId(hwnd)
135 # pshandle = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_VM_READ, False, processid[1])
136 # exename = win32process.GetModuleFileNameEx(pshandle, 0)
138 # # clean up
139 # win32api.CloseHandle(pshandle)
140 # win32api.CloseHandle(hToken)
142 # return exename
144 def topify(self, hud):
145 """Set the specified gtk window to stayontop in MS Windows."""
147 # def windowEnumerationHandler(hwnd, resultList):
148 # '''Callback for win32gui.EnumWindows() to generate list of window handles.'''
149 # resultList.append((hwnd, win32gui.GetWindowText(hwnd)))
151 # unique_name = 'unique name for finding this window'
152 # real_name = hud.main_window.get_title()
153 # hud.main_window.set_title(unique_name)
154 # tl_windows = []
155 # win32gui.EnumWindows(windowEnumerationHandler, tl_windows)
157 # for w in tl_windows:
158 # if w[1] == unique_name:
159 # hud.main_window.gdkhandle = gtk.gdk.window_foreign_new(w[0])
160 hud.main_window.gdkhandle = hud.main_window.window
161 hud.main_window.gdkhandle.set_transient_for(self.gdkhandle)
162 # rect = self.gdkhandle.get_frame_extents()
163 # (innerx, innery) = self.gdkhandle.get_origin()
164 # b_width = rect.x - innerx
165 # tb_height = rect.y - innery
167 # style = win32gui.GetWindowLong(self.number, win32con.GWL_EXSTYLE)
168 # style |= win32con.WS_CLIPCHILDREN
169 # win32gui.SetWindowLong(self.number, win32con.GWL_EXSTYLE, style)
170 # break
172 # hud.main_window.set_title(real_name)
175 def win_enum_handler(hwnd, titles):
176 titles[hwnd] = win32gui.GetWindowText(hwnd)