~bennabiy/+junk/python-xlib

« back to all changes in this revision

Viewing changes to .pc/python3.patch/examples/xrandr.py

  • Committer: Package Import Robot
  • Author(s): Andrew Shadura, Ramkumar Ramachandra, Andrew Shadura
  • Date: 2015-08-13 08:14:19 UTC
  • mfrom: (6.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20150813081419-hdefinnghp2iydkx
Tags: 0.14+20091101-3
[ Ramkumar Ramachandra ]
* Remove useless debugging output (Closes: #565996)

[ Andrew Shadura ]
* Switch to 3.0 (quilt) format.
* Rename patches.
* Use debhelper 9 in its short form.
* Use pybuild.
* Bump Standards-Version.
* Don't build or install PostScript documentation and info files.
* Use system-provided texi2html instead of a shipped version
  (Closes: #795057).
* Update debian/copyright (Closes: #795057).
* Don't install Makefile or texi2html with the documentation.
* Set executable bit for examples.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
#
 
3
# examples/xrandr.py -- demonstrate the RandR extension
 
4
#
 
5
#    Copyright (C) 2009 David H. Bronke <whitelynx@gmail.com>
 
6
#
 
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.
 
11
#
 
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.
 
16
#
 
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
 
20
 
 
21
 
 
22
import sys, os, pprint
 
23
 
 
24
# Change path so we find Xlib
 
25
sys.path.insert(1, os.path.join(sys.path[0], '..'))
 
26
 
 
27
from Xlib import X, display, Xutil
 
28
from Xlib.ext import randr
 
29
 
 
30
# Application window (only one)
 
31
class Window:
 
32
    def __init__(self, display):
 
33
        self.d = display
 
34
 
 
35
        # Check for extension
 
36
        if not self.d.has_extension('RANDR'):
 
37
            sys.stderr.write('%s: server does not have the RANDR extension\n'
 
38
                             % sys.argv[0])
 
39
            print self.d.query_extension('RANDR')
 
40
            sys.stderr.write("\n".join(self.d.list_extensions()))
 
41
            if self.d.query_extension('RANDR') is None:
 
42
                sys.exit(1)
 
43
 
 
44
        # print version
 
45
        r = self.d.xrandr_query_version()
 
46
        print 'RANDR version %d.%d' % (r.major_version, r.minor_version)
 
47
 
 
48
 
 
49
        # Grab the current screen
 
50
        self.screen = self.d.screen()
 
51
 
 
52
        self.window = self.screen.root.create_window(
 
53
            50, 50, 300, 200, 2,
 
54
            self.screen.root_depth,
 
55
            X.InputOutput,
 
56
            X.CopyFromParent,
 
57
 
 
58
            # special attribute values
 
59
            background_pixel = self.screen.white_pixel,
 
60
            event_mask = (X.ExposureMask |
 
61
                          X.StructureNotifyMask |
 
62
                          X.ButtonPressMask |
 
63
                          X.ButtonReleaseMask |
 
64
                          X.Button1MotionMask),
 
65
            colormap = X.CopyFromParent,
 
66
            )
 
67
 
 
68
        self.gc = self.window.create_gc(
 
69
            foreground = self.screen.black_pixel,
 
70
            background = self.screen.white_pixel,
 
71
            )
 
72
 
 
73
        # Set some WM info
 
74
 
 
75
        self.WM_DELETE_WINDOW = self.d.intern_atom('WM_DELETE_WINDOW')
 
76
        self.WM_PROTOCOLS = self.d.intern_atom('WM_PROTOCOLS')
 
77
 
 
78
        self.window.set_wm_name('Xlib example: xrandr.py')
 
79
        self.window.set_wm_icon_name('xrandr.py')
 
80
        self.window.set_wm_class('xrandr', 'XlibExample')
 
81
 
 
82
        self.window.set_wm_protocols([self.WM_DELETE_WINDOW])
 
83
        self.window.set_wm_hints(flags = Xutil.StateHint,
 
84
                                 initial_state = Xutil.NormalState)
 
85
 
 
86
        self.window.set_wm_normal_hints(flags = (Xutil.PPosition | Xutil.PSize
 
87
                                                 | Xutil.PMinSize),
 
88
                                        min_width = 20,
 
89
                                        min_height = 20)
 
90
 
 
91
        # Map the window, making it visible
 
92
        self.window.map()
 
93
 
 
94
        # Enable all RandR events.
 
95
        self.window.xrandr_select_input(
 
96
            randr.RRScreenChangeNotifyMask
 
97
            | randr.RRCrtcChangeNotifyMask
 
98
            | randr.RROutputChangeNotifyMask
 
99
            | randr.RROutputPropertyNotifyMask
 
100
            )
 
101
 
 
102
        self.pp = pprint.PrettyPrinter(indent=4)
 
103
 
 
104
        print "Screen info:"
 
105
        self.pp.pprint(self.window.xrandr_get_screen_info()._data)
 
106
 
 
107
        print "Screen size range:"
 
108
        self.pp.pprint(self.window.xrandr_get_screen_size_range()._data)
 
109
 
 
110
        print "Primary output:"
 
111
        self.pp.pprint(self.window.xrandr_get_output_primary()._data)
 
112
 
 
113
        resources = self.window.xrandr_get_screen_resources()._data
 
114
 
 
115
        print "Modes:"
 
116
        for mode_id, mode in self.parseModes(resources['mode_names'], resources['modes']).iteritems():
 
117
            print "    %d: %s" % (mode_id, mode['name'])
 
118
 
 
119
        for output in resources['outputs']:
 
120
            print "Output %d info:" % (output, )
 
121
            self.pp.pprint(self.d.xrandr_get_output_info(output, resources['config_timestamp'])._data)
 
122
 
 
123
        for crtc in resources['crtcs']:
 
124
            print "CRTC %d info:" % (crtc, )
 
125
            self.pp.pprint(self.d.xrandr_get_crtc_info(crtc, resources['config_timestamp'])._data)
 
126
 
 
127
        print "Raw screen resources:"
 
128
        self.pp.pprint(resources)
 
129
 
 
130
    def parseModes(self, mode_names, modes):
 
131
        lastIdx = 0
 
132
        modedatas = dict()
 
133
        for mode in modes:
 
134
            modedata = dict(mode._data)
 
135
            modedata['name'] = mode_names[lastIdx:lastIdx + modedata['name_length']]
 
136
            modedatas[modedata['id']] = modedata
 
137
            lastIdx += modedata['name_length']
 
138
        return modedatas
 
139
 
 
140
    # Main loop, handling events
 
141
    def loop(self):
 
142
        current = None
 
143
        while 1:
 
144
            e = self.d.next_event()
 
145
 
 
146
            # Window has been destroyed, quit
 
147
            if e.type == X.DestroyNotify:
 
148
                sys.exit(0)
 
149
 
 
150
            # Screen information has changed
 
151
            elif e.type == self.d.extension_event.ScreenChangeNotify:
 
152
                print 'Screen change'
 
153
                print self.pp.pprint(e._data)
 
154
 
 
155
            # CRTC information has changed
 
156
            elif e.type == self.d.extension_event.CrtcChangeNotify:
 
157
                print 'CRTC change'
 
158
                print self.pp.pprint(e._data)
 
159
 
 
160
            # Output information has changed
 
161
            elif e.type == self.d.extension_event.OutputChangeNotify:
 
162
                print 'Output change'
 
163
                print self.pp.pprint(e._data)
 
164
 
 
165
            # Output property information has changed
 
166
            elif e.type == self.d.extension_event.OutputPropertyNotify:
 
167
                print 'Output property change'
 
168
                print self.pp.pprint(e._data)
 
169
 
 
170
            # Somebody wants to tell us something
 
171
            elif e.type == X.ClientMessage:
 
172
                if e.client_type == self.WM_PROTOCOLS:
 
173
                    fmt, data = e.data
 
174
                    if fmt == 32 and data[0] == self.WM_DELETE_WINDOW:
 
175
                        sys.exit(0)
 
176
 
 
177
 
 
178
if __name__ == '__main__':
 
179
    Window(display.Display()).loop()
 
180