Interface.cpp (17656B)
1 #include "Interface.hpp" 2 #include <iostream> 3 #include <fstream> 4 #include <SDL/SDL_image.h> 5 #include <sstream> 6 7 Interface::Interface() 8 :sonX(0) 9 ,sonY(0) 10 ,sonScroll(0) 11 ,sonMenu(0) 12 ,sonEcran(NULL) 13 ,sonArrierePlan(NULL) 14 ,sonFondMessage(NULL) 15 ,sonFondMenu(NULL) 16 ,sonMenuSelectionne(NULL) 17 ,sonIconeDossier(NULL) 18 ,sonIconeFichier(NULL) 19 ,sonIconeInconnu(NULL) 20 ,saPolice(NULL) 21 { 22 23 if (SDL_Init(SDL_INIT_VIDEO) == -1) 24 { 25 std::cerr << "SDL_Init(): " << SDL_GetError() << std::endl; 26 exit(EXIT_FAILURE); 27 } 28 29 sonEcran = SDL_SetVideoMode(320, 240, 16, SDL_SWSURFACE); 30 31 if (sonEcran == NULL) 32 { 33 std::cerr << "SDL_SetVideoMode(): " << SDL_GetError() << std::endl; 34 exit(EXIT_FAILURE); 35 } 36 37 SDL_EnableKeyRepeat(500, 100); 38 SDL_ShowCursor(SDL_DISABLE); 39 atexit(SDL_Quit); 40 SDL_Surface * lEcranNoir = SDL_CreateRGBSurface(SDL_SWSURFACE, 320, 240, 16, 0, 0, 0, 0); 41 SDL_Surface * leBootScreen = chargerImage(sonExplorateur.getSaValeur("boot-image")); 42 43 for (int i = SDL_ALPHA_OPAQUE; i != SDL_ALPHA_TRANSPARENT; i = i - 15) 44 { 45 SDL_SetAlpha(lEcranNoir, SDL_SRCALPHA, i); 46 47 if (SDL_BlitSurface(leBootScreen, NULL, sonEcran, NULL) == -1) 48 { 49 std::cerr << "SDL_BlitSurface(): " << SDL_GetError() << std::endl; 50 exit(EXIT_FAILURE); 51 } 52 53 if (SDL_BlitSurface(lEcranNoir, NULL, sonEcran, NULL) == -1) 54 { 55 std::cerr << "SDL_BlitSurface(): " << SDL_GetError() << std::endl; 56 exit(EXIT_FAILURE); 57 } 58 59 if (SDL_Flip(sonEcran) == -1) 60 { 61 std::cerr << "SDL_Flip(): " << SDL_GetError() << std::endl; 62 exit(EXIT_FAILURE); 63 } 64 65 } 66 67 SDL_FreeSurface(lEcranNoir); 68 69 if (TTF_Init() == -1) 70 { 71 std::cerr << "TTF_Init(): " << TTF_GetError() << std::endl; 72 exit(EXIT_FAILURE); 73 } 74 75 saPolice = TTF_OpenFont(sonExplorateur.getSaValeur("font-family").c_str(), atoi(sonExplorateur.getSaValeur("font-size").c_str())); 76 77 if (saPolice == NULL) 78 { 79 std::cerr << "TTF_OpenFont(): " << TTF_GetError() << std::endl; 80 exit(EXIT_FAILURE); 81 } 82 83 sonBlanc.r = 255; 84 sonBlanc.g = 255; 85 sonBlanc.b = 255; 86 sonNoir.r = 0; 87 sonNoir.g = 0; 88 sonNoir.b = 0; 89 sonFocus.r = atoi(sonExplorateur.getSaValeur("focus-r").c_str()); 90 sonFocus.g = atoi(sonExplorateur.getSaValeur("focus-g").c_str()); 91 sonFocus.b = atoi(sonExplorateur.getSaValeur("focus-b").c_str()); 92 std::ifstream lesIcones("icons.txt"); 93 std::string lExtension, lIcone; 94 95 while (lesIcones.good() == true) 96 { 97 getline(lesIcones, lExtension, '\t'); 98 getline(lesIcones, lIcone, '\n'); 99 100 if (lExtension == "" || lIcone == "" || sesIcones.insert(std::make_pair(lExtension, chargerImage(lIcone))).second == false) 101 { 102 sonExplorateur.ajouterErreur("The icons.txt file is corrupted."); 103 } 104 105 } 106 107 lesIcones.close(); 108 sonArrierePlan = chargerImage(sonExplorateur.getSaValeur("background-image")); 109 sonFondMessage = chargerImage(sonExplorateur.getSaValeur("foreground-image")); 110 sonFondMenu = chargerImage(sonExplorateur.getSaValeur("menu-background")); 111 sonMenuSelectionne = chargerImage(sonExplorateur.getSaValeur("menu-focus")); 112 sonIconeDossier = chargerImage(sonExplorateur.getSaValeur("folder-icon")); 113 sonIconeFichier = chargerImage(sonExplorateur.getSaValeur("file-icon")); 114 sonIconeInconnu = chargerImage(sonExplorateur.getSaValeur("unknown-icon")); 115 sonExplorateur.lireRepertoire(); 116 117 for (int i = SDL_ALPHA_OPAQUE; i != SDL_ALPHA_TRANSPARENT; i = i - 15) 118 { 119 SDL_SetAlpha(leBootScreen, SDL_SRCALPHA, i); 120 121 if (SDL_BlitSurface(sonArrierePlan, NULL, sonEcran, NULL) == -1) 122 { 123 std::cerr << "SDL_BlitSurface(): " << SDL_GetError() << std::endl; 124 exit(EXIT_FAILURE); 125 } 126 127 if (SDL_BlitSurface(leBootScreen, NULL, sonEcran, NULL) == -1) 128 { 129 std::cerr << "SDL_BlitSurface(): " << SDL_GetError() << std::endl; 130 exit(EXIT_FAILURE); 131 } 132 133 if (SDL_Flip(sonEcran) == -1) 134 { 135 std::cerr << "SDL_Flip(): " << SDL_GetError() << std::endl; 136 exit(EXIT_FAILURE); 137 } 138 139 } 140 141 SDL_FreeSurface(leBootScreen); 142 boucler(); 143 } 144 145 Interface::~Interface() 146 { 147 TTF_CloseFont(saPolice); 148 TTF_Quit(); 149 SDL_FreeSurface(sonArrierePlan); 150 SDL_FreeSurface(sonFondMessage); 151 SDL_FreeSurface(sonFondMenu); 152 SDL_FreeSurface(sonMenuSelectionne); 153 SDL_FreeSurface(sonIconeDossier); 154 SDL_FreeSurface(sonIconeFichier); 155 SDL_FreeSurface(sonIconeInconnu); 156 SDL_Quit(); 157 } 158 159 void Interface::boucler() 160 { 161 int nb_icons_x = atoi(sonExplorateur.getSaValeur("nb-icons-x").c_str()); 162 int nb_icons_y = atoi(sonExplorateur.getSaValeur("nb-icons-y").c_str()); 163 std::vector<std::vector<Element *> > leContenu = sonExplorateur.getSonContenu(); 164 bool b = true; 165 SDL_Event lEvenement; 166 167 while (b) 168 { 169 afficher(); 170 171 if (SDL_WaitEvent(&lEvenement)) 172 { 173 174 if (lEvenement.type == SDL_KEYDOWN) 175 { 176 177 if (sonExplorateur.getSesErreurs() != "") 178 { 179 180 if (lEvenement.key.keysym.sym == SDLK_LCTRL) 181 { 182 sonExplorateur.resetSesErreurs(); 183 } 184 185 } 186 187 else 188 { 189 190 if (sonMenu != 0) 191 { 192 193 switch (lEvenement.key.keysym.sym) 194 { 195 case SDLK_UP : 196 switch (sonMenu) 197 { 198 case 10 : 199 sonMenu = 6; 200 break; 201 case 1 : 202 sonMenu = 11; 203 break; 204 default : 205 sonMenu--; 206 } 207 break; 208 case SDLK_DOWN : 209 switch (sonMenu) 210 { 211 case 6 : 212 sonMenu = 10; 213 break; 214 case 11 : 215 sonMenu = 1; 216 break; 217 default : 218 sonMenu++; 219 } 220 break; 221 case SDLK_LCTRL : 222 switch (sonMenu) 223 { 224 case 1 : 225 switch (leContenu[sonY][sonX]->getSonType()) 226 { 227 case dossier : 228 sonExplorateur.changerRepertoire(leContenu[sonY][sonX]->getSonNom()); 229 sonScroll = 0; 230 sonX = 0; 231 sonY = 0; 232 sonExplorateur.lireRepertoire(); 233 leContenu = sonExplorateur.getSonContenu(); 234 break; 235 case fichier : 236 sonExplorateur.ouvrirFichier(leContenu[sonY][sonX], false); 237 break; 238 default : 239 sonExplorateur.ajouterErreur("Could not open the file."); 240 } 241 sonMenu = 0; 242 break; 243 case 2 : 244 if (leContenu[sonY][sonX]->getSonType() == fichier) 245 { 246 sonExplorateur.ouvrirFichier(leContenu[sonY][sonX], true); 247 sonMenu = 0; 248 } 249 break; 250 case 3 : 251 sonExplorateur.copier(leContenu[sonY][sonX]->getSonNom()); 252 sonMenu = 0; 253 break; 254 case 4 : 255 sonExplorateur.coller(""); 256 sonMenu = 0; 257 sonExplorateur.lireRepertoire(); 258 leContenu = sonExplorateur.getSonContenu(); 259 break; 260 case 5 : 261 if (leContenu[sonY][sonX]->getSonType() == dossier) 262 { 263 sonExplorateur.coller(leContenu[sonY][sonX]->getSonNom()); 264 sonMenu = 0; 265 sonExplorateur.lireRepertoire(); 266 leContenu = sonExplorateur.getSonContenu(); 267 } 268 break; 269 case 6 : 270 sonExplorateur.supprimer(leContenu[sonY][sonX]->getSonNom()); 271 sonMenu = 0; 272 sonScroll = 0; 273 sonX = 0; 274 sonY = 0; 275 sonExplorateur.lireRepertoire(); 276 leContenu = sonExplorateur.getSonContenu(); 277 break; 278 case 10 : 279 sonExplorateur.eteindre(); 280 sonMenu = 0; 281 break; 282 case 11 : 283 sonExplorateur.redemarrer(); 284 sonMenu = 0; 285 break; 286 default : 287 sonMenu = 0; 288 } 289 break; 290 case SDLK_LALT : 291 sonMenu = 0; 292 break; 293 case SDLK_LSHIFT : 294 sonMenu = 0; 295 break; 296 } 297 298 } 299 300 else 301 { 302 303 switch (lEvenement.key.keysym.sym) 304 { 305 case SDLK_UP : 306 if (sonY != 0) 307 { 308 sonY--; 309 if (sonY < sonScroll) 310 { 311 sonScroll--; 312 } 313 } 314 break; 315 case SDLK_DOWN : 316 if (leContenu[sonY + 1][sonX] != NULL) 317 { 318 sonY++; 319 if (sonY >= sonScroll + nb_icons_y) 320 { 321 sonScroll++; 322 } 323 } 324 break; 325 case SDLK_LEFT : 326 if (sonX != 0) 327 { 328 sonX--; 329 } 330 break; 331 case SDLK_RIGHT : 332 if (sonX + 1 < nb_icons_x && leContenu[sonY][sonX + 1] != NULL) 333 { 334 sonX++; 335 } 336 break; 337 case SDLK_LCTRL : 338 switch (leContenu[sonY][sonX]->getSonType()) 339 { 340 case dossier : 341 sonExplorateur.changerRepertoire(leContenu[sonY][sonX]->getSonNom()); 342 sonScroll = 0; 343 sonX = 0; 344 sonY = 0; 345 sonExplorateur.lireRepertoire(); 346 leContenu = sonExplorateur.getSonContenu(); 347 break; 348 case fichier : 349 sonExplorateur.ouvrirFichier(leContenu[sonY][sonX], false); 350 break; 351 default : 352 sonExplorateur.ajouterErreur("Could not open the file."); 353 } 354 break; 355 case SDLK_LALT : 356 sonExplorateur.changerRepertoire(".."); 357 sonScroll = 0; 358 sonX = 0; 359 sonY = 0; 360 sonExplorateur.lireRepertoire(); 361 leContenu = sonExplorateur.getSonContenu(); 362 break; 363 case SDLK_LSHIFT : 364 sonMenu = 1; 365 break; 366 case SDLK_ESCAPE : 367 b = false; 368 break; 369 } 370 371 } 372 373 } 374 375 } 376 377 } 378 379 } 380 381 return; 382 } 383 384 void Interface::afficher() 385 { 386 387 if (SDL_BlitSurface(sonArrierePlan, NULL, sonEcran, NULL) == -1) 388 { 389 std::cerr << "SDL_BlitSurface(): " << SDL_GetError() << std::endl; 390 exit(EXIT_FAILURE); 391 } 392 393 ecrire(sonExplorateur.getSonRepertoireCourant(), 0, 0, false); 394 std::ifstream laBatterie("/proc/jz/battery"); 395 std::string leNiveau; 396 getline(laBatterie, leNiveau); 397 laBatterie.close(); 398 int leNiveau2 = atoi(leNiveau.c_str()); 399 int font_size = atoi(sonExplorateur.getSaValeur("font-size").c_str()); 400 ecrire("[", 320 - font_size / 2 * 5, 0, false); 401 402 if (leNiveau2 >= 3739) 403 { 404 ecrire("|", 320 - font_size * 2, 0, false); 405 } 406 407 if (leNiveau2 >= 3675) 408 { 409 ecrire("|", 320 - font_size / 2 * 3, 0, false); 410 } 411 412 if (leNiveau2 >= 3611) 413 { 414 ecrire("|", 320 - font_size, 0, false); 415 } 416 417 ecrire("]", 320 - font_size / 2, 0, false); 418 int leX = 0; 419 int leY = 0; 420 SDL_Rect laPosition; 421 std::string leNom; 422 int nb_icons_x = atoi(sonExplorateur.getSaValeur("nb-icons-x").c_str()); 423 int nb_icons_y = atoi(sonExplorateur.getSaValeur("nb-icons-y").c_str()); 424 int icon_width = atoi(sonExplorateur.getSaValeur("icon-width").c_str()); 425 int icon_height = atoi(sonExplorateur.getSaValeur("icon-height").c_str()); 426 unsigned int name_max_size = ((320 / nb_icons_x) / (font_size / 2 )) - 1; 427 std::vector<std::vector<Element *> > leContenu = sonExplorateur.getSonContenu(); 428 std::vector<std::vector<Element *> >::const_iterator leItY; 429 std::vector<Element *>::const_iterator leItX; 430 431 for (leItY = leContenu.begin(); leItY != leContenu.end(); leItY++) 432 { 433 434 for (leItX = leItY->begin(); leItX != leItY->end(); leItX++) 435 { 436 437 if ((*leItX) != NULL && leY >= sonScroll && leY < sonScroll + nb_icons_y) 438 { 439 laPosition.x = leX * (320 / nb_icons_x) + (320 / nb_icons_x - icon_width) / 2; 440 laPosition.y = (leY - sonScroll) * (240 / nb_icons_y) + (240 / nb_icons_y - icon_height) / 2; 441 442 switch ((*leItX)->getSonType()) 443 { 444 case dossier : 445 if (SDL_BlitSurface(sonIconeDossier, NULL, sonEcran, &laPosition) == -1) 446 { 447 std::cerr << "SDL_BlitSurface(): " << SDL_GetError() << std::endl; 448 exit(EXIT_FAILURE); 449 } 450 break; 451 case fichier : 452 { 453 SDL_Surface * lIcone = getSonIcone((*leItX)->getSonExtension()); 454 if (lIcone == NULL) 455 { 456 lIcone = sonIconeFichier; 457 } 458 if (SDL_BlitSurface(lIcone, NULL, sonEcran, &laPosition) == -1) 459 { 460 std::cerr << "SDL_BlitSurface(): " << SDL_GetError() << std::endl; 461 exit(EXIT_FAILURE); 462 } 463 break; 464 } 465 default : 466 if (SDL_BlitSurface(sonIconeInconnu, NULL, sonEcran, &laPosition) == -1) 467 { 468 std::cerr << "SDL_BlitSurface(): " << SDL_GetError() << std::endl; 469 exit(EXIT_FAILURE); 470 } 471 472 } 473 474 leNom = (*leItX)->getSonNom(); 475 476 if (leNom.size() > name_max_size) 477 { 478 leNom = leNom.substr(0, name_max_size); 479 } 480 481 ecrire(leNom, laPosition.x + icon_width / 2.0 - ((leNom.size()) / 2.0) * (font_size / 2.0), laPosition.y + icon_height, leX == sonX && leY == sonY); 482 } 483 484 leX++; 485 } 486 487 leX = 0; 488 leY++; 489 } 490 491 if (sonMenu != 0) 492 { 493 494 if (SDL_BlitSurface(sonFondMenu, NULL, sonEcran, NULL) == -1) 495 { 496 std::cerr << "SDL_BlitSurface(): " << SDL_GetError() << std::endl; 497 exit(EXIT_FAILURE); 498 } 499 500 laPosition.x = 188; 501 laPosition.y = (2 + sonMenu) * font_size + 1; 502 SDL_Rect lesDimensions; 503 lesDimensions.x = 0; 504 lesDimensions.y = (2 + sonMenu) * font_size + 1; 505 lesDimensions.w = 132; 506 lesDimensions.h = font_size; 507 508 if (SDL_BlitSurface(sonMenuSelectionne, &lesDimensions, sonEcran, &laPosition) == -1) 509 { 510 std::cerr << "SDL_BlitSurface(): " << SDL_GetError() << std::endl; 511 exit(EXIT_FAILURE); 512 } 513 514 ecrire("Open", 200, 3 * font_size, false); 515 ecrire("Force execution", 200, 4 * font_size, false); 516 ecrire("Copy", 200, 5 * font_size, false); 517 ecrire("Paste", 200, 6 * font_size, false); 518 ecrire("Paste Into Folder", 200, 7 * font_size, false); 519 ecrire("Delete", 200, 8 * font_size, false); 520 ecrire("Shut Down", 200, 12 * font_size, false); 521 ecrire("Restart", 200, 13 * font_size, false); 522 } 523 524 if (sonExplorateur.getSesErreurs() != "") 525 { 526 std::istringstream lesErreurs(sonExplorateur.getSesErreurs()); 527 std::string lErreur; 528 int i = 0; 529 530 if (SDL_BlitSurface(sonFondMessage, NULL, sonEcran, NULL) == -1) 531 { 532 std::cerr << "SDL_BlitSurface(): " << SDL_GetError() << std::endl; 533 exit(EXIT_FAILURE); 534 } 535 536 while (lesErreurs.good()) 537 { 538 getline(lesErreurs, lErreur); 539 540 if (lErreur != "") 541 { 542 ecrire(lErreur, 160.0 - lErreur.size() / 2.0 * (font_size / 2.0), (3.0 + i) * font_size, false); 543 i++; 544 } 545 } 546 547 ecrire("A : OK", 200, 240 - font_size, false); 548 } 549 550 if (SDL_Flip(sonEcran) == -1) 551 { 552 std::cerr << "SDL_Flip(): " << SDL_GetError() << std::endl; 553 exit(EXIT_FAILURE); 554 } 555 556 return; 557 } 558 559 SDL_Surface * Interface::getSonIcone(std::string uneExtension) const 560 { 561 SDL_Surface * lIcone = NULL; 562 std::map<std::string, SDL_Surface *>::const_iterator leIt = sesIcones.find(uneExtension); 563 564 if (leIt != sesIcones.end()) 565 { 566 lIcone = leIt->second; 567 } 568 569 return lIcone; 570 } 571 572 SDL_Surface * Interface::chargerImage(std::string unFichier) const 573 { 574 SDL_Surface * lImage = IMG_Load(unFichier.c_str()); 575 576 if (lImage == NULL) 577 { 578 std::cerr << "IMG_Load(): " << IMG_GetError() << std::endl; 579 exit(EXIT_FAILURE); 580 } 581 582 return lImage; 583 } 584 585 void Interface::ecrire(std::string unTexte, int unX, int unY, bool estSelectionne) 586 { 587 SDL_Rect laPosition; 588 SDL_Surface * leTexte; 589 590 if (estSelectionne == true) 591 { 592 laPosition.x = unX; 593 laPosition.y = unY; 594 leTexte = TTF_RenderUTF8_Shaded(saPolice, unTexte.c_str(), sonBlanc, sonFocus); 595 596 if (leTexte == NULL) 597 { 598 std::cerr << "TTF_RenderUTF8_Shaded(): " << TTF_GetError() << std::endl; 599 exit(EXIT_FAILURE); 600 } 601 602 if (SDL_BlitSurface(leTexte, NULL, sonEcran, &laPosition) == -1) 603 { 604 std::cerr << "SDL_BlitSurface(): " << SDL_GetError() << std::endl; 605 exit(EXIT_FAILURE); 606 } 607 608 } 609 610 else 611 { 612 laPosition.x = unX + 1; 613 laPosition.y = unY + 1; 614 leTexte = TTF_RenderUTF8_Blended(saPolice, unTexte.c_str(), sonNoir); 615 616 if (leTexte == NULL) 617 { 618 std::cerr << "TTF_RenderUTF8_Blended(): " << TTF_GetError() << std::endl; 619 exit(EXIT_FAILURE); 620 } 621 622 if (SDL_BlitSurface(leTexte, NULL, sonEcran, &laPosition) == -1) 623 { 624 std::cerr << "SDL_BlitSurface(): " << SDL_GetError() << std::endl; 625 exit(EXIT_FAILURE); 626 } 627 628 laPosition.x = unX; 629 laPosition.y = unY; 630 leTexte = TTF_RenderUTF8_Blended(saPolice, unTexte.c_str(), sonBlanc); 631 632 if (leTexte == NULL) 633 { 634 std::cerr << "TTF_RenderUTF8_Blended(): " << TTF_GetError() << std::endl; 635 exit(EXIT_FAILURE); 636 } 637 638 if (SDL_BlitSurface(leTexte, NULL, sonEcran, &laPosition) == -1) 639 { 640 std::cerr << "SDL_BlitSurface(): " << SDL_GetError() << std::endl; 641 exit(EXIT_FAILURE); 642 } 643 644 } 645 646 SDL_FreeSurface(leTexte); 647 return; 648 } 649 650 void Application::eteindre() const 651 { 652 TTF_Quit(); 653 SDL_Quit(); 654 execlp("poweroff", "poweroff", NULL); 655 return; 656 } 657 658 void Application::ouvrirFichier(Element * unFichier, bool estExecutable) 659 { 660 char exe[512]; 661 int exe_len = readlink("/proc/self/exe", exe, sizeof(exe) - 1); 662 exe[exe_len] = '\0'; 663 std::string cwd(getcwd(NULL, 0)); 664 std::string leNom = unFichier->getSonNom(); 665 std::string lExtension = unFichier->getSonExtension(); 666 667 if (lExtension == "dge" || lExtension == "goo" || lExtension == "sh" || estExecutable == true) 668 { 669 std::string laCommande = "\"./" + unFichier->getSonNom() + "\"; cd \"" + cwd + "\"; exec \"" + exe + "\";"; 670 671 if (chdir(sonRepertoireCourant.c_str()) == -1) 672 { 673 ajouterErreur("Permission denied."); 674 } 675 676 else 677 { 678 TTF_Quit(); 679 SDL_Quit(); 680 execlp("/bin/sh", "/bin/sh", "-c", laCommande.c_str(), NULL); 681 } 682 683 } 684 685 else 686 { 687 std::string lApplication = getSonApplication(lExtension); 688 689 if (lApplication.empty()) 690 { 691 ajouterErreur("Could not open the file."); 692 } 693 694 else 695 { 696 int leSlash = lApplication.find_last_of('/'); 697 std::string leRepertoire = lApplication.substr(0, leSlash); 698 lApplication = lApplication.substr(leSlash + 1); 699 std::string laCommande = "\"./" + lApplication + "\" \"" + sonRepertoireCourant + unFichier->getSonNom() + "\"; cd \"" + cwd + "\"; exec \"" + exe + "\";"; 700 701 if (chdir(leRepertoire.c_str()) == -1) 702 { 703 ajouterErreur("Permission denied."); 704 } 705 706 else 707 { 708 TTF_Quit(); 709 SDL_Quit(); 710 execlp("/bin/sh", "/bin/sh", "-c", laCommande.c_str(), NULL); 711 } 712 713 } 714 715 } 716 717 return; 718 } 719