Ported naughty to lua 5.2

Tested on lua 5.1: all good

Signed-off-by: Arvydas Sidorenko <asido4@gmail.com>
This commit is contained in:
Arvydas Sidorenko 2012-06-12 03:48:41 +02:00 committed by Uli Schlachter
parent 0bbd9eac0a
commit 26797ab494
2 changed files with 43 additions and 41 deletions

View file

@ -7,7 +7,7 @@ require("wibox")
-- Theme handling library
local beautiful = require("beautiful")
-- Notification library
require("naughty")
local naughty = require("naughty")
require("menubar")
-- {{{ Error handling

View file

@ -23,7 +23,7 @@ local surface = require("gears.surface")
local cairo = require("lgi").cairo
--- Notification library
module("naughty")
local naughty = {}
--- Naughty configuration - a table containing common popup settings.
-- @name config
@ -42,12 +42,12 @@ module("naughty")
-- end
-- @class table
config = {}
config.padding = 4
config.spacing = 1
config.icon_dirs = { "/usr/share/pixmaps/", }
config.icon_formats = { "png", "gif" }
config.notify_callback = nil
naughty.config = {}
naughty.config.padding = 4
naughty.config.spacing = 1
naughty.config.icon_dirs = { "/usr/share/pixmaps/", }
naughty.config.icon_formats = { "png", "gif" }
naughty.config.notify_callback = nil
--- Notification Presets - a table containing presets for different purposes
@ -61,7 +61,7 @@ config.notify_callback = nil
-- @field critical The preset for notifications with a critical urgency level
-- @class table
config.presets = {
naughty.config.presets = {
normal = {},
low = {
timeout = 5
@ -77,7 +77,7 @@ config.presets = {
-- These can optionally be overridden by specifying a preset
-- @see config.presets
-- @see notify
config.defaults = {
naughty.config.defaults = {
timeout = 5,
text = "",
screen = 1,
@ -88,7 +88,7 @@ config.defaults = {
}
-- DBUS Notification constants
urgency = {
local urgency = {
low = "\0",
normal = "\1",
critical = "\2"
@ -101,10 +101,10 @@ urgency = {
-- The rules object can contain: urgency, category, appname
-- The second element is the preset
config.mapping = {
{{urgency = urgency.low}, config.presets.low},
{{urgency = urgency.normal}, config.presets.normal},
{{urgency = urgency.critical}, config.presets.critical}
naughty.config.mapping = {
{{urgency = urgency.low}, naughty.config.presets.low},
{{urgency = urgency.normal}, naughty.config.presets.normal},
{{urgency = urgency.critical}, naughty.config.presets.critical}
}
-- Counter for the notifications
@ -124,7 +124,7 @@ local suspended = false
-- @name notifications[screen][position]
-- @class table
notifications = { suspended = { } }
local notifications = { suspended = { } }
for s = 1, capi.screen.count() do
notifications[s] = {
top_left = {},
@ -135,12 +135,12 @@ for s = 1, capi.screen.count() do
end
--- Suspend notifications
function suspend()
function naughty.suspend()
suspended = true
end
--- Resume notifications
function resume()
function naughty.resume()
suspended = false
for i, v in pairs(notifications.suspended) do
v.box.visible = true
@ -150,11 +150,11 @@ function resume()
end
--- Toggle notification state
function toggle()
function naughty.toggle()
if suspended then
resume()
naughty.resume()
else
suspend()
naughty.suspend()
end
end
@ -172,22 +172,22 @@ local function get_offset(screen, position, idx, width, height)
-- calculate x
if position:match("left") then
v.x = ws.x + config.padding
v.x = ws.x + naughty.config.padding
else
v.x = ws.x + ws.width - (width + config.padding)
v.x = ws.x + ws.width - (width + naughty.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
existing = existing + notifications[screen][position][i].height + naughty.config.spacing
end
-- calculate y
if position:match("top") then
v.y = ws.y + config.padding + existing
v.y = ws.y + naughty.config.padding + existing
else
v.y = ws.y + ws.height - (config.padding + height + existing)
v.y = ws.y + ws.height - (naughty.config.padding + height + existing)
end
-- if positioned outside workarea, destroy oldest popup and recalculate
@ -216,7 +216,7 @@ end
--- Destroy notification by notification object
-- @param notification Notification object to be destroyed
-- @return True if the popup was successfully destroyed, nil otherwise
function destroy(notification)
function naughty.destroy(notification)
if notification and notification.box.visible then
if suspended then
for k, v in pairs(notifications.suspended) do
@ -283,15 +283,15 @@ end
-- note: this function is only relevant to notifications sent via dbus
-- @usage naughty.notify({ title = "Achtung!", text = "You're idling", timeout = 0 })
-- @return The notification object
function notify(args)
if config.notify_callback then
args = config.notify_callback(args)
function naughty.notify(args)
if naughty.config.notify_callback then
args = naughty.config.notify_callback(args)
if not args then return end
end
-- gather variables together
local preset = util.table.join(config.defaults or {},
args.preset or config.presets.normal or {})
local preset = util.table.join(naughty.config.defaults or {},
args.preset or naughty.config.presets.normal or {})
local timeout = args.timeout or preset.timeout
local icon = args.icon or preset.icon
local icon_size = args.icon_size or preset.icon_size
@ -320,7 +320,7 @@ function notify(args)
local obj = getById(args.replaces_id)
if obj then
-- destroy this and ...
destroy(obj)
naughty.destroy(obj)
end
-- ... may use its ID
if args.replaces_id <= counter then
@ -340,7 +340,7 @@ function notify(args)
if title then title = title .. "\n" else title = "" end
-- hook destroy
local die = function () destroy(notification) end
local die = function () naughty.destroy(notification) end
if timeout > 0 then
local timer_die = capi.timer { timeout = timeout }
timer_die:connect_signal("timeout", die)
@ -404,7 +404,7 @@ function notify(args)
if icon then
-- try to guess icon if the provided one is non-existent/readable
if type(icon) == "string" and not util.file_readable(icon) then
icon = util.geticonpath(icon, config.icon_formats, config.icon_dirs, icon_size)
icon = util.geticonpath(icon, naughty.config.icon_formats, naughty.config.icon_dirs, icon_size)
end
-- if we have an icon, use it
@ -454,11 +454,11 @@ function notify(args)
-- crop to workarea size if too big
local workarea = capi.screen[screen].workarea
if width > workarea.width - 2 * (border_width or 0) - 2 * (config.padding or 0) then
width = workarea.width - 2 * (border_width or 0) - 2 * (config.padding or 0)
if width > workarea.width - 2 * (border_width or 0) - 2 * (naughty.config.padding or 0) then
width = workarea.width - 2 * (border_width or 0) - 2 * (naughty.config.padding or 0)
end
if height > workarea.height - 2 * (border_width or 0) - 2 * (config.padding or 0) then
height = workarea.height - 2 * (border_width or 0) - 2 * (config.padding or 0)
if height > workarea.height - 2 * (border_width or 0) - 2 * (naughty.config.padding or 0) then
height = workarea.height - 2 * (border_width or 0) - 2 * (naughty.config.padding or 0)
end
-- set size in notification object
@ -517,7 +517,7 @@ if capi.dbus then
return
end
end
for i, obj in pairs(config.mapping) do
for i, obj in pairs(naughty.config.mapping) do
local filter, preset, s = obj[1], obj[2], 0
if (not filter.urgency or filter.urgency == hints.urgency) and
(not filter.category or filter.category == hints.category) and
@ -525,7 +525,7 @@ if capi.dbus then
args.preset = util.table.join(args.preset, preset)
end
end
local preset = args.preset or config.defaults
local preset = args.preset or naughty.config.defaults
if not preset.callback or (type(preset.callback) == "function" and
preset.callback(data, appname, replaces_id, icon, title, text, actions, hints, expire)) then
if icon ~= "" then
@ -631,4 +631,6 @@ if capi.dbus then
capi.dbus.request_name("session", "org.freedesktop.Notifications")
end
return naughty
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80