File: dpms.py

package info (click to toggle)
python-xlib 0.33-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,040 kB
  • sloc: python: 23,022; awk: 89; makefile: 60; sh: 10
file content (130 lines) | stat: -rwxr-xr-x 4,076 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/python
#
# examples/dpms.py -- DPMS usage examples.
#
#    Copyright (C) 2020 Thiago Kenji Okada <thiagokokada@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation; either version 2.1
# of the License, or (at your option) any later version.
#
# This library 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
#    Free Software Foundation, Inc.,
#    59 Temple Place,
#    Suite 330,
#    Boston, MA 02111-1307 USA

import random
import time

from Xlib import display
from Xlib.ext import dpms


class DPMSExamples(object):
    def __init__(self):
        self.d = display.Display()

        capable = self.d.dpms_capable()
        assert capable, 'DPMS is not supported in your system'

        self.initial_info = self.d.dpms_info()
        self.initial_timeouts = self.d.dpms_get_timeouts()

        # Making sure that DPMS is enable for this examples
        self.d.dpms_enable()
        self.d.sync()

    def print_dpms(self):
        current_info = self.d.dpms_info()
        print('\nDPMS state: {}\nPower level: {}'.format(current_info.state,
                                                         current_info.power_level))
        current_timeouts = self.d.dpms_get_timeouts()
        print('Standby: {}, Suspend: {}, Off: {}\n'.format(current_timeouts.standby_timeout,
                                                           current_timeouts.suspend_timeout,
                                                           current_timeouts.off_timeout))

    def toggle_dpms(self):
        current_info = self.d.dpms_info()
        if current_info.state:
            self.d.dpms_disable()
        else:
            self.d.dpms_enable()

        self.d.sync()

    def restore(self):
        print('Restoring DPMS configuration')
        self.d.dpms_set_timeouts(self.initial_timeouts.standby_timeout,
                                 self.initial_timeouts.suspend_timeout,
                                 self.initial_timeouts.off_timeout)
        if self.initial_info.state:
            self.d.dpms_enable()
        else:
            self.d.dpms_disable()

        self.d.sync()

        self.print_dpms()

    def set_random_timeouts(self):
        # Can be any number greater than 0
        # Using 10 just to not turnoff the screen suddenly
        standby_timeout = random.randint(10, 600)
        # Shouldn't be smaller than standby_timeout
        suspend_timeout = random.randint(standby_timeout, 600)
        # Shouldn't be smaller than standby_timeout or suspend_timeout
        off_timeout = random.randint(suspend_timeout, 600)
        self.d.dpms_set_timeouts(standby_timeout, suspend_timeout, off_timeout)
        self.d.sync()

    def turn_off_display(self):
        self.d.dpms_force_level(dpms.DPMSModeOff)
        self.d.sync()

    def turn_on_display(self):
        self.d.dpms_force_level(dpms.DPMSModeOn)
        self.d.sync()


def main():
    try:
        examples = DPMSExamples()

        print('Initial state')
        examples.print_dpms()

        print('Setting random timeouts')
        examples.set_random_timeouts()
        examples.print_dpms()

        print('The next example will turn-off your screen, press Ctrl-C to cancel.')
        time.sleep(2)
        examples.turn_off_display()

        print('Turning it on again...')
        time.sleep(2)
        examples.turn_on_display()

        print()

        print('Toggle DPMS')
        examples.toggle_dpms()
        examples.print_dpms()

        print('Toggle it again')
        examples.toggle_dpms()
        examples.print_dpms()
    finally:
        examples.restore()


if __name__ == '__main__':
    main()