1 // Baseresource.cc for bbtools - tools to display resources in X11.
3 // Copyright (c) 1998-1999 John Kennis, jkennis@chello.nl
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.
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)
25 #include "Baseresource.hh"
26 #include "blackboxstyle.hh"
29 # include "../config.h"
30 #endif // HAVE_CONFIG_H
32 BaseResource::BaseResource(ToolWindow
*toolwindow
) {
37 style
.style_filename
=NULL
;
38 style
.conf_filename
=NULL
;
40 if (bbtool
->nobb_config
) {
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
;
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;
62 if (bbtool
->colors_per_channel
< 2) bbtool
->colors_per_channel
= 2;
63 if (bbtool
->colors_per_channel
> 6) bbtool
->colors_per_channel
= 6;
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
;
74 bbtool
->image_dither
= False
;
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
)) {
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
;
100 bbtool
->honor_modifiers
= False
;
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)
114 if ((bbtool
->config_filename
!=NULL
)&(auto_config
)) {
115 if (stat(bbtool
->config_filename
,&file_status
)!=0) {
116 fprintf(stderr
,"Can't use autoconfig");
120 mtime
=file_status
.st_mtime
;
122 timer
=new BTimer(toolwindow
->getCurrentScreenInfo()->getBaseDisplay(),
124 timer
->setTimeout(1000*check_timeout
);
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
);
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
) {
177 ReadBBtoolResource();
182 if ((resource_db
= XrmGetFileDatabase(bbtool
->config_filename
))!=NULL
)
183 ReadBlackboxResource();
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;
195 mtime
=file_status
.st_mtime
;
198 XrmDestroyDatabase(resource_db
);
201 bool BaseResource::ReadResourceFromFilename(char *rname
, char *rclass
) {
202 struct stat file_status
;
207 if (XrmGetResource(resource_db
,rname
,rclass
, &value_type
, &value
)) {
208 int len
= strlen(value
.addr
);
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) {
218 db
= XrmGetFileDatabase(filename
);
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");
236 bbtool
->config_filename
=bbtool
->config_file
;
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");
258 bbtool
->config_filename
=bbtool
->config_file
;
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");
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
);
279 XrmCombineDatabase(db
,&resource_db
,True
);
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");
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
,
335 texture
->setTexture(0);
337 if (XrmGetResource(resource_db
, rname
, rclass
, &value_type
,
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
;
353 if ((! texture
->getColor()->isAllocated()) ||
354 (texture
->getTexture() & BImage_Flat
))
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(),
376 texture
->getHiColor()->setPixel(xcol
.pixel
);
379 (unsigned int) ((texture
->getColor()->getRed() >> 2) +
380 (texture
->getColor()->getRed() >> 1)) * 0xff;
382 (unsigned int) ((texture
->getColor()->getGreen() >> 2) +
383 (texture
->getColor()->getGreen() >> 1)) * 0xff;
385 (unsigned int) ((texture
->getColor()->getBlue() >> 2) +
386 (texture
->getColor()->getBlue() >> 1)) * 0xff;
388 if (! XAllocColor(bbtool
->getXDisplay(), bbtool
->getImageControl()->getColormap(),
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
;
409 delete [] colortoclass
;
410 delete [] colortoname
;
414 void BaseResource::readDatabaseColor(char *rname
, char *rclass
, BColor
*color
) {
417 if (XrmGetResource(resource_db
, rname
, rclass
, &value_type
,
419 bbtool
->getImageControl()->parseColor(color
, value
.addr
);
421 // parsing with no color string just deallocates the color, if it has
422 // been previously allocated
423 bbtool
->getImageControl()->parseColor(color
);