2010-07-08 20:01:03 +02:00
|
|
|
#!/usr/bin/env python
|
2010-07-04 03:05:16 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2010-09-09 02:35:59 +02:00
|
|
|
"""Routines for detecting and handling poker client windows for MS Windows.
|
2009-05-21 17:13:39 +02:00
|
|
|
"""
|
2010-09-09 02:35:59 +02:00
|
|
|
# Copyright 2008 - 2010, Ray E. Barker
|
2009-05-21 17:13:39 +02:00
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
2009-11-23 16:24:38 +01:00
|
|
|
#
|
2009-05-21 17:13:39 +02:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2009-11-23 16:24:38 +01:00
|
|
|
#
|
2009-05-21 17:13:39 +02:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
|
2010-09-23 08:31:16 +02:00
|
|
|
import L10n
|
|
|
|
_ = L10n.get_translation()
|
|
|
|
|
2009-05-21 17:13:39 +02:00
|
|
|
# Standard Library modules
|
|
|
|
import re
|
|
|
|
|
2010-02-06 19:59:33 +01:00
|
|
|
import logging
|
|
|
|
# logging has been set up in fpdb.py or HUD_main.py, use their settings:
|
|
|
|
log = logging.getLogger("hud")
|
|
|
|
|
2009-05-21 17:13:39 +02:00
|
|
|
# pyGTK modules
|
|
|
|
import pygtk
|
|
|
|
import gtk
|
|
|
|
|
|
|
|
# Other Library modules
|
|
|
|
import win32gui
|
|
|
|
import win32process
|
|
|
|
import win32api
|
|
|
|
import win32con
|
|
|
|
import win32security
|
|
|
|
|
|
|
|
# FreePokerTools modules
|
|
|
|
from TableWindow import Table_Window
|
|
|
|
|
|
|
|
# We don't know the border width or title bar height
|
|
|
|
# so we guess here. We can probably get these from a windows call.
|
|
|
|
b_width = 3
|
|
|
|
tb_height = 29
|
|
|
|
|
2010-10-12 00:22:55 +02:00
|
|
|
|
2009-05-21 17:13:39 +02:00
|
|
|
class Table(Table_Window):
|
|
|
|
|
2010-09-09 02:35:59 +02:00
|
|
|
def find_table_parameters(self):
|
2009-05-21 17:13:39 +02:00
|
|
|
"""Finds poker client window with the given table name."""
|
|
|
|
titles = {}
|
|
|
|
win32gui.EnumWindows(win_enum_handler, titles)
|
2011-03-06 12:08:08 +01:00
|
|
|
for hwnd in titles:
|
2010-10-12 00:22:55 +02:00
|
|
|
if titles[hwnd] == "":
|
|
|
|
continue
|
2010-11-12 17:02:21 +01:00
|
|
|
if re.search(self.search_string, titles[hwnd], re.I):
|
2010-10-12 00:22:55 +02:00
|
|
|
if self.check_bad_words(titles[hwnd]):
|
|
|
|
continue
|
2011-03-06 12:08:08 +01:00
|
|
|
# if window not visible, probably not a table
|
|
|
|
if not win32gui.IsWindowVisible(hwnd):
|
2011-03-04 14:46:41 +01:00
|
|
|
continue
|
2011-03-06 12:08:08 +01:00
|
|
|
# if window is a child of another window, probably not a table
|
|
|
|
if win32gui.GetParent(hwnd) != 0:
|
2011-03-04 14:46:41 +01:00
|
|
|
continue
|
|
|
|
HasNoOwner = win32gui.GetWindow(hwnd, win32con.GW_OWNER) == 0
|
|
|
|
WindowStyle = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
|
|
|
|
if HasNoOwner and WindowStyle & win32con.WS_EX_TOOLWINDOW != 0:
|
|
|
|
continue
|
|
|
|
if not HasNoOwner and WindowStyle & win32con.WS_EX_APPWINDOW == 0:
|
|
|
|
continue
|
2011-03-06 12:08:08 +01:00
|
|
|
|
2009-05-21 17:13:39 +02:00
|
|
|
self.window = hwnd
|
|
|
|
break
|
2009-11-23 16:24:38 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
if self.window == None:
|
2010-10-24 22:01:40 +02:00
|
|
|
log.error(_("Window %s not found. Skipping." % self.search_string))
|
2009-11-23 16:24:38 +01:00
|
|
|
return None
|
|
|
|
except AttributeError:
|
2010-08-15 07:38:44 +02:00
|
|
|
log.error(_("self.window doesn't exist? why?"))
|
2009-05-21 17:13:39 +02:00
|
|
|
return None
|
|
|
|
|
2010-10-12 00:22:55 +02:00
|
|
|
self.title = titles[hwnd]
|
|
|
|
self.hud = None
|
2009-06-09 22:38:26 +02:00
|
|
|
self.number = hwnd
|
2010-12-11 16:42:04 +01:00
|
|
|
if self.gdkhandle is not None:
|
|
|
|
try: # Windows likes this here - Linux doesn't
|
|
|
|
self.gdkhandle = gtk.gdk.window_foreign_new(self.number)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2009-05-21 17:13:39 +02:00
|
|
|
|
|
|
|
def get_geometry(self):
|
|
|
|
try:
|
2010-10-12 00:22:55 +02:00
|
|
|
if win32gui.IsWindow(self.number):
|
|
|
|
(x, y, width, height) = win32gui.GetWindowRect(self.number)
|
|
|
|
# this apparently returns x = far left side of window, width = far right side of window, y = top of window, height = bottom of window
|
|
|
|
# so apparently we have to subtract x from "width" to get actual width, and y from "height" to get actual height ?
|
|
|
|
# it definitely gives slightly different results than the GTK code that does the same thing.
|
|
|
|
#print "x=", x, "y=", y, "width=", width, "height=", height
|
|
|
|
width = width - x
|
|
|
|
height = height - y
|
|
|
|
return {
|
|
|
|
'x' : int(x) + b_width,
|
2009-05-21 17:13:39 +02:00
|
|
|
'y' : int(y) + tb_height,
|
2010-09-17 02:29:58 +02:00
|
|
|
'height' : int(height) - y,
|
|
|
|
'width' : int(width) - x
|
2010-10-12 00:22:55 +02:00
|
|
|
}
|
|
|
|
except AttributeError:
|
2009-05-21 17:13:39 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
def get_window_title(self):
|
2010-10-12 00:40:58 +02:00
|
|
|
try: # after window is destroyed, self.window = attribute error
|
|
|
|
return win32gui.GetWindowText(self.window)
|
|
|
|
except AttributeError:
|
|
|
|
return ""
|
2009-05-21 17:13:39 +02:00
|
|
|
|
2010-09-17 02:29:58 +02:00
|
|
|
# def get_nt_exe(self, hwnd):
|
|
|
|
# """Finds the name of the executable that the given window handle belongs to."""
|
|
|
|
#
|
|
|
|
# # Request privileges to enable "debug process", so we can later use PROCESS_VM_READ, retardedly required to GetModuleFileNameEx()
|
|
|
|
# priv_flags = win32security.TOKEN_ADJUST_PRIVILEGES | win32security.TOKEN_QUERY
|
|
|
|
# hToken = win32security.OpenProcessToken (win32api.GetCurrentProcess(), priv_flags)
|
|
|
|
# # enable "debug process"
|
|
|
|
# privilege_id = win32security.LookupPrivilegeValue (None, win32security.SE_DEBUG_NAME)
|
|
|
|
# old_privs = win32security.AdjustTokenPrivileges (hToken, 0, [(privilege_id, win32security.SE_PRIVILEGE_ENABLED)])
|
|
|
|
#
|
|
|
|
# # Open the process, and query it's filename
|
|
|
|
# processid = win32process.GetWindowThreadProcessId(hwnd)
|
|
|
|
# pshandle = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_VM_READ, False, processid[1])
|
|
|
|
# exename = win32process.GetModuleFileNameEx(pshandle, 0)
|
|
|
|
#
|
|
|
|
# # clean up
|
|
|
|
# win32api.CloseHandle(pshandle)
|
|
|
|
# win32api.CloseHandle(hToken)
|
|
|
|
#
|
|
|
|
# return exename
|
2009-11-23 16:24:38 +01:00
|
|
|
|
2009-06-09 22:38:26 +02:00
|
|
|
def topify(self, hud):
|
|
|
|
"""Set the specified gtk window to stayontop in MS Windows."""
|
2009-11-23 16:24:38 +01:00
|
|
|
|
2009-06-10 19:47:07 +02:00
|
|
|
# def windowEnumerationHandler(hwnd, resultList):
|
|
|
|
# '''Callback for win32gui.EnumWindows() to generate list of window handles.'''
|
|
|
|
# resultList.append((hwnd, win32gui.GetWindowText(hwnd)))
|
2009-11-23 16:24:38 +01:00
|
|
|
#
|
2009-06-10 19:47:07 +02:00
|
|
|
# unique_name = 'unique name for finding this window'
|
|
|
|
# real_name = hud.main_window.get_title()
|
|
|
|
# hud.main_window.set_title(unique_name)
|
|
|
|
# tl_windows = []
|
|
|
|
# win32gui.EnumWindows(windowEnumerationHandler, tl_windows)
|
2009-11-23 16:24:38 +01:00
|
|
|
#
|
2009-06-10 19:47:07 +02:00
|
|
|
# for w in tl_windows:
|
|
|
|
# if w[1] == unique_name:
|
2009-06-10 18:58:14 +02:00
|
|
|
# hud.main_window.gdkhandle = gtk.gdk.window_foreign_new(w[0])
|
2009-06-10 19:47:07 +02:00
|
|
|
hud.main_window.gdkhandle = hud.main_window.window
|
|
|
|
hud.main_window.gdkhandle.set_transient_for(self.gdkhandle)
|
2010-09-17 02:29:58 +02:00
|
|
|
# rect = self.gdkhandle.get_frame_extents()
|
|
|
|
# (innerx, innery) = self.gdkhandle.get_origin()
|
|
|
|
# b_width = rect.x - innerx
|
|
|
|
# tb_height = rect.y - innery
|
2009-11-23 16:24:38 +01:00
|
|
|
#
|
2009-06-09 22:38:26 +02:00
|
|
|
# style = win32gui.GetWindowLong(self.number, win32con.GWL_EXSTYLE)
|
|
|
|
# style |= win32con.WS_CLIPCHILDREN
|
|
|
|
# win32gui.SetWindowLong(self.number, win32con.GWL_EXSTYLE, style)
|
2009-06-10 19:47:07 +02:00
|
|
|
# break
|
2009-11-23 16:24:38 +01:00
|
|
|
|
2009-06-10 19:47:07 +02:00
|
|
|
# hud.main_window.set_title(real_name)
|
2009-06-09 22:38:26 +02:00
|
|
|
|
2010-10-12 00:22:55 +02:00
|
|
|
|
2009-05-21 17:13:39 +02:00
|
|
|
def win_enum_handler(hwnd, titles):
|
|
|
|
titles[hwnd] = win32gui.GetWindowText(hwnd)
|