Skip to content

Commit 66af90e

Browse files
committedDec 13, 2019
Add an option for loading config files
Can be used multiple times. I had to change the type of `cfg` to implement it cleanly, thus the diff is rather large.
1 parent 79244e9 commit 66af90e

File tree

7 files changed

+147
-138
lines changed

7 files changed

+147
-138
lines changed
 

‎app.cpp

+60-50
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,20 @@ App::App(int argc, char ** argv)
145145
firstlogin(true), Dpy(NULL)
146146
{
147147
int tmp;
148+
bool configLoaded = false;
148149

149150
/* Parse command line
150151
Note: we force a option for nodaemon switch to handle "-nodaemon" */
151-
while ((tmp = getopt(argc, argv, "vhsp:n:d?")) != EOF) {
152+
while ((tmp = getopt(argc, argv, "c:p:ndsh?")) != EOF) {
152153
switch (tmp) {
154+
case 'c': /* Config */
155+
if (optarg == NULL) {
156+
logStream << "The -c option requires an argument" << endl;
157+
exit(ERR_EXIT);
158+
}
159+
cfg.readConf(optarg);
160+
configLoaded = true;
161+
break;
153162
case 'p': /* Test theme */
154163
testtheme = optarg;
155164
testing = true;
@@ -179,8 +188,9 @@ App::App(int argc, char ** argv)
179188
case 'h': /* Help */
180189
logStream << "usage: " << APPNAME << " [option ...]" << endl
181190
<< "options:" << endl
191+
<< " -c file: configuration file" << endl
182192
<< " -d: daemon mode" << endl
183-
<< " -nodaemon: no-daemon mode" << endl
193+
<< " -n: no-daemon mode" << endl
184194
<< " -v: show version" << endl
185195
#ifdef USE_CONSOLEKIT
186196
<< " -s: start for systemd, disable consolekit support"
@@ -197,6 +207,9 @@ App::App(int argc, char ** argv)
197207
exit(ERR_EXIT);
198208
}
199209
#endif /* XNEST_DEBUG */
210+
211+
if (!configLoaded)
212+
cfg.readConf(CFGFILE);
200213
}
201214

202215
void App::Run()
@@ -211,9 +224,7 @@ void App::Run()
211224
}
212225
#endif
213226

214-
/* Read configuration and theme */
215-
cfg = new Cfg;
216-
cfg->readConf(CFGFILE);
227+
/* Read theme */
217228
string themebase = "";
218229
string themefile = "";
219230
string themedir = "";
@@ -222,7 +233,7 @@ void App::Run()
222233
themeName = testtheme;
223234
} else {
224235
themebase = string(THEMESDIR) + "/";
225-
themeName = cfg->getOption("current_theme");
236+
themeName = cfg.getOption("current_theme");
226237
string::size_type pos;
227238
if ((pos = themeName.find(",")) != string::npos) {
228239
/* input is a set */
@@ -248,7 +259,7 @@ void App::Run()
248259
while (!loaded) {
249260
themedir = themebase + themeName;
250261
themefile = themedir + THEMESFILE;
251-
if (!cfg->readConf(themefile)) {
262+
if (!cfg.readConf(themefile)) {
252263
if (themeName == "default") {
253264
logStream << APPNAME << ": Failed to open default theme file "
254265
<< themefile << endl;
@@ -278,7 +289,7 @@ void App::Run()
278289
signal(SIGUSR1, User1Signal);
279290

280291
#ifndef XNEST_DEBUG
281-
if (!force_nodaemon && cfg->getOption("daemon") == "yes") {
292+
if (!force_nodaemon && cfg.getOption("daemon") == "yes") {
282293
daemonmode = true;
283294
}
284295

@@ -299,8 +310,8 @@ void App::Run()
299310
StartServer();
300311

301312
// Run setup script
302-
if (cfg->getOption("xsetup_script") != "") {
303-
const char * xsetup_cmd = cfg->getOption("xsetup_script").c_str();
313+
if (cfg.getOption("xsetup_script") != "") {
314+
const char * xsetup_cmd = cfg.getOption("xsetup_script").c_str();
304315
logStream << APPNAME << ": executing xsetup script '" << xsetup_cmd
305316
<< "'" << endl;
306317
system(xsetup_cmd);
@@ -342,14 +353,14 @@ void App::Run()
342353
LoginPanel = new Panel(Dpy, Scr, Root, cfg, themedir, Panel::Mode_DM);
343354
bool firstloop =
344355
true; /* 1st time panel is shown (for automatic username) */
345-
bool focuspass = cfg->getOption("focus_password") == "yes";
346-
bool autologin = cfg->getOption("auto_login") == "yes";
356+
bool focuspass = cfg.getOption("focus_password") == "yes";
357+
bool autologin = cfg.getOption("auto_login") == "yes";
347358

348-
if (firstlogin && cfg->getOption("default_user") != "") {
349-
LoginPanel->SetName(cfg->getOption("default_user"));
359+
if (firstlogin && cfg.getOption("default_user") != "") {
360+
LoginPanel->SetName(cfg.getOption("default_user"));
350361
#ifdef USE_PAM
351362
pam.set_item(
352-
PAM::Authenticator::User, cfg->getOption("default_user").c_str());
363+
PAM::Authenticator::User, cfg.getOption("default_user").c_str());
353364
#endif
354365
firstlogin = false;
355366
if (autologin) {
@@ -358,7 +369,7 @@ void App::Run()
358369
}
359370

360371
/* Set NumLock */
361-
string numlock = cfg->getOption("numlock");
372+
string numlock = cfg.getOption("numlock");
362373
if (numlock == "on") {
363374
NumLock::setOn(Dpy);
364375
} else if (numlock == "off") {
@@ -386,8 +397,8 @@ void App::Run()
386397

387398
LoginPanel->Reset();
388399

389-
if (firstloop && cfg->getOption("default_user") != "")
390-
LoginPanel->SetName(cfg->getOption("default_user"));
400+
if (firstloop && cfg.getOption("default_user") != "")
401+
LoginPanel->SetName(cfg.getOption("default_user"));
391402

392403
if (firstloop) {
393404
LoginPanel->SwitchSession();
@@ -522,7 +533,7 @@ int App::GetServerPID() { return ServerPID; }
522533
/* Hide the cursor */
523534
void App::HideCursor()
524535
{
525-
if (cfg->getOption("hidecursor") == "true") {
536+
if (cfg.getOption("hidecursor") == "true") {
526537
XColor black;
527538
char cursordata[1];
528539
Pixmap cursorpixmap;
@@ -586,7 +597,7 @@ void App::Login()
586597
pam.setenv("SHELL", pw->pw_shell);
587598
pam.setenv("USER", pw->pw_name);
588599
pam.setenv("LOGNAME", pw->pw_name);
589-
pam.setenv("PATH", cfg->getOption("default_path").c_str());
600+
pam.setenv("PATH", cfg.getOption("default_path").c_str());
590601
pam.setenv("DISPLAY", DisplayName);
591602
pam.setenv("MAIL", maildir.c_str());
592603
pam.setenv("XAUTHORITY", xauthority.c_str());
@@ -652,7 +663,7 @@ void App::Login()
652663
child_env[n++] = StrConcat("USER=", pw->pw_name);
653664
child_env[n++] = StrConcat("LOGNAME=", pw->pw_name);
654665
child_env[n++] =
655-
StrConcat("PATH=", cfg->getOption("default_path").c_str());
666+
StrConcat("PATH=", cfg.getOption("default_path").c_str());
656667
child_env[n++] = StrConcat("DISPLAY=", DisplayName);
657668
child_env[n++] = StrConcat("MAIL=", maildir.c_str());
658669
child_env[n++] = StrConcat("XAUTHORITY=", xauthority.c_str());
@@ -669,10 +680,10 @@ void App::Login()
669680
/* Login process starts here */
670681
SwitchUser Su(pw, cfg, DisplayName, child_env);
671682
string session = LoginPanel->getSession();
672-
string loginCommand = cfg->getOption("login_cmd");
683+
string loginCommand = cfg.getOption("login_cmd");
673684
replaceVariables(loginCommand, SESSION_VAR, session);
674685
replaceVariables(loginCommand, THEME_VAR, themeName);
675-
string sessStart = cfg->getOption("sessionstart_cmd");
686+
string sessStart = cfg.getOption("sessionstart_cmd");
676687
if (sessStart != "") {
677688
replaceVariables(sessStart, USER_VAR, pw->pw_name);
678689
system(sessStart.c_str());
@@ -697,7 +708,7 @@ void App::Login()
697708
LoginPanel->Message("Failed to execute login command");
698709
sleep(3);
699710
} else {
700-
string sessStop = cfg->getOption("sessionstop_cmd");
711+
string sessStop = cfg.getOption("sessionstop_cmd");
701712
if (sessStop != "") {
702713
replaceVariables(sessStop, USER_VAR, pw->pw_name);
703714
system(sessStop.c_str());
@@ -753,13 +764,13 @@ void App::Reboot()
753764
#endif
754765

755766
/* Write message */
756-
LoginPanel->Message((char *)cfg->getOption("reboot_msg").c_str());
767+
LoginPanel->Message((char *)cfg.getOption("reboot_msg").c_str());
757768
sleep(3);
758769

759770
/* Stop server and reboot */
760771
StopServer();
761772
RemoveLock();
762-
system(cfg->getOption("reboot_cmd").c_str());
773+
system(cfg.getOption("reboot_cmd").c_str());
763774
exit(OK_EXIT);
764775
}
765776

@@ -774,20 +785,20 @@ void App::Halt()
774785
#endif
775786

776787
/* Write message */
777-
LoginPanel->Message((char *)cfg->getOption("shutdown_msg").c_str());
788+
LoginPanel->Message((char *)cfg.getOption("shutdown_msg").c_str());
778789
sleep(3);
779790

780791
/* Stop server and halt */
781792
StopServer();
782793
RemoveLock();
783-
system(cfg->getOption("halt_cmd").c_str());
794+
system(cfg.getOption("halt_cmd").c_str());
784795
exit(OK_EXIT);
785796
}
786797

787798
void App::Suspend()
788799
{
789800
sleep(1);
790-
system(cfg->getOption("suspend_cmd").c_str());
801+
system(cfg.getOption("suspend_cmd").c_str());
791802
}
792803

793804
void App::Console()
@@ -802,7 +813,7 @@ void App::Console()
802813
(XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)) - (posy * 2)) / fonty;
803814

804815
/* Execute console */
805-
const char * cmd = cfg->getOption("console_cmd").c_str();
816+
const char * cmd = cfg.getOption("console_cmd").c_str();
806817
char * tmp = new char[strlen(cmd) + 60];
807818
sprintf(tmp, cmd, width, height, posx, posy, fontx, fonty);
808819
system(tmp);
@@ -811,7 +822,7 @@ void App::Console()
811822

812823
void App::Exit()
813824
{
814-
if (cfg->getOption("allow_exit") == "false")
825+
if (cfg.getOption("allow_exit") == "false")
815826
return;
816827
#ifdef USE_PAM
817828
try {
@@ -832,7 +843,6 @@ void App::Exit()
832843
StopServer();
833844
RemoveLock();
834845
}
835-
delete cfg;
836846
exit(OK_EXIT);
837847
}
838848

@@ -951,10 +961,10 @@ int App::StartServer()
951961
int argc = 1, pos = 0, i;
952962
static const int MAX_XSERVER_ARGS = 256;
953963
static char * server[MAX_XSERVER_ARGS + 2] = {NULL};
954-
server[0] = (char *)cfg->getOption("default_xserver").c_str();
955-
string argOption = cfg->getOption("xserver_arguments");
964+
server[0] = (char *)cfg.getOption("default_xserver").c_str();
965+
string argOption = cfg.getOption("xserver_arguments");
956966
/* Add mandatory -xauth option */
957-
argOption = argOption + " -auth " + cfg->getOption("authfile");
967+
argOption = argOption + " -auth " + cfg.getOption("authfile");
958968
char * args = new char[argOption.length() + 2]; /* NULL plus vt */
959969
strcpy(args, argOption.c_str());
960970

@@ -1122,20 +1132,20 @@ void App::setBackground(const string & themedir)
11221132
}
11231133

11241134
if (loaded) {
1125-
string bgstyle = cfg->getOption("background_style");
1135+
string bgstyle = cfg.getOption("background_style");
11261136
if (bgstyle == "stretch") {
11271137
image->Resize(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)),
11281138
XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)));
11291139
} else if (bgstyle == "tile") {
11301140
image->Tile(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)),
11311141
XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)));
11321142
} else if (bgstyle == "center") {
1133-
string hexvalue = cfg->getOption("background_color");
1143+
string hexvalue = cfg.getOption("background_color");
11341144
hexvalue = hexvalue.substr(1, 6);
11351145
image->Center(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)),
11361146
XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)), hexvalue.c_str());
11371147
} else { /* plain color or error */
1138-
string hexvalue = cfg->getOption("background_color");
1148+
string hexvalue = cfg.getOption("background_color");
11391149
hexvalue = hexvalue.substr(1, 6);
11401150
image->Center(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)),
11411151
XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)), hexvalue.c_str());
@@ -1154,14 +1164,14 @@ void App::setBackground(const string & themedir)
11541164
/* Check if there is a lockfile and a corresponding process */
11551165
void App::GetLock()
11561166
{
1157-
std::ifstream lockfile(cfg->getOption("lockfile").c_str());
1167+
std::ifstream lockfile(cfg.getOption("lockfile").c_str());
11581168
if (!lockfile) {
11591169
/* no lockfile present, create one */
11601170
std::ofstream lockfile(
1161-
cfg->getOption("lockfile").c_str(), ios_base::out);
1171+
cfg.getOption("lockfile").c_str(), ios_base::out);
11621172
if (!lockfile) {
11631173
logStream << APPNAME << ": Could not create lock file: "
1164-
<< cfg->getOption("lockfile").c_str() << std::endl;
1174+
<< cfg.getOption("lockfile").c_str() << std::endl;
11651175
exit(ERR_EXIT);
11661176
}
11671177
lockfile << getpid() << std::endl;
@@ -1184,10 +1194,10 @@ void App::GetLock()
11841194
logStream << APPNAME << ": Stale lockfile found, removing it"
11851195
<< std::endl;
11861196
std::ofstream lockfile(
1187-
cfg->getOption("lockfile").c_str(), ios_base::out);
1197+
cfg.getOption("lockfile").c_str(), ios_base::out);
11881198
if (!lockfile) {
11891199
logStream << APPNAME << ": Could not create new lock file: "
1190-
<< cfg->getOption("lockfile") << std::endl;
1200+
<< cfg.getOption("lockfile") << std::endl;
11911201
exit(ERR_EXIT);
11921202
}
11931203
lockfile << getpid() << std::endl;
@@ -1198,17 +1208,17 @@ void App::GetLock()
11981208
}
11991209

