1 //**************************************************************************************** 2 // 3 // File: Prefs.cpp 4 // 5 // Written by: Daniel Switkin 6 // 7 // Copyright 1999, Be Incorporated 8 // 9 //**************************************************************************************** 10 11 #include "Prefs.h" 12 #include "Common.h" 13 #include "PulseApp.h" 14 #include <FindDirectory.h> 15 #include <Path.h> 16 #include <interface/Screen.h> 17 #include <kernel/OS.h> 18 #include <stdio.h> 19 #include <string.h> 20 21 Prefs::Prefs() 22 : fatalerror(false) 23 { 24 BPath path; 25 find_directory(B_USER_SETTINGS_DIRECTORY, &path); 26 path.Append("Pulse_settings"); 27 file = new BFile(path.Path(), B_READ_WRITE | B_CREATE_FILE); 28 if (file->InitCheck() != B_OK) { 29 fatalerror = true; 30 return; 31 } 32 33 int i = NORMAL_WINDOW_MODE; 34 if (!GetInt("window_mode", &window_mode, &i)) { 35 fatalerror = true; 36 return; 37 } 38 39 // These three prefs require a connection to the app_server 40 BRect r = GetNormalWindowRect(); 41 if (!GetRect("normal_window_rect", &normal_window_rect, &r)) { 42 fatalerror = true; 43 return; 44 } 45 // While normal window position is under user control, size is not. 46 // Width is fixed and height must be dynamically computed each time, 47 // as number of CPUs could change since boot. 48 ComputeNormalWindowSize(); 49 50 r = GetMiniWindowRect(); 51 if (!GetRect("mini_window_rect", &mini_window_rect, &r)) { 52 fatalerror = true; 53 return; 54 } 55 56 r.Set(100, 100, 415, 329); 57 if (!GetRect("prefs_window_rect", &prefs_window_rect, &r)) { 58 fatalerror = true; 59 return; 60 } 61 62 i = DEFAULT_NORMAL_BAR_COLOR; 63 if (!GetInt("normal_bar_color", &normal_bar_color, &i)) { 64 fatalerror = true; 65 return; 66 } 67 68 i = DEFAULT_MINI_ACTIVE_COLOR; 69 if (!GetInt("mini_active_color", &mini_active_color, &i)) { 70 fatalerror = true; 71 return; 72 } 73 74 i = DEFAULT_MINI_IDLE_COLOR; 75 if (!GetInt("mini_idle_color", &mini_idle_color, &i)) { 76 fatalerror = true; 77 return; 78 } 79 80 i = DEFAULT_MINI_FRAME_COLOR; 81 if (!GetInt("mini_frame_color", &mini_frame_color, &i)) { 82 fatalerror = true; 83 return; 84 } 85 86 i = DEFAULT_DESKBAR_ACTIVE_COLOR; 87 if (!GetInt("deskbar_active_color", &deskbar_active_color, &i)) { 88 fatalerror = true; 89 return; 90 } 91 92 i = DEFAULT_DESKBAR_IDLE_COLOR; 93 if (!GetInt("deskbar_idle_color", &deskbar_idle_color, &i)) { 94 fatalerror = true; 95 return; 96 } 97 98 i = DEFAULT_DESKBAR_FRAME_COLOR; 99 if (!GetInt("deskbar_frame_color", &deskbar_frame_color, &i)) { 100 fatalerror = true; 101 return; 102 } 103 104 bool b = DEFAULT_NORMAL_FADE_COLORS; 105 if (!GetBool("normal_fade_colors", &normal_fade_colors, &b)) { 106 fatalerror = true; 107 return; 108 } 109 110 // Use the default size unless it would prevent having at least 111 // a one pixel wide display per CPU... this will only happen with > quad 112 i = DEFAULT_DESKBAR_ICON_WIDTH; 113 if (i < GetMinimumViewWidth()) 114 i = GetMinimumViewWidth(); 115 if (!GetInt("deskbar_icon_width", &deskbar_icon_width, &i)) { 116 fatalerror = true; 117 return; 118 } 119 } 120 121 122 Prefs::~Prefs() 123 { 124 delete file; 125 } 126 127 128 float 129 Prefs::GetNormalWindowHeight() 130 { 131 system_info sys_info; 132 get_system_info(&sys_info); 133 134 float height = PROGRESS_MTOP + PROGRESS_MBOTTOM + sys_info.cpu_count * ITEM_OFFSET; 135 if (PULSEVIEW_MIN_HEIGHT > height) 136 height = PULSEVIEW_MIN_HEIGHT; 137 138 return height; 139 } 140 141 142 void 143 Prefs::ComputeNormalWindowSize() 144 { 145 normal_window_rect.right = normal_window_rect.left + PULSEVIEW_WIDTH; 146 normal_window_rect.bottom = normal_window_rect.top + GetNormalWindowHeight(); 147 } 148 149 150 BRect 151 Prefs::GetNormalWindowRect() 152 { 153 // Dock the window in the lower right hand corner just like the original 154 BRect r(0, 0, PULSEVIEW_WIDTH, GetNormalWindowHeight()); 155 BRect screen_rect = BScreen(B_MAIN_SCREEN_ID).Frame(); 156 r.OffsetTo(screen_rect.right - r.Width() - 5, screen_rect.bottom - r.Height() - 5); 157 return r; 158 } 159 160 161 BRect 162 Prefs::GetMiniWindowRect() 163 { 164 // Lower right hand corner by default 165 BRect screen_rect = BScreen(B_MAIN_SCREEN_ID).Frame(); 166 screen_rect.left = screen_rect.right - 30; 167 screen_rect.top = screen_rect.bottom - 150; 168 screen_rect.OffsetBy(-5, -5); 169 return screen_rect; 170 } 171 172 173 bool 174 Prefs::GetInt(char *name, int *value, int *defaultvalue) 175 { 176 status_t err = file->ReadAttr(name, B_INT32_TYPE, 0, value, 4); 177 if (err == B_ENTRY_NOT_FOUND) { 178 *value = *defaultvalue; 179 if (file->WriteAttr(name, B_INT32_TYPE, 0, defaultvalue, 4) < 0) { 180 printf("WriteAttr on %s died\n", name); 181 fatalerror = true; 182 return false; 183 } 184 } else if (err < 0) { 185 printf("Unknown error reading %s:\n%s\n", name, strerror(err)); 186 fatalerror = true; 187 return false; 188 } 189 return true; 190 } 191 192 193 bool 194 Prefs::GetBool(char *name, bool *value, bool *defaultvalue) 195 { 196 status_t err = file->ReadAttr(name, B_BOOL_TYPE, 0, value, 1); 197 if (err == B_ENTRY_NOT_FOUND) { 198 *value = *defaultvalue; 199 if (file->WriteAttr(name, B_BOOL_TYPE, 0, defaultvalue, 1) < 0) { 200 printf("WriteAttr on %s died\n", name); 201 fatalerror = true; 202 return false; 203 } 204 } else if (err < 0) { 205 printf("Unknown error reading %s:\n%s\n", name, strerror(err)); 206 fatalerror = true; 207 return false; 208 } 209 return true; 210 } 211 212 213 bool 214 Prefs::GetRect(char *name, BRect *value, BRect *defaultvalue) 215 { 216 status_t err = file->ReadAttr(name, B_RECT_TYPE, 0, value, sizeof(BRect)); 217 if (err == B_ENTRY_NOT_FOUND) { 218 *value = *defaultvalue; 219 if (file->WriteAttr(name, B_RECT_TYPE, 0, defaultvalue, sizeof(BRect)) < 0) { 220 printf("WriteAttr on %s died\n", name); 221 fatalerror = true; 222 return false; 223 } 224 } else if (err < 0) { 225 printf("Unknown error reading %s:\n%s\n", name, strerror(err)); 226 fatalerror = true; 227 return false; 228 } 229 return true; 230 } 231 232 233 bool 234 Prefs::PutInt(char *name, int *value) 235 { 236 status_t err = file->WriteAttr(name, B_INT32_TYPE, 0, value, 4); 237 if (err < 0) { 238 printf("Unknown error writing %s:\n%s\n", name, strerror(err)); 239 fatalerror = true; 240 return false; 241 } 242 return true; 243 } 244 245 246 bool 247 Prefs::PutBool(char *name, bool *value) 248 { 249 status_t err = file->WriteAttr(name, B_BOOL_TYPE, 0, value, 1); 250 if (err < 0) { 251 printf("Unknown error writing %s:\n%s\n", name, strerror(err)); 252 fatalerror = true; 253 return false; 254 } 255 return true; 256 } 257 258 259 bool 260 Prefs::PutRect(char *name, BRect *value) 261 { 262 status_t err = file->WriteAttr(name, B_RECT_TYPE, 0, value, sizeof(BRect)); 263 if (err < 0) { 264 printf("Unknown error writing %s:\n%s\n", name, strerror(err)); 265 fatalerror = true; 266 return false; 267 } 268 return true; 269 } 270 271 272 bool 273 Prefs::Save() 274 { 275 if (fatalerror) 276 return false; 277 278 if (!PutInt("window_mode", &window_mode) 279 || !PutRect("normal_window_rect", &normal_window_rect) 280 || !PutRect("mini_window_rect", &mini_window_rect) 281 || !PutRect("prefs_window_rect", &prefs_window_rect) 282 || !PutInt("normal_bar_color", &normal_bar_color) 283 || !PutInt("mini_active_color", &mini_active_color) 284 || !PutInt("mini_idle_color", &mini_idle_color) 285 || !PutInt("mini_frame_color", &mini_frame_color) 286 || !PutInt("deskbar_active_color", &deskbar_active_color) 287 || !PutInt("deskbar_idle_color", &deskbar_idle_color) 288 || !PutInt("deskbar_frame_color", &deskbar_frame_color) 289 || !PutBool("normal_fade_colors", &normal_fade_colors) 290 || !PutInt("deskbar_icon_width", &deskbar_icon_width)) 291 return false; 292 293 return true; 294 } 295 296