This file is indexed.

/usr/share/pyshared/plwm/wmanager.py is in python-plwm 2.6a+20080530-1.1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
#
# wmanager.py -- core window manager functionality
#
#    Copyright (C) 1999-2002  Peter Liljenberg <petli@ctrl-c.liu.se>
#
#    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.
#
#    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.
#
#    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


import sys
import os
import signal
import errno
import array
import traceback
import re
import string
import types

from Xlib import display, X, Xutil, Xatom, rdb, error
import Xlib.protocol.event

import plwm
import event
import wmevents
import cfilter

# Minimum Xlib version
required_xlib_version = (0, 8)

# Errors
class UnavailableScreenError(Exception): pass
class NoUnmanagedScreensError(Exception): pass
class BadXlibVersion(Exception): pass


# Debugging

debug_file = sys.stderr
debug_what = []

def do_debug(what, fmt, *args):
    if not debug_what or what in debug_what:
        debug_file.write(what + ': ' + (fmt % args) + '\n')

def dont_debug(*args):
    pass

debug = dont_debug


class Window:
    """Class representing any window object on a screen, either internal
    or client windows.
    """

    full_screen_windows = cfilter.none

    def __init__(self, screen, window):
        "Create a window object managing WINDOW."

        self.screen = screen
        self.window = window
        self.wm = screen.wm        # So we can pass Windows to KeyHandlers.

        self.withdrawn = 0
        self.delayed_moveresize = 0

        self.current = 0
        self.focused = 0

        self.force_iconified = 0

        self.dispatch = event.WindowDispatcher(self.window)

        # Initial mapped state and geometry
        a = window.get_attributes()
        self.mapped = a.map_state != X.IsUnmapped

        r = self.window.get_geometry()
        self.x = r.x
        self.y = r.y
        self.width = r.width
        self.height = r.height
        self.border_width = r.border_width


    def __str__(self):
        return '<%s %s>' % (self.__class__, self.window)

    def __repr__(self):
        return self.__str__()

    #
    # Internal methods
    #

    def withdraw(self, destroyed = 0):
        """Window has been withdrawn.
        If DESTROYED is true the window doesn't exist anymore."""

        if self.withdrawn:
            return

        self.mapped = 0
        self.withdrawn = 1

        # Clear the dispatcher to avoid cirkular references
        self.dispatch = event.SlaveDispatcher([])


    def handle_event(self, event, grabbed = 0):
        """Handle EVENT for this client by calling its dispatch table.
        """
        self.dispatch.handle_event(event, grabbed)


    def get_focus(self, time):
        debug('focus', 'client gets focus: %s', self)
        self.focused = 1
        self.window.set_input_focus(X.RevertToPointerRoot, time)

    def lose_focus(self):
        debug('focus', 'client loses focus: %s', self)
        self.focused = 0

    #
    # External methods
    #

    def configure(self, **keys):
        if self.withdrawn:
            return

        for i in ['x', 'y', 'width', 'height', 'border_width']:
            setattr(self, i, keys.get(i, getattr(self, i)))

        apply(self.window.configure, (), keys)


    def is_mapped(self):
        return self.mapped

    def valid_window(self):
        """Return true if the window still exists.
        """
        if self.withdrawn:
            return 0

        # Check for an invalid window by trigging BadDrawable
        # on a simple request
        try:
            r = self.window.get_geometry()
        except error.BadDrawable:
            return 0
        else:
            return 1

    def resize(self, width, height):
        if self.withdrawn:
            return
        self.window.configure(width = width, height = height)
        self.width = width
        self.height = height


    def move(self, x, y):
        if self.withdrawn:
            return
        self.window.configure(x = x, y = y)
        self.x = x
        self.y = y


    def moveresize(self, x, y, width, height, delayed = 0):
        if self.withdrawn:
            return

        # If client is iconified and delayed is true, don't actually
        # resize the window now but postpone it until deiconifying
        if self.mapped or not delayed:
            self.window.configure(x = x, y = y, width = width, height = height)
            self.delayed_moveresize = 0
        else:
            self.delayed_moveresize = 1

        self.x = x
        self.y = y
        self.width = width
        self.height = height


    def setborderwidth(self, width):
        if self.withdrawn:
            return
        self.window.configure(border_width = width)
        self.border_width = width


    def set_border_color(self, color):
        if self.withdrawn:
            return

        self.window.change_attributes(border_pixel = color)


    def keep_on_screen(self, x, y, width, height):
        """Return X, Y, WIDTH, HEIGHT after adjusting so the entire window
        is visible on the screen, including the border.
        """

        if self.full_screen_windows(self):
            root_x = 0
            root_y = 0
            root_width = self.screen.root_full_width
            root_height = self.screen.root_full_height
        else:
            root_x = self.screen.root_x
            root_y = self.screen.root_y
            root_width = self.screen.root_width
            root_height = self.screen.root_height

        # negative sizes is impossible
        if width < 0:
            width = 0
        if height < 0:
            height = 0

        # width and height must not be larger than the screen area
        if width + 2 * self.border_width > root_width:
            width = root_width - 2 * self.border_width
        if height + 2 * self.border_width > root_height:
            height = root_height - 2 * self.border_width

        # Move window if right/bottom edge is outside screen
        if (x + width + 2 * self.border_width
            > root_x + root_width):
            x = (root_x + root_width
                 - width - 2 * self.border_width)

        if (y + height + 2 * self.border_width
            > root_y + root_height):
            y = (root_y + root_height
                 - height - 2 * self.border_width)

        # Move window if left/top edge is outside screen
        if x < root_x:
            x = root_x
        if y < root_y:
            y = root_y

        return x, y, width, height

    def geometry(self):
        return self.x, self.y, self.width, self.height, self.border_width

    def get_top_edge(self):
        """Return the y coordinate of the top edge of the client."""
        return self.y

    def get_bottom_edge(self):
        """Return the y coordinate of the bottom edge of the client."""
        return self.y + self.height + 2 * self.border_width

    def get_left_edge(self):
        """Return the x coordinate of the left edge of the client."""
        return self.x

    def get_right_edge(self):
        """Return the x coordinate of the right edge of the client."""
        return self.x + self.width + 2 * self.border_width

    def pointer_position(self):
        """Return the pointer x and y position relative to the window
        origin.  Return None if the pointer is on another screen.
        """
        if self.withdrawn:
            return None

        # Handle destroyed windows gracefully, registering the error
        # so the window is withdrawn from managing.

        try:
            r = self.window.query_pointer()
        except error.BadWindow, e:
            self.wm.events.put_event(e)
            return None

        if r.same_screen:
            return r.win_x, r.win_y
        else:
            return None

    #
    # Window ops
    #

    def map(self):
        if self.withdrawn: return None

        # Perform delayed resizing
        if self.delayed_moveresize:
            self.window.configure(x = self.x, y = self.y,
                                  width = self.width,
                                  height = self.height)
            self.delayed_moveresize = 0

        self.window.map()

    def unmap(self):
        if self.withdrawn: return None
        self.window.unmap()

    def clear_area(self, *args, **keys):
        if self.withdrawn: return None
        apply(self.window.clear_area, args, keys)

    def fill_rectangle(self, gc, x, y, width, height, onerror = None):
        if self.withdrawn: return None
        self.window.fill_rectangle(gc, x, y, width, height, onerror)

    def image_text(self, gc, x, y, string, onerror = None):
        if self.withdrawn: return None
        self.window.image_text(gc, x, y, string, onerror)

    def draw_text(self, gc, x, y, text, onerror = None):
        if self.withdrawn: return None
        self.window.draw_text(gc, x, y, text, onerror)

    def convert_selection(self, selection, target, property, time, onerror = None):
        if self.withdrawn: return None
        self.window.convert_selection(selection, target, property, time, onerror)

    def destroy(self):
        if self.withdrawn:
            return
        self.screen.remove_window(self.window)
        self.window.destroy()

    def raisewindow(self):
        if self.withdrawn:
            return
        self.window.configure(stack_mode = X.Above)

    def lowerwindow(self):
        if self.withdrawn:
            return
        self.window.configure(stack_mode = X.Below)

    def raiselower(self):
        if self.withdrawn:
            return
        self.window.configure(stack_mode = X.Opposite)


