Update the code following release of xcb-util 0.3.8.
[awesome.git] / lib / beautiful.lua.in
blob566612aeb06e185d081bdd95100aec9d1cdc81a6
1 ----------------------------------------------------------------------------
2 -- @author Damien Leone <damien.leone@gmail.com>
3 -- @author Julien Danjou <julien@danjou.info>
4 -- @copyright 2008-2009 Damien Leone, Julien Danjou
5 -- @release @AWESOME_VERSION@
6 ----------------------------------------------------------------------------
8 -- Grab environment
9 local os = os
10 local print = print
11 local pcall = pcall
12 local pairs = pairs
13 local type = type
14 local dofile = dofile
15 local setmetatable = setmetatable
16 local util = require("awful.util")
17 local capi =
19 screen = screen,
20 awesome = awesome,
21 oocairo = oocairo,
22 oopango = oopango
25 --- Theme library.
26 module("beautiful")
28 -- Local data
29 local theme
30 local descs = setmetatable({}, { __mode = 'k' })
31 local fonts = setmetatable({}, { __mode = 'v' })
32 local active_font
34 local function load_font(name)
35 name = name or active_font
36 if name and type(name) ~= "string" and descs[name] then
37 name = descs[name]
38 end
39 if fonts[name] then
40 return fonts[name]
41 end
42 -- load new font
43 local desc = capi.oopango.font_description_from_string(name)
45 -- Create a temporary surface that we need for computing the size :(
46 local surface = capi.oocairo.image_surface_create("argb32", 1, 1)
47 local cr = capi.oocairo.context_create(surface)
48 local layout
49 -- Api breakage in oopango
50 if capi.oopango.cairo_layout_create then
51 layout = capi.oopango.cairo_layout_create(cr)
52 else
53 layout = capi.oopango.cairo.layout_create(cr)
54 end
56 layout:set_font_description(desc)
58 local width, height = layout:get_pixel_size()
59 local font = { name = name, description = desc, height = height }
60 fonts[name] = font
61 descs[desc] = name
62 return font
63 end
65 local function set_font(name)
66 active_font = load_font(name).name
67 end
69 function get_font(name)
70 return load_font(name).description
71 end
73 function get_font_height(name)
74 return load_font(name).height
75 end
77 --- Init function, should be runned at the beginning of configuration file.
78 -- @param path The theme file path.
79 function init(path)
80 if path then
81 local success
82 success, theme = pcall(function() return dofile(path) end)
84 if not success then
85 return print("E: beautiful: error loading theme file " .. theme)
86 elseif theme then
87 -- try and grab user's $HOME directory
88 local homedir = os.getenv("HOME")
89 -- expand '~'
90 if homedir then
91 for k, v in pairs(theme) do
92 if type(v) == "string" then theme[k] = v:gsub("~", homedir) end
93 end
94 end
96 -- setup wallpaper
97 if theme.wallpaper_cmd then
98 for s = 1, capi.screen.count() do
99 util.spawn(theme.wallpaper_cmd[util.cycle(#theme.wallpaper_cmd, s)], false, s)
102 if theme.font then set_font(theme.font) end
103 if theme.fg_normal then capi.awesome.fg = theme.fg_normal end
104 if theme.bg_normal then capi.awesome.bg = theme.bg_normal end
105 else
106 return print("E: beautiful: error loading theme file " .. path)
108 else
109 return print("E: beautiful: error loading theme: no path specified")
113 --- Get the current theme.
114 -- @return The current theme table.
115 function get()
116 return theme
119 -- Set the default font
120 set_font("sans 8")
122 setmetatable(_M, { __index = function(t, k) return theme[k] end })
124 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80