1 ----------------------------------------------------------------------------
2 -- @author koniu <gkusnierz@gmail.com>
3 -- @copyright 2008 koniu
4 -- @release @AWESOME_VERSION@
5 ----------------------------------------------------------------------------
12 local capi
= { screen
= screen
,
19 local button
= require("awful.button")
20 local util
= require("awful.util")
21 local bt
= require("beautiful")
22 local layout
= require("awful.widget.layout")
24 --- Notification library
27 --- Naughty configuration - a table containing common popup settings.
29 -- @field padding Space between popups and edge of the workarea. Default: 4
30 -- @field spacing Spacing between popups. Default: 1
31 -- @field icon_dirs List of directories that will be checked by getIcon()
32 -- Default: { "/usr/share/pixmaps/", }
33 -- @field icon_formats List of formats that will be checked by getIcon()
34 -- Default: { "png", "gif" }
35 -- @field default_preset Preset to be used by default.
36 -- Default: config.presets.normal
42 config
.icon_dirs
= { "/usr/share/pixmaps/", }
43 config
.icon_formats
= { "png", "gif" }
46 --- Notification Presets - a table containing presets for different purposes
47 -- Preset is a table of any parameters available to notify()
48 -- You have to pass a reference of a preset in your notify() call to use the preset
49 -- At least the default preset named "normal" has to be defined
50 -- The presets "low", "normal" and "critical" are used for notifications over DBUS
51 -- @name config.presets
52 -- @field low The preset for notifications with low urgency level
53 -- @field normal The default preset for every notification without a preset that will also be used for normal urgency level
54 -- @field critical The preset for notifications with a critical urgency level
69 config
.default_preset
= config
.presets
.normal
71 -- DBUS Notification constants
78 --- DBUS notification to preset mapping
79 -- @name config.mapping
80 -- The first element is an object containing the filter
81 -- If the rules in the filter matches the associated preset will be applied
82 -- The rules object can contain: urgency, category, appname
83 -- The second element is the preset
86 {{urgency
= urgency
.low
}, config
.presets
.low
},
87 {{urgency
= urgency
.normal
}, config
.presets
.normal
},
88 {{urgency
= urgency
.critical
}, config
.presets
.critical
}
91 -- Counter for the notifications
92 -- Required for later access via DBUS
95 -- True if notifying is suspended
96 local suspended
= false
98 --- Index of notifications. See config table for valid 'position' values.
99 -- Each element is a table consisting of:
100 -- @field box Wibox object containing the popup
101 -- @field height Popup height
102 -- @field width Popup width
103 -- @field die Function to be executed on timeout
104 -- @field id Unique notification id based on a counter
105 -- @name notifications[screen][position]
108 notifications
= { suspended
= { } }
109 for s
= 1, capi
.screen
.count() do
118 --- Suspend notifications
123 --- Resume notifications
126 for i
, v
in pairs(notifications
.suspended
) do
128 if v
.timer
then v
.timer
:start() end
130 notifications
.suspended
= { }
133 -- Evaluate desired position of the notification by index - internal
134 -- @param idx Index of the notification
135 -- @param position top_right | top_left | bottom_right | bottom_left
136 -- @param height Popup height
137 -- @param width Popup width (optional)
138 -- @return Absolute position and index in { x = X, y = Y, idx = I } table
139 local function get_offset(screen
, position
, idx
, width
, height
)
140 local ws
= capi
.screen
[screen
].workarea
142 local idx
= idx
or #notifications
[screen
][position
] + 1
143 local width
= width
or notifications
[screen
][position
][idx
].width
146 if position
:match("left") then
147 v
.x
= ws
.x
+ config
.padding
149 v
.x
= ws
.x
+ ws
.width
- (width
+ config
.padding
)
152 -- calculate existing popups' height
154 for i
= 1, idx
-1, 1 do
155 existing
= existing
+ notifications
[screen
][position
][i
].height
+ config
.spacing
159 if position
:match("top") then
160 v
.y
= ws
.y
+ config
.padding
+ existing
162 v
.y
= ws
.y
+ ws
.height
- (config
.padding
+ height
+ existing
)
165 -- if positioned outside workarea, destroy oldest popup and recalculate
166 if v
.y
+ height
> ws
.y
+ ws
.height
or v
.y
< ws
.y
then
168 destroy(notifications
[screen
][position
][1])
169 v
= get_offset(screen
, position
, idx
, width
, height
)
171 if not v
.idx
then v
.idx
= idx
end
176 -- Re-arrange notifications according to their position and index - internal
178 local function arrange(screen
)
179 for p
,pos
in pairs(notifications
[screen
]) do
180 for i
,notification
in pairs(notifications
[screen
][p
]) do
181 local offset
= get_offset(screen
, p
, i
, notification
.width
, notification
.height
)
182 notification
.box
:geometry({ x
= offset
.x
, y
= offset
.y
})
183 notification
.idx
= offset
.idx
188 --- Destroy notification by notification object
189 -- @param notification Notification object to be destroyed
190 -- @return True if the popup was successfully destroyed, nil otherwise
191 function destroy(notification
)
192 if notification
and notification
.box
.screen
then
194 for k
, v
in pairs(notifications
.suspended
) do
195 if v
.box
== notification
.box
then
196 table.remove(notifications
.suspended
, k
)
201 local scr
= notification
.box
.screen
202 table.remove(notifications
[notification
.box
.screen
][notification
.position
], notification
.idx
)
203 if notification
.timer
then
204 notification
.timer
:stop()
206 notification
.box
.screen
= nil
212 -- Get notification by ID
213 -- @param id ID of the notification
214 -- @return notification object if it was found, nil otherwise
215 local function getById(id
)
216 -- iterate the notifications to get the notfications with the correct ID
217 for s
= 1, capi
.screen
.count() do
218 for p
,pos
in pairs(notifications
[s
]) do
219 for i
,notification
in pairs(notifications
[s
][p
]) do
220 if notification
.id
== id
then
228 -- Search for an icon in specified directories with a specified format
229 -- @param icon Name of the icon
230 -- @return full path of the icon, or nil of no icon was found
231 local function getIcon(name
)
232 for d
, dir
in pairs(config
.icon_dirs
) do
233 for f
, format in pairs(config
.icon_formats
) do
234 local icon
= dir
.. name
.. "." .. format
235 if util
.file_readable(icon
) then
242 --- Create notification. args is a dictionary of (optional) arguments.
243 -- @param text Text of the notification. Default: ''
244 -- @param title Title of the notification. Default: nil
245 -- @param timeout Time in seconds after which popup expires.
246 -- Set 0 for no timeout. Default: 5
247 -- @param hover_timeout Delay in seconds after which hovered popup disappears.
249 -- @param screen Target screen for the notification. Default: 1
250 -- @param position Corner of the workarea displaying the popups.
251 -- Values: "top_right" (default), "top_left", "bottom_left", "bottom_right".
252 -- @param ontop Boolean forcing popups to display on top. Default: true
253 -- @param height Popup height. Default: nil (auto)
254 -- @param width Popup width. Default: nil (auto)
255 -- @param font Notification font. Default: beautiful.font or awesome.font
256 -- @param icon Path to icon. Default: nil
257 -- @param icon_size Desired icon size in px. Default: nil
258 -- @param fg Foreground color. Default: beautiful.fg_focus or '#ffffff'
259 -- @param bg Background color. Default: beautiful.bg_focus or '#535d6c'
260 -- @param border_width Border width. Default: 1
261 -- @param border_color Border color.
262 -- Default: beautiful.border_focus or '#535d6c'
263 -- @param run Function to run on left click. Default: nil
264 -- @param preset Table with any of the above parameters. Note: Any parameters
265 -- specified directly in args will override ones defined in the preset.
266 -- @param replaces_id Replace the notification with the given ID
267 -- @param callback function that will be called with all arguments
268 -- the notification will only be displayed if the function returns true
269 -- note: this function is only relevant to notifications sent via dbus
270 -- @usage naughty.notify({ title = "Achtung!", text = "You're idling", timeout = 0 })
271 -- @return The notification object
272 function notify(args
)
273 -- gather variables together
274 local preset
= args
.preset
or config
.default_preset
or {}
275 local timeout
= args
.timeout
or preset
.timeout
or 5
276 local icon
= args
.icon
or preset
.icon
277 local icon_size
= args
.icon_size
or preset
.icon_size
278 local text
= args
.text
or preset
.text
or ""
279 local title
= args
.title
or preset
.title
280 local screen
= args
.screen
or preset
.screen
or 1
281 local ontop
= args
.ontop
or preset
.ontop
or true
282 local width
= args
.width
or preset
.width
283 local height
= args
.height
or preset
.height
284 local hover_timeout
= args
.hover_timeout
or preset
.hover_timeout
285 local opacity
= args
.opacity
or preset
.opacity
286 local margin
= args
.margin
or preset
.margin
or "5"
287 local border_width
= args
.border_width
or preset
.border_width
or "1"
288 local position
= args
.position
or preset
.position
or "top_right"
291 local beautiful
= bt
.get()
292 local font
= args
.font
or preset
.font
or beautiful
.font
or capi
.awesome
.font
293 local fg
= args
.fg
or preset
.fg
or beautiful
.fg_normal
or '#ffffff'
294 local bg
= args
.bg
or preset
.bg
or beautiful
.bg_normal
or '#535d6c'
295 local border_color
= args
.border_color
or preset
.border_color
or beautiful
.bg_focus
or '#535d6c'
296 local notification
= {}
298 -- replace notification if needed
299 if args
.replaces_id
then
300 obj
= getById(args
.replaces_id
)
302 -- destroy this and ...
305 -- ... may use its ID
306 if args
.replaces_id
< counter
then
307 notification
.id
= args
.replaces_id
309 counter
= counter
+ 1
310 notification
.id
= counter
313 -- get a brand new ID
314 counter
= counter
+ 1
315 notification
.id
= counter
318 notification
.position
= position
320 if title
then title
= title
.. "\n" else title
= "" end
323 local die
= function () destroy(notification
) end
325 local timer_die
= capi
.timer
{ timeout
= timeout
}
326 timer_die
:add_signal("timeout", die
)
327 if not suspended
then
330 notification
.timer
= timer_die
332 notification
.die
= die
334 local run
= function ()
336 args
.run(notification
)
342 local hover_destroy
= function ()
343 if hover_timeout
== 0 then
346 if notification
.timer
then notification
.timer
:stop() end
347 notification
.timer
= capi
.timer
{ timeout
= hover_timeout
}
348 notification
.timer
:add_signal("timeout", die
)
349 notification
.timer
:start()
354 local textbox
= capi
.widget({ type = "textbox", align
= "flex" })
355 textbox
:buttons(util
.table.join(button({ }, 1, run
), button({ }, 3, die
)))
356 layout
.margins
[textbox
] = { right
= margin
, left
= margin
, bottom
= margin
, top
= margin
}
357 textbox
.text
= string.format('<span font_desc="%s"><b>%s</b>%s</span>', font
, title
, text
:gsub("<br.->", "\n"))
358 textbox
.valign
= "middle"
363 -- try to guess icon if the provided one is non-existent/readable
364 if type(icon
) == "string" and not util
.file_readable(icon
) then
368 -- if we have an icon, use it
370 iconbox
= capi
.widget({ type = "imagebox", align
= "left" })
371 layout
.margins
[iconbox
] = { right
= margin
, left
= margin
, bottom
= margin
, top
= margin
}
372 iconbox
:buttons(util
.table.join(button({ }, 1, run
), button({ }, 3, die
)))
374 if type(icon
) == "string" then
375 img
= capi
.image(icon
)
380 img
= img
:crop_and_scale(0,0,img
.height
,img
.width
,icon_size
,icon_size
)
382 iconbox
.resize
= false
387 -- create container wibox
388 notification
.box
= capi
.wibox({ fg
= fg
,
390 border_color
= border_color
,
391 border_width
= border_width
})
393 if hover_timeout
then notification
.box
:add_signal("mouse::enter", hover_destroy
) end
395 -- calculate the height
397 if iconbox
and iconbox
:extents().height
+ 2 * margin
> textbox
:extents().height
+ 2 * margin
then
398 height
= iconbox
:extents().height
+ 2 * margin
400 height
= textbox
:extents().height
+ 2 * margin
404 -- calculate the width
406 width
= textbox
:extents().width
+ (iconbox
and iconbox
:extents().width
+ 2 * margin
or 0) + 2 * margin
409 -- crop to workarea size if too big
410 local workarea
= capi
.screen
[screen
].workarea
411 if width
> workarea
.width
- 2 * (border_width
or 0) - 2 * (config
.padding
or 0) then
412 width
= workarea
.width
- 2 * (border_width
or 0) - 2 * (config
.padding
or 0)
414 if height
> workarea
.height
- 2 * (border_width
or 0) - 2 * (config
.padding
or 0) then
415 height
= workarea
.height
- 2 * (border_width
or 0) - 2 * (config
.padding
or 0)
418 -- set size in notification object
419 notification
.height
= height
+ 2 * (border_width
or 0)
420 notification
.width
= width
+ 2 * (border_width
or 0)
422 -- position the wibox
423 local offset
= get_offset(screen
, notification
.position
, nil, notification
.width
, notification
.height
)
424 notification
.box
.ontop
= ontop
425 notification
.box
:geometry({ width
= width
,
429 notification
.box
.opacity
= opacity
430 notification
.box
.screen
= screen
431 notification
.idx
= offset
.idx
434 notification
.box
.widgets
= { iconbox
, textbox
, ["layout"] = layout
.horizontal
.leftright
}
436 -- insert the notification to the table
437 table.insert(notifications
[screen
][notification
.position
], notification
)
440 notification
.box
.visible
= false
441 table.insert(notifications
.suspended
, notification
)
444 -- return the notification
448 -- DBUS/Notification support
451 capi
.dbus
.add_signal("org.freedesktop.Notifications", function (data
, appname
, replaces_id
, icon
, title
, text
, actions
, hints
, expire
)
452 args
= { preset
= { } }
453 if data
.member
== "Notify" then
467 for i
, obj
in pairs(config
.mapping
) do
468 local filter
, preset
, s
= obj
[1], obj
[2], 0
469 if (not filter
.urgency
or filter
.urgency
== hints
.urgency
) and
470 (not filter
.category
or filter
.category
== hints
.category
) and
471 (not filter
.appname
or filter
.appname
== appname
) then
472 for j
, el
in pairs(filter
) do s
= s
+ 1 end
479 if not args
.preset
.callback
or (type(args
.preset
.callback
) == "function" and
480 args
.preset
.callback(data
, appname
, replaces_id
, icon
, title
, text
, actions
, hints
, expire
)) then
483 elseif hints
.icon_data
then
484 -- icon_data is an array:
485 -- 1 -> width, 2 -> height, 3 -> rowstride, 4 -> has alpha
486 -- 5 -> bits per sample, 6 -> channels, 7 -> data
489 -- If has alpha (ARGB32)
490 if hints
.icon_data
[6] == 4 then
491 imgdata
= hints
.icon_data
[7]
492 -- If has not alpha (RGB24)
493 elseif hints
.icon_data
[6] == 3 then
495 for i
= 1, #hints
.icon_data
[7], 3 do
496 imgdata
= imgdata
.. hints
.icon_data
[7]:sub(i
, i
+ 2):reverse()
497 imgdata
= imgdata
.. string.format("%c", 255) -- alpha is 255
501 args
.icon
= capi
.image
.argb32(hints
.icon_data
[1], hints
.icon_data
[2], imgdata
)
504 if replaces_id
and replaces_id
~= "" and replaces_id
~= 0 then
505 args
.replaces_id
= replaces_id
507 if expire
and expire
> -1 then
508 args
.timeout
= expire
/ 1000
510 local id
= notify(args
).id
514 elseif data
.member
== "CloseNotification" then
515 local obj
= getById(appname
)
519 elseif data
.member
== "GetServerInfo" or data
.member
== "GetServerInformation" then
520 -- name of notification app, name of vender, version
521 return "s", "naughty", "s", "awesome", "s", capi
.awesome
.version
:match("%d.%d"), "s", "1.0"
522 elseif data
.member
== "GetCapabilities" then
523 -- We actually do display the body of the message, we support <b>, <i>
524 -- and <u> in the body and we handle static (non-animated) icons.
525 return "as", { "s", "body", "s", "body-markup", "s", "icon-static" }
529 capi
.dbus
.add_signal("org.freedesktop.DBus.Introspectable",
530 function (data
, text
)
531 if data
.member
== "Introspect" then
532 local xml
= [=[<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object
533 Introspection 1.0//EN"
534 "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
536 <interface name="org.freedesktop.DBus.Introspectable">
537 <method name="Introspect">
538 <arg name="data" direction="out" type="s"/>
541 <interface name="org.freedesktop.Notifications">
542 <method name="GetCapabilities">
543 <arg name="caps" type="as" direction="out"/>
545 <method name="CloseNotification">
546 <arg name="id" type="u" direction="in"/>
548 <method name="Notify">
549 <arg name="app_name" type="s" direction="in"/>
550 <arg name="id" type="u" direction="in"/>
551 <arg name="icon" type="s" direction="in"/>
552 <arg name="summary" type="s" direction="in"/>
553 <arg name="body" type="s" direction="in"/>
554 <arg name="actions" type="as" direction="in"/>
555 <arg name="hints" type="a{sv}" direction="in"/>
556 <arg name="timeout" type="i" direction="in"/>
557 <arg name="return_id" type="u" direction="out"/>
559 <method name="GetServerInformation">
560 <arg name="return_name" type="s" direction="out"/>
561 <arg name="return_vendor" type="s" direction="out"/>
562 <arg name="return_version" type="s" direction="out"/>
563 <arg name="return_spec_version" type="s" direction="out"/>
565 <method name="GetServerInfo">
566 <arg name="return_name" type="s" direction="out"/>
567 <arg name="return_vendor" type="s" direction="out"/>
568 <arg name="return_version" type="s" direction="out"/>
576 -- listen for dbus notification requests
577 capi
.dbus
.request_name("session", "org.freedesktop.Notifications")
580 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80