2015-05-14 22:57:22 +02:00
|
|
|
----------------------------------------------------------------------------
|
|
|
|
--- Library for getting xrdb data.
|
|
|
|
--
|
|
|
|
-- @author Yauhen Kirylau <yawghen@gmail.com>
|
|
|
|
-- @copyright 2015 Yauhen Kirylau
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @module beautiful
|
2015-05-14 22:57:22 +02:00
|
|
|
----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Grab environment
|
|
|
|
local awesome = awesome
|
2016-02-26 18:19:24 +01:00
|
|
|
local screen = screen
|
2017-03-08 21:18:33 +01:00
|
|
|
local round = require("gears.math").round
|
2015-12-22 19:28:05 +01:00
|
|
|
local gears_debug = require("gears.debug")
|
2015-05-14 22:57:22 +02:00
|
|
|
|
|
|
|
local xresources = {}
|
|
|
|
|
|
|
|
local fallback = {
|
|
|
|
--black
|
|
|
|
color0 = '#000000',
|
|
|
|
color8 = '#465457',
|
|
|
|
--red
|
|
|
|
color1 = '#cb1578',
|
|
|
|
color9 = '#dc5e86',
|
|
|
|
--green
|
|
|
|
color2 = '#8ecb15',
|
|
|
|
color10 = '#9edc60',
|
|
|
|
--yellow
|
|
|
|
color3 = '#cb9a15',
|
|
|
|
color11 = '#dcb65e',
|
|
|
|
--blue
|
|
|
|
color4 = '#6f15cb',
|
|
|
|
color12 = '#7e5edc',
|
|
|
|
--purple
|
|
|
|
color5 = '#cb15c9',
|
|
|
|
color13 = '#b75edc',
|
|
|
|
--cyan
|
|
|
|
color6 = '#15b4cb',
|
|
|
|
color14 = '#5edcb4',
|
|
|
|
--white
|
|
|
|
color7 = '#888a85',
|
|
|
|
color15 = '#ffffff',
|
|
|
|
--
|
|
|
|
background = '#0e0021',
|
|
|
|
foreground = '#bcbcbc',
|
|
|
|
}
|
|
|
|
|
|
|
|
--- Get current base colorscheme from xrdb.
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @treturn table Color table with keys 'background', 'foreground' and 'color0'..'color15'.
|
|
|
|
-- @staticfct beautiful.xresources.get_current_theme
|
2015-05-14 22:57:22 +02:00
|
|
|
function xresources.get_current_theme()
|
|
|
|
local keys = { 'background', 'foreground' }
|
|
|
|
for i=0,15 do table.insert(keys, "color"..i) end
|
|
|
|
local colors = {}
|
|
|
|
for _, key in ipairs(keys) do
|
2018-01-18 04:43:15 +01:00
|
|
|
local color = awesome.xrdb_get_value("", key)
|
|
|
|
if color then
|
|
|
|
if color:find("rgb:") then
|
|
|
|
color = "#"..color:gsub("[a]?rgb:", ""):gsub("/", "")
|
|
|
|
end
|
|
|
|
else
|
|
|
|
gears_debug.print_warning(
|
|
|
|
"beautiful: can't get colorscheme from xrdb for value '"..key.."' (using fallback)."
|
|
|
|
)
|
|
|
|
color = fallback[key]
|
2015-12-07 09:31:16 +01:00
|
|
|
end
|
2018-01-18 04:43:15 +01:00
|
|
|
colors[key] = color
|
2015-05-14 22:57:22 +02:00
|
|
|
end
|
|
|
|
return colors
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2016-02-26 18:19:24 +01:00
|
|
|
local function get_screen(s)
|
|
|
|
return s and screen[s]
|
|
|
|
end
|
|
|
|
|
2015-07-22 13:47:16 +02:00
|
|
|
--- Get global or per-screen DPI value falling back to xrdb.
|
2017-11-19 05:23:24 +01:00
|
|
|
--
|
|
|
|
-- This function is deprecated. Use `s.dpi` and avoid getting the DPI without
|
|
|
|
-- a screen.
|
|
|
|
--
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @deprecated beautiful.xresources.get_dpi
|
2016-02-26 18:19:24 +01:00
|
|
|
-- @tparam[opt] integer|screen s The screen.
|
2015-05-14 22:57:22 +02:00
|
|
|
-- @treturn number DPI value.
|
2017-11-19 05:23:24 +01:00
|
|
|
|
2015-07-22 13:47:16 +02:00
|
|
|
function xresources.get_dpi(s)
|
2016-02-26 18:19:24 +01:00
|
|
|
s = get_screen(s)
|
2017-10-08 12:30:21 +02:00
|
|
|
if s then
|
|
|
|
return s.dpi
|
2015-07-22 13:47:16 +02:00
|
|
|
end
|
2015-05-14 22:57:22 +02:00
|
|
|
if not xresources.dpi then
|
2016-02-21 17:08:50 +01:00
|
|
|
-- Might not be present when run under unit tests
|
|
|
|
if awesome and awesome.xrdb_get_value then
|
|
|
|
xresources.dpi = tonumber(awesome.xrdb_get_value("", "Xft.dpi"))
|
|
|
|
end
|
2017-01-25 21:34:48 +01:00
|
|
|
-- Following Keith Packard's whitepaper on Xft,
|
|
|
|
-- https://keithp.com/~keithp/talks/xtc2001/paper/xft.html#sec-editing
|
|
|
|
-- the proper fallback for Xft.dpi is the vertical DPI reported by
|
|
|
|
-- the X server. This will generally be 96 on Xorg, unless the user
|
|
|
|
-- has configured it differently
|
|
|
|
if not xresources.dpi then
|
|
|
|
if root then
|
|
|
|
local mm_to_inch = 25.4
|
2017-05-26 15:33:56 +02:00
|
|
|
local _, h = root.size()
|
|
|
|
local _, hmm = root.size_mm()
|
2017-01-25 21:34:48 +01:00
|
|
|
if hmm ~= 0 then
|
|
|
|
xresources.dpi = round(h*mm_to_inch/hmm)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- ultimate fallback
|
2016-02-21 17:08:50 +01:00
|
|
|
if not xresources.dpi then
|
|
|
|
xresources.dpi = 96
|
|
|
|
end
|
2015-05-14 22:57:22 +02:00
|
|
|
end
|
|
|
|
return xresources.dpi
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-07-22 13:47:16 +02:00
|
|
|
--- Set DPI for a given screen (defaults to global).
|
|
|
|
-- @tparam number dpi DPI value.
|
|
|
|
-- @tparam[opt] integer s Screen.
|
2022-07-05 10:37:14 +02:00
|
|
|
-- @noreturn
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct beautiful.xresources.set_dpi
|
2015-07-22 13:47:16 +02:00
|
|
|
function xresources.set_dpi(dpi, s)
|
2016-02-26 18:19:24 +01:00
|
|
|
s = get_screen(s)
|
2015-07-22 13:47:16 +02:00
|
|
|
if not s then
|
|
|
|
xresources.dpi = dpi
|
|
|
|
else
|
2017-10-08 12:30:21 +02:00
|
|
|
s.dpi = dpi
|
2015-07-22 13:47:16 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--- Compute resulting size applying current DPI value (optionally per screen).
|
2015-05-14 22:57:22 +02:00
|
|
|
-- @tparam number size Size
|
2016-02-26 18:19:24 +01:00
|
|
|
-- @tparam[opt] integer|screen s The screen.
|
2015-08-02 21:51:06 +02:00
|
|
|
-- @treturn integer Resulting size (rounded to integer).
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct beautiful.xresources.apply_dpi
|
2015-07-22 13:47:16 +02:00
|
|
|
function xresources.apply_dpi(size, s)
|
2015-08-12 14:18:57 +02:00
|
|
|
return round(size / 96 * xresources.get_dpi(s))
|
2015-05-14 22:57:22 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return xresources
|
|
|
|
|
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|