1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // Resource.cc for Blackbox - an X11 Window manager
3 // Copyright (c) 2001 - 2005 Sean 'Shaleh' Perry <shaleh@debian.org>
4 // Copyright (c) 1997 - 2000, 2002 - 2005
5 //         Bradley T Hughes <bhughes at trolltech.com>
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a
8 // copy of this software and associated documentation files (the "Software"),
9 // to deal in the Software without restriction, including without limitation
10 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 // and/or sell copies of the Software, and to permit persons to whom the
12 // Software is furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in
15 // all copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 // DEALINGS IN THE SOFTWARE.
24 
25 #include "Resource.hh"
26 #include "Util.hh"
27 
28 #include <X11/Xlib.h>
29 #include <X11/Xresource.h>
30 
31 #include <cstring>
32 #include <stdio.h>
33 
34 
Resource(void)35 bt::Resource::Resource(void)
36   : db(NULL)
37 { }
38 
39 
Resource(const std::string & filename)40 bt::Resource::Resource(const std::string &filename)
41   : db(NULL)
42 { load(filename); }
43 
44 
~Resource(void)45 bt::Resource::~Resource(void)
46 { XrmDestroyDatabase(db); }
47 
48 
load(const std::string & filename)49 void bt::Resource::load(const std::string &filename)
50 {
51   XrmDestroyDatabase(db);
52   if (!filename.empty())
53     db = XrmGetFileDatabase(expandTilde(filename).c_str());
54   else
55     db = NULL;
56 }
57 
58 
save(const std::string & filename)59 void bt::Resource::save(const std::string &filename)
60 {
61   if (!valid() || filename.empty())
62     return;
63   XrmPutFileDatabase(db, expandTilde(filename).c_str());
64 }
65 
66 
merge(const std::string & filename)67 void bt::Resource::merge(const std::string &filename)
68 {
69   if (filename.empty())
70     return;
71   XrmCombineFileDatabase(expandTilde(filename).c_str(), &db, false);
72 }
73 
74 
read(const char * name,const char * classname,const char * default_value) const75 std::string bt::Resource::read(const char* name, const char* classname,
76                                const char* default_value) const {
77   XrmValue value;
78   char *value_type;
79   if (XrmGetResource(db, name, classname, &value_type, &value))
80     return std::string(value.addr, value.size - 1);
81   return std::string(default_value);
82 }
83 
84 
read(const std::string & name,const std::string & classname,const std::string & default_value) const85 std::string bt::Resource::read(const std::string& name,
86                                const std::string& classname,
87                                const std::string& default_value) const {
88   XrmValue value;
89   char *value_type;
90   if (XrmGetResource(db, name.c_str(), classname.c_str(),
91                      &value_type, &value))
92     return std::string(value.addr, value.size - 1);
93   return default_value;
94 }
95 
96 
read(const char * name,const char * classname,int default_value) const97 int bt::Resource::read(const char* name, const char* classname,
98                        int default_value) const {
99   XrmValue value;
100   char *value_type;
101   if (XrmGetResource(db, name, classname, &value_type, &value)) {
102     int output;
103     sscanf(value.addr, "%d", &output);
104     return output;
105   }
106   return default_value;
107 }
108 
109 
read(const char * name,const char * classname,unsigned int default_value) const110 unsigned int bt::Resource::read(const char* name, const char* classname,
111                                 unsigned int default_value) const {
112   XrmValue value;
113   char *value_type;
114   if (XrmGetResource(db, name, classname, &value_type, &value)) {
115     unsigned int output;
116     sscanf(value.addr, "%u", &output);
117     return output;
118   }
119   return default_value;
120 }
121 
122 
read(const char * name,const char * classname,long default_value) const123 long bt::Resource::read(const char* name, const char* classname,
124                         long default_value) const {
125   XrmValue value;
126   char *value_type;
127   if (XrmGetResource(db, name, classname, &value_type, &value)) {
128     long output;
129     sscanf(value.addr, "%ld", &output);
130     return output;
131   }
132   return default_value;
133 }
134 
135 
read(const char * name,const char * classname,unsigned long default_value) const136 unsigned long bt::Resource::read(const char* name, const char* classname,
137                                  unsigned long default_value) const {
138   XrmValue value;
139   char *value_type;
140   if (XrmGetResource(db, name, classname, &value_type, &value)) {
141     unsigned long output;
142     sscanf(value.addr, "%lu", &output);
143     return output;
144   }
145   return default_value;
146 }
147 
148 
read(const char * name,const char * classname,bool default_value) const149 bool bt::Resource::read(const char* name, const char* classname,
150                         bool default_value) const {
151   XrmValue value;
152   char *value_type;
153   if (XrmGetResource(db, name, classname, &value_type, &value)) {
154     if (strncasecmp(value.addr, "true", value.size) == 0)
155       return true;
156     return false;
157   }
158   return default_value;
159 }
160 
161 
read(const char * name,const char * classname,double default_value) const162 double bt::Resource::read(const char* name, const char* classname,
163                           double default_value) const {
164   XrmValue value;
165   char *value_type;
166   if (XrmGetResource(db, name, classname, &value_type, &value)) {
167     double output;
168     sscanf(value.addr, "%lf", &output);
169     return output;
170   }
171   return default_value;
172 }
173 
174 
write(const char * resource,const std::string & value)175 void bt::Resource::write(const char *resource, const std::string &value)
176 { write(resource, value.c_str()); }
177 
178 
write(const char * resource,const char * value)179 void bt::Resource::write(const char* resource, const char* value)
180 { XrmPutStringResource(&db, resource, value); }
181 
182 
write(const char * resource,int value)183 void bt::Resource::write(const char* resource, int value) {
184   char tmp[16];
185   sprintf(tmp, "%d", value);
186   write(resource, tmp);
187 }
188 
189 
write(const char * resource,unsigned int value)190 void bt::Resource::write(const char* resource, unsigned int value) {
191   char tmp[16];
192   sprintf(tmp, "%u", value);
193   write(resource, tmp);
194 }
195 
196 
write(const char * resource,long value)197 void bt::Resource::write(const char* resource, long value) {
198   char tmp[64];
199   sprintf(tmp, "%ld", value);
200   write(resource, tmp);
201 }
202 
203 
write(const char * resource,unsigned long value)204 void bt::Resource::write(const char* resource, unsigned long value) {
205   char tmp[64];
206   sprintf(tmp, "%lu", value);
207   write(resource, tmp);
208 }
209 
210 
write(const char * resource,bool value)211 void bt::Resource::write(const char* resource, bool value)
212 { write(resource, boolAsString(value)); }
213 
214 
write(const char * resource,double value)215 void bt::Resource::write(const char* resource, double value) {
216   char tmp[80];
217   sprintf(tmp, "%f", value);
218   write(resource, tmp);
219 }
220