awesome/lib/naughty.lua.in

405 lines
14 KiB
Lua

----------------------------------------------------------------------------
-- @author koniu <gkusnierz@gmail.com>
-- @copyright 2008 koniu
-- @release @AWESOME_VERSION@
----------------------------------------------------------------------------
-- Package environment
local pairs = pairs
local table = table
local wibox = wibox
local image = image
local hooks = require("awful.hooks")
local string = string
local widget = widget
local button = button
local capi = { screen = screen }
local bt = require("beautiful")
local screen = screen
local awful = awful
local dbus = dbus
--- Notification library
module("naughty")
--- Naughty configuration - a table containing common/default popup settings.
-- You can override some of these for individual popups using args to notify().
-- @name config
-- @field timeout Number of seconds after which popups disappear.
-- Set to 0 for no timeout. Default: 5
-- @field screen Screen on which the popups will appear number. Default: 1
-- @field position Corner of the workarea the popups will appear.
-- Valid values: 'top_right', 'top_left', 'bottom_right', 'bottom_left'.
-- Default: 'top_right'
-- @field padding Space between popups and edge of the workarea. Default: 4
-- @field height Height of a single line of text. Default: 16
-- @field width Width of a popup. Default: 300
-- @field spacing Spacing between popups. Default: 1
-- @field ontop Boolean forcing popups to display on top. Default: true
-- @field margin Space between popup edge and content. Default: 10
-- @field font Popup font. Default: beautiful.font or "Verdana 8"
-- @field icon Popup icon. Default: nil
-- @field icon_size Size of the icon in pixels. Default: nil
-- @field icon_dirs List of directories that will be checked by getIcon()
-- Default: { "/usr/share/pixmaps/", }
-- @field icon_formats List of formats that will be checked by getIcon()
-- Default: { "png", "gif" }
-- @field fg Foreground color. Default: beautiful.fg_focus or '#ffffff'
-- @field bg Background color. Default: beautiful.bg_focus or '#535d6c'
-- @field border_color Border color.
-- Default: beautiful.border_focus or '#535d6c'
-- @field border_width Border width. Default: 1
-- @field hover_timeout Delay in seconds after which hovered popup disappears.
-- Default: nil
-- @class table
config = {}
config.timeout = 5
config.screen = 1
config.position = "top_right"
config.padding = 4
config.height = 16
config.width = 300
config.spacing = 1
config.ontop = true
config.margin = 10
config.icon = nil
config.icon_size = nil
config.icon_dirs = { "/usr/share/pixmaps/", }
config.icon_formats = { "png", "gif" }
config.border_width = 1
config.hover_timeout = nil
-- Counter for the notifications
-- Required for later access via DBUS
local counter = 1
--- Index of notifications. See config table for valid 'position' values.
-- Each element is a table consisting of:
-- @field box Wibox object containing the popup
-- @field height Popup height
-- @field width Popup width
-- @field die Function to be executed on timeout
-- @field id Unique notification id based on a counter
-- @name notifications[position]
-- @class table
notifications = {}
for s = 1, screen.count() do
notifications[s] = {
top_left = {},
top_right = {},
bottom_left = {},
bottom_right = {},
}
end
--- Evaluate desired position of the notification by index - internal
-- @param idx Index of the notification
-- @param position top_right | top_left | bottom_right | bottom_left
-- @param height Popup height
-- @param width Popup width (optional)
-- @return Absolute position in {x, y} dictionary
local function get_offset(screen, position, idx, width, height)
local ws = capi.screen[screen].workarea
local v = {}
width = width or notifications[screen][position][idx].width or config.width
-- calculate x
if position:match("left") then
v.x = ws.x + config.padding
else
v.x = ws.x + ws.width - (width + config.border_width*2 + config.padding)
end
-- calculate existing popups' height
local existing = 0
for i = 1, idx-1, 1 do
existing = existing + notifications[screen][position][i].height + config.spacing + config.border_width*2
end
-- calculate y
if position:match("top") then
v.y = ws.y + config.padding + existing
else
v.y = ws.y + ws.height - (config.padding + config.border_width*2 + height + existing)
end
-- if positioned outside workarea, destroy oldest popup and recalculate
if v.y + height > ws.y + ws.height or v.y < ws.y then
idx = idx - 1
destroy(notifications[screen][position][1])
v = get_offset(screen, position, idx, width, height)
end
return v
end
--- Re-arrange notifications according to their position and index - internal
-- @return None
local function arrange(screen)
for p,pos in pairs(notifications[screen]) do
for i,notification in pairs(notifications[screen][p]) do
local offset = get_offset(screen, p, i, notification.width, notification.height)
notification.box:geometry({ x = offset.x, y = offset.y, width = notification.width, height = notification.height })
notification.idx = i
end
end
end
--- Destroy notification by index
-- @param notification Notification object to be destroyed
-- @return True if the popup was successfully destroyed, nil otherwise
function destroy(notification)
if notification and notification.box.screen then
local scr = notification.box.screen
table.remove(notifications[notification.box.screen][notification.position], notification.idx)
hooks.timer.unregister(notification.die)
notification.box.screen = nil
arrange(scr)
return true
end
end
--- Get notification by ID
-- @param id ID of the notification
-- @return notification object if it was found, nil otherwise
local function getById(id)
-- iterate the notifications to get the notfications with the correct ID
for s = 1, screen.count() do
for p,pos in pairs(notifications[s]) do
for i,notification in pairs(notifications[s][p]) do
if notification.id == id then
return notification
end
end
end
end
end
--- Search for an icon in specified directories with a specified format
-- @param icon Name of the icon
-- @return full path of the icon, or nil of no icon was found
local function getIcon(name)
for d, dir in pairs(config.icon_dirs) do
for f, format in pairs(config.icon_formats) do
local icon = dir .. name .. "." .. format
if awful.util.file_readable(icon) then
return icon
end
end
end
end
--- Create notification. args is a dictionary of optional arguments. For more information and defaults see respective fields in config table.
-- @param text Text of the notification
-- @param timeout Time in seconds after which popup expires
-- @param title Title of the notification
-- @param position Corner of the workarea the popups will appear
-- @param icon Path to icon
-- @param icon_size Desired icon size in px
-- @param fg Foreground color
-- @param bg Background color
-- @param screen Target screen for the notification
-- @param ontop Target screen for the notification
-- @param run Function to run on left click
-- @param width The popup width
-- @field hover_timeout Delay in seconds after which hovered popup disappears.
-- @param replaces_id Replace the notification with the given ID
-- @usage naughty.notify({ title = 'Achtung!', text = 'You\'re idling', timeout = 0 })
-- @return The notification object
function notify(args)
-- gather variables together
local timeout = args.timeout or config.timeout
local icon = args.icon or config.icon
local icon_size = args.icon_size or config.icon_size
local text = args.text or ""
local screen = args.screen or config.screen
local ontop = args.ontop or config.ontop
local width = args.width or config.width
local hover_timeout = args.hover_timeout or config.hover_timeout
-- beautiful
local beautiful = bt.get()
local font = args.font or config.font or beautiful.font or "Verdana 8"
local fg = args.fg or config.fg or beautiful.fg_normal or '#ffffff'
local bg = args.bg or config.bg or beautiful.bg_normal or '#535d6c'
local border_color = config.border_color or beautiful.bg_focus or '#535d6c'
local notification = {}
-- replace notification if needed
if args.replaces_id then
obj = getById(args.replaces_id)
if obj then
-- destroy this and ...
destroy(obj)
end
-- ... may use its ID
if args.replaces_id < counter then
notification.id = args.replaces_id
else
counter = counter + 1
notification.id = counter
end
else
-- get a brand new ID
counter = counter + 1
notification.id = counter
end
notification.position = args.position or config.position
notification.idx = #notifications[screen][notification.position] + 1
local title = ""
if args.title then title = args.title .. "\n" end
-- hook destroy
local die = function () destroy(notification) end
hooks.timer.register(timeout, die)
notification.die = die
local run = function ()
if args.run then args.run() end
die()
end
local hover_destroy = function ()
if hover_timeout == 0 then die()
else hooks.timer.register(hover_timeout, die) end
end
-- create textbox
local textbox = widget({ type = "textbox", align = "flex" })
textbox:buttons({ button({ }, 1, run),
button({ }, 3, die) })
textbox.text = string.format('<margin right="'..config.margin..'" left="'..config.margin..'"/><span font_desc="%s"><b>%s</b>%s</span>', font, title, text)
if hover_timeout then textbox.mouse_enter = hover_destroy end
-- create iconbox
local iconbox = nil
if icon then
-- try to guess icon if the provided one is non-existent/readable
if not awful.util.file_readable(icon) then icon = getIcon(icon) end
-- if we have an icon, use it
if icon then
iconbox = widget({ type = "imagebox", align = "left" })
iconbox:buttons({ button({ }, 1, run), button({ }, 3, die) })
local img = image(icon)
if icon_size then
img = img:crop_and_scale(0,0,img.height,img.width,icon_size,icon_size)
end
iconbox.resize = false
iconbox.image = img
if hover_timeout then iconbox.mouse_enter = hover_destroy end
end
end
-- create container wibox
notification.box = wibox({ position = "floating",
fg = fg,
bg = bg,
border_color = config.border_color,
border_width = config.border_width })
-- position the wibox
local lines = 1; for i in string.gmatch(title..text, "\n") do lines = lines + 1 end
if iconbox and iconbox.image.height > lines * config.height then
notification.height = iconbox.image.height
else
notification.height = lines * config.height end
notification.width = width
local offset = get_offset(screen, notification.position, notification.idx, notification.width, notification.height)
notification.box:geometry({ width = notification.width,
height = notification.height,
x = offset.x,
y = offset.y })
notification.box.ontop = ontop
notification.box.screen = screen
-- populate widgets
notification.box.widgets = { iconbox, textbox }
-- insert the notification to the table
table.insert(notifications[screen][notification.position], notification)
-- return the notification
return notification
end
-- DBUS/Notification support
-- Notify
awful.hooks.dbus.register("org.freedesktop.Notifications", function (data, arg1, replaces_id, icon, title, text)
args = {}
if data.member == "Notify" then
if text ~= "" then
args.text = text
if title ~= "" then
args.title = title
end
else
if title ~= "" then
args.text = title
else
return nil
end
end
if icon ~= "" then
args.icon = icon
end
if replaces_id and replaces_id ~= "" and replaces_id ~= 0 then
args.replaces_id = replaces_id
end
local id = notify(args).id
return "i", id
elseif data.member == "CloseNotification" then
local obj = getById(arg1)
if obj then
destroy(obj)
end
elseif data.member == "GetServerInfo" or data.member == "GetServerInformation" then
-- name of notification app, name of project, version of notification app, project version
return "s", "naughty", "s", "awesome", "s", "1337", "s", "1337"
end
end)
awful.hooks.dbus.register("org.freedesktop.DBus.Introspectable",
function (data, text)
if data.member == "Introspect" then
local xml = [=[<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object
Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
<interface name="org.freedesktop.DBus.Introspectable">
<method name="Introspect">
<arg name="data" direction="out" type="s"/>
</method>
</interface>
<interface name="org.freedesktop.Notifications">
<method name="CloseNotification">
<arg name="id" type="u" direction="in"/>
</method>
<method name="Notify">
<arg name="app_name" type="s" direction="in"/>
<arg name="id" type="u" direction="in"/>
<arg name="icon" type="s" direction="in"/>
<arg name="summary" type="s" direction="in"/>
<arg name="body" type="s" direction="in"/>
<arg name="actions" type="as" direction="in"/>
<arg name="hints" type="a{sv}" direction="in"/>
<arg name="timeout" type="i" direction="in"/>
<arg name="return_id" type="u" direction="out"/>
</method>
</interface>
</node>]=]
return "s", xml
end
end)
-- listen for dbus notification requests
dbus.request_name("org.freedesktop.Notifications")
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80