12001210
/* Remove lockfile and close logs */
1201-
void App::RemoveLock() { remove(cfg->getOption("lockfile").c_str()); }
1211+
void App::RemoveLock() { remove(cfg.getOption("lockfile").c_str()); }
12021212

12031213
/* Get server start check flag. */
12041214
bool App::isServerStarted() { return serverStarted; }
12051215

12061216
/* Redirect stdout and stderr to log file */
12071217
void App::OpenLog()
12081218
{
1209-
if (!logStream.openLog(cfg->getOption("logfile").c_str())) {
1219+
if (!logStream.openLog(cfg.getOption("logfile").c_str())) {
12101220
logStream << APPNAME << ": Could not accesss log file: "
1211-
<< cfg->getOption("logfile") << endl;
1221+
<< cfg.getOption("logfile") << endl;
12121222
RemoveLock();
12131223
exit(ERR_EXIT);
12141224
}
@@ -1286,10 +1296,10 @@ void App::CreateServerAuth()
12861296
mcookie[i + 3] = digits[hi >> 4];
12871297
}
12881298
/* reinitialize auth file */
1289-
authfile = cfg->getOption("authfile");
1299+
authfile = cfg.getOption("authfile");
12901300
remove(authfile.c_str());
12911301
putenv(StrConcat("XAUTHORITY=", authfile.c_str()));
1292-
Util::add_mcookie(mcookie, ":0", cfg->getOption("xauth_path"), authfile);
1302+
Util::add_mcookie(mcookie, ":0", cfg.getOption("xauth_path"), authfile);
12931303
}
12941304