class InternalWindow(Window):
    pass

class Client(Window):
    "Container for clients of the window manager."

    start_iconified_clients = cfilter.none
    default_pointer_pos = {}
    client_maxsize = {}

    needs_reparent_clients = cfilter.name('AWTapp')

    window_proxy_class = None
    
    def __init__(self, screen, window, maprequest):
        """Create a client object managing WINDOW.

        If MAPPED is true, this client has been created
        because of a MapRequest event.  If false, it was created
        when the window manager started and scanned available
        windows.
        """

        # Let any window proxy in on the fun
        if self.window_proxy_class is not None:
            window = self.window_proxy_class(screen, window)
            
        Window.__init__(self, screen, window)

        # Let proxy register event handlers now that we have a dispatcher 
        if self.window_proxy_class is not None:
            window.__proxy_event_init__(self)

        self.window.change_save_set(X.SetModeInsert)

        self.from_maprequest = maprequest

        # Set up the system event handlers

        self.dispatch.add_system_handler(X.DestroyNotify, self.handle_destroy_notify)
        self.dispatch.add_system_handler(X.UnmapNotify, self.handle_unmap_notify)
        self.dispatch.add_system_handler(X.PropertyNotify, self.handle_property_notify)

        # Fetch WM hints
        self.wmhints = self.window.get_wm_hints()
        self.sizehints = self.window.get_wm_normal_hints()
        self.protocols = self.window.get_wm_protocols()

        hint = self.window.get_wm_class()
        if hint:
            self.res_name, self.res_class = hint
        else:
            self.res_name = self.res_class = None


        # Some broken widget sets (e.g. Java AWT) checks whether a
        # window manager is running, and if so, will do nothing until
        # they receive the ReparentNotify that indicates that their
        # window has now been put into a frame by the wm.  Because
        # PLWM does not have frame windows, this breaks horribly.

        # To cater for AWT and its demented cousins, we reparent the
        # window to be at the same position in the root window.  This
        # is effectively a no-op, but will generate the
        # ReparentNotify.  It can also generates an UnmapNotify, so
        # block that event.

        if self.needs_reparent_clients(self):
            debug('client', 'reparenting incompetent window')
            self.dispatch.block_masks(X.StructureNotifyMask)
            self.screen.dispatch.block_masks(X.SubstructureNotifyMask)

            self.window.reparent(self.screen.root, self.x, self.y)

            self.screen.dispatch.unblock_masks(X.SubstructureNotifyMask)
            self.dispatch.unblock_masks(X.StructureNotifyMask)


        # Detect client focus model

        # Always call set_input_focus, unless the client has set input
        # hint to false

        if self.wmhints is not None \
           and self.wmhints.flags & Xutil.InputHint \
           and not self.wmhints.input:
            self.do_set_focus = 0
        else:
            self.do_set_focus = 1

        # We send message if client supports the WM_TAKE_FOCUS protocol
        if self.wm.WM_TAKE_FOCUS in self.protocols:
            self.do_send_focus_msg = 1

            # However: we always set focus of globally active windows,
            # to avoid having to track FocusIn/FocusOut.
            if not self.do_set_focus:
                self.do_set_focus = 1
        else:
            self.do_send_focus_msg = 0


        # Figure out if we should start iconified or not

        # First: if the window is mapped from a withdrawn state
        # use the WM hints state hint if present

        if maprequest and self.wmhints and self.wmhints.flags & Xutil.StateHint \
           and self.wmhints.initial_state == Xutil.IconicState:
            self.start_iconified = 1

        # Second: start iconified if the client already is iconic
        elif self.get_wm_state() == Xutil.IconicState:
            self.start_iconified = 1

        # Third : start iconified if the clients matches
        # start_iconified_clients
        elif self.start_iconified_clients(self):
            self.start_iconified = 1

        # Otherwise start mapped, although this can be overridden
        else:
            self.start_iconified = 0

        # Now find and call all __client_init__ methods
        call_inits(self.__class__, '__client_init__', self)


    def __del__(self):
        # Just call all __client_del__ methods
        call_inits(self.__class__, '__client_del__', self)

    #
    # Internal methods
    #

    def withdraw(self, destroyed = 0):
        """Window has been withdrawn.
        If DESTROYED is true the window doesn't exist anymore."""

        if self.withdrawn:
            return

        Window.withdraw(self, destroyed)
        if not destroyed:
            self.window.change_save_set(X.SetModeDelete)
            self.window.delete_property(self.wm.WM_STATE)

        # Pass note to proxy, if any, that the window is gone
        if self.window_proxy_class is not None:
            self.window._proxy_withdraw()

        
    def handle_property_notify(self, event):
        """Called when a property has changed on the client window.
        EVENT is a PropertyEvent object.
        """
        if self.withdrawn:
            return

        # The only ICCCM property we should follow is WM_NORMAL_HINTS,
        # as that one can change e.g. when changing font in an Emacs.
        # The other properties should be set before the window is
        # mapped, and mostly affect initial state anyway.

        if event.atom == Xatom.WM_NORMAL_HINTS:

            # Handle destroyed windows gracefully, registering the error
            # so the window is withdrawn from managing.

            try:
                self.sizehints = self.window.get_wm_normal_hints()
            except error.BadWindow, e:
                self.wm.events.put_event(e)

    def handle_destroy_notify(self, event):
        debug('clientnotify', 'Destroy of window %s', event.window)
        self.wm.remove_window(event.window, 1)

    def handle_unmap_notify(self, event):
        debug('clientnotify', 'Unmapping window %s', event.window)
        self.wm.remove_window(event.window)

    def initial_map(self):
        """Called after creating a client to map its window."""

        debug('client', 'start_iconified: %d' % self.start_iconified)
        if self.start_iconified:
            self.iconify()
        else:
            self.deiconify()

    def get_focus(self, time):
        debug('focus', 'client gets focus: %s', self)
        self.focused = 1
        self.wm.events.put_event(wmevents.ClientFocusIn(self))
        if self.do_set_focus:
            self.window.set_input_focus(X.RevertToPointerRoot, time)
        if self.do_send_focus_msg:
            self.send_message(self.wm.WM_TAKE_FOCUS, time)

    def lose_focus(self):
        debug('focus', 'client loses focus: %s', self)
        self.wm.events.put_event(wmevents.ClientFocusOut(self))
        self.focused = 0


    #
    # External methods
    #

    def configure(self, **keys):
        if self.client_maxsize.has_key(self.res_name):
            maxsize = self.client_maxsize[self.res_name]
        elif self.client_maxsize.has_key(self.res_class):
            maxsize = self.client_maxsize[self.res_class]
        else:
            maxsize = None

        if keys.has_key('width') and maxsize:
            keys['width'] = min(keys['width'], maxsize[0])

        if keys.has_key('height') and maxsize:
            keys['height'] = min(keys['height'], maxsize[1])

        apply(Window.configure, (self, ), keys)


    def iconify(self):
        if self.withdrawn:
            return

        debug('client', 'iconify')

        # Prevent us from recieving UnmapNotify events
        self.dispatch.block_masks(X.StructureNotifyMask)
        self.screen.dispatch.block_masks(X.SubstructureNotifyMask)

        self.window.unmap()

        debug('client', 'iconify')
        
        # Prevent us from recieving UnmapNotify events
        self.dispatch.block_masks(X.StructureNotifyMask)
        self.screen.dispatch.block_masks(X.SubstructureNotifyMask)

        self.unmap()

        self.screen.dispatch.unblock_masks(X.SubstructureNotifyMask)
        self.dispatch.unblock_masks(X.StructureNotifyMask)

        self.mapped = 0
        self.window.set_wm_state(state = Xutil.IconicState, icon = 0)
        self.wm.events.put_event(wmevents.ClientIconified(self))


    def deiconify(self):
        if self.withdrawn:
            return

        if self.force_iconified:
            return

        if self.force_iconified:
            return
        
        debug('client', 'deiconify')
        
        self.map()
        self.mapped = 1
        self.window.set_wm_state(state = Xutil.NormalState, icon = 0)
        self.wm.events.put_event(wmevents.ClientDeiconified(self))


    def delete(self, destroy = 0):
        if self.withdrawn:
            return

        if self.wm.WM_DELETE_WINDOW in self.protocols:
            self.send_message(self.wm.WM_DELETE_WINDOW)
        elif destroy:
            self.destroy()

    def activate(self):
        """Make this client active by raising it's window and moving
        the pointer into it.
        """
        if self.withdrawn:
            return

        if not self.mapped:
            self.deiconify()

        ### FIXME:  This really could be done so much prettier to
        ### avoid having to upset the window stacking order.
        self.raisewindow()
        self.warppointer()

        # Explicitly set focus, in case we use a non-pointer-based
        # focus method
        self.wm.set_current_client(self)

    def send_message(self, atom, time = X.CurrentTime, args = []):
        if self.withdrawn:
            return
        if len(args) > 3:
            args = args[:3]
        elif len(args) < 3:
            args = args + [0] * (3 - len(args))

        ev = Xlib.protocol.event.ClientMessage(window = self.window,
                                               client_type = self.wm.WM_PROTOCOLS,
                                               data = (32, ([atom, time] + args)))
        self.window.send_event(ev)


    def resize_increment(self):
        if self.sizehints and self.sizehints.flags & Xutil.PResizeInc:
            return self.sizehints.width_inc, self.sizehints.height_inc
        else:
            return 0, 0

    def base_size(self):
        if self.sizehints and self.sizehints.flags & Xutil.PBaseSize:
            return self.sizehints.base_width, self.sizehints.base_height
        else:
            return 0, 0

    def follow_size_hints(self, width, height):
        w = width
        h = height

        if self.sizehints:
            sh = self.sizehints

            # Fix resize increment stuff
            if sh.flags & Xutil.PResizeInc:

                # Find base size
                if sh.flags & Xutil.PBaseSize:
                    base_width = sh.base_width
                    base_height = sh.base_height

                # If no base size is provided, the minimum size should
                # be used instead, it that is provided
                elif sh.flags & Xutil.PMinSize:
                    base_width = sh.min_width
                    base_height = sh.min_height

                # Else no base size at all
                else:
                    base_width = 0
                    base_height = 0


                w = width - base_width
                h = height - base_height

                wi = w % sh.width_inc
                if wi != 0:
                    if 2 * wi >= sh.width_inc:
                        w = w + sh.width_inc - wi
                    else:
                        w = w - wi
                hi = h % sh.height_inc
                if hi != 0:
                    if 2 * hi >= sh.height_inc:
                        h = h + sh.height_inc - hi
                    else:
                        h = h - hi

                if self.full_screen_windows(self):
                    root_width = self.screen.root_full_width
                    root_height = self.screen.root_full_height
                else:
                    root_width = self.screen.root_width
                    root_height = self.screen.root_height

                # Make sure the window fits on screen
                rw = root_width - base_width - 2 * self.border_width
                rh = root_height - base_height - 2 * self.border_width

                if w > rw:
                    w = rw - rw % sh.width_inc
                if h > rh:
                    h = rh - rh % sh.height_inc

                w = w + base_width
                h = h + base_height


            # Use minimum size if provided
            if sh.flags & Xutil.PMinSize:
                if w < sh.min_width:
                    w = sh.min_width
                if h < sh.min_height:
                    h = sh.min_height

            # Fall back to base size if that is provided
            elif sh.flags & Xutil.PBaseSize:
                if w < sh.base_width:
                    w = sh.base_width
                if h < sh.base_height:
                    h = sh.base_height

            # Use maximum size, but there's no fallback
            if sh.flags & Xutil.PMaxSize:
                if w > sh.max_width:
                    w = sh.max_width
                if h > sh.max_height:
                    h = sh.max_height

            # Fixing aspect ratio?  Nah.  Not now.

        return w, h


    def warppointer(self, x = None, y = None):
        """Warp the pointer to the coordinate (X, Y) in this window.

        X and Y are the coordinates to move the pointer to.  Positive values
        count from top/left edges, negative values from the
        bottom/right edges (the "negative" origo is (-1, -1)).

        If X and Y is omitted, warp to the default position (which
        normally is the middle of the window.

        A different default position can be stored in the mapping
        Client.default_pointer_pos.  The key is the res_name or res_class
        of the window, the value a tuple of two integers: the x and y coordinates.
        """
        if self.withdrawn:
            return

        # Get default position, if needed
        if x == None or y == None:
            if self.default_pointer_pos.has_key(self.res_name):
                x, y = self.default_pointer_pos[self.res_name]
            elif self.default_pointer_pos.has_key(self.res_class):
                x, y = self.default_pointer_pos[self.res_class]
            else:
                x = self.width / 2
                y = self.height / 2

        # Handle negative positions
        if x < 0:
            x = self.width + x - 1
        if y < 0:
            y = self.height + y - 1

        # Make sure that the pointer is inside the window
        if x < 0:
            x = 0
        elif x >= self.width:
            x = self.width - 1
        if y < 0:
            y = 0
        elif y >= self.height:
            y = self.height - 1

        self.window.warp_pointer(x, y)


    def fetch_name(self):
        if self.withdrawn:
            return None

        # Handle destroyed windows gracefully, registering the error
        # so the window is withdrawn from managing.
        try:
            return self.window.get_wm_name()
        except error.BadWindow, e:
            self.wm.events.put_event(e)
            return None


    def get_title(self):
        """Return an appropriate title for this client.
        """
        name = self.fetch_name()
        if not name:
            if self.res_name:
                name = self.res_name
            else:
                name = '<untitled>'
        return name


    def get_wm_state(self):
        if self.withdrawn:
            return Xutil.WithdrawnState

        try:
            r = self.window.get_wm_state()
        except error.BadWindow, e:
            self.wm.events.put_event(e)
            return None

        if r:
            return r.state
        else:
            return None


