From 33626005127bd253ada6a088486bd9ebf2ea97ce Mon Sep 17 00:00:00 2001 From: milaq Date: Thu, 20 Jun 2019 21:30:19 +0200 Subject: [PATCH] handle installation in gui binary this allow us to output status messages and is more transparent to the user. --- .gitignore | 2 +- Makefile | 8 +- build_steamlink.sh | 5 +- install.sh | 38 --------- vhf_installer.c | 171 +++++++++++++++++++++++++++++++++++++++++ vhf_installer_screen.c | 98 ----------------------- 6 files changed, 178 insertions(+), 144 deletions(-) delete mode 100755 install.sh create mode 100644 vhf_installer.c delete mode 100644 vhf_installer_screen.c diff --git a/.gitignore b/.gitignore index 9d5fb16..ef77be3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ *.o -vhf_installer_screen +vhf_installer steamlink diff --git a/Makefile b/Makefile index 7e5e340..fc5b48b 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,14 @@ # Source setenv.sh before running make CFLAGS := -I$(MARVELL_ROOTFS)/usr/include/SDL2 -LDFLAGS := -lSDL2 -lSDL2_ttf +LDFLAGS := -lSDL2 -lSDL2_ttf -lpthread -vhf_installer_screen: vhf_installer_screen.o +vhf_installer: vhf_installer.o $(CC) -o $@ $< $(LDFLAGS) clean: - $(RM) vhf_installer_screen.o + $(RM) vhf_installer.o distclean: clean - $(RM) vhf_installer_screen + $(RM) vhf_installer $(RM) -r steamlink diff --git a/build_steamlink.sh b/build_steamlink.sh index 10f68ac..a0cec9a 100755 --- a/build_steamlink.sh +++ b/build_steamlink.sh @@ -17,15 +17,14 @@ export DESTDIR="${PWD}/steamlink/apps/VirtualHereFreeInstaller" # Copy the files to the app directory mkdir -p "${DESTDIR}" cp -v -r res/ "${DESTDIR}" -cp -v install.sh "${DESTDIR}" -cp -v vhf_installer_screen "${DESTDIR}" +cp -v vhf_installer "${DESTDIR}" cp -v icon.png "${DESTDIR}" # Create the table of contents and icon cat >"${DESTDIR}/toc.txt" <<__EOF__ name=VirtualHereFree Installer icon=icon.png -run=install.sh +run=vhf_installer __EOF__ # Pack it up diff --git a/install.sh b/install.sh deleted file mode 100755 index 67ad4d1..0000000 --- a/install.sh +++ /dev/null @@ -1,38 +0,0 @@ -#!/bin/sh - -bail () { - pkill vhf_installer_screen - exit 1 -} - -./vhf_installer_screen & - -echo "Downloading free VirtualHere server binary..." -mkdir -p /mnt/config/overlay/usr/local/bin/ -wget https://virtualhere.com/sites/default/files/usbserver/vhusbdarm -O /mnt/config/overlay/usr/local/bin/vhusbdarm -if [ $? -ne 0 ]; then - echo "ERROR: Download failed." - bail -fi -chmod 755 /mnt/config/overlay/usr/local/bin/vhusbdarm - -echo "Deploying VirtualHereFree server startup script..." -mkdir -p /mnt/config/overlay/etc/init.d/startup/ -cp res/S98virtualherefree /mnt/config/overlay/etc/init.d/startup/S98virtualherefree - -echo "Deploying VirtualHereFree server watchdog..." -mkdir -p /mnt/config/overlay/usr/local/bin/ -cp res/vhfwatchdog /mnt/config/overlay/usr/local/bin/vhfwatchdog - -if [ -f /mnt/config/system/virtualherefree_config.ini ]; then - echo "VirtualHereFree server config already exists. Not overwriting." -else - echo "Deploying default VirtualHereFree server config..." - cp res/virtualherefree_config.ini /mnt/config/system/virtualherefree_config.ini -fi - -echo "Enabling VirtualHereFree server..." -touch /mnt/config/system/virtualherefree_enabled.txt - -echo "Rebooting device..." -/etc/init.d/reboot.sh & diff --git a/vhf_installer.c b/vhf_installer.c new file mode 100644 index 0000000..d34ff6c --- /dev/null +++ b/vhf_installer.c @@ -0,0 +1,171 @@ +#include +#include +#include +#include +#include +#include + +#include "SDL.h" +#include "SDL_ttf.h" + +#define TEXT_MARGIN 40 +#define FONT "res/sans.ttf" +#define FONTSIZE 24 + +#define INSTALL_DOWNLOAD 1 +#define INSTALL_COPY 2 +#define INSTALL_DONE 3 + +static int installStep = 0; +static int installError = 0; +static int installAbortSignal = 0; + +void getSDLText(SDL_Renderer *renderer, int x, int y, char *text, TTF_Font *font, SDL_Color textColor, + SDL_Texture **texture, SDL_Rect *rect) { + int text_width; + int text_height; + SDL_Surface *surface; + + surface = TTF_RenderText_Solid(font, text, textColor); + *texture = SDL_CreateTextureFromSurface(renderer, surface); + text_width = surface->w; + text_height = surface->h; + SDL_FreeSurface(surface); + rect->x = x; + rect->y = y; + rect->w = text_width; + rect->h = text_height; +} + +void *installThread(void *vargp) { + int ret; + sleep(2); + + installStep = INSTALL_DOWNLOAD; + ret = 0; + ret += system("mkdir -p /mnt/config/overlay/usr/local/bin/"); + ret += system("wget https://virtualhere.com/sites/default/files/usbserver/vhusbdarm -O /mnt/config/overlay/usr/local/bin/vhusbdarm"); + ret += system("chmod 755 /mnt/config/overlay/usr/local/bin/vhusbdarm"); + if (ret > 0) { + installError = INSTALL_DOWNLOAD; + sleep(4); + installAbortSignal = 1; + return; + } + + installStep = INSTALL_COPY; + ret = 0; + ret += system("mkdir -p /mnt/config/overlay/etc/init.d/startup/"); + ret += system("cp res/S98virtualherefree /mnt/config/overlay/etc/init.d/startup/S98virtualherefree"); + ret += system("mkdir -p /mnt/config/overlay/usr/local/bin/"); + ret += system("cp res/vhfwatchdog /mnt/config/overlay/usr/local/bin/vhfwatchdog"); + ret += system("[[ -f /mnt/config/system/virtualherefree_config.ini ]] || cp res/virtualherefree_config.ini /mnt/config/system/virtualherefree_config.ini"); + ret += system("touch /mnt/config/system/virtualherefree_enabled.txt"); + if (ret > 0) { + installError = INSTALL_COPY; + sleep(4); + installAbortSignal = 1; + return; + } + + installStep = INSTALL_DONE; + sleep(4); + system("/etc/init.d/reboot.sh"); +} + +int main(int argc, char *argv[]) { + SDL_DisplayMode displaymode; + SDL_Renderer *renderer; + SDL_Window *window; + SDL_Surface *surface; + SDL_Texture *texture_header, *texture_download, *texture_copy, *texture_done, *texture_dl_failed, *texture_copy_failed; + SDL_Rect textbox_header, textbox_download, textbox_copy, textbox_done, textbox_dl_failed, textbox_copy_failed; + SDL_Event event; + + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError()); + return EXIT_FAILURE; + } + + if (SDL_GetCurrentDisplayMode(0, &displaymode) < 0) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't get displaymode: %s", SDL_GetError()); + return EXIT_FAILURE; + } + + if (SDL_CreateWindowAndRenderer(displaymode.w, displaymode.h, 0, &window, &renderer) < 0) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window and renderer: %s", SDL_GetError()); + return EXIT_FAILURE; + } + SDL_ShowCursor(SDL_DISABLE); + + if (TTF_Init() < 0) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize TTF library: %s", SDL_GetError()); + return EXIT_FAILURE; + } + + TTF_Font* font = TTF_OpenFont(FONT, FONTSIZE); + if (!font) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load font: %s", SDL_GetError()); + return EXIT_FAILURE; + } + + SDL_Color textColorHeader = {120, 120, 120, 0}; + SDL_Color textColorError = {255, 0, 0, 0}; + SDL_Color textColor = {255, 255, 255, 0}; + + getSDLText(renderer, TEXT_MARGIN, TEXT_MARGIN, "VirtualHereFree installer by milaq", font, textColorHeader, &texture_header, &textbox_header); + getSDLText(renderer, TEXT_MARGIN, textbox_header.y + 2 * textbox_header.h, "Downloading VirtualHere binary...", font, textColor, &texture_download, &textbox_download); + getSDLText(renderer, TEXT_MARGIN, textbox_download.y + textbox_download.h, "Installing service...", font, textColor, &texture_copy, &textbox_copy); + + getSDLText(renderer, TEXT_MARGIN, textbox_copy.y + 2 * textbox_copy.h, "All done. Rebooting...", font, textColor, &texture_done, &textbox_done); + getSDLText(renderer, TEXT_MARGIN, textbox_copy.y + 2 * textbox_copy.h, "Download failed.", font, textColorError, &texture_dl_failed, &textbox_dl_failed); + getSDLText(renderer, TEXT_MARGIN, textbox_copy.y + 2 * textbox_copy.h, "Service installation failed.", font, textColorError, &texture_copy_failed, &textbox_copy_failed); + + SDL_FreeSurface(surface); + + pthread_t thread_id; + pthread_create(&thread_id, NULL, installThread, NULL); + + while (!installAbortSignal) { + SDL_PollEvent(&event); + if (event.type == SDL_QUIT) { + break; + } + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); + SDL_RenderClear(renderer); + + SDL_RenderCopy(renderer, texture_header, NULL, &textbox_header); + + if (installStep >= INSTALL_DOWNLOAD) { + SDL_RenderCopy(renderer, texture_download, NULL, &textbox_download); + } + if (installStep >= INSTALL_COPY) { + SDL_RenderCopy(renderer, texture_copy, NULL, &textbox_copy); + } + + if (installError == INSTALL_DOWNLOAD) { + SDL_RenderCopy(renderer, texture_dl_failed, NULL, &textbox_dl_failed); + } else if (installError == INSTALL_COPY) { + SDL_RenderCopy(renderer, texture_copy_failed, NULL, &textbox_copy_failed); + } else if (installStep >= INSTALL_DONE) { + SDL_RenderCopy(renderer, texture_done, NULL, &textbox_done); + } + + SDL_RenderPresent(renderer); + } + + SDL_DestroyTexture(texture_header); + SDL_DestroyTexture(texture_download); + SDL_DestroyTexture(texture_copy); + SDL_DestroyTexture(texture_done); + SDL_DestroyTexture(texture_dl_failed); + SDL_DestroyTexture(texture_copy_failed); + TTF_Quit(); + + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + + SDL_Quit(); + + return EXIT_SUCCESS; +} diff --git a/vhf_installer_screen.c b/vhf_installer_screen.c deleted file mode 100644 index e5276c1..0000000 --- a/vhf_installer_screen.c +++ /dev/null @@ -1,98 +0,0 @@ -#include -#include -#include - -#include "SDL.h" -#include "SDL_ttf.h" - -#define TEXT_MARGIN 40 -#define FONT "res/sans.ttf" -#define FONTSIZE 24 - -void get_text(SDL_Renderer *renderer, int x, int y, char *text, TTF_Font *font, SDL_Color textColor, - SDL_Texture **texture, SDL_Rect *rect) { - int text_width; - int text_height; - SDL_Surface *surface; - - surface = TTF_RenderText_Solid(font, text, textColor); - *texture = SDL_CreateTextureFromSurface(renderer, surface); - text_width = surface->w; - text_height = surface->h; - SDL_FreeSurface(surface); - rect->x = x; - rect->y = y; - rect->w = text_width; - rect->h = text_height; -} - -int main(int argc, char *argv[]) -{ - SDL_DisplayMode displaymode; - SDL_Renderer *renderer; - SDL_Window *window; - SDL_Surface *surface; - SDL_Texture *texture1, *texture2; - SDL_Rect textbox1, textbox2; - SDL_Event event; - - if (SDL_Init(SDL_INIT_VIDEO) < 0) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError()); - return EXIT_FAILURE; - } - - if (SDL_GetCurrentDisplayMode(0, &displaymode) < 0) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't get displaymode: %s", SDL_GetError()); - return EXIT_FAILURE; - } - - if (SDL_CreateWindowAndRenderer(displaymode.w, displaymode.h, 0, &window, &renderer) < 0) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window and renderer: %s", SDL_GetError()); - return EXIT_FAILURE; - } - SDL_ShowCursor(SDL_DISABLE); - - if (TTF_Init() < 0) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize TTF library: %s", SDL_GetError()); - return EXIT_FAILURE; - } - - TTF_Font* font = TTF_OpenFont(FONT, FONTSIZE); - if (!font) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load font: %s", SDL_GetError()); - return EXIT_FAILURE; - } - - SDL_Color textColorHeader = {255, 0, 0, 0}; - SDL_Color textColor = {255, 255, 255, 0}; - - get_text(renderer, TEXT_MARGIN, TEXT_MARGIN, "VirtualHereFree installer by milaq", font, textColorHeader, &texture1, &textbox1); - get_text(renderer, TEXT_MARGIN, textbox1.y + 2 * textbox1.h, "Installing, please wait...", font, textColor, &texture2, &textbox2); - - SDL_FreeSurface(surface); - - while (1) { - SDL_PollEvent(&event); - if (event.type == SDL_QUIT) { - break; - } - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); - SDL_RenderClear(renderer); - - SDL_RenderCopy(renderer, texture1, NULL, &textbox1); - SDL_RenderCopy(renderer, texture2, NULL, &textbox2); - - SDL_RenderPresent(renderer); - } - - SDL_DestroyTexture(texture1); - SDL_DestroyTexture(texture2); - TTF_Quit(); - - SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(window); - - SDL_Quit(); - - return EXIT_SUCCESS; -}