1 ----------------------------------------------------------------------------
2 -- @author koniu <gkusnierz@gmail.com>
3 -- @copyright 2008 koniu
4 -- @release @AWESOME_VERSION@
5 ----------------------------------------------------------------------------
12 local capi
= { screen
= screen
,
18 local hooks
= require("awful.hooks")
19 local button
= require("awful.button")
20 local util
= require("awful.util")
21 local wibox
= require("awful.wibox")
22 local bt
= require("beautiful")
23 local layout
= require("awful.widget.layout")
25 --- Notification library
28 --- Naughty configuration - a table containing common popup settings.
30 -- @field padding Space between popups and edge of the workarea. Default: 4
31 -- @field spacing Spacing between popups. Default: 1
32 -- @field icon_dirs List of directories that will be checked by getIcon()
33 -- Default: { "/usr/share/pixmaps/", }
34 -- @field icon_formats List of formats that will be checked by getIcon()
35 -- Default: { "png", "gif" }
36 -- @field default_preset Preset to be used by default.
37 -- Default: config.presets.normal
43 config
.icon_dirs
= { "/usr/share/pixmaps/", }
44 config
.icon_formats
= { "png", "gif" }
47 --- Notification Presets - a table containing presets for different purposes
48 -- Preset is a table of any parameters available to notify()
49 -- You have to pass a reference of a preset in your notify() call to use the preset
50 -- At least the default preset named "normal" has to be defined
51 -- The presets "low", "normal" and "critical" are used for notifications over DBUS
52 -- @name config.presets
53 -- @field low The preset for notifications with low urgency level
54 -- @field normal The default preset for every notification without a preset that will also be used for normal urgency level
55 -- @field critical The preset for notifications with a critical urgency level
70 config
.default_preset
= config
.presets
.normal
72 -- DBUS Notification constants
79 --- DBUS notification to preset mapping
80 -- @name config.mapping
81 -- The first element is an object containing the filter
82 -- If the rules in the filter matches the associated preset will be applied
83 -- The rules object can contain: urgency, category, appname
84 -- The second element is the preset
87 {{urgency
= urgency
.low
}, config
.presets
.low
},
88 {{urgency
= urgency
.normal
}, config
.presets
.normal
},
89 {{urgency
= urgency
.critical
}, config
.presets
.critical
}
92 -- Counter for the notifications
93 -- Required for later access via DBUS
96 --- Index of notifications. See config table for valid 'position' values.
97 -- Each element is a table consisting of:
98 -- @field box Wibox object containing the popup
99 -- @field height Popup height
100 -- @field width Popup width
101 -- @field die Function to be executed on timeout
102 -- @field id Unique notification id based on a counter
103 -- @name notifications[screen][position]
107 for s
= 1, capi
.screen
.count() do
116 -- Evaluate desired position of the notification by index - internal
117 -- @param idx Index of the notification
118 -- @param position top_right | top_left | bottom_right | bottom_left
119 -- @param height Popup height
120 -- @param width Popup width (optional)
121 -- @return Absolute position and index in { x = X, y = Y, idx = I } table
122 local function get_offset(screen
, position
, idx
, width
, height
)
123 local ws
= wibox
.get_workarea(screen
)
125 local idx
= idx
or #notifications
[screen
][position
] + 1
126 local width
= width
or notifications
[screen
][position
][idx
].width
129 if position
:match("left") then
130 v
.x
= ws
.x
+ config
.padding
132 v
.x
= ws
.x
+ ws
.width
- (width
+ config
.padding
)
135 -- calculate existing popups' height
137 for i
= 1, idx
-1, 1 do
138 existing
= existing
+ notifications
[screen
][position
][i
].height
+ config
.spacing
142 if position
:match("top") then
143 v
.y
= ws
.y
+ config
.padding
+ existing
145 v
.y
= ws
.y
+ ws
.height
- (config
.padding
+ height
+ existing
)
148 -- if positioned outside workarea, destroy oldest popup and recalculate
149 if v
.y
+ height
> ws
.y
+ ws
.height
or v
.y
< ws
.y
then
151 destroy(notifications
[screen
][position
][1])
152 v
= get_offset(screen
, position
, idx
, width
, height
)
154 if not v
.idx
then v
.idx
= idx
end
159 -- Re-arrange notifications according to their position and index - internal
161 local function arrange(screen
)
162 for p
,pos
in pairs(notifications
[screen
]) do
163 for i
,notification
in pairs(notifications
[screen
][p
]) do
164 local offset
= get_offset(screen
, p
, i
, notification
.width
, notification
.height
)
165 notification
.box
:geometry({ x
= offset
.x
, y
= offset
.y
})
166 notification
.idx
= offset
.idx
171 --- Destroy notification by index
172 -- @param notification Notification object to be destroyed
173 -- @return True if the popup was successfully destroyed, nil otherwise
174 function destroy(notification
)
175 if notification
and notification
.box
.screen
then
176 local scr
= notification
.box
.screen
177 table.remove(notifications
[notification
.box
.screen
][notification
.position
], notification
.idx
)
178 hooks
.timer
.unregister(notification
.die
)
179 notification
.box
.screen
= nil
185 -- Get notification by ID
186 -- @param id ID of the notification
187 -- @return notification object if it was found, nil otherwise
188 local function getById(id
)
189 -- iterate the notifications to get the notfications with the correct ID
190 for s
= 1, capi
.screen
.count() do
191 for p
,pos
in pairs(notifications
[s
]) do
192 for i
,notification
in pairs(notifications
[s
][p
]) do
193 if notification
.id
== id
then
201 -- Search for an icon in specified directories with a specified format
202 -- @param icon Name of the icon
203 -- @return full path of the icon, or nil of no icon was found
204 local function getIcon(name
)
205 for d
, dir
in pairs(config
.icon_dirs
) do
206 for f
, format in pairs(config
.icon_formats
) do
207 local icon
= dir
.. name
.. "." .. format
208 if util
.file_readable(icon
) then
215 --- Create notification. args is a dictionary of (optional) arguments.
216 -- @param text Text of the notification. Default: ''
217 -- @param title Title of the notification. Default: nil
218 -- @param timeout Time in seconds after which popup expires.
219 -- Set 0 for no timeout. Default: 5
220 -- @param hover_timeout Delay in seconds after which hovered popup disappears.
222 -- @param screen Target screen for the notification. Default: 1
223 -- @param position Corner of the workarea displaying the popups.
224 -- Values: "top_right" (default), "top_left", "bottom_left", "bottom_right".
225 -- @param ontop Boolean forcing popups to display on top. Default: true
226 -- @param height Popup height. Default: nil (auto)
227 -- @param width Popup width. Default: nil (auto)
228 -- @param font Notification font. Default: beautiful.font or awesome.font
229 -- @param icon Path to icon. Default: nil
230 -- @param icon_size Desired icon size in px. Default: nil
231 -- @param fg Foreground color. Default: beautiful.fg_focus or '#ffffff'
232 -- @param bg Background color. Default: beautiful.bg_focus or '#535d6c'
233 -- @param border_width Border width. Default: 1
234 -- @param border_color Border color.
235 -- Default: beautiful.border_focus or '#535d6c'
236 -- @param run Function to run on left click. Default: nil
237 -- @param preset Table with any of the above parameters. Note: Any parameters
238 -- specified directly in args will override ones defined in the preset.
239 -- @param replaces_id Replace the notification with the given ID
240 -- @param callback function that will be called with all arguments
241 -- the notification will only be displayed if the function returns true
242 -- note: this function is only relevant to notifications sent via dbus
243 -- @usage naughty.notify({ title = "Achtung!", text = "You're idling", timeout = 0 })
244 -- @return The notification object
245 function notify(args
)
246 -- gather variables together
247 local preset
= args
.preset
or config
.default_preset
or {}
248 local timeout
= args
.timeout
or preset
.timeout
or 5
249 local icon
= args
.icon
or preset
.icon
250 local icon_size
= args
.icon_size
or preset
.icon_size
251 local text
= args
.text
or preset
.text
or ""
252 local title
= args
.title
or preset
.title
253 local screen
= args
.screen
or preset
.screen
or 1
254 local ontop
= args
.ontop
or preset
.ontop
or true
255 local width
= args
.width
or preset
.width
256 local height
= args
.height
or preset
.height
257 local hover_timeout
= args
.hover_timeout
or preset
.hover_timeout
258 local opacity
= args
.opacity
or preset
.opacity
259 local margin
= args
.margin
or preset
.margin
or "5"
260 local border_width
= args
.border_width
or preset
.border_width
or "1"
261 local position
= args
.position
or preset
.position
or "top_right"
264 local beautiful
= bt
.get()
265 local font
= args
.font
or preset
.font
or beautiful
.font
or capi
.awesome
.font
266 local fg
= args
.fg
or preset
.fg
or beautiful
.fg_normal
or '#ffffff'
267 local bg
= args
.bg
or preset
.bg
or beautiful
.bg_normal
or '#535d6c'
268 local border_color
= args
.border_color
or preset
.border_color
or beautiful
.bg_focus
or '#535d6c'
269 local notification
= {}
271 -- replace notification if needed
272 if args
.replaces_id
then
273 obj
= getById(args
.replaces_id
)
275 -- destroy this and ...
278 -- ... may use its ID
279 if args
.replaces_id
< counter
then
280 notification
.id
= args
.replaces_id
282 counter
= counter
+ 1
283 notification
.id
= counter
286 -- get a brand new ID
287 counter
= counter
+ 1
288 notification
.id
= counter
291 notification
.position
= position
293 if title
then title
= title
.. "\n" else title
= "" end
296 local die
= function () destroy(notification
) end
297 hooks
.timer
.register(timeout
, die
)
298 notification
.die
= die
300 local run
= function ()
302 args
.run(notification
)
308 local hover_destroy
= function ()
309 if hover_timeout
== 0 then die()
310 else hooks
.timer
.register(hover_timeout
, die
) end
314 local textbox
= capi
.widget({ type = "textbox", align
= "flex" })
315 textbox
:buttons(util
.table.join(button({ }, 1, run
), button({ }, 3, die
)))
316 textbox
:margin({ right
= margin
, left
= margin
, bottom
= margin
, top
= margin
})
317 textbox
.text
= string.format('<span font_desc="%s"><b>%s</b>%s</span>', font
, title
, text
)
318 if hover_timeout
then textbox
.mouse_enter
= hover_destroy
end
323 -- try to guess icon if the provided one is non-existent/readable
324 if type(icon
) == "string" and not util
.file_readable(icon
) then
328 -- if we have an icon, use it
330 iconbox
= capi
.widget({ type = "imagebox", align
= "left" })
331 iconbox
:buttons(util
.table.join(button({ }, 1, run
), button({ }, 3, die
)))
333 if type(icon
) == "string" then
334 img
= capi
.image(icon
)
339 img
= img
:crop_and_scale(0,0,img
.height
,img
.width
,icon_size
,icon_size
)
341 iconbox
.resize
= false
343 if hover_timeout
then iconbox
.mouse_enter
= hover_destroy
end
347 -- create container wibox
348 notification
.box
= capi
.wibox({ fg
= fg
,
350 border_color
= border_color
,
351 border_width
= border_width
})
353 -- calculate the height
355 if iconbox
and iconbox
:extents().height
> textbox
:extents().height
then
356 height
= iconbox
:extents().height
358 height
= textbox
:extents().height
362 -- calculate the width
364 width
= textbox
:extents().width
+ (iconbox
and iconbox
:extents().width
or 0)
367 -- crop to workarea size if too big
368 local workarea
= wibox
.get_workarea(screen
)
369 if width
> workarea
.width
- 2 * (border_width
or 0) - 2 * (config
.padding
or 0) then
370 width
= workarea
.width
- 2 * (border_width
or 0) - 2 * (config
.padding
or 0)
372 if height
> workarea
.height
- 2 * (border_width
or 0) - 2 * (config
.padding
or 0) then
373 height
= workarea
.height
- 2 * (border_width
or 0) - 2 * (config
.padding
or 0)
376 -- set size in notification object
377 notification
.height
= height
+ 2 * (border_width
or 0)
378 notification
.width
= width
+ 2 * (border_width
or 0)
380 -- position the wibox
381 local offset
= get_offset(screen
, notification
.position
, nil, notification
.width
, notification
.height
)
382 notification
.box
.ontop
= ontop
383 notification
.box
:geometry({ width
= width
,
387 notification
.box
.opacity
= opacity
388 notification
.box
.screen
= screen
389 notification
.idx
= offset
.idx
392 notification
.box
.widgets
= { iconbox
, textbox
, ["layout"] = layout
.horizontal
.leftright
}
394 -- insert the notification to the table
395 table.insert(notifications
[screen
][notification
.position
], notification
)
397 -- return the notification
401 -- DBUS/Notification support
404 capi
.dbus
.add_signal("org.freedesktop.Notifications", function (data
, appname
, replaces_id
, icon
, title
, text
, actions
, hints
, expire
)
405 args
= { preset
= { } }
406 if data
.member
== "Notify" then
420 for i
, obj
in pairs(config
.mapping
) do
421 local filter
, preset
, s
= obj
[1], obj
[2], 0
422 if (not filter
.urgency
or filter
.urgency
== hints
.urgency
) and
423 (not filter
.category
or filter
.category
== hints
.category
) and
424 (not filter
.appname
or filter
.appname
== appname
) then
425 for j
, el
in pairs(filter
) do s
= s
+ 1 end
432 if not args
.preset
.callback
or (type(args
.preset
.callback
) == "function" and
433 args
.preset
.callback(data
, appname
, replaces_id
, icon
, title
, text
, actions
, hints
, expire
)) then
436 elseif hints
.icon_data
then
437 -- icon_data is an array:
438 -- 1 -> width, 2 -> height, 3 -> rowstride, 4 -> has alpha
439 -- 5 -> bits per sample, 6 -> channels, 7 -> data
442 -- If has alpha (ARGB32)
443 if hints
.icon_data
[6] == 4 then
444 imgdata
= hints
.icon_data
[7]
445 -- If has not alpha (RGB24)
446 elseif hints
.icon_data
[6] == 3 then
448 for i
= 1, #hints
.icon_data
[7], 3 do
449 imgdata
= imgdata
.. hints
.icon_data
[7]:sub(i
, i
+ 2):reverse()
450 imgdata
= imgdata
.. string.format("%c", 255) -- alpha is 255
454 args
.icon
= capi
.image
.argb32(hints
.icon_data
[1], hints
.icon_data
[2], imgdata
)
457 if replaces_id
and replaces_id
~= "" and replaces_id
~= 0 then
458 args
.replaces_id
= replaces_id
460 if expire
and expire
> -1 then
461 args
.timeout
= expire
/ 1000
463 local id
= notify(args
).id
467 elseif data
.member
== "CloseNotification" then
468 local obj
= getById(arg1
)
472 elseif data
.member
== "GetServerInfo" or data
.member
== "GetServerInformation" then
473 -- name of notification app, name of vender, version
474 return "s", "naughty", "s", "awesome", "s", capi
.awesome
.version
:match("%d.%d")
478 capi
.dbus
.add_signal("org.freedesktop.DBus.Introspectable",
479 function (data
, text
)
480 if data
.member
== "Introspect" then
481 local xml
= [=[<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object
482 Introspection 1.0//EN"
483 "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
485 <interface name="org.freedesktop.DBus.Introspectable">
486 <method name="Introspect">
487 <arg name="data" direction="out" type="s"/>
490 <interface name="org.freedesktop.Notifications">
491 <method name="CloseNotification">
492 <arg name="id" type="u" direction="in"/>
494 <method name="Notify">
495 <arg name="app_name" type="s" direction="in"/>
496 <arg name="id" type="u" direction="in"/>
497 <arg name="icon" type="s" direction="in"/>
498 <arg name="summary" type="s" direction="in"/>
499 <arg name="body" type="s" direction="in"/>
500 <arg name="actions" type="as" direction="in"/>
501 <arg name="hints" type="a{sv}" direction="in"/>
502 <arg name="timeout" type="i" direction="in"/>
503 <arg name="return_id" type="u" direction="out"/>
505 <method name="GetServerInformation">
506 <arg name="return_name" type="s" direction="out"/>
507 <arg name="return_vendor" type="s" direction="out"/>
508 <arg name="return_version" type="s" direction="out"/>
510 <method name="GetServerInfo">
511 <arg name="return_name" type="s" direction="out"/>
512 <arg name="return_vendor" type="s" direction="out"/>
513 <arg name="return_version" type="s" direction="out"/>
521 -- listen for dbus notification requests
522 capi
.dbus
.request_name("session", "org.freedesktop.Notifications")
525 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80