diff --git a/index.js b/index.js index 9194c7a..47d1687 100644 --- a/index.js +++ b/index.js @@ -6,9 +6,11 @@ const integration = require("./js/integration"); const hardware = require("./js/hardware"); const webview = require("./js/webview"); const { app } = require("electron"); +const Events = require("events"); global.APP = global.APP || {}; global.ARGS = global.ARGS || {}; +global.EVENTS = global.EVENTS || new Events(); if (!process.env.DISPLAY) { console.error(`\n$DISPLAY variable not set to run the GUI application, are you connected via SSH?\n`); diff --git a/js/hardware.js b/js/hardware.js index 1fe05d5..7cb105b 100644 --- a/js/hardware.js +++ b/js/hardware.js @@ -3,13 +3,11 @@ const fs = require("fs"); const path = require("path"); const fsp = require("fs/promises"); const cpr = require("child_process"); -const Events = require("events"); global.HARDWARE = global.HARDWARE || { initialized: false, - events: new Events(), - session: {}, support: {}, + session: {}, battery: { level: { path: null, @@ -106,12 +104,13 @@ const init = async () => { return; } HARDWARE.keyboard.visibility = property.Visible === "true"; - HARDWARE.events.emit("updateKeyboard"); + EVENTS.emit("updateKeyboard"); }); }); // Check for display changes interval(update, 1000); + setDisplayStatus("ON"); return true; }; @@ -130,7 +129,7 @@ const update = async () => { if (status !== HARDWARE.display.status.value) { console.log("Update Display Status:", getDisplayStatus()); HARDWARE.display.status.value = status; - HARDWARE.events.emit("updateDisplay"); + EVENTS.emit("updateDisplay"); } } @@ -140,7 +139,7 @@ const update = async () => { if (brightness !== HARDWARE.display.brightness.value) { console.log("Update Display Brightness:", getDisplayBrightness()); HARDWARE.display.brightness.value = brightness; - HARDWARE.events.emit("updateDisplay"); + EVENTS.emit("updateDisplay"); } } }; diff --git a/js/integration.js b/js/integration.js index c38449b..7b48791 100644 --- a/js/integration.js +++ b/js/integration.js @@ -1,11 +1,9 @@ const mqtt = require("mqtt"); const hardware = require("./hardware"); const { app } = require("electron"); -const Events = require("events"); global.INTEGRATION = global.INTEGRATION || { initialized: false, - events: new Events(), }; /** @@ -96,8 +94,8 @@ const init = async () => { }); // Update sensor states from events - HARDWARE.events.on("updateDisplay", updateDisplay); - HARDWARE.events.on("updateKeyboard", updateKeyboard); + EVENTS.on("updateDisplay", updateDisplay); + EVENTS.on("updateKeyboard", updateKeyboard); // Update time sensors periodically (30s) setInterval(() => { @@ -307,7 +305,7 @@ const initRefresh = () => { if (topic === config.command_topic) { console.log("Refreshing webview..."); hardware.setDisplayStatus("ON"); - WEBVIEW.events.emit("reloadView"); + EVENTS.emit("reloadView"); } }) .subscribe(config.command_topic); @@ -333,7 +331,6 @@ const initKiosk = () => { if (topic === config.command_topic) { const status = message.toString(); console.log("Set Kiosk Status:", status); - hardware.setDisplayStatus("ON"); switch (status) { case "Framed": WEBVIEW.window.restore(); @@ -368,7 +365,7 @@ const initKiosk = () => { * Updates the kiosk status via the mqtt connection. */ const updateKiosk = async () => { - const kiosk = WEBVIEW.status; + const kiosk = WEBVIEW.tracker.status; publishState("kiosk", kiosk); }; @@ -400,20 +397,20 @@ const initDisplay = () => { const status = message.toString(); console.log("Set Display Status:", status); hardware.setDisplayStatus(status, (reply, error) => { - if (error) { - console.warn("Failed:", error); - } else { + if (!error) { hardware.update(); + } else { + console.warn("Failed:", error); } }); } else if (topic === config.brightness_command_topic) { const brightness = parseInt(message, 10); console.log("Set Display Brightness:", brightness); hardware.setDisplayBrightness(brightness, (reply, error) => { - if (error) { - console.warn("Failed:", error); - } else { + if (!error) { hardware.update(); + } else { + console.warn("Failed:", error); } }); } @@ -508,7 +505,7 @@ const initPageNumber = () => { const number = parseInt(message, 10); console.log("Set Page Number:", number); WEBVIEW.viewActive = number || 1; - WEBVIEW.events.emit("updateView"); + EVENTS.emit("updateView"); } }) .subscribe(config.command_topic); @@ -547,7 +544,7 @@ const initPageZoom = () => { const zoom = parseInt(message, 10); console.log("Set Page Zoom:", zoom); WEBVIEW.views[WEBVIEW.viewActive || 1].webContents.setZoomFactor(zoom / 100.0); - WEBVIEW.events.emit("updateView"); + EVENTS.emit("updateView"); } }) .subscribe(config.command_topic); @@ -946,8 +943,8 @@ const initLastActive = () => { */ const updateLastActive = async () => { const now = new Date(); - const then = WEBVIEW.pointer.time; - const lastActive = Math.abs(now - then) / (1000 * 60); + const then = WEBVIEW.tracker.pointer.time; + const lastActive = (now - then) / (1000 * 60); publishState("last_active", lastActive); }; diff --git a/js/webview.js b/js/webview.js index 6761f65..295a83a 100644 --- a/js/webview.js +++ b/js/webview.js @@ -3,16 +3,16 @@ const axios = require("axios"); const hardware = require("./hardware"); const integration = require("./integration"); const { app, nativeTheme, globalShortcut, ipcMain, session, BaseWindow, WebContentsView } = require("electron"); -const Events = require("events"); global.WEBVIEW = global.WEBVIEW || { initialized: false, - events: new Events(), - status: null, - locked: false, - pointer: { - position: {}, - time: new Date(), + tracker: { + pointer: { + position: {}, + time: new Date(), + }, + display: {}, + status: null, }, }; @@ -50,8 +50,6 @@ const init = async () => { WEBVIEW.navigationTheme = theme; WEBVIEW.navigationEnabled = widget; nativeTheme.themeSource = WEBVIEW.viewTheme; - WEBVIEW.events.on("reloadView", reloadView); - WEBVIEW.events.on("updateView", updateView); // Init global root window WEBVIEW.window = new BaseWindow({ @@ -118,7 +116,17 @@ const init = async () => { WEBVIEW.window.contentView.addChildView(WEBVIEW.navigation); WEBVIEW.navigation.webContents.loadFile(path.join(APP.path, "html", "navigation.html")); - // Register events + // Register global events + EVENTS.on("reloadView", reloadView); + EVENTS.on("updateView", updateView); + EVENTS.on("updateDisplay", () => { + const status = hardware.getDisplayStatus(); + if (status) { + WEBVIEW.tracker.display[status.toLowerCase()] = new Date(); + } + }); + + // Register local events await windowEvents(); await widgetEvents(); await navigationEvents(); @@ -136,15 +144,15 @@ const update = async () => { return; } - // Update windows status + // Update window status if (WEBVIEW.window.isFullScreen()) { - WEBVIEW.status = "Fullscreen"; + WEBVIEW.tracker.status = "Fullscreen"; } else if (WEBVIEW.window.isMinimized()) { - WEBVIEW.status = "Minimized"; + WEBVIEW.tracker.status = "Minimized"; } else if (WEBVIEW.window.isMaximized()) { - WEBVIEW.status = "Maximized"; + WEBVIEW.tracker.status = "Maximized"; } else { - WEBVIEW.status = "Framed"; + WEBVIEW.tracker.status = "Framed"; } // Update widget status @@ -154,7 +162,7 @@ const update = async () => { updateNavigation(); // Update integration sensor - console.log("Update Kiosk Status:", WEBVIEW.status); + console.log("Update Kiosk Status:", WEBVIEW.tracker.status); integration.update(); }; @@ -434,21 +442,6 @@ const windowEvents = async () => { WEBVIEW.window.on("resize", resizeView); resizeView(); - // Handle window focus events - WEBVIEW.window.on("focus", () => { - if (WEBVIEW.locked) { - hardware.setDisplayStatus("ON"); - WEBVIEW.window.blur(); - } - WEBVIEW.locked = false; - }); - HARDWARE.events.on("updateDisplay", () => { - WEBVIEW.locked = hardware.getDisplayStatus() === "OFF"; - if (WEBVIEW.locked) { - WEBVIEW.window.blur(); - } - }); - // Handle window status updates WEBVIEW.window.on("minimize", update); WEBVIEW.window.on("restore", update); @@ -480,8 +473,8 @@ const windowEvents = async () => { // Check for window touch events (1s) setInterval(() => { const now = new Date(); - const then = WEBVIEW.pointer.time; - const delta = Math.abs(now - then) / 1000; + const then = WEBVIEW.tracker.pointer.time; + const delta = (now - then) / 1000; // Auto-hide navigation if (delta > 60) { @@ -741,18 +734,19 @@ const viewEvents = async () => { // Handle webview mouse events view.webContents.on("before-mouse-event", (e, mouse) => { const now = new Date(); - const then = WEBVIEW.pointer.time; - const delta = Math.abs(now - then) / 1000; + const then = WEBVIEW.tracker.pointer.time; + const delta = (now - then) / 1000; switch (mouse.type) { case "mouseMove": const posNew = { x: mouse.globalX, y: mouse.globalY }; if (posNew.x < 0 || posNew.y < 0) { break; } - const posOld = WEBVIEW.pointer.position; + // Update last active on pointer position change + const posOld = WEBVIEW.tracker.pointer.position; if (posOld.x !== posNew.x || posOld.y !== posNew.y) { - WEBVIEW.pointer.time = now; - WEBVIEW.pointer.position = posNew; + WEBVIEW.tracker.pointer.time = now; + WEBVIEW.tracker.pointer.position = posNew; if (delta > 30) { console.log("Update Last Active"); integration.update(); @@ -761,6 +755,17 @@ const viewEvents = async () => { break; case "mouseDown": switch (mouse.button) { + case "left": + // Ignore touch event if display was off + if (WEBVIEW.tracker.display.off > WEBVIEW.tracker.display.on) { + console.log("Display Touch Event: Ignored"); + e.preventDefault(); + } + // Turn display on if it was off + if (hardware.getDisplayStatus() === "OFF") { + hardware.setDisplayStatus("ON"); + } + break; case "back": historyBackward(); break; @@ -781,7 +786,7 @@ const appEvents = async () => { // Handle signal and exit events process.on("SIGINT", app.quit); app.on("before-quit", () => { - WEBVIEW.status = "Terminated"; + WEBVIEW.tracker.status = "Terminated"; integration.update(); });