Carbon Poker offers a 5 card stud game that wasn't listed here. It's not available...
[fpdb-dooglus.git] / pyfpdb / XTables.py
blob9813e118237ed239b2ad85674a515e1e07e9ee01
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 """XWindows specific methods for TableWindows Class.
4 """
5 # Copyright 2008 - 2011, 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
28 import os
30 # pyGTK modules
31 import gtk
33 # Other Library modules
34 import Xlib.display
36 # FPDB modules
37 from TableWindow import Table_Window
38 import Configuration
40 # We might as well do this once and make them globals
41 disp = Xlib.display.Display()
42 root = disp.screen().root
44 c = Configuration.Config()
45 log = Configuration.get_logger("logging.conf", "hud", log_dir=c.dir_log, log_file='HUD-log.txt')
47 class Table(Table_Window):
49 def find_table_parameters(self):
51 # This is called by __init__(). Find the poker table window of interest,
52 # given the self.search_string. Then populate self.number, self.title,
53 # self.window, and self.parent (if required).
55 reg = '''
56 \s+(?P<XID>[\dxabcdef]+) # XID in hex
57 \s(?P<TITLE>.+): # window title
58 '''
60 self.number = None
61 for listing in os.popen('xwininfo -root -tree').readlines():
62 if re.search(self.search_string, listing, re.I):
63 log.info(listing)
64 mo = re.match(reg, listing, re.VERBOSE)
65 title = re.sub('\"', '', mo.groupdict()["TITLE"])
66 if self.check_bad_words(title): continue
67 self.number = int( mo.groupdict()["XID"], 0 )
68 self.title = title
69 if self.number is None:
70 log.warning(_("Could not retrieve XID from table xwininfo. xwininfo is %s") % listing)
71 break
73 if self.number is None:
74 log.warning(_("No match in XTables for table '%s'.") % self.search_string)
75 return None
76 (self.window, self.parent) = self.get_window_from_xid(self.number)
78 # def get_window_from_xid(self, id):
79 # for outside in root.query_tree().children:
80 # if outside.id == id:
81 # return (outside, outside.query_tree().parent)
82 # for inside in outside.query_tree().children:
83 # if inside.id == id: # GNOME, Xfce
84 # return (inside, inside.query_tree().parent)
85 # for wayinside in inside.query_tree().children:
86 # if wayinside.id == id: # KDE
87 # parent = wayinside.query_tree().parent
88 # return (wayinside, parent.query_tree().parent)
89 # return (None, None)
91 def get_window_from_xid(self, id):
92 for top_level in root.query_tree().children:
93 if top_level.id == id:
94 return (top_level, None)
95 for w in treewalk(top_level):
96 if w.id == id:
97 return (w, top_level)
99 #def get_geometry(self):
100 #try:
101 #my_geo = self.window.get_geometry()
102 #if self.parent is None:
103 #return {'x' : my_geo.x,
104 #'y' : my_geo.y,
105 #'width' : my_geo.width,
106 #'height' : my_geo.height
108 #else:
109 #pa_geo = self.parent.get_geometry()
110 #return {'x' : my_geo.x + pa_geo.x,
111 #'y' : my_geo.y + pa_geo.y,
112 #'width' : my_geo.width,
113 #'height' : my_geo.height
115 #except:
116 #return None
118 def get_geometry(self):
119 geo_re = '''
120 Absolute\supper-left\sX: \s+ (?P<X>\d+) # x
122 Absolute\supper-left\sY: \s+ (?P<Y>\d+) # y
124 Width: \s+ (?P<W>\d+) # width
126 Height: \s+ (?P<H>\d+) # height
128 des_re = 'No such window with id'
130 listing = os.popen("xwininfo -id %d -stats" % (self.number)).read()
131 if listing == "": return
132 mo = re.search(des_re, listing)
133 if mo is not None:
134 return None # table has been destroyed
136 mo = re.search(geo_re, listing, re.VERBOSE|re.DOTALL)
137 try:
138 return {'x' : int(mo.groupdict()['X']),
139 'y' : int(mo.groupdict()['Y']),
140 'width' : int(mo.groupdict()['W']),
141 'height' : int(mo.groupdict()['H'])
143 except AttributeError:
144 return None
146 def get_window_title(self):
147 s = os.popen("xwininfo -wm -id %d" % self.number).read()
148 mo = re.search('"(.+)"', s)
149 try:
150 return mo.group(1)
151 except AttributeError:
152 return None
154 def topify(self, window):
155 # The idea here is to call set_transient_for on the HUD window, with the table window
156 # as the argument. This should keep the HUD window on top of the table window, as if
157 # the hud window was a dialog belonging to the table.
159 # This is the gdkhandle for the HUD window
160 gdkwindow = gtk.gdk.window_foreign_new(window.window.xid)
161 gdkwindow.set_transient_for(self.gdkhandle)
163 def treewalk(parent):
164 for w in parent.query_tree().children:
165 for ww in treewalk(w):
166 yield ww
167 yield w