fixing parse error in execcommand
[bbkeys.git] / src / Baseresource.cc
blob308f91d0d5cda67eb66f3866ac7e5a6d0699d8d5
1 // Baseresource.cc for bbtools - tools to display resources in X11.
2 //
3 // Copyright (c) 1998-1999 John Kennis, jkennis@chello.nl
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 // (See the included file COPYING / GPL-2.0)
21 // $Id$
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif // HAVE_CONFIG_H
27 #ifdef HAVE_STDLIB_H
28 # include <stdlib.h>
29 #endif // HAVE_STDLIB_H
31 #include "bbkeys.hh"
32 #include "Baseresource.hh"
33 #include "blackboxstyle.hh"
35 BaseResource::BaseResource(ToolWindow *toolwindow) {
36 XrmValue value;
37 char *value_type;
39 bbtool=toolwindow;
40 style.style_filename=NULL;
41 style.conf_filename=NULL;
43 if (bbtool->nobb_config) {
44 ReadBBtoolResource();
45 ResourceType=BBTOOLS;
46 } else {
47 char *homedir = getenv("HOME");
48 bbtool->config_filename = new char[strlen(homedir) + 32];
49 sprintf(bbtool->config_filename, "%s/.blackboxrc", homedir);
50 if ((resource_db = XrmGetFileDatabase(bbtool->config_filename))!=NULL) {
51 ReadBlackboxResource();
52 ResourceType=BLACKBOX;
54 else {
55 ReadBBtoolResource();
56 ResourceType=BBTOOLS;
60 if (XrmGetResource(resource_db, "session.colorsPerChannel",
61 "Session.ColorsPerChannel", &value_type, &value)) {
62 if (sscanf(value.addr, "%d", &bbtool->colors_per_channel) != 1) {
63 bbtool->colors_per_channel = 4;
64 } else {
65 if (bbtool->colors_per_channel < 2) bbtool->colors_per_channel = 2;
66 if (bbtool->colors_per_channel > 6) bbtool->colors_per_channel = 6;
68 } else
69 bbtool->colors_per_channel = 4;
72 if (XrmGetResource(resource_db, "session.imageDither",
73 "Session.ImageDither", &value_type, &value)) {
74 if (! strncasecmp("true", value.addr, value.size))
75 bbtool->image_dither = True;
76 else
77 bbtool->image_dither = False;
78 } else
79 bbtool->image_dither = True;
81 if (bbtool->image_dither &&
82 bbtool->getCurrentScreenInfo()->getVisual()->c_class == TrueColor
83 && bbtool->getCurrentScreenInfo()->getDepth() >= 24)
84 bbtool->image_dither = False;
86 /* Need to do this here */
87 bbtool->setupImageControl();
89 if (XrmGetResource(resource_db, "bbkeys.autoConfig",
90 "Bbkeys.Autoconfig", &value_type, &value)) {
91 if (! strncasecmp("true", value.addr, value.size)) {
92 auto_config = True;
93 } else
94 auto_config = False;
95 } else
96 auto_config = False;
98 if (XrmGetResource(resource_db, "bbkeys.honorModifiers",
99 "Bbkeys.Honormodifiers", &value_type, &value)) {
100 if (! strncasecmp("true", value.addr, value.size)) {
101 bbtool->honor_modifiers = True;
102 } else
103 bbtool->honor_modifiers = False;
104 } else
105 bbtool->honor_modifiers = False;
107 if (bbtool->withdrawn) auto_config=False;
109 if (XrmGetResource(resource_db, "bbkeys.autoConfig.checkTimeout",
110 "Bbkeys.Autoconfig.CheckTimeout", &value_type, &value)) {
111 if (sscanf(value.addr, "%u", &check_timeout) != 1)
112 check_timeout = 10;
114 else
115 check_timeout = 10;
117 if ((bbtool->config_filename!=NULL)&(auto_config)) {
118 if (stat(bbtool->config_filename,&file_status)!=0) {
119 fprintf(stderr,"Can't use autoconfig");
120 auto_config=false;
121 mtime=0;
122 } else {
123 mtime=file_status.st_mtime;
125 timer=new BTimer(toolwindow->getCurrentScreenInfo()->getBaseDisplay(),
126 this);
127 timer->setTimeout(1000*check_timeout);
128 timer->start();
134 void BaseResource::Load() {
136 LoadBBToolResource();
137 XrmDestroyDatabase(resource_db);
140 BaseResource::~BaseResource() {
141 delete [] style.style_filename;
142 delete [] style.conf_filename;
144 style.style_filename=NULL;
145 style.conf_filename=NULL;
148 void BaseResource::timeout() {
149 if (stat(bbtool->config_filename,&file_status)!=0) {
150 fprintf(stderr,"Autoconfig error: Cannot get status of:%s\n",
151 bbtool->config_filename);
152 style.mtime=0;
153 } else {
154 if (mtime!=file_status.st_mtime) {
155 bbtool->reconfigure();
156 mtime=file_status.st_mtime;
161 void BaseResource::CopyColor(BColor *Color1,BColor *Color2) {
162 Color2->setPixel(Color1->getPixel());
163 Color2->setRGB(Color1->getRed(),Color1->getGreen(),Color1->getBlue());
166 void BaseResource::CopyTexture(BTexture Texture1,BTexture *Texture2) {
167 CopyColor(Texture1.getColor(),Texture2->getColor());
168 CopyColor(Texture1.getColorTo(),Texture2->getColorTo());
169 CopyColor(Texture1.getHiColor(),Texture2->getHiColor());
170 CopyColor(Texture1.getLoColor(),Texture2->getLoColor());
171 Texture2->setTexture(Texture1.getTexture());
174 void BaseResource::Reload() {
175 struct stat fstatus;
177 switch (ResourceType) {
178 case BBTOOLS:
180 ReadBBtoolResource();
182 break;
183 case BLACKBOX:
185 if ((resource_db = XrmGetFileDatabase(bbtool->config_filename))!=NULL)
186 ReadBlackboxResource();
188 break;
190 LoadBBToolResource();
192 if ((bbtool->config_file!=NULL) && (style.auto_config)) {
193 if (stat(bbtool->config_filename,&fstatus)!=0) {
194 fprintf(stderr,"Can't use autoconfig");
195 style.auto_config=false;
196 mtime=0;
197 } else
198 mtime=fstatus.st_mtime;
201 XrmDestroyDatabase(resource_db);
204 bool BaseResource::ReadResourceFromFilename(const char *rname,
205 const char *rclass) {
206 struct stat fstatus;
207 char *filename=NULL;
208 XrmValue value;
209 char *value_type;
211 if (XrmGetResource(resource_db, (char *)rname, (char *)rclass, &value_type,
212 &value)) {
213 int len = strlen(value.addr);
214 delete [] filename;
215 filename = new char[len + 1];
216 memset(filename, 0, len + 1);
217 strncpy(filename, value.addr, len);
218 if (stat(filename,&fstatus)!=0) {
219 db=NULL;
220 delete [] filename;
221 return(False);
223 db = XrmGetFileDatabase(filename);
224 delete [] filename;
225 return(True);
227 db=NULL;
228 delete [] filename;
229 return(False);
232 void BaseResource::ReadBBtoolResource() {
234 if (bbtool->config_file) {
235 if ((resource_db = XrmGetFileDatabase(bbtool->config_file))==NULL) {
236 fprintf(stderr,"Could not open config file: %s\n",
237 bbtool->config_file);
238 fprintf(stderr,"Using internal defaults.\n");
240 else
241 bbtool->config_filename=bbtool->config_file;
243 else {
244 delete [] style.conf_filename;
245 char *homedir = getenv("HOME");
246 int len=strlen(homedir) + strlen(BBTOOL_LOCAL);
248 style.conf_filename = new char[len+2];
249 memset(style.conf_filename, 0, len + 2);
250 sprintf(style.conf_filename, "%s/%s", homedir,BBTOOL_LOCAL);
251 if ((resource_db = XrmGetFileDatabase(style.conf_filename))==NULL) {
252 delete [] style.conf_filename;
253 int len=strlen(GLOBAL_NOBB);
254 style.conf_filename = new char[len + 1];
255 memset(style.conf_filename, 0, len + 1);
256 strncpy(style.conf_filename,GLOBAL_NOBB, len);
257 if ((resource_db = XrmGetFileDatabase(style.conf_filename))==NULL) {
258 fprintf(stderr,"Could not open default config file: %s\n",
259 style.conf_filename);
260 fprintf(stderr,"Using internal defaults.\n");
262 else
263 bbtool->config_filename=bbtool->config_file;
265 else
266 bbtool->config_filename=bbtool->config_file;
271 void BaseResource::ReadBlackboxResource() {
273 if (!ReadResourceFromFilename("session.styleFile","Session.StyleFile")) {
274 fprintf(stderr,"Could not open blackbox style file\n");
275 } else
276 XrmCombineDatabase(db,&resource_db,False);
278 if (bbtool->config_file!=NULL) {
279 if ((db = XrmGetFileDatabase(bbtool->config_file))==NULL) {
280 fprintf(stderr,"Could not open config file: %s\n",
281 bbtool->config_file);
282 return;
283 } else {
284 XrmCombineDatabase(db,&resource_db,True);
286 } else {
287 delete [] style.conf_filename;
288 char *homedir = getenv("HOME");
289 int len=strlen(homedir) + strlen(BLACKBOX_LOCAL);
290 style.conf_filename = new char[len+2];
291 memset(style.conf_filename, 0, len + 2);
292 sprintf(style.conf_filename, "%s/%s", homedir,BLACKBOX_LOCAL);
293 if ((db = XrmGetFileDatabase(style.conf_filename))==NULL) {
294 delete [] style.conf_filename;
295 int len=strlen(GLOBAL_BB);
296 style.conf_filename = new char[len + 1];
297 memset(style.conf_filename, 0, len + 1);
298 strncpy(style.conf_filename,GLOBAL_BB, len);
299 if ((db = XrmGetFileDatabase(style.conf_filename))==NULL) {
300 fprintf(stderr,"Could not open default config file: %s\n",
301 style.conf_filename);
302 fprintf(stderr,"Using internal defaults.\n");
303 return;
305 else XrmCombineDatabase(db,&resource_db,True);
307 else XrmCombineDatabase(db,&resource_db,True);
311 void BaseResource::readTexture(const char *rname, const char *rclass,
312 const char *bbname, const char *bbclass,
313 const char *dcolor, const char *dcolorTo,
314 const char *dtexture, BTexture *texture) {
315 readDatabaseTexture(rname,rclass,texture);
316 if (!texture->getTexture()) {
317 readDatabaseTexture(bbname,bbclass,texture);
318 if (!texture->getTexture()) {
319 bbtool->getImageControl()->parseTexture(texture, dtexture);
320 bbtool->getImageControl()->parseColor(texture->getColor(), dcolor);
321 bbtool->getImageControl()->parseColor(texture->getColorTo(), dcolorTo);
326 void BaseResource::readColor(const char *rname, const char *rclass,
327 const char *bbname, const char *bbclass,
328 const char *dcolor, BColor *color) {
329 readDatabaseColor(rname,rclass,color);
330 if (!color->isAllocated()) {
331 readDatabaseColor(bbname,bbclass,color);
332 if (!color->isAllocated())
333 bbtool->getImageControl()->parseColor(color,dcolor);
337 void BaseResource::readDatabaseTexture(const char *rname, const char *rclass,
338 BTexture *texture) {
339 XrmValue value;
340 char *value_type;
342 texture->setTexture(0);
344 if (XrmGetResource(resource_db, rname, rclass, &value_type,
345 &value))
346 bbtool->getImageControl()->parseTexture(texture, value.addr);
348 if (texture->getTexture() & BImage_Solid) {
349 int clen = strlen(rclass) + 8, nlen = strlen(rname) + 8;
350 char *colorclass = new char[clen], *colorname = new char[nlen];
352 sprintf(colorclass, "%s.Color", rclass);
353 sprintf(colorname, "%s.color", rname);
355 readDatabaseColor(colorname, colorclass, texture->getColor());
357 delete [] colorclass;
358 delete [] colorname;
360 if ((! texture->getColor()->isAllocated()) ||
361 (texture->getTexture() & BImage_Flat))
362 return;
364 XColor xcol;
366 xcol.red = (unsigned int) (texture->getColor()->getRed() +
367 (texture->getColor()->getRed() >> 1));
368 if (xcol.red >= 0xff) xcol.red = 0xffff;
369 else xcol.red *= 0xff;
370 xcol.green = (unsigned int) (texture->getColor()->getGreen() +
371 (texture->getColor()->getGreen() >> 1));
372 if (xcol.green >= 0xff) xcol.green = 0xffff;
373 else xcol.green *= 0xff;
374 xcol.blue = (unsigned int) (texture->getColor()->getBlue() +
375 (texture->getColor()->getBlue() >> 1));
376 if (xcol.blue >= 0xff) xcol.blue = 0xffff;
377 else xcol.blue *= 0xff;
379 if (! XAllocColor(bbtool->getXDisplay(), bbtool->getImageControl()->getColormap(),
380 &xcol))
381 xcol.pixel = 0;
383 texture->getHiColor()->setPixel(xcol.pixel);
385 xcol.red =
386 (unsigned int) ((texture->getColor()->getRed() >> 2) +
387 (texture->getColor()->getRed() >> 1)) * 0xff;
388 xcol.green =
389 (unsigned int) ((texture->getColor()->getGreen() >> 2) +
390 (texture->getColor()->getGreen() >> 1)) * 0xff;
391 xcol.blue =
392 (unsigned int) ((texture->getColor()->getBlue() >> 2) +
393 (texture->getColor()->getBlue() >> 1)) * 0xff;
395 if (! XAllocColor(bbtool->getXDisplay(), bbtool->getImageControl()->getColormap(),
396 &xcol))
397 xcol.pixel = 0;
399 texture->getLoColor()->setPixel(xcol.pixel);
400 } else if (texture->getTexture() & BImage_Gradient) {
401 int clen = strlen(rclass) + 10, nlen = strlen(rname) + 10;
402 char *colorclass = new char[clen], *colorname = new char[nlen],
403 *colortoclass = new char[clen], *colortoname = new char[nlen];
405 sprintf(colorclass, "%s.Color", rclass);
406 sprintf(colorname, "%s.color", rname);
408 sprintf(colortoclass, "%s.ColorTo", rclass);
409 sprintf(colortoname, "%s.colorTo", rname);
411 readDatabaseColor(colorname, colorclass, texture->getColor());
412 readDatabaseColor(colortoname, colortoclass, texture->getColorTo());
414 delete [] colorclass;
415 delete [] colorname;
416 delete [] colortoclass;
417 delete [] colortoname;
421 void BaseResource::readDatabaseColor(const char *rname,
422 const char *rclass, BColor *color) {
423 XrmValue value;
424 char *value_type;
425 if (XrmGetResource(resource_db, rname, rclass, &value_type,
426 &value))
427 bbtool->getImageControl()->parseColor(color, value.addr);
428 else
429 // parsing with no color string just deallocates the color, if it has
430 // been previously allocated
431 bbtool->getImageControl()->parseColor(color);