12951305
char * App::StrConcat(const char * str1, const char * str2)
@@ -1302,10 +1312,10 @@ char * App::StrConcat(const char * str1, const char * str2)
13021312

13031313
void App::UpdatePid()
13041314
{
1305-
std::ofstream lockfile(cfg->getOption("lockfile").c_str(), ios_base::out);
1315+
std::ofstream lockfile(cfg.getOption("lockfile").c_str(), ios_base::out);
13061316
if (!lockfile) {
13071317
logStream << APPNAME << ": Could not update lock file: "
1308-
<< cfg->getOption("lockfile").c_str() << endl;
1318+
<< cfg.getOption("lockfile").c_str() << endl;
13091319
exit(ERR_EXIT);
13101320
}
13111321
lockfile << getpid() << endl;

‎app.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class App
6969

7070
bool AuthenticateUser(bool focuspass);
7171

72-
static std::string findValidRandomTheme(const std::string & set);
72+
std::string findValidRandomTheme(const std::string & set);
7373
static void replaceVariables(std::string & input, const std::string & var,
7474
const std::string & value);
7575

@@ -98,7 +98,7 @@ class App
9898
/* Options */
9999
char * DispName;
100100

101-
Cfg * cfg;
101+
Cfg cfg;
102102

103103
Pixmap BackgroundPixmap;
104104

‎panel.cpp

+67-67
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
using namespace std;
2020

21-
Panel::Panel(Display * dpy, int scr, Window root, Cfg * config,
21+
Panel::Panel(Display * dpy, int scr, Window root, Cfg & config,
2222
const string & themedir, PanelType panel_mode)
2323
: Dpy(dpy), Scr(scr), Root(root), cfg(config), mode(panel_mode),
2424
session_name(""), session_exec("")
@@ -50,47 +50,47 @@ Panel::Panel(Display * dpy, int scr, Window root, Cfg * config,
5050
}
5151
}
5252

53-
font = XftFontOpenName(Dpy, Scr, cfg->getOption("input_font").c_str());
53+
font = XftFontOpenName(Dpy, Scr, cfg.getOption("input_font").c_str());
5454
welcomefont =
55-
XftFontOpenName(Dpy, Scr, cfg->getOption("welcome_font").c_str());
56-
introfont = XftFontOpenName(Dpy, Scr, cfg->getOption("intro_font").c_str());
55+
XftFontOpenName(Dpy, Scr, cfg.getOption("welcome_font").c_str());
56+
introfont = XftFontOpenName(Dpy, Scr, cfg.getOption("intro_font").c_str());
5757
enterfont =
58-
XftFontOpenName(Dpy, Scr, cfg->getOption("username_font").c_str());
59-
msgfont = XftFontOpenName(Dpy, Scr, cfg->getOption("msg_font").c_str());
58+
XftFontOpenName(Dpy, Scr, cfg.getOption("username_font").c_str());
59+
msgfont = XftFontOpenName(Dpy, Scr, cfg.getOption("msg_font").c_str());
6060

6161
Visual * visual = DefaultVisual(Dpy, Scr);
6262
Colormap colormap = DefaultColormap(Dpy, Scr);
6363
/* NOTE: using XftColorAllocValue() would be a better solution. Lazy me. */
6464
XftColorAllocName(Dpy, visual, colormap,
65-
cfg->getOption("input_color").c_str(), &inputcolor);
65+
cfg.getOption("input_color").c_str(), &inputcolor);
6666
XftColorAllocName(Dpy, visual, colormap,
67-
cfg->getOption("input_shadow_color").c_str(), &inputshadowcolor);
67+
cfg.getOption("input_shadow_color").c_str(), &inputshadowcolor);
6868
XftColorAllocName(Dpy, visual, colormap,
69-
cfg->getOption("welcome_color").c_str(), &welcomecolor);
69+
cfg.getOption("welcome_color").c_str(), &welcomecolor);
7070
XftColorAllocName(Dpy, visual, colormap,
71-
cfg->getOption("welcome_shadow_color").c_str(), &welcomeshadowcolor);
71+
cfg.getOption("welcome_shadow_color").c_str(), &welcomeshadowcolor);
7272
XftColorAllocName(Dpy, visual, colormap,
73-
cfg->getOption("username_color").c_str(), &entercolor);
73+
cfg.getOption("username_color").c_str(), &entercolor);
7474
XftColorAllocName(Dpy, visual, colormap,
75-
cfg->getOption("username_shadow_color").c_str(), &entershadowcolor);
75+
cfg.getOption("username_shadow_color").c_str(), &entershadowcolor);
7676
XftColorAllocName(
77-
Dpy, visual, colormap, cfg->getOption("msg_color").c_str(), &msgcolor);
77+
Dpy, visual, colormap, cfg.getOption("msg_color").c_str(), &msgcolor);
7878
XftColorAllocName(Dpy, visual, colormap,
79-
cfg->getOption("msg_shadow_color").c_str(), &msgshadowcolor);
79+
cfg.getOption("msg_shadow_color").c_str(), &msgshadowcolor);
8080
XftColorAllocName(Dpy, visual, colormap,
81-
cfg->getOption("intro_color").c_str(), &introcolor);
81+
cfg.getOption("intro_color").c_str(), &introcolor);
8282
XftColorAllocName(Dpy, visual, colormap,
83-
cfg->getOption("session_color").c_str(), &sessioncolor);
83+
cfg.getOption("session_color").c_str(), &sessioncolor);
8484
XftColorAllocName(Dpy, visual, colormap,
85-
cfg->getOption("session_shadow_color").c_str(), &sessionshadowcolor);
85+
cfg.getOption("session_shadow_color").c_str(), &sessionshadowcolor);
8686

