Root/
Source at commit 7fd1ab0 created 13 years 10 months ago. By Lars-Peter Clausen, Use a simple array instead of a hashmap for the colors. | |
---|---|
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 | #include <sys/types.h> |
21 | #include <sys/stat.h> |
22 | #include <unistd.h> |
23 | |
24 | #include <fstream> |
25 | #include <sstream> |
26 | #include "linkapp.h" |
27 | #include "menu.h" |
28 | #include "selector.h" |
29 | #include "textmanualdialog.h" |
30 | |
31 | using namespace std; |
32 | |
33 | LinkApp::LinkApp(GMenu2X *gmenu2x, const char* linkfile) |
34 | : Link(gmenu2x) { |
35 | this->gmenu2x = gmenu2x; |
36 | manual = ""; |
37 | file = linkfile; |
38 | wrapper = false; |
39 | dontleave = false; |
40 | setClock(336); |
41 | setVolume(-1); |
42 | //G |
43 | //setGamma(0); |
44 | setBacklight(100); |
45 | selectordir = ""; |
46 | selectorfilter = ""; |
47 | icon = iconPath = ""; |
48 | selectorbrowser = false; |
49 | useRamTimings = false; |
50 | |
51 | string line; |
52 | ifstream infile (linkfile, ios_base::in); |
53 | while (getline(infile, line, '\n')) { |
54 | line = trim(line); |
55 | if (line=="") continue; |
56 | if (line[0]=='#') continue; |
57 | |
58 | string::size_type position = line.find("="); |
59 | string name = trim(line.substr(0,position)); |
60 | string value = trim(line.substr(position+1)); |
61 | if (name == "title") { |
62 | title = value; |
63 | } else if (name == "description") { |
64 | description = value; |
65 | } else if (name == "icon") { |
66 | setIcon(value); |
67 | } else if (name == "exec") { |
68 | exec = value; |
69 | } else if (name == "params") { |
70 | params = value; |
71 | } else if (name == "workdir") { |
72 | workdir = value; |
73 | } else if (name == "manual") { |
74 | manual = value; |
75 | } else if (name == "wrapper") { |
76 | if (value=="true") wrapper = true; |
77 | } else if (name == "dontleave") { |
78 | if (value=="true") dontleave = true; |
79 | } else if (name == "clock") { |
80 | setClock( atoi(value.c_str()) ); |
81 | //G |
82 | } else if (name == "gamma") { |
83 | setGamma( atoi(value.c_str()) ); |
84 | } else if (name == "backlight") { |
85 | setBacklight( atoi(value.c_str()) ); |
86 | } else if (name == "volume") { |
87 | setVolume( atoi(value.c_str()) ); |
88 | } else if (name == "selectordir") { |
89 | setSelectorDir( value ); |
90 | } else if (name == "selectorbrowser") { |
91 | if (value=="true") selectorbrowser = true; |
92 | } else if (name == "useramtimings") { |
93 | if (value=="true") useRamTimings = true; |
94 | } else if (name == "selectorfilter") { |
95 | setSelectorFilter( value ); |
96 | } else if (name == "selectorscreens") { |
97 | setSelectorScreens( value ); |
98 | } else if (name == "selectoraliases") { |
99 | setAliasFile( value ); |
100 | } else { |
101 | cout << "Unrecognized option: " << name << endl; |
102 | break; |
103 | } |
104 | } |
105 | infile.close(); |
106 | |
107 | if (iconPath.empty()) searchIcon(); |
108 | |
109 | edited = false; |
110 | } |
111 | |
112 | const string &LinkApp::searchIcon() { |
113 | string execicon = exec; |
114 | string::size_type pos = exec.rfind("."); |
115 | if (pos != string::npos) execicon = exec.substr(0,pos); |
116 | execicon += ".png"; |
117 | string exectitle = execicon; |
118 | pos = execicon.rfind("/"); |
119 | if (pos != string::npos) |
120 | string exectitle = execicon.substr(pos+1,execicon.length()); |
121 | |
122 | if (!gmenu2x->sc.getSkinFilePath("icons/"+exectitle).empty()) |
123 | iconPath = gmenu2x->sc.getSkinFilePath("icons/"+exectitle); |
124 | else if (fileExists(execicon)) |
125 | iconPath = execicon; |
126 | else |
127 | iconPath = gmenu2x->sc.getSkinFilePath("icons/generic.png"); |
128 | |
129 | return iconPath; |
130 | } |
131 | |
132 | int LinkApp::clock() { |
133 | return iclock; |
134 | } |
135 | |
136 | const string &LinkApp::clockStr(int maxClock) { |
137 | if (iclock>maxClock) setClock(maxClock); |
138 | return sclock; |
139 | } |
140 | |
141 | void LinkApp::setClock(int mhz) { |
142 | iclock = constrain(mhz,200,430); |
143 | stringstream ss; |
144 | sclock = ""; |
145 | ss << iclock << "Mhz"; |
146 | ss >> sclock; |
147 | |
148 | edited = true; |
149 | } |
150 | |
151 | int LinkApp::volume() { |
152 | return ivolume; |
153 | } |
154 | |
155 | const string &LinkApp::volumeStr() { |
156 | return svolume; |
157 | } |
158 | |
159 | void LinkApp::setVolume(int vol) { |
160 | ivolume = constrain(vol,-1,100); |
161 | stringstream ss; |
162 | svolume = ""; |
163 | if (ivolume<0) |
164 | ss << gmenu2x->confInt["globalVolume"]; |
165 | else |
166 | ss << ivolume; |
167 | ss >> svolume; |
168 | |
169 | edited = true; |
170 | } |
171 | |
172 | int LinkApp::backlight() |
173 | { |
174 | return ibacklight; |
175 | } |
176 | |
177 | const string &LinkApp::backlightStr() |
178 | { |
179 | return sbacklight; |
180 | } |
181 | |
182 | void LinkApp::setBacklight(int val) |
183 | { |
184 | ibacklight = constrain(val,5,100); |
185 | stringstream ss; |
186 | sbacklight = ""; |
187 | ss << ibacklight; |
188 | ss >> sbacklight; |
189 | |
190 | edited = true; |
191 | } |
192 | //G |
193 | int LinkApp::gamma() { |
194 | return igamma; |
195 | } |
196 | |
197 | const string &LinkApp::gammaStr() { |
198 | return sgamma; |
199 | } |
200 | |
201 | void LinkApp::setGamma(int gamma) { |
202 | igamma = constrain(gamma,0,100); |
203 | stringstream ss; |
204 | sgamma = ""; |
205 | ss << igamma; |
206 | ss >> sgamma; |
207 | |
208 | edited = true; |
209 | } |
210 | // /G |
211 | |
212 | bool LinkApp::targetExists() { |
213 | #ifndef TARGET_GP2X |
214 | return true; //For displaying elements during testing on pc |
215 | #endif |
216 | |
217 | string target = exec; |
218 | if (!exec.empty() && exec[0]!='/' && !workdir.empty()) |
219 | target = workdir + "/" + exec; |
220 | |
221 | return fileExists(target); |
222 | } |
223 | |
224 | bool LinkApp::save() { |
225 | if (!edited) return false; |
226 | |
227 | ofstream f(file.c_str()); |
228 | if (f.is_open()) { |
229 | if (title!="" ) f << "title=" << title << endl; |
230 | if (description!="" ) f << "description=" << description << endl; |
231 | if (icon!="" ) f << "icon=" << icon << endl; |
232 | if (exec!="" ) f << "exec=" << exec << endl; |
233 | if (params!="" ) f << "params=" << params << endl; |
234 | if (workdir!="" ) f << "workdir=" << workdir << endl; |
235 | if (manual!="" ) f << "manual=" << manual << endl; |
236 | if (iclock!=0 ) f << "clock=" << iclock << endl; |
237 | if (useRamTimings ) f << "useramtimings=true" << endl; |
238 | if (ivolume>0 ) f << "volume=" << ivolume << endl; |
239 | //G |
240 | if (igamma!=0 ) f << "gamma=" << igamma << endl; |
241 | if (ibacklight!=0 ) f << "backlight=" << ibacklight << endl; |
242 | if (selectordir!="" ) f << "selectordir=" << selectordir << endl; |
243 | if (selectorbrowser ) f << "selectorbrowser=true" << endl; |
244 | if (selectorfilter!="" ) f << "selectorfilter=" << selectorfilter << endl; |
245 | if (selectorscreens!="") f << "selectorscreens=" << selectorscreens << endl; |
246 | if (aliasfile!="" ) f << "selectoraliases=" << aliasfile << endl; |
247 | if (wrapper ) f << "wrapper=true" << endl; |
248 | if (dontleave ) f << "dontleave=true" << endl; |
249 | f.close(); |
250 | sync(); |
251 | return true; |
252 | } else |
253 | cout << "\033[0;34mGMENU2X:\033[0;31m Error while opening the file '" << file << "' for write\033[0m" << endl; |
254 | return false; |
255 | } |
256 | |
257 | void LinkApp::drawRun() { |
258 | //Darkened background |
259 | gmenu2x->s->box(0, 0, gmenu2x->resX, gmenu2x->resY, 0,0,0,150); |
260 | |
261 | string text = gmenu2x->tr.translate("Launching $1",getTitle().c_str(),NULL); |
262 | int textW = gmenu2x->font->getTextWidth(text); |
263 | int boxW = 62+textW; |
264 | int halfBoxW = boxW/2; |
265 | |
266 | //outer box |
267 | gmenu2x->s->box(gmenu2x->halfX-2-halfBoxW, gmenu2x->halfY-23, halfBoxW*2+5, 47, gmenu2x->skinConfColors[COLOR_MESSAGE_BOX_BG]); |
268 | //inner rectangle |
269 | gmenu2x->s->rectangle(gmenu2x->halfX-halfBoxW, gmenu2x->halfY-21, boxW, 42, gmenu2x->skinConfColors[COLOR_MESSAGE_BOX_BORDER]); |
270 | |
271 | int x = gmenu2x->halfX+10-halfBoxW; |
272 | /*if (getIcon()!="") |
273 | gmenu2x->sc[getIcon()]->blit(gmenu2x->s,x,104); |
274 | else |
275 | gmenu2x->sc["icons/generic.png"]->blit(gmenu2x->s,x,104);*/ |
276 | gmenu2x->sc[getIconPath()]->blit(gmenu2x->s,x,gmenu2x->halfY-16); |
277 | gmenu2x->s->write( gmenu2x->font, text, x+42, gmenu2x->halfY+1, SFontHAlignLeft, SFontVAlignMiddle ); |
278 | gmenu2x->s->flip(); |
279 | } |
280 | |
281 | void LinkApp::run() { |
282 | if (selectordir!="") |
283 | selector(); |
284 | else |
285 | launch(); |
286 | } |
287 | |
288 | void LinkApp::showManual() { |
289 | if (manual=="" || !fileExists(manual)) return; |
290 | |
291 | // Png manuals |
292 | string ext8 = manual.substr(manual.size()-8,8); |
293 | if (ext8==".man.png" || ext8==".man.bmp" || ext8==".man.jpg" || manual.substr(manual.size()-9,9)==".man.jpeg") { |
294 | //Raise the clock to speed-up the loading of the manual |
295 | gmenu2x->setClock(336); |
296 | |
297 | Surface pngman(manual); |
298 | Surface bg(gmenu2x->confStr["wallpaper"],false); |
299 | stringstream ss; |
300 | string pageStatus; |
301 | |
302 | bool close = false, repaint = true; |
303 | int page=0, pagecount=pngman.raw->w/320; |
304 | |
305 | ss << pagecount; |
306 | string spagecount; |
307 | ss >> spagecount; |
308 | |
309 | //Lower the clock |
310 | gmenu2x->setClock(gmenu2x->confInt["menuClock"]); |
311 | |
312 | while (!close) { |
313 | if (repaint) { |
314 | bg.blit(gmenu2x->s, 0, 0); |
315 | pngman.blit(gmenu2x->s, -page*320, 0); |
316 | |
317 | gmenu2x->drawBottomBar(); |
318 | gmenu2x->drawButton(gmenu2x->s, "x", gmenu2x->tr["Exit"], |
319 | gmenu2x->drawButton(gmenu2x->s, "right", gmenu2x->tr["Change page"], |
320 | gmenu2x->drawButton(gmenu2x->s, "left", "", 5)-10)); |
321 | |
322 | ss.clear(); |
323 | ss << page+1; |
324 | ss >> pageStatus; |
325 | pageStatus = gmenu2x->tr["Page"]+": "+pageStatus+"/"+spagecount; |
326 | gmenu2x->s->write(gmenu2x->font, pageStatus, 310, 230, SFontHAlignRight, SFontVAlignMiddle); |
327 | |
328 | gmenu2x->s->flip(); |
329 | repaint = false; |
330 | } |
331 | |
332 | gmenu2x->input.update(); |
333 | if ( gmenu2x->input[ACTION_Y] || gmenu2x->input[ACTION_X] || gmenu2x->input[ACTION_START] ) close = true; |
334 | if ( gmenu2x->input[ACTION_LEFT] && page>0 ) { page--; repaint=true; } |
335 | if ( gmenu2x->input[ACTION_RIGHT] && page<pagecount-1 ) { page++; repaint=true; } |
336 | } |
337 | return; |
338 | } |
339 | |
340 | // Txt manuals |
341 | if (manual.substr(manual.size()-8,8)==".man.txt") { |
342 | vector<string> txtman; |
343 | |
344 | string line; |
345 | ifstream infile(manual.c_str(), ios_base::in); |
346 | if (infile.is_open()) { |
347 | gmenu2x->setClock(336); |
348 | while (getline(infile, line, '\n')) txtman.push_back(line); |
349 | infile.close(); |
350 | |
351 | TextManualDialog tmd(gmenu2x, getTitle(), getIconPath(), &txtman); |
352 | gmenu2x->setClock(gmenu2x->confInt["menuClock"]); |
353 | tmd.exec(); |
354 | } |
355 | |
356 | return; |
357 | } |
358 | |
359 | //Readmes |
360 | vector<string> readme; |
361 | |
362 | string line; |
363 | ifstream infile(manual.c_str(), ios_base::in); |
364 | if (infile.is_open()) { |
365 | gmenu2x->setClock(336); |
366 | while (getline(infile, line, '\n')) readme.push_back(line); |
367 | infile.close(); |
368 | |
369 | TextDialog td(gmenu2x, getTitle(), "ReadMe", getIconPath(), &readme); |
370 | gmenu2x->setClock(gmenu2x->confInt["menuClock"]); |
371 | td.exec(); |
372 | } |
373 | } |
374 | |
375 | void LinkApp::selector(int startSelection, const string &selectorDir) { |
376 | //Run selector interface |
377 | Selector sel(gmenu2x, this, selectorDir); |
378 | int selection = sel.exec(startSelection); |
379 | if (selection!=-1) { |
380 | gmenu2x->writeTmp(selection,sel.dir); |
381 | launch(sel.file, sel.dir); |
382 | } |
383 | } |
384 | |
385 | void LinkApp::launch(const string &selectedFile, const string &selectedDir) { |
386 | drawRun(); |
387 | save(); |
388 | #ifndef TARGET_GP2X |
389 | //delay for testing |
390 | SDL_Delay(1000); |
391 | #endif |
392 | |
393 | //Set correct working directory |
394 | string wd = workdir; |
395 | if (wd=="") { |
396 | string::size_type pos = exec.rfind("/"); |
397 | if (pos!=string::npos) |
398 | wd = exec.substr(0,pos); |
399 | } |
400 | if (!wd.empty()) { |
401 | if (wd[0]!='/') wd = gmenu2x->getExePath() + wd; |
402 | chdir(wd.c_str()); |
403 | } |
404 | |
405 | //selectedFile |
406 | if (selectedFile!="") { |
407 | string selectedFileExtension; |
408 | string selectedFileName; |
409 | string dir; |
410 | string::size_type i = selectedFile.rfind("."); |
411 | if (i != string::npos) { |
412 | selectedFileExtension = selectedFile.substr(i,selectedFile.length()); |
413 | selectedFileName = selectedFile.substr(0,i); |
414 | } |
415 | |
416 | if (selectedDir=="") |
417 | dir = getSelectorDir(); |
418 | else |
419 | dir = selectedDir; |
420 | if (params=="") { |
421 | params = cmdclean(dir+selectedFile); |
422 | } else { |
423 | string origParams = params; |
424 | params = strreplace(params,"[selFullPath]",cmdclean(dir+selectedFile)); |
425 | params = strreplace(params,"[selPath]",cmdclean(dir)); |
426 | params = strreplace(params,"[selFile]",cmdclean(selectedFileName)); |
427 | params = strreplace(params,"[selExt]",cmdclean(selectedFileExtension)); |
428 | if (params == origParams) params += " " + cmdclean(dir+selectedFile); |
429 | } |
430 | } |
431 | |
432 | if (useRamTimings) |
433 | gmenu2x->applyRamTimings(); |
434 | if (volume()>=0) |
435 | gmenu2x->setVolume(volume()); |
436 | |
437 | #ifdef DEBUG |
438 | cout << "\033[0;34mGMENU2X:\033[0m Executing '" << title << "' (" << exec << " " << params << ")" << endl; |
439 | #endif |
440 | |
441 | //check if we have to quit |
442 | string command = cmdclean(exec); |
443 | |
444 | // Check to see if permissions are desirable |
445 | struct stat fstat; |
446 | if( stat( command.c_str(), &fstat ) == 0 ) { |
447 | struct stat newstat = fstat; |
448 | if( S_IRUSR != ( fstat.st_mode & S_IRUSR ) ) |
449 | newstat.st_mode |= S_IRUSR; |
450 | if( S_IXUSR != ( fstat.st_mode & S_IXUSR ) ) |
451 | newstat.st_mode |= S_IXUSR; |
452 | if( fstat.st_mode != newstat.st_mode ) |
453 | chmod( command.c_str(), newstat.st_mode ); |
454 | } // else, well.. we are no worse off :) |
455 | |
456 | if (params!="") command += " " + params; |
457 | if (gmenu2x->confInt["outputLogs"]) command += " &> " + cmdclean(gmenu2x->getExePath()) + "/log.txt"; |
458 | if (wrapper) command += "; sync & cd "+cmdclean(gmenu2x->getExePath())+"; exec ./gmenu2x"; |
459 | if (dontleave) { |
460 | system(command.c_str()); |
461 | } else { |
462 | if (gmenu2x->confInt["saveSelection"] && (gmenu2x->confInt["section"]!=gmenu2x->menu->selSectionIndex() || gmenu2x->confInt["link"]!=gmenu2x->menu->selLinkIndex())) |
463 | gmenu2x->writeConfig(); |
464 | if (gmenu2x->fwType == "open2x" && gmenu2x->savedVolumeMode != gmenu2x->volumeMode) |
465 | gmenu2x->writeConfigOpen2x(); |
466 | if (selectedFile=="") |
467 | gmenu2x->writeTmp(); |
468 | gmenu2x->quit(); |
469 | if (clock()!=gmenu2x->confInt["menuClock"]) |
470 | gmenu2x->setClock(clock()); |
471 | //if (gamma()!=0 && gamma()!=gmenu2x->confInt["gamma"]) |
472 | // gmenu2x->setGamma(gamma()); |
473 | if((backlight() != 0) && (backlight() != gmenu2x->confInt["backlight"])) |
474 | gmenu2x->setBacklight(backlight()); |
475 | |
476 | execlp("/bin/sh","/bin/sh","-c",command.c_str(),NULL); |
477 | //if execution continues then something went wrong and as we already called SDL_Quit we cannot continue |
478 | //try relaunching gmenu2x |
479 | chdir(gmenu2x->getExePath().c_str()); |
480 | execlp("./gmenu2x", "./gmenu2x", NULL); |
481 | } |
482 | |
483 | |
484 | chdir(gmenu2x->getExePath().c_str()); |
485 | } |
486 | |
487 | const string &LinkApp::getExec() { |
488 | return exec; |
489 | } |
490 | |
491 | void LinkApp::setExec(const string &exec) { |
492 | this->exec = exec; |
493 | edited = true; |
494 | } |
495 | |
496 | const string &LinkApp::getParams() { |
497 | return params; |
498 | } |
499 | |
500 | void LinkApp::setParams(const string ¶ms) { |
501 | this->params = params; |
502 | edited = true; |
503 | } |
504 | |
505 | const string &LinkApp::getWorkdir() { |
506 | return workdir; |
507 | } |
508 | |
509 | void LinkApp::setWorkdir(const string &workdir) { |
510 | this->workdir = workdir; |
511 | edited = true; |
512 | } |
513 | |
514 | const string &LinkApp::getManual() { |
515 | return manual; |
516 | } |
517 | |
518 | void LinkApp::setManual(const string &manual) { |
519 | this->manual = manual; |
520 | edited = true; |
521 | } |
522 | |
523 | const string &LinkApp::getSelectorDir() { |
524 | return selectordir; |
525 | } |
526 | |
527 | void LinkApp::setSelectorDir(const string &selectordir) { |
528 | this->selectordir = selectordir; |
529 | if (this->selectordir!="" && this->selectordir[this->selectordir.length()-1]!='/') this->selectordir += "/"; |
530 | edited = true; |
531 | } |
532 | |
533 | bool LinkApp::getSelectorBrowser() { |
534 | return selectorbrowser; |
535 | } |
536 | |
537 | void LinkApp::setSelectorBrowser(bool value) { |
538 | selectorbrowser = value; |
539 | edited = true; |
540 | } |
541 | |
542 | bool LinkApp::getUseRamTimings() { |
543 | return useRamTimings; |
544 | } |
545 | |
546 | void LinkApp::setUseRamTimings(bool value) { |
547 | useRamTimings = value; |
548 | edited = true; |
549 | } |
550 | |
551 | const string &LinkApp::getSelectorFilter() { |
552 | return selectorfilter; |
553 | } |
554 | |
555 | void LinkApp::setSelectorFilter(const string &selectorfilter) { |
556 | this->selectorfilter = selectorfilter; |
557 | edited = true; |
558 | } |
559 | |
560 | const string &LinkApp::getSelectorScreens() { |
561 | return selectorscreens; |
562 | } |
563 | |
564 | void LinkApp::setSelectorScreens(const string &selectorscreens) { |
565 | this->selectorscreens = selectorscreens; |
566 | edited = true; |
567 | } |
568 | |
569 | const string &LinkApp::getAliasFile() { |
570 | return aliasfile; |
571 | } |
572 | |
573 | void LinkApp::setAliasFile(const string &aliasfile) { |
574 | if (fileExists(aliasfile)) { |
575 | this->aliasfile = aliasfile; |
576 | edited = true; |
577 | } |
578 | } |
579 |
Branches:
install_locations
master
opkrun
packages