initial import
[bbkeys.git] / Baseresource.cc
blob1b8db21aa66369f8b45ff3c0b9e16310490f188e
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)
22 #include <stdlib.h>
23 #include "bbkeys.hh"
24 #include "Baseresource.hh"
25 #include "blackboxstyle.hh"
27 BaseResource::BaseResource(ToolWindow *toolwindow) {
28 XrmValue value;
29 char *value_type;
31 bbtool=toolwindow;
32 style.style_filename=NULL;
33 style.conf_filename=NULL;
35 if (bbtool->nobb_config) {
36 ReadBBtoolResource();
37 ResourceType=BBTOOLS;
38 } else {
39 char *homedir = getenv("HOME");
40 bbtool->config_filename = new char[strlen(homedir) + 32];
41 sprintf(bbtool->config_filename, "%s/.blackboxrc", homedir);
42 if ((resource_db = XrmGetFileDatabase(bbtool->config_filename))!=NULL) {
43 ReadBlackboxResource();
44 ResourceType=BLACKBOX;
46 else {
47 ReadBBtoolResource();
48 ResourceType=BBTOOLS;
52 if (XrmGetResource(resource_db, "session.colorsPerChannel",
53 "Session.ColorsPerChannel", &value_type, &value)) {
54 if (sscanf(value.addr, "%d", &bbtool->colors_per_channel) != 1) {
55 bbtool->colors_per_channel = 4;
56 } else {
57 if (bbtool->colors_per_channel < 2) bbtool->colors_per_channel = 2;
58 if (bbtool->colors_per_channel > 6) bbtool->colors_per_channel = 6;
60 } else
61 bbtool->colors_per_channel = 4;
64 if (XrmGetResource(resource_db, "session.imageDither",
65 "Session.ImageDither", &value_type, &value)) {
66 if (! strncasecmp("true", value.addr, value.size))
67 bbtool->image_dither = True;
68 else
69 bbtool->image_dither = False;
70 } else
71 bbtool->image_dither = True;
73 if (bbtool->image_dither &&
74 bbtool->getCurrentScreenInfo()->getVisual()->c_class == TrueColor
75 && bbtool->getCurrentScreenInfo()->getDepth() >= 24)
76 bbtool->image_dither = False;
78 /* Need to do this here */
79 bbtool->setupImageControl();
81 if (XrmGetResource(resource_db, "bbkeys.autoConfig",
82 "Bbkeys.Autoconfig", &value_type, &value)) {
83 if (! strncasecmp("true", value.addr, value.size)) {
84 auto_config = True;
85 } else
86 auto_config = False;
87 } else
88 auto_config = False;
90 if (XrmGetResource(resource_db, "bbkeys.honorModifiers",
91 "Bbkeys.Honormodifiers", &value_type, &value)) {
92 if (! strncasecmp("true", value.addr, value.size)) {
93 bbtool->honor_modifiers = True;
94 } else
95 bbtool->honor_modifiers = False;
96 } else
97 bbtool->honor_modifiers = False;
99 if (bbtool->withdrawn) auto_config=False;
101 if (XrmGetResource(resource_db, "bbkeys.autoConfig.checkTimeout",
102 "Bbkeys.Autoconfig.CheckTimeout", &value_type, &value)) {
103 if (sscanf(value.addr, "%u", &check_timeout) != 1)
104 check_timeout = 10;
106 else
107 check_timeout = 10;
109 if ((bbtool->config_filename!=NULL)&(auto_config)) {
110 if (stat(bbtool->config_filename,&file_status)!=0) {
111 fprintf(stderr,"Can't use autoconfig");
112 auto_config=false;
113 mtime=0;
114 } else {
115 mtime=file_status.st_mtime;
117 timer=new BTimer(toolwindow->getCurrentScreenInfo()->getBaseDisplay(),
118 this);
119 timer->setTimeout(1000*check_timeout);
120 timer->start();
126 void BaseResource::Load() {
128 LoadBBToolResource();
129 XrmDestroyDatabase(resource_db);
132 BaseResource::~BaseResource() {
133 delete [] style.style_filename;
134 delete [] style.conf_filename;
136 style.style_filename=NULL;
137 style.conf_filename=NULL;
140 void BaseResource::timeout() {
141 if (stat(bbtool->config_filename,&file_status)!=0) {
142 fprintf(stderr,"Autoconfig error: Cannot get status of:%s\n",
143 bbtool->config_filename);
144 style.mtime=0;
145 } else {
146 if (mtime!=file_status.st_mtime) {
147 bbtool->reconfigure();
148 mtime=file_status.st_mtime;
153 void BaseResource::CopyColor(BColor *Color1,BColor *Color2) {
154 Color2->setPixel(Color1->getPixel());
155 Color2->setRGB(Color1->getRed(),Color1->getGreen(),Color1->getBlue());
158 void BaseResource::CopyTexture(BTexture Texture1,BTexture *Texture2) {
159 CopyColor(Texture1.getColor(),Texture2->getColor());
160 CopyColor(Texture1.getColorTo(),Texture2->getColorTo());
161 CopyColor(Texture1.getHiColor(),Texture2->getHiColor());
162 CopyColor(Texture1.getLoColor(),Texture2->getLoColor());
163 Texture2->setTexture(Texture1.getTexture());
166 void BaseResource::Reload() {
167 struct stat file_status;
169 switch (ResourceType) {
170 case BBTOOLS:
172 ReadBBtoolResource();
174 break;
175 case BLACKBOX:
177 if ((resource_db = XrmGetFileDatabase(bbtool->config_filename))!=NULL)
178 ReadBlackboxResource();
180 break;
182 LoadBBToolResource();
184 if ((bbtool->config_file!=NULL) && (style.auto_config)) {
185 if (stat(bbtool->config_filename,&file_status)!=0) {
186 fprintf(stderr,"Can't use autoconfig");
187 style.auto_config=false;
188 mtime=0;
189 } else
190 mtime=file_status.st_mtime;
193 XrmDestroyDatabase(resource_db);
196 bool BaseResource::ReadResourceFromFilename(char *rname, char *rclass) {
197 struct stat file_status;
198 char *filename=NULL;
199 XrmValue value;
200 char *value_type;
202 if (XrmGetResource(resource_db,rname,rclass, &value_type, &value)) {
203 int len = strlen(value.addr);
204 delete [] filename;
205 filename = new char[len + 1];
206 memset(filename, 0, len + 1);
207 strncpy(filename, value.addr, len);
208 if (stat(filename,&file_status)!=0) {
209 db=NULL;
210 delete [] filename;
211 return(False);
213 db = XrmGetFileDatabase(filename);
214 delete [] filename;
215 return(True);
217 db=NULL;
218 delete [] filename;
219 return(False);
222 void BaseResource::ReadBBtoolResource() {
224 if (bbtool->config_file) {
225 if ((resource_db = XrmGetFileDatabase(bbtool->config_file))==NULL) {
226 fprintf(stderr,"Could not open config file: %s\n",
227 bbtool->config_file);
228 fprintf(stderr,"Using internal defaults.\n");
230 else
231 bbtool->config_filename=bbtool->config_file;
233 else {
234 delete [] style.conf_filename;
235 char *homedir = getenv("HOME");
236 int len=strlen(homedir) + strlen(BBTOOL_LOCAL);
238 style.conf_filename = new char[len+2];
239 memset(style.conf_filename, 0, len + 2);
240 sprintf(style.conf_filename, "%s/%s", homedir,BBTOOL_LOCAL);
241 if ((resource_db = XrmGetFileDatabase(style.conf_filename))==NULL) {
242 delete [] style.conf_filename;
243 int len=strlen(GLOBAL_NOBB);
244 style.conf_filename = new char[len + 1];
245 memset(style.conf_filename, 0, len + 1);
246 strncpy(style.conf_filename,GLOBAL_NOBB, len);
247 if ((resource_db = XrmGetFileDatabase(style.conf_filename))==NULL) {
248 fprintf(stderr,"Could not open default config file: %s\n",
249 style.conf_filename);
250 fprintf(stderr,"Using internal defaults.\n");
252 else
253 bbtool->config_filename=bbtool->config_file;
255 else
256 bbtool->config_filename=bbtool->config_file;
261 void BaseResource::ReadBlackboxResource() {
263 if (!ReadResourceFromFilename("session.styleFile","Session.StyleFile")) {
264 fprintf(stderr,"Could not open blackbox style file\n");
265 } else
266 XrmCombineDatabase(db,&resource_db,False);
268 if (bbtool->config_file!=NULL) {
269 if ((db = XrmGetFileDatabase(bbtool->config_file))==NULL) {
270 fprintf(stderr,"Could not open config file: %s\n",
271 bbtool->config_file);
272 return;
273 } else {
274 XrmCombineDatabase(db,&resource_db,True);
276 } else {
277 delete [] style.conf_filename;
278 char *homedir = getenv("HOME");
279 int len=strlen(homedir) + strlen(BLACKBOX_LOCAL);
280 style.conf_filename = new char[len+2];
281 memset(style.conf_filename, 0, len + 2);
282 sprintf(style.conf_filename, "%s/%s", homedir,BLACKBOX_LOCAL);
283 if ((db = XrmGetFileDatabase(style.conf_filename))==NULL) {
284 delete [] style.conf_filename;
285 int len=strlen(GLOBAL_BB);
286 style.conf_filename = new char[len + 1];
287 memset(style.conf_filename, 0, len + 1);
288 strncpy(style.conf_filename,GLOBAL_BB, len);
289 if ((db = XrmGetFileDatabase(style.conf_filename))==NULL) {
290 fprintf(stderr,"Could not open default config file: %s\n",
291 style.conf_filename);
292 fprintf(stderr,"Using internal defaults.\n");
293 return;
295 else XrmCombineDatabase(db,&resource_db,True);
297 else XrmCombineDatabase(db,&resource_db,True);
301 void BaseResource::readTexture(char *rname,char *rclass, char *bbname,
302 char *bbclass,char *dcolor,char *dcolorTo,
303 char *dtexture,BTexture *texture) {
304 readDatabaseTexture(rname,rclass,texture);
305 if (!texture->getTexture()) {
306 readDatabaseTexture(bbname,bbclass,texture);
307 if (!texture->getTexture()) {
308 bbtool->getImageControl()->parseTexture(texture, dtexture);
309 bbtool->getImageControl()->parseColor(texture->getColor(), dcolor);
310 bbtool->getImageControl()->parseColor(texture->getColorTo(), dcolorTo);
315 void BaseResource::readColor(char *rname,char *rclass, char *bbname,
316 char *bbclass,char *dcolor,BColor *color) {
317 readDatabaseColor(rname,rclass,color);
318 if (!color->isAllocated()) {
319 readDatabaseColor(bbname,bbclass,color);
320 if (!color->isAllocated())
321 bbtool->getImageControl()->parseColor(color,dcolor);
325 void BaseResource::readDatabaseTexture(char *rname, char *rclass,
326 BTexture *texture) {
327 XrmValue value;
328 char *value_type;
330 texture->setTexture(0);
332 if (XrmGetResource(resource_db, rname, rclass, &value_type,
333 &value))
334 bbtool->getImageControl()->parseTexture(texture, value.addr);
336 if (texture->getTexture() & BImage_Solid) {
337 int clen = strlen(rclass) + 8, nlen = strlen(rname) + 8;
338 char *colorclass = new char[clen], *colorname = new char[nlen];
340 sprintf(colorclass, "%s.Color", rclass);
341 sprintf(colorname, "%s.color", rname);
343 readDatabaseColor(colorname, colorclass, texture->getColor());
345 delete [] colorclass;
346 delete [] colorname;
348 if ((! texture->getColor()->isAllocated()) ||
349 (texture->getTexture() & BImage_Flat))
350 return;
352 XColor xcol;
354 xcol.red = (unsigned int) (texture->getColor()->getRed() +
355 (texture->getColor()->getRed() >> 1));
356 if (xcol.red >= 0xff) xcol.red = 0xffff;
357 else xcol.red *= 0xff;
358 xcol.green = (unsigned int) (texture->getColor()->getGreen() +
359 (texture->getColor()->getGreen() >> 1));
360 if (xcol.green >= 0xff) xcol.green = 0xffff;
361 else xcol.green *= 0xff;
362 xcol.blue = (unsigned int) (texture->getColor()->getBlue() +
363 (texture->getColor()->getBlue() >> 1));
364 if (xcol.blue >= 0xff) xcol.blue = 0xffff;
365 else xcol.blue *= 0xff;
367 if (! XAllocColor(bbtool->getXDisplay(), bbtool->getImageControl()->getColormap(),
368 &xcol))
369 xcol.pixel = 0;
371 texture->getHiColor()->setPixel(xcol.pixel);
373 xcol.red =
374 (unsigned int) ((texture->getColor()->getRed() >> 2) +
375 (texture->getColor()->getRed() >> 1)) * 0xff;
376 xcol.green =
377 (unsigned int) ((texture->getColor()->getGreen() >> 2) +
378 (texture->getColor()->getGreen() >> 1)) * 0xff;
379 xcol.blue =
380 (unsigned int) ((texture->getColor()->getBlue() >> 2) +
381 (texture->getColor()->getBlue() >> 1)) * 0xff;
383 if (! XAllocColor(bbtool->getXDisplay(), bbtool->getImageControl()->getColormap(),
384 &xcol))
385 xcol.pixel = 0;
387 texture->getLoColor()->setPixel(xcol.pixel);
388 } else if (texture->getTexture() & BImage_Gradient) {
389 int clen = strlen(rclass) + 10, nlen = strlen(rname) + 10;
390 char *colorclass = new char[clen], *colorname = new char[nlen],
391 *colortoclass = new char[clen], *colortoname = new char[nlen];
393 sprintf(colorclass, "%s.Color", rclass);
394 sprintf(colorname, "%s.color", rname);
396 sprintf(colortoclass, "%s.ColorTo", rclass);
397 sprintf(colortoname, "%s.colorTo", rname);
399 readDatabaseColor(colorname, colorclass, texture->getColor());
400 readDatabaseColor(colortoname, colortoclass, texture->getColorTo());
402 delete [] colorclass;
403 delete [] colorname;
404 delete [] colortoclass;
405 delete [] colortoname;
409 void BaseResource::readDatabaseColor(char *rname, char *rclass, BColor *color) {
410 XrmValue value;
411 char *value_type;
412 if (XrmGetResource(resource_db, rname, rclass, &value_type,
413 &value))
414 bbtool->getImageControl()->parseColor(color, value.addr);
415 else
416 // parsing with no color string just deallocates the color, if it has
417 // been previously allocated
418 bbtool->getImageControl()->parseColor(color);