8787
/* Load properties from config / theme */
88-
input_name_x = cfg->getIntOption("input_name_x");
89-
input_name_y = cfg->getIntOption("input_name_y");
90-
input_pass_x = cfg->getIntOption("input_pass_x");
91-
input_pass_y = cfg->getIntOption("input_pass_y");
92-
inputShadowXOffset = cfg->getIntOption("input_shadow_xoffset");
93-
inputShadowYOffset = cfg->getIntOption("input_shadow_yoffset");
88+
input_name_x = cfg.getIntOption("input_name_x");
89+
input_name_y = cfg.getIntOption("input_name_y");
90+
input_pass_x = cfg.getIntOption("input_pass_x");
91+
input_pass_y = cfg.getIntOption("input_pass_y");
92+
inputShadowXOffset = cfg.getIntOption("input_shadow_xoffset");
93+
inputShadowYOffset = cfg.getIntOption("input_shadow_yoffset");
9494

9595
if (input_pass_x < 0 || input_pass_y < 0) { /* single inputbox mode */
9696
input_pass_x = input_name_x;
@@ -113,7 +113,7 @@ Panel::Panel(Display * dpy, int scr, Window root, Cfg * config,
113113
}
114114

115115
Image * bg = new Image();
116-
string bgstyle = cfg->getOption("background_style");
116+
string bgstyle = cfg.getOption("background_style");
117117
if (bgstyle != "color") {
118118
panelpng = themedir + "/background.png";
119119
loaded = bg->Read(panelpng.c_str());
@@ -137,11 +137,11 @@ Panel::Panel(Display * dpy, int scr, Window root, Cfg * config,
137137
else if (bgstyle == "tile")
138138
bg->Tile(viewport.width, viewport.height);
139139
else if (bgstyle == "center") {
140-
string hexvalue = cfg->getOption("background_color");
140+
string hexvalue = cfg.getOption("background_color");
141141
hexvalue = hexvalue.substr(1, 6);
142142
bg->Center(viewport.width, viewport.height, hexvalue.c_str());
143143
} else { // plain color or error
144-
string hexvalue = cfg->getOption("background_color");
144+
string hexvalue = cfg.getOption("background_color");
145145
hexvalue = hexvalue.substr(1, 6);
146146
bg->Center(viewport.width, viewport.height, hexvalue.c_str());
147147
}
@@ -153,20 +153,20 @@ Panel::Panel(Display * dpy, int scr, Window root, Cfg * config,
153153
bg->Tile(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)),
154154
XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)));
155155
} else if (bgstyle == "center") {
156-
string hexvalue = cfg->getOption("background_color");
156+
string hexvalue = cfg.getOption("background_color");
157157
hexvalue = hexvalue.substr(1, 6);
158158
bg->Center(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)),
159159
XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)), hexvalue.c_str());
160160
} else { /* plain color or error */
161-
string hexvalue = cfg->getOption("background_color");
161+
string hexvalue = cfg.getOption("background_color");
162162
hexvalue = hexvalue.substr(1, 6);
163163
bg->Center(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)),
164164
XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)), hexvalue.c_str());
165165
}
166166
}
167167

