Root/
Source at commit 4159e6925beb9b2f2732b2ac7dce7eae9d75de0b created 8 years 11 months ago. By Maarten ter Huurne, Declare "dirp" and "dptr" pointers just before they're needed | |
---|---|
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 <sstream> |
22 | #include <sys/stat.h> |
23 | #include <sys/types.h> |
24 | #include <dirent.h> |
25 | #include <algorithm> |
26 | #include <math.h> |
27 | #include <fstream> |
28 | #include <unistd.h> |
29 | #include <cassert> |
30 | #include <cerrno> |
31 | #include <cstring> |
32 | |
33 | #ifdef HAVE_LIBOPK |
34 | #include <opk.h> |
35 | #endif |
36 | |
37 | #include "gmenu2x.h" |
38 | #include "linkapp.h" |
39 | #include "menu.h" |
40 | #include "monitor.h" |
41 | #include "filelister.h" |
42 | #include "utilities.h" |
43 | #include "debug.h" |
44 | |
45 | using namespace std; |
46 | |
47 | |
48 | Menu::Animation::Animation() |
49 | : curr(0) |
50 | { |
51 | } |
52 | |
53 | void Menu::Animation::adjust(int delta) |
54 | { |
55 | curr += delta; |
56 | } |
57 | |
58 | void Menu::Animation::step() |
59 | { |
60 | if (curr == 0) { |
61 | ERROR("Computing step past animation end\n"); |
62 | } else if (curr < 0) { |
63 | const int v = ((1 << 16) - curr) / 32; |
64 | curr = std::min(0, curr + v); |
65 | } else { |
66 | const int v = ((1 << 16) + curr) / 32; |
67 | curr = std::max(0, curr - v); |
68 | } |
69 | } |
70 | |
71 | Menu::Menu(GMenu2X& gmenu2x) |
72 | : gmenu2x(gmenu2x) |
73 | , btnContextMenu(gmenu2x, "skin:imgs/menu.png", "", |
74 | std::bind(&GMenu2X::showContextMenu, &gmenu2x)) |
75 | { |
76 | readSections(GMENU2X_SYSTEM_DIR "/sections"); |
77 | readSections(GMenu2X::getHome() + "/sections"); |
78 | |
79 | setSectionIndex(0); |
80 | readLinks(); |
81 | |
82 | #ifdef HAVE_LIBOPK |
83 | { |
84 | DIR *dirp = opendir(CARD_ROOT); |
85 | if (dirp) { |
86 | struct dirent *dptr; |
87 | while ((dptr = readdir(dirp))) { |
88 | if (dptr->d_type != DT_DIR) |
89 | continue; |
90 | |
91 | if (!strcmp(dptr->d_name, ".") || !strcmp(dptr->d_name, "..")) |
92 | continue; |
93 | |
94 | openPackagesFromDir((string) CARD_ROOT + "/" + |
95 | dptr->d_name + "/apps"); |
96 | } |
97 | closedir(dirp); |
98 | } |
99 | } |
100 | #endif |
101 | |
102 | btnContextMenu.setPosition(gmenu2x.resX - 38, gmenu2x.bottomBarIconY); |
103 | } |
104 | |
105 | Menu::~Menu() |
106 | { |
107 | } |
108 | |
109 | void Menu::readSections(std::string const& parentDir) |
110 | { |
111 | DIR *dirp = opendir(parentDir.c_str()); |
112 | if (!dirp) return; |
113 | |
114 | struct dirent *dptr; |
115 | while ((dptr = readdir(dirp))) { |
116 | if (dptr->d_name[0] != '.' && dptr->d_type == DT_DIR) { |
117 | // Create section if it doesn't exist yet. |
118 | sectionNamed(dptr->d_name); |
119 | } |
120 | } |
121 | |
122 | closedir(dirp); |
123 | } |
124 | |
125 | string Menu::createSectionDir(string const& sectionName) |
126 | { |
127 | string parentDir = GMenu2X::getHome() + "/sections"; |
128 | if (mkdir(parentDir.c_str(), 0755) && errno != EEXIST) { |
129 | WARNING("Failed to create parent sections dir: %s\n", strerror(errno)); |
130 | return ""; |
131 | } |
132 | |
133 | string childDir = parentDir + "/" + sectionName; |
134 | if (mkdir(parentDir.c_str(), 0755) && errno != EEXIST) { |
135 | WARNING("Failed to create child section dir: %s\n", strerror(errno)); |
136 | return ""; |
137 | } |
138 | |
139 | return childDir; |
140 | } |
141 | |
142 | void Menu::skinUpdated() { |
143 | ConfIntHash &skinConfInt = gmenu2x.skinConfInt; |
144 | |
145 | //recalculate some coordinates based on the new element sizes |
146 | linkColumns = (gmenu2x.resX - 10) / skinConfInt["linkWidth"]; |
147 | linkRows = (gmenu2x.resY - 35 - skinConfInt["topBarHeight"]) / skinConfInt["linkHeight"]; |
148 | |
149 | //reload section icons |
150 | decltype(links)::size_type i = 0; |
151 | for (auto& sectionName : sections) { |
152 | gmenu2x.sc["skin:sections/" + sectionName + ".png"]; |
153 | |
154 | for (auto& link : links[i]) { |
155 | link->loadIcon(); |
156 | } |
157 | |
158 | i++; |
159 | } |
160 | } |
161 | |
162 | void Menu::calcSectionRange(int &leftSection, int &rightSection) { |
163 | ConfIntHash &skinConfInt = gmenu2x.skinConfInt; |
164 | const int linkWidth = skinConfInt["linkWidth"]; |
165 | const int screenWidth = gmenu2x.resX; |
166 | const int numSections = sections.size(); |
167 | rightSection = min( |
168 | max(1, (screenWidth - 20 - linkWidth) / (2 * linkWidth)), |
169 | numSections / 2); |
170 | leftSection = max( |
171 | -rightSection, |
172 | rightSection - numSections + 1); |
173 | } |
174 | |
175 | bool Menu::runAnimations() { |
176 | if (sectionAnimation.isRunning()) { |
177 | sectionAnimation.step(); |
178 | } |
179 | return sectionAnimation.isRunning(); |
180 | } |
181 | |
182 | void Menu::paint(Surface &s) { |
183 | const uint width = s.width(), height = s.height(); |
184 | Font &font = *gmenu2x.font; |
185 | SurfaceCollection &sc = gmenu2x.sc; |
186 | |
187 | ConfIntHash &skinConfInt = gmenu2x.skinConfInt; |
188 | const int topBarHeight = skinConfInt["topBarHeight"]; |
189 | const int bottomBarHeight = skinConfInt["bottomBarHeight"]; |
190 | const int linkWidth = skinConfInt["linkWidth"]; |
191 | const int linkHeight = skinConfInt["linkHeight"]; |
192 | RGBAColor &selectionBgColor = gmenu2x.skinConfColors[COLOR_SELECTION_BG]; |
193 | |
194 | // Apply section header animation. |
195 | int leftSection, rightSection; |
196 | calcSectionRange(leftSection, rightSection); |
197 | int sectionFP = sectionAnimation.currentValue(); |
198 | int sectionDelta = (sectionFP * linkWidth + (1 << 15)) >> 16; |
199 | int centerSection = iSection - sectionDelta / linkWidth; |
200 | sectionDelta %= linkWidth; |
201 | if (sectionDelta < 0) { |
202 | rightSection++; |
203 | } else if (sectionDelta > 0) { |
204 | leftSection--; |
205 | } |
206 | |
207 | // Paint section headers. |
208 | s.box(width / 2 - linkWidth / 2, 0, linkWidth, topBarHeight, selectionBgColor); |
209 | const uint sectionLinkPadding = (topBarHeight - 32 - font.getLineSpacing()) / 3; |
210 | const uint numSections = sections.size(); |
211 | for (int i = leftSection; i <= rightSection; i++) { |
212 | uint j = (centerSection + numSections + i) % numSections; |
213 | string sectionIcon = "skin:sections/" + sections[j] + ".png"; |
214 | Surface *icon = sc.exists(sectionIcon) |
215 | ? sc[sectionIcon] |
216 | : sc.skinRes("icons/section.png"); |
217 | int x = width / 2 + i * linkWidth + sectionDelta; |
218 | if (i == leftSection) { |
219 | int t = sectionDelta > 0 ? linkWidth - sectionDelta : -sectionDelta; |
220 | x -= (((t * t) / linkWidth) * t) / linkWidth; |
221 | } else if (i == rightSection) { |
222 | int t = sectionDelta < 0 ? sectionDelta + linkWidth : sectionDelta; |
223 | x += (((t * t) / linkWidth) * t) / linkWidth; |
224 | } |
225 | icon->blit(s, x - 16, sectionLinkPadding, 32, 32); |
226 | font.write(s, sections[j], x, topBarHeight - sectionLinkPadding, |
227 | Font::HAlignCenter, Font::VAlignBottom); |
228 | } |
229 | sc.skinRes("imgs/section-l.png")->blit(s, 0, 0); |
230 | sc.skinRes("imgs/section-r.png")->blit(s, width - 10, 0); |
231 | |
232 | auto& sectionLinks = links[iSection]; |
233 | auto numLinks = sectionLinks.size(); |
234 | gmenu2x.drawScrollBar( |
235 | linkRows, (numLinks + linkColumns - 1) / linkColumns, iFirstDispRow); |
236 | |
237 | //Links |
238 | const uint linksPerPage = linkColumns * linkRows; |
239 | const int linkSpacingX = (width - 10 - linkColumns * linkWidth) / linkColumns; |
240 | const int linkMarginX = ( |
241 | width - linkWidth * linkColumns - linkSpacingX * (linkColumns - 1) |
242 | ) / 2; |
243 | const int linkSpacingY = (height - 35 - topBarHeight - linkRows * linkHeight) / linkRows; |
244 | for (uint i = iFirstDispRow * linkColumns; i < iFirstDispRow * linkColumns + linksPerPage && i < numLinks; i++) { |
245 | const int ir = i - iFirstDispRow * linkColumns; |
246 | const int x = linkMarginX + (ir % linkColumns) * (linkWidth + linkSpacingX); |
247 | const int y = ir / linkColumns * (linkHeight + linkSpacingY) + topBarHeight + 2; |
248 | sectionLinks.at(i)->setPosition(x, y); |
249 | |
250 | if (i == (uint)iLink) { |
251 | sectionLinks.at(i)->paintHover(); |
252 | } |
253 | |
254 | sectionLinks.at(i)->paint(); |
255 | } |
256 | |
257 | if (selLink()) { |
258 | font.write(s, selLink()->getDescription(), |
259 | width / 2, height - bottomBarHeight + 2, |
260 | Font::HAlignCenter, Font::VAlignBottom); |
261 | } |
262 | |
263 | LinkApp *linkApp = selLinkApp(); |
264 | if (linkApp) { |
265 | #ifdef ENABLE_CPUFREQ |
266 | font.write(s, linkApp->clockStr(gmenu2x.confInt["maxClock"]), |
267 | gmenu2x.cpuX, gmenu2x.bottomBarTextY, |
268 | Font::HAlignLeft, Font::VAlignMiddle); |
269 | #endif |
270 | //Manual indicator |
271 | if (!linkApp->getManual().empty()) |
272 | sc.skinRes("imgs/manual.png")->blit( |
273 | s, gmenu2x.manualX, gmenu2x.bottomBarIconY); |
274 | } |
275 | } |
276 | |
277 | bool Menu::handleButtonPress(InputManager::Button button) { |
278 | switch (button) { |
279 | case InputManager::ACCEPT: |
280 | if (selLink() != NULL) selLink()->run(); |
281 | return true; |
282 | case InputManager::UP: |
283 | linkUp(); |
284 | return true; |
285 | case InputManager::DOWN: |
286 | linkDown(); |
287 | return true; |
288 | case InputManager::LEFT: |
289 | linkLeft(); |
290 | return true; |
291 | case InputManager::RIGHT: |
292 | linkRight(); |
293 | return true; |
294 | case InputManager::ALTLEFT: |
295 | decSectionIndex(); |
296 | return true; |
297 | case InputManager::ALTRIGHT: |
298 | incSectionIndex(); |
299 | return true; |
300 | case InputManager::MENU: |
301 | gmenu2x.showContextMenu(); |
302 | return true; |
303 | default: |
304 | return false; |
305 | } |
306 | } |
307 | |
308 | /*==================================== |
309 | SECTION MANAGEMENT |
310 | ====================================*/ |
311 | |
312 | vector<unique_ptr<Link>> *Menu::sectionLinks(int i) |
313 | { |
314 | if (i<0 || i>=(int)links.size()) { |
315 | i = selSectionIndex(); |
316 | } |
317 | |
318 | if (i<0 || i>=(int)links.size()) { |
319 | return nullptr; |
320 | } |
321 | |
322 | return &links[i]; |
323 | } |
324 | |
325 | void Menu::decSectionIndex() { |
326 | sectionAnimation.adjust(-1 << 16); |
327 | setSectionIndex(iSection - 1); |
328 | } |
329 | |
330 | void Menu::incSectionIndex() { |
331 | sectionAnimation.adjust(1 << 16); |
332 | setSectionIndex(iSection + 1); |
333 | } |
334 | |
335 | int Menu::selSectionIndex() { |
336 | return iSection; |
337 | } |
338 | |
339 | const string &Menu::selSection() { |
340 | return sections[iSection]; |
341 | } |
342 | |
343 | void Menu::setSectionIndex(int i) { |
344 | if (i<0) |
345 | i=sections.size()-1; |
346 | else if (i>=(int)sections.size()) |
347 | i=0; |
348 | iSection = i; |
349 | |
350 | iLink = 0; |
351 | iFirstDispRow = 0; |
352 | } |
353 | |
354 | /*==================================== |
355 | LINKS MANAGEMENT |
356 | ====================================*/ |
357 | void Menu::addActionLink(uint section, string const& title, Action action, |
358 | string const& description, string const& icon) |
359 | { |
360 | assert(section < sections.size()); |
361 | |
362 | Link *link = new Link(gmenu2x, action); |
363 | link->setSize(gmenu2x.skinConfInt["linkWidth"], gmenu2x.skinConfInt["linkHeight"]); |
364 | link->setTitle(title); |
365 | link->setDescription(description); |
366 | if (gmenu2x.sc.exists(icon) |
367 | || (icon.substr(0,5)=="skin:" |
368 | && !gmenu2x.sc.getSkinFilePath(icon.substr(5,icon.length())).empty()) |
369 | || fileExists(icon)) { |
370 | link->setIcon(icon); |
371 | } |
372 | |
373 | links[section].emplace_back(link); |
374 | } |
375 | |
376 | bool Menu::addLink(string const& path, string const& file) |
377 | { |
378 | string const& sectionName = selSection(); |
379 | |
380 | string sectionDir = createSectionDir(sectionName); |
381 | if (sectionDir.empty()) { |
382 | return false; |
383 | } |
384 | |
385 | //strip the extension from the filename |
386 | string title = file; |
387 | string::size_type pos = title.rfind("."); |
388 | if (pos!=string::npos && pos>0) { |
389 | string ext = title.substr(pos, title.length()); |
390 | transform(ext.begin(), ext.end(), ext.begin(), ::tolower); |
391 | title = title.substr(0, pos); |
392 | } |
393 | |
394 | string linkpath = sectionDir + "/" + title; |
395 | int x = 2; |
396 | while (fileExists(linkpath)) { |
397 | stringstream ss; |
398 | ss << sectionDir << '/' << title << x; |
399 | ss >> linkpath; |
400 | x++; |
401 | } |
402 | INFO("Adding link: '%s'\n", linkpath.c_str()); |
403 | |
404 | string dirPath = path; |
405 | if (dirPath.empty() || dirPath.back() != '/') dirPath += '/'; |
406 | |
407 | //search for a manual |
408 | pos = file.rfind("."); |
409 | string exename = dirPath + file.substr(0, pos); |
410 | string manual = ""; |
411 | if (fileExists(exename+".man.png")) { |
412 | manual = exename+".man.png"; |
413 | } else if (fileExists(exename+".man.txt")) { |
414 | manual = exename+".man.txt"; |
415 | } else { |
416 | //scan directory for a file like *readme* |
417 | FileLister fl; |
418 | fl.setShowDirectories(false); |
419 | fl.setFilter(".txt"); |
420 | fl.browse(dirPath); |
421 | bool found = false; |
422 | for (uint x=0; x<fl.size() && !found; x++) { |
423 | string lcfilename = fl[x]; |
424 | |
425 | if (lcfilename.find("readme") != string::npos) { |
426 | found = true; |
427 | manual = dirPath + fl.getFiles()[x]; |
428 | } |
429 | } |
430 | } |
431 | |
432 | INFO("Manual: '%s'\n", manual.c_str()); |
433 | |
434 | string shorttitle=title, description="", exec=dirPath+file, icon=""; |
435 | if (fileExists(exename+".png")) icon = exename+".png"; |
436 | |
437 | //Reduce title lenght to fit the link width |
438 | if (gmenu2x.font->getTextWidth(shorttitle)>gmenu2x.skinConfInt["linkWidth"]) { |
439 | while (gmenu2x.font->getTextWidth(shorttitle+"..")>gmenu2x.skinConfInt["linkWidth"]) |
440 | shorttitle = shorttitle.substr(0,shorttitle.length()-1); |
441 | shorttitle += ".."; |
442 | } |
443 | |
444 | ofstream f(linkpath.c_str()); |
445 | if (f.is_open()) { |
446 | f << "title=" << shorttitle << endl; |
447 | f << "exec=" << exec << endl; |
448 | if (!description.empty()) f << "description=" << description << endl; |
449 | if (!icon.empty()) f << "icon=" << icon << endl; |
450 | if (!manual.empty()) f << "manual=" << manual << endl; |
451 | f.close(); |
452 | sync(); |
453 | |
454 | auto idx = sectionNamed(sectionName); |
455 | auto link = new LinkApp(gmenu2x, linkpath, true); |
456 | link->setSize(gmenu2x.skinConfInt["linkWidth"], gmenu2x.skinConfInt["linkHeight"]); |
457 | links[idx].emplace_back(link); |
458 | } else { |
459 | |
460 | ERROR("Error while opening the file '%s' for write.\n", linkpath.c_str()); |
461 | |
462 | return false; |
463 | } |
464 | |
465 | return true; |
466 | } |
467 | |
468 | int Menu::sectionNamed(const char *sectionName) |
469 | { |
470 | auto it = lower_bound(sections.begin(), sections.end(), sectionName); |
471 | int idx = it - sections.begin(); |
472 | if (it == sections.end() || *it != sectionName) { |
473 | sections.emplace(it, sectionName); |
474 | links.emplace(links.begin() + idx); |
475 | } |
476 | return idx; |
477 | } |
478 | |
479 | void Menu::deleteSelectedLink() |
480 | { |
481 | string iconpath = selLink()->getIconPath(); |
482 | |
483 | INFO("Deleting link '%s'\n", selLink()->getTitle().c_str()); |
484 | |
485 | if (selLinkApp()!=NULL) |
486 | unlink(selLinkApp()->getFile().c_str()); |
487 | sectionLinks()->erase( sectionLinks()->begin() + selLinkIndex() ); |
488 | setLinkIndex(selLinkIndex()); |
489 | |
490 | bool icon_used = false; |
491 | for (auto& section : links) { |
492 | for (auto& link : section) { |
493 | if (iconpath == link->getIconPath()) { |
494 | icon_used = true; |
495 | } |
496 | } |
497 | } |
498 | if (!icon_used) { |
499 | gmenu2x.sc.del(iconpath); |
500 | } |
501 | } |
502 | |
503 | void Menu::deleteSelectedSection() { |
504 | INFO("Deleting section '%s'\n", selSection().c_str()); |
505 | |
506 | gmenu2x.sc.del("sections/"+selSection()+".png"); |
507 | links.erase( links.begin()+selSectionIndex() ); |
508 | sections.erase( sections.begin()+selSectionIndex() ); |
509 | setSectionIndex(0); //reload sections |
510 | } |
511 | |
512 | bool Menu::linkChangeSection(uint linkIndex, uint oldSectionIndex, uint newSectionIndex) { |
513 | // Fetch sections. |
514 | auto oldSectionLinks = sectionLinks(oldSectionIndex); |
515 | if (!oldSectionLinks) return false; |
516 | auto newSectionLinks = sectionLinks(newSectionIndex); |
517 | if (!newSectionLinks) return false; |
518 | |
519 | // Find link in old section. |
520 | if (linkIndex >= oldSectionLinks->size()) return false; |
521 | auto it = oldSectionLinks->begin() + linkIndex; |
522 | |
523 | // Move link. |
524 | auto link = it->release(); |
525 | oldSectionLinks->erase(it); |
526 | newSectionLinks->emplace_back(link); |
527 | |
528 | // Select the same link in the new section. |
529 | setSectionIndex(newSectionIndex); |
530 | setLinkIndex(newSectionLinks->size() - 1); |
531 | |
532 | return true; |
533 | } |
534 | |
535 | void Menu::linkLeft() { |
536 | if (iLink % linkColumns == 0) |
537 | setLinkIndex(sectionLinks()->size() > iLink + linkColumns - 1 |
538 | ? iLink + linkColumns - 1 : sectionLinks()->size() - 1); |
539 | else |
540 | setLinkIndex(iLink - 1); |
541 | } |
542 | |
543 | void Menu::linkRight() { |
544 | if (iLink % linkColumns == linkColumns - 1 |
545 | || iLink == (int)sectionLinks()->size() - 1) |
546 | setLinkIndex(iLink - iLink % linkColumns); |
547 | else |
548 | setLinkIndex(iLink + 1); |
549 | } |
550 | |
551 | #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) |
552 | |
553 | void Menu::linkUp() { |
554 | int l = iLink - linkColumns; |
555 | if (l < 0) { |
556 | const auto numLinks = sectionLinks()->size(); |
557 | unsigned int rows = DIV_ROUND_UP(numLinks, linkColumns); |
558 | l = (rows * linkColumns) + l; |
559 | if (l >= static_cast<int>(numLinks)) |
560 | l -= linkColumns; |
561 | } |
562 | setLinkIndex(l); |
563 | } |
564 | |
565 | void Menu::linkDown() { |
566 | int l = iLink + linkColumns; |
567 | const auto numLinks = sectionLinks()->size(); |
568 | if (l >= static_cast<int>(numLinks)) { |
569 | unsigned int rows = DIV_ROUND_UP(numLinks, linkColumns); |
570 | unsigned int curCol = DIV_ROUND_UP(iLink + 1, linkColumns); |
571 | if (rows > curCol) |
572 | l = numLinks - 1; |
573 | else |
574 | l %= linkColumns; |
575 | } |
576 | setLinkIndex(l); |
577 | } |
578 | |
579 | int Menu::selLinkIndex() { |
580 | return iLink; |
581 | } |
582 | |
583 | Link *Menu::selLink() { |
584 | auto curSectionLinks = sectionLinks(); |
585 | if (curSectionLinks->empty()) { |
586 | return nullptr; |
587 | } else { |
588 | return curSectionLinks->at(iLink).get(); |
589 | } |
590 | } |
591 | |
592 | LinkApp *Menu::selLinkApp() { |
593 | return dynamic_cast<LinkApp*>(selLink()); |
594 | } |
595 | |
596 | void Menu::setLinkIndex(int i) { |
597 | auto links = sectionLinks(); |
598 | if (!links) { |
599 | // No sections. |
600 | return; |
601 | } |
602 | const int numLinks = static_cast<int>(sectionLinks()->size()); |
603 | if (i < 0) |
604 | i = numLinks - 1; |
605 | else if (i >= numLinks) |
606 | i = 0; |
607 | iLink = i; |
608 | |
609 | int row = i / linkColumns; |
610 | if (row >= (int)(iFirstDispRow + linkRows - 1)) |
611 | iFirstDispRow = min(row + 1, (int)DIV_ROUND_UP(numLinks, linkColumns) - 1) |
612 | - linkRows + 1; |
613 | else if (row <= (int)iFirstDispRow) |
614 | iFirstDispRow = max(row - 1, 0); |
615 | } |
616 | |
617 | #ifdef HAVE_LIBOPK |
618 | void Menu::openPackagesFromDir(std::string const& path) |
619 | { |
620 | DEBUG("Opening packages from directory: %s\n", path.c_str()); |
621 | if (readPackages(path)) { |
622 | #ifdef ENABLE_INOTIFY |
623 | monitors.emplace_back(new Monitor(path.c_str())); |
624 | #endif |
625 | } |
626 | } |
627 | |
628 | void Menu::openPackage(std::string const& path, bool order) |
629 | { |
630 | /* First try to remove existing links of the same OPK |
631 | * (needed for instance when an OPK is modified) */ |
632 | removePackageLink(path); |
633 | |
634 | struct OPK *opk = opk_open(path.c_str()); |
635 | if (!opk) { |
636 | ERROR("Unable to open OPK %s\n", path.c_str()); |
637 | return; |
638 | } |
639 | |
640 | for (;;) { |
641 | bool has_metadata = false; |
642 | const char *name; |
643 | |
644 | for (;;) { |
645 | string::size_type pos; |
646 | int ret = opk_open_metadata(opk, &name); |
647 | if (ret < 0) { |
648 | ERROR("Error while loading meta-data\n"); |
649 | break; |
650 | } else if (!ret) |
651 | break; |
652 | |
653 | /* Strip .desktop */ |
654 | string metadata(name); |
655 | pos = metadata.rfind('.'); |
656 | metadata = metadata.substr(0, pos); |
657 | |
658 | /* Keep only the platform name */ |
659 | pos = metadata.rfind('.'); |
660 | metadata = metadata.substr(pos + 1); |
661 | |
662 | if (metadata == PLATFORM || metadata == "all") { |
663 | has_metadata = true; |
664 | break; |
665 | } |
666 | } |
667 | |
668 | if (!has_metadata) |
669 | break; |
670 | |
671 | // Note: OPK links can only be deleted by removing the OPK itself, |
672 | // but that is not something we want to do in the menu, |
673 | // so consider this link undeletable. |
674 | auto link = new LinkApp(gmenu2x, path, false, opk, name); |
675 | link->setSize(gmenu2x.skinConfInt["linkWidth"], gmenu2x.skinConfInt["linkHeight"]); |
676 | |
677 | auto idx = sectionNamed(link->getCategory()); |
678 | links[idx].emplace_back(link); |
679 | } |
680 | |
681 | opk_close(opk); |
682 | |
683 | if (order) |
684 | orderLinks(); |
685 | } |
686 | |
687 | bool Menu::readPackages(std::string const& parentDir) |
688 | { |
689 | DIR *dirp = opendir(parentDir.c_str()); |
690 | if (!dirp) { |
691 | return false; |
692 | } |
693 | |
694 | while (struct dirent *dptr = readdir(dirp)) { |
695 | if (dptr->d_type != DT_REG) |
696 | continue; |
697 | |
698 | char *c = strrchr(dptr->d_name, '.'); |
699 | if (!c) /* File without extension */ |
700 | continue; |
701 | |
702 | if (strcasecmp(c + 1, "opk")) |
703 | continue; |
704 | |
705 | if (dptr->d_name[0] == '.') { |
706 | // Ignore hidden files. |
707 | // Mac OS X places these on SD cards, probably to store metadata. |
708 | continue; |
709 | } |
710 | |
711 | openPackage(parentDir + '/' + dptr->d_name, false); |
712 | } |
713 | |
714 | closedir(dirp); |
715 | orderLinks(); |
716 | |
717 | return true; |
718 | } |
719 | |
720 | #ifdef ENABLE_INOTIFY |
721 | /* Remove all links that correspond to the given path. |
722 | * If "path" is a directory, it will remove all links that |
723 | * correspond to an OPK present in the directory. */ |
724 | void Menu::removePackageLink(std::string const& path) |
725 | { |
726 | for (auto section = links.begin(); section != links.end(); ++section) { |
727 | for (auto link = section->begin(); link != section->end(); ++link) { |
728 | LinkApp *app = dynamic_cast<LinkApp *>(link->get()); |
729 | if (!app || !app->isOpk() || app->getOpkFile().empty()) |
730 | continue; |
731 | |
732 | if (app->getOpkFile().compare(0, path.size(), path) == 0) { |
733 | DEBUG("Removing link corresponding to package %s\n", |
734 | app->getOpkFile().c_str()); |
735 | section->erase(link); |
736 | if (section - links.begin() == iSection |
737 | && iLink == (int) section->size()) { |
738 | setLinkIndex(iLink - 1); |
739 | } |
740 | --link; |
741 | } |
742 | } |
743 | } |
744 | |
745 | /* Remove registered monitors */ |
746 | for (auto it = monitors.begin(); it < monitors.end(); ++it) { |
747 | if ((*it)->getPath().compare(0, path.size(), path) == 0) { |
748 | monitors.erase(it); |
749 | } |
750 | } |
751 | } |
752 | #endif |
753 | #endif |
754 | |
755 | static bool compare_links(unique_ptr<Link> const& a, unique_ptr<Link> const& b) |
756 | { |
757 | LinkApp *app1 = dynamic_cast<LinkApp *>(a.get()); |
758 | LinkApp *app2 = dynamic_cast<LinkApp *>(b.get()); |
759 | bool app1_is_opk = app1 && app1->isOpk(), |
760 | app2_is_opk = app2 && app2->isOpk(); |
761 | |
762 | if (app1_is_opk && !app2_is_opk) { |
763 | return false; |
764 | } |
765 | if (app2_is_opk && !app1_is_opk) { |
766 | return true; |
767 | } |
768 | return a->getTitle().compare(b->getTitle()) < 0; |
769 | } |
770 | |
771 | void Menu::orderLinks() |
772 | { |
773 | for (auto& section : links) { |
774 | sort(section.begin(), section.end(), compare_links); |
775 | } |
776 | } |
777 | |
778 | void Menu::readLinks() |
779 | { |
780 | iLink = 0; |
781 | iFirstDispRow = 0; |
782 | |
783 | for (uint i=0; i<links.size(); i++) { |
784 | links[i].clear(); |
785 | |
786 | int correct = (i>sections.size() ? iSection : i); |
787 | string const& section = sections[correct]; |
788 | |
789 | readLinksOfSection( |
790 | links[i], GMENU2X_SYSTEM_DIR "/sections/" + section, false); |
791 | readLinksOfSection( |
792 | links[i], GMenu2X::getHome() + "/sections/" + section, true); |
793 | } |
794 | |
795 | orderLinks(); |
796 | } |
797 | |
798 | void Menu::readLinksOfSection( |
799 | vector<unique_ptr<Link>>& links, string const& path, bool deletable) |
800 | { |
801 | DIR *dirp = opendir(path.c_str()); |
802 | if (!dirp) return; |
803 | |
804 | while (struct dirent *dptr = readdir(dirp)) { |
805 | if (dptr->d_type != DT_REG) continue; |
806 | string linkfile = path + '/' + dptr->d_name; |
807 | |
808 | LinkApp *link = new LinkApp(gmenu2x, linkfile, deletable); |
809 | if (link->targetExists()) { |
810 | link->setSize( |
811 | gmenu2x.skinConfInt["linkWidth"], |
812 | gmenu2x.skinConfInt["linkHeight"]); |
813 | links.emplace_back(link); |
814 | } else { |
815 | delete link; |
816 | } |
817 | } |
818 | |
819 | closedir(dirp); |
820 | } |
821 |
Branches:
install_locations
master
opkrun
packages