-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathstatnot
More file actions
executable file
·318 lines (272 loc) · 11.7 KB
/
statnot
File metadata and controls
executable file
·318 lines (272 loc) · 11.7 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
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
#!/usr/bin/env python3
#
# statnot - Status and Notifications
#
# Lightweight notification-(to-become)-deamon intended to be used
# with lightweight WMs, like dwm.
# Receives Desktop Notifications (including libnotify / notify-send)
# See: http://www.galago-project.org/specs/notification/0.9/index.html
#
# Note: VERY early prototype, to get feedback.
#
# Copyright (c) 2009-2020 by the authors
#
# 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, see <http://www.gnu.org/licenses/>.
#
import dbus
import dbus.service
import dbus.mainloop.glib
from gi.repository import GLib
import os
import subprocess
import sys
import _thread
import time
from html.entities import name2codepoint as n2cp
import re
# ===== CONFIGURATION DEFAULTS =====
#
# See helpstring below for what each setting does
DEFAULT_NOTIFY_TIMEOUT = 3000 # milliseconds
MAX_NOTIFY_TIMEOUT = 5000 # milliseconds
NOTIFICATION_MAX_LENGTH = 100 # number of characters
STATUS_UPDATE_INTERVAL = 2.0 # seconds
STATUS_COMMAND = ["/bin/sh", "%s/.statusline.sh" % os.getenv("HOME")]
USE_STATUSTEXT=True
QUEUE_NOTIFICATIONS=True
# dwm
def update_text(text):
# Get first line
first_line = text.splitlines()[0] if text else ''
subprocess.call(["xsetroot", "-name", first_line])
# ===== CONFIGURATION END =====
def _getconfigvalue(configmodule, name, default):
if hasattr(configmodule, name):
return getattr(configmodule, name)
return default
def readconfig(filename):
import imp
try:
config = imp.load_source("config", filename)
except Exception as e:
print(f"Error: failed to read config file {filename}")
print(e)
sys.exit(2)
for setting in ("DEFAULT_NOTIFY_TIMEOUT", "MAX_NOTIFY_TIMEOUT", "NOTIFICATION_MAX_LENGTH", "STATUS_UPDATE_INTERVAL",
"STATUS_COMMAND", "USE_STATUSTEXT", "QUEUE_NOTIFICATIONS", "update_text"):
if hasattr(config, setting):
globals()[setting] = getattr(config, setting)
def strip_tags(value):
"Return the given HTML with all tags stripped."
return re.sub(r'<[^>]*?>', '', value)
# from http://snipplr.com/view/19472/decode-html-entities/
# also on http://snippets.dzone.com/posts/show/4569
def substitute_entity(match):
ent = match.group(3)
if match.group(1) == "#":
if match.group(2) == '':
return unichr(int(ent))
elif match.group(2) == 'x':
return unichr(int('0x'+ent, 16))
else:
cp = n2cp.get(ent)
if cp:
return unichr(cp)
else:
return match.group()
def decode_htmlentities(string):
entity_re = re.compile(r'&(#?)(x?)(\w+);')
return entity_re.subn(substitute_entity, string)[0]
# List of not shown notifications.
# Array of arrays: [id, text, timeout in s]
# 0th element is being displayed right now, and may change
# Replacements of notification happens att add
# message_thread only checks first element for changes
notification_queue = []
notification_queue_lock = _thread.allocate_lock()
def add_notification(notif):
with notification_queue_lock:
for index, n in enumerate(notification_queue):
if n[0] == notif[0]: # same id, replace instead of queue
n[1:] = notif[1:]
return
notification_queue.append(notif)
def next_notification(pop = False):
# No need to be thread safe here. Also most common scenario
if not notification_queue:
return None
with notification_queue_lock:
if QUEUE_NOTIFICATIONS:
# If there are several pending messages, discard the first 0-timeouts
while len(notification_queue) > 1 and notification_queue[0][2] == 0:
notification_queue.pop(0)
else:
while len(notification_queue) > 1:
notification_queue.pop(0)
if pop:
return notification_queue.pop(0)
else:
return notification_queue[0]
def get_statustext(notification = ''):
output = ''
try:
if not notification:
command = STATUS_COMMAND
else:
command = STATUS_COMMAND + [notification]
p = subprocess.Popen(command, stdout=subprocess.PIPE)
output = p.stdout.read()
except:
sys.stderr.write("%s: could not read status message (%s)\n"
% (sys.argv[0], ' '.join(STATUS_COMMAND)))
# Error - STATUS_COMMAND didn't exist or delivered empty result
# Fallback to notification only
if not output:
output = notification
return output
def message_thread(dummy):
last_status_update = 0
last_notification_update = 0
current_notification_text = ''
while 1:
notif = next_notification()
current_time = time.time()
update_status = False
if notif:
if notif[1] != current_notification_text:
update_status = True
elif current_time > last_notification_update + notif[2]:
# If requested timeout is zero, notification shows until
# a new notification arrives or a regular status mesasge
# cleans it
# This way is a bit risky, but works. Keep an eye on this
# when changing code
if notif[2] != 0:
update_status = True
# Pop expired notification
next_notification(True)
notif = next_notification()
if update_status == True:
last_notification_update = current_time
if current_time > last_status_update + STATUS_UPDATE_INTERVAL:
update_status = True
if update_status:
if notif:
current_notification_text = notif[1]
else:
current_notification_text = ''
if USE_STATUSTEXT:
update_text(get_statustext(current_notification_text))
else:
if current_notification_text != '':
update_text(current_notification_text)
last_status_update = current_time
time.sleep(0.1)
class NotificationFetcher(dbus.service.Object):
_id = 0
@dbus.service.method("org.freedesktop.Notifications",
in_signature='susssasa{ss}i',
out_signature='u')
def Notify(self, app_name, notification_id, app_icon,
summary, body, actions, hints, expire_timeout):
if (expire_timeout < 0) or (expire_timeout > MAX_NOTIFY_TIMEOUT):
expire_timeout = DEFAULT_NOTIFY_TIMEOUT
if not notification_id:
self._id += 1
notification_id = self._id
text = (f"{summary} {body}").strip()
add_notification( [notification_id,
text[:NOTIFICATION_MAX_LENGTH],
int(expire_timeout) / 1000.0] )
return notification_id
@dbus.service.method("org.freedesktop.Notifications", in_signature='', out_signature='as')
def GetCapabilities(self):
return ("body")
@dbus.service.signal('org.freedesktop.Notifications', signature='uu')
def NotificationClosed(self, id_in, reason_in):
pass
@dbus.service.method("org.freedesktop.Notifications", in_signature='u', out_signature='')
def CloseNotification(self, id):
pass
@dbus.service.method("org.freedesktop.Notifications", in_signature='', out_signature='ssss')
def GetServerInformation(self):
return ("statnot", "http://code.k2h.se", "0.0.2", "1")
if __name__ == '__main__':
for curarg in sys.argv[1:]:
if curarg in ('-v', '--version'):
print(f"{sys.argv[0]} CURVERSION")
sys.exit(1)
elif curarg in ('-h', '--help'):
print(f" Usage: {sys.argv[0]} [-h] [--help] [-v] [--version] [configuration file]\n"
" -h, --help: Print this help and exit\n"
" -v, --version: Print version and exit\n"
"\n"
" Configuration:\n"
" A file can be read to set the configuration.\n"
" This configuration file must be written in valid python,\n"
" which will be read if the filename is given on the command line.\n"
" You do only need to set the variables you want to change, and can\n"
" leave the rest out.\n"
"\n"
" Below is an example of a configuration which sets the defaults.\n"
"\n"
" # Default time a notification is show, unless specified in notification\n"
" DEFAULT_NOTIFY_TIMEOUT = 3000 # milliseconds\n"
" \n"
" # Maximum time a notification is allowed to show\n"
" MAX_NOTIFY_TIMEOUT = 5000 # milliseconds\n"
" \n"
" # Maximum number of characters in a notification.\n"
" NOTIFICATION_MAX_LENGTH = 100 # number of characters\n"
" \n"
" # Time between regular status updates\n"
" STATUS_UPDATE_INTERVAL = 2.0 # seconds\n"
" \n"
" # Command to fetch status text from. We read from stdout.\n"
" # Each argument must be an element in the array\n"
" # os must be imported to use os.getenv\n"
" import os\n"
" STATUS_COMMAND = ['/bin/sh', '%s/.statusline.sh' % os.getenv('HOME')]\n"
"\n"
" # Always show text from STATUS_COMMAND? If false, only show notifications\n"
" USE_STATUSTEXT=True\n"
" \n"
" # Put incoming notifications in a queue, so each one is shown.\n"
" # If false, the most recent notification is shown directly.\n"
" QUEUE_NOTIFICATIONS=True\n"
" \n"
" # update_text(text) is called when the status text should be updated\n"
" # If there is a pending notification to be formatted, it is appended as\n"
" # the final argument to the STATUS_COMMAND, e.g. as $1 in default shellscript\n"
"\n"
" # dwm statusbar update\n"
" import subprocess\n"
" def update_text(text):\n"
" subprocess.call(['xsetroot', '-name', text])\n")
sys.exit(1)
else:
readconfig(curarg)
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
session_bus = dbus.SessionBus()
name = dbus.service.BusName("org.freedesktop.Notifications", session_bus)
nf = NotificationFetcher(session_bus, '/org/freedesktop/Notifications')
# We must use contexts and iterations to run threads
# http://www.jejik.com/articles/2007/01/python-gstreamer_threading_and_the_main_loop/
# Calling threads_init is not longer needed
# https://wiki.gnome.org/PyGObject/Threading
#GLib.threads_init()
context = GLib.MainLoop().get_context()
_thread.start_new_thread(message_thread, (None,))
while 1:
context.iteration(True)