168-
string cfgX = cfg->getOption("input_panel_x");
169-
string cfgY = cfg->getOption("input_panel_y");
168+
string cfgX = cfg.getOption("input_panel_x");
169+
string cfgY = cfg.getOption("input_panel_y");
170170

171171
if (mode == Mode_Lock) {
172172
X = Cfg::absolutepos(cfgX, viewport.width, image->Width());
@@ -195,8 +195,8 @@ Panel::Panel(Display * dpy, int scr, Window root, Cfg * config,
195195
delete bg;
196196

197197
/* Read (and substitute vars in) the welcome message */
198-
welcome_message = cfg->getWelcomeMessage();
199-
intro_message = cfg->getOption("intro_msg");
198+
welcome_message = cfg.getWelcomeMessage();
199+
intro_message = cfg.getOption("intro_msg");
200200

201201
if (mode == Mode_Lock) {
202202
SetName(getenv("USER"));
@@ -284,21 +284,21 @@ void Panel::WrongPassword(int timeout)
284284

285285
#if 0
286286
if (CapsLockOn)
287-
message = cfg->getOption("passwd_feedback_capslock");
287+
message = cfg.getOption("passwd_feedback_capslock");
288288
else
289289
#endif
290-
message = cfg->getOption("passwd_feedback_msg");
290+
message = cfg.getOption("passwd_feedback_msg");
291291

292292
XftDraw * draw = XftDrawCreate(
293293
Dpy, Win, DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr));
294294
XftTextExtentsUtf8(Dpy, msgfont,
295295
reinterpret_cast<const XftChar8 *>(message.c_str()), message.length(),
296296
&extents);
297297

298-
string cfgX = cfg->getOption("passwd_feedback_x");
299-
string cfgY = cfg->getOption("passwd_feedback_y");
300-
int shadowXOffset = cfg->getIntOption("msg_shadow_xoffset");
301-
int shadowYOffset = cfg->getIntOption("msg_shadow_yoffset");
298+
string cfgX = cfg.getOption("passwd_feedback_x");
299+
string cfgY = cfg.getOption("passwd_feedback_y");
300+
int shadowXOffset = cfg.getIntOption("msg_shadow_xoffset");
301+
int shadowYOffset = cfg.getIntOption("msg_shadow_yoffset");
302302
int msg_x = Cfg::absolutepos(
303303
cfgX, XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)), extents.width);
304304
int msg_y = Cfg::absolutepos(
@@ -308,7 +308,7 @@ void Panel::WrongPassword(int timeout)
308308
SlimDrawString8(draw, &msgcolor, msgfont, msg_x, msg_y, message,
309309
&msgshadowcolor, shadowXOffset, shadowYOffset);
310310

311-
if (cfg->getOption("bell") == "1")
311+
if (cfg.getOption("bell") == "1")
312312
XBell(Dpy, 100);
313313

314314
XFlush(Dpy);
@@ -339,10 +339,10 @@ void Panel::Message(const string & text)
339339
XftTextExtentsUtf8(Dpy, msgfont,
340340
reinterpret_cast<const XftChar8 *>(text.c_str()), text.length(),
341341
&extents);
342-
cfgX = cfg->getOption("msg_x");
343-
cfgY = cfg->getOption("msg_y");
344-
int shadowXOffset = cfg->getIntOption("msg_shadow_xoffset");
345-
int shadowYOffset = cfg->getIntOption("msg_shadow_yoffset");
342+
cfgX = cfg.getOption("msg_x");
343+
cfgY = cfg.getOption("msg_y");
344+
int shadowXOffset = cfg.getIntOption("msg_shadow_xoffset");
345+
int shadowYOffset = cfg.getIntOption("msg_shadow_yoffset");
346346
int msg_x, msg_y;
347347

348348
if (mode == Mode_Lock) {
@@ -430,7 +430,7 @@ void Panel::Cursor(int visible)
430430
y2 += viewport.y;
431431
}
432432
XSetForeground(
433-
Dpy, TextGC, GetColor(cfg->getOption("input_color").c_str()));
433+
Dpy, TextGC, GetColor(cfg.getOption("input_color").c_str()));
434434