class Screen:
    allow_self_changes = cfilter.all

    def __init__(self, wm, screenno):
        self.wm = wm
        self.number = screenno
        self.info = self.wm.display.screen(screenno)

        # Fetch root window and important values
        self.root = self.wm.display.screen(self.number).root
        g =  self.root.get_geometry()
        self.root_x = 0
        self.root_y = 0
        self.root_width = g.width
        self.root_height = g.height
        self.root_full_width = g.width
        self.root_full_height = g.height

        self.windows = {}

        # Map proxy windows to actual windows
        self.proxy_windows = {}
        
        # Set up event handler for this screen
        self.dispatch = event.WindowDispatcher(self.root)

        ec = error.CatchError(error.BadAccess)
        self.dispatch.set_masks(X.SubstructureRedirectMask, onerror = ec)

        # And sync here, so we catch any errors caused by
        # failing to set SubstructureRedirectMask
        self.wm.display.sync()

        err = ec.get_error()
        if err:
            # Another wm already manages this screen: cancel
            raise UnavailableScreenError(err)

        # Fix a DISPLAY string for this screen by replacing the
        # screen number in the DISPLAY with this screen's number
        
        dstr = self.wm.display.get_display_name()
        m = re.search(r'(:\d+)(\.\d+)?$', dstr)
        if m:
            self.displaystring = dstr[:m.end(1)] + '.' + str(self.number)
        else:
            self.displaystring = dstr

        # Set up all the system handlers.

        # Handlers for the redirect events.  We don't want to update the masks
        # as we explicitly set those masks above.
        self.dispatch.add_system_handler(X.MapRequest,
                                         self.handle_map_request,
                                         masks = ())

        self.dispatch.add_system_handler(X.ConfigureRequest,
                                         self.handle_configure_request,
                                         masks = ())

        self.dispatch.add_system_handler(X.CirculateRequest,
                                         self.handle_circulate_request,
                                         masks = ())
        self.dispatch.add_system_handler(X.ClientMessage,
                                          self.handle_client_message)

        # Track screen changes
        self.dispatch.add_system_handler(X.EnterNotify, self.handle_screen_enter)
        self.dispatch.add_system_handler(X.ConfigureNotify, self.handle_screen_change)

        call_inits(self.__class__, '__screen_client_init__', self)

        # Find all clients, ignoring transient windows (override_redirect).
        r = self.root.query_tree()
        wins = r.children

        # Weed out icon windows (thanks to ctwm for the idea...)
        for w in wins[:]:
            wmh = w.get_wm_hints()
            if wmh and wmh.flags & Xutil.IconWindowHint:
                try:
                    wins.remove(wmh.icon_window)
                except ValueError:
                    pass

        # Then add any mapped window, or windows with non-withdrawn
        # WM_STATE property, unless it has override_redirect set.
        # Skip internal windows.

        for w in wins:
            a = w.get_attributes()
            r = w.get_wm_state()
            if (a.map_state != X.IsUnmapped
                or (r and r.state in (Xutil.NormalState, Xutil.IconicState))) \
                and not a.override_redirect \
                and not self.is_internal_window(w):

                self.add_client(w, 0)

        call_inits(self.__class__, '__screen_init__', self)

    def __del__(self):
        # Just call all __screen_del__ methods
        call_inits(self.__class__, '__screen_del__', self)

    def add_client(self, window, maprequest):
        """Add a client managing WINDOW.

        Returns 1 if a client was added, 0 if it was already managed.
        """
        if self.is_client(window):
            return 0
        else:
            debug('clients', 'Adding client for %s', window)
            client = self.wm.client_class(self, window, maprequest)

            # Use what client think its window is, to handle proxy windows
            self.windows[client.window] = client

            client.initial_map()
            self.wm.events.put_event(wmevents.AddClient(client))
            return 1

    def remove_window(self, window, destroyed = 0):
        """Remove the Window objet handling WINDOW.
        If DESTROYED is true, WINDOW doesn't exist anymore.
        """

        debug('window', 'Removing window for %s', window)

        wobj = self.get_window(window)

        if wobj is None:
            return

        # If it is a Client object, post a RemoveClient event.  As
        # that will not reach the client object, dispatch it manually.
        # Also, this must be done before withdrawing the window, as
        # that kills the event dispatcher.
        if isinstance(wobj, Client):
            debug('clientnotify', 'Sending RemoveClient for %s', window)
            evt = wmevents.RemoveClient(wobj)
            self.wm.events.put_event(evt)
            wobj.handle_event(evt)

        wobj.withdraw(destroyed)

        del self.windows[window]

    def add_internal_window(self, window):
        self.windows[window] = InternalWindow(self, window)
        return self.windows[window]

    def get_client(self, window):
        c = self.get_window(window)
        if isinstance(c, Client):
            return c
        else:
            return None

    def get_window(self, window):
        """Translate an Xlib window object into its controlling Window
        or Client object.

        Returns None if the Xlib window isn't known. 
        """

        while window is not None:
            try:
                return self.windows[window]
            except KeyError:
                # Try to translate via proxy window
                window = self.proxy_windows.get(window, None)

        return None


    def is_client(self, window):
        return isinstance(self.get_window(window), Client)
    

    def is_internal_window(self, window):
        return isinstance(self.get_window(window), InternalWindow)


    def add_proxy_window(self, proxy, window):
        """Add a proxy window, so that get_window() on PROXY leads on
        to WINDOW to find the actual Window object.
        """
    
        self.proxy_windows[proxy] = window


    def remove_proxy_window(self, proxy):
        """Remove a proxy window previously registered with add_proxy_window().
        """
    
        del self.proxy_windows[proxy]
        

    def query_clients(self, client_filter = cfilter.all, stackorder = 0):
        """Return a list of clients on this screen matching CLIENT_FILTER.

        By default, all clients are returned in no particular order.
        But if STACKORDER is true, the clients will be returned in their
        current stacking order, lowest client first.
        """

        if stackorder:
            wins = self.root.query_tree().children
            clients = []
            for w in wins:
                c = self.get_client(w)
                if c and client_filter(c):
                    clients.append(c)

            return clients
        else:
            # Use the internal filter function, but filter
            # out non-client windows by augmenting the client
            # filter
            return filter(cfilter.And(cfilter.is_client,
                                      client_filter),
                          self.windows.values())

    def alloc_border(self, edge, size):
        """Allocate a part of the outmost area of the root to display
        wm info in.  Clients will not infringe on this area.

        EDGE is one of 'top', 'bottom', 'left', or 'right', indicating
        on which edge of the root the area should be placed against.
        SIZE is the height or width of the area, depending on EDGE.

        The return value is a tuple, giving the coordinates of the
        allocated area: (x, y, width, height)
        """

        if edge == 'top':
            assert size < self.root_height

            c =  (self.root_x, self.root_y, self.root_width, size)

            self.root_height = self.root_height - size
            self.root_y = self.root_y + size

            return c

        elif edge == 'bottom':
            assert size < self.root_height

            self.root_height = self.root_height - size
            return (self.root_x, self.root_y + self.root_height,
                    self.root_width, size)

        elif edge == 'left':
            assert size < self.root_width

            c =  (self.root_x, self.root_y, size, self.root_height)

            self.root_width = self.root_width - size
            self.root_x = self.root_x + size

            return c

        elif edge == 'right':
            assert size < self.root_height

            self.root_width = self.root_width - size
            return (self.root_x + self.root_width, self.root_y,
                    size, self.root_height)

        else:
            raise TypeError('bad edge value: %s' % edge)


    def handle_event(self, event, window = None, grabbed = 0):
        grabbed = self.dispatch.handle_event(event, grabbed)

        if window:
            window.handle_event(event, grabbed)

    def handle_map_request(self, event):
        debug('redirect', 'Maprequest for client %s', event.window)
        # add the client, unless it already is managed
        if not self.add_client(event.window, 1):
            # already mapped window, map it if the user allows it
            w = self.get_client(event.window)
            if self.allow_self_changes(w):
                w.map()

    def event_to_change(self, event):
        change = {}
        if event.value_mask & X.CWX:
            change['x'] = event.x
        if event.value_mask & X.CWY:
            change['y'] = event.y
        if event.value_mask & X.CWWidth:
            change['width'] = event.width
        if event.value_mask & X.CWHeight:
            change['height'] = event.height
        w = self.get_window(event.window)
        if w and self.allow_self_changes(w):
            if event.value_mask & X.CWSibling:
                change['sibling'] = event.above
            if event.value_mask & X.CWStackMode:
                change['stack_mode'] = event.stack_mode
        return change

    def handle_configure_request(self, event):
        w = self.get_window(event.window)
        if w:
            debug('redirect', 'ConfigureRequest for window %s', event.window)
            apply(w.configure, (), self.event_to_change(event))
        else:
            debug('redirect', 'ConfigureRequest for unmanaged window %s', event.window)
            apply(event.window.configure, (), self.event_to_change(event))

    def handle_circulate_request(self, event):
        debug('redirect', 'CirculateRequest for %s', event.window)
        w = self.get_window(event.window)
        if not (w and self.allow_self_changes(w)):
            return

        if event.place == X.PlaceOnTop:
            if w:
                w.raisewindow()
            else:
                event.window.configure(stack_mode = X.Above)
        elif event.place == X.PlaceOnBottom:
            if w:
                w.lowerwindow()
            else:
                event.window.configure(stack_mode = X.Below)

    def handle_client_message(self, event):
        debug('client', 'client sent us a message: %s', event)
        w = self.get_client(event.window)
        if w is None:
            return
        if event.client_type == self.wm.WM_CHANGE_STATE and \
           event.data[1][0] == Xutil.IconicState and \
           self.allow_self_changes(w):
            w.iconify()

    def handle_screen_enter(self, event):
        # Mouse has entered this screen from another screen
        # iff these conditions are true:
        if event.window == self.root \
           and event.detail in (X.NotifyNonlinear, X.NotifyNonlinearVirtual):
            self.wm.current_screen = self

    def handle_screen_change(self, event):
        """The screen changed geometry. Cool."""

        if event.window == self.root and \
               (self.root_full_width != event.width or \
                self.root_full_height != event.height):

            self.root_full_width = self.root_width = event.width 
            self.root_full_height = self.root_height = event.height
            self.wm.handle_screen_resize(event)

    def system(self, cmd, fg = 0, evt = None, redirect = None):
        """Run the shell command CMD, with DISPLAY set to this screen.

        CMD is run in the background by default, but is FG is true
        this call will block until it has finished.

        If EVT is not None, it should be a CommandEvent or subclass.
        This event will be put on the event queue when CMD exits.  EVT
        is ignored though if FG is true.

        If REDIRECT is given, it should be either a single integer 0
        to 2, or a tuple of them.  These file descriptors will
        be redirected into pipes allowing the caller to capture the
        data.  The pipes are returned as file objects in a
        three-tuple, with index N corresponding the the redirected
        file descriptor N.  REDIRECT implies background execution.

        Returns None if CMD is run in the background and REDIRECT is
        None, if it is run in the foreground the exit status of CMD is
        returned as encoded by system().
        """

        # purge collected exit statuses of non-event children
        del self.wm.children_exit_status[:]

        if redirect is not None:
            if type(redirect) is not types.TupleType:
                redirect = (redirect, )

            fg = 0
            pipes = [None, None, None]
            for fd in redirect:
                if fd < 0 or fd > 2:
                    raise ValueError('redirect file descriptor should be 0-2: %s' % fd)
                if pipes[fd]:
                    raise ValueError('redirect file descriptor should not be repeated: %s' % redirect)

                pipes[fd] = os.pipe()


        pid = os.fork()
        if pid == 0:
            # Create new process group for the child, so it doesn't
            # get the signals sent to the parent process
            os.setpgid(0, 0)

            # Child process, run the command with a little help from sh
            os.environ['DISPLAY'] = self.displaystring
            try:
                # dup and close pipes on the child end
                if redirect:
                    if pipes[0]:
                        r, w = pipes[0]
                        os.close(w)
                        os.dup2(r, 0)
                        os.close(r)

                    if pipes[1]:
                        r, w = pipes[1]
                        os.close(r)
                        os.dup2(w, 1)
                        os.close(w)

                    if pipes[2]:
                        r, w = pipes[2]
                        os.close(r)
                        os.dup2(w, 2)
                        os.close(w)

                os.execlp('sh', 'sh', '-c', cmd)
            except os.error, msg:
                # Failed to run the program
                sys.stderr.write('%s: %s: %s\n' % (sys.argv[0], cmd, str(msg)))
                os._exit(127)

        else:
            # Parent, should we block here?
            if fg:
                # check if child still exists
                try:
                    p2, status = os.waitpid(pid, os.WNOHANG)
                    if p2 == pid:
                        return status
                except os.error, val:
                    if val.errno != errno.ECHILD:
                        raise val

                    # it is gone, grab error from collected exit
                    # statuses
                    for p2, status in self.wm.children_exit_status:
                        if pid == p2:
                            return status

                    # odd, the exit status have disappeared
                    raise RuntimeError("foreground children exit status has gone, this can't happen!")


                # otherwise we must loop until the children exits
                while 1:
                    # We must catch EINTR errors to avoid exceptions
                    # when recieving SIGCHLD.
                    try:
                        signal.pause()
                    except os.error, val:
                        raise val
                        if val.errno not in (errno.EINTR, errno.ECHILD):
                            raise val

                    for p2, status in self.wm.children_exit_status:
                        if pid == p2:
                            return status

            else:
                # FIXME: possible race condition here, should handle through
                # children_exit_status as above but can't be bothered
                # until something actually uses child events.
                if evt:
                    self.wm.add_command_event(pid, evt)

                if redirect:
                    # close pipes and create file objects
                    # on the parent end
                    if pipes[0]:
                        r, w = pipes[0]
                        os.close(r)
                        pipes[0] = os.fdopen(w, 'w')

                    if pipes[1]:
                        r, w = pipes[1]
                        os.close(w)
                        pipes[1] = os.fdopen(r, 'r')

                    if pipes[2]:
                        r, w = pipes[2]
                        os.close(w)
                        pipes[2] = os.fdopen(r, 'r')

                    return pipes
                else:
                    return None


    #
    # Cyclops circular reference detecting support
    #
    def _register_cycle_roots(self, cyclefinder):
        """Called by the corresponding function in WindowManager.
        """

        # We just add all the clients
        for c in self.clients.values():
            cyclefinder.register(c)

    def _cleanup_cycle_roots(self):
        """Called by the corresponding function in WindowManager.
        """
        for c in self.clients.values():
            self.remove_client(c.window, 1)


