2012-03-08 20:24:21 +01:00
|
|
|
---------------------------------------------------------------------------
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Utility module for menubar
|
2014-05-19 13:37:13 +02:00
|
|
|
--
|
2012-03-08 20:24:21 +01:00
|
|
|
-- @author Antonio Terceiro
|
|
|
|
-- @copyright 2009, 2011-2012 Antonio Terceiro, Alexander Yakushev
|
|
|
|
-- @release @AWESOME_VERSION@
|
2014-05-19 13:37:13 +02:00
|
|
|
-- @module menubar.utils
|
2012-03-08 20:24:21 +01:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Grab environment
|
|
|
|
local io = io
|
|
|
|
local table = table
|
|
|
|
local ipairs = ipairs
|
|
|
|
local string = string
|
2016-02-26 18:25:55 +01:00
|
|
|
local screen = screen
|
2012-03-08 20:24:21 +01:00
|
|
|
local awful_util = require("awful.util")
|
|
|
|
local theme = require("beautiful")
|
2015-02-05 19:42:49 +01:00
|
|
|
local glib = require("lgi").GLib
|
2015-02-19 22:11:38 +01:00
|
|
|
local wibox = require("wibox")
|
2012-03-08 20:24:21 +01:00
|
|
|
|
2012-06-12 10:36:28 +02:00
|
|
|
local utils = {}
|
2012-03-08 20:24:21 +01:00
|
|
|
|
|
|
|
-- NOTE: This icons/desktop files module was written according to the
|
|
|
|
-- following freedesktop.org specifications:
|
|
|
|
-- Icons: http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-0.11.html
|
|
|
|
-- Desktop files: http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-1.0.html
|
|
|
|
|
|
|
|
-- Options section
|
|
|
|
|
2013-12-29 15:10:31 +01:00
|
|
|
--- Terminal which applications that need terminal would open in.
|
2012-06-12 10:36:28 +02:00
|
|
|
utils.terminal = 'xterm'
|
2012-03-08 20:24:21 +01:00
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- The default icon for applications that don't provide any icon in
|
2012-03-08 20:24:21 +01:00
|
|
|
-- their .desktop files.
|
2012-06-12 10:36:28 +02:00
|
|
|
local default_icon = nil
|
2012-03-08 20:24:21 +01:00
|
|
|
|
2013-12-29 15:10:31 +01:00
|
|
|
--- Name of the WM for the OnlyShownIn entry in the .desktop file.
|
2012-11-22 12:11:47 +01:00
|
|
|
utils.wm_name = "awesome"
|
2012-03-08 20:24:21 +01:00
|
|
|
|
|
|
|
-- Private section
|
|
|
|
|
|
|
|
local all_icon_sizes = {
|
|
|
|
'128x128' ,
|
|
|
|
'96x96',
|
|
|
|
'72x72',
|
|
|
|
'64x64',
|
|
|
|
'48x48',
|
|
|
|
'36x36',
|
|
|
|
'32x32',
|
|
|
|
'24x24',
|
|
|
|
'22x22',
|
|
|
|
'16x16'
|
|
|
|
}
|
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- List of supported icon formats.
|
2015-02-09 19:49:01 +01:00
|
|
|
local icon_formats = { "png", "xpm", "svg" }
|
2012-03-08 20:24:21 +01:00
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Check whether the icon format is supported.
|
2012-03-08 20:24:21 +01:00
|
|
|
-- @param icon_file Filename of the icon.
|
|
|
|
-- @return true if format is supported, false otherwise.
|
|
|
|
local function is_format_supported(icon_file)
|
|
|
|
for _, f in ipairs(icon_formats) do
|
|
|
|
if icon_file:match('%.' .. f) then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2015-02-09 19:23:42 +01:00
|
|
|
local icon_lookup_path = nil
|
2015-10-11 19:53:00 +02:00
|
|
|
--- Get a list of icon lookup paths.
|
|
|
|
-- @treturn table A list of directories, without trailing slash.
|
2015-02-09 19:23:42 +01:00
|
|
|
local function get_icon_lookup_path()
|
|
|
|
if not icon_lookup_path then
|
2015-10-11 19:53:00 +02:00
|
|
|
local add_if_readable = function(t, path)
|
|
|
|
if awful_util.dir_readable(path) then
|
|
|
|
table.insert(t, path)
|
|
|
|
end
|
|
|
|
end
|
2015-02-09 19:23:42 +01:00
|
|
|
icon_lookup_path = {}
|
2012-03-08 20:24:21 +01:00
|
|
|
local icon_theme_paths = {}
|
|
|
|
local icon_theme = theme.icon_theme
|
2015-02-05 19:42:49 +01:00
|
|
|
local paths = glib.get_system_data_dirs()
|
2015-02-09 19:23:42 +01:00
|
|
|
table.insert(paths, 1, glib.get_user_data_dir())
|
2015-10-11 19:53:00 +02:00
|
|
|
table.insert(paths, 1, glib.build_filenamev({glib.get_home_dir(),
|
|
|
|
'.icons'}))
|
2016-02-07 14:29:40 +01:00
|
|
|
for _,dir in ipairs(paths) do
|
2015-10-11 19:53:00 +02:00
|
|
|
local icons_dir = glib.build_filenamev({dir, 'icons'})
|
|
|
|
if awful_util.dir_readable(icons_dir) then
|
|
|
|
if icon_theme then
|
|
|
|
add_if_readable(icon_theme_paths,
|
|
|
|
glib.build_filenamev({icons_dir,
|
|
|
|
icon_theme}))
|
|
|
|
end
|
|
|
|
-- Fallback theme.
|
|
|
|
add_if_readable(icon_theme_paths,
|
|
|
|
glib.build_filenamev({icons_dir, 'hicolor'}))
|
2015-02-05 19:42:49 +01:00
|
|
|
end
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
2016-02-07 14:29:40 +01:00
|
|
|
for _, icon_theme_directory in ipairs(icon_theme_paths) do
|
|
|
|
for _, size in ipairs(all_icon_sizes) do
|
2015-10-11 19:53:00 +02:00
|
|
|
add_if_readable(icon_lookup_path,
|
|
|
|
glib.build_filenamev({icon_theme_directory,
|
|
|
|
size, 'apps'}))
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
|
|
|
end
|
2016-02-07 14:29:40 +01:00
|
|
|
for _,dir in ipairs(paths)do
|
2015-02-05 19:42:49 +01:00
|
|
|
-- lowest priority fallbacks
|
2015-10-11 19:53:00 +02:00
|
|
|
add_if_readable(icon_lookup_path,
|
|
|
|
glib.build_filenamev({dir, 'pixmaps'}))
|
|
|
|
add_if_readable(icon_lookup_path,
|
|
|
|
glib.build_filenamev({dir, 'icons'}))
|
2015-02-05 19:42:49 +01:00
|
|
|
end
|
2015-02-09 19:23:42 +01:00
|
|
|
end
|
|
|
|
return icon_lookup_path
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Lookup an icon in different folders of the filesystem.
|
2015-10-12 00:25:39 +02:00
|
|
|
-- @tparam string icon_file Short or full name of the icon.
|
|
|
|
-- @treturn string|boolean Full name of the icon, or false on failure.
|
|
|
|
function utils.lookup_icon_uncached(icon_file)
|
2015-02-09 19:23:42 +01:00
|
|
|
if not icon_file or icon_file == "" then
|
2015-10-12 00:25:39 +02:00
|
|
|
return false
|
2015-02-09 19:23:42 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if icon_file:sub(1, 1) == '/' and is_format_supported(icon_file) then
|
|
|
|
-- If the path to the icon is absolute and its format is
|
|
|
|
-- supported, do not perform a lookup.
|
|
|
|
return awful_util.file_readable(icon_file) and icon_file or nil
|
|
|
|
else
|
2016-02-07 14:29:40 +01:00
|
|
|
for _, directory in ipairs(get_icon_lookup_path()) do
|
2015-10-11 19:53:00 +02:00
|
|
|
if is_format_supported(icon_file) and
|
|
|
|
awful_util.file_readable(directory .. "/" .. icon_file) then
|
|
|
|
return directory .. "/" .. icon_file
|
2012-03-08 20:24:21 +01:00
|
|
|
else
|
|
|
|
-- Icon is probably specified without path and format,
|
|
|
|
-- like 'firefox'. Try to add supported extensions to
|
|
|
|
-- it and see if such file exists.
|
|
|
|
for _, format in ipairs(icon_formats) do
|
2015-10-11 19:53:00 +02:00
|
|
|
local possible_file = directory .. "/" .. icon_file .. "." .. format
|
2012-03-08 20:24:21 +01:00
|
|
|
if awful_util.file_readable(possible_file) then
|
|
|
|
return possible_file
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-10-12 00:25:39 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local lookup_icon_cache = {}
|
|
|
|
--- Lookup an icon in different folders of the filesystem (cached).
|
2015-11-07 13:55:04 +01:00
|
|
|
-- @param icon Short or full name of the icon.
|
2015-10-12 00:25:39 +02:00
|
|
|
-- @return full name of the icon.
|
|
|
|
function utils.lookup_icon(icon)
|
|
|
|
if not lookup_icon_cache[icon] and lookup_icon_cache[icon] ~= false then
|
|
|
|
lookup_icon_cache[icon] = utils.lookup_icon_uncached(icon)
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
2015-10-12 00:25:39 +02:00
|
|
|
return lookup_icon_cache[icon] or default_icon
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Parse a .desktop file.
|
|
|
|
-- @param file The .desktop file.
|
|
|
|
-- @return A table with file entries.
|
2012-06-12 10:36:28 +02:00
|
|
|
function utils.parse(file)
|
2012-03-08 20:24:21 +01:00
|
|
|
local program = { show = true, file = file }
|
2012-12-01 15:42:00 +01:00
|
|
|
local desktop_entry = false
|
|
|
|
|
|
|
|
-- Parse the .desktop file.
|
|
|
|
-- We are interested in [Desktop Entry] group only.
|
2012-03-08 20:24:21 +01:00
|
|
|
for line in io.lines(file) do
|
2014-09-25 02:47:40 +02:00
|
|
|
if line:find("^%s*#") then
|
|
|
|
-- Skip comments.
|
2016-02-07 14:29:40 +01:00
|
|
|
(function() end)() -- I haven't found a nice way to silence luacheck here
|
2014-09-25 02:47:40 +02:00
|
|
|
elseif not desktop_entry and line == "[Desktop Entry]" then
|
2012-12-01 15:42:00 +01:00
|
|
|
desktop_entry = true
|
|
|
|
else
|
|
|
|
if line:sub(1, 1) == "[" and line:sub(-1) == "]" then
|
|
|
|
-- A declaration of new group - stop parsing
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Grab the values
|
|
|
|
for key, value in line:gmatch("(%w+)%s*=%s*(.+)") do
|
|
|
|
program[key] = value
|
|
|
|
end
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-01 15:42:00 +01:00
|
|
|
-- In case [Desktop Entry] was not found
|
|
|
|
if not desktop_entry then return nil end
|
|
|
|
|
2015-11-27 11:44:47 +01:00
|
|
|
-- In case the (required) 'Name' entry was not found
|
|
|
|
if not program.Name or program.Name == '' then return nil end
|
|
|
|
|
2012-03-08 20:24:21 +01:00
|
|
|
-- Don't show program if NoDisplay attribute is false
|
|
|
|
if program.NoDisplay and string.lower(program.NoDisplay) == "true" then
|
|
|
|
program.show = false
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Only show the program if there is no OnlyShowIn attribute
|
2012-11-22 12:11:47 +01:00
|
|
|
-- or if it's equal to utils.wm_name
|
2013-01-10 01:22:46 +01:00
|
|
|
if program.OnlyShowIn ~= nil and not program.OnlyShowIn:match(utils.wm_name) then
|
2012-03-08 20:24:21 +01:00
|
|
|
program.show = false
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Look up for a icon.
|
|
|
|
if program.Icon then
|
2012-06-30 21:51:11 +02:00
|
|
|
program.icon_path = utils.lookup_icon(program.Icon)
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Split categories into a table. Categories are written in one
|
|
|
|
-- line separated by semicolon.
|
|
|
|
if program.Categories then
|
|
|
|
program.categories = {}
|
2012-12-01 14:02:49 +01:00
|
|
|
for category in program.Categories:gmatch('[^;]+') do
|
2012-03-08 20:24:21 +01:00
|
|
|
table.insert(program.categories, category)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if program.Exec then
|
|
|
|
-- Substitute Exec special codes as specified in
|
|
|
|
-- http://standards.freedesktop.org/desktop-entry-spec/1.1/ar01s06.html
|
2015-01-20 19:28:46 +01:00
|
|
|
if program.Name == nil then
|
|
|
|
program.Name = '['.. file:match("([^/]+)%.desktop$") ..']'
|
|
|
|
end
|
2012-03-08 20:24:21 +01:00
|
|
|
local cmdline = program.Exec:gsub('%%c', program.Name)
|
|
|
|
cmdline = cmdline:gsub('%%[fuFU]', '')
|
|
|
|
cmdline = cmdline:gsub('%%k', program.file)
|
|
|
|
if program.icon_path then
|
|
|
|
cmdline = cmdline:gsub('%%i', '--icon ' .. program.icon_path)
|
|
|
|
else
|
|
|
|
cmdline = cmdline:gsub('%%i', '')
|
|
|
|
end
|
|
|
|
if program.Terminal == "true" then
|
2012-06-12 10:36:28 +02:00
|
|
|
cmdline = utils.terminal .. ' -e ' .. cmdline
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
|
|
|
program.cmdline = cmdline
|
|
|
|
end
|
|
|
|
|
|
|
|
return program
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Parse a directory with .desktop files
|
|
|
|
-- @param dir The directory.
|
|
|
|
-- @return A table with all .desktop entries.
|
2012-06-12 10:36:28 +02:00
|
|
|
function utils.parse_dir(dir)
|
2012-03-08 20:24:21 +01:00
|
|
|
local programs = {}
|
2012-11-24 17:58:15 +01:00
|
|
|
local files = io.popen('find '.. dir ..' -maxdepth 1 -name "*.desktop" 2>/dev/null')
|
2012-04-07 11:01:33 +02:00
|
|
|
for file in files:lines() do
|
2012-12-01 15:42:00 +01:00
|
|
|
local program = utils.parse(file)
|
|
|
|
if program then
|
|
|
|
table.insert(programs, program)
|
|
|
|
end
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
|
|
|
return programs
|
|
|
|
end
|
|
|
|
|
2015-09-20 16:42:24 +02:00
|
|
|
--- Compute textbox width.
|
|
|
|
-- @tparam wibox.widget.textbox textbox Textbox instance.
|
2016-02-26 18:25:55 +01:00
|
|
|
-- @tparam number|screen s Screen
|
2015-09-20 16:42:24 +02:00
|
|
|
-- @treturn int Text width.
|
|
|
|
function utils.compute_textbox_width(textbox, s)
|
2016-02-26 18:25:55 +01:00
|
|
|
s = screen[s or mouse.screen]
|
2016-02-07 14:29:40 +01:00
|
|
|
local w, _ = textbox:get_preferred_size(s)
|
2015-09-20 16:42:24 +02:00
|
|
|
return w
|
|
|
|
end
|
|
|
|
|
2015-02-19 22:11:38 +01:00
|
|
|
--- Compute text width.
|
|
|
|
-- @tparam str text Text.
|
2016-02-26 18:25:55 +01:00
|
|
|
-- @tparam number|screen s Screen
|
2015-02-19 22:11:38 +01:00
|
|
|
-- @treturn int Text width.
|
2015-09-20 16:42:24 +02:00
|
|
|
function utils.compute_text_width(text, s)
|
|
|
|
return utils.compute_textbox_width(wibox.widget.textbox(awful_util.escape(text)), s)
|
2015-02-19 22:11:38 +01:00
|
|
|
end
|
|
|
|
|
2012-06-12 10:36:28 +02:00
|
|
|
return utils
|
|
|
|
|
2012-03-08 20:24:21 +01:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|