435435
XDrawLine(Dpy, Win, TextGC, xx + 1, yy - cheight, xx + 1, y2);
436436
} else {
@@ -551,7 +551,7 @@ bool Panel::OnKeyPress(XEvent & event)
551551

552552
case XK_F11:
553553
/* Take a screenshot */
554-
system(cfg->getOption("screenshot_cmd").c_str());
554+
system(cfg.getOption("screenshot_cmd").c_str());
555555
return true;
556556

557557
case XK_Return:
@@ -697,10 +697,10 @@ void Panel::ShowText()
697697
/* welcome message */
698698
XftTextExtentsUtf8(Dpy, welcomefont, (XftChar8 *)welcome_message.c_str(),
699699
strlen(welcome_message.c_str()), &extents);
700-
cfgX = cfg->getOption("welcome_x");
701-
cfgY = cfg->getOption("welcome_y");
702-
int shadowXOffset = cfg->getIntOption("welcome_shadow_xoffset");
703-
int shadowYOffset = cfg->getIntOption("welcome_shadow_yoffset");
700+
cfgX = cfg.getOption("welcome_x");
701+
cfgY = cfg.getOption("welcome_y");
702+
int shadowXOffset = cfg.getIntOption("welcome_shadow_xoffset");
703+
int shadowYOffset = cfg.getIntOption("welcome_shadow_yoffset");
704704

705705
welcome_x = Cfg::absolutepos(cfgX, image->Width(), extents.width);
706706
welcome_y = Cfg::absolutepos(cfgY, image->Height(), extents.height);
@@ -712,13 +712,13 @@ void Panel::ShowText()
712712
/* Enter username-password message */
713713
string msg;
714714
if ((!singleInputMode || field == Get_Passwd) && mode == Mode_DM) {
715-
msg = cfg->getOption("password_msg");
715+
msg = cfg.getOption("password_msg");
716716
XftTextExtentsUtf8(Dpy, enterfont, (XftChar8 *)msg.c_str(),
717717
strlen(msg.c_str()), &extents);
718-
cfgX = cfg->getOption("password_x");
719-
cfgY = cfg->getOption("password_y");
720-
int shadowXOffset = cfg->getIntOption("username_shadow_xoffset");
721-
int shadowYOffset = cfg->getIntOption("username_shadow_yoffset");
718+
cfgX = cfg.getOption("password_x");
719+
cfgY = cfg.getOption("password_y");
720+
int shadowXOffset = cfg.getIntOption("username_shadow_xoffset");
721+
int shadowYOffset = cfg.getIntOption("username_shadow_yoffset");
722722
password_x = Cfg::absolutepos(cfgX, image->Width(), extents.width);
723723
password_y = Cfg::absolutepos(cfgY, image->Height(), extents.height);
724724
if (password_x >= 0 && password_y >= 0) {
@@ -729,13 +729,13 @@ void Panel::ShowText()
729729
}
730730

731731
if (!singleInputMode || field == Get_Name) {
732-
msg = cfg->getOption("username_msg");
732+
msg = cfg.getOption("username_msg");
733733
XftTextExtentsUtf8(Dpy, enterfont, (XftChar8 *)msg.c_str(),
734734
strlen(msg.c_str()), &extents);
735-
cfgX = cfg->getOption("username_x");
736-
cfgY = cfg->getOption("username_y");
737-
int shadowXOffset = cfg->getIntOption("username_shadow_xoffset");
738-
int shadowYOffset = cfg->getIntOption("username_shadow_yoffset");
735+
cfgX = cfg.getOption("username_x");
736+
cfgY = cfg.getOption("username_y");
737+
int shadowXOffset = cfg.getIntOption("username_shadow_xoffset");
738+
int shadowYOffset = cfg.getIntOption("username_shadow_yoffset");
739739
username_x = Cfg::absolutepos(cfgX, image->Width(), extents.width);
740740
username_y = Cfg::absolutepos(cfgY, image->Height(), extents.height);
741741
if (username_x >= 0 && username_y >= 0) {
@@ -749,7 +749,7 @@ void Panel::ShowText()
749749
if (mode == Mode_Lock) {
750750
// If only the password box is visible, draw the user name somewhere too
751751
string user_msg = "User: " + GetName();
752-
int show_username = cfg->getIntOption("show_username");
752+
int show_username = cfg.getIntOption("show_username");
753753
if (singleInputMode && show_username) {
754754
Message(user_msg);
755755
}
@@ -761,7 +761,7 @@ string Panel::getSession() { return session_exec; }
761761
/* choose next available session type */
762762
void Panel::SwitchSession()
763763
{
764-
pair<string, string> ses = cfg->nextSession();
764+
pair<string, string> ses = cfg.nextSession();
765765
session_name = ses.first;
766766
session_exec = ses.second;
767767
if (session_name.size() > 0) {
@@ -774,25 +774,25 @@ void Panel::ShowSession()
774774
{
775775
string msg_x, msg_y;
776776
XClearWindow(Dpy, Root);
777-
string currsession = cfg->getOption("session_msg") + " " + session_name;
777+
string currsession = cfg.getOption("session_msg") + " " + session_name;
778778
XGlyphInfo extents;
779779

780780
sessionfont =
781-
XftFontOpenName(Dpy, Scr, cfg->getOption("session_font").c_str());
781+
XftFontOpenName(Dpy, Scr, cfg.getOption("session_font").c_str());
782782

783783
XftDraw * draw = XftDrawCreate(
784784
Dpy, Root, DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr));
785785
XftTextExtentsUtf8(Dpy, sessionfont,
786786
reinterpret_cast<const XftChar8 *>(currsession.c_str()),
787787
currsession.length(), &extents);
788-
msg_x = cfg->getOption("session_x");
789-
msg_y = cfg->getOption("session_y");
788+
msg_x = cfg.getOption("session_x");
789+
msg_y = cfg.getOption("session_y");
790790
int x = Cfg::absolutepos(
791791
msg_x, XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)), extents.width);
792792
int y = Cfg::absolutepos(
793793
msg_y, XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)), extents.height);
794-
int shadowXOffset = cfg->getIntOption("session_shadow_xoffset");
795-
int shadowYOffset = cfg->getIntOption("session_shadow_yoffset");
794+
int shadowXOffset = cfg.getIntOption("session_shadow_xoffset");
795+
int shadowYOffset = cfg.getIntOption("session_shadow_yoffset");
796796

797797
SlimDrawString8(draw, &sessioncolor, sessionfont, x, y, currsession,
798798
&sessionshadowcolor, shadowXOffset, shadowYOffset);

‎panel.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Panel
5353

5454
enum PanelType { Mode_DM, Mode_Lock };
5555

56-
Panel(Display * dpy, int scr, Window root, Cfg * config,
56+
Panel(Display * dpy, int scr, Window root, Cfg & config,
5757
const std::string & themed, PanelType panel_mode);
5858
~Panel();
5959
void OpenPanel();
@@ -93,7 +93,7 @@ class Panel
9393

9494
/* Private data */
9595
PanelType mode; /* work mode */
96-
Cfg * cfg;
96+
Cfg & cfg;
9797
Window Win;
9898
Window Root;
9999
Display * Dpy;

‎slimlock.cpp

+12-13
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void * RaiseWindow(void * data);
5050
Display * dpy;
5151
int scr;
5252
Window win, root;
53-
Cfg * cfg;
53+
Cfg cfg;
5454
Panel * loginPanel;
5555
string themeName = "";
5656

@@ -106,15 +106,14 @@ int main(int argc, char ** argv)
106106

107107
unsigned int cfg_passwd_timeout;
108108
// Read user's current theme
109-
cfg = new Cfg;
110-
cfg->readConf(CFGFILE);
111-
cfg->readConf(SLIMLOCKCFG);
109+
cfg.readConf(CFGFILE);
110+
cfg.readConf(SLIMLOCKCFG);
112111
string themebase = "";
113112
string themefile = "";
114113
string themedir = "";
115114
themeName = "";
116115
themebase = string(THEMESDIR) + "/";
117-
themeName = cfg->getOption("current_theme");
116+
themeName = cfg.getOption("current_theme");
118117
string::size_type pos;
119118
if ((pos = themeName.find(",")) != string::npos) {
120119
themeName = findValidRandomTheme(themeName);
@@ -124,7 +123,7 @@ int main(int argc, char ** argv)
124123
while (!loaded) {
125124
themedir = themebase + themeName;
126125
themefile = themedir + THEMESFILE;
127-
if (!cfg->readConf(themefile)) {
126+
if (!cfg.readConf(themefile)) {
128127
if (themeName == "default") {
129128
cerr << APPNAME << ": Failed to open default theme file "
130129
<< themefile << endl;
@@ -181,7 +180,7 @@ int main(int argc, char ** argv)
181180
die("PAM: %s\n", pam_strerror(pam_handle, ret));
182181

183182
// disable tty switching
184-
if (cfg->getOption("tty_lock") == "1") {
183+
if (cfg.getOption("tty_lock") == "1") {
185184
if ((term = open("/dev/console", O_RDWR)) == -1)
186185
perror("error opening console");
187186

@@ -192,8 +191,8 @@ int main(int argc, char ** argv)
192191
// Set up DPMS
193192
unsigned int cfg_dpms_standby, cfg_dpms_off;
194193
cfg_dpms_standby =
195-
Cfg::string2int(cfg->getOption("dpms_standby_timeout").c_str());
196-
cfg_dpms_off = Cfg::string2int(cfg->getOption("dpms_off_timeout").c_str());
194+
Cfg::string2int(cfg.getOption("dpms_standby_timeout").c_str());
195+
cfg_dpms_off = Cfg::string2int(cfg.getOption("dpms_off_timeout").c_str());
197196
using_dpms = DPMSCapable(dpy) && (cfg_dpms_standby > 0);
198197
if (using_dpms) {
199198
DPMSGetTimeouts(dpy, &dpms_standby, &dpms_suspend, &dpms_off);
@@ -207,7 +206,7 @@ int main(int argc, char ** argv)
207206

208207
// Get password timeout
209208
cfg_passwd_timeout =
210-
Cfg::string2int(cfg->getOption("wrong_passwd_timeout").c_str());
209+
Cfg::string2int(cfg.getOption("wrong_passwd_timeout").c_str());
211210
// Let's just make sure it has a sane value
212211
cfg_passwd_timeout = cfg_passwd_timeout > 60 ? 60 : cfg_passwd_timeout;
213212

@@ -244,7 +243,7 @@ int main(int argc, char ** argv)
244243
flock(lock_file, LOCK_UN);
245244
close(lock_file);
246245

247-
if (cfg->getOption("tty_lock") == "1") {
246+
if (cfg.getOption("tty_lock") == "1") {
248247
if ((ioctl(term, VT_UNLOCKSWITCH)) == -1) {
249248
perror("error unlocking console");
250249
}
@@ -256,7 +255,7 @@ int main(int argc, char ** argv)
256255

257256
void HideCursor()
258257
{
259-
if (cfg->getOption("hidecursor") == "true") {
258+
if (cfg.getOption("hidecursor") == "true") {
260259
XColor black;
261260
char cursordata[1];
262261
Pixmap cursorpixmap;
@@ -323,7 +322,7 @@ string findValidRandomTheme(const string & set)
323322
int sel = Util::random() % themes.size();
324323

325324
name = Cfg::Trim(themes[sel]);
326-
themefile = string(THEMESDIR) + "/" + name + THEMESFILE;
325+
themefile = cfg.getOption("themes_dir") + "/" + name + THEMESFILE;
327326
if (stat(themefile.c_str(), &buf) != 0) {
328327
themes.erase(find(themes.begin(), themes.end(), name));
329328
cerr << APPNAME << ": Invalid theme in config: " << name << endl;

‎switchuser.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
using namespace std;
1818

1919
SwitchUser::SwitchUser(
20-
struct passwd * pw, Cfg * c, const string & display, char ** _env)
20+
struct passwd * pw, Cfg & c, const string & display, char ** _env)
2121
: cfg(c), Pw(pw), displayName(display), env(_env)
2222
{
2323
}
@@ -54,5 +54,5 @@ void SwitchUser::SetClientAuth(const char * mcookie)
5454
string home = string(Pw->pw_dir);
5555
string authfile = home + "/.Xauthority";
5656
remove(authfile.c_str());
57-
Util::add_mcookie(mcookie, ":0", cfg->getOption("xauth_path"), authfile);
57+
Util::add_mcookie(mcookie, ":0", cfg.getOption("xauth_path"), authfile);
5858
}

‎switchuser.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class SwitchUser
2727
{
2828
public:
2929
SwitchUser(
30-
struct passwd * pw, Cfg * c, const std::string & display, char ** _env);
30+
struct passwd * pw, Cfg & c, const std::string & display, char ** _env);
3131
~SwitchUser();
3232
void Login(const char * cmd, const char * mcookie);
3333

@@ -37,7 +37,7 @@ class SwitchUser
3737
void SetUserId();
3838
void Execute(const char * cmd);
3939
void SetClientAuth(const char * mcookie);
40-
Cfg * cfg;
40+
Cfg & cfg;
4141
struct passwd * Pw;
4242

4343
std::string displayName;

0 commit comments

Comments
 (0)
Please sign in to comment.