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)
24 #include "Baseresource.hh"
25 #include "blackboxstyle.hh"
27 BaseResource::BaseResource(ToolWindow
*toolwindow
) {
32 style
.style_filename
=NULL
;
33 style
.conf_filename
=NULL
;
35 if (bbtool
->nobb_config
) {
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
;
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;
57 if (bbtool
->colors_per_channel
< 2) bbtool
->colors_per_channel
= 2;
58 if (bbtool
->colors_per_channel
> 6) bbtool
->colors_per_channel
= 6;
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
;
69 bbtool
->image_dither
= False
;
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
)) {
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
;
95 bbtool
->honor_modifiers
= False
;
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)
109 if ((bbtool
->config_filename
!=NULL
)&(auto_config
)) {
110 if (stat(bbtool
->config_filename
,&file_status
)!=0) {
111 fprintf(stderr
,"Can't use autoconfig");
115 mtime
=file_status
.st_mtime
;
117 timer
=new BTimer(toolwindow
->getCurrentScreenInfo()->getBaseDisplay(),
119 timer
->setTimeout(1000*check_timeout
);
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
);
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
) {
172 ReadBBtoolResource();
177 if ((resource_db
= XrmGetFileDatabase(bbtool
->config_filename
))!=NULL
)
178 ReadBlackboxResource();
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;
190 mtime
=file_status
.st_mtime
;
193 XrmDestroyDatabase(resource_db
);
196 bool BaseResource::ReadResourceFromFilename(char *rname
, char *rclass
) {
197 struct stat file_status
;
202 if (XrmGetResource(resource_db
,rname
,rclass
, &value_type
, &value
)) {
203 int len
= strlen(value
.addr
);
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) {
213 db
= XrmGetFileDatabase(filename
);
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");
231 bbtool
->config_filename
=bbtool
->config_file
;
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");
253 bbtool
->config_filename
=bbtool
->config_file
;
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");
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
);
274 XrmCombineDatabase(db
,&resource_db
,True
);
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");
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
,
330 texture
->setTexture(0);
332 if (XrmGetResource(resource_db
, rname
, rclass
, &value_type
,
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
;
348 if ((! texture
->getColor()->isAllocated()) ||
349 (texture
->getTexture() & BImage_Flat
))
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(),
371 texture
->getHiColor()->setPixel(xcol
.pixel
);
374 (unsigned int) ((texture
->getColor()->getRed() >> 2) +
375 (texture
->getColor()->getRed() >> 1)) * 0xff;
377 (unsigned int) ((texture
->getColor()->getGreen() >> 2) +
378 (texture
->getColor()->getGreen() >> 1)) * 0xff;
380 (unsigned int) ((texture
->getColor()->getBlue() >> 2) +
381 (texture
->getColor()->getBlue() >> 1)) * 0xff;
383 if (! XAllocColor(bbtool
->getXDisplay(), bbtool
->getImageControl()->getColormap(),
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
;
404 delete [] colortoclass
;
405 delete [] colortoname
;
409 void BaseResource::readDatabaseColor(char *rname
, char *rclass
, BColor
*color
) {
412 if (XrmGetResource(resource_db
, rname
, rclass
, &value_type
,
414 bbtool
->getImageControl()->parseColor(color
, value
.addr
);
416 // parsing with no color string just deallocates the color, if it has
417 // been previously allocated
418 bbtool
->getImageControl()->parseColor(color
);