Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | // FileSelector.cpp - GUIFileSelector object |
| 2 | |
| 3 | #include <linux/input.h> |
| 4 | #include <pthread.h> |
| 5 | #include <stdarg.h> |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | #include <fcntl.h> |
| 10 | #include <sys/reboot.h> |
| 11 | #include <sys/stat.h> |
| 12 | #include <sys/time.h> |
| 13 | #include <sys/mman.h> |
| 14 | #include <sys/types.h> |
| 15 | #include <sys/ioctl.h> |
| 16 | #include <time.h> |
| 17 | #include <unistd.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <dirent.h> |
| 20 | #include <ctype.h> |
| 21 | |
| 22 | #include <algorithm> |
| 23 | |
| 24 | extern "C" { |
| 25 | #include "../common.h" |
| 26 | #include "../roots.h" |
| 27 | #include "../minuitwrp/minui.h" |
| 28 | #include "../recovery_ui.h" |
| 29 | } |
| 30 | |
| 31 | #include "rapidxml.hpp" |
| 32 | #include "objects.hpp" |
| 33 | #include "../data.hpp" |
| 34 | |
| 35 | #define TW_FILESELECTOR_UP_A_LEVEL "(Up A Level)" |
| 36 | |
| 37 | #define SCROLLING_SPEED_DECREMENT 6 |
| 38 | #define SCROLLING_FLOOR 10 |
| 39 | #define SCROLLING_MULTIPLIER 6 |
| 40 | |
| 41 | int GUIFileSelector::mSortOrder = 0; |
| 42 | |
| 43 | GUIFileSelector::GUIFileSelector(xml_node<>* node) |
| 44 | { |
| 45 | xml_attribute<>* attr; |
| 46 | xml_node<>* child; |
| 47 | int header_separator_color_specified = 0, header_separator_height_specified = 0, header_text_color_specified = 0, header_background_color_specified = 0; |
| 48 | |
| 49 | mStart = mLineSpacing = startY = mFontHeight = mSeparatorH = scrollingY = scrollingSpeed = 0; |
| 50 | mIconWidth = mIconHeight = mFolderIconHeight = mFileIconHeight = mFolderIconWidth = mFileIconWidth = mHeaderIconHeight = mHeaderIconWidth = 0; |
| 51 | mHeaderSeparatorH = mLineHeight = mHeaderIsStatic = mHeaderH = actualLineHeight = 0; |
| 52 | mFolderIcon = mFileIcon = mBackground = mFont = mHeaderIcon = NULL; |
| 53 | mBackgroundX = mBackgroundY = mBackgroundW = mBackgroundH = 0; |
| 54 | mShowFolders = mShowFiles = mShowNavFolders = 1; |
| 55 | mUpdate = 0; |
| 56 | touchDebounce = 6; |
| 57 | mPathVar = "cwd"; |
| 58 | ConvertStrToColor("black", &mBackgroundColor); |
| 59 | ConvertStrToColor("black", &mHeaderBackgroundColor); |
| 60 | ConvertStrToColor("black", &mSeparatorColor); |
| 61 | ConvertStrToColor("black", &mHeaderSeparatorColor); |
| 62 | ConvertStrToColor("white", &mFontColor); |
| 63 | ConvertStrToColor("white", &mHeaderFontColor); |
| 64 | |
| 65 | // Load header text |
| 66 | child = node->first_node("header"); |
| 67 | if (child) |
| 68 | { |
| 69 | attr = child->first_attribute("icon"); |
| 70 | if (attr) |
| 71 | mHeaderIcon = PageManager::FindResource(attr->value()); |
| 72 | |
| 73 | attr = child->first_attribute("background"); |
| 74 | if (attr) |
| 75 | { |
| 76 | std::string color = attr->value(); |
| 77 | ConvertStrToColor(color, &mHeaderBackgroundColor); |
| 78 | header_background_color_specified = -1; |
| 79 | } |
| 80 | attr = child->first_attribute("textcolor"); |
| 81 | if (attr) |
| 82 | { |
| 83 | std::string color = attr->value(); |
| 84 | ConvertStrToColor(color, &mHeaderFontColor); |
| 85 | header_text_color_specified = -1; |
| 86 | } |
| 87 | attr = child->first_attribute("separatorcolor"); |
| 88 | if (attr) |
| 89 | { |
| 90 | std::string color = attr->value(); |
| 91 | ConvertStrToColor(color, &mHeaderSeparatorColor); |
| 92 | header_separator_color_specified = -1; |
| 93 | } |
| 94 | attr = child->first_attribute("separatorheight"); |
| 95 | if (attr) { |
| 96 | string parsevalue = gui_parse_text(attr->value()); |
| 97 | mHeaderSeparatorH = atoi(parsevalue.c_str()); |
| 98 | header_separator_height_specified = -1; |
| 99 | } |
| 100 | } |
| 101 | child = node->first_node("text"); |
| 102 | if (child) mHeaderText = child->value(); |
| 103 | |
| 104 | // Simple way to check for static state |
| 105 | mLastValue = gui_parse_text(mHeaderText); |
| 106 | if (mLastValue != mHeaderText) |
| 107 | mHeaderIsStatic = 0; |
| 108 | else |
| 109 | mHeaderIsStatic = -1; |
| 110 | |
| 111 | child = node->first_node("icon"); |
| 112 | if (child) |
| 113 | { |
| 114 | attr = child->first_attribute("folder"); |
| 115 | if (attr) |
| 116 | mFolderIcon = PageManager::FindResource(attr->value()); |
| 117 | attr = child->first_attribute("file"); |
| 118 | if (attr) |
| 119 | mFileIcon = PageManager::FindResource(attr->value()); |
| 120 | } |
| 121 | child = node->first_node("background"); |
| 122 | if (child) |
| 123 | { |
| 124 | attr = child->first_attribute("resource"); |
| 125 | if (attr) |
| 126 | mBackground = PageManager::FindResource(attr->value()); |
| 127 | attr = child->first_attribute("color"); |
| 128 | if (attr) |
| 129 | { |
| 130 | std::string color = attr->value(); |
| 131 | ConvertStrToColor(color, &mBackgroundColor); |
| 132 | if (!header_background_color_specified) |
| 133 | ConvertStrToColor(color, &mHeaderBackgroundColor); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // Load the placement |
| 138 | LoadPlacement(node->first_node("placement"), &mRenderX, &mRenderY, &mRenderW, &mRenderH); |
| 139 | SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 140 | |
| 141 | // Load the font, and possibly override the color |
| 142 | child = node->first_node("font"); |
| 143 | if (child) |
| 144 | { |
| 145 | attr = child->first_attribute("resource"); |
| 146 | if (attr) |
| 147 | mFont = PageManager::FindResource(attr->value()); |
| 148 | |
| 149 | attr = child->first_attribute("color"); |
| 150 | if (attr) |
| 151 | { |
| 152 | std::string color = attr->value(); |
| 153 | ConvertStrToColor(color, &mFontColor); |
| 154 | if (!header_text_color_specified) |
| 155 | ConvertStrToColor(color, &mHeaderFontColor); |
| 156 | } |
| 157 | |
| 158 | attr = child->first_attribute("spacing"); |
| 159 | if (attr) { |
| 160 | string parsevalue = gui_parse_text(attr->value()); |
| 161 | mLineSpacing = atoi(parsevalue.c_str()); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // Load the separator if it exists |
| 166 | child = node->first_node("separator"); |
| 167 | if (child) |
| 168 | { |
| 169 | attr = child->first_attribute("color"); |
| 170 | if (attr) |
| 171 | { |
| 172 | std::string color = attr->value(); |
| 173 | ConvertStrToColor(color, &mSeparatorColor); |
| 174 | if (!header_separator_color_specified) |
| 175 | ConvertStrToColor(color, &mHeaderSeparatorColor); |
| 176 | } |
| 177 | |
| 178 | attr = child->first_attribute("height"); |
| 179 | if (attr) { |
| 180 | string parsevalue = gui_parse_text(attr->value()); |
| 181 | mSeparatorH = atoi(parsevalue.c_str()); |
| 182 | if (!header_separator_height_specified) |
| 183 | mHeaderSeparatorH = mSeparatorH; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | child = node->first_node("filter"); |
| 188 | if (child) |
| 189 | { |
| 190 | attr = child->first_attribute("extn"); |
| 191 | if (attr) |
| 192 | mExtn = attr->value(); |
| 193 | attr = child->first_attribute("folders"); |
| 194 | if (attr) |
| 195 | mShowFolders = atoi(attr->value()); |
| 196 | attr = child->first_attribute("files"); |
| 197 | if (attr) |
| 198 | mShowFiles = atoi(attr->value()); |
| 199 | attr = child->first_attribute("nav"); |
| 200 | if (attr) |
| 201 | mShowNavFolders = atoi(attr->value()); |
| 202 | } |
| 203 | |
| 204 | // Handle the path variable |
| 205 | child = node->first_node("path"); |
| 206 | if (child) |
| 207 | { |
| 208 | attr = child->first_attribute("name"); |
| 209 | if (attr) |
| 210 | mPathVar = attr->value(); |
| 211 | attr = child->first_attribute("default"); |
| 212 | if (attr) |
| 213 | DataManager::SetValue(mPathVar, attr->value()); |
| 214 | } |
| 215 | |
| 216 | // Handle the result variable |
| 217 | child = node->first_node("data"); |
| 218 | if (child) |
| 219 | { |
| 220 | attr = child->first_attribute("name"); |
| 221 | if (attr) |
| 222 | mVariable = attr->value(); |
| 223 | attr = child->first_attribute("default"); |
| 224 | if (attr) |
| 225 | DataManager::SetValue(mVariable, attr->value()); |
| 226 | } |
| 227 | |
| 228 | // Handle the sort variable |
| 229 | child = node->first_node("sort"); |
| 230 | if (child) |
| 231 | { |
| 232 | attr = child->first_attribute("name"); |
| 233 | if (attr) |
| 234 | mSortVariable = attr->value(); |
| 235 | attr = child->first_attribute("default"); |
| 236 | if (attr) |
| 237 | DataManager::SetValue(mSortVariable, attr->value()); |
| 238 | |
| 239 | DataManager::GetValue(mSortVariable, mSortOrder); |
| 240 | } |
| 241 | |
| 242 | // Handle the selection variable |
| 243 | child = node->first_node("selection"); |
| 244 | if (child) |
| 245 | { |
| 246 | attr = child->first_attribute("name"); |
| 247 | if (attr) |
| 248 | mSelection = attr->value(); |
| 249 | else |
| 250 | mSelection = "0"; |
| 251 | } else |
| 252 | mSelection = "0"; |
| 253 | |
| 254 | // Retrieve the line height |
| 255 | gr_getFontDetails(mFont ? mFont->GetResource() : NULL, &mFontHeight, NULL); |
| 256 | mLineHeight = mFontHeight; |
| 257 | mHeaderH = mFontHeight; |
| 258 | |
| 259 | if (mFolderIcon && mFolderIcon->GetResource()) |
| 260 | { |
| 261 | mFolderIconWidth = gr_get_width(mFolderIcon->GetResource()); |
| 262 | mFolderIconHeight = gr_get_height(mFolderIcon->GetResource()); |
| 263 | if (mFolderIconHeight > (int)mLineHeight) |
| 264 | mLineHeight = mFolderIconHeight; |
| 265 | mIconWidth = mFolderIconWidth; |
| 266 | } |
| 267 | |
| 268 | if (mFileIcon && mFileIcon->GetResource()) |
| 269 | { |
| 270 | mFileIconWidth = gr_get_width(mFileIcon->GetResource()); |
| 271 | mFileIconHeight = gr_get_height(mFileIcon->GetResource()); |
| 272 | if (mFileIconHeight > (int)mLineHeight) |
| 273 | mLineHeight = mFileIconHeight; |
| 274 | if (mFileIconWidth > mIconWidth) |
| 275 | mIconWidth = mFileIconWidth; |
| 276 | } |
| 277 | |
| 278 | if (mHeaderIcon && mHeaderIcon->GetResource()) |
| 279 | { |
| 280 | mHeaderIconWidth = gr_get_width(mHeaderIcon->GetResource()); |
| 281 | mHeaderIconHeight = gr_get_height(mHeaderIcon->GetResource()); |
| 282 | if (mHeaderIconHeight > mHeaderH) |
| 283 | mHeaderH = mHeaderIconHeight; |
| 284 | if (mHeaderIconWidth > mIconWidth) |
| 285 | mIconWidth = mHeaderIconWidth; |
| 286 | } |
| 287 | |
| 288 | mHeaderH += mLineSpacing + mHeaderSeparatorH; |
| 289 | actualLineHeight = mLineHeight + mLineSpacing + mSeparatorH; |
| 290 | if (mHeaderH < actualLineHeight) |
| 291 | mHeaderH = actualLineHeight; |
| 292 | |
| 293 | if (actualLineHeight / 3 > 6) |
| 294 | touchDebounce = actualLineHeight / 3; |
| 295 | |
| 296 | if (mBackground && mBackground->GetResource()) |
| 297 | { |
| 298 | mBackgroundW = gr_get_width(mBackground->GetResource()); |
| 299 | mBackgroundH = gr_get_height(mBackground->GetResource()); |
| 300 | } |
| 301 | |
| 302 | // Fetch the file/folder list |
| 303 | std::string value; |
| 304 | DataManager::GetValue(mPathVar, value); |
| 305 | if (GetFileList(value) != 0 && (mShowNavFolders != 0 || mShowFiles != 0)) { |
| 306 | GetFileList(DataManager::GetCurrentStoragePath()); |
| 307 | DataManager::SetValue(mPathVar, DataManager::GetCurrentStoragePath()); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | GUIFileSelector::~GUIFileSelector() |
| 312 | { |
| 313 | } |
| 314 | |
| 315 | int GUIFileSelector::Render(void) |
| 316 | { |
| 317 | // First step, fill background |
| 318 | gr_color(mBackgroundColor.red, mBackgroundColor.green, mBackgroundColor.blue, 255); |
| 319 | gr_fill(mRenderX, mRenderY + mHeaderH, mRenderW, mRenderH - mHeaderH); |
| 320 | |
| 321 | // Next, render the background resource (if it exists) |
| 322 | if (mBackground && mBackground->GetResource()) |
| 323 | { |
| 324 | mBackgroundX = mRenderX + ((mRenderW - mBackgroundW) / 2); |
| 325 | mBackgroundY = mRenderY + ((mRenderH - mBackgroundH) / 2); |
| 326 | gr_blit(mBackground->GetResource(), 0, 0, mBackgroundW, mBackgroundH, mBackgroundX, mBackgroundY); |
| 327 | } |
| 328 | |
| 329 | // This tells us how many lines we can actually render |
| 330 | int lines = (mRenderH - mHeaderH) / (actualLineHeight); |
| 331 | int line; |
| 332 | |
| 333 | int folderSize = mShowFolders ? mFolderList.size() : 0; |
| 334 | int fileSize = mShowFiles ? mFileList.size() : 0; |
| 335 | |
| 336 | if (folderSize + fileSize < lines) { |
| 337 | lines = folderSize + fileSize; |
| 338 | scrollingY = 0; |
| 339 | } else { |
| 340 | lines++; |
| 341 | if (lines < folderSize + fileSize) |
| 342 | lines++; |
| 343 | } |
| 344 | |
| 345 | void* fontResource = NULL; |
| 346 | if (mFont) fontResource = mFont->GetResource(); |
| 347 | |
| 348 | int yPos = mRenderY + mHeaderH + scrollingY; |
| 349 | int fontOffsetY = (int)((actualLineHeight - mFontHeight) / 2); |
| 350 | int currentIconHeight = 0, currentIconWidth = 0; |
| 351 | int currentIconOffsetY = 0, currentIconOffsetX = 0; |
| 352 | int folderIconOffsetY = (int)((actualLineHeight - mFolderIconHeight) / 2), fileIconOffsetY = (int)((actualLineHeight - mFileIconHeight) / 2); |
| 353 | int folderIconOffsetX = (mIconWidth - mFolderIconWidth) / 2, fileIconOffsetX = (mIconWidth - mFileIconWidth) / 2; |
| 354 | |
| 355 | for (line = 0; line < lines; line++) |
| 356 | { |
| 357 | Resource* icon; |
| 358 | std::string label; |
| 359 | |
| 360 | // Set the color for the font |
| 361 | gr_color(mFontColor.red, mFontColor.green, mFontColor.blue, 255); |
| 362 | |
| 363 | if (line + mStart < folderSize) |
| 364 | { |
| 365 | icon = mFolderIcon; |
| 366 | label = mFolderList.at(line + mStart).fileName; |
| 367 | currentIconHeight = mFolderIconHeight; |
| 368 | currentIconWidth = mFolderIconWidth; |
| 369 | currentIconOffsetY = folderIconOffsetY; |
| 370 | currentIconOffsetX = folderIconOffsetX; |
| 371 | } |
| 372 | else if (line + mStart < folderSize + fileSize) |
| 373 | { |
| 374 | icon = mFileIcon; |
| 375 | label = mFileList.at((line + mStart) - folderSize).fileName; |
| 376 | currentIconHeight = mFileIconHeight; |
| 377 | currentIconWidth = mFileIconWidth; |
| 378 | currentIconOffsetY = fileIconOffsetY; |
| 379 | currentIconOffsetX = fileIconOffsetX; |
| 380 | } else { |
| 381 | continue; |
| 382 | } |
| 383 | |
| 384 | if (icon && icon->GetResource()) |
| 385 | { |
| 386 | int rect_y = 0, image_y = (yPos + currentIconOffsetY); |
| 387 | if (image_y + currentIconHeight > mRenderY + mRenderH) |
| 388 | rect_y = mRenderY + mRenderH - image_y; |
| 389 | else |
| 390 | rect_y = currentIconHeight; |
| 391 | gr_blit(icon->GetResource(), 0, 0, currentIconWidth, rect_y, mRenderX + currentIconOffsetX, image_y); |
| 392 | } |
| 393 | gr_textExWH(mRenderX + mIconWidth + 5, yPos + fontOffsetY, label.c_str(), fontResource, mRenderX + mRenderW, mRenderY + mRenderH); |
| 394 | |
| 395 | // Add the separator |
| 396 | if (yPos + actualLineHeight < mRenderH + mRenderY) { |
| 397 | gr_color(mSeparatorColor.red, mSeparatorColor.green, mSeparatorColor.blue, 255); |
| 398 | gr_fill(mRenderX, yPos + actualLineHeight - mSeparatorH, mRenderW, mSeparatorH); |
| 399 | } |
| 400 | |
| 401 | // Move the yPos |
| 402 | yPos += actualLineHeight; |
| 403 | } |
| 404 | |
| 405 | // Render the Header (last so that it overwrites the top most row for per pixel scrolling) |
| 406 | // First step, fill background |
| 407 | gr_color(mHeaderBackgroundColor.red, mHeaderBackgroundColor.green, mHeaderBackgroundColor.blue, 255); |
| 408 | gr_fill(mRenderX, mRenderY, mRenderW, mHeaderH); |
| 409 | |
| 410 | // Now, we need the header (icon + text) |
| 411 | yPos = mRenderY; |
| 412 | { |
| 413 | Resource* headerIcon; |
| 414 | int mIconOffsetX = 0; |
| 415 | |
| 416 | // render the icon if it exists |
| 417 | headerIcon = mHeaderIcon; |
| 418 | if (headerIcon && headerIcon->GetResource()) |
| 419 | { |
| 420 | gr_blit(headerIcon->GetResource(), 0, 0, mHeaderIconWidth, mHeaderIconHeight, mRenderX + ((mHeaderIconWidth - mIconWidth) / 2), (yPos + (int)((mHeaderH - mHeaderIconHeight) / 2))); |
| 421 | mIconOffsetX = mIconWidth; |
| 422 | } |
| 423 | |
| 424 | // render the text |
| 425 | gr_color(mHeaderFontColor.red, mHeaderFontColor.green, mHeaderFontColor.blue, 255); |
| 426 | gr_textExWH(mRenderX + mIconOffsetX + 5, yPos + (int)((mHeaderH - mFontHeight) / 2), mLastValue.c_str(), fontResource, mRenderX + mRenderW, mRenderY + mRenderH); |
| 427 | |
| 428 | // Add the separator |
| 429 | gr_color(mHeaderSeparatorColor.red, mHeaderSeparatorColor.green, mHeaderSeparatorColor.blue, 255); |
| 430 | gr_fill(mRenderX, yPos + mHeaderH - mHeaderSeparatorH, mRenderW, mHeaderSeparatorH); |
| 431 | } |
| 432 | |
| 433 | mUpdate = 0; |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | int GUIFileSelector::Update(void) |
| 438 | { |
| 439 | if (!mHeaderIsStatic) { |
| 440 | std::string newValue = gui_parse_text(mHeaderText); |
| 441 | if (mLastValue != newValue) { |
| 442 | mLastValue = newValue; |
| 443 | mUpdate = 1; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | if (mUpdate) |
| 448 | { |
| 449 | mUpdate = 0; |
| 450 | if (Render() == 0) |
| 451 | return 2; |
| 452 | } |
| 453 | |
| 454 | // Handle kinetic scrolling |
| 455 | if (scrollingSpeed == 0) { |
| 456 | // Do nothing |
| 457 | } else if (scrollingSpeed > 0) { |
| 458 | if (scrollingSpeed < ((int) (actualLineHeight * 2.5))) { |
| 459 | scrollingY += scrollingSpeed; |
| 460 | scrollingSpeed -= SCROLLING_SPEED_DECREMENT; |
| 461 | } else { |
| 462 | scrollingY += ((int) (actualLineHeight * 2.5)); |
| 463 | scrollingSpeed -= SCROLLING_SPEED_DECREMENT; |
| 464 | } |
| 465 | while (mStart && scrollingY > 0) { |
| 466 | mStart--; |
| 467 | scrollingY -= actualLineHeight; |
| 468 | } |
| 469 | if (mStart == 0 && scrollingY > 0) { |
| 470 | scrollingY = 0; |
| 471 | scrollingSpeed = 0; |
| 472 | } else if (scrollingSpeed < SCROLLING_FLOOR) |
| 473 | scrollingSpeed = 0; |
| 474 | mUpdate = 1; |
| 475 | } else if (scrollingSpeed < 0) { |
| 476 | int totalSize = (mShowFolders ? mFolderList.size() : 0) + (mShowFiles ? mFileList.size() : 0); |
| 477 | int lines = (mRenderH - mHeaderH) / (actualLineHeight); |
| 478 | |
| 479 | if (totalSize > lines) { |
| 480 | int bottom_offset = ((int)(mRenderH) - mHeaderH) - (lines * actualLineHeight); |
| 481 | |
| 482 | bottom_offset -= actualLineHeight; |
| 483 | |
| 484 | if (abs(scrollingSpeed) < ((int) (actualLineHeight * 2.5))) { |
| 485 | scrollingY += scrollingSpeed; |
| 486 | scrollingSpeed += SCROLLING_SPEED_DECREMENT; |
| 487 | } else { |
| 488 | scrollingY -= ((int) (actualLineHeight * 2.5)); |
| 489 | scrollingSpeed += SCROLLING_SPEED_DECREMENT; |
| 490 | } |
| 491 | while (mStart + lines + (bottom_offset ? 1 : 0) < totalSize && abs(scrollingY) > actualLineHeight) { |
| 492 | mStart++; |
| 493 | scrollingY += actualLineHeight; |
| 494 | } |
| 495 | if (bottom_offset != 0 && mStart + lines + 1 >= totalSize && scrollingY <= bottom_offset) { |
| 496 | mStart = totalSize - lines - 1; |
| 497 | scrollingY = bottom_offset; |
| 498 | } else if (mStart + lines >= totalSize && scrollingY < 0) { |
| 499 | mStart = totalSize - lines; |
| 500 | scrollingY = 0; |
| 501 | } else if (scrollingSpeed * -1 < SCROLLING_FLOOR) |
| 502 | scrollingSpeed = 0; |
| 503 | mUpdate = 1; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | int GUIFileSelector::GetSelection(int x, int y) |
| 511 | { |
| 512 | // We only care about y position |
| 513 | if (y < mRenderY || y - mRenderY <= mHeaderH || y - mRenderY > mRenderH) return -1; |
| 514 | return (y - mRenderY - mHeaderH); |
| 515 | } |
| 516 | |
| 517 | int GUIFileSelector::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 518 | { |
| 519 | static int startSelection = -1; |
| 520 | static int lastY = 0, last2Y = 0; |
| 521 | int selection = 0; |
| 522 | |
| 523 | switch (state) |
| 524 | { |
| 525 | case TOUCH_START: |
| 526 | if (scrollingSpeed != 0) |
| 527 | startSelection = -1; |
| 528 | else |
| 529 | startSelection = GetSelection(x,y); |
| 530 | startY = lastY = last2Y = y; |
| 531 | scrollingSpeed = 0; |
| 532 | break; |
| 533 | |
| 534 | case TOUCH_DRAG: |
| 535 | // Check if we dragged out of the selection window |
| 536 | if (GetSelection(x, y) == -1) { |
| 537 | last2Y = lastY = 0; |
| 538 | break; |
| 539 | } |
| 540 | |
| 541 | // Provide some debounce on initial touches |
| 542 | if (startSelection != -1 && abs(y - startY) < touchDebounce) { |
| 543 | break; |
| 544 | } |
| 545 | |
| 546 | last2Y = lastY; |
| 547 | lastY = y; |
| 548 | startSelection = -1; |
| 549 | |
| 550 | // Handle scrolling |
| 551 | scrollingY += y - startY; |
| 552 | startY = y; |
| 553 | while(mStart && scrollingY > 0) { |
| 554 | mStart--; |
| 555 | scrollingY -= actualLineHeight; |
| 556 | } |
| 557 | if (mStart == 0 && scrollingY > 0) |
| 558 | scrollingY = 0; |
| 559 | { |
| 560 | int totalSize = (mShowFolders ? mFolderList.size() : 0) + (mShowFiles ? mFileList.size() : 0); |
| 561 | int lines = (mRenderH - mHeaderH) / (actualLineHeight); |
| 562 | |
| 563 | if (totalSize > lines) { |
| 564 | int bottom_offset = ((int)(mRenderH) - mHeaderH) - (lines * actualLineHeight); |
| 565 | |
| 566 | bottom_offset -= actualLineHeight; |
| 567 | |
| 568 | while (mStart + lines + (bottom_offset ? 1 : 0) < totalSize && abs(scrollingY) > actualLineHeight) { |
| 569 | mStart++; |
| 570 | scrollingY += actualLineHeight; |
| 571 | } |
| 572 | if (bottom_offset != 0 && mStart + lines + 1 >= totalSize && scrollingY <= bottom_offset) { |
| 573 | mStart = totalSize - lines - 1; |
| 574 | scrollingY = bottom_offset; |
| 575 | } else if (mStart + lines >= totalSize && scrollingY < 0) { |
| 576 | mStart = totalSize - lines; |
| 577 | scrollingY = 0; |
| 578 | } |
| 579 | } else |
| 580 | scrollingY = 0; |
| 581 | } |
| 582 | mUpdate = 1; |
| 583 | break; |
| 584 | |
| 585 | case TOUCH_RELEASE: |
| 586 | if (startSelection >= 0) |
| 587 | { |
| 588 | // We've selected an item! |
| 589 | std::string str; |
| 590 | |
| 591 | int folderSize = mShowFolders ? mFolderList.size() : 0; |
| 592 | int fileSize = mShowFiles ? mFileList.size() : 0; |
| 593 | int selectY = scrollingY, actualSelection = mStart; |
| 594 | |
| 595 | // Move the selection to the proper place in the array |
| 596 | while (selectY + actualLineHeight < startSelection) { |
| 597 | selectY += actualLineHeight; |
| 598 | actualSelection++; |
| 599 | } |
| 600 | startSelection = actualSelection; |
| 601 | |
| 602 | if (startSelection < folderSize + fileSize) |
| 603 | { |
| 604 | if (startSelection < folderSize) |
| 605 | { |
| 606 | std::string oldcwd; |
| 607 | std::string cwd; |
| 608 | |
| 609 | str = mFolderList.at(startSelection).fileName; |
| 610 | if (mSelection != "0") |
| 611 | DataManager::SetValue(mSelection, str); |
| 612 | DataManager::GetValue(mPathVar, cwd); |
| 613 | |
| 614 | oldcwd = cwd; |
| 615 | // Ignore requests to do nothing |
| 616 | if (str == ".") return 0; |
| 617 | if (str == TW_FILESELECTOR_UP_A_LEVEL) |
| 618 | { |
| 619 | if (cwd != "/") |
| 620 | { |
| 621 | size_t found; |
| 622 | found = cwd.find_last_of('/'); |
| 623 | cwd = cwd.substr(0,found); |
| 624 | |
| 625 | if (cwd.length() < 2) cwd = "/"; |
| 626 | } |
| 627 | } |
| 628 | else |
| 629 | { |
| 630 | // Add a slash if we're not the root folder |
| 631 | if (cwd != "/") cwd += "/"; |
| 632 | cwd += str; |
| 633 | } |
| 634 | |
| 635 | if (mShowNavFolders == 0 && mShowFiles == 0) |
| 636 | { |
| 637 | // This is a "folder" selection |
| 638 | DataManager::SetValue(mVariable, cwd); |
| 639 | } |
| 640 | else |
| 641 | { |
| 642 | DataManager::SetValue(mPathVar, cwd); |
| 643 | if (GetFileList(cwd) != 0) |
| 644 | { |
| 645 | LOGE("Unable to change folders.\n"); |
| 646 | DataManager::SetValue(mPathVar, oldcwd); |
| 647 | GetFileList(oldcwd); |
| 648 | } |
| 649 | mStart = 0; |
| 650 | scrollingY = 0; |
| 651 | mUpdate = 1; |
| 652 | } |
| 653 | } |
| 654 | else if (!mVariable.empty()) |
| 655 | { |
| 656 | str = mFileList.at(startSelection - folderSize).fileName; |
| 657 | if (mSelection != "0") |
| 658 | DataManager::SetValue(mSelection, str); |
| 659 | |
| 660 | std::string cwd; |
| 661 | DataManager::GetValue(mPathVar, cwd); |
| 662 | if (cwd != "/") cwd += "/"; |
| 663 | DataManager::SetValue(mVariable, cwd + str); |
| 664 | } |
| 665 | } |
| 666 | } else { |
| 667 | // This is for kinetic scrolling |
| 668 | scrollingSpeed = lastY - last2Y; |
| 669 | if (abs(scrollingSpeed) > SCROLLING_FLOOR) |
| 670 | scrollingSpeed *= SCROLLING_MULTIPLIER; |
| 671 | else |
| 672 | scrollingSpeed = 0; |
| 673 | } |
| 674 | case TOUCH_REPEAT: |
| 675 | case TOUCH_HOLD: |
| 676 | break; |
| 677 | } |
| 678 | return 0; |
| 679 | } |
| 680 | |
| 681 | int GUIFileSelector::NotifyVarChange(std::string varName, std::string value) |
| 682 | { |
| 683 | if (varName.empty()) |
| 684 | { |
| 685 | // Always clear the data variable so we know to use it |
| 686 | DataManager::SetValue(mVariable, ""); |
| 687 | } |
| 688 | if (!mHeaderIsStatic) { |
| 689 | std::string newValue = gui_parse_text(mHeaderText); |
| 690 | if (mLastValue != newValue) { |
| 691 | mLastValue = newValue; |
| 692 | mStart = 0; |
| 693 | scrollingY = 0; |
| 694 | scrollingSpeed = 0; |
| 695 | mUpdate = 1; |
| 696 | } |
| 697 | } |
| 698 | if (varName == mPathVar || varName == mSortVariable) |
| 699 | { |
| 700 | DataManager::GetValue(mPathVar, value); // sometimes the value will be the sort order instead of the path, so we read the path everytime |
| 701 | DataManager::GetValue(mSortVariable, mSortOrder); |
| 702 | mStart = 0; |
| 703 | scrollingY = 0; |
| 704 | scrollingSpeed = 0; |
| 705 | GetFileList(value); |
| 706 | mUpdate = 1; |
| 707 | return 0; |
| 708 | } |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | int GUIFileSelector::SetRenderPos(int x, int y, int w /* = 0 */, int h /* = 0 */) |
| 713 | { |
| 714 | mRenderX = x; |
| 715 | mRenderY = y; |
| 716 | if (w || h) |
| 717 | { |
| 718 | mRenderW = w; |
| 719 | mRenderH = h; |
| 720 | } |
| 721 | SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 722 | mUpdate = 1; |
| 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | bool GUIFileSelector::fileSort(FileData d1, FileData d2) |
| 727 | { |
| 728 | if (d1.fileName == ".") |
| 729 | return -1; |
| 730 | if (d2.fileName == ".") |
| 731 | return 0; |
| 732 | if (d1.fileName == TW_FILESELECTOR_UP_A_LEVEL) |
| 733 | return -1; |
| 734 | if (d2.fileName == TW_FILESELECTOR_UP_A_LEVEL) |
| 735 | return 0; |
| 736 | |
| 737 | switch (mSortOrder) { |
| 738 | case 3: // by size largest first |
| 739 | if (d1.fileSize == d2.fileSize || d1.fileType == DT_DIR) // some directories report a different size than others - but this is not the size of the files inside the directory, so we just sort by name on directories |
| 740 | return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) < 0); |
| 741 | return d1.fileSize > d2.fileSize; |
| 742 | case -3: // by size smallest first |
| 743 | if (d1.fileSize == d2.fileSize || d1.fileType == DT_DIR) // some directories report a different size than others - but this is not the size of the files inside the directory, so we just sort by name on directories |
| 744 | return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0); |
| 745 | return d1.fileSize < d2.fileSize; |
| 746 | case 2: // by last modified date newest first |
| 747 | if (d1.lastModified == d2.lastModified) |
| 748 | return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) < 0); |
| 749 | return d1.lastModified > d2.lastModified; |
| 750 | case -2: // by date oldest first |
| 751 | if (d1.lastModified == d2.lastModified) |
| 752 | return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0); |
| 753 | return d1.lastModified < d2.lastModified; |
| 754 | case -1: // by name descending |
| 755 | return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0); |
| 756 | default: // should be a 1 - sort by name ascending |
| 757 | return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) < 0); |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | int GUIFileSelector::GetFileList(const std::string folder) |
| 762 | { |
| 763 | DIR* d; |
| 764 | struct dirent* de; |
| 765 | struct stat st; |
| 766 | |
| 767 | // Clear all data |
| 768 | mFolderList.clear(); |
| 769 | mFileList.clear(); |
| 770 | |
| 771 | d = opendir(folder.c_str()); |
| 772 | if (d == NULL) |
| 773 | { |
| 774 | LOGI("Unable to open '%s'\n", folder.c_str()); |
| 775 | return -1; |
| 776 | } |
| 777 | |
| 778 | while ((de = readdir(d)) != NULL) |
| 779 | { |
| 780 | FileData data; |
| 781 | |
| 782 | data.fileName = de->d_name; |
| 783 | if (data.fileName == ".") |
| 784 | continue; |
| 785 | if (data.fileName == ".." && folder == "/") |
| 786 | continue; |
| 787 | if (data.fileName == "..") |
| 788 | data.fileName = TW_FILESELECTOR_UP_A_LEVEL; |
| 789 | data.fileType = de->d_type; |
| 790 | |
| 791 | std::string path = folder + "/" + data.fileName; |
| 792 | stat(path.c_str(), &st); |
| 793 | data.protection = st.st_mode; |
| 794 | data.userId = st.st_uid; |
| 795 | data.groupId = st.st_gid; |
| 796 | data.fileSize = st.st_size; |
| 797 | data.lastAccess = st.st_atime; |
| 798 | data.lastModified = st.st_mtime; |
| 799 | data.lastStatChange = st.st_ctime; |
| 800 | |
| 801 | if (data.fileType == DT_DIR) |
| 802 | { |
| 803 | if (mShowNavFolders || (data.fileName != "." && data.fileName != TW_FILESELECTOR_UP_A_LEVEL)) |
| 804 | mFolderList.push_back(data); |
| 805 | } |
Dees_Troy | af4d0ce | 2012-09-26 20:19:06 -0400 | [diff] [blame] | 806 | else if (data.fileType == DT_REG || data.fileType == DT_LNK || data.fileType == DT_BLK) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 807 | { |
| 808 | if (mExtn.empty() || (data.fileName.length() > mExtn.length() && data.fileName.substr(data.fileName.length() - mExtn.length()) == mExtn)) |
| 809 | { |
| 810 | mFileList.push_back(data); |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | closedir(d); |
| 815 | |
| 816 | std::sort(mFolderList.begin(), mFolderList.end(), fileSort); |
| 817 | std::sort(mFileList.begin(), mFileList.end(), fileSort); |
| 818 | return 0; |
| 819 | } |
| 820 | |
| 821 | void GUIFileSelector::SetPageFocus(int inFocus) |
| 822 | { |
| 823 | if (inFocus) |
| 824 | { |
| 825 | std::string value; |
| 826 | DataManager::GetValue(mPathVar, value); |
| 827 | if (GetFileList(value) != 0 && (mShowNavFolders != 0 || mShowFiles != 0)) { |
| 828 | GetFileList(DataManager::GetCurrentStoragePath()); |
| 829 | DataManager::SetValue(mPathVar, DataManager::GetCurrentStoragePath()); |
| 830 | } |
| 831 | } |
| 832 | } |
| 833 | |