1 #include "config.h"
2 #include "gfxinterface.h"
3 #include "instance.h"
4 #include <pwd.h>
5 #include <cstdlib>
6 #include <sys/types.h>
7 #include <unistd.h>
8 
9 
10 namespace {
11 
GetUserFullName()12 std::string GetUserFullName()
13 {
14 	const passwd *const pwd (getpwuid(getuid()));
15 	const char * gecos (pwd->pw_gecos);
16 	if (!gecos || !gecos[0]) return pwd->pw_name;
17 	while (*gecos && *gecos != ',') ++gecos;
18 	return std::string(pwd->pw_gecos, gecos - pwd->pw_gecos);
19 }
20 
21 
PlayerName()22 std::string PlayerName()
23 {
24 	const char *const env_name (std::getenv("XGAL_PSEUDO"));
25 	return env_name ? env_name : GetUserFullName();
26 }
27 
28 
29 #define TO_STRING(s) #s
30 #define MAKE_STRING(s) TO_STRING(s)
31 
32 #ifndef HIGH_SCORES_FILE
33 	#define HIGH_SCORES_FILE .xgalaga++.scores
34 #endif
35 
36 // If absolute path, return as-is, else prefix with home dir.
ScoreFileName()37 std::string ScoreFileName()
38 {
39 	static const char scores_file_name[] = MAKE_STRING(HIGH_SCORES_FILE);
40 	if (scores_file_name[0] != '/') {
41 		const passwd *const pwd (getpwuid(getuid()));
42 		if (pwd->pw_dir) return std::string(pwd->pw_dir) + '/' + scores_file_name;
43 	}
44 	return scores_file_name;
45 }
46 
47 }
48 
49 
50 
51 /*
52  * Config
53  */
54 
55 Config * Config::singleton_ (0);
56 
57 
Instance()58 Config & Config::Instance()
59 {
60 	if (!singleton_) {
61 		singleton_ = new Config;
62 		InstancesManager::Instance().Push(DestroyInstance);
63 	}
64 	return *singleton_;
65 }
66 
67 
Config()68 Config::Config()
69 : details_level_  (max_details)
70 , refresh_rate_   (50)
71 , player_name_    (PlayerName())
72 , score_file_name_(ScoreFileName())
73 {
74 }
75 
76 
AddDetailsLevel(int i)77 void Config::AddDetailsLevel(int i) {
78 	if (details_level_ + i >= min_details &&
79 	    details_level_ + i <= max_details) {
80 		details_level_ = details_level_ + i;
81 	}
82 }
83 
84 
NextRefreshRate(void) const85 int Config::NextRefreshRate(void) const {
86 	return refresh_rate_ >= 120 ? 50 : refresh_rate_ + 5;
87 }
88 
89 
90 /*
91  *
92  */
93 
SetStandardWindowSize(unsigned win_size)94 void SetStandardWindowSize(unsigned win_size)
95 {
96 	static const unsigned widths[]  = { 640, 800, 1024, 1280, 1600 };
97 	static const unsigned heights[] = { 480, 600,  768, 1024, 1200 };
98 	if (win_size > 0 && win_size <= sizeof widths / sizeof widths[0]) {
99 		--win_size;
100 		X11::Inst().ResizeWindow(widths[win_size], heights[win_size]);
101 		X11::Inst().ClearWindow();
102 	}
103 }
104