Root/
Source at commit 3f299f6 created 10 years 7 months ago. By Maarten ter Huurne, Set initial key repeat delay to a more sensible value | |
---|---|
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 "gp2x.h" |
22 | |
23 | #include "background.h" |
24 | #include "cpu.h" |
25 | #include "debug.h" |
26 | #include "filedialog.h" |
27 | #include "filelister.h" |
28 | #include "font.h" |
29 | #include "gmenu2x.h" |
30 | #include "helppopup.h" |
31 | #include "iconbutton.h" |
32 | #include "inputdialog.h" |
33 | #include "linkapp.h" |
34 | #include "mediamonitor.h" |
35 | #include "menu.h" |
36 | #include "menusettingbool.h" |
37 | #include "menusettingdir.h" |
38 | #include "menusettingfile.h" |
39 | #include "menusettingimage.h" |
40 | #include "menusettingint.h" |
41 | #include "menusettingmultistring.h" |
42 | #include "menusettingrgba.h" |
43 | #include "menusettingstring.h" |
44 | #include "messagebox.h" |
45 | #include "powersaver.h" |
46 | #include "settingsdialog.h" |
47 | #include "textdialog.h" |
48 | #include "wallpaperdialog.h" |
49 | #include "utilities.h" |
50 | |
51 | #include <iostream> |
52 | #include <sstream> |
53 | #include <fstream> |
54 | #include <algorithm> |
55 | #include <stdlib.h> |
56 | #include <unistd.h> |
57 | #include <math.h> |
58 | #include <SDL.h> |
59 | #include <SDL_gfxPrimitives.h> |
60 | #include <signal.h> |
61 | |
62 | #include <sys/statvfs.h> |
63 | #include <errno.h> |
64 | |
65 | #include <sys/fcntl.h> //for battery |
66 | |
67 | #ifdef PLATFORM_PANDORA |
68 | //#include <pnd_container.h> |
69 | //#include <pnd_conf.h> |
70 | //#include <pnd_discovery.h> |
71 | #endif |
72 | |
73 | //for browsing the filesystem |
74 | #include <sys/stat.h> |
75 | #include <sys/types.h> |
76 | #include <dirent.h> |
77 | |
78 | //for soundcard |
79 | #include <sys/ioctl.h> |
80 | #include <linux/soundcard.h> |
81 | |
82 | #include <sys/mman.h> |
83 | |
84 | struct MenuOption { |
85 | std::string text; |
86 | function_t action; |
87 | }; |
88 | |
89 | using namespace std; |
90 | |
91 | #ifndef DEFAULT_WALLPAPER_PATH |
92 | #define DEFAULT_WALLPAPER_PATH \ |
93 | GMENU2X_SYSTEM_DIR "/skins/Default/wallpapers/default.png" |
94 | #endif |
95 | |
96 | #ifdef _CARD_ROOT |
97 | const char *CARD_ROOT = _CARD_ROOT; |
98 | #elif defined(PLATFORM_A320) || defined(PLATFORM_GCW0) |
99 | const char *CARD_ROOT = "/media"; |
100 | #else |
101 | const char *CARD_ROOT = "/card"; |
102 | #endif |
103 | |
104 | static GMenu2X *app; |
105 | static string gmenu2x_home; |
106 | |
107 | // Note: Keep this in sync with the enum! |
108 | static const char *colorNames[NUM_COLORS] = { |
109 | "topBarBg", |
110 | "bottomBarBg", |
111 | "selectionBg", |
112 | "messageBoxBg", |
113 | "messageBoxBorder", |
114 | "messageBoxSelection", |
115 | }; |
116 | |
117 | static enum color stringToColor(const string &name) |
118 | { |
119 | for (unsigned int i = 0; i < NUM_COLORS; i++) { |
120 | if (strcmp(colorNames[i], name.c_str()) == 0) { |
121 | return (enum color)i; |
122 | } |
123 | } |
124 | return (enum color)-1; |
125 | } |
126 | |
127 | static const char *colorToString(enum color c) |
128 | { |
129 | return colorNames[c]; |
130 | } |
131 | |
132 | static void quit_all(int err) { |
133 | delete app; |
134 | exit(err); |
135 | } |
136 | |
137 | const string GMenu2X::getHome(void) |
138 | { |
139 | return gmenu2x_home; |
140 | } |
141 | |
142 | static void set_handler(int signal, void (*handler)(int)) |
143 | { |
144 | struct sigaction sig; |
145 | sigaction(signal, NULL, &sig); |
146 | sig.sa_handler = handler; |
147 | sigaction(signal, &sig, NULL); |
148 | } |
149 | |
150 | int main(int /*argc*/, char * /*argv*/[]) { |
151 | INFO("---- GMenu2X starting ----\n"); |
152 | |
153 | set_handler(SIGINT, &quit_all); |
154 | set_handler(SIGSEGV, &quit_all); |
155 | set_handler(SIGTERM, &quit_all); |
156 | |
157 | char *home = getenv("HOME"); |
158 | if (home == NULL) { |
159 | ERROR("Unable to find gmenu2x home directory. The $HOME variable is not defined.\n"); |
160 | return 1; |
161 | } |
162 | |
163 | gmenu2x_home = (string)home + (string)"/.gmenu2x"; |
164 | if (!fileExists(gmenu2x_home) && mkdir(gmenu2x_home.c_str(), 0770) < 0) { |
165 | ERROR("Unable to create gmenu2x home directory.\n"); |
166 | return 1; |
167 | } |
168 | |
169 | DEBUG("Home path: %s.\n", gmenu2x_home.c_str()); |
170 | |
171 | app = new GMenu2X(); |
172 | DEBUG("Starting main()\n"); |
173 | app->main(); |
174 | |
175 | return 0; |
176 | } |
177 | |
178 | #ifdef ENABLE_CPUFREQ |
179 | void GMenu2X::initCPULimits() { |
180 | // Note: These values are for the Dingoo. |
181 | // The NanoNote does not have cpufreq enabled in its kernel and |
182 | // other devices are not actively maintained. |
183 | // TODO: Read min and max from sysfs. |
184 | cpuFreqMin = 30; |
185 | cpuFreqMax = 500; |
186 | cpuFreqSafeMax = 420; |
187 | cpuFreqMenuDefault = 200; |
188 | cpuFreqAppDefault = 384; |
189 | cpuFreqMultiple = 24; |
190 | |
191 | // Round min and max values to the specified multiple. |
192 | cpuFreqMin = ((cpuFreqMin + cpuFreqMultiple - 1) / cpuFreqMultiple) |
193 | * cpuFreqMultiple; |
194 | cpuFreqMax = (cpuFreqMax / cpuFreqMultiple) * cpuFreqMultiple; |
195 | cpuFreqSafeMax = (cpuFreqSafeMax / cpuFreqMultiple) * cpuFreqMultiple; |
196 | cpuFreqMenuDefault = (cpuFreqMenuDefault / cpuFreqMultiple) * cpuFreqMultiple; |
197 | cpuFreqAppDefault = (cpuFreqAppDefault / cpuFreqMultiple) * cpuFreqMultiple; |
198 | } |
199 | #endif |
200 | |
201 | GMenu2X::GMenu2X() |
202 | { |
203 | usbnet = samba = inet = web = false; |
204 | useSelectionPng = false; |
205 | |
206 | #ifdef ENABLE_CPUFREQ |
207 | initCPULimits(); |
208 | #endif |
209 | //load config data |
210 | readConfig(); |
211 | |
212 | halfX = resX/2; |
213 | halfY = resY/2; |
214 | bottomBarIconY = resY-18; |
215 | bottomBarTextY = resY-10; |
216 | |
217 | /* Do not clear the screen on exit. |
218 | * This may require an SDL patch available at |
219 | * https://github.com/mthuurne/opendingux-buildroot/blob |
220 | * /opendingux-2010.11/package/sdl/sdl-fbcon-clear-onexit.patch |
221 | */ |
222 | setenv("SDL_FBCON_DONT_CLEAR", "1", 0); |
223 | |
224 | //Screen |
225 | if( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) { |
226 | ERROR("Could not initialize SDL: %s\n", SDL_GetError()); |
227 | quit(); |
228 | } |
229 | |
230 | s = Surface::openOutputSurface(resX, resY, confInt["videoBpp"]); |
231 | |
232 | bg = NULL; |
233 | font = NULL; |
234 | btnContextMenu = nullptr; |
235 | setSkin(confStr["skin"], !fileExists(confStr["wallpaper"])); |
236 | layers.insert(layers.begin(), make_shared<Background>(*this)); |
237 | initMenu(); |
238 | |
239 | monitor = new MediaMonitor(CARD_ROOT); |
240 | |
241 | if (!fileExists(confStr["wallpaper"])) { |
242 | DEBUG("No wallpaper defined; we will take the default one.\n"); |
243 | confStr["wallpaper"] = DEFAULT_WALLPAPER_PATH; |
244 | } |
245 | |
246 | initBG(); |
247 | |
248 | /* If a user-specified input.conf file exists, we load it; |
249 | * otherwise, we load the default one. */ |
250 | string input_file = getHome() + "/input.conf"; |
251 | if (fileExists(input_file.c_str())) { |
252 | DEBUG("Loading user-specific input.conf file: %s.\n", input_file.c_str()); |
253 | } else { |
254 | input_file = GMENU2X_SYSTEM_DIR "/input.conf"; |
255 | DEBUG("Loading system input.conf file: %s.\n", input_file.c_str()); |
256 | } |
257 | |
258 | input.init(input_file, menu.get()); |
259 | |
260 | if (confInt["backlightTimeout"] > 0) |
261 | PowerSaver::getInstance()->setScreenTimeout( confInt["backlightTimeout"] ); |
262 | |
263 | setInputSpeed(); |
264 | #ifdef ENABLE_CPUFREQ |
265 | setClock(confInt["menuClock"]); |
266 | #endif |
267 | //recover last session |
268 | readTmp(); |
269 | if (lastSelectorElement>-1 && menu->selLinkApp()!=NULL && (!menu->selLinkApp()->getSelectorDir().empty() || !lastSelectorDir.empty())) |
270 | menu->selLinkApp()->selector(lastSelectorElement,lastSelectorDir); |
271 | |
272 | } |
273 | |
274 | GMenu2X::~GMenu2X() { |
275 | if (PowerSaver::isRunning()) |
276 | delete PowerSaver::getInstance(); |
277 | quit(); |
278 | |
279 | delete btnContextMenu; |
280 | delete font; |
281 | delete monitor; |
282 | } |
283 | |
284 | void GMenu2X::quit() { |
285 | fflush(NULL); |
286 | sc.clear(); |
287 | delete s; |
288 | |
289 | SDL_Quit(); |
290 | unsetenv("SDL_FBCON_DONT_CLEAR"); |
291 | } |
292 | |
293 | void GMenu2X::initBG() { |
294 | sc.del("bgmain"); |
295 | |
296 | // Load wallpaper. |
297 | delete bg; |
298 | bg = Surface::loadImage(confStr["wallpaper"]); |
299 | if (!bg) { |
300 | bg = Surface::emptySurface(resX, resY); |
301 | } |
302 | |
303 | drawTopBar(bg); |
304 | drawBottomBar(bg); |
305 | |
306 | Surface *bgmain = new Surface(bg); |
307 | sc.add(bgmain,"bgmain"); |
308 | |
309 | Surface *sd = Surface::loadImage("imgs/sd.png", confStr["skin"]); |
310 | if (sd) sd->blit(bgmain, 3, bottomBarIconY); |
311 | |
312 | #if defined(PLATFORM_A320) || defined(PLATFORM_GCW0) |
313 | string df = getDiskFree("/boot"); |
314 | #else |
315 | string df = getDiskFree(CARD_ROOT); |
316 | #endif |
317 | bgmain->write(font, df, 22, bottomBarTextY, Font::HAlignLeft, Font::VAlignMiddle); |
318 | delete sd; |
319 | |
320 | cpuX = font->getTextWidth(df)+32; |
321 | #ifdef ENABLE_CPUFREQ |
322 | Surface *cpu = Surface::loadImage("imgs/cpu.png", confStr["skin"]); |
323 | if (cpu) cpu->blit(bgmain, cpuX, bottomBarIconY); |
324 | cpuX += 19; |
325 | manualX = cpuX+font->getTextWidth("300MHz")+5; |
326 | delete cpu; |
327 | #else |
328 | manualX = cpuX; |
329 | #endif |
330 | |
331 | int serviceX = resX-38; |
332 | if (usbnet) { |
333 | if (web) { |
334 | Surface *webserver = Surface::loadImage( |
335 | "imgs/webserver.png", confStr["skin"]); |
336 | if (webserver) webserver->blit(bgmain, serviceX, bottomBarIconY); |
337 | serviceX -= 19; |
338 | delete webserver; |
339 | } |
340 | if (samba) { |
341 | Surface *sambaS = Surface::loadImage( |
342 | "imgs/samba.png", confStr["skin"]); |
343 | if (sambaS) sambaS->blit(bgmain, serviceX, bottomBarIconY); |
344 | serviceX -= 19; |
345 | delete sambaS; |
346 | } |
347 | if (inet) { |
348 | Surface *inetS = Surface::loadImage("imgs/inet.png", confStr["skin"]); |
349 | if (inetS) inetS->blit(bgmain, serviceX, bottomBarIconY); |
350 | serviceX -= 19; |
351 | delete inetS; |
352 | } |
353 | } |
354 | |
355 | bgmain->convertToDisplayFormat(); |
356 | } |
357 | |
358 | void GMenu2X::initFont() { |
359 | delete font; |
360 | font = Font::defaultFont(); |
361 | if (!font) { |
362 | ERROR("Cannot function without font; aborting...\n"); |
363 | quit(); |
364 | exit(-1); |
365 | } |
366 | } |
367 | |
368 | void GMenu2X::initMenu() { |
369 | //Menu structure handler |
370 | menu.reset(new Menu(this, ts)); |
371 | for (uint i=0; i<menu->getSections().size(); i++) { |
372 | //Add virtual links in the applications section |
373 | if (menu->getSections()[i]=="applications") { |
374 | menu->addActionLink(i,"Explorer", BIND(&GMenu2X::explorer),tr["Launch an application"],"skin:icons/explorer.png"); |
375 | } |
376 | |
377 | //Add virtual links in the setting section |
378 | else if (menu->getSections()[i]=="settings") { |
379 | menu->addActionLink(i,"GMenu2X",BIND(&GMenu2X::showSettings),tr["Configure GMenu2X's options"],"skin:icons/configure.png"); |
380 | menu->addActionLink(i,tr["Skin"],BIND(&GMenu2X::skinMenu),tr["Configure skin"],"skin:icons/skin.png"); |
381 | menu->addActionLink(i,tr["Wallpaper"],BIND(&GMenu2X::changeWallpaper),tr["Change GMenu2X wallpaper"],"skin:icons/wallpaper.png"); |
382 | if (fileExists(getHome()+"/log.txt")) |
383 | menu->addActionLink(i,tr["Log Viewer"],BIND(&GMenu2X::viewLog),tr["Displays last launched program's output"],"skin:icons/ebook.png"); |
384 | menu->addActionLink(i,tr["About"],BIND(&GMenu2X::about),tr["Info about GMenu2X"],"skin:icons/about.png"); |
385 | } |
386 | } |
387 | |
388 | menu->skinUpdated(); |
389 | |
390 | menu->setSectionIndex(confInt["section"]); |
391 | menu->setLinkIndex(confInt["link"]); |
392 | |
393 | btnContextMenu = new IconButton(this, ts, "skin:imgs/menu.png"); |
394 | btnContextMenu->setPosition(resX - 38, bottomBarIconY); |
395 | btnContextMenu->setAction(BIND(&GMenu2X::contextMenu)); |
396 | |
397 | //DEBUG |
398 | //menu->addLink( CARD_ROOT, "sample.pxml", "applications" ); |
399 | |
400 | layers.push_back(menu); |
401 | } |
402 | |
403 | void GMenu2X::about() { |
404 | vector<string> text; |
405 | string line; |
406 | string fn(GMENU2X_SYSTEM_DIR); |
407 | string build_date("Build date: "); |
408 | fn.append("/about.txt"); |
409 | build_date.append(__DATE__); |
410 | |
411 | ifstream inf(fn.c_str(), ios_base::in); |
412 | |
413 | while(getline(inf, line, '\n')) |
414 | text.push_back(line); |
415 | inf.close(); |
416 | |
417 | TextDialog td(this, "GMenu2X", build_date, "icons/about.png", &text); |
418 | td.exec(); |
419 | } |
420 | |
421 | void GMenu2X::viewLog() { |
422 | string logfile = getHome()+"/log.txt"; |
423 | if (fileExists(logfile)) { |
424 | ifstream inf(logfile.c_str(), ios_base::in); |
425 | if (inf.is_open()) { |
426 | vector<string> log; |
427 | |
428 | string line; |
429 | while (getline(inf, line, '\n')) |
430 | log.push_back(line); |
431 | inf.close(); |
432 | |
433 | TextDialog td(this, tr["Log Viewer"], tr["Displays last launched program's output"], "icons/ebook.png", &log); |
434 | td.exec(); |
435 | |
436 | MessageBox mb(this, tr["Do you want to delete the log file?"], "icons/ebook.png"); |
437 | mb.setButton(InputManager::ACCEPT, tr["Yes"]); |
438 | mb.setButton(InputManager::CANCEL, tr["No"]); |
439 | if (mb.exec() == InputManager::ACCEPT) { |
440 | unlink(logfile.c_str()); |
441 | sync(); |
442 | menu->deleteSelectedLink(); |
443 | } |
444 | } |
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 | if (fileExists(conffile)) { |
458 | ifstream inf(conffile.c_str(), ios_base::in); |
459 | if (inf.is_open()) { |
460 | string line; |
461 | while (getline(inf, line, '\n')) { |
462 | string::size_type pos = line.find("="); |
463 | string name = trim(line.substr(0,pos)); |
464 | string value = trim(line.substr(pos+1,line.length())); |
465 | |
466 | if (value.length()>1 && value.at(0)=='"' && value.at(value.length()-1)=='"') |
467 | confStr[name] = value.substr(1,value.length()-2); |
468 | else |
469 | confInt[name] = atoi(value.c_str()); |
470 | } |
471 | inf.close(); |
472 | } |
473 | } |
474 | if (!confStr["lang"].empty()) |
475 | tr.setLang(confStr["lang"]); |
476 | |
477 | if (!confStr["wallpaper"].empty() && !fileExists(confStr["wallpaper"])) |
478 | confStr["wallpaper"] = ""; |
479 | |
480 | if (confStr["skin"].empty() || SurfaceCollection::getSkinPath(confStr["skin"]).empty()) |
481 | confStr["skin"] = "Default"; |
482 | |
483 | evalIntConf( &confInt["outputLogs"], 0, 0,1 ); |
484 | #ifdef ENABLE_CPUFREQ |
485 | evalIntConf( &confInt["maxClock"], |
486 | cpuFreqSafeMax, cpuFreqMin, cpuFreqMax ); |
487 | evalIntConf( &confInt["menuClock"], |
488 | cpuFreqMenuDefault, cpuFreqMin, cpuFreqSafeMax ); |
489 | #endif |
490 | evalIntConf( &confInt["backlightTimeout"], 15, 0,120 ); |
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 | sync(); |
521 | } |
522 | } |
523 | |
524 | void GMenu2X::writeSkinConfig() { |
525 | string conffile = getHome() + "/skins/"; |
526 | if (!fileExists(conffile)) |
527 | mkdir(conffile.c_str(), 0770); |
528 | conffile = conffile + confStr["skin"]; |
529 | if (!fileExists(conffile)) |
530 | mkdir(conffile.c_str(), 0770); |
531 | conffile = conffile + "/skin.conf"; |
532 | |
533 | ofstream inf(conffile.c_str()); |
534 | if (inf.is_open()) { |
535 | ConfStrHash::iterator endS = skinConfStr.end(); |
536 | for(ConfStrHash::iterator curr = skinConfStr.begin(); curr != endS; curr++) |
537 | inf << curr->first << "=\"" << curr->second << "\"" << endl; |
538 | |
539 | ConfIntHash::iterator endI = skinConfInt.end(); |
540 | for(ConfIntHash::iterator curr = skinConfInt.begin(); curr != endI; curr++) |
541 | inf << curr->first << "=" << curr->second << endl; |
542 | |
543 | int i; |
544 | for (i = 0; i < NUM_COLORS; ++i) { |
545 | inf << colorToString((enum color)i) << "=#"; |
546 | inf.width(2); inf.fill('0'); |
547 | inf << right << hex << skinConfColors[i].r; |
548 | inf.width(2); inf.fill('0'); |
549 | inf << right << hex << skinConfColors[i].g; |
550 | inf.width(2); inf.fill('0'); |
551 | inf << right << hex << skinConfColors[i].b; |
552 | inf.width(2); inf.fill('0'); |
553 | inf << right << hex << skinConfColors[i].a << endl; |
554 | } |
555 | |
556 | inf.close(); |
557 | sync(); |
558 | } |
559 | } |
560 | |
561 | void GMenu2X::readTmp() { |
562 | lastSelectorElement = -1; |
563 | if (fileExists("/tmp/gmenu2x.tmp")) { |
564 | ifstream inf("/tmp/gmenu2x.tmp", ios_base::in); |
565 | if (inf.is_open()) { |
566 | string line; |
567 | string section = ""; |
568 | while (getline(inf, line, '\n')) { |
569 | string::size_type pos = line.find("="); |
570 | string name = trim(line.substr(0,pos)); |
571 | string value = trim(line.substr(pos+1,line.length())); |
572 | |
573 | if (name=="section") |
574 | menu->setSectionIndex(atoi(value.c_str())); |
575 | else if (name=="link") |
576 | menu->setLinkIndex(atoi(value.c_str())); |
577 | else if (name=="selectorelem") |
578 | lastSelectorElement = atoi(value.c_str()); |
579 | else if (name=="selectordir") |
580 | lastSelectorDir = value; |
581 | } |
582 | inf.close(); |
583 | } |
584 | } |
585 | } |
586 | |
587 | void GMenu2X::writeTmp(int selelem, const string &selectordir) { |
588 | string conffile = "/tmp/gmenu2x.tmp"; |
589 | ofstream inf(conffile.c_str()); |
590 | if (inf.is_open()) { |
591 | inf << "section=" << menu->selSectionIndex() << endl; |
592 | inf << "link=" << menu->selLinkIndex() << endl; |
593 | if (selelem>-1) |
594 | inf << "selectorelem=" << selelem << endl; |
595 | if (selectordir!="") |
596 | inf << "selectordir=" << selectordir << endl; |
597 | inf.close(); |
598 | sync(); |
599 | } |
600 | } |
601 | |
602 | void GMenu2X::paint() { |
603 | for (auto layer : layers) { |
604 | layer->paint(*s); |
605 | } |
606 | |
607 | LinkApp *linkApp = menu->selLinkApp(); |
608 | if (linkApp) { |
609 | #ifdef ENABLE_CPUFREQ |
610 | s->write(font, linkApp->clockStr(confInt["maxClock"]), cpuX, bottomBarTextY, Font::HAlignLeft, Font::VAlignMiddle); |
611 | #endif |
612 | //Manual indicator |
613 | if (!linkApp->getManual().empty()) |
614 | sc.skinRes("imgs/manual.png")->blit(s, manualX, bottomBarIconY); |
615 | } |
616 | |
617 | if (ts.available()) { |
618 | btnContextMenu->paint(); |
619 | } |
620 | } |
621 | |
622 | void GMenu2X::main() { |
623 | if (!fileExists(CARD_ROOT)) |
624 | CARD_ROOT = ""; |
625 | |
626 | bool quit = false; |
627 | while (!quit) { |
628 | |
629 | paint(); |
630 | s->flip(); |
631 | |
632 | //touchscreen |
633 | if (ts.available()) { |
634 | ts.poll(); |
635 | btnContextMenu->handleTS(); |
636 | for (auto it = layers.rbegin(); it != layers.rend(); ++it) { |
637 | if ((*it)->handleTouchscreen(ts)) { |
638 | break; |
639 | } |
640 | } |
641 | } |
642 | |
643 | InputManager::Button button = input.waitForPressedButton(); |
644 | for (auto it = layers.rbegin(); it != layers.rend(); ++it) { |
645 | if ((*it)->handleButtonPress(button)) { |
646 | break; |
647 | } |
648 | } |
649 | |
650 | for (auto it = layers.begin(); it != layers.end(); ) { |
651 | auto layer = *it; |
652 | if (layer->wasDismissed()) { |
653 | it = layers.erase(it); |
654 | } else { |
655 | ++it; |
656 | } |
657 | } |
658 | } |
659 | } |
660 | |
661 | void GMenu2X::explorer() { |
662 | FileDialog fd(this, ts, tr["Select an application"], "dge,sh,bin,py,elf,"); |
663 | if (fd.exec()) { |
664 | if (confInt["saveSelection"] && (confInt["section"]!=menu->selSectionIndex() || confInt["link"]!=menu->selLinkIndex())) |
665 | writeConfig(); |
666 | |
667 | string command = cmdclean(fd.getPath()+"/"+fd.getFile()); |
668 | chdir(fd.getPath().c_str()); |
669 | quit(); |
670 | #ifdef ENABLE_CPUFREQ |
671 | setClock(cpuFreqAppDefault); |
672 | #endif |
673 | execlp("/bin/sh","/bin/sh","-c",command.c_str(),NULL); |
674 | |
675 | //if execution continues then something went wrong and as we already called SDL_Quit we cannot continue |
676 | //try relaunching gmenu2x |
677 | ERROR("Error executing selected application, re-launching gmenu2x\n"); |
678 | main(); |
679 | } |
680 | } |
681 | |
682 | void GMenu2X::showHelpPopup() { |
683 | layers.push_back(make_shared<HelpPopup>(*this)); |
684 | } |
685 | |
686 | void GMenu2X::showSettings() { |
687 | #ifdef ENABLE_CPUFREQ |
688 | int curMenuClock = confInt["menuClock"]; |
689 | #endif |
690 | bool showRootFolder = fileExists(CARD_ROOT); |
691 | |
692 | FileLister fl_tr(GMENU2X_SYSTEM_DIR "/translations"); |
693 | fl_tr.browse(); |
694 | |
695 | string tr_path = getHome() + "/translations"; |
696 | if (fileExists(tr_path)) { |
697 | fl_tr.setPath(tr_path, false); |
698 | fl_tr.browse(false); |
699 | } |
700 | |
701 | fl_tr.insertFile("English"); |
702 | string lang = tr.lang(); |
703 | |
704 | vector<string> encodings; |
705 | encodings.push_back("NTSC"); |
706 | encodings.push_back("PAL"); |
707 | |
708 | SettingsDialog sd(this, input, ts, tr["Settings"]); |
709 | sd.addSetting(new MenuSettingMultiString(this, ts, tr["Language"], tr["Set the language used by GMenu2X"], &lang, &fl_tr.getFiles())); |
710 | sd.addSetting(new MenuSettingBool(this, ts, tr["Save last selection"], tr["Save the last selected link and section on exit"], &confInt["saveSelection"])); |
711 | #ifdef ENABLE_CPUFREQ |
712 | sd.addSetting(new MenuSettingInt(this, ts, tr["Clock for GMenu2X"], tr["Set the cpu working frequency when running GMenu2X"], &confInt["menuClock"], cpuFreqMin, cpuFreqSafeMax, cpuFreqMultiple)); |
713 | sd.addSetting(new MenuSettingInt(this, ts, tr["Maximum overclock"], tr["Set the maximum overclock for launching links"], &confInt["maxClock"], cpuFreqMin, cpuFreqMax, cpuFreqMultiple)); |
714 | #endif |
715 | sd.addSetting(new MenuSettingBool(this, ts, tr["Output logs"], tr["Logs the output of the links. Use the Log Viewer to read them."], &confInt["outputLogs"])); |
716 | sd.addSetting(new MenuSettingInt(this, ts, tr["Screen Timeout"], tr["Set screen's backlight timeout in seconds"], &confInt["backlightTimeout"], 0, 120)); |
717 | // sd.addSetting(new MenuSettingMultiString(this, ts, tr["Tv-Out encoding"], tr["Encoding of the tv-out signal"], &confStr["tvoutEncoding"], &encodings)); |
718 | sd.addSetting(new MenuSettingBool(this, ts, tr["Show root"], tr["Show root folder in the file selection dialogs"], &showRootFolder)); |
719 | |
720 | if (sd.exec() && sd.edited()) { |
721 | #ifdef ENABLE_CPUFREQ |
722 | if (curMenuClock != confInt["menuClock"]) setClock(confInt["menuClock"]); |
723 | #endif |
724 | |
725 | if (confInt["backlightTimeout"] == 0) { |
726 | if (PowerSaver::isRunning()) |
727 | delete PowerSaver::getInstance(); |
728 | } else { |
729 | PowerSaver::getInstance()->setScreenTimeout( confInt["backlightTimeout"] ); |
730 | } |
731 | |
732 | if (lang == "English") lang = ""; |
733 | if (lang != tr.lang()) { |
734 | tr.setLang(lang); |
735 | confStr["lang"] = lang; |
736 | } |
737 | /*if (fileExists(CARD_ROOT) && !showRootFolder) |
738 | unlink(CARD_ROOT); |
739 | else if (!fileExists(CARD_ROOT) && showRootFolder) |
740 | symlink("/", CARD_ROOT);*/ |
741 | //WARNING: the above might be dangerous with CARD_ROOT set to / |
742 | writeConfig(); |
743 | } |
744 | } |
745 | |
746 | void GMenu2X::skinMenu() { |
747 | FileLister fl_sk(getHome() + "/skins", true, false); |
748 | fl_sk.addExclude(".."); |
749 | fl_sk.browse(); |
750 | fl_sk.setPath(GMENU2X_SYSTEM_DIR "/skins", false); |
751 | fl_sk.browse(false); |
752 | |
753 | string curSkin = confStr["skin"]; |
754 | |
755 | SettingsDialog sd(this, input, ts, tr["Skin"]); |
756 | sd.addSetting(new MenuSettingMultiString(this, ts, tr["Skin"], tr["Set the skin used by GMenu2X"], &confStr["skin"], &fl_sk.getDirectories())); |
757 | sd.addSetting(new MenuSettingRGBA(this, ts, tr["Top Bar Color"], tr["Color of the top bar"], &skinConfColors[COLOR_TOP_BAR_BG])); |
758 | sd.addSetting(new MenuSettingRGBA(this, ts, tr["Bottom Bar Color"], tr["Color of the bottom bar"], &skinConfColors[COLOR_BOTTOM_BAR_BG])); |
759 | sd.addSetting(new MenuSettingRGBA(this, ts, tr["Selection Color"], tr["Color of the selection and other interface details"], &skinConfColors[COLOR_SELECTION_BG])); |
760 | sd.addSetting(new MenuSettingRGBA(this, ts, tr["Message Box Color"], tr["Background color of the message box"], &skinConfColors[COLOR_MESSAGE_BOX_BG])); |
761 | sd.addSetting(new MenuSettingRGBA(this, ts, tr["Message Box Border Color"], tr["Border color of the message box"], &skinConfColors[COLOR_MESSAGE_BOX_BORDER])); |
762 | sd.addSetting(new MenuSettingRGBA(this, ts, tr["Message Box Selection Color"], tr["Color of the selection of the message box"], &skinConfColors[COLOR_MESSAGE_BOX_SELECTION])); |
763 | |
764 | if (sd.exec() && sd.edited()) { |
765 | if (curSkin != confStr["skin"]) { |
766 | setSkin(confStr["skin"]); |
767 | writeConfig(); |
768 | } |
769 | writeSkinConfig(); |
770 | initBG(); |
771 | } |
772 | } |
773 | |
774 | void GMenu2X::setSkin(const string &skin, bool setWallpaper) { |
775 | confStr["skin"] = skin; |
776 | |
777 | //Clear previous skin settings |
778 | skinConfStr.clear(); |
779 | skinConfInt.clear(); |
780 | |
781 | DEBUG("GMenu2X: setting new skin %s.\n", skin.c_str()); |
782 | |
783 | //clear collection and change the skin path |
784 | sc.clear(); |
785 | sc.setSkin(skin); |
786 | |
787 | //reset colors to the default values |
788 | skinConfColors[COLOR_TOP_BAR_BG] = (RGBAColor){255,255,255,130}; |
789 | skinConfColors[COLOR_BOTTOM_BAR_BG] = (RGBAColor){255,255,255,130}; |
790 | skinConfColors[COLOR_SELECTION_BG] = (RGBAColor){255,255,255,130}; |
791 | skinConfColors[COLOR_MESSAGE_BOX_BG] = (RGBAColor){255,255,255,255}; |
792 | skinConfColors[COLOR_MESSAGE_BOX_BORDER] = (RGBAColor){80,80,80,255}; |
793 | skinConfColors[COLOR_MESSAGE_BOX_SELECTION] = (RGBAColor){160,160,160,255}; |
794 | |
795 | /* Load skin settings from user directory if present, |
796 | * or from the system directory. */ |
797 | string skinconfname = getHome() + "/skins/" + skin + "/skin.conf"; |
798 | if (!fileExists(skinconfname)) |
799 | skinconfname = GMENU2X_SYSTEM_DIR "/skins/" + skin + "/skin.conf"; |
800 | |
801 | if (fileExists(skinconfname)) { |
802 | ifstream skinconf(skinconfname.c_str(), ios_base::in); |
803 | if (skinconf.is_open()) { |
804 | string line; |
805 | while (getline(skinconf, line, '\n')) { |
806 | line = trim(line); |
807 | DEBUG("skinconf: '%s'\n", line.c_str()); |
808 | string::size_type pos = line.find("="); |
809 | string name = trim(line.substr(0,pos)); |
810 | string value = trim(line.substr(pos+1,line.length())); |
811 | |
812 | if (value.length()>0) { |
813 | if (value.length()>1 && value.at(0)=='"' && value.at(value.length()-1)=='"') |
814 | skinConfStr[name] = value.substr(1,value.length()-2); |
815 | else if (value.at(0) == '#') |
816 | skinConfColors[stringToColor(name)] = strtorgba( value.substr(1,value.length()) ); |
817 | else |
818 | skinConfInt[name] = atoi(value.c_str()); |
819 | } |
820 | } |
821 | skinconf.close(); |
822 | |
823 | if (setWallpaper && !skinConfStr["wallpaper"].empty()) { |
824 | string fp = sc.getSkinFilePath("wallpapers/" + skinConfStr["wallpaper"]); |
825 | if (!fp.empty()) |
826 | confStr["wallpaper"] = fp; |
827 | else |
828 | WARNING("Unable to find wallpaper defined on skin %s\n", skin.c_str()); |
829 | } |
830 | } |
831 | } |
832 | |
833 | evalIntConf( &skinConfInt["topBarHeight"], 40, 32,120 ); |
834 | evalIntConf( &skinConfInt["linkHeight"], 40, 32,120 ); |
835 | evalIntConf( &skinConfInt["linkWidth"], 60, 32,120 ); |
836 | |
837 | if (menu != NULL) menu->skinUpdated(); |
838 | |
839 | //Selection png |
840 | useSelectionPng = sc.addSkinRes("imgs/selection.png", false) != NULL; |
841 | |
842 | //font |
843 | initFont(); |
844 | } |
845 | |
846 | void GMenu2X::showManual() { |
847 | menu->selLinkApp()->showManual(); |
848 | } |
849 | |
850 | void GMenu2X::contextMenu() { |
851 | vector<MenuOption> voices; |
852 | { |
853 | MenuOption opt = {tr.translate("Add link in $1",menu->selSection().c_str(),NULL), BIND(&GMenu2X::addLink)}; |
854 | voices.push_back(opt); |
855 | } |
856 | |
857 | { |
858 | LinkApp* app = menu->selLinkApp(); |
859 | if (app && !app->getManual().empty()) { |
860 | MenuOption opt = {tr.translate("Show manual of $1",menu->selLink()->getTitle().c_str(),NULL), |
861 | BIND(&GMenu2X::showManual), |
862 | }; |
863 | voices.push_back(opt); |
864 | } |
865 | } |
866 | |
867 | if (menu->selLinkApp()!=NULL && menu->selLinkApp()->isEditable()) { |
868 | |
869 | /* FIXME(percuei): this permits to mask the "Edit link" entry |
870 | * on the contextual menu in case CPUFREQ support is |
871 | * not compiled in and the link corresponds to an OPK. |
872 | * This is not a good idea as it'll break things if |
873 | * a new config option is added to the contextual menu. */ |
874 | #if defined(HAVE_LIBOPK) && !defined(ENABLE_CPUFREQ) |
875 | if (!menu->selLinkApp()->isOpk() || |
876 | !menu->selLinkApp()->getSelectorDir().empty()) |
877 | #endif |
878 | { |
879 | MenuOption opt = {tr.translate("Edit $1",menu->selLink()->getTitle().c_str(),NULL), BIND(&GMenu2X::editLink)}; |
880 | voices.push_back(opt); |
881 | } |
882 | #ifdef HAVE_LIBOPK |
883 | if (!menu->selLinkApp()->isOpk()) |
884 | #endif |
885 | { |
886 | MenuOption opt = {tr.translate("Delete $1 link",menu->selLink()->getTitle().c_str(),NULL), BIND(&GMenu2X::deleteLink)}; |
887 | voices.push_back(opt); |
888 | } |
889 | } |
890 | |
891 | { |
892 | MenuOption opt = {tr["Add section"], BIND(&GMenu2X::addSection)}; |
893 | voices.push_back(opt); |
894 | }{ |
895 | MenuOption opt = {tr["Rename section"], BIND(&GMenu2X::renameSection)}; |
896 | voices.push_back(opt); |
897 | }{ |
898 | MenuOption opt = {tr["Delete section"], BIND(&GMenu2X::deleteSection)}; |
899 | voices.push_back(opt); |
900 | }{ |
901 | MenuOption opt = {tr["Scan for applications and games"], BIND(&GMenu2X::scanner)}; |
902 | voices.push_back(opt); |
903 | } |
904 | |
905 | bool close = false; |
906 | uint i, fadeAlpha=0; |
907 | int sel = 0; |
908 | |
909 | int h = font->getHeight(); |
910 | SDL_Rect box; |
911 | box.h = (h+2)*voices.size()+8; |
912 | box.w = 0; |
913 | for (i=0; i<voices.size(); i++) { |
914 | int w = font->getTextWidth(voices[i].text); |
915 | if (w>box.w) box.w = w; |
916 | } |
917 | box.w += 23; |
918 | box.x = halfX - box.w/2; |
919 | box.y = halfY - box.h/2; |
920 | |
921 | SDL_Rect selbox = { |
922 | static_cast<Sint16>(box.x + 4), |
923 | 0, |
924 | static_cast<Uint16>(box.w - 8), |
925 | static_cast<Uint16>(h + 2) |
926 | }; |
927 | long tickNow, tickStart = SDL_GetTicks(); |
928 | |
929 | Surface bg(s); |
930 | /*//Darken background |
931 | bg.box(0, 0, resX, resY, 0,0,0,150); |
932 | bg.box(box.x, box.y, box.w, box.h, skinConfColors["messageBoxBg"]); |
933 | bg.rectangle( box.x+2, box.y+2, box.w-4, box.h-4, skinConfColors["messageBoxBorder"] );*/ |
934 | |
935 | InputManager::ButtonEvent event; |
936 | while (!close) { |
937 | tickNow = SDL_GetTicks(); |
938 | |
939 | selbox.y = box.y+4+(h+2)*sel; |
940 | bg.blit(s,0,0); |
941 | |
942 | if (fadeAlpha<200) fadeAlpha = intTransition(0,200,tickStart,500,tickNow); |
943 | s->box(0, 0, resX, resY, 0,0,0,fadeAlpha); |
944 | s->box(box.x, box.y, box.w, box.h, skinConfColors[COLOR_MESSAGE_BOX_BG]); |
945 | s->rectangle( box.x+2, box.y+2, box.w-4, box.h-4, skinConfColors[COLOR_MESSAGE_BOX_BORDER] ); |
946 | |
947 | |
948 | //draw selection rect |
949 | s->box( selbox.x, selbox.y, selbox.w, selbox.h, skinConfColors[COLOR_MESSAGE_BOX_SELECTION] ); |
950 | for (i=0; i<voices.size(); i++) |
951 | s->write( font, voices[i].text, box.x+12, box.y+5+(h+2)*i, Font::HAlignLeft, Font::VAlignTop ); |
952 | s->flip(); |
953 | |
954 | //touchscreen |
955 | if (ts.available()) { |
956 | ts.poll(); |
957 | if (ts.released()) { |
958 | if (!ts.inRect(box)) |
959 | close = true; |
960 | else if (ts.getX() >= selbox.x |
961 | && ts.getX() <= selbox.x + selbox.w) |
962 | for (i=0; i<voices.size(); i++) { |
963 | selbox.y = box.y+4+(h+2)*i; |
964 | if (ts.getY() >= selbox.y |
965 | && ts.getY() <= selbox.y + selbox.h) { |
966 | voices[i].action(); |
967 | close = true; |
968 | i = voices.size(); |
969 | } |
970 | } |
971 | } else if (ts.pressed() && ts.inRect(box)) { |
972 | for (i=0; i<voices.size(); i++) { |
973 | selbox.y = box.y+4+(h+2)*i; |
974 | if (ts.getY() >= selbox.y |
975 | && ts.getY() <= selbox.y + selbox.h) { |
976 | sel = i; |
977 | i = voices.size(); |
978 | } |
979 | } |
980 | } |
981 | } |
982 | |
983 | |
984 | if (fadeAlpha < 200) { |
985 | if (!input.pollEvent(&event) || event.state != InputManager::PRESSED) continue; |
986 | } else { |
987 | event.button = input.waitForPressedButton(); |
988 | } |
989 | |
990 | switch(event.button) { |
991 | case InputManager::MENU: |
992 | close = true; |
993 | break; |
994 | case InputManager::UP: |
995 | sel = std::max(0, sel-1); |
996 | break; |
997 | case InputManager::DOWN: |
998 | sel = std::min((int)voices.size()-1, sel+1); |
999 | break; |
1000 | case InputManager::ACCEPT: |
1001 | voices[sel].action(); |
1002 | close = true; |
1003 | break; |
1004 | default: |
1005 | break; |
1006 | } |
1007 | } |
1008 | } |
1009 | |
1010 | void GMenu2X::changeWallpaper() { |
1011 | WallpaperDialog wp(this, ts); |
1012 | if (wp.exec() && confStr["wallpaper"] != wp.wallpaper) { |
1013 | confStr["wallpaper"] = wp.wallpaper; |
1014 | initBG(); |
1015 | writeConfig(); |
1016 | } |
1017 | } |
1018 | |
1019 | void GMenu2X::addLink() { |
1020 | FileDialog fd(this, ts, tr["Select an application"], "dge,sh,bin,py,elf,"); |
1021 | if (fd.exec()) { |
1022 | menu->addLink(fd.getPath(), fd.getFile()); |
1023 | sync(); |
1024 | } |
1025 | } |
1026 | |
1027 | void GMenu2X::editLink() { |
1028 | if (menu->selLinkApp()==NULL) return; |
1029 | |
1030 | vector<string> pathV; |
1031 | split(pathV,menu->selLinkApp()->getFile(),"/"); |
1032 | string oldSection = ""; |
1033 | if (pathV.size()>1) |
1034 | oldSection = pathV[pathV.size()-2]; |
1035 | string newSection = oldSection; |
1036 | |
1037 | string linkTitle = menu->selLinkApp()->getTitle(); |
1038 | string linkDescription = menu->selLinkApp()->getDescription(); |
1039 | string linkIcon = menu->selLinkApp()->getIcon(); |
1040 | string linkManual = menu->selLinkApp()->getManual(); |
1041 | string linkSelFilter = menu->selLinkApp()->getSelectorFilter(); |
1042 | string linkSelDir = menu->selLinkApp()->getSelectorDir(); |
1043 | bool linkSelBrowser = menu->selLinkApp()->getSelectorBrowser(); |
1044 | string linkSelScreens = menu->selLinkApp()->getSelectorScreens(); |
1045 | string linkSelAliases = menu->selLinkApp()->getAliasFile(); |
1046 | int linkClock = menu->selLinkApp()->clock(); |
1047 | |
1048 | string diagTitle = tr.translate("Edit link: $1",linkTitle.c_str(),NULL); |
1049 | string diagIcon = menu->selLinkApp()->getIconPath(); |
1050 | |
1051 | SettingsDialog sd(this, input, ts, diagTitle, diagIcon); |
1052 | #ifdef HAVE_LIBOPK |
1053 | if (!menu->selLinkApp()->isOpk()) { |
1054 | #endif |
1055 | sd.addSetting(new MenuSettingString(this, ts, tr["Title"], tr["Link title"], &linkTitle, diagTitle, diagIcon)); |
1056 | sd.addSetting(new MenuSettingString(this, ts, tr["Description"], tr["Link description"], &linkDescription, diagTitle, diagIcon)); |
1057 | sd.addSetting(new MenuSettingMultiString(this, ts, tr["Section"], tr["The section this link belongs to"], &newSection, &menu->getSections())); |
1058 | sd.addSetting(new MenuSettingImage(this, ts, tr["Icon"], |
1059 | tr.translate("Select an icon for the link: $1", |
1060 | linkTitle.c_str(), NULL), &linkIcon, "png")); |
1061 | sd.addSetting(new MenuSettingFile(this, ts, tr["Manual"], |
1062 | tr["Select a graphic/textual manual or a readme"], |
1063 | &linkManual, "man.png,txt")); |
1064 | #ifdef HAVE_LIBOPK |
1065 | } |
1066 | if (!menu->selLinkApp()->isOpk() || |
1067 | !menu->selLinkApp()->getSelectorDir().empty()) { |
1068 | #endif |
1069 | sd.addSetting(new MenuSettingDir(this, ts, tr["Selector Directory"], tr["Directory to scan for the selector"], &linkSelDir)); |
1070 | sd.addSetting(new MenuSettingBool(this, ts, tr["Selector Browser"], tr["Allow the selector to change directory"], &linkSelBrowser)); |
1071 | #ifdef HAVE_LIBOPK |
1072 | } |
1073 | #endif |
1074 | #ifdef ENABLE_CPUFREQ |
1075 | sd.addSetting(new MenuSettingInt(this, ts, tr["Clock frequency"], tr["Cpu clock frequency to set when launching this link"], &linkClock, cpuFreqMin, confInt["maxClock"], cpuFreqMultiple)); |
1076 | #endif |
1077 | #ifdef HAVE_LIBOPK |
1078 | if (!menu->selLinkApp()->isOpk()) { |
1079 | #endif |
1080 | sd.addSetting(new MenuSettingString(this, ts, tr["Selector Filter"], tr["Selector filter (Separate values with a comma)"], &linkSelFilter, diagTitle, diagIcon)); |
1081 | sd.addSetting(new MenuSettingDir(this, ts, tr["Selector Screenshots"], tr["Directory of the screenshots for the selector"], &linkSelScreens)); |
1082 | sd.addSetting(new MenuSettingFile(this, ts, tr["Selector Aliases"], tr["File containing a list of aliases for the selector"], &linkSelAliases)); |
1083 | sd.addSetting(new MenuSettingBool(this, ts, tr["Don't Leave"], tr["Don't quit GMenu2X when launching this link"], &menu->selLinkApp()->runsInBackgroundRef())); |
1084 | #if defined(PLATFORM_A320) || defined(PLATFORM_GCW0) |
1085 | sd.addSetting(new MenuSettingBool(this, ts, tr["Display Console"], tr["Must be enabled for console-based applications"], &menu->selLinkApp()->consoleApp)); |
1086 | #endif |
1087 | #ifdef HAVE_LIBOPK |
1088 | } |
1089 | #endif |
1090 | |
1091 | if (sd.exec() && sd.edited()) { |
1092 | menu->selLinkApp()->setTitle(linkTitle); |
1093 | menu->selLinkApp()->setDescription(linkDescription); |
1094 | menu->selLinkApp()->setIcon(linkIcon); |
1095 | menu->selLinkApp()->setManual(linkManual); |
1096 | menu->selLinkApp()->setSelectorFilter(linkSelFilter); |
1097 | menu->selLinkApp()->setSelectorDir(linkSelDir); |
1098 | menu->selLinkApp()->setSelectorBrowser(linkSelBrowser); |
1099 | menu->selLinkApp()->setSelectorScreens(linkSelScreens); |
1100 | menu->selLinkApp()->setAliasFile(linkSelAliases); |
1101 | menu->selLinkApp()->setClock(linkClock); |
1102 | |
1103 | INFO("New Section: '%s'\n", newSection.c_str()); |
1104 | |
1105 | //if section changed move file and update link->file |
1106 | if (oldSection!=newSection) { |
1107 | vector<string>::const_iterator newSectionIndex = find(menu->getSections().begin(),menu->getSections().end(),newSection); |
1108 | if (newSectionIndex==menu->getSections().end()) return; |
1109 | string newFileName = "sections/"+newSection+"/"+linkTitle; |
1110 | uint x=2; |
1111 | while (fileExists(newFileName)) { |
1112 | string id = ""; |
1113 | stringstream ss; ss << x; ss >> id; |
1114 | newFileName = "sections/"+newSection+"/"+linkTitle+id; |
1115 | x++; |
1116 | } |
1117 | rename(menu->selLinkApp()->getFile().c_str(),newFileName.c_str()); |
1118 | menu->selLinkApp()->renameFile(newFileName); |
1119 | |
1120 | INFO("New section index: %i.\n", newSectionIndex - menu->getSections().begin()); |
1121 | |
1122 | menu->linkChangeSection(menu->selLinkIndex(), menu->selSectionIndex(), newSectionIndex - menu->getSections().begin()); |
1123 | } |
1124 | menu->selLinkApp()->save(); |
1125 | sync(); |
1126 | } |
1127 | } |
1128 | |
1129 | void GMenu2X::deleteLink() { |
1130 | if (menu->selLinkApp()!=NULL) { |
1131 | MessageBox mb(this, tr.translate("Deleting $1",menu->selLink()->getTitle().c_str(),NULL)+"\n"+tr["Are you sure?"], menu->selLink()->getIconPath()); |
1132 | mb.setButton(InputManager::ACCEPT, tr["Yes"]); |
1133 | mb.setButton(InputManager::CANCEL, tr["No"]); |
1134 | if (mb.exec() == InputManager::ACCEPT) { |
1135 | menu->deleteSelectedLink(); |
1136 | sync(); |
1137 | } |
1138 | } |
1139 | } |
1140 | |
1141 | void GMenu2X::addSection() { |
1142 | InputDialog id(this, input, ts, tr["Insert a name for the new section"]); |
1143 | if (id.exec()) { |
1144 | //only if a section with the same name does not exist |
1145 | if (find(menu->getSections().begin(), menu->getSections().end(), id.getInput()) |
1146 | == menu->getSections().end()) { |
1147 | //section directory doesn't exists |
1148 | if (menu->addSection(id.getInput())) { |
1149 | menu->setSectionIndex( menu->getSections().size()-1 ); //switch to the new section |
1150 | sync(); |
1151 | } |
1152 | } |
1153 | } |
1154 | } |
1155 | |
1156 | void GMenu2X::renameSection() { |
1157 | InputDialog id(this, input, ts, tr["Insert a new name for this section"],menu->selSection()); |
1158 | if (id.exec()) { |
1159 | //only if a section with the same name does not exist & !samename |
1160 | if (menu->selSection() != id.getInput() |
1161 | && find(menu->getSections().begin(),menu->getSections().end(), id.getInput()) |
1162 | == menu->getSections().end()) { |
1163 | //section directory doesn't exists |
1164 | string newsectiondir = getHome() + "/sections/" + id.getInput(); |
1165 | string sectiondir = getHome() + "/sections/" + menu->selSection(); |
1166 | |
1167 | if (!rename(sectiondir.c_str(), newsectiondir.c_str())) { |
1168 | string oldpng = menu->selSection() + ".png"; |
1169 | string newpng = id.getInput() + ".png"; |
1170 | string oldicon = sc.getSkinFilePath(oldpng); |
1171 | string newicon = sc.getSkinFilePath(newpng); |
1172 | |
1173 | if (!oldicon.empty() && newicon.empty()) { |
1174 | newicon = oldicon; |
1175 | newicon.replace(newicon.find(oldpng), oldpng.length(), newpng); |
1176 | |
1177 | if (!fileExists(newicon)) { |
1178 | rename(oldicon.c_str(), newicon.c_str()); |
1179 | sc.move("skin:"+oldpng, "skin:"+newpng); |
1180 | } |
1181 | } |
1182 | menu->renameSection(menu->selSectionIndex(), id.getInput()); |
1183 | sync(); |
1184 | } |
1185 | } |
1186 | } |
1187 | } |
1188 | |
1189 | void GMenu2X::deleteSection() { |
1190 | MessageBox mb(this,tr["You will lose all the links in this section."]+"\n"+tr["Are you sure?"]); |
1191 | mb.setButton(InputManager::ACCEPT, tr["Yes"]); |
1192 | mb.setButton(InputManager::CANCEL, tr["No"]); |
1193 | if (mb.exec() == InputManager::ACCEPT) { |
1194 | |
1195 | if (rmtree(getHome() + "/sections/" + menu->selSection())) { |
1196 | menu->deleteSelectedSection(); |
1197 | sync(); |
1198 | } |
1199 | } |
1200 | } |
1201 | |
1202 | void GMenu2X::scanner() { |
1203 | Surface scanbg(bg); |
1204 | drawButton(&scanbg, "cancel", tr["Exit"], |
1205 | drawButton(&scanbg, "accept", "", 5)-10); |
1206 | scanbg.write(font,tr["Link Scanner"],halfX,7,Font::HAlignCenter,Font::VAlignMiddle); |
1207 | scanbg.convertToDisplayFormat(); |
1208 | |
1209 | uint lineY = 42; |
1210 | |
1211 | #ifdef PLATFORM_PANDORA |
1212 | //char *configpath = pnd_conf_query_searchpath(); |
1213 | #else |
1214 | #ifdef ENABLE_CPUFREQ |
1215 | setSafeMaxClock(); |
1216 | #endif |
1217 | |
1218 | scanbg.write(font,tr["Scanning filesystem..."],5,lineY); |
1219 | scanbg.blit(s,0,0); |
1220 | s->flip(); |
1221 | lineY += 26; |
1222 | |
1223 | vector<string> files; |
1224 | scanPath(CARD_ROOT, &files); |
1225 | |
1226 | stringstream ss; |
1227 | ss << files.size(); |
1228 | string str = ""; |
1229 | ss >> str; |
1230 | scanbg.write(font,tr.translate("$1 files found.",str.c_str(),NULL),5,lineY); |
1231 | lineY += 26; |
1232 | scanbg.write(font,tr["Creating links..."],5,lineY); |
1233 | scanbg.blit(s,0,0); |
1234 | s->flip(); |
1235 | lineY += 26; |
1236 | |
1237 | string path, file; |
1238 | string::size_type pos; |
1239 | uint linkCount = 0; |
1240 | |
1241 | for (uint i = 0; i<files.size(); i++) { |
1242 | pos = files[i].rfind("/"); |
1243 | if (pos!=string::npos && pos>0) { |
1244 | path = files[i].substr(0, pos+1); |
1245 | file = files[i].substr(pos+1, files[i].length()); |
1246 | if (menu->addLink(path,file,"found "+file.substr(file.length()-3,3))) |
1247 | linkCount++; |
1248 | } |
1249 | } |
1250 | |
1251 | ss.clear(); |
1252 | ss << linkCount; |
1253 | ss >> str; |
1254 | scanbg.write(font,tr.translate("$1 links created.",str.c_str(),NULL),5,lineY); |
1255 | scanbg.blit(s,0,0); |
1256 | s->flip(); |
1257 | lineY += 26; |
1258 | |
1259 | #ifdef ENABLE_CPUFREQ |
1260 | setMenuClock(); |
1261 | #endif |
1262 | sync(); |
1263 | #endif |
1264 | |
1265 | InputManager::Button button; |
1266 | do { |
1267 | button = input.waitForPressedButton(); |
1268 | } while ((button != InputManager::SETTINGS) |
1269 | && (button != InputManager::ACCEPT) |
1270 | && (button != InputManager::CANCEL)); |
1271 | } |
1272 | |
1273 | void GMenu2X::scanPath(string path, vector<string> *files) { |
1274 | DIR *dirp; |
1275 | struct stat st; |
1276 | struct dirent *dptr; |
1277 | string filepath, ext; |
1278 | |
1279 | if (path[path.length()-1]!='/') path += "/"; |
1280 | if ((dirp = opendir(path.c_str())) == NULL) return; |
1281 | |
1282 | while ((dptr = readdir(dirp))) { |
1283 | if (dptr->d_name[0]=='.') |
1284 | continue; |
1285 | filepath = path+dptr->d_name; |
1286 | int statRet = stat(filepath.c_str(), &st); |
1287 | if (S_ISDIR(st.st_mode)) |
1288 | scanPath(filepath, files); |
1289 | if (statRet != -1) { |
1290 | ext = filepath.substr(filepath.length()-4,4); |
1291 | #if defined(PLATFORM_A320) || defined(PLATFORM_GCW0) || defined(PLATFORM_NANONOTE) |
1292 | if (ext==".dge") |
1293 | #else |
1294 | if (ext==".pxml") |
1295 | #endif |
1296 | files->push_back(filepath); |
1297 | } |
1298 | } |
1299 | |
1300 | closedir(dirp); |
1301 | } |
1302 | |
1303 | typedef struct { |
1304 | unsigned short batt; |
1305 | unsigned short remocon; |
1306 | } MMSP2ADC; |
1307 | |
1308 | void GMenu2X::setInputSpeed() { |
1309 | SDL_EnableKeyRepeat(250, 150); |
1310 | } |
1311 | |
1312 | #ifdef ENABLE_CPUFREQ |
1313 | void GMenu2X::setClock(unsigned mhz) { |
1314 | mhz = constrain(mhz, cpuFreqMin, confInt["maxClock"]); |
1315 | #if defined(PLATFORM_A320) || defined(PLATFORM_GCW0) || defined(PLATFORM_NANONOTE) |
1316 | jz_cpuspeed(mhz); |
1317 | #endif |
1318 | } |
1319 | #endif |
1320 | |
1321 | string GMenu2X::getDiskFree(const char *path) { |
1322 | string df = ""; |
1323 | struct statvfs b; |
1324 | |
1325 | int ret = statvfs(path, &b); |
1326 | if (ret == 0) { |
1327 | // Make sure that the multiplication happens in 64 bits. |
1328 | unsigned long freeMiB = |
1329 | ((unsigned long long)b.f_bfree * b.f_bsize) / (1024 * 1024); |
1330 | unsigned long totalMiB = |
1331 | ((unsigned long long)b.f_blocks * b.f_frsize) / (1024 * 1024); |
1332 | stringstream ss; |
1333 | if (totalMiB >= 10000) { |
1334 | ss << (freeMiB / 1024) << "." << ((freeMiB % 1024) * 10) / 1024 << "/" |
1335 | << (totalMiB / 1024) << "." << ((totalMiB % 1024) * 10) / 1024 << "GiB"; |
1336 | } else { |
1337 | ss << freeMiB << "/" << totalMiB << "MiB"; |
1338 | } |
1339 | ss >> df; |
1340 | } else WARNING("statvfs failed with error '%s'.\n", strerror(errno)); |
1341 | return df; |
1342 | } |
1343 | |
1344 | int GMenu2X::drawButton(Button *btn, int x, int y) { |
1345 | if (y<0) y = resY+y; |
1346 | btn->setPosition(x, y-7); |
1347 | btn->paint(); |
1348 | return x+btn->getRect().w+6; |
1349 | } |
1350 | |
1351 | int GMenu2X::drawButton(Surface *s, const string &btn, const string &text, int x, int y) { |
1352 | if (y<0) y = resY+y; |
1353 | SDL_Rect re = { static_cast<Sint16>(x), static_cast<Sint16>(y - 7), 0, 16 }; |
1354 | if (sc.skinRes("imgs/buttons/"+btn+".png") != NULL) { |
1355 | sc["imgs/buttons/"+btn+".png"]->blit(s, x, y-7); |
1356 | re.w = sc["imgs/buttons/"+btn+".png"]->width() + 3; |
1357 | s->write(font, text, x+re.w, y, Font::HAlignLeft, Font::VAlignMiddle); |
1358 | re.w += font->getTextWidth(text); |
1359 | } |
1360 | return x+re.w+6; |
1361 | } |
1362 | |
1363 | int GMenu2X::drawButtonRight(Surface *s, const string &btn, const string &text, int x, int y) { |
1364 | if (y<0) y = resY+y; |
1365 | if (sc.skinRes("imgs/buttons/"+btn+".png") != NULL) { |
1366 | x -= 16; |
1367 | sc["imgs/buttons/"+btn+".png"]->blit(s, x, y-7); |
1368 | x -= 3; |
1369 | s->write(font, text, x, y, Font::HAlignRight, Font::VAlignMiddle); |
1370 | return x-6-font->getTextWidth(text); |
1371 | } |
1372 | return x-6; |
1373 | } |
1374 | |
1375 | void GMenu2X::drawScrollBar(uint pagesize, uint totalsize, uint pagepos, uint top, uint height) { |
1376 | if (totalsize<=pagesize) return; |
1377 | |
1378 | s->rectangle(resX-8, top, 7, height, skinConfColors[COLOR_SELECTION_BG]); |
1379 | |
1380 | //internal bar total height = height-2 |
1381 | //bar size |
1382 | uint bs = (height-2) * pagesize / totalsize; |
1383 | //bar y position |
1384 | uint by = (height-2) * pagepos / totalsize; |
1385 | by = top+2+by; |
1386 | if (by+bs>top+height-2) by = top+height-2-bs; |
1387 | |
1388 | |
1389 | s->box(resX-6, by, 3, bs, skinConfColors[COLOR_SELECTION_BG]); |
1390 | } |
1391 | |
1392 | void GMenu2X::drawTopBar(Surface *s) { |
1393 | if (s==NULL) s = this->s; |
1394 | |
1395 | Surface *bar = sc.skinRes("imgs/topbar.png", false); |
1396 | if (bar != NULL) |
1397 | bar->blit(s, 0, 0); |
1398 | else |
1399 | s->box(0, 0, resX, skinConfInt["topBarHeight"], |
1400 | skinConfColors[COLOR_TOP_BAR_BG]); |
1401 | } |
1402 | |
1403 | void GMenu2X::drawBottomBar(Surface *s) { |
1404 | if (s==NULL) s = this->s; |
1405 | |
1406 | Surface *bar = sc.skinRes("imgs/bottombar.png", false); |
1407 | if (bar != NULL) |
1408 | bar->blit(s, 0, resY-bar->height()); |
1409 | else |
1410 | s->box(0, resY-20, resX, 20, skinConfColors[COLOR_BOTTOM_BAR_BG]); |
1411 | } |
1412 |
Branches:
install_locations
master
opkrun
packages