class WindowManager:
    client_class = Client
    screen_class = Screen

    appclass = 'Plwm'

    def __init__(self, disp, appname, db):
        """WindowManager(display, appname, rdb)

        Create a WindowManager object.
        """

        self.display = disp
        self.appname = appname
        self.rdb = db

        # Set up some atoms not defined in Xatom
        self.WM_DELETE_WINDOW = self.display.intern_atom('WM_DELETE_WINDOW')
        self.WM_PROTOCOLS = self.display.intern_atom('WM_PROTOCOLS')
        self.WM_STATE = self.display.intern_atom('WM_STATE')
        self.WM_TAKE_FOCUS = self.display.intern_atom('WM_TAKE_FOCUS')
        self.WM_CHANGE_STATE = self.display.intern_atom('WM_CHANGE_STATE')
        debug('atoms', 'DELETE_WINDOW: %s\tPROTOCOLS: %s\tSTATE: %s\t' \
              'TAKE_FOCUS: %s\t, CHANGE_STATE: %s',
              self.WM_DELETE_WINDOW, self.WM_PROTOCOLS, self.WM_STATE,
              self.WM_TAKE_FOCUS, self.WM_CHANGE_STATE)

        if display is not None:
            os.environ['DISPLAY'] = self.display.get_display_name()

        # Set up the event handling.
        self.events = event.EventFetcher(self.display)

        # Install handlers for child processes
        self.child_events = {}
        self.children_exit_status = []
        self.old_sigchld_handler = signal.signal(signal.SIGCHLD,
                                                 self.sigchld_handler)
        if self.old_sigchld_handler in (signal.SIG_IGN, signal.SIG_DFL):
            self.old_sigchld_handler = None


        # current_client is the client which currently contains the
        # pointer, and on which window operations should take placce.

        # focus_client is the client which actually holds the input
        # focus.

        self.current_client = None
        self.focus_client = None

        # Setup a default focus policy, which can be overridden later
        self.display.set_input_focus(X.NONE, X.PointerRoot, X.RevertToPointerRoot)

        # Set up a screen-indepentend event handler
        self.dispatch = event.SlaveDispatcher([])

        # Call mixin initialisation needed before adding screens
        call_inits(self.__class__, '__wm_screen_init__', self)

        # Find all the screens
        self.screens = []
        self.screen_nums = {}
        self.screen_roots = {}
        for sno in range(0, self.display.screen_count()):
            try:
                s = self.screen_class(self, sno)
                self.screens.append(s)
                self.screen_nums[sno] = s
                self.screen_roots[s.root] = s
                self.dispatch.add_master(s.dispatch)

            # This screen is already managed by some other
            # window manager, print a message and skip it
            except UnavailableScreenError:
                sys.stderr.write('%s: Screen %d already managed by some other window manager\n' % (sys.argv[0], sno))

        # If we cant find any screens, abort now
        if not self.screens:
            raise NoUnmanagedScreensError('another window manager already running?')

        try:
            self.default_screen = self.screen_nums[self.display.get_default_screen()]
        except KeyError:
            self.default_screen = self.screens[0]

        # Find the current screen
        self.current_screen = self.default_screen
        for s in self.screens:
            if s.root.query_pointer().same_screen:
                self.current_screen = s
                break

        # Handle to refresh the keyboard mapping information
        self.dispatch.add_system_handler(X.MappingNotify,
                                         self.handle_mapping_notify)

        # Handle errors caused by destroyed windows
        self.display.set_error_handler(self.x_error_handler)

        # Call all final mixin constructors
        call_inits(self.__class__, '__wm_init__', self)

    def __del__(self):
        # Just call all __wm_del__ methods
        call_inits(self.__class__, '__wm_del__', self)

    def loop(self):
        """Loop indefinitely, handling events.
        """
        while 1:
            event = self.events.next_event()
            self.handle_event(event)
            if event.type == wmevents.QuitWindowManager:
                self.display.sync()
                return

    def brave_loop(self, max_exc = 10):
        """Loop indefinitely, handling events.

        If an exception occur, catch it and print a traceback.
        Contiune processing events while less than MAX_EXC exceptions
        has occured.
        """
        exc = 0
        while 1:
            try:
                event = self.events.next_event()
                self.handle_event(event)
                if event.type == wmevents.QuitWindowManager:
                    self.display.sync()
                    return

            # Pass on keyboardinterrupt, exiting loop
            except KeyboardInterrupt:
                raise

            # Print all other exceptions and continue
            except:
                if exc < max_exc:
                    apply(traceback.print_exception, sys.exc_info())
                    exc = exc + 1
                else:
                    raise sys.exc_info()[0], sys.exc_info()[1]

    def quit(self):
        """Quit PLWM, or at least return to caller of loop()
        or brave_loop().
        """

        self.events.put_event(wmevents.QuitWindowManager())


    def handle_events(self):
        """Handle all the events on the queue.
        """
        while 1:
            event = self.events.next_event(timeout = 0)
            if event is None:
                return
            self.handle_event(event)


    def remove_window(self, window, destroyed = 0):
        """Remove the Window object of WINDOW.
        If DESTROYED is true, the window doesn't exist anymore.
        """
        w = self.get_window(window)
        if w is not None:
            w.screen.remove_window(window, destroyed)

    def get_client(self, window):
        for s in self.screens:
            c = s.get_client(window)
            if c:
                return c

        return None

    def get_window(self, window):
        for s in self.screens:
            w = s.get_window(window)
            if w:
                return w

        return None

    def is_client(self, window):
        for s in self.screens:
            if s.is_client(window):
                return 1
        return 0

    def is_internal_window(self, window):
        for s in self.screens:
            if s.is_internal_window(window):
                return 1
        return 0

    def query_clients(self, client_filter = cfilter.all, stackorder = 0):
        """Return a list of clients on all screens, matching CLIENT_FILTER.

        By default, all clients are returned sorted by their screen
        number, but with no additional sorting.

        If STACKORDER is true, the clients will be returned in their
        current stacking order, lowest client first.
        """

        clients = []
        for s in self.screens:
            clients = clients + s.query_clients(client_filter, stackorder)

        return clients


    def x_error_handler(self, err, request):
        """Handle window errors by telling the client than it
        is withdrawn.
        """

        sys.stderr.write('X protocol error:\n%s\n' % err)
        
        if isinstance(err, error.BadWindow) or isinstance(err, error.BadDrawable):
            # The error can't be handled immediately, as the error
            # handler must not call any Xlib methods.  Solve this by
            # queing up the error object on the event queue and
            # checking for this in the event handler just below.

            # The event queue doesn't care about us putting an XError
            # there, it just returns it to us.

            self.events.put_event(err)


    def handle_event(self, event):

        """Handle EVENT by dispatching to registered handlers.

        First handlers for the WindowManager as whole is called,
        followed by the handlers for each of the managed screen's root
        window.  Then if the events corresponds to a client, call its
        handlers.  A grab handler for the window manager prevents any
        grab and normal handlers for the client to be called.
        """

        debug('verbose_event', 'handling %s', event)

        # Continuation of the error handling hack: check if this is a
        # window error and if so remove it
        if isinstance(event, error.BadWindow) or isinstance(event, error.BadDrawable):
            self.remove_window(event.resource_id, 1)
            return


        grabbed = self.dispatch.handle_event(event)

        if hasattr(event, 'client') and event.client:
            event.client.screen.handle_event(event, event.client, grabbed)
            return

        if hasattr(event, 'window'):
            window = event.window
            # Find the correct screen.

            # First check if the event window is
            # the root window or is an already handled client for
            # some screen
            for s in self.screens:
                if window == s.root:
                    s.handle_event(event, None, grabbed)
                    return

                w = s.get_window(window)
                if w:
                    s.handle_event(event, w, grabbed)
                    return

            # Unknown window, ask for it's root window
            if window:
                try:
                    root = window.get_geometry().root
                    s = self.screen_roots[root]
                # Bad window, or unmanaged screen, just abort
                except (error.BadDrawable, KeyError):
                    pass
                else:
                    s.handle_event(event, None, grabbed)

            return

        # Event destined for specific screen
        if hasattr(event, 'screen') and event.screen:
            event.screen.handle_event(event, None, grabbed)

        # Unmanaged screen, ignore
        return


    def handle_mapping_notify(self, event):
        debug('keys', 'MappingNotify event')
        self.display.refresh_keyboard_mapping(event)


    def handle_screen_resize(self, event):
        # The default window manager does nada; but let mixins know

	call_inits(self.__class__, '__wm_screen_resize__', self)

    def set_current_client(self, client, time = X.CurrentTime, force_focus = 0):
        """Set the current client to CLIENT, occuring at the X event
        TIME.

        This will update the WindowManager.current_client and
        Client.current of the affected clients.  A CurrentClientChange
        event will be sent with the new current client.

        CLIENT can also be None, meaning that now no client is the
        current one.

        If the client accepts focus, or FORCE_FOCUS is true, CLIENT
        will get the focus, and WindowManager.focus_client and
        Client.focused will be changed.  ClientFocusOut and
        ClientFocusIn events will be sent.
        """

        # Will the new current client also get focus?
        # If client is None, we treat that as getting focus
        if client:
            getfocus = (client.do_set_focus or force_focus)
        else:
            getfocus = 1

        # If focus will change, drop focus from the old focused client

        if getfocus and self.focus_client and self.focus_client != client:
            self.focus_client.lose_focus()
            self.focus_client = None

        # If current client changes, update that

        if self.current_client != client:
            if self.current_client:
                self.current_client.current = 0
                screen = self.current_client.screen
            else:
                screen = None

            if client:
                client.current = 1

            self.current_client = client
            self.events.put_event(wmevents.CurrentClientChange(screen, client))
            debug('focus', 'current client: %s', self.current_client)

        # Finally, give the client focus if it should have it
        if client:
            if getfocus and self.focus_client != client:
                self.focus_client = client
                client.get_focus(time)

        # No client, reset focus
        else:
            self.display.set_input_focus(X.PointerRoot,
                                         X.RevertToPointerRoot, time)

    def rdb_get(self, res, cls, default = None):
        """rdb_get(res, cls, default = None)

        Get a value from the resource database.

        RES and CLS should omit the first component, and thus start
        with periods '.'.  This is because the application name
        (fetched from sys.argv[0]) will be prepended to RES,
        and self.appclass will be prepended to CLS.

        DEFAULT is returned if the resource can't be found.
        """

        return self.rdb.get(self.appname + res, self.appclass + cls, default)


    def system(self, cmd, fg = 0, evt = None, redirect = None):
        """Run CMD on the current screen.
        """
        return self.current_screen.system(cmd, fg = fg, evt = evt,
                                          redirect = redirect)

    def add_command_event(self, pid, evt):
        """Add PID to the list of child processes to wait for, inserting
        EVT in the event queue when it does.
        """
        self.child_events[pid] = evt

    def sigchld_handler(self, sig, frame):
        """Signal handler for SIGCHLD.
        """

        while 1:
            try:
                pid, status = os.waitpid(-1, os.WNOHANG)
                if pid == 0:
                    break

                # child with event handler
                if self.child_events.has_key(pid):
                    evt = self.child_events[pid]
                    del self.child_events[pid]
                    evt.status = status
                    self.events.put_event(evt)

                # unmanaged child, we might be interested in it anyway
                # so record it.
                else:
                    self.children_exit_status.append((pid, status))

            except os.error, val:
                if val.errno == errno.EINTR:
                    continue
                if val.errno == errno.ECHILD:
                    break
                raise val


        # Call the old sigchld handler, if any
        if self.old_sigchld_handler:
            self.old_sigchld_handler(sig, frame)


    def fake_button_click(self, button):
        self.display.xtest_fake_input(X.ButtonPress, button, 0)
        self.display.xtest_fake_input(X.ButtonRelease, button, 5)
        self.display.flush()



    #
    # Cyclops circular reference detecting support
    #
    def _register_cycle_roots(self, cyclefinder):
        """Call this function after having initialised the
        WindowManager.  It will register all dynamically added objects,
        mainly Clients.  Then call loop or brave_loop with the run
        method of the cyclefinder object.
        """

        # Currently, the WindowManager has no dynamic objects,
        # only static ones like Screen.  So ask them.
        for s in self.screens:
            s._register_cycle_roots(cyclefinder)


    def _cleanup_cycle_roots(self):
        """Free all dynamically added objects.
        """

        # WindowManager only has this link to any Clients
        self.focus_client = None

        # But the screens have more...
        for s in self.screens:
            s._cleanup_cycle_roots()


