File: xresman.cc

package info (click to toggle)
clxclient 3.9.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 304 kB
  • sloc: cpp: 3,707; makefile: 141
file content (165 lines) | stat: -rw-r--r-- 5,022 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// ---------------------------------------------------------------------------------
//
//  Copyright (C) 2003-2013 Fons Adriaensen <fons@linuxaudio.org>
//    
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published
//  by the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this program; if not, write to the Free Software
//  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ---------------------------------------------------------------------------------


#include <string.h>
#include <ctype.h>
#include "clxclient.h"


XrmOptionDescRec X_resman::defopt [X_resman::NDEFOPT] =
{
    {(char *)("-display"),          (char *)(".display"),          XrmoptionSepArg,  NULL },
    {(char *)("-name"),             (char *)(".name"),             XrmoptionSepArg,  NULL },
    {(char *)("-title"),            (char *)(".title"),            XrmoptionSepArg,  NULL },
    {(char *)("-iconic"),           (char *)("*iconic"),           XrmoptionNoArg,   (char *)("on") },
    {(char *)("-mono"),             (char *)(".mono"),             XrmoptionNoArg,   (char *)("on") },
    {(char *)("-gray"),             (char *)(".gray"),             XrmoptionNoArg,   (char *)("on") },
    {(char *)("-geometry"),         (char *)("*geometry"),         XrmoptionSepArg,  NULL },
    {(char *)("-background"),       (char *)("*background"),       XrmoptionSepArg,  NULL },
    {(char *)("-bg"),               (char *)("*background"),       XrmoptionSepArg,  NULL },
    {(char *)("-foreground"),       (char *)("*foreground"),       XrmoptionSepArg,  NULL },
    {(char *)("-fg"),               (char *)("*foreground"),       XrmoptionSepArg,  NULL },
    {(char *)("-font"),             (char *)("*font"),             XrmoptionSepArg,  NULL },
    {(char *)("-fn"),               (char *)("*font"),             XrmoptionSepArg,  NULL },
};



X_resman::X_resman (void) :
    _xrmdata (NULL)
{
}


X_resman::~X_resman (void)
{
}


void X_resman::init (int *argc, char *argv [], char *name,
		     XrmOptionDescRec *opt, int nopt)
{
    XrmDatabase cmd_db = NULL;  
    XrmDatabase def_db = NULL;
    XrmDatabase app_db = NULL;

    char     line [256];
    int      ac;
    char   **av;

    _home = getenv ("HOME");
    if (_home == 0) _home = (char *)"";

    // set default name and class
    strncpy (_rname, name, 64);
    strncpy (_rclas, name, 64);
    _rclas [0] = toupper (_rclas [0]);

    // Scan command line args for -name and -class
    ac = *argc;
    av =  argv;
    while (++av, --ac > 0)
    {
	if (! strcmp (*av, "-name")  && ac > 1) strncpy (_rname, av [1], 64);
	if (! strcmp (*av, "-class") && ac > 1) strncpy (_rclas, av [1], 64);
    }

    // Initialise X resource manager
    XrmInitialize ();

    // Read user default options. 
    sprintf (line, "%s/.Xdefaults", _home);
    def_db = XrmGetFileDatabase (line);

    // Read options from application options file or system wide config file
    sprintf (line, "%s/.%src", _home, _rname);
    app_db = XrmGetFileDatabase (line);
    if (! app_db)
    {
        sprintf (line, "/etc/%s.conf", _rname);
        app_db = XrmGetFileDatabase (line);
    }    

    // Scan command line for options.
    if (opt && nopt) XrmParseCommand (&cmd_db, opt, nopt, _rname, argc, argv);

    // Merge all Xrm databases
    XrmMergeDatabases (def_db, &_xrmdata);
    XrmMergeDatabases (app_db, &_xrmdata);
    XrmMergeDatabases (cmd_db, &_xrmdata);
}


const char *X_resman::get (const char *res, const char *def) const
{
    char       r1 [128];
    char       r2 [128];
    char       *t [20];
    XrmValue   v;

    strcpy (r1, _rname);
    strcat (r1, res);
    strcpy (r2, _rclas);
    strcat (r2, res);
    return XrmGetResource (_xrmdata, r1, r2, t, &v) ? (char *) v.addr : def;
}


int X_resman::getb (const char *res, int def) const
{
    const char *val;

    val = get (res, 0);
    if (val) return ! (    strcmp (val, "on")
  		        && strcmp (val, "true")
			&& strcmp (val, "On")
			&& strcmp (val, "True"));
    else return def;
}


void X_resman::geometry (const char *res, int xd, int yd, int bd, int &xp, int &yp, int &xs, int &ys)
{
    const char *g;
    int   f, xp1, yp1;
    unsigned int xs1, ys1;

    g = get (res, NULL);
    if (g)
    {
	f = XParseGeometry (g, &xp1, &yp1, &xs1, &ys1);
 
	if (f & WidthValue)  xs = xs1;
	if (f & HeightValue) ys = ys1;

	if (f & XValue)
	{
	    if (f & XNegative) xp1 += xd - xs - bd;
	    xp = xp1;
	}
	if (f & YValue)
	{
	    if (f & YNegative) yp1 += yd - ys - bd;
	    yp = yp1;
	}
    }
}