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 "linkapp.h" |
| 22 | |
| 23 | #include "debug.h" |
| 24 | #include "gmenu2x.h" |
| 25 | #include "launcher.h" |
| 26 | #include "layer.h" |
| 27 | #include "menu.h" |
| 28 | #include "selector.h" |
| 29 | #include "surface.h" |
| 30 | #include "textmanualdialog.h" |
| 31 | #include "utilities.h" |
| 32 | |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/stat.h> |
| 35 | #include <sys/ioctl.h> |
| 36 | #include <signal.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <unistd.h> |
| 39 | #include <fcntl.h> |
| 40 | |
| 41 | #include <array> |
| 42 | #include <cerrno> |
| 43 | #include <fstream> |
| 44 | #include <sstream> |
| 45 | #include <utility> |
| 46 | |
| 47 | #ifdef HAVE_LIBOPK |
| 48 | #include <opk.h> |
| 49 | #endif |
| 50 | |
| 51 | #ifdef HAVE_LIBXDGMIME |
| 52 | #include <xdgmime.h> |
| 53 | #endif |
| 54 | |
| 55 | using namespace std; |
| 56 | |
| 57 | static array<const char *, 4> tokens = { "%f", "%F", "%u", "%U", }; |
| 58 | |
| 59 | |
| 60 | /** |
| 61 | * Displays the launch message (loading screen). |
| 62 | */ |
| 63 | class LaunchLayer: public Layer { |
| 64 | public: |
| 65 | LaunchLayer(LinkApp& app) : app(app) {} |
| 66 | |
| 67 | void paint(Surface &s) override { |
| 68 | app.drawLaunch(s); |
| 69 | } |
| 70 | |
| 71 | bool handleButtonPress(InputManager::Button) override { |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | private: |
| 76 | LinkApp& app; |
| 77 | }; |
| 78 | |
| 79 | |
| 80 | #ifdef HAVE_LIBOPK |
| 81 | LinkApp::LinkApp(GMenu2X& gmenu2x, string const& linkfile, bool deletable, |
| 82 | struct OPK *opk, const char *metadata_) |
| 83 | #else |
| 84 | LinkApp::LinkApp(GMenu2X& gmenu2x, string const& linkfile, bool deletable) |
| 85 | #endif |
| 86 | : Link(gmenu2x, bind(&LinkApp::start, this)) |
| 87 | , deletable(deletable) |
| 88 | { |
| 89 | manual = ""; |
| 90 | file = linkfile; |
| 91 | #ifdef ENABLE_CPUFREQ |
| 92 | setClock(gmenu2x.getDefaultAppClock()); |
| 93 | #else |
| 94 | setClock(0); |
| 95 | #endif |
| 96 | selectordir = ""; |
| 97 | selectorfilter = "*"; |
| 98 | icon = iconPath = ""; |
| 99 | selectorbrowser = true; |
| 100 | editable = true; |
| 101 | edited = false; |
| 102 | |
| 103 | bool appTakesFileArg = true; |
| 104 | #ifdef HAVE_LIBOPK |
| 105 | isOPK = !!opk; |
| 106 | |
| 107 | if (isOPK) { |
| 108 | string::size_type pos; |
| 109 | const char *key, *val; |
| 110 | size_t lkey, lval; |
| 111 | int ret; |
| 112 | |
| 113 | metadata.assign(metadata_); |
| 114 | opkFile = file; |
| 115 | pos = file.rfind('/'); |
| 116 | opkMount = file.substr(pos+1); |
| 117 | pos = opkMount.rfind('.'); |
| 118 | opkMount = opkMount.substr(0, pos); |
| 119 | |
| 120 | appTakesFileArg = false; |
| 121 | category = "applications"; |
| 122 | |
| 123 | while ((ret = opk_read_pair(opk, &key, &lkey, &val, &lval))) { |
| 124 | if (ret < 0) { |
| 125 | ERROR("Unable to read meta-data\n"); |
| 126 | break; |
| 127 | } |
| 128 | |
| 129 | char buf[lval + 1]; |
| 130 | sprintf(buf, "%.*s", lval, val); |
| 131 | |
| 132 | if (!strncmp(key, "Categories", lkey)) { |
| 133 | category = buf; |
| 134 | |
| 135 | pos = category.find(';'); |
| 136 | if (pos != category.npos) |
| 137 | category = category.substr(0, pos); |
| 138 | |
| 139 | } else if ((!strncmp(key, "Name", lkey) && title.empty()) |
| 140 | || !strncmp(key, ("Name[" + gmenu2x.tr["Lng"] + |
| 141 | "]").c_str(), lkey)) { |
| 142 | title = buf; |
| 143 | |
| 144 | } else if ((!strncmp(key, "Comment", lkey) && description.empty()) |
| 145 | || !strncmp(key, ("Comment[" + |
| 146 | gmenu2x.tr["Lng"] + "]").c_str(), lkey)) { |
| 147 | description = buf; |
| 148 | |
| 149 | } else if (!strncmp(key, "Terminal", lkey)) { |
| 150 | consoleApp = !strncmp(val, "true", lval); |
| 151 | |
| 152 | } else if (!strncmp(key, "X-OD-Manual", lkey)) { |
| 153 | manual = buf; |
| 154 | |
| 155 | } else if (!strncmp(key, "Icon", lkey)) { |
| 156 | /* Read the icon from the OPK only |
| 157 | * if it doesn't exist on the skin */ |
| 158 | this->icon = gmenu2x.sc.getSkinFilePath("icons/" + (string) buf + ".png"); |
| 159 | if (this->icon.empty()) { |
| 160 | this->icon = linkfile + '#' + buf + ".png"; |
| 161 | } |
| 162 | iconPath = this->icon; |
| 163 | updateSurfaces(); |
| 164 | |
| 165 | } else if (!strncmp(key, "Exec", lkey)) { |
| 166 | string tmp = buf; |
| 167 | |
| 168 | for (auto token : tokens) { |
| 169 | if (tmp.find(token) != tmp.npos) { |
| 170 | selectordir = CARD_ROOT; |
| 171 | appTakesFileArg = true; |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | continue; |
| 177 | } |
| 178 | |
| 179 | #ifdef HAVE_LIBXDGMIME |
| 180 | if (!strncmp(key, "MimeType", lkey)) { |
| 181 | string mimetypes = buf; |
| 182 | selectorfilter = ""; |
| 183 | |
| 184 | while ((pos = mimetypes.find(';')) != mimetypes.npos) { |
| 185 | int nb = 16; |
| 186 | char *extensions[nb]; |
| 187 | string mimetype = mimetypes.substr(0, pos); |
| 188 | mimetypes = mimetypes.substr(pos + 1); |
| 189 | |
| 190 | nb = xdg_mime_get_extensions_from_mime_type( |
| 191 | mimetype.c_str(), extensions, nb); |
| 192 | |
| 193 | while (nb--) { |
| 194 | selectorfilter += (string) extensions[nb] + ','; |
| 195 | free(extensions[nb]); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /* Remove last comma */ |
| 200 | if (!selectorfilter.empty()) { |
| 201 | selectorfilter.pop_back(); |
| 202 | DEBUG("Compatible extensions: %s\n", selectorfilter.c_str()); |
| 203 | } |
| 204 | |
| 205 | continue; |
| 206 | } |
| 207 | #endif /* HAVE_LIBXDGMIME */ |
| 208 | } |
| 209 | |
| 210 | file = gmenu2x.getHome() + "/sections/" + category + '/' + opkMount; |
| 211 | opkMount = (string) "/mnt/" + opkMount + '/'; |
| 212 | edited = true; |
| 213 | } else |
| 214 | #endif /* HAVE_LIBOPK */ |
| 215 | { |
| 216 | // Non-packaged application. |
| 217 | |
| 218 | // Consider non-deletable applications to be immutable. |
| 219 | editable = deletable; |
| 220 | } |
| 221 | |
| 222 | string line; |
| 223 | ifstream infile (file.c_str(), ios_base::in); |
| 224 | while (getline(infile, line, '\n')) { |
| 225 | line = trim(line); |
| 226 | if (line.empty()) continue; |
| 227 | if (line[0]=='#') continue; |
| 228 | |
| 229 | string::size_type position = line.find("="); |
| 230 | string name = trim(line.substr(0,position)); |
| 231 | string value = trim(line.substr(position+1)); |
| 232 | |
| 233 | if (name == "clock") { |
| 234 | setClock( atoi(value.c_str()) ); |
| 235 | } else if (name == "selectordir") { |
| 236 | if (appTakesFileArg) setSelectorDir(value); |
| 237 | } else if (name == "selectorbrowser") { |
| 238 | if (value=="false") selectorbrowser = false; |
| 239 | } else if (!isOpk()) { |
| 240 | if (name == "title") { |
| 241 | title = value; |
| 242 | } else if (name == "description") { |
| 243 | description = value; |
| 244 | } else if (name == "launchmsg") { |
| 245 | launchMsg = value; |
| 246 | } else if (name == "icon") { |
| 247 | setIcon(value); |
| 248 | } else if (name == "exec") { |
| 249 | exec = value; |
| 250 | } else if (name == "params") { |
| 251 | params = value; |
| 252 | } else if (name == "manual") { |
| 253 | manual = value; |
| 254 | } else if (name == "consoleapp") { |
| 255 | if (value == "true") consoleApp = true; |
| 256 | } else if (name == "selectorfilter") { |
| 257 | setSelectorFilter( value ); |
| 258 | } else if (name == "editable") { |
| 259 | if (value == "false") |
| 260 | editable = false; |
| 261 | } else |
| 262 | WARNING("Unrecognized option: '%s'\n", name.c_str()); |
| 263 | } else |
| 264 | WARNING("Unrecognized option: '%s'\n", name.c_str()); |
| 265 | } |
| 266 | infile.close(); |
| 267 | |
| 268 | if (iconPath.empty()) searchIcon(); |
| 269 | } |
| 270 | |
| 271 | void LinkApp::loadIcon() { |
| 272 | if (icon.compare(0, 5, "skin:") == 0) { |
| 273 | string linkIcon = gmenu2x.sc.getSkinFilePath( |
| 274 | icon.substr(5, string::npos)); |
| 275 | if (!fileExists(linkIcon)) |
| 276 | searchIcon(); |
| 277 | else |
| 278 | setIconPath(linkIcon); |
| 279 | |
| 280 | } else if (!fileExists(icon)) { |
| 281 | searchIcon(); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | const string &LinkApp::searchIcon() { |
| 286 | if (!iconPath.empty()) |
| 287 | return iconPath; |
| 288 | |
| 289 | string execicon = exec; |
| 290 | string::size_type pos = exec.rfind("."); |
| 291 | if (pos != string::npos) execicon = exec.substr(0,pos); |
| 292 | execicon += ".png"; |
| 293 | string exectitle = execicon; |
| 294 | pos = execicon.rfind("/"); |
| 295 | if (pos != string::npos) |
| 296 | string exectitle = execicon.substr(pos+1,execicon.length()); |
| 297 | |
| 298 | if (!gmenu2x.sc.getSkinFilePath("icons/"+exectitle).empty()) |
| 299 | iconPath = gmenu2x.sc.getSkinFilePath("icons/"+exectitle); |
| 300 | else if (fileExists(execicon)) |
| 301 | iconPath = execicon; |
| 302 | else |
| 303 | iconPath = gmenu2x.sc.getSkinFilePath("icons/generic.png"); |
| 304 | |
| 305 | return iconPath; |
| 306 | } |
| 307 | |
| 308 | int LinkApp::clock() { |
| 309 | return iclock; |
| 310 | } |
| 311 | |
| 312 | const string &LinkApp::clockStr(int maxClock) { |
| 313 | if (iclock>maxClock) setClock(maxClock); |
| 314 | return sclock; |
| 315 | } |
| 316 | |
| 317 | void LinkApp::setClock(int mhz) { |
| 318 | iclock = mhz; |
| 319 | stringstream ss; |
| 320 | sclock = ""; |
| 321 | ss << iclock << "MHz"; |
| 322 | ss >> sclock; |
| 323 | |
| 324 | edited = true; |
| 325 | } |
| 326 | |
| 327 | bool LinkApp::targetExists() |
| 328 | { |
| 329 | return fileExists(exec); |
| 330 | } |
| 331 | |
| 332 | bool LinkApp::save() { |
| 333 | // TODO: In theory a non-editable Link wouldn't have 'edited' set, but |
| 334 | // currently 'edited' is set on more than a few non-edits, so this |
| 335 | // extra check helps prevent write attempts that will never succeed. |
| 336 | // Maybe we shouldn't have an 'edited' flag at all and make the |
| 337 | // outside world fully responsible for calling save() when needed. |
| 338 | if (!editable || !edited) return true; |
| 339 | |
| 340 | std::ostringstream out; |
| 341 | if (!isOpk()) { |
| 342 | if (!title.empty() ) out << "title=" << title << endl; |
| 343 | if (!description.empty() ) out << "description=" << description << endl; |
| 344 | if (!launchMsg.empty() ) out << "launchmsg=" << launchMsg << endl; |
| 345 | if (!icon.empty() ) out << "icon=" << icon << endl; |
| 346 | if (!exec.empty() ) out << "exec=" << exec << endl; |
| 347 | if (!params.empty() ) out << "params=" << params << endl; |
| 348 | if (!manual.empty() ) out << "manual=" << manual << endl; |
| 349 | if (consoleApp ) out << "consoleapp=true" << endl; |
| 350 | if (selectorfilter != "*") out << "selectorfilter=" << selectorfilter << endl; |
| 351 | } |
| 352 | if (iclock != 0 ) out << "clock=" << iclock << endl; |
| 353 | if (!selectordir.empty() ) out << "selectordir=" << selectordir << endl; |
| 354 | if (!selectorbrowser ) out << "selectorbrowser=false" << endl; |
| 355 | |
| 356 | if (out.tellp() > 0) { |
| 357 | DEBUG("Saving app settings: %s\n", file.c_str()); |
| 358 | if (writeStringToFile(file, out.str())) { |
| 359 | string dir = parentDir(file); |
| 360 | if (!syncDir(dir)) { |
| 361 | ERROR("Failed to sync dir: %s\n", dir.c_str()); |
| 362 | // Note: Even if syncDir fails, the app settings have been |
| 363 | // written, so have save() return true. |
| 364 | } |
| 365 | return true; |
| 366 | } else { |
| 367 | return false; |
| 368 | } |
| 369 | } else { |
| 370 | DEBUG("Empty app settings: %s\n", file.c_str()); |
| 371 | return unlink(file.c_str()) == 0 || errno == ENOENT; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | void LinkApp::drawLaunch(Surface& s) { |
| 376 | //Darkened background |
| 377 | s.box(0, 0, gmenu2x.resX, gmenu2x.resY, 0,0,0,150); |
| 378 | |
| 379 | string text = getLaunchMsg().empty() |
| 380 | ? gmenu2x.tr.translate("Launching $1", getTitle().c_str(), nullptr) |
| 381 | : gmenu2x.tr.translate(getLaunchMsg().c_str(), nullptr); |
| 382 | |
| 383 | int textW = gmenu2x.font->getTextWidth(text); |
| 384 | int boxW = 62+textW; |
| 385 | int halfBoxW = boxW/2; |
| 386 | |
| 387 | //outer box |
| 388 | s.box(gmenu2x.halfX-2-halfBoxW, gmenu2x.halfY-23, halfBoxW*2+5, 47, gmenu2x.skinConfColors[COLOR_MESSAGE_BOX_BG]); |
| 389 | //inner rectangle |
| 390 | s.rectangle(gmenu2x.halfX-halfBoxW, gmenu2x.halfY-21, boxW, 42, gmenu2x.skinConfColors[COLOR_MESSAGE_BOX_BORDER]); |
| 391 | |
| 392 | int x = gmenu2x.halfX + 10 - halfBoxW; |
| 393 | /*if (!getIcon().empty()) |
| 394 | gmenu2x.sc[getIcon()]->blit(gmenu2x.s,x,104); |
| 395 | else |
| 396 | gmenu2x.sc["icons/generic.png"]->blit(gmenu2x.s,x,104);*/ |
| 397 | if (iconSurface) { |
| 398 | iconSurface->blit(s, x, gmenu2x.halfY - 16); |
| 399 | } |
| 400 | gmenu2x.font->write(s, text, x + 42, gmenu2x.halfY + 1, Font::HAlignLeft, Font::VAlignMiddle); |
| 401 | } |
| 402 | |
| 403 | void LinkApp::start() { |
| 404 | if (selectordir.empty()) { |
| 405 | gmenu2x.queueLaunch(prepareLaunch(""), make_shared<LaunchLayer>(*this)); |
| 406 | } else { |
| 407 | selector(); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | void LinkApp::showManual() { |
| 412 | if (manual.empty()) |
| 413 | return; |
| 414 | |
| 415 | #ifdef HAVE_LIBOPK |
| 416 | if (isOPK) { |
| 417 | struct OPK *opk = opk_open(opkFile.c_str()); |
| 418 | if (!opk) { |
| 419 | WARNING("Unable to open OPK to read manual\n"); |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | void *buf; |
| 424 | size_t len; |
| 425 | int err = opk_extract_file(opk, manual.c_str(), &buf, &len); |
| 426 | opk_close(opk); |
| 427 | if (err < 0) { |
| 428 | WARNING("Unable to extract manual from OPK\n"); |
| 429 | return; |
| 430 | } |
| 431 | string str((char *) buf, len); |
| 432 | free(buf); |
| 433 | |
| 434 | if (manual.substr(manual.size()-8,8)==".man.txt") { |
| 435 | TextManualDialog tmd(gmenu2x, getTitle(), getIconPath(), str); |
| 436 | tmd.exec(); |
| 437 | } else { |
| 438 | TextDialog td(gmenu2x, getTitle(), "ReadMe", getIconPath(), str); |
| 439 | td.exec(); |
| 440 | } |
| 441 | return; |
| 442 | } |
| 443 | #endif |
| 444 | if (!fileExists(manual)) |
| 445 | return; |
| 446 | |
| 447 | // Png manuals |
| 448 | if (manual.substr(manual.size()-8,8)==".man.png") { |
| 449 | #ifdef ENABLE_CPUFREQ |
| 450 | //Raise the clock to speed-up the loading of the manual |
| 451 | gmenu2x.setSafeMaxClock(); |
| 452 | #endif |
| 453 | |
| 454 | auto pngman = OffscreenSurface::loadImage(manual); |
| 455 | if (!pngman) { |
| 456 | return; |
| 457 | } |
| 458 | auto bg = OffscreenSurface::loadImage(gmenu2x.confStr["wallpaper"]); |
| 459 | if (!bg) { |
| 460 | bg = OffscreenSurface::emptySurface(gmenu2x.s->width(), gmenu2x.s->height()); |
| 461 | } |
| 462 | bg->convertToDisplayFormat(); |
| 463 | |
| 464 | stringstream ss; |
| 465 | string pageStatus; |
| 466 | |
| 467 | bool close = false, repaint = true; |
| 468 | int page = 0, pagecount = pngman->width() / 320; |
| 469 | |
| 470 | ss << pagecount; |
| 471 | string spagecount; |
| 472 | ss >> spagecount; |
| 473 | |
| 474 | #ifdef ENABLE_CPUFREQ |
| 475 | //Lower the clock |
| 476 | gmenu2x.setMenuClock(); |
| 477 | #endif |
| 478 | |
| 479 | while (!close) { |
| 480 | OutputSurface& s = *gmenu2x.s; |
| 481 | |
| 482 | if (repaint) { |
| 483 | bg->blit(s, 0, 0); |
| 484 | pngman->blit(s, -page*320, 0); |
| 485 | |
| 486 | gmenu2x.drawBottomBar(s); |
| 487 | int x = 5; |
| 488 | x = gmenu2x.drawButton(s, "left", "", x); |
| 489 | x = gmenu2x.drawButton(s, "right", gmenu2x.tr["Change page"], x); |
| 490 | x = gmenu2x.drawButton(s, "cancel", "", x); |
| 491 | x = gmenu2x.drawButton(s, "start", gmenu2x.tr["Exit"], x); |
| 492 | (void)x; |
| 493 | |
| 494 | ss.clear(); |
| 495 | ss << page+1; |
| 496 | ss >> pageStatus; |
| 497 | pageStatus = gmenu2x.tr["Page"]+": "+pageStatus+"/"+spagecount; |
| 498 | gmenu2x.font->write(s, pageStatus, 310, 230, Font::HAlignRight, Font::VAlignMiddle); |
| 499 | |
| 500 | s.flip(); |
| 501 | repaint = false; |
| 502 | } |
| 503 | |
| 504 | switch(gmenu2x.input.waitForPressedButton()) { |
| 505 | case InputManager::SETTINGS: |
| 506 | case InputManager::CANCEL: |
| 507 | close = true; |
| 508 | break; |
| 509 | case InputManager::LEFT: |
| 510 | if (page > 0) { |
| 511 | page--; |
| 512 | repaint = true; |
| 513 | } |
| 514 | break; |
| 515 | case InputManager::RIGHT: |
| 516 | if (page < pagecount-1) { |
| 517 | page++; |
| 518 | repaint=true; |
| 519 | } |
| 520 | break; |
| 521 | default: |
| 522 | break; |
| 523 | } |
| 524 | } |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | // Txt manuals |
| 529 | if (manual.substr(manual.size()-8,8)==".man.txt") { |
| 530 | string text(readFileAsString(manual)); |
| 531 | TextManualDialog tmd(gmenu2x, getTitle(), getIconPath(), text); |
| 532 | tmd.exec(); |
| 533 | return; |
| 534 | } |
| 535 | |
| 536 | //Readmes |
| 537 | string str, line; |
| 538 | ifstream infile(manual.c_str(), ios_base::in); |
| 539 | if (infile.is_open()) { |
| 540 | while (getline(infile, line, '\n')) { |
| 541 | str.append(line).append("\n"); |
| 542 | } |
| 543 | infile.close(); |
| 544 | |
| 545 | TextDialog td(gmenu2x, getTitle(), "ReadMe", getIconPath(), str); |
| 546 | td.exec(); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | void LinkApp::selector(int startSelection, const string &selectorDir) { |
| 551 | //Run selector interface |
| 552 | Selector sel(gmenu2x, *this, selectorDir); |
| 553 | int selection = sel.exec(startSelection); |
| 554 | if (selection!=-1) { |
| 555 | const string &selectedDir = sel.getDir(); |
| 556 | if (!selectedDir.empty()) { |
| 557 | selectordir = selectedDir; |
| 558 | } |
| 559 | gmenu2x.writeTmp(selection, selectedDir); |
| 560 | gmenu2x.queueLaunch( |
| 561 | prepareLaunch(selectedDir + sel.getFile()), |
| 562 | make_shared<LaunchLayer>(*this)); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | unique_ptr<Launcher> LinkApp::prepareLaunch(const string &selectedFile) { |
| 567 | if (!save()) { |
| 568 | ERROR("Error saving app settings to '%s'.\n", file.c_str()); |
| 569 | } |
| 570 | |
| 571 | if (!isOpk()) { |
| 572 | //Set correct working directory |
| 573 | string::size_type pos = exec.rfind("/"); |
| 574 | if (pos != string::npos) { |
| 575 | string wd = exec.substr(0, pos + 1); |
| 576 | chdir(wd.c_str()); |
| 577 | exec = wd + exec.substr(pos + 1); |
| 578 | DEBUG("Changed working directory to %s\n", wd.c_str()); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | if (!selectedFile.empty()) { |
| 583 | string path = selectedFile; |
| 584 | if (!isOpk()) |
| 585 | path = cmdclean(path); |
| 586 | |
| 587 | if (params.empty()) { |
| 588 | params = path; |
| 589 | } else { |
| 590 | string::size_type pos; |
| 591 | |
| 592 | for (auto token : tokens) { |
| 593 | while ((pos = params.find(token)) != params.npos) { |
| 594 | params.replace(pos, 2, path); |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | if (gmenu2x.confInt["outputLogs"] && !consoleApp) { |
| 601 | int fd = open(LOG_FILE, O_WRONLY | O_TRUNC | O_CREAT, 0644); |
| 602 | if (fd < 0) { |
| 603 | ERROR("Unable to open log file for write: %s\n", LOG_FILE); |
| 604 | } else { |
| 605 | fflush(stdout); |
| 606 | dup2(fd, STDOUT_FILENO); |
| 607 | dup2(fd, STDERR_FILENO); |
| 608 | close(fd); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | gmenu2x.saveSelection(); |
| 613 | |
| 614 | if (selectedFile.empty()) { |
| 615 | gmenu2x.writeTmp(); |
| 616 | } |
| 617 | #ifdef ENABLE_CPUFREQ |
| 618 | if (clock() != gmenu2x.confInt["menuClock"]) { |
| 619 | gmenu2x.setClock(clock()); |
| 620 | } |
| 621 | #endif |
| 622 | |
| 623 | vector<string> commandLine; |
| 624 | if (isOpk()) { |
| 625 | #ifdef HAVE_LIBOPK |
| 626 | commandLine = { "opkrun", "-m", metadata, opkFile }; |
| 627 | if (!params.empty()) { |
| 628 | commandLine.push_back(params); |
| 629 | } |
| 630 | #endif |
| 631 | } else { |
| 632 | commandLine = { "/bin/sh", "-c", exec + " " + params }; |
| 633 | } |
| 634 | |
| 635 | return std::unique_ptr<Launcher>(new Launcher( |
| 636 | move(commandLine), consoleApp)); |
| 637 | } |
| 638 | |
| 639 | const string &LinkApp::getManual() { |
| 640 | return manual; |
| 641 | } |
| 642 | |
| 643 | void LinkApp::setManual(const string &manual) { |
| 644 | this->manual = manual; |
| 645 | edited = true; |
| 646 | } |
| 647 | |
| 648 | const string &LinkApp::getSelectorDir() { |
| 649 | return selectordir; |
| 650 | } |
| 651 | |
| 652 | void LinkApp::setSelectorDir(const string &selectordir) { |
| 653 | this->selectordir = selectordir; |
| 654 | if (!selectordir.empty() && selectordir[selectordir.length() - 1] != '/') { |
| 655 | this->selectordir += "/"; |
| 656 | } |
| 657 | edited = true; |
| 658 | } |
| 659 | |
| 660 | bool LinkApp::getSelectorBrowser() { |
| 661 | return selectorbrowser; |
| 662 | } |
| 663 | |
| 664 | void LinkApp::setSelectorBrowser(bool value) { |
| 665 | selectorbrowser = value; |
| 666 | edited = true; |
| 667 | } |
| 668 | |
| 669 | const string &LinkApp::getSelectorFilter() { |
| 670 | return selectorfilter; |
| 671 | } |
| 672 | |
| 673 | void LinkApp::setSelectorFilter(const string &selectorfilter) { |
| 674 | this->selectorfilter = selectorfilter; |
| 675 | edited = true; |
| 676 | } |
| 677 | |
| 678 | void LinkApp::setFile(const string &name) { |
| 679 | file = name; |
| 680 | } |
| 681 |
Branches:
install_locations
master
opkrun
packages
