1 ----------------------------------------------------------------------------
2 -- @author koniu <gkusnierz@gmail.com>
3 -- @copyright 2008 koniu
4 -- @release @AWESOME_VERSION@
5 ----------------------------------------------------------------------------
13 local hooks
= require("awful.hooks")
16 local button
= require("awful.button")
17 local util
= require("awful.util")
18 local capi
= { screen
= screen
, awesome
= awesome
}
19 local bt
= require("beautiful")
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 --- Index of notifications. See config table for valid 'position' values.
96 -- Each element is a table consisting of:
97 -- @field box Wibox object containing the popup
98 -- @field height Popup height
99 -- @field width Popup width
100 -- @field die Function to be executed on timeout
101 -- @field id Unique notification id based on a counter
102 -- @name notifications[screen][position]
106 for s
= 1, screen
.count() do
115 -- Evaluate desired position of the notification by index - internal
116 -- @param idx Index of the notification
117 -- @param position top_right | top_left | bottom_right | bottom_left
118 -- @param height Popup height
119 -- @param width Popup width (optional)
120 -- @return Absolute position and index in { x = X, y = Y, idx = I } table
121 local function get_offset(screen
, position
, idx
, width
, height
)
122 local ws
= capi
.screen
[screen
].workarea
124 local idx
= idx
or #notifications
[screen
][position
] + 1
125 local width
= width
or notifications
[screen
][position
][idx
].width
128 if position
:match("left") then
129 v
.x
= ws
.x
+ config
.padding
131 v
.x
= ws
.x
+ ws
.width
- (width
+ config
.padding
)
134 -- calculate existing popups' height
136 for i
= 1, idx
-1, 1 do
137 existing
= existing
+ notifications
[screen
][position
][i
].height
+ config
.spacing
141 if position
:match("top") then
142 v
.y
= ws
.y
+ config
.padding
+ existing
144 v
.y
= ws
.y
+ ws
.height
- (config
.padding
+ height
+ existing
)
147 -- if positioned outside workarea, destroy oldest popup and recalculate
148 if v
.y
+ height
> ws
.y
+ ws
.height
or v
.y
< ws
.y
then
150 destroy(notifications
[screen
][position
][1])
151 v
= get_offset(screen
, position
, idx
, width
, height
)
153 if not v
.idx
then v
.idx
= idx
end
158 -- Re-arrange notifications according to their position and index - internal
160 local function arrange(screen
)
161 for p
,pos
in pairs(notifications
[screen
]) do
162 for i
,notification
in pairs(notifications
[screen
][p
]) do
163 local offset
= get_offset(screen
, p
, i
, notification
.width
, notification
.height
)
164 notification
.box
:geometry({ x
= offset
.x
, y
= offset
.y
, width
= notification
.width
, height
= notification
.height
})
165 notification
.idx
= offset
.idx
170 --- Destroy notification by index
171 -- @param notification Notification object to be destroyed
172 -- @return True if the popup was successfully destroyed, nil otherwise
173 function destroy(notification
)
174 if notification
and notification
.box
.screen
then
175 local scr
= notification
.box
.screen
176 table.remove(notifications
[notification
.box
.screen
][notification
.position
], notification
.idx
)
177 hooks
.timer
.unregister(notification
.die
)
178 notification
.box
.screen
= nil
184 -- Get notification by ID
185 -- @param id ID of the notification
186 -- @return notification object if it was found, nil otherwise
187 local function getById(id
)
188 -- iterate the notifications to get the notfications with the correct ID
189 for s
= 1, screen
.count() do
190 for p
,pos
in pairs(notifications
[s
]) do
191 for i
,notification
in pairs(notifications
[s
][p
]) do
192 if notification
.id
== id
then
200 -- Search for an icon in specified directories with a specified format
201 -- @param icon Name of the icon
202 -- @return full path of the icon, or nil of no icon was found
203 local function getIcon(name
)
204 for d
, dir
in pairs(config
.icon_dirs
) do
205 for f
, format in pairs(config
.icon_formats
) do
206 local icon
= dir
.. name
.. "." .. format
207 if awful
.util
.file_readable(icon
) then
214 --- Create notification. args is a dictionary of (optional) arguments.
215 -- @param text Text of the notification. Default: ''
216 -- @param title Title of the notification. Default: nil
217 -- @param timeout Time in seconds after which popup expires.
218 -- Set 0 for no timeout. Default: 5
219 -- @param hover_timeout Delay in seconds after which hovered popup disappears.
221 -- @param screen Target screen for the notification. Default: 1
222 -- @param position Corner of the workarea displaying the popups.
223 -- Values: "top_right" (default), "top_left", "bottom_left", "bottom_right".
224 -- @param ontop Boolean forcing popups to display on top. Default: true
225 -- @param height Popup height. Default: nil (auto)
226 -- @param width Popup width. Default: nil (auto)
227 -- @param font Notification font. Default: beautiful.font or awesome.font
228 -- @param icon Path to icon. Default: nil
229 -- @param icon_size Desired icon size in px. Default: nil
230 -- @param fg Foreground color. Default: beautiful.fg_focus or '#ffffff'
231 -- @param bg Background color. Default: beautiful.bg_focus or '#535d6c'
232 -- @param border_width Border width. Default: 1
233 -- @param border_color Border color.
234 -- Default: beautiful.border_focus or '#535d6c'
235 -- @param run Function to run on left click. Default: nil
236 -- @param preset Table with any of the above parameters. Note: Any parameters
237 -- specified directly in args will override ones defined in the preset.
238 -- @param replaces_id Replace the notification with the given ID
239 -- @param callback function that will be called with all arguments
240 -- the notification will only be displayed if the function returns true
241 -- note: this function is only relevant to notifications sent via dbus
242 -- @usage naughty.notify({ title = "Achtung!", text = "You're idling", timeout = 0 })
243 -- @return The notification object
244 function notify(args
)
245 -- gather variables together
246 local preset
= args
.preset
or config
.default_preset
or {}
247 local timeout
= args
.timeout
or preset
.timeout
or 5
248 local icon
= args
.icon
or preset
.icon
249 local icon_size
= args
.icon_size
or preset
.icon_size
250 local text
= args
.text
or preset
.text
or ""
251 local title
= args
.title
or preset
.title
252 local screen
= args
.screen
or preset
.screen
or 1
253 local ontop
= args
.ontop
or preset
.ontop
or true
254 local width
= args
.width
or preset
.width
255 local height
= args
.height
or preset
.height
256 local hover_timeout
= args
.hover_timeout
or preset
.hover_timeout
257 local opacity
= args
.opacity
or preset
.opacity
258 local margin
= args
.margin
or preset
.margin
or "5"
259 local border_width
= args
.border_width
or preset
.border_width
or "1"
260 local position
= args
.position
or preset
.position
or "top_right"
263 local beautiful
= bt
.get()
264 local font
= args
.font
or preset
.font
or beautiful
.font
or awesome
.font
265 local fg
= args
.fg
or preset
.fg
or beautiful
.fg_normal
or '#ffffff'
266 local bg
= args
.bg
or preset
.bg
or beautiful
.bg_normal
or '#535d6c'
267 local border_color
= args
.border_color
or preset
.border_color
or beautiful
.bg_focus
or '#535d6c'
268 local notification
= {}
270 -- replace notification if needed
271 if args
.replaces_id
then
272 obj
= getById(args
.replaces_id
)
274 -- destroy this and ...
277 -- ... may use its ID
278 if args
.replaces_id
< counter
then
279 notification
.id
= args
.replaces_id
281 counter
= counter
+ 1
282 notification
.id
= counter
285 -- get a brand new ID
286 counter
= counter
+ 1
287 notification
.id
= counter
290 notification
.position
= position
292 if title
then title
= title
.. "\n" else title
= "" end
295 local die
= function () destroy(notification
) end
296 hooks
.timer
.register(timeout
, die
)
297 notification
.die
= die
299 local run
= function ()
301 args
.run(notification
)
307 local hover_destroy
= function ()
308 if hover_timeout
== 0 then die()
309 else hooks
.timer
.register(hover_timeout
, die
) end
313 local textbox
= widget({ type = "textbox", align
= "flex" })
314 textbox
:buttons(util
.table.join(button({ }, 1, run
), button({ }, 3, die
)))
315 textbox
:margin({ right
= margin
, left
= margin
, bottom
= 2 * margin
})
316 textbox
.text
= string.format('<span font_desc="%s"><b>%s</b>%s</span>', font
, title
, text
)
317 if hover_timeout
then textbox
.mouse_enter
= hover_destroy
end
322 -- try to guess icon if the provided one is non-existent/readable
323 if type(icon
) == "string" and not awful
.util
.file_readable(icon
) then
327 -- if we have an icon, use it
329 iconbox
= widget({ type = "imagebox", align
= "left" })
330 iconbox
:buttons(util
.table.join(button({ }, 1, run
), button({ }, 3, die
)))
332 if type(icon
) == "string" then
338 img
= img
:crop_and_scale(0,0,img
.height
,img
.width
,icon_size
,icon_size
)
340 iconbox
.resize
= false
342 iconbox
.valign
= "center"
343 if hover_timeout
then iconbox
.mouse_enter
= hover_destroy
end
347 -- create container wibox
348 notification
.box
= wibox({ position
= "floating",
351 border_color
= border_color
,
352 border_width
= border_width
})
354 -- calculate the height
356 if iconbox
and iconbox
:extents().height
> textbox
:extents().height
then
357 height
= iconbox
:extents().height
359 height
= textbox
:extents().height
363 -- calculate the width
365 width
= textbox
:extents().width
+ (iconbox
and iconbox
:extents().width
or 0)
368 -- crop to workarea size if too big
369 if width
> capi
.screen
[screen
].workarea
.width
- 2 * (border_width
or 0) - 2 * (config
.padding
or 0) then
370 width
= capi
.screen
[screen
].workarea
.width
- 2 * (border_width
or 0) - 2 * (config
.padding
or 0)
372 if height
> capi
.screen
[screen
].workarea
.height
- 2 * (border_width
or 0) - 2 * (config
.padding
or 0) then
373 height
= capi
.screen
[screen
].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
:geometry({ width
= width
,
386 notification
.box
.ontop
= ontop
387 notification
.box
.opacity
= opacity
388 notification
.box
.screen
= screen
389 notification
.idx
= offset
.idx
392 notification
.box
.widgets
= { iconbox
, textbox
}
394 -- insert the notification to the table
395 table.insert(notifications
[screen
][notification
.position
], notification
)
397 -- return the notification
401 -- DBUS/Notification support
403 if awful
.hooks
.dbus
then
404 awful
.hooks
.dbus
.register("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
= 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 awful
.hooks
.dbus
.register("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 dbus
.request_name("session", "org.freedesktop.Notifications")
525 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80