-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdetect_monitors.py
More file actions
executable file
·160 lines (126 loc) · 4.93 KB
/
detect_monitors.py
File metadata and controls
executable file
·160 lines (126 loc) · 4.93 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Detect Screens
Copyright (C) 2016 Thomaz de Oliveira dos Reis <thor27@gmail.com>
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 3 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, see <http://www.gnu.org/licenses/>.
"""
from __future__ import print_function
import gtk
import sys
import argparse
try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse
class Kiosk(object):
__NUM_KIOSKS = 0
@staticmethod
def push_kiosk():
Kiosk.__NUM_KIOSKS += 1
return Kiosk.__NUM_KIOSKS
@staticmethod
def pop_kiosk():
Kiosk.__NUM_KIOSKS -= 1
return Kiosk.__NUM_KIOSKS
def __init__(self, monitor, disable_close=True, text="Hello World"):
self.disable_close = disable_close
self.text = text
self.create_window()
self.move_to_monitor(monitor)
self.fullscreen()
Kiosk.push_kiosk()
def create_window(self):
self.window = gtk.Window()
self.screen = self.window.get_screen()
self.label = gtk.Label()
self.label.set_markup("<span weight='bold' size='100000'>{0}</span>".format(self.text))
self.label.set_alignment(xalign=0.05, yalign=0.05)
self.window.add(self.label)
self.window.connect("delete-event", self.on_close)
self.window.show_all()
def fullscreen(self):
self.window.fullscreen()
self.window.show_all()
self.window.set_resizable(False)
def move_to_monitor(self, id):
monitor = self.screen.get_monitor_geometry(id)
self.window.move(monitor.x, monitor.y)
self.window.set_size_request(monitor.width, monitor.height)
def on_close(self, *args, **kwargs):
if self.disable_close:
return True
if not Kiosk.pop_kiosk():
gtk.main_quit()
def get_text(parent, message, default=''):
"""
Display a dialog with a text entry.
Returns the text, or None if canceled.
"""
d = gtk.MessageDialog(parent,
gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
gtk.MESSAGE_QUESTION,
gtk.BUTTONS_OK_CANCEL,
message)
entry = gtk.Entry()
entry.set_text(default)
entry.show()
def filter_numbers(entry, *args):
text = entry.get_text().strip()
entry.set_text(''.join([i for i in text if i.isdigit()]))
entry.connect('changed', filter_numbers)
d.vbox.pack_end(entry)
entry.connect('activate', lambda _: d.response(gtk.RESPONSE_OK))
d.set_default_response(gtk.RESPONSE_OK)
r = d.run()
text = entry.get_text().decode('utf8')
d.destroy()
if r == gtk.RESPONSE_OK:
return text
else:
return None
def parse_args(args):
parser = argparse.ArgumentParser(description='Detect screens')
parser.add_argument('urls', metavar='URL', type=str, nargs='+',
help='All urls you want to show')
parser.add_argument('-m', '--messages', type=str, nargs='+', dest='messages',
help='Text of each message to ask for monitor URL')
parser.add_argument('-d', '--default-url', type=str, dest='default_url', default='about:blank',
help='Default URL to use (Default: about:blank)')
parser.add_argument('-c', '--allow-close', dest='disable_close', action='store_false',
help='Allow browser window to be closed')
return parser.parse_args(args)
def get_num_monitors():
screen = gtk.gdk.Screen()
return screen.get_n_monitors()
def main():
args = parse_args(sys.argv[1:])
num_monitors = get_num_monitors()
for monitor in range(num_monitors):
Kiosk(
monitor,
text="{0}".format(monitor),
disable_close=args.disable_close,
)
result = [args.default_url for x in range(num_monitors)]
for pos, url in enumerate(args.urls[:num_monitors]):
message = args.messages[pos] if args.messages and pos < len(args.messages) else url
while True:
user_input = get_text(None, message)
if user_input:
monitor_number = int(user_input)
if monitor_number < num_monitors and result[monitor_number] == args.default_url:
break
result[monitor_number] = url
return result
if __name__ == '__main__':
print(" ".join(main()))