diff --git a/.env b/.env new file mode 100644 index 0000000..39e5bcc --- /dev/null +++ b/.env @@ -0,0 +1,4 @@ +SERVER_URL="https://play.discus.sh" +START_SERVER="false" +GEOM_X="1280" +GEOM_Y="720" \ No newline at end of file diff --git a/assets/favicon.ico b/assets/favicon.ico new file mode 100644 index 0000000..206eea0 Binary files /dev/null and b/assets/favicon.ico differ diff --git a/main.js b/main.js index 559f499..8306bf2 100644 --- a/main.js +++ b/main.js @@ -1,15 +1,47 @@ // Modules to control application life and create native browser window const { app, BrowserWindow, screen } = require('electron') const path = require('node:path') +require('dotenv').config() +const { spawn } = require('child_process'); + +console.log(process.env) +const server_url = process.env.SERVER_URL let factor = null +var server = null +function launchServer() { + server = spawn('swingmusic_bin/swingmusic'); + server.stdout.on('data', (data) => { + console.log(`[SERVER]: ${data}`); + }); + server.stderr.on('data', (data) => { + console.error(`[SERVER ERROR]: ${data}`); + }) + server.on('error', (error) => { + console.error(`[SERVER error]: ${error.message}`); + }); + server.on('close', (code) => { + console.log(`[SERVER EXITED] child process exited with code ${code}`); + }); + +} + +if (process.env.START_SERVER == "true") { + console.log("Launching server") + launchServer() +} else { + console.log("Server not launched") +} function createWindow () { // Create the browser window. const mainWindow = new BrowserWindow({ - width: 1280 / factor, - height: 1024 / factor, + icon: "assets/favicon.ico", + title: "SwingMusic", + center: true, + width: process.env.GEOM_X / factor, + height: process.env.GEOM_Y / factor, webPreferences: { zoomFactor: 1.0 / factor, nodeIntegration: true, @@ -18,7 +50,7 @@ function createWindow () { // Testing some env variables mainWindow.setMenuBarVisibility(false) - mainWindow.loadURL("http://localhost:1970", {userAgent: 'swingmusic-desktop'}) + mainWindow.loadURL(server_url, {userAgent: 'swingmusic-desktop'}) } @@ -36,9 +68,10 @@ app.whenReady().then(() => { }) }) -// Quit when all windows are closed, except on macOS. There, it's common -// for applications and their menu bar to stay active until the user quits -// explicitly with Cmd + Q. +app.on("before-quit", function() { + server.kill() // Killing the server if started +}) + app.on('window-all-closed', function () { app.quit() }) diff --git a/package.json b/package.json index d166571..6f9afbd 100644 --- a/package.json +++ b/package.json @@ -18,5 +18,8 @@ "license": "CC0-1.0", "devDependencies": { "electron": "^28.2.0" + }, + "dependencies": { + "dotenv": "^16.4.1" } } diff --git a/run b/run deleted file mode 100755 index 63cc436..0000000 --- a/run +++ /dev/null @@ -1,51 +0,0 @@ -#!/bin/python -import os -import sys -import signal -import subprocess -import time -import psutil - -BINARY_PATH = os.path.join(os.path.dirname(__file__), "swingmusic_bin/swingmusic") -print(BINARY_PATH) - -# We need to find the swingmusic binary -if not (os.path.exists(BINARY_PATH)): - print("[FATAL] Could not find swingmusic binary in path: " + BINARY_PATH) - sys.exit(1) - -print("[OK] Swingmusic binary found in path: " + BINARY_PATH) - -# Running the binary and getting the pid -pid = subprocess.Popen([BINARY_PATH], stderr=subprocess.PIPE).pid -print(psutil.Process(pid)) -print("[OK] Swingmusic started with pid: " + str(pid)) - -# Running yarn run . -os.system("yarn start .") - -# Kill the binary process -os.kill(pid, signal.SIGINT) - -# Ensure that the process is killed -max_kill_time = 15 # Seconds -kill_time = 0 -while (kill_time < max_kill_time): - if not psutil.pid_exists(pid): - break - # If the process is sleeping, is ok (it means that it is waiting for the next tick and then will die gracefully) - process_status = psutil.Process(pid) - if (process_status.status() == psutil.STATUS_SLEEPING): - print("[WARNING] The subprocess is sleeping, but it is ok, it will die gracefully (maybe with some output)") - break - elif (process_status.status() == psutil.STATUS_ZOMBIE): - print("[WARNING] The subprocess is a zombie, but it is ok, it will die gracefully ") - break - print("[WAITING] For process to stop... " + str(kill_time)) - kill_time += 1 - time.sleep(1) - -if (kill_time >= max_kill_time): - print("[ERROR] The subprocess looks stuck, please kill it manually with: kill -9 " + str(pid)) -else: - print("[OK] Swingmusic stopped (theoretically) with pid: " + str(pid)) \ No newline at end of file diff --git a/run_source b/run_source deleted file mode 100755 index 76ae155..0000000 --- a/run_source +++ /dev/null @@ -1,50 +0,0 @@ -#!/bin/python -import os -import sys -import signal - -SOURCE_PATH = os.path.join(os.path.dirname(__file__), "../swingmusic/") -os.chdir(SOURCE_PATH) - -# We need to find the swingmusic binary -if not (os.path.exists(SOURCE_PATH)): - print("[FATAL] Could not find swingmusic source in path: " + SOURCE_PATH) - sys.exit(1) - -print("[OK] Swingmusic source found in path: " + SOURCE_PATH) - -pid = os.fork() - -# pid greater than 0 represents -# the parent process -if pid > 0 : - print("I am parent process:") - print("Process ID:", os.getpid()) - print("Child's process ID:", pid) - os.chdir("../swingmusic-desktop") - os.system("yarn start .") - # Terminating the child when we are done (not gracefully: it has to stop) - os.kill(pid, signal.SIGINT) - print("Bye!") - -# pid equal to 0 represents -# the created child process -else : - print("\nI am child process:") - print("Process ID:", os.getpid()) - print("Parent's process ID:", os.getppid()) - os.system("./run") - -exit(0) - - -# Running the binary and getting the pid -pid = subprocess.Popen([BINARY_PATH]).pid -print("[OK] Swingmusic started with pid: " + str(pid)) - -# Running yarn run . -os.system("yarn start .") - -# Kill the binary process -os.kill(pid, signal.SIGTERM) -print("[OK] Swingmusic stopped (theoretically) with pid: " + str(pid)) \ No newline at end of file