- changing MaxInstructions to 100 for cthulhain (crazy nut) =:)
[bbkeys.git] / src / Baseresource.cc
bloba390ce504f872d52eed29af9e452b800189736a0
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 #include <stdlib.h>
24 #include "bbkeys.hh"
25 #include "Baseresource.hh"
26 #include "blackboxstyle.hh"
28 #ifdef HAVE_CONFIG_H
29 # include "../config.h"
30 #endif // HAVE_CONFIG_H
32 BaseResource::BaseResource(ToolWindow *toolwindow) {
33 XrmValue value;
34 char *value_type;
36 bbtool=toolwindow;
37 style.style_filename=NULL;
38 style.conf_filename=NULL;
40 if (bbtool->nobb_config) {
41 ReadBBtoolResource();
42 ResourceType=BBTOOLS;
43 } else {
44 char *homedir = getenv("HOME");
45 bbtool->config_filename = new char[strlen(homedir) + 32];
46 sprintf(bbtool->config_filename, "%s/.blackboxrc", homedir);
47 if ((resource_db = XrmGetFileDatabase(bbtool->config_filename))!=NULL) {
48 ReadBlackboxResource();
49 ResourceType=BLACKBOX;
51 else {
52 ReadBBtoolResource();
53 ResourceType=BBTOOLS;
57 if (XrmGetResource(resource_db, "session.colorsPerChannel",
58 "Session.ColorsPerChannel", &value_type, &value)) {
59 if (sscanf(value.addr, "%d", &bbtool->colors_per_channel) != 1) {
60 bbtool->colors_per_channel = 4;
61 } else {
62 if (bbtool->colors_per_channel < 2) bbtool->colors_per_channel = 2;
63 if (bbtool->colors_per_channel > 6) bbtool->colors_per_channel = 6;
65 } else
66 bbtool->colors_per_channel = 4;
69 if (XrmGetResource(resource_db, "session.imageDither",
70 "Session.ImageDither", &value_type, &value)) {
71 if (! strncasecmp("true", value.addr, value.size))
72 bbtool->image_dither = True;
73 else
74 bbtool->image_dither = False;
75 } else
76 bbtool->image_dither = True;
78 if (bbtool->image_dither &&
79 bbtool->getCurrentScreenInfo()->getVisual()->c_class == TrueColor
80 && bbtool->getCurrentScreenInfo()->getDepth() >= 24)
81 bbtool->image_dither = False;
83 /* Need to do this here */
84 bbtool->setupImageControl();
86 if (XrmGetResource(resource_db, "bbkeys.autoConfig",
87 "Bbkeys.Autoconfig", &value_type, &value)) {
88 if (! strncasecmp("true", value.addr, value.size)) {
89 auto_config = True;
90 } else
91 auto_config = False;
92 } else
93 auto_config = False;
95 if (XrmGetResource(resource_db, "bbkeys.honorModifiers",
96 "Bbkeys.Honormodifiers", &value_type, &value)) {
97 if (! strncasecmp("true", value.addr, value.size)) {
98 bbtool->honor_modifiers = True;
99 } else
100 bbtool->honor_modifiers = False;
101 } else
102 bbtool->honor_modifiers = False;
104 if (bbtool->withdrawn) auto_config=False;
106 if (XrmGetResource(resource_db, "bbkeys.autoConfig.checkTimeout",
107 "Bbkeys.Autoconfig.CheckTimeout", &value_type, &value)) {
108 if (sscanf(value.addr, "%u", &check_timeout) != 1)
109 check_timeout = 10;
111 else
112 check_timeout = 10;
114 if ((bbtool->config_filename!=NULL)&(auto_config)) {
115 if (stat(bbtool->config_filename,&file_status)!=0) {
116 fprintf(stderr,"Can't use autoconfig");
117 auto_config=false;
118 mtime=0;
119 } else {
120 mtime=file_status.st_mtime;
122 timer=new BTimer(toolwindow->getCurrentScreenInfo()->getBaseDisplay(),
123 this);
124 timer->setTimeout(1000*check_timeout);
125 timer->start();
131 void BaseResource::Load() {
133 LoadBBToolResource();
134 XrmDestroyDatabase(resource_db);
137 BaseResource::~BaseResource() {
138 delete [] style.style_filename;
139 delete [] style.conf_filename;
141 style.style_filename=NULL;
142 style.conf_filename=NULL;
145 void BaseResource::timeout() {
146 if (stat(bbtool->config_filename,&file_status)!=0) {
147 fprintf(stderr,"Autoconfig error: Cannot get status of:%s\n",
148 bbtool->config_filename);
149 style.mtime=0;
150 } else {
151 if (mtime!=file_status.st_mtime) {
152 bbtool->reconfigure();
153 mtime=file_status.st_mtime;
158 void BaseResource::CopyColor(BColor *Color1,BColor *Color2) {
159 Color2->setPixel(Color1->getPixel());
160 Color2->setRGB(Color1->getRed(),Color1->getGreen(),Color1->getBlue());
163 void BaseResource::CopyTexture(BTexture Texture1,BTexture *Texture2) {
164 CopyColor(Texture1.getColor(),Texture2->getColor());
165 CopyColor(Texture1.getColorTo(),Texture2->getColorTo());
166 CopyColor(Texture1.getHiColor(),Texture2->getHiColor());
167 CopyColor(Texture1.getLoColor(),Texture2->getLoColor());
168 Texture2->setTexture(Texture1.getTexture());
171 void BaseResource::Reload() {
172 struct stat file_status;
174 switch (ResourceType) {
175 case BBTOOLS:
177 ReadBBtoolResource();
179 break;
180 case BLACKBOX:
182 if ((resource_db = XrmGetFileDatabase(bbtool->config_filename))!=NULL)
183 ReadBlackboxResource();
185 break;
187 LoadBBToolResource();
189 if ((bbtool->config_file!=NULL) && (style.auto_config)) {
190 if (stat(bbtool->config_filename,&file_status)!=0) {
191 fprintf(stderr,"Can't use autoconfig");
192 style.auto_config=false;
193 mtime=0;
194 } else
195 mtime=file_status.st_mtime;
198 XrmDestroyDatabase(resource_db);
201 bool BaseResource::ReadResourceFromFilename(char *rname, char *rclass) {
202 struct stat file_status;
203 char *filename=NULL;
204 XrmValue value;
205 char *value_type;
207 if (XrmGetResource(resource_db,rname,rclass, &value_type, &value)) {
208 int len = strlen(value.addr);
209 delete [] filename;
210 filename = new char[len + 1];
211 memset(filename, 0, len + 1);
212 strncpy(filename, value.addr, len);
213 if (stat(filename,&file_status)!=0) {
214 db=NULL;
215 delete [] filename;
216 return(False);
218 db = XrmGetFileDatabase(filename);
219 delete [] filename;
220 return(True);
222 db=NULL;
223 delete [] filename;
224 return(False);
227 void BaseResource::ReadBBtoolResource() {
229 if (bbtool->config_file) {
230 if ((resource_db = XrmGetFileDatabase(bbtool->config_file))==NULL) {
231 fprintf(stderr,"Could not open config file: %s\n",
232 bbtool->config_file);
233 fprintf(stderr,"Using internal defaults.\n");
235 else
236 bbtool->config_filename=bbtool->config_file;
238 else {
239 delete [] style.conf_filename;
240 char *homedir = getenv("HOME");
241 int len=strlen(homedir) + strlen(BBTOOL_LOCAL);
243 style.conf_filename = new char[len+2];
244 memset(style.conf_filename, 0, len + 2);
245 sprintf(style.conf_filename, "%s/%s", homedir,BBTOOL_LOCAL);
246 if ((resource_db = XrmGetFileDatabase(style.conf_filename))==NULL) {
247 delete [] style.conf_filename;
248 int len=strlen(GLOBAL_NOBB);
249 style.conf_filename = new char[len + 1];
250 memset(style.conf_filename, 0, len + 1);
251 strncpy(style.conf_filename,GLOBAL_NOBB, len);
252 if ((resource_db = XrmGetFileDatabase(style.conf_filename))==NULL) {
253 fprintf(stderr,"Could not open default config file: %s\n",
254 style.conf_filename);
255 fprintf(stderr,"Using internal defaults.\n");
257 else
258 bbtool->config_filename=bbtool->config_file;
260 else
261 bbtool->config_filename=bbtool->config_file;
266 void BaseResource::ReadBlackboxResource() {
268 if (!ReadResourceFromFilename("session.styleFile","Session.StyleFile")) {
269 fprintf(stderr,"Could not open blackbox style file\n");
270 } else
271 XrmCombineDatabase(db,&resource_db,False);
273 if (bbtool->config_file!=NULL) {
274 if ((db = XrmGetFileDatabase(bbtool->config_file))==NULL) {
275 fprintf(stderr,"Could not open config file: %s\n",
276 bbtool->config_file);
277 return;
278 } else {
279 XrmCombineDatabase(db,&resource_db,True);
281 } else {
282 delete [] style.conf_filename;
283 char *homedir = getenv("HOME");
284 int len=strlen(homedir) + strlen(BLACKBOX_LOCAL);
285 style.conf_filename = new char[len+2];
286 memset(style.conf_filename, 0, len + 2);
287 sprintf(style.conf_filename, "%s/%s", homedir,BLACKBOX_LOCAL);
288 if ((db = XrmGetFileDatabase(style.conf_filename))==NULL) {
289 delete [] style.conf_filename;
290 int len=strlen(GLOBAL_BB);
291 style.conf_filename = new char[len + 1];
292 memset(style.conf_filename, 0, len + 1);
293 strncpy(style.conf_filename,GLOBAL_BB, len);
294 if ((db = XrmGetFileDatabase(style.conf_filename))==NULL) {
295 fprintf(stderr,"Could not open default config file: %s\n",
296 style.conf_filename);
297 fprintf(stderr,"Using internal defaults.\n");
298 return;
300 else XrmCombineDatabase(db,&resource_db,True);
302 else XrmCombineDatabase(db,&resource_db,True);
306 void BaseResource::readTexture(char *rname,char *rclass, char *bbname,
307 char *bbclass,char *dcolor,char *dcolorTo,
308 char *dtexture,BTexture *texture) {
309 readDatabaseTexture(rname,rclass,texture);
310 if (!texture->getTexture()) {
311 readDatabaseTexture(bbname,bbclass,texture);
312 if (!texture->getTexture()) {
313 bbtool->getImageControl()->parseTexture(texture, dtexture);
314 bbtool->getImageControl()->parseColor(texture->getColor(), dcolor);
315 bbtool->getImageControl()->parseColor(texture->getColorTo(), dcolorTo);
320 void BaseResource::readColor(char *rname,char *rclass, char *bbname,
321 char *bbclass,char *dcolor,BColor *color) {
322 readDatabaseColor(rname,rclass,color);
323 if (!color->isAllocated()) {
324 readDatabaseColor(bbname,bbclass,color);
325 if (!color->isAllocated())
326 bbtool->getImageControl()->parseColor(color,dcolor);
330 void BaseResource::readDatabaseTexture(char *rname, char *rclass,
331 BTexture *texture) {
332 XrmValue value;
333 char *value_type;
335 texture->setTexture(0);
337 if (XrmGetResource(resource_db, rname, rclass, &value_type,
338 &value))
339 bbtool->getImageControl()->parseTexture(texture, value.addr);
341 if (texture->getTexture() & BImage_Solid) {
342 int clen = strlen(rclass) + 8, nlen = strlen(rname) + 8;
343 char *colorclass = new char[clen], *colorname = new char[nlen];
345 sprintf(colorclass, "%s.Color", rclass);
346 sprintf(colorname, "%s.color", rname);
348 readDatabaseColor(colorname, colorclass, texture->getColor());
350 delete [] colorclass;
351 delete [] colorname;
353 if ((! texture->getColor()->isAllocated()) ||
354 (texture->getTexture() & BImage_Flat))
355 return;
357 XColor xcol;
359 xcol.red = (unsigned int) (texture->getColor()->getRed() +
360 (texture->getColor()->getRed() >> 1));
361 if (xcol.red >= 0xff) xcol.red = 0xffff;
362 else xcol.red *= 0xff;
363 xcol.green = (unsigned int) (texture->getColor()->getGreen() +
364 (texture->getColor()->getGreen() >> 1));
365 if (xcol.green >= 0xff) xcol.green = 0xffff;
366 else xcol.green *= 0xff;
367 xcol.blue = (unsigned int) (texture->getColor()->getBlue() +
368 (texture->getColor()->getBlue() >> 1));
369 if (xcol.blue >= 0xff) xcol.blue = 0xffff;
370 else xcol.blue *= 0xff;
372 if (! XAllocColor(bbtool->getXDisplay(), bbtool->getImageControl()->getColormap(),
373 &xcol))
374 xcol.pixel = 0;
376 texture->getHiColor()->setPixel(xcol.pixel);
378 xcol.red =
379 (unsigned int) ((texture->getColor()->getRed() >> 2) +
380 (texture->getColor()->getRed() >> 1)) * 0xff;
381 xcol.green =
382 (unsigned int) ((texture->getColor()->getGreen() >> 2) +
383 (texture->getColor()->getGreen() >> 1)) * 0xff;
384 xcol.blue =
385 (unsigned int) ((texture->getColor()->getBlue() >> 2) +
386 (texture->getColor()->getBlue() >> 1)) * 0xff;
388 if (! XAllocColor(bbtool->getXDisplay(), bbtool->getImageControl()->getColormap(),
389 &xcol))
390 xcol.pixel = 0;
392 texture->getLoColor()->setPixel(xcol.pixel);
393 } else if (texture->getTexture() & BImage_Gradient) {
394 int clen = strlen(rclass) + 10, nlen = strlen(rname) + 10;
395 char *colorclass = new char[clen], *colorname = new char[nlen],
396 *colortoclass = new char[clen], *colortoname = new char[nlen];
398 sprintf(colorclass, "%s.Color", rclass);
399 sprintf(colorname, "%s.color", rname);
401 sprintf(colortoclass, "%s.ColorTo", rclass);
402 sprintf(colortoname, "%s.colorTo", rname);
404 readDatabaseColor(colorname, colorclass, texture->getColor());
405 readDatabaseColor(colortoname, colortoclass, texture->getColorTo());
407 delete [] colorclass;
408 delete [] colorname;
409 delete [] colortoclass;
410 delete [] colortoname;
414 void BaseResource::readDatabaseColor(char *rname, char *rclass, BColor *color) {
415 XrmValue value;
416 char *value_type;
417 if (XrmGetResource(resource_db, rname, rclass, &value_type,
418 &value))
419 bbtool->getImageControl()->parseColor(color, value.addr);
420 else
421 // parsing with no color string just deallocates the color, if it has
422 // been previously allocated
423 bbtool->getImageControl()->parseColor(color);