def call_inits(cls, method, obj):
    """Call constructors for all mixin classes.

    If CLS has a method named METHOD, it will be called with OBJ as an argument.
    Otherwise call_inits will be called recursively for each base class of CLS.
    """

    if cls.__dict__.has_key(method):
        cls.__dict__[method](obj)
    else:
        for c in cls.__bases__:
            call_inits(c, method, obj)



class WindowProxyBase:
    """Base class for proxy objects for the real Xlib window objects.

    These proxies can override the Xlib window object methods when
    necessary.
    """

    def __init__(self, screen, window):
        self._screen = screen
        self._window = window
        self._wm = screen.wm

        # These Xlib casts allows the proxy object to be passed to any
        # Xlib method expecting one of the following objects
        self.__resource__ = window.__resource__
        self.__drawable__ = window.__resource__
        self.__window__ = window.__resource__

        # And these allows the proxy, for dictionary purposes, to
        # pretend to be the actual window object.
        self.__cmp__ = window.__cmp__
        self.__hash__ = window.__hash__


    def __proxy_event_init__(self, client):
        # Client can now register event handlers
        self._client = client


    def __getattr__(self, attr):
        # Unknown attribute in the proxy, grab it from the real window instead
        v = getattr(self._window, attr)

        # And avoid this getting called again for this attr
        setattr(self, attr, v)

        return v


    def _proxy_withdraw(self):
        """Called by Client when the proxied window is gone, to allow any cleanup.
        """
        pass
        

    def __str__(self):
        return '<%s for %s 0x%08x>' % (
            self.__class__, 
            self._window.__class__, self._window.id)


    def __repr__(self):
        return self.__str__()


# main function, parsing some common options and doing
# all that typical initialization.

wm_options = rdb.stdopts.copy()
wm_options['-debug'] =  rdb.SepArg('.debug')
wm_options['-version'] =  rdb.IsArg('.version')

def main(wmclass, extra_options = {}, xlib_version = required_xlib_version):
    global debug, debug_what

    # Check Xlib version
    if Xlib.__version__ < xlib_version:
        raise BadXlibVersion('requires Xlib %s, found %s'
                             % (string.join(map(str, xlib_version), '.'),
                                string.join(map(str, Xlib.__version__), '.')))

    opts = wm_options.copy()
    opts.update(extra_options)

    disp, name, db, args = rdb.get_display_opts(opts)

    # Print PLWM version, if set
    version = db.get(name + '.version', 'PLWM.Version', None)
    if version == '-version':
        try:
            sys.stderr.write('PLWM version: %s\n' % plwm.__version__)
        except AttributeError:
            sys.stderr.write('PLWM version: unknown (probably in build tree)\n')

    # Get debug info
    dbg = db.get(name + '.debug', 'PLWM.Debug', None)

    # Parse debugstring, separated by commas, and remove any whitespace and
    # empty elements
    if dbg is not None:
        dbg = string.split(dbg, ',')
        dbg = map(string.strip, dbg)
        dbg = filter(None, dbg)

        debug_what = dbg
        debug = do_debug

    try:
        wm = wmclass(disp, name, db)
    except NoUnmanagedScreensError:
        sys.stderr.write(sys.argv[0] + ': Another window manager already running?\n')
        sys.exit(1)

    try:
        wm.brave_loop()

    # Exit gracefully on Ctrl-C
    except KeyboardInterrupt:
        sys.exit(0)