Root/
| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2006 by Massimiliano Torromeo * |
| 3 | * massimiliano.torromeo@gmail.com * |
| 4 | * * |
| 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. * |
| 9 | * * |
| 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. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program; if not, write to the * |
| 17 | * Free Software Foundation, Inc., * |
| 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 19 | ***************************************************************************/ |
| 20 | |
| 21 | #include "background.h" |
| 22 | #include "cpu.h" |
| 23 | #include "debug.h" |
| 24 | #include "filedialog.h" |
| 25 | #include "filelister.h" |
| 26 | #include "font.h" |
| 27 | #include "gmenu2x.h" |
| 28 | #include "helppopup.h" |
| 29 | #include "iconbutton.h" |
| 30 | #include "inputdialog.h" |
| 31 | #include "launcher.h" |
| 32 | #include "linkapp.h" |
| 33 | #include "mediamonitor.h" |
| 34 | #include "menu.h" |
| 35 | #include "menusettingbool.h" |
| 36 | #include "menusettingdir.h" |
| 37 | #include "menusettingfile.h" |
| 38 | #include "menusettingimage.h" |
| 39 | #include "menusettingint.h" |
| 40 | #include "menusettingmultistring.h" |
| 41 | #include "menusettingrgba.h" |
| 42 | #include "menusettingstring.h" |
| 43 | #include "messagebox.h" |
| 44 | #include "powersaver.h" |
| 45 | #include "settingsdialog.h" |
| 46 | #include "textdialog.h" |
| 47 | #include "wallpaperdialog.h" |
| 48 | #include "utilities.h" |
| 49 | |
| 50 | #include <iostream> |
| 51 | #include <sstream> |
| 52 | #include <fstream> |
| 53 | #include <algorithm> |
| 54 | #include <stdlib.h> |
| 55 | #include <unistd.h> |
| 56 | #include <math.h> |
| 57 | #include <SDL.h> |
| 58 | #include <signal.h> |
| 59 | |
| 60 | #include <sys/statvfs.h> |
| 61 | #include <errno.h> |
| 62 | |
| 63 | #ifdef PLATFORM_PANDORA |
| 64 | //#include <pnd_container.h> |
| 65 | //#include <pnd_conf.h> |
| 66 | //#include <pnd_discovery.h> |
| 67 | #endif |
| 68 | |
| 69 | //for browsing the filesystem |
| 70 | #include <sys/stat.h> |
| 71 | #include <sys/types.h> |
| 72 | #include <dirent.h> |
| 73 | |
| 74 | //for soundcard |
| 75 | #include <sys/ioctl.h> |
| 76 | #include <linux/soundcard.h> |
| 77 | |
| 78 | #include <sys/mman.h> |
| 79 | |
| 80 | using namespace std; |
| 81 | |
| 82 | #ifndef DEFAULT_WALLPAPER_PATH |
| 83 | #define DEFAULT_WALLPAPER_PATH \ |
| 84 | GMENU2X_SYSTEM_DIR "/skins/Default/wallpapers/default.png" |
| 85 | #endif |
| 86 | |
| 87 | #ifdef _CARD_ROOT |
| 88 | const char *CARD_ROOT = _CARD_ROOT; |
| 89 | #elif defined(PLATFORM_A320) || defined(PLATFORM_GCW0) |
| 90 | const char *CARD_ROOT = "/media"; |
| 91 | #else |
| 92 | const char *CARD_ROOT = "/card"; |
| 93 | #endif |
| 94 | |
| 95 | static GMenu2X *app; |
| 96 | static string gmenu2x_home; |
| 97 | |
| 98 | // Note: Keep this in sync with the enum! |
| 99 | static const char *colorNames[NUM_COLORS] = { |
| 100 | "topBarBg", |
| 101 | "bottomBarBg", |
| 102 | "selectionBg", |
| 103 | "messageBoxBg", |
| 104 | "messageBoxBorder", |
| 105 | "messageBoxSelection", |
| 106 | }; |
| 107 | |
| 108 | static enum color stringToColor(const string &name) |
| 109 | { |
| 110 | for (unsigned int i = 0; i < NUM_COLORS; i++) { |
| 111 | if (strcmp(colorNames[i], name.c_str()) == 0) { |
| 112 | return (enum color)i; |
| 113 | } |
| 114 | } |
| 115 | return (enum color)-1; |
| 116 | } |
| 117 | |
| 118 | static const char *colorToString(enum color c) |
| 119 | { |
| 120 | return colorNames[c]; |
| 121 | } |
| 122 | |
| 123 | static void quit_all(int err) { |
| 124 | delete app; |
| 125 | SDL_Quit(); |
| 126 | exit(err); |
| 127 | } |
| 128 | |
| 129 | const string GMenu2X::getHome() |
| 130 | { |
| 131 | return gmenu2x_home; |
| 132 | } |
| 133 | |
| 134 | static void set_handler(int signal, void (*handler)(int)) |
| 135 | { |
| 136 | struct sigaction sig; |
| 137 | sigaction(signal, NULL, &sig); |
| 138 | sig.sa_handler = handler; |
| 139 | sig.sa_flags |= SA_RESTART; |
| 140 | sigaction(signal, &sig, NULL); |
| 141 | } |
| 142 | |
| 143 | int main(int /*argc*/, char * /*argv*/[]) { |
| 144 | INFO("---- GMenu2X starting ----\n"); |
| 145 | |
| 146 | set_handler(SIGINT, &quit_all); |
| 147 | set_handler(SIGSEGV, &quit_all); |
| 148 | set_handler(SIGTERM, &quit_all); |
| 149 | |
| 150 | char *home = getenv("HOME"); |
| 151 | if (home == NULL) { |
| 152 | ERROR("Unable to find gmenu2x home directory. The $HOME variable is not defined.\n"); |
| 153 | return 1; |
| 154 | } |
| 155 | |
| 156 | gmenu2x_home = (string)home + (string)"/.gmenu2x"; |
| 157 | if (mkdir(gmenu2x_home.c_str(), 0770) < 0 && errno != EEXIST) { |
| 158 | ERROR("Unable to create gmenu2x home directory.\n"); |
| 159 | return 1; |
| 160 | } |
| 161 | |
| 162 | DEBUG("Home path: %s.\n", gmenu2x_home.c_str()); |
| 163 | |
| 164 | GMenu2X::run(); |
| 165 | |
| 166 | return EXIT_FAILURE; |
| 167 | } |
| 168 | |
| 169 | void GMenu2X::run() { |
| 170 | auto menu = new GMenu2X(); |
| 171 | app = menu; |
| 172 | DEBUG("Starting main()\n"); |
| 173 | menu->mainLoop(); |
| 174 | |
| 175 | app = nullptr; |
| 176 | Launcher *toLaunch = menu->toLaunch.release(); |
| 177 | delete menu; |
| 178 | |
| 179 | SDL_Quit(); |
| 180 | unsetenv("SDL_FBCON_DONT_CLEAR"); |
| 181 | |
| 182 | if (toLaunch) { |
| 183 | toLaunch->exec(); |
| 184 | // If control gets here, execution failed. Since we already destructed |
| 185 | // everything, the easiest solution is to exit and let the system |
| 186 | // respawn the menu. |
| 187 | delete toLaunch; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | #ifdef ENABLE_CPUFREQ |
| 192 | void GMenu2X::initCPULimits() { |
| 193 | // Note: These values are for the Dingoo. |
| 194 | // The NanoNote does not have cpufreq enabled in its kernel and |
| 195 | // other devices are not actively maintained. |
| 196 | // TODO: Read min and max from sysfs. |
| 197 | cpuFreqMin = 30; |
| 198 | cpuFreqMax = 500; |
| 199 | cpuFreqSafeMax = 420; |
| 200 | cpuFreqMenuDefault = 200; |
| 201 | cpuFreqAppDefault = 384; |
| 202 | cpuFreqMultiple = 24; |
| 203 | |
| 204 | // Round min and max values to the specified multiple. |
| 205 | cpuFreqMin = ((cpuFreqMin + cpuFreqMultiple - 1) / cpuFreqMultiple) |
| 206 | * cpuFreqMultiple; |
| 207 | cpuFreqMax = (cpuFreqMax / cpuFreqMultiple) * cpuFreqMultiple; |
| 208 | cpuFreqSafeMax = (cpuFreqSafeMax / cpuFreqMultiple) * cpuFreqMultiple; |
| 209 | cpuFreqMenuDefault = (cpuFreqMenuDefault / cpuFreqMultiple) * cpuFreqMultiple; |
| 210 | cpuFreqAppDefault = (cpuFreqAppDefault / cpuFreqMultiple) * cpuFreqMultiple; |
| 211 | } |
| 212 | #endif |
| 213 | |
| 214 | GMenu2X::GMenu2X() |
| 215 | : input(*this, powerSaver) |
| 216 | { |
| 217 | usbnet = samba = inet = web = false; |
| 218 | useSelectionPng = false; |
| 219 | |
| 220 | #ifdef ENABLE_CPUFREQ |
| 221 | initCPULimits(); |
| 222 | #endif |
| 223 | //load config data |
| 224 | readConfig(); |
| 225 | |
| 226 | halfX = resX/2; |
| 227 | halfY = resY/2; |
| 228 | bottomBarIconY = resY-18; |
| 229 | bottomBarTextY = resY-10; |
| 230 | |
| 231 | /* Do not clear the screen on exit. |
| 232 | * This may require an SDL patch available at |
| 233 | * https://github.com/mthuurne/opendingux-buildroot/blob |
| 234 | * /opendingux-2010.11/package/sdl/sdl-fbcon-clear-onexit.patch |
| 235 | */ |
| 236 | setenv("SDL_FBCON_DONT_CLEAR", "1", 0); |
| 237 | |
| 238 | if( SDL_Init(SDL_INIT_TIMER) < 0) { |
| 239 | ERROR("Could not initialize SDL: %s\n", SDL_GetError()); |
| 240 | // TODO: We don't use exceptions, so don't put things that can fail |
| 241 | // in a constructor. |
| 242 | exit(EXIT_FAILURE); |
| 243 | } |
| 244 | |
| 245 | bg = NULL; |
| 246 | font = NULL; |
| 247 | setSkin(confStr["skin"], !fileExists(confStr["wallpaper"])); |
| 248 | layers.insert(layers.begin(), make_shared<Background>(*this)); |
| 249 | |
| 250 | /* We enable video at a later stage, so that the menu elements are |
| 251 | * loaded before SDL inits the video; this is made so that we won't show |
| 252 | * a black screen for a couple of seconds. */ |
| 253 | if( SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) { |
| 254 | ERROR("Could not initialize SDL: %s\n", SDL_GetError()); |
| 255 | // TODO: We don't use exceptions, so don't put things that can fail |
| 256 | // in a constructor. |
| 257 | exit(EXIT_FAILURE); |
| 258 | } |
| 259 | |
| 260 | SDL_WM_SetCaption("GMenu2X", nullptr); |
| 261 | |
| 262 | s = OutputSurface::open(resX, resY, confInt["videoBpp"]); |
| 263 | |
| 264 | if (!fileExists(confStr["wallpaper"])) { |
| 265 | DEBUG("No wallpaper defined; we will take the default one.\n"); |
| 266 | confStr["wallpaper"] = DEFAULT_WALLPAPER_PATH; |
| 267 | } |
| 268 | |
| 269 | initBG(); |
| 270 | |
| 271 | /* the menu may take a while to load, so we show the background here */ |
| 272 | for (auto layer : layers) |
| 273 | layer->paint(*s); |
| 274 | s->flip(); |
| 275 | |
| 276 | initMenu(); |
| 277 | |
| 278 | #ifdef ENABLE_INOTIFY |
| 279 | monitor = new MediaMonitor(CARD_ROOT); |
| 280 | #endif |
| 281 | |
| 282 | if (!input.init(menu.get())) { |
| 283 | exit(EXIT_FAILURE); |
| 284 | } |
| 285 | |
| 286 | powerSaver.setScreenTimeout(confInt["backlightTimeout"]); |
| 287 | |
| 288 | #ifdef ENABLE_CPUFREQ |
| 289 | setClock(confInt["menuClock"]); |
| 290 | #endif |
| 291 | } |
| 292 | |
| 293 | GMenu2X::~GMenu2X() { |
| 294 | fflush(NULL); |
| 295 | sc.clear(); |
| 296 | |
| 297 | #ifdef ENABLE_INOTIFY |
| 298 | delete monitor; |
| 299 | #endif |
| 300 | } |
| 301 | |
| 302 | void GMenu2X::initBG() { |
| 303 | bg.reset(); |
| 304 | bgmain.reset(); |
| 305 | |
| 306 | // Load wallpaper. |
| 307 | bg = OffscreenSurface::loadImage(confStr["wallpaper"]); |
| 308 | if (!bg) { |
| 309 | bg = OffscreenSurface::emptySurface(resX, resY); |
| 310 | } |
| 311 | |
| 312 | drawTopBar(*bg); |
| 313 | drawBottomBar(*bg); |
| 314 | |
| 315 | bgmain.reset(new OffscreenSurface(*bg)); |
| 316 | |
| 317 | { |
| 318 | auto sd = OffscreenSurface::loadImage( |
| 319 | sc.getSkinFilePath("imgs/sd.png")); |
| 320 | if (sd) sd->blit(*bgmain, 3, bottomBarIconY); |
| 321 | } |
| 322 | |
| 323 | cpuX = 32 + font->write(*bgmain, getDiskFree(getHome().c_str()), |
| 324 | 22, bottomBarTextY, Font::HAlignLeft, Font::VAlignMiddle); |
| 325 | |
| 326 | #ifdef ENABLE_CPUFREQ |
| 327 | { |
| 328 | auto cpu = OffscreenSurface::loadImage( |
| 329 | sc.getSkinFilePath("imgs/cpu.png")); |
| 330 | if (cpu) cpu->blit(*bgmain, cpuX, bottomBarIconY); |
| 331 | } |
| 332 | cpuX += 19; |
| 333 | manualX = cpuX + font->getTextWidth("300MHz") + 5; |
| 334 | #else |
| 335 | manualX = cpuX; |
| 336 | #endif |
| 337 | |
| 338 | int serviceX = resX-38; |
| 339 | if (usbnet) { |
| 340 | if (web) { |
| 341 | auto webserver = OffscreenSurface::loadImage( |
| 342 | sc.getSkinFilePath("imgs/webserver.png")); |
| 343 | if (webserver) webserver->blit(*bgmain, serviceX, bottomBarIconY); |
| 344 | serviceX -= 19; |
| 345 | } |
| 346 | if (samba) { |
| 347 | auto sambaS = OffscreenSurface::loadImage( |
| 348 | sc.getSkinFilePath("imgs/samba.png")); |
| 349 | if (sambaS) sambaS->blit(*bgmain, serviceX, bottomBarIconY); |
| 350 | serviceX -= 19; |
| 351 | } |
| 352 | if (inet) { |
| 353 | auto inetS = OffscreenSurface::loadImage( |
| 354 | sc.getSkinFilePath("imgs/inet.png")); |
| 355 | if (inetS) inetS->blit(*bgmain, serviceX, bottomBarIconY); |
| 356 | serviceX -= 19; |
| 357 | } |
| 358 | } |
| 359 | (void)serviceX; |
| 360 | |
| 361 | bgmain->convertToDisplayFormat(); |
| 362 | } |
| 363 | |
| 364 | void GMenu2X::initFont() { |
| 365 | string path = skinConfStr["font"]; |
| 366 | if (!path.empty()) { |
| 367 | unsigned int size = skinConfInt["fontsize"]; |
| 368 | if (!size) |
| 369 | size = 12; |
| 370 | if (path.substr(0,5)=="skin:") |
| 371 | path = sc.getSkinFilePath(path.substr(5, path.length())); |
| 372 | font.reset(new Font(path, size)); |
| 373 | } else { |
| 374 | font = Font::defaultFont(); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | void GMenu2X::initMenu() { |
| 379 | //Menu structure handler |
| 380 | menu.reset(new Menu(*this)); |
| 381 | |
| 382 | // Add action links in the applications section. |
| 383 | auto appIdx = menu->sectionNamed("applications"); |
| 384 | menu->addActionLink(appIdx, "Explorer", |
| 385 | bind(&GMenu2X::explorer, this), |
| 386 | tr["Launch an application"], |
| 387 | "skin:icons/explorer.png"); |
| 388 | |
| 389 | // Add action links in the settings section. |
| 390 | auto settingIdx = menu->sectionNamed("settings"); |
| 391 | menu->addActionLink(settingIdx, "GMenu2X", |
| 392 | bind(&GMenu2X::showSettings, this), |
| 393 | tr["Configure GMenu2X's options"], |
| 394 | "skin:icons/configure.png"); |
| 395 | menu->addActionLink(settingIdx, tr["Skin"], |
| 396 | bind(&GMenu2X::skinMenu, this), |
| 397 | tr["Configure skin"], |
| 398 | "skin:icons/skin.png"); |
| 399 | menu->addActionLink(settingIdx, tr["Wallpaper"], |
| 400 | bind(&GMenu2X::changeWallpaper, this), |
| 401 | tr["Change GMenu2X wallpaper"], |
| 402 | "skin:icons/wallpaper.png"); |
| 403 | if (fileExists(LOG_FILE)) { |
| 404 | menu->addActionLink(settingIdx, tr["Log Viewer"], |
| 405 | bind(&GMenu2X::viewLog, this), |
| 406 | tr["Displays last launched program's output"], |
| 407 | "skin:icons/ebook.png"); |
| 408 | } |
| 409 | menu->addActionLink(settingIdx, tr["About"], |
| 410 | bind(&GMenu2X::about, this), |
| 411 | tr["Info about GMenu2X"], |
| 412 | "skin:icons/about.png"); |
| 413 | |
| 414 | menu->skinUpdated(); |
| 415 | menu->orderLinks(); |
| 416 | |
| 417 | menu->setSectionIndex(confInt["section"]); |
| 418 | menu->setLinkIndex(confInt["link"]); |
| 419 | |
| 420 | layers.push_back(menu); |
| 421 | } |
| 422 | |
| 423 | void GMenu2X::about() { |
| 424 | string text(readFileAsString(GMENU2X_SYSTEM_DIR "/about.txt")); |
| 425 | string build_date("Build date: " __DATE__); |
| 426 | TextDialog td(*this, "GMenu2X", build_date, "icons/about.png", text); |
| 427 | td.exec(); |
| 428 | } |
| 429 | |
| 430 | void GMenu2X::viewLog() { |
| 431 | string text(readFileAsString(LOG_FILE)); |
| 432 | |
| 433 | TextDialog td(*this, tr["Log Viewer"], |
| 434 | tr["Displays last launched program's output"], |
| 435 | "icons/ebook.png", text); |
| 436 | td.exec(); |
| 437 | |
| 438 | MessageBox mb(*this, tr["Do you want to delete the log file?"], |
| 439 | "icons/ebook.png"); |
| 440 | mb.setButton(InputManager::ACCEPT, tr["Yes"]); |
| 441 | mb.setButton(InputManager::CANCEL, tr["No"]); |
| 442 | if (mb.exec() == InputManager::ACCEPT) { |
| 443 | unlink(LOG_FILE); |
| 444 | menu->deleteSelectedLink(); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | void GMenu2X::readConfig() { |
| 449 | string conffile = GMENU2X_SYSTEM_DIR "/gmenu2x.conf"; |
| 450 | readConfig(conffile); |
| 451 | |
| 452 | conffile = getHome() + "/gmenu2x.conf"; |
| 453 | readConfig(conffile); |
| 454 | } |
| 455 | |
| 456 | void GMenu2X::readConfig(string conffile) { |
| 457 | ifstream inf(conffile.c_str(), ios_base::in); |
| 458 | if (inf.is_open()) { |
| 459 | string line; |
| 460 | while (getline(inf, line, '\n')) { |
| 461 | string::size_type pos = line.find("="); |
| 462 | string name = trim(line.substr(0,pos)); |
| 463 | string value = trim(line.substr(pos+1,line.length())); |
| 464 | |
| 465 | if (value.length()>1 && value.at(0)=='"' && value.at(value.length()-1)=='"') |
| 466 | confStr[name] = value.substr(1,value.length()-2); |
| 467 | else |
| 468 | confInt[name] = atoi(value.c_str()); |
| 469 | } |
| 470 | inf.close(); |
| 471 | } |
| 472 | |
| 473 | if (!confStr["lang"].empty()) |
| 474 | tr.setLang(confStr["lang"]); |
| 475 | |
| 476 | if (!confStr["wallpaper"].empty() && !fileExists(confStr["wallpaper"])) |
| 477 | confStr["wallpaper"] = ""; |
| 478 | |
| 479 | if (confStr["skin"].empty() || SurfaceCollection::getSkinPath(confStr["skin"]).empty()) |
| 480 | confStr["skin"] = "Default"; |
| 481 | |
| 482 | evalIntConf( confInt, "outputLogs", 0, 0,1 ); |
| 483 | #ifdef ENABLE_CPUFREQ |
| 484 | evalIntConf( confInt, "maxClock", |
| 485 | cpuFreqSafeMax, cpuFreqMin, cpuFreqMax ); |
| 486 | evalIntConf( confInt, "menuClock", |
| 487 | cpuFreqMenuDefault, cpuFreqMin, cpuFreqSafeMax ); |
| 488 | #endif |
| 489 | evalIntConf( confInt, "backlightTimeout", 15, 0,120 ); |
| 490 | evalIntConf( confInt, "buttonRepeatRate", 10, 0, 20 ); |
| 491 | evalIntConf( confInt, "videoBpp", 32, 16, 32 ); |
| 492 | |
| 493 | if (confStr["tvoutEncoding"] != "PAL") confStr["tvoutEncoding"] = "NTSC"; |
| 494 | resX = constrain( confInt["resolutionX"], 320,1920 ); |
| 495 | resY = constrain( confInt["resolutionY"], 240,1200 ); |
| 496 | } |
| 497 | |
| 498 | void GMenu2X::saveSelection() { |
| 499 | if (confInt["saveSelection"] && ( |
| 500 | confInt["section"] != menu->selSectionIndex() |
| 501 | || confInt["link"] != menu->selLinkIndex() |
| 502 | )) { |
| 503 | writeConfig(); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | void GMenu2X::writeConfig() { |
| 508 | string conffile = getHome() + "/gmenu2x.conf"; |
| 509 | ofstream inf(conffile.c_str()); |
| 510 | if (inf.is_open()) { |
| 511 | ConfStrHash::iterator endS = confStr.end(); |
| 512 | for(ConfStrHash::iterator curr = confStr.begin(); curr != endS; curr++) |
| 513 | inf << curr->first << "=\"" << curr->second << "\"" << endl; |
| 514 | |
| 515 | ConfIntHash::iterator endI = confInt.end(); |
| 516 | for(ConfIntHash::iterator curr = confInt.begin(); curr != endI; curr++) |
| 517 | inf << curr->first << "=" << curr->second << endl; |
| 518 | |
| 519 | inf.close(); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | void GMenu2X::writeSkinConfig() { |
| 524 | string conffile = getHome() + "/skins/"; |
| 525 | if (mkdir(conffile.c_str(), 0770) < 0 && errno != EEXIST) { |
| 526 | ERROR("Failed to create directory %s to write skin configuration: %s\n", conffile.c_str(), strerror(errno)); |
| 527 | return; |
| 528 | } |
| 529 | conffile = conffile + confStr["skin"]; |
| 530 | if (mkdir(conffile.c_str(), 0770) < 0 && errno != EEXIST) { |
| 531 | ERROR("Failed to create directory %s to write skin configuration: %s\n", conffile.c_str(), strerror(errno)); |
| 532 | return; |
| 533 | } |
| 534 | conffile = conffile + "/skin.conf"; |
| 535 | |
| 536 | ofstream inf(conffile.c_str()); |
| 537 | if (inf.is_open()) { |
| 538 | ConfStrHash::iterator endS = skinConfStr.end(); |
| 539 | for(ConfStrHash::iterator curr = skinConfStr.begin(); curr != endS; curr++) |
| 540 | inf << curr->first << "=\"" << curr->second << "\"" << endl; |
| 541 | |
| 542 | ConfIntHash::iterator endI = skinConfInt.end(); |
| 543 | for(ConfIntHash::iterator curr = skinConfInt.begin(); curr != endI; curr++) |
| 544 | inf << curr->first << "=" << curr->second << endl; |
| 545 | |
| 546 | int i; |
| 547 | for (i = 0; i < NUM_COLORS; ++i) { |
| 548 | inf << colorToString((enum color)i) << "=#" |
| 549 | << skinConfColors[i] << endl; |
| 550 | } |
| 551 | |
| 552 | inf.close(); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | void GMenu2X::readTmp() { |
| 557 | lastSelectorElement = -1; |
| 558 | ifstream inf("/tmp/gmenu2x.tmp", ios_base::in); |
| 559 | if (inf.is_open()) { |
| 560 | string line; |
| 561 | string section = ""; |
| 562 | while (getline(inf, line, '\n')) { |
| 563 | string::size_type pos = line.find("="); |
| 564 | string name = trim(line.substr(0,pos)); |
| 565 | string value = trim(line.substr(pos+1,line.length())); |
| 566 | |
| 567 | if (name=="section") |
| 568 | menu->setSectionIndex(atoi(value.c_str())); |
| 569 | else if (name=="link") |
| 570 | menu->setLinkIndex(atoi(value.c_str())); |
| 571 | else if (name=="selectorelem") |
| 572 | lastSelectorElement = atoi(value.c_str()); |
| 573 | else if (name=="selectordir") |
| 574 | lastSelectorDir = value; |
| 575 | } |
| 576 | inf.close(); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | void GMenu2X::writeTmp(int selelem, const string &selectordir) { |
| 581 | string conffile = "/tmp/gmenu2x.tmp"; |
| 582 | ofstream inf(conffile.c_str()); |
| 583 | if (inf.is_open()) { |
| 584 | inf << "section=" << menu->selSectionIndex() << endl; |
| 585 | inf << "link=" << menu->selLinkIndex() << endl; |
| 586 | if (selelem>-1) |
| 587 | inf << "selectorelem=" << selelem << endl; |
| 588 | if (!selectordir.empty()) |
| 589 | inf << "selectordir=" << selectordir << endl; |
| 590 | inf.close(); |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | void GMenu2X::mainLoop() { |
| 595 | if (!fileExists(CARD_ROOT)) |
| 596 | CARD_ROOT = ""; |
| 597 | |
| 598 | // Recover last session |
| 599 | readTmp(); |
| 600 | if (lastSelectorElement > -1 && menu->selLinkApp() && |
| 601 | (!menu->selLinkApp()->getSelectorDir().empty() |
| 602 | || !lastSelectorDir.empty())) |
| 603 | menu->selLinkApp()->selector(lastSelectorElement, lastSelectorDir); |
| 604 | |
| 605 | while (true) { |
| 606 | // Remove dismissed layers from the stack. |
| 607 | for (auto it = layers.begin(); it != layers.end(); ) { |
| 608 | if ((*it)->getStatus() == Layer::Status::DISMISSED) { |
| 609 | it = layers.erase(it); |
| 610 | } else { |
| 611 | ++it; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | // Run animations. |
| 616 | bool animating = false; |
| 617 | for (auto layer : layers) { |
| 618 | animating |= layer->runAnimations(); |
| 619 | } |
| 620 | |
| 621 | // Paint layers. |
| 622 | for (auto layer : layers) { |
| 623 | layer->paint(*s); |
| 624 | } |
| 625 | s->flip(); |
| 626 | |
| 627 | // Exit main loop once we have something to launch. |
| 628 | if (toLaunch) { |
| 629 | break; |
| 630 | } |
| 631 | |
| 632 | // Handle other input events. |
| 633 | InputManager::Button button; |
| 634 | bool gotEvent; |
| 635 | const bool wait = !animating; |
| 636 | do { |
| 637 | gotEvent = input.getButton(&button, wait); |
| 638 | } while (wait && !gotEvent); |
| 639 | if (gotEvent) { |
| 640 | if (button == InputManager::QUIT) { |
| 641 | break; |
| 642 | } |
| 643 | for (auto it = layers.rbegin(); it != layers.rend(); ++it) { |
| 644 | if ((*it)->handleButtonPress(button)) { |
| 645 | break; |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | void GMenu2X::explorer() { |
| 653 | FileDialog fd(*this, tr["Select an application"], "sh,bin,py,elf,"); |
| 654 | if (fd.exec()) { |
| 655 | if (confInt["saveSelection"] && (confInt["section"]!=menu->selSectionIndex() || confInt["link"]!=menu->selLinkIndex())) |
| 656 | writeConfig(); |
| 657 | |
| 658 | string command = cmdclean(fd.getPath()+"/"+fd.getFile()); |
| 659 | chdir(fd.getPath().c_str()); |
| 660 | #ifdef ENABLE_CPUFREQ |
| 661 | setClock(cpuFreqAppDefault); |
| 662 | #endif |
| 663 | |
| 664 | toLaunch.reset(new Launcher( |
| 665 | vector<string> { "/bin/sh", "-c", command })); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | void GMenu2X::queueLaunch( |
| 670 | unique_ptr<Launcher>&& launcher, shared_ptr<Layer> launchLayer |
| 671 | ) { |
| 672 | toLaunch = move(launcher); |
| 673 | layers.push_back(launchLayer); |
| 674 | } |
| 675 | |
| 676 | void GMenu2X::showHelpPopup() { |
| 677 | layers.push_back(make_shared<HelpPopup>(*this)); |
| 678 | } |
| 679 | |
| 680 | void GMenu2X::showSettings() { |
| 681 | #ifdef ENABLE_CPUFREQ |
| 682 | int curMenuClock = confInt["menuClock"]; |
| 683 | #endif |
| 684 | |
| 685 | FileLister fl_tr; |
| 686 | fl_tr.setShowDirectories(false); |
| 687 | fl_tr.browse(GMENU2X_SYSTEM_DIR "/translations"); |
| 688 | fl_tr.browse(getHome() + "/translations", false); |
| 689 | |
| 690 | vector<string> translations = fl_tr.getFiles(); |
| 691 | translations.insert(translations.begin(), "English"); |
| 692 | string lang = tr.lang(); |
| 693 | |
| 694 | vector<string> encodings; |
| 695 | encodings.push_back("NTSC"); |
| 696 | encodings.push_back("PAL"); |
| 697 | |
| 698 | SettingsDialog sd(*this, input, tr["Settings"]); |
| 699 | sd.addSetting(unique_ptr<MenuSetting>(new MenuSettingMultiString( |
| 700 | *this, tr["Language"], |
| 701 | tr["Set the language used by GMenu2X"], |
| 702 | &lang, &translations))); |
| 703 | sd.addSetting(unique_ptr<MenuSetting>(new MenuSettingBool( |
| 704 | *this, tr["Save last selection"], |
| 705 | tr["Save the last selected link and section on exit"], |
| 706 | &confInt["saveSelection"]))); |
| 707 | #ifdef ENABLE_CPUFREQ |
| 708 | sd.addSetting(unique_ptr<MenuSetting>(new MenuSettingInt( |
| 709 | *this, tr["Clock for GMenu2X"], |
| 710 | tr["Set the cpu working frequency when running GMenu2X"], |
| 711 | &confInt["menuClock"], cpuFreqMin, cpuFreqSafeMax, cpuFreqMultiple))); |
| 712 | sd.addSetting(unique_ptr<MenuSetting>(new MenuSettingInt( |
| 713 | *this, tr["Maximum overclock"], |
| 714 | tr["Set the maximum overclock for launching links"], |
| 715 | &confInt["maxClock"], cpuFreqMin, cpuFreqMax, cpuFreqMultiple))); |
| 716 | #endif |
| 717 | sd.addSetting(unique_ptr<MenuSetting>(new MenuSettingBool( |
| 718 | *this, tr["Output logs"], |
| 719 | tr["Logs the output of the links. Use the Log Viewer to read them."], |
| 720 | &confInt["outputLogs"]))); |
| 721 | sd.addSetting(unique_ptr<MenuSetting>(new MenuSettingInt( |
| 722 | *this, tr["Screen Timeout"], |
| 723 | tr["Set screen's backlight timeout in seconds"], |
| 724 | &confInt["backlightTimeout"], 0, 120))); |
| 725 | sd.addSetting(unique_ptr<MenuSetting>(new MenuSettingInt( |
| 726 | *this, tr["Button repeat rate"], |
| 727 | tr["Set button repetitions per second"], |
| 728 | &confInt["buttonRepeatRate"], 0, 20))); |
| 729 | |
| 730 | if (sd.exec()) { |
| 731 | #ifdef ENABLE_CPUFREQ |
| 732 | if (curMenuClock != confInt["menuClock"]) setClock(confInt["menuClock"]); |
| 733 | #endif |
| 734 | |
| 735 | powerSaver.setScreenTimeout(confInt["backlightTimeout"]); |
| 736 | |
| 737 | input.repeatRateChanged(); |
| 738 | |
| 739 | if (lang == "English") lang = ""; |
| 740 | if (lang != tr.lang()) { |
| 741 | tr.setLang(lang); |
| 742 | confStr["lang"] = lang; |
| 743 | } |
| 744 | |
| 745 | writeConfig(); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | void GMenu2X::skinMenu() { |
| 750 | FileLister fl_sk; |
| 751 | fl_sk.setShowFiles(false); |
| 752 | fl_sk.setShowUpdir(false); |
| 753 | fl_sk.browse(getHome() + "/skins"); |
| 754 | fl_sk.browse(GMENU2X_SYSTEM_DIR "/skins", false); |
| 755 | |
| 756 | string curSkin = confStr["skin"]; |
| 757 | |
| 758 | SettingsDialog sd(*this, input, tr["Skin"]); |
| 759 | sd.addSetting(unique_ptr<MenuSetting>(new MenuSettingMultiString( |
| 760 | *this, tr["Skin"], |
| 761 | tr["Set the skin used by GMenu2X"], |
| 762 | &confStr["skin"], &fl_sk.getDirectories()))); |
| 763 | sd.addSetting(unique_ptr<MenuSetting>(new MenuSettingRGBA( |
| 764 | *this, tr["Top Bar"], |
| 765 | tr["Color of the top bar"], |
| 766 | &skinConfColors[COLOR_TOP_BAR_BG]))); |
| 767 | sd.addSetting(unique_ptr<MenuSetting>(new MenuSettingRGBA( |
| 768 | *this, tr["Bottom Bar"], |
| 769 | tr["Color of the bottom bar"], |
| 770 | &skinConfColors[COLOR_BOTTOM_BAR_BG]))); |
| 771 | sd.addSetting(unique_ptr<MenuSetting>(new MenuSettingRGBA( |
| 772 | *this, tr["Selection"], |
| 773 | tr["Color of the selection and other interface details"], |
| 774 | &skinConfColors[COLOR_SELECTION_BG]))); |
| 775 | sd.addSetting(unique_ptr<MenuSetting>(new MenuSettingRGBA( |
| 776 | *this, tr["Message Box"], |
| 777 | tr["Background color of the message box"], |
| 778 | &skinConfColors[COLOR_MESSAGE_BOX_BG]))); |
| 779 | sd.addSetting(unique_ptr<MenuSetting>(new MenuSettingRGBA( |
| 780 | *this, tr["Message Box Border"], |
| 781 | tr["Border color of the message box"], |
| 782 | &skinConfColors[COLOR_MESSAGE_BOX_BORDER]))); |
| 783 | sd.addSetting(unique_ptr<MenuSetting>(new MenuSettingRGBA( |
| 784 | *this, tr["Message Box Selection"], |
| 785 | tr["Color of the selection of the message box"], |
| 786 | &skinConfColors[COLOR_MESSAGE_BOX_SELECTION]))); |
| 787 | |
| 788 | if (sd.exec()) { |
| 789 | if (curSkin != confStr["skin"]) { |
| 790 | setSkin(confStr["skin"]); |
| 791 | writeConfig(); |
| 792 | } |
| 793 | writeSkinConfig(); |
| 794 | initBG(); |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | void GMenu2X::setSkin(const string &skin, bool setWallpaper) { |
| 799 | confStr["skin"] = skin; |
| 800 | |
| 801 | //Clear previous skin settings |
| 802 | skinConfStr.clear(); |
| 803 | skinConfInt.clear(); |
| 804 | |
| 805 | DEBUG("GMenu2X: setting new skin %s.\n", skin.c_str()); |
| 806 | |
| 807 | //clear collection and change the skin path |
| 808 | sc.clear(); |
| 809 | sc.setSkin(skin); |
| 810 | |
| 811 | //reset colors to the default values |
| 812 | skinConfColors[COLOR_TOP_BAR_BG] = RGBAColor(255, 255, 255, 130); |
| 813 | skinConfColors[COLOR_BOTTOM_BAR_BG] = RGBAColor(255, 255, 255, 130); |
| 814 | skinConfColors[COLOR_SELECTION_BG] = RGBAColor(255, 255, 255, 130); |
| 815 | skinConfColors[COLOR_MESSAGE_BOX_BG] = RGBAColor(255, 255, 255); |
| 816 | skinConfColors[COLOR_MESSAGE_BOX_BORDER] = RGBAColor(80, 80, 80); |
| 817 | skinConfColors[COLOR_MESSAGE_BOX_SELECTION] = RGBAColor(160, 160, 160); |
| 818 | |
| 819 | /* Load skin settings from user directory if present, |
| 820 | * or from the system directory. */ |
| 821 | if (!readSkinConfig(getHome() + "/skins/" + skin + "/skin.conf")) { |
| 822 | readSkinConfig(GMENU2X_SYSTEM_DIR "/skins/" + skin + "/skin.conf"); |
| 823 | } |
| 824 | |
| 825 | if (setWallpaper && !skinConfStr["wallpaper"].empty()) { |
| 826 | string fp = sc.getSkinFilePath("wallpapers/" + skinConfStr["wallpaper"]); |
| 827 | if (!fp.empty()) |
| 828 | confStr["wallpaper"] = fp; |
| 829 | else |
| 830 | WARNING("Unable to find wallpaper defined on skin %s\n", skin.c_str()); |
| 831 | } |
| 832 | |
| 833 | evalIntConf(skinConfInt, "topBarHeight", 50, 32, 120); |
| 834 | evalIntConf(skinConfInt, "bottomBarHeight", 20, 20, 120); |
| 835 | evalIntConf(skinConfInt, "linkHeight", 50, 32, 120); |
| 836 | evalIntConf(skinConfInt, "linkWidth", 80, 32, 120); |
| 837 | |
| 838 | if (menu != NULL) menu->skinUpdated(); |
| 839 | |
| 840 | //Selection png |
| 841 | useSelectionPng = sc.addSkinRes("imgs/selection.png", false) != NULL; |
| 842 | |
| 843 | //font |
| 844 | initFont(); |
| 845 | } |
| 846 | |
| 847 | bool GMenu2X::readSkinConfig(const string& conffile) |
| 848 | { |
| 849 | ifstream skinconf(conffile.c_str(), ios_base::in); |
| 850 | if (skinconf.is_open()) { |
| 851 | string line; |
| 852 | while (getline(skinconf, line, '\n')) { |
| 853 | line = trim(line); |
| 854 | DEBUG("skinconf: '%s'\n", line.c_str()); |
| 855 | string::size_type pos = line.find("="); |
| 856 | string name = trim(line.substr(0,pos)); |
| 857 | string value = trim(line.substr(pos+1,line.length())); |
| 858 | |
| 859 | if (value.length()>0) { |
| 860 | if (value.length()>1 && value.at(0)=='"' && value.at(value.length()-1)=='"') |
| 861 | skinConfStr[name] = value.substr(1,value.length()-2); |
| 862 | else if (value.at(0) == '#') |
| 863 | skinConfColors[stringToColor(name)] = |
| 864 | RGBAColor::fromString(value.substr(1, value.length())); |
| 865 | else |
| 866 | skinConfInt[name] = atoi(value.c_str()); |
| 867 | } |
| 868 | } |
| 869 | skinconf.close(); |
| 870 | return true; |
| 871 | } else { |
| 872 | return false; |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | void GMenu2X::showManual() { |
| 877 | menu->selLinkApp()->showManual(); |
| 878 | } |
| 879 | |
| 880 | void GMenu2X::showContextMenu() { |
| 881 | layers.push_back(make_shared<ContextMenu>(*this, *menu)); |
| 882 | } |
| 883 | |
| 884 | void GMenu2X::changeWallpaper() { |
| 885 | WallpaperDialog wp(*this); |
| 886 | if (wp.exec() && confStr["wallpaper"] != wp.wallpaper) { |
| 887 | confStr["wallpaper"] = wp.wallpaper; |
| 888 | initBG(); |
| 889 | writeConfig(); |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | void GMenu2X::addLink() { |
| 894 | FileDialog fd(*this, tr["Select an application"], "sh,bin,py,elf,"); |
| 895 | if (fd.exec()) |
| 896 | menu->addLink(fd.getPath(), fd.getFile()); |
| 897 | } |
| 898 | |
| 899 | void GMenu2X::editLink() { |
| 900 | LinkApp *linkApp = menu->selLinkApp(); |
| 901 | if (!linkApp) return; |
| 902 | |
| 903 | string oldSection = menu->selSection(); |
| 904 | string newSection = oldSection; |
| 905 | |
| 906 | string linkTitle = linkApp->getTitle(); |
| 907 | string linkDescription = linkApp->getDescription(); |
| 908 | string linkIcon = linkApp->getIcon(); |
| 909 | string linkManual = linkApp->getManual(); |
| 910 | string linkSelFilter = linkApp->getSelectorFilter(); |
| 911 | string linkSelDir = linkApp->getSelectorDir(); |
| 912 | bool linkSelBrowser = linkApp->getSelectorBrowser(); |
| 913 | int linkClock = linkApp->clock(); |
| 914 | |
| 915 | string diagTitle = tr.translate("Edit $1",linkTitle.c_str(),NULL); |
| 916 | string diagIcon = linkApp->getIconPath(); |
| 917 | |
| 918 | SettingsDialog sd(*this, input, diagTitle, diagIcon); |
| 919 | if (!linkApp->isOpk()) { |
| 920 | sd.addSetting(unique_ptr<MenuSetting>(new MenuSettingString( |
| 921 | *this, tr["Title"], |
| 922 | tr["Link title"], |
| 923 | &linkTitle, diagTitle, diagIcon))); |
| 924 | sd.addSetting(unique_ptr<MenuSetting>(new MenuSettingString( |
| 925 | *this, tr["Description"], |
| 926 | tr["Link description"], |
| 927 | &linkDescription, diagTitle, diagIcon))); |
| 928 | sd.addSetting(unique_ptr<MenuSetting>(new MenuSettingMultiString( |
| 929 | *this, tr["Section"], |
| 930 | tr["The section this link belongs to"], |
| 931 | &newSection, &menu->getSections()))); |
| 932 | sd.addSetting(unique_ptr<MenuSetting>(new MenuSettingImage( |
| 933 | *this, tr["Icon"], |
| 934 | tr.translate("Select an icon for this link", linkTitle.c_str(), NULL), |
| 935 | &linkIcon, "png"))); |
| 936 | sd.addSetting(unique_ptr<MenuSetting>(new MenuSettingFile( |
| 937 | *this, tr["Manual"], |
| 938 | tr["Select a manual or README file"], |
| 939 | &linkManual, "man.png,txt"))); |
| 940 | } |
| 941 | if (!linkApp->isOpk() || !linkApp->getSelectorDir().empty()) { |
| 942 | sd.addSetting(unique_ptr<MenuSetting>(new MenuSettingDir( |
| 943 | *this, tr["Selector Directory"], |
| 944 | tr["Directory to scan for the selector"], |
| 945 | &linkSelDir))); |
| 946 | sd.addSetting(unique_ptr<MenuSetting>(new MenuSettingBool( |
| 947 | *this, tr["Selector Browser"], |
| 948 | tr["Allow the selector to change directory"], |
| 949 | &linkSelBrowser))); |
| 950 | } |
| 951 | #ifdef ENABLE_CPUFREQ |
| 952 | sd.addSetting(unique_ptr<MenuSetting>(new MenuSettingInt( |
| 953 | *this, tr["Clock frequency"], |
| 954 | tr["CPU clock frequency for this link"], |
| 955 | &linkClock, cpuFreqMin, confInt["maxClock"], cpuFreqMultiple))); |
| 956 | #endif |
| 957 | if (!linkApp->isOpk()) { |
| 958 | sd.addSetting(unique_ptr<MenuSetting>(new MenuSettingString( |
| 959 | *this, tr["Selector Filter"], |
| 960 | tr["Selector filter (Separate values with a comma)"], |
| 961 | &linkSelFilter, diagTitle, diagIcon))); |
| 962 | sd.addSetting(unique_ptr<MenuSetting>(new MenuSettingBool( |
| 963 | *this, tr["Display Console"], |
| 964 | tr["Must be enabled for console-based applications"], |
| 965 | &linkApp->consoleApp))); |
| 966 | } |
| 967 | |
| 968 | if (sd.exec()) { |
| 969 | linkApp->setTitle(linkTitle); |
| 970 | linkApp->setDescription(linkDescription); |
| 971 | linkApp->setIcon(linkIcon); |
| 972 | linkApp->setManual(linkManual); |
| 973 | linkApp->setSelectorFilter(linkSelFilter); |
| 974 | linkApp->setSelectorDir(linkSelDir); |
| 975 | linkApp->setSelectorBrowser(linkSelBrowser); |
| 976 | linkApp->setClock(linkClock); |
| 977 | linkApp->save(); |
| 978 | |
| 979 | if (oldSection != newSection) { |
| 980 | INFO("Changed section: '%s' -> '%s'\n", |
| 981 | oldSection.c_str(), newSection.c_str()); |
| 982 | menu->moveSelectedLink(newSection); |
| 983 | } |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | void GMenu2X::deleteLink() { |
| 988 | if (menu->selLinkApp()!=NULL) { |
| 989 | MessageBox mb(*this, tr.translate("Deleting $1",menu->selLink()->getTitle().c_str(),NULL)+"\n"+tr["Are you sure?"], menu->selLink()->getIconPath()); |
| 990 | mb.setButton(InputManager::ACCEPT, tr["Yes"]); |
| 991 | mb.setButton(InputManager::CANCEL, tr["No"]); |
| 992 | if (mb.exec() == InputManager::ACCEPT) |
| 993 | menu->deleteSelectedLink(); |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | void GMenu2X::addSection() { |
| 998 | InputDialog id(*this, input, tr["Insert a name for the new section"]); |
| 999 | if (id.exec()) { |
| 1000 | // Look up section; create if it doesn't exist yet. |
| 1001 | auto idx = menu->sectionNamed(id.getInput()); |
| 1002 | // Switch to the new section. |
| 1003 | menu->setSectionIndex(idx); |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | void GMenu2X::deleteSection() |
| 1008 | { |
| 1009 | menu->deleteSelectedSection(); |
| 1010 | } |
| 1011 | |
| 1012 | #ifdef ENABLE_CPUFREQ |
| 1013 | void GMenu2X::setClock(unsigned mhz) { |
| 1014 | mhz = constrain(mhz, cpuFreqMin, confInt["maxClock"]); |
| 1015 | #if defined(PLATFORM_A320) || defined(PLATFORM_GCW0) || defined(PLATFORM_NANONOTE) |
| 1016 | jz_cpuspeed(mhz); |
| 1017 | #endif |
| 1018 | } |
| 1019 | #endif |
| 1020 | |
| 1021 | string GMenu2X::getDiskFree(const char *path) { |
| 1022 | string df = ""; |
| 1023 | struct statvfs b; |
| 1024 | |
| 1025 | int ret = statvfs(path, &b); |
| 1026 | if (ret == 0) { |
| 1027 | // Make sure that the multiplication happens in 64 bits. |
| 1028 | unsigned long freeMiB = |
| 1029 | ((unsigned long long)b.f_bfree * b.f_bsize) / (1024 * 1024); |
| 1030 | unsigned long totalMiB = |
| 1031 | ((unsigned long long)b.f_blocks * b.f_frsize) / (1024 * 1024); |
| 1032 | stringstream ss; |
| 1033 | if (totalMiB >= 10000) { |
| 1034 | ss << (freeMiB / 1024) << "." << ((freeMiB % 1024) * 10) / 1024 << "/" |
| 1035 | << (totalMiB / 1024) << "." << ((totalMiB % 1024) * 10) / 1024 << "GiB"; |
| 1036 | } else { |
| 1037 | ss << freeMiB << "/" << totalMiB << "MiB"; |
| 1038 | } |
| 1039 | ss >> df; |
| 1040 | } else WARNING("statvfs failed with error '%s'.\n", strerror(errno)); |
| 1041 | return df; |
| 1042 | } |
| 1043 | |
| 1044 | int GMenu2X::drawButton(Surface& s, const string &btn, const string &text, int x, int y) { |
| 1045 | int w = 0; |
| 1046 | auto icon = sc["skin:imgs/buttons/" + btn + ".png"]; |
| 1047 | if (icon) { |
| 1048 | if (y < 0) y = resY + y; |
| 1049 | w = icon->width(); |
| 1050 | icon->blit(s, x, y - 7); |
| 1051 | if (!text.empty()) { |
| 1052 | w += 3; |
| 1053 | w += font->write( |
| 1054 | s, text, x + w, y, Font::HAlignLeft, Font::VAlignMiddle); |
| 1055 | w += 6; |
| 1056 | } |
| 1057 | } |
| 1058 | return x + w; |
| 1059 | } |
| 1060 | |
| 1061 | int GMenu2X::drawButtonRight(Surface& s, const string &btn, const string &text, int x, int y) { |
| 1062 | int w = 0; |
| 1063 | auto icon = sc["skin:imgs/buttons/" + btn + ".png"]; |
| 1064 | if (icon) { |
| 1065 | if (y < 0) y = resY + y; |
| 1066 | w = icon->width(); |
| 1067 | icon->blit(s, x - w, y - 7); |
| 1068 | if (!text.empty()) { |
| 1069 | w += 3; |
| 1070 | w += font->write( |
| 1071 | s, text, x - w, y, Font::HAlignRight, Font::VAlignMiddle); |
| 1072 | w += 6; |
| 1073 | } |
| 1074 | } |
| 1075 | return x - w; |
| 1076 | } |
| 1077 | |
| 1078 | void GMenu2X::drawScrollBar(uint pageSize, uint totalSize, uint pagePos) { |
| 1079 | if (totalSize <= pageSize) { |
| 1080 | // Everything fits on one screen, no scroll bar needed. |
| 1081 | return; |
| 1082 | } |
| 1083 | |
| 1084 | unsigned int top, height; |
| 1085 | tie(top, height) = getContentArea(); |
| 1086 | top += 1; |
| 1087 | height -= 2; |
| 1088 | |
| 1089 | s->rectangle(resX - 8, top, 7, height, skinConfColors[COLOR_SELECTION_BG]); |
| 1090 | top += 2; |
| 1091 | height -= 4; |
| 1092 | |
| 1093 | const uint barSize = max(height * pageSize / totalSize, 4u); |
| 1094 | const uint barPos = (height - barSize) * pagePos / (totalSize - pageSize); |
| 1095 | |
| 1096 | s->box(resX - 6, top + barPos, 3, barSize, |
| 1097 | skinConfColors[COLOR_SELECTION_BG]); |
| 1098 | } |
| 1099 | |
| 1100 | void GMenu2X::drawTopBar(Surface& s) { |
| 1101 | Surface *bar = sc.skinRes("imgs/topbar.png", false); |
| 1102 | if (bar) { |
| 1103 | bar->blit(s, 0, 0); |
| 1104 | } else { |
| 1105 | const int h = skinConfInt["topBarHeight"]; |
| 1106 | s.box(0, 0, resX, h, skinConfColors[COLOR_TOP_BAR_BG]); |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | void GMenu2X::drawBottomBar(Surface& s) { |
| 1111 | Surface *bar = sc.skinRes("imgs/bottombar.png", false); |
| 1112 | if (bar) { |
| 1113 | bar->blit(s, 0, resY-bar->height()); |
| 1114 | } else { |
| 1115 | const int h = skinConfInt["bottomBarHeight"]; |
| 1116 | s.box(0, resY - h, resX, h, skinConfColors[COLOR_BOTTOM_BAR_BG]); |
| 1117 | } |
| 1118 | } |
| 1119 |
Branches:
install_locations
master
opkrun
packages
