#include"../es/es.h" #include #include #include #include #include "SDL.h" #define PLUG_NAME "MX GUI Installer" #define PLUG_VER "1.0" extern "C" { #include "SDL_mxf.h" } SDL_Surface *surface = 0; SDL_Font *sysfont = 0; std::vector programs; std::vector files; void rebuild(); void drawInstalled(); void drawList(); void drawMenu(); void createPackageList(std::string name, std::vector& list); void listFiles(std::string name, std::vector &list); std::pair cursor_pos; struct uninstallGo { int index; bool ask; } ugo; uninstallGo igo; void rebuild() { createPackageList("data/registry", programs); listFiles("packages/", files); } void ES_init() { /* first Inittialize the Render Surface */ static int rmask, gmask, bmask, amask; #if SDL_BYTEORDER == SDL_BIG_ENDIAN rmask = 0xff000000; gmask = 0x00ff0000; bmask = 0x0000ff00; amask = 0x000000ff; #else rmask = 0x000000ff; gmask = 0x0000ff00; bmask = 0x00ff0000; amask = 0xff000000; #endif surface = SDL_CreateRGBSurface(SDL_SWSURFACE , 640, 480, 32, rmask, gmask,bmask, amask); sysfont = SDL_InitFont("data/font/system.mxf"); // fill rect with nothing SDL_FillRect(surface, 0, 0); rebuild(); cursor_pos.first = 0; cursor_pos.second = 0; ugo.ask = false; ugo.index = 0; igo.ask = false; igo.index = 0; } void ES_quit() { SDL_FreeFont(sysfont); SDL_FreeSurface(surface); // clean up what we were using } const char *ES_plug_name() { return PLUG_NAME; } const char *ES_plug_version() { return PLUG_VER; } SDL_Surface *ES_render(int *return_value) { // a random color every frame for a example SDL_FillRect(surface, 0, SDL_MapRGB(surface->format, 19, 188, 237)); if(ugo.ask == true || igo.ask == true) { std::string str ; str = "Are you sure you wish to"; str += (igo.ask == true) ? " install " : " uninstall "; str += (igo.ask == true) ? files[igo.index] : programs[ugo.index]; str += " ?\nButton 0 for yes, button 1 for No"; SDL_PrintTextScaled(surface, sysfont, 25, 25, 30, 35, SDL_MapRGB(surface->format, 255, 255, 255), str.c_str()); return surface; } drawInstalled(); drawList(); drawMenu(); return surface; } void ES_axis_movement(int jid, int axis, int value) { if(axis == 4) { if(value < 0) { cursor_pos.first = 0; cursor_pos.second = 0; } else if(value > 0) { cursor_pos.first = 1; cursor_pos.second = 0; } } if(axis == 5) { if(value < 0) { if(cursor_pos.second > 0) cursor_pos.second--; } if(value > 0) { if(cursor_pos.first == 0 && cursor_pos.second+1 < programs.size()) cursor_pos.second++; if(cursor_pos.first == 1 && cursor_pos.second+1 < files.size()) cursor_pos.second++; } } } void ES_button_down(int jid, int button) { if(button == 0) { if(cursor_pos.first == 0) { if(ugo.ask == false && cursor_pos.first == 0) { ugo.index = cursor_pos.second; ugo.ask = true; return; } if(ugo.ask == true) { // uninstall std::ostringstream buffer; #ifdef _WIN32 buffer << "mxp-i.exe --remove=" << programs[ugo.index]; system(buffer.str().c_str()); #else buffer << "mxp-i --remove=" << programs[ugo.index]; system(buffer.str().c_str()); #endif rebuild(); ugo.index = 0; ugo.ask = false; } } else if(cursor_pos.first == 1) { if(igo.ask == false && cursor_pos.first == 1) { igo.index = cursor_pos.second; igo.ask = true; return; } if(igo.ask == true) { std::ostringstream buffer; #ifdef _WIN32 buffer << "mxp-i.exe --install=packages/" << files[igo.index]; system(buffer.str().c_str()); #else buffer << "mxp-i --install=packages/" << files[igo.index]; system(buffer.str().c_str()); #endif igo.index = 0; igo.ask = false; rebuild(); } } } if(button == 1) { if(ugo.ask == true) { ugo.ask = false; ugo.index = 0; } } } void ES_button_up(int jid, int button) { } void createPackageList(std::string path, std::vector& list) { if(!list.empty()) list.erase(list.begin(), list.end()); DIR *pDir = opendir(path.c_str()); struct dirent *pent; std::string total = " "; std::vector files; while((pent = readdir(pDir))) { if(strcmp(pent->d_name, ".") == 0 || strcmp(pent->d_name, "..") == 0) continue; std::string full_path; full_path = path; full_path += "/"; full_path += pent->d_name; DIR *tdir = opendir(full_path.c_str()); if(tdir) { if(pent && pent->d_name) { if(pent->d_name[0] != '.') { list.push_back(pent->d_name); std::cout << "Detected Package:" << pent->d_name << "\n"; } } } closedir(tdir); } closedir(pDir); } void drawMenu() { SDL_Rect rcz = { 25, 400, 500, 60 }; SDL_FillRect(surface, &rcz, SDL_MapRGB(surface->format, 0, 0, 0)); SDL_PrintText(surface, sysfont, 35, 410, SDL_MapRGB(surface->format, 255, 255, 255), "Select a item to uninstall, or item to install\n then press Button 0 "); } void drawInstalled() { SDL_Rect rc = { 25, 100, 305, 200 }; SDL_FillRect(surface, &rc, SDL_MapRGB(surface->format, 0,0,0)); SDL_PrintText(surface, sysfont, 30, 80, SDL_MapRGB(surface->format,255,255,255), "Installed Packages"); int i; std::string package; for(i = 0; i < 8 && i < programs.size(); i++) { Uint32 color = SDL_MapRGB(surface->format, 255, 0, 0); if(cursor_pos.first == 0 && cursor_pos.second == i) color = SDL_MapRGB(surface->format,255,255,255); package = programs[i]; SDL_PrintText(surface, sysfont, 35, 105+(25*i), color, package.c_str()); } } void drawList() { SDL_Rect rc = { 340, 100, 280, 200 }; SDL_FillRect(surface, &rc, SDL_MapRGB(surface->format, 0, 0, 0)); int i; std::string file_name; for(i = 0; i < 8 && i < files.size(); i++) { Uint32 color = SDL_MapRGB(surface->format, 0, 255, 0); if(cursor_pos.first == 1 && cursor_pos.second == i) color = SDL_MapRGB(surface->format, 255, 255, 255); file_name = files[i]; SDL_PrintText(surface, sysfont, 350, 105+(25*i), color, file_name.c_str()); } SDL_PrintText(surface, sysfont, 350, 80, SDL_MapRGB(surface->format, 255, 255, 255), "MX Packages"); } void listFiles(std::string name, std::vector &list) { if(!list.empty()) list.erase(list.begin(), list.end()); DIR *direct = opendir(name.c_str()); struct dirent *dir; while ((dir = readdir(direct))) { if(strstr(dir->d_name, ".mxp")) { list.push_back(dir->d_name); } } closedir(direct); }