forked from awesomeWM/awesome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xrdb.c
76 lines (67 loc) · 2.47 KB
/
xrdb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* xrdb.c - X Resources DataBase communication functions
*
* Copyright © 2015 Yauhen Kirylau <yawghen@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "xrdb.h"
#include "globalconf.h"
#include <X11/Xresource.h>
#include <string.h>
/* \brief open X display and X Resources DB
*/
static void xrdb_init(void) {
XrmInitialize(); // @TODO: it works without it but in docs it's said what it's
// needed
if (!(globalconf.xrmdb = XrmGetDatabase(globalconf.display))) {
/* taken from xpbiff: */
/* >> what a hack; need to initialize dpy->db */
(void)XGetDefault(globalconf.display, "", "");
/**/
if (!(globalconf.xrmdb = XrmGetDatabase(globalconf.display)))
warn("Cannot open xrdb.");
}
}
/* \brief get value from X Resources DataBase
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
* \luastack
* \lparam string xrdb class, ie "URxvt" or ""
* \lparam string xrdb name, ie "background" or "color0"
* \lreturn string xrdb value or nil if not exists. \
*/
int luaA_xrdb_get_value(lua_State *L) {
if (!globalconf.xrmdb)
xrdb_init();
char *resource_type;
int resource_code;
XrmValue resource_value;
const char *resource_class = luaL_checkstring(L, 1);
const char *resource_name = luaL_checkstring(L, 2);
resource_code = XrmGetResource(globalconf.xrmdb, resource_name, resource_class,
&resource_type, &resource_value);
if (resource_code && (strcmp(resource_type, "String") == 0)) {
lua_pushstring(L, (char *)resource_value.addr);
return 1;
} else {
if (strlen(resource_class))
luaA_warn(L, "Failed to get xrdb value '%s' (class '%s').", resource_name, resource_class);
else
luaA_warn(L, "Failed to get xrdb value '%s'.", resource_name);
return 0;
}
}