From 8cf894bc4eb2305d4630cbc34095bbdebbca6ae5 Mon Sep 17 00:00:00 2001 From: LordGrey <48840279+Lord-Grey@users.noreply.github.com> Date: Sat, 12 Oct 2024 14:17:28 +0200 Subject: [PATCH 01/13] Correct JS requestConfig call --- assets/webconfig/js/hyperion.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/webconfig/js/hyperion.js b/assets/webconfig/js/hyperion.js index a06776b60..92cf06f9c 100644 --- a/assets/webconfig/js/hyperion.js +++ b/assets/webconfig/js/hyperion.js @@ -530,7 +530,7 @@ async function requestServiceDiscovery(type, params) { async function requestConfig(globalTypes, instances, instanceTypes) { let globalFilter = { "global": { "types": globalTypes } }; let instanceFilter = { "instances": { "ids": instances, "types": instanceTypes } }; - let filter = { "configFilter" : globalFilter, instanceFilter }; + let filter = { "configFilter" : { globalFilter, instanceFilter} }; return sendAsyncToHyperion("config", "getconfig", filter); } From 075204482ddec535b5bbc842c6f878f2f116ae5a Mon Sep 17 00:00:00 2001 From: LordGrey <48840279+Lord-Grey@users.noreply.github.com> Date: Fri, 8 Nov 2024 16:34:06 +0100 Subject: [PATCH 02/13] Update requestWriteConfig to new API format --- assets/webconfig/js/hyperion.js | 404 +++++++++---------- libsrc/api/JSONRPC_schema/schema-config.json | 4 +- libsrc/api/JsonAPI.cpp | 119 +++--- 3 files changed, 246 insertions(+), 281 deletions(-) diff --git a/assets/webconfig/js/hyperion.js b/assets/webconfig/js/hyperion.js index 92cf06f9c..a06dbdaa8 100644 --- a/assets/webconfig/js/hyperion.js +++ b/assets/webconfig/js/hyperion.js @@ -34,35 +34,29 @@ tokenList = {}; const ENDLESS = -1; -function initRestart() -{ +function initRestart() { $(window.hyperion).off(); window.watchdog = 10; - connectionLostDetection('restart'); + connectionLostDetection('restart'); } -function connectionLostDetection(type) -{ - if ( window.watchdog > 2 ) - { - var interval_id = window.setInterval(function(){clearInterval(interval_id);}, 9999); // Get a reference to the last +function connectionLostDetection(type) { + if (window.watchdog > 2) { + var interval_id = window.setInterval(function () { clearInterval(interval_id); }, 9999); // Get a reference to the last for (var i = 1; i < interval_id; i++) window.clearInterval(i); - if(type == 'restart') - { + if (type == 'restart') { $("body").html($("#container_restart").html()); // setTimeout delay for probably slower systems, some browser don't execute THIS action - setTimeout(restartAction,250); + setTimeout(restartAction, 250); } - else - { + else { $("body").html($("#container_connection_lost").html()); connectionLostAction(); } } - else - { - $.get( "/cgi/cfg_jsonserver", function() {window.watchdog=0}).fail(function() {window.watchdog++;}); + else { + $.get("/cgi/cfg_jsonserver", function () { window.watchdog = 0 }).fail(function () { window.watchdog++; }); } } @@ -70,25 +64,22 @@ setInterval(connectionLostDetection, 3000); // init websocket to hyperion and bind socket events to jquery events of $(hyperion) object -function initWebSocket() -{ - if ("WebSocket" in window) - { - if (window.websocket == null) - { +function initWebSocket() { + if ("WebSocket" in window) { + if (window.websocket == null) { window.jsonPort = ''; - if(document.location.port == '' && document.location.protocol == "http:") + if (document.location.port == '' && document.location.protocol == "http:") window.jsonPort = '80'; else if (document.location.port == '' && document.location.protocol == "https:") window.jsonPort = '443'; else window.jsonPort = document.location.port; - window.websocket = (document.location.protocol == "https:") ? new WebSocket('wss://'+document.location.hostname+":"+window.jsonPort) : new WebSocket('ws://'+document.location.hostname+":"+window.jsonPort); + window.websocket = (document.location.protocol == "https:") ? new WebSocket('wss://' + document.location.hostname + ":" + window.jsonPort) : new WebSocket('ws://' + document.location.hostname + ":" + window.jsonPort); window.websocket.onopen = function (event) { - $(window.hyperion).trigger({type:"open"}); + $(window.hyperion).trigger({ type: "open" }); - $(window.hyperion).on("cmd-serverinfo", function(event) { + $(window.hyperion).on("cmd-serverinfo", function (event) { window.watchdog = 0; }); }; @@ -96,8 +87,7 @@ function initWebSocket() window.websocket.onclose = function (event) { // See http://tools.ietf.org/html/rfc6455#section-7.4.1 var reason; - switch(event.code) - { + switch (event.code) { case 1000: reason = "Normal closure, meaning that the purpose for which the connection was established has been fulfilled."; break; case 1001: reason = "An endpoint is \"going away\", such as a server going down or a browser having navigated away from a page."; break; case 1002: reason = "An endpoint is terminating the connection due to a protocol error"; break; @@ -113,72 +103,66 @@ function initWebSocket() case 1015: reason = "The connection was closed due to a failure to perform a TLS handshake (e.g., the server certificate can't be verified)."; break; default: reason = "Unknown reason"; } - $(window.hyperion).trigger({type:"close", reason:reason}); + $(window.hyperion).trigger({ type: "close", reason: reason }); window.watchdog = 10; connectionLostDetection(); }; window.websocket.onmessage = function (event) { - try - { + try { var response = JSON.parse(event.data); var success = response.success; var cmd = response.command; var tan = response.tan - if (success || typeof(success) == "undefined") - { - $(window.hyperion).trigger({type:"cmd-"+cmd, response:response}); + if (success || typeof (success) == "undefined") { + $(window.hyperion).trigger({ type: "cmd-" + cmd, response: response }); } - else - { - // skip tan -1 error handling - if(tan != -1){ - var error = response.hasOwnProperty("error")? response.error : "unknown"; - if (error == "Service Unavailable") { - window.location.reload(); - } else { - $(window.hyperion).trigger({type:"error", reason:error}); - } - let errorData = response.hasOwnProperty("errorData")? response.errorData : ""; - console.log("[window.websocket::onmessage] ",error, ", Description:", errorData); + else { + // skip tan -1 error handling + if (tan != -1) { + var error = response.hasOwnProperty("error") ? response.error : "unknown"; + if (error == "Service Unavailable") { + window.location.reload(); + } else { + $(window.hyperion).trigger({ type: "error", reason: error }); } + let errorData = response.hasOwnProperty("errorData") ? response.errorData : ""; + console.log("[window.websocket::onmessage] ", error, ", Description:", errorData); + } } } - catch(exception_error) - { - $(window.hyperion).trigger({type:"error",reason:exception_error}); - console.log("[window.websocket::onmessage] ",exception_error) + catch (exception_error) { + $(window.hyperion).trigger({ type: "error", reason: exception_error }); + console.log("[window.websocket::onmessage] ", exception_error) } }; window.websocket.onerror = function (error) { - $(window.hyperion).trigger({type:"error",reason:error}); - console.log("[window.websocket::onerror] ",error) + $(window.hyperion).trigger({ type: "error", reason: error }); + console.log("[window.websocket::onerror] ", error) }; } } - else - { + else { $(window.hyperion).trigger("error"); alert("Websocket is not supported by your browser"); return; } } -function sendToHyperion(command, subcommand, msg) -{ +function sendToHyperion(command, subcommand, msg) { if (typeof subcommand != 'undefined' && subcommand.length > 0) - subcommand = ',"subcommand":"'+subcommand+'"'; + subcommand = ',"subcommand":"' + subcommand + '"'; else subcommand = ""; if (typeof msg != 'undefined' && msg.length > 0) - msg = ","+msg; + msg = "," + msg; else msg = ""; - window.wsTan = Math.floor(Math.random() * 1000) - window.websocket.send('{"command":"'+command+'", "tan":'+window.wsTan+subcommand+msg+'}'); + window.wsTan = Math.floor(Math.random() * 1000) + window.websocket.send('{"command":"' + command + '", "tan":' + window.wsTan + subcommand + msg + '}'); } // Send a json message to Hyperion and wait for a matching response @@ -188,9 +172,9 @@ function sendToHyperion(command, subcommand, msg) // data: The json data as Object // tan: The optional tan, default 1. If the tan is -1, we skip global response error handling // Returns data of response or false if timeout -async function sendAsyncToHyperion (command, subcommand, data, tan = Math.floor(Math.random() * 1000) ) { +async function sendAsyncToHyperion(command, subcommand, data, tan = Math.floor(Math.random() * 1000)) { let obj = { command, tan } - if (subcommand) {Object.assign(obj, {subcommand})} + if (subcommand) { Object.assign(obj, { subcommand }) } if (data) { Object.assign(obj, data) } //if (process.env.DEV || sstore.getters['common/getDebugState']) console.log('SENDAS', obj) @@ -200,7 +184,7 @@ async function sendAsyncToHyperion (command, subcommand, data, tan = Math.floor( // Send a json message to Hyperion and wait for a matching response // A response matches, when command(+subcommand) of request and response is the same // Returns data of response or false if timeout -async function __sendAsync (data) { +async function __sendAsync(data) { return new Promise((resolve, reject) => { let cmd = data.command let subc = data.subcommand @@ -213,7 +197,7 @@ async function __sendAsync (data) { try { rdata = JSON.parse(e.data) } catch (error) { - console.error("[window.websocket::onmessage] ",error) + console.error("[window.websocket::onmessage] ", error) resolve(false) } if (rdata.command == cmd && rdata.tan == tan) { @@ -232,279 +216,273 @@ async function __sendAsync (data) { // wrapped server commands // Test if admin requires authentication -function requestRequiresAdminAuth() -{ - sendToHyperion("authorize","adminRequired"); +function requestRequiresAdminAuth() { + sendToHyperion("authorize", "adminRequired"); } // Test if the default password needs to be changed -function requestRequiresDefaultPasswortChange() -{ - sendToHyperion("authorize","newPasswordRequired"); +function requestRequiresDefaultPasswortChange() { + sendToHyperion("authorize", "newPasswordRequired"); } // Change password -function requestChangePassword(oldPw, newPw) -{ - sendToHyperion("authorize","newPassword",'"password": "'+oldPw+'", "newPassword":"'+newPw+'"'); +function requestChangePassword(oldPw, newPw) { + sendToHyperion("authorize", "newPassword", '"password": "' + oldPw + '", "newPassword":"' + newPw + '"'); } -function requestAuthorization(password) -{ - sendToHyperion("authorize","login",'"password": "' + password + '"'); +function requestAuthorization(password) { + sendToHyperion("authorize", "login", '"password": "' + password + '"'); } -function requestTokenAuthorization(token) -{ - sendToHyperion("authorize","login",'"token": "' + token + '"'); +function requestTokenAuthorization(token) { + sendToHyperion("authorize", "login", '"token": "' + token + '"'); } -function requestToken(comment) -{ - sendToHyperion("authorize","createToken",'"comment": "'+comment+'"'); +function requestToken(comment) { + sendToHyperion("authorize", "createToken", '"comment": "' + comment + '"'); } -function requestTokenInfo() -{ - sendToHyperion("authorize","getTokenList",""); +function requestTokenInfo() { + sendToHyperion("authorize", "getTokenList", ""); } -function requestGetPendingTokenRequests (id, state) { +function requestGetPendingTokenRequests(id, state) { sendToHyperion("authorize", "getPendingTokenRequests", ""); } -function requestHandleTokenRequest(id, state) -{ - sendToHyperion("authorize","answerRequest",'"id":"'+id+'", "accept":'+state); +function requestHandleTokenRequest(id, state) { + sendToHyperion("authorize", "answerRequest", '"id":"' + id + '", "accept":' + state); } -function requestTokenDelete(id) -{ - sendToHyperion("authorize","deleteToken",'"id":"'+id+'"'); +function requestTokenDelete(id) { + sendToHyperion("authorize", "deleteToken", '"id":"' + id + '"'); } -function requestInstanceRename(inst, name) -{ - sendToHyperion("instance", "saveName",'"instance": '+inst+', "name": "'+name+'"'); +function requestInstanceRename(inst, name) { + sendToHyperion("instance", "saveName", '"instance": ' + inst + ', "name": "' + name + '"'); } -function requestInstanceStartStop(inst, start) -{ - if(start) - sendToHyperion("instance","startInstance",'"instance": '+inst); +function requestInstanceStartStop(inst, start) { + if (start) + sendToHyperion("instance", "startInstance", '"instance": ' + inst); else - sendToHyperion("instance","stopInstance",'"instance": '+inst); + sendToHyperion("instance", "stopInstance", '"instance": ' + inst); } -function requestInstanceDelete(inst) -{ - sendToHyperion("instance","deleteInstance",'"instance": '+inst); +function requestInstanceDelete(inst) { + sendToHyperion("instance", "deleteInstance", '"instance": ' + inst); } -function requestInstanceCreate(name) -{ - sendToHyperion("instance","createInstance",'"name": "'+name+'"'); +function requestInstanceCreate(name) { + sendToHyperion("instance", "createInstance", '"name": "' + name + '"'); } -function requestInstanceSwitch(inst) -{ - sendToHyperion("instance","switchTo",'"instance": '+inst); +function requestInstanceSwitch(inst) { + sendToHyperion("instance", "switchTo", '"instance": ' + inst); } -function requestServerInfo() -{ - sendToHyperion("serverinfo","",'"subscribe":["components-update", "priorities-update", "imageToLedMapping-update", "adjustment-update", "videomode-update", "effects-update", "settings-update", "instance-update"]'); +function requestServerInfo() { + sendToHyperion("serverinfo", "", '"subscribe":["components-update", "priorities-update", "imageToLedMapping-update", "adjustment-update", "videomode-update", "effects-update", "settings-update", "instance-update"]'); } -function requestSysInfo() -{ +function requestSysInfo() { sendToHyperion("sysinfo"); } -function requestSystemSuspend() -{ - sendToHyperion("system","suspend"); +function requestSystemSuspend() { + sendToHyperion("system", "suspend"); } -function requestSystemResume() -{ - sendToHyperion("system","resume"); +function requestSystemResume() { + sendToHyperion("system", "resume"); } -function requestSystemRestart() -{ - sendToHyperion("system","restart"); +function requestSystemRestart() { + sendToHyperion("system", "restart"); } -function requestServerConfigSchema() -{ - sendToHyperion("config","getschema"); +function requestServerConfigSchema() { + sendToHyperion("config", "getschema"); } -function requestServerConfig() -{ +function requestServerConfig() { sendToHyperion("config", "getconfig"); } -function requestServerConfigOld() -{ +function requestServerConfigOld() { sendToHyperion("config", "getconfig-old"); } -function requestServerConfigReload() -{ +function requestServerConfigReload() { sendToHyperion("config", "reload"); } -function requestLedColorsStart() -{ - window.ledStreamActive=true; +function requestLedColorsStart() { + window.ledStreamActive = true; sendToHyperion("ledcolors", "ledstream-start"); } -function requestLedColorsStop() -{ - window.ledStreamActive=false; +function requestLedColorsStop() { + window.ledStreamActive = false; sendToHyperion("ledcolors", "ledstream-stop"); } -function requestLedImageStart() -{ - window.imageStreamActive=true; +function requestLedImageStart() { + window.imageStreamActive = true; sendToHyperion("ledcolors", "imagestream-start"); } -function requestLedImageStop() -{ - window.imageStreamActive=false; +function requestLedImageStop() { + window.imageStreamActive = false; sendToHyperion("ledcolors", "imagestream-stop"); } -function requestPriorityClear(prio) -{ - if(typeof prio !== 'number') +function requestPriorityClear(prio) { + if (typeof prio !== 'number') prio = window.webPrio; - $(window.hyperion).trigger({type:"stopBrowerScreenCapture"}); - sendToHyperion("clear", "", '"priority":'+prio+''); + $(window.hyperion).trigger({ type: "stopBrowerScreenCapture" }); + sendToHyperion("clear", "", '"priority":' + prio + ''); } -function requestClearAll() -{ - $(window.hyperion).trigger({type:"stopBrowerScreenCapture"}); +function requestClearAll() { + $(window.hyperion).trigger({ type: "stopBrowerScreenCapture" }); requestPriorityClear(-1) } -function requestPlayEffect(effectName, duration) -{ - $(window.hyperion).trigger({type:"stopBrowerScreenCapture"}); - sendToHyperion("effect", "", '"effect":{"name":"'+effectName+'"},"priority":'+window.webPrio+',"duration":'+validateDuration(duration)+',"origin":"'+window.webOrigin+'"'); +function requestPlayEffect(effectName, duration) { + $(window.hyperion).trigger({ type: "stopBrowerScreenCapture" }); + sendToHyperion("effect", "", '"effect":{"name":"' + effectName + '"},"priority":' + window.webPrio + ',"duration":' + validateDuration(duration) + ',"origin":"' + window.webOrigin + '"'); } -function requestSetColor(r,g,b,duration) -{ - $(window.hyperion).trigger({type:"stopBrowerScreenCapture"}); - sendToHyperion("color", "", '"color":['+r+','+g+','+b+'], "priority":'+window.webPrio+',"duration":'+validateDuration(duration)+',"origin":"'+window.webOrigin+'"'); +function requestSetColor(r, g, b, duration) { + $(window.hyperion).trigger({ type: "stopBrowerScreenCapture" }); + sendToHyperion("color", "", '"color":[' + r + ',' + g + ',' + b + '], "priority":' + window.webPrio + ',"duration":' + validateDuration(duration) + ',"origin":"' + window.webOrigin + '"'); } -function requestSetImage(data,duration,name) -{ - sendToHyperion("image", "", '"imagedata":"'+data+'", "priority":'+window.webPrio+',"duration":'+validateDuration(duration)+', "format":"auto", "origin":"'+window.webOrigin+'", "name":"'+name+'"'); +function requestSetImage(data, duration, name) { + sendToHyperion("image", "", '"imagedata":"' + data + '", "priority":' + window.webPrio + ',"duration":' + validateDuration(duration) + ', "format":"auto", "origin":"' + window.webOrigin + '", "name":"' + name + '"'); } -function requestSetComponentState(comp, state) -{ +function requestSetComponentState(comp, state) { var state_str = state ? "true" : "false"; - sendToHyperion("componentstate", "", '"componentstate":{"component":"'+comp+'","state":'+state_str+'}'); + sendToHyperion("componentstate", "", '"componentstate":{"component":"' + comp + '","state":' + state_str + '}'); } -function requestSetSource(prio) -{ - if ( prio == "auto" ) +function requestSetSource(prio) { + if (prio == "auto") sendToHyperion("sourceselect", "", '"auto":true'); else - sendToHyperion("sourceselect", "", '"priority":'+prio); + sendToHyperion("sourceselect", "", '"priority":' + prio); } -function requestWriteConfig(config, full) -{ - if(full === true) - window.serverConfig = config; - else - { - jQuery.each(config, function(i, val) { +// Function to transform the legacy config into thee new API format +function transformConfig(configInput, instanceId = 0) { + const globalConfig = {}; + const instanceSettings = {}; + + // Populate globalConfig and instanceSettings based on the specified properties + for (const [key, value] of Object.entries(configInput)) { + if (window.schema.propertiesTypes.globalProperties.includes(key)) { + globalConfig[key] = value; + } else if (window.schema.propertiesTypes.instanceProperties.includes(key)) { + instanceSettings[key] = value; + } + } + + // Initialize the final transformed configuration + const transformedConfig = {}; + + // Add `global` only if it has properties + if (Object.keys(globalConfig).length > 0) { + transformedConfig.global = { settings: globalConfig }; + } + + // Add `instance` only if there are instance settings + if (Object.keys(instanceSettings).length > 0) { + transformedConfig.instances = [ + { + id: instanceId, + settings: instanceSettings + } + ]; + } + + return transformedConfig; +} + +function requestWriteConfig(singleInstanceConfig, full) { + let newConfig = ""; + const instance = Number(window.currentHyperionInstance); + + if (full === true) { + window.serverConfig = singleInstanceConfig; + newConfig = transformConfig(window.serverConfig, instance); + } + else { + jQuery.each(singleInstanceConfig, function (i, val) { window.serverConfig[i] = val; }); + newConfig = transformConfig(singleInstanceConfig, instance); } - sendToHyperion("config","setconfig", '"config":'+JSON.stringify(window.serverConfig)); + sendToHyperion("config", "setconfig", '"config":' + JSON.stringify(newConfig)); } function requestRestoreConfig(config) { sendToHyperion("config", "restoreconfig", '"config":' + JSON.stringify(config)); } -function requestWriteEffect(effectName,effectPy,effectArgs,data) -{ +function requestWriteEffect(effectName, effectPy, effectArgs, data) { var cutArgs = effectArgs.slice(1, -1); - sendToHyperion("create-effect", "", '"name":"'+effectName+'", "script":"'+effectPy+'", '+cutArgs+',"imageData":"'+data+'"'); + sendToHyperion("create-effect", "", '"name":"' + effectName + '", "script":"' + effectPy + '", ' + cutArgs + ',"imageData":"' + data + '"'); } -function requestTestEffect(effectName,effectPy,effectArgs,data) -{ - sendToHyperion("effect", "", '"effect":{"name":"'+effectName+'", "args":'+effectArgs+'}, "priority":'+window.webPrio+', "origin":"'+window.webOrigin+'", "pythonScript":"'+effectPy+'", "imageData":"'+data+'"'); +function requestTestEffect(effectName, effectPy, effectArgs, data) { + sendToHyperion("effect", "", '"effect":{"name":"' + effectName + '", "args":' + effectArgs + '}, "priority":' + window.webPrio + ', "origin":"' + window.webOrigin + '", "pythonScript":"' + effectPy + '", "imageData":"' + data + '"'); } -function requestDeleteEffect(effectName) -{ - sendToHyperion("delete-effect", "", '"name":"'+effectName+'"'); +function requestDeleteEffect(effectName) { + sendToHyperion("delete-effect", "", '"name":"' + effectName + '"'); } -function requestLoggingStart() -{ - window.loggingStreamActive=true; +function requestLoggingStart() { + window.loggingStreamActive = true; sendToHyperion("logging", "start"); } -function requestLoggingStop() -{ - window.loggingStreamActive=false; +function requestLoggingStop() { + window.loggingStreamActive = false; sendToHyperion("logging", "stop"); } -function requestMappingType(type) -{ - sendToHyperion("processing", "", '"mappingType": "'+type+'"'); +function requestMappingType(type) { + sendToHyperion("processing", "", '"mappingType": "' + type + '"'); } -function requestVideoMode(newMode) -{ - sendToHyperion("videomode", "", '"videoMode": "'+newMode+'"'); +function requestVideoMode(newMode) { + sendToHyperion("videomode", "", '"videoMode": "' + newMode + '"'); } -function requestAdjustment(type, value, complete) -{ - if(complete === true) - sendToHyperion("adjustment", "", '"adjustment": '+type+''); +function requestAdjustment(type, value, complete) { + if (complete === true) + sendToHyperion("adjustment", "", '"adjustment": ' + type + ''); else - sendToHyperion("adjustment", "", '"adjustment": {"'+type+'": '+value+'}'); + sendToHyperion("adjustment", "", '"adjustment": {"' + type + '": ' + value + '}'); } -async function requestLedDeviceDiscovery(type, params) -{ +async function requestLedDeviceDiscovery(type, params) { let data = { ledDeviceType: type, params: params }; return sendAsyncToHyperion("leddevice", "discover", data); } -async function requestLedDeviceProperties(type, params) -{ +async function requestLedDeviceProperties(type, params) { let data = { ledDeviceType: type, params: params }; return sendAsyncToHyperion("leddevice", "getProperties", data); } -function requestLedDeviceIdentification(type, params) -{ - let data = { ledDeviceType: type, params: params }; +function requestLedDeviceIdentification(type, params) { + let data = { ledDeviceType: type, params: params }; return sendAsyncToHyperion("leddevice", "identify", data); } @@ -528,9 +506,9 @@ async function requestServiceDiscovery(type, params) { } async function requestConfig(globalTypes, instances, instanceTypes) { - let globalFilter = { "global": { "types": globalTypes } }; + let globalFilter = { "global": { "types": globalTypes } }; let instanceFilter = { "instances": { "ids": instances, "types": instanceTypes } }; - let filter = { "configFilter" : { globalFilter, instanceFilter} }; + let filter = { "configFilter": { globalFilter, instanceFilter } }; return sendAsyncToHyperion("config", "getconfig", filter); } diff --git a/libsrc/api/JSONRPC_schema/schema-config.json b/libsrc/api/JSONRPC_schema/schema-config.json index c5e09eb99..50937242b 100644 --- a/libsrc/api/JSONRPC_schema/schema-config.json +++ b/libsrc/api/JSONRPC_schema/schema-config.json @@ -43,9 +43,7 @@ }, "config": { "required": false, - "$ref": "schema-settings-full-relaxed.json", - "required": false, - "$ref": "schema-settings-ui.json" + "$ref": "schema-settings-full-relaxed.json" } }, "additionalProperties": false diff --git a/libsrc/api/JsonAPI.cpp b/libsrc/api/JsonAPI.cpp index 7b7fc757c..52e4c6af8 100644 --- a/libsrc/api/JsonAPI.cpp +++ b/libsrc/api/JsonAPI.cpp @@ -736,90 +736,64 @@ void JsonAPI::handleConfigSetCommand(const QJsonObject &message, const JsonApiCo } QJsonObject config = message["config"].toObject(); - if (config.contains("global") || config.contains("instances")) + if (config.isEmpty()) { - QStringList errorDetails; + sendErrorReply("Update configuration failed", {"No configuration data provided!"}, cmd); + return; + } - QMap instancesNewConfigs; + QStringList errorDetails; - const QJsonArray instances = config["instances"].toArray(); - if (!instances.isEmpty()) + QMap instancesNewConfigs; + + const QJsonArray instances = config["instances"].toArray(); + if (!instances.isEmpty()) + { + QList configuredInstanceIds = _instanceManager->getInstanceIds(); + for (const auto &instance : instances) { - QList configuredInstanceIds = _instanceManager->getInstanceIds(); - for (const auto &instance : instances) + QJsonObject instanceObject = instance.toObject(); + const QJsonValue idx = instanceObject["id"]; + if (idx.isDouble()) { - QJsonObject instanceObject = instance.toObject(); - const QJsonValue idx = instanceObject["id"]; - if (idx.isDouble()) + quint8 instanceId = static_cast(idx.toInt()); + if (configuredInstanceIds.contains(instanceId)) { - quint8 instanceId = static_cast(idx.toInt()); - if (configuredInstanceIds.contains(instanceId)) - { - instancesNewConfigs.insert(instanceId,instanceObject.value("settings").toObject()); - } - else - { - errorDetails.append(QString("Given instance id '%1' does not exist. Configuration item will be ignored").arg(instanceId)); - } + instancesNewConfigs.insert(instanceId,instanceObject.value("settings").toObject()); + } + else + { + errorDetails.append(QString("Given instance id '%1' does not exist. Configuration item will be ignored").arg(instanceId)); } } } - - const QJsonObject globalSettings = config["global"].toObject().value("settings").toObject(); - if (!globalSettings.isEmpty()) - { - const QJsonObject instanceZeroConfig = instancesNewConfigs.value(0); - instancesNewConfigs.insert(0, JsonUtils::mergeJsonObjects(instanceZeroConfig, globalSettings)); - } - - QMapIterator i (instancesNewConfigs); - while (i.hasNext()) { - i.next(); - - quint8 idx = i.key(); - Hyperion* instance = HyperionIManager::getInstance()->getHyperionInstance(idx); - - QPair isSaved = instance->saveSettings(i.value()); - errorDetails.append(isSaved.second); - } - - if (!errorDetails.isEmpty()) - { - sendErrorReply("Update configuration failed", errorDetails, cmd); - return; - } - - sendSuccessReply(cmd); - - return; } - if (config.isEmpty()) + const QJsonObject globalSettings = config["global"].toObject().value("settings").toObject(); + if (!globalSettings.isEmpty()) { - sendErrorReply("Update configuration failed", {"No configuration data provided!"}, cmd); - return; + const QJsonObject instanceZeroConfig = instancesNewConfigs.value(0); + instancesNewConfigs.insert(0, JsonUtils::mergeJsonObjects(instanceZeroConfig, globalSettings)); } - //Backward compatability until UI mesages are updated - if (API::isHyperionEnabled()) - { - QStringList errorDetails; + QMapIterator i (instancesNewConfigs); + while (i.hasNext()) { + i.next(); - QPair isSaved = _hyperion->saveSettings(config); - errorDetails.append(isSaved.second); + quint8 idx = i.key(); + Hyperion* instance = HyperionIManager::getInstance()->getHyperionInstance(idx); - if (!errorDetails.isEmpty()) - { - sendErrorReply("Save settings failed", errorDetails, cmd); - return; - } - - sendSuccessReply(cmd); + QPair isSaved = instance->saveSettings(i.value()); + errorDetails.append(isSaved.second); } - else + + if (!errorDetails.isEmpty()) { - sendErrorReply("Updating the configuration while Hyperion is disabled is not possible", cmd); + sendErrorReply("Update configuration failed", errorDetails, cmd); + return; } + + sendSuccessReply(cmd); } void JsonAPI::handleConfigGetCommand(const QJsonObject &message, const JsonApiCommand& cmd) @@ -943,6 +917,21 @@ void JsonAPI::handleSchemaGetCommand(const QJsonObject& /*message*/, const JsonA alldevices = LedDeviceWrapper::getLedDeviceSchemas(); properties.insert("alldevices", alldevices); + // Add infor about the type of setting elements + QJsonObject settingTypes; + QJsonArray globalSettingTypes; + for (const QString &type : SettingsTable().getGlobalSettingTypes()) { + globalSettingTypes.append(type); + } + settingTypes.insert("globalProperties", globalSettingTypes); + + QJsonArray instanceSettingTypes; + for (const QString &type : SettingsTable().getInstanceSettingTypes()) { + instanceSettingTypes.append(type); + } + settingTypes.insert("instanceProperties", instanceSettingTypes); + properties.insert("propertiesTypes", settingTypes); + #if defined(ENABLE_EFFECTENGINE) // collect all available effect schemas QJsonArray schemaList; From 1dfbdb7f6fe9c956900207cfa13e19b6169f1b4a Mon Sep 17 00:00:00 2001 From: LordGrey <48840279+Lord-Grey@users.noreply.github.com> Date: Fri, 8 Nov 2024 16:40:05 +0100 Subject: [PATCH 03/13] Add hyperion-light and bare-minimum preset scenarios --- CMakePresets.json | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/CMakePresets.json b/CMakePresets.json index ae1743c4b..83ed2dc3c 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -40,6 +40,60 @@ "BUILDCACHE_DIR": "${sourceDir}/.buildcache" } }, + { + "name": "hyperion-light", + "hidden": true, + "cacheVariables": { + "HYPERION_LIGHT": "ON" + } + }, + { + "name": "hyperion-bare-minimum", + "hidden": true, + "cacheVariables": { + // Disable Grabbers + "ENABLE_AMLOGIC": "OFF", + "ENABLE_DDA": "OFF", + "ENABLE_DISPMANX": "OFF", + "ENABLE_DX": "OFF", + "ENABLE_FB": "OFF", + "ENABLE_MF": "OFF", + "ENABLE_OSX": "OFF", + "ENABLE_QT": "OFF", + "ENABLE_V4L2": "OFF", + "ENABLE_X11": "OFF", + "ENABLE_XCB": "OFF", + "ENABLE_AUDIO": "OFF", + + // LED-Devices + "ENABLE_DEV_FTDI": "OFF", + "ENABLE_DEV_NETWORK": "OFF", + "ENABLE_DEV_SERIAL": "ON", + "ENABLE_DEV_SPI": "OFF", + "ENABLE_DEV_TINKERFORGE": "OFF", + "ENABLE_DEV_USB_HID": "OFF", + "ENABLE_DEV_WS281XPWM": "OFF", + + // Disable Input Servers + "ENABLE_BOBLIGHT_SERVER": "OFF", + "ENABLE_CEC": "OFF", + "ENABLE_FLATBUF_SERVER": "OFF", + "ENABLE_PROTOBUF_SERVER": "OFF", + + // Disable Output Connectors + "ENABLE_FORWARDER": "OFF", + "ENABLE_FLATBUF_CONNECT": "OFF", + + // Disable Services + "ENABLE_EXPERIMENTAL": "OFF", + "ENABLE_MDNS": "OFF", + "ENABLE_REMOTE_CTL": "OFF", + "ENABLE_EFFECTENGINE": "OFF", + + "ENABLE_JSONCHECKS": "ON", + "ENABLE_DEPLOY_DEPENDENCIES": "ON" + } + }, { "name": "debug", "hidden": true, From 60f5a075005e2e7c4dd8c7ba4507223d31c1ade8 Mon Sep 17 00:00:00 2001 From: Lord-Grey Date: Thu, 14 Nov 2024 23:00:10 +0100 Subject: [PATCH 04/13] Refactor Python --- include/effectengine/Effect.h | 30 +- include/effectengine/EffectModule.h | 58 ++- include/python/PythonInit.h | 6 + include/python/PythonProgram.h | 8 + libsrc/effectengine/Effect.cpp | 72 +++- libsrc/effectengine/EffectModule.cpp | 541 +++++++++++++++------------ libsrc/python/PythonInit.cpp | 59 +-- libsrc/python/PythonProgram.cpp | 187 ++++----- 8 files changed, 557 insertions(+), 404 deletions(-) diff --git a/include/effectengine/Effect.h b/include/effectengine/Effect.h index 4766f372e..294f5efc5 100644 --- a/include/effectengine/Effect.h +++ b/include/effectengine/Effect.h @@ -24,13 +24,13 @@ class Effect : public QThread friend class EffectModule; - Effect(Hyperion *hyperion - , int priority - , int timeout - , const QString &script - , const QString &name - , const QJsonObject &args = QJsonObject() - , const QString &imageData = "" + Effect(Hyperion* hyperion + , int priority + , int timeout + , const QString& script + , const QString& name + , const QJsonObject& args = QJsonObject() + , const QString& imageData = "" ); ~Effect() override; @@ -64,20 +64,20 @@ class Effect : public QThread QString getScript() const { return _script; } QString getName() const { return _name; } - int getTimeout() const {return _timeout; } + int getTimeout() const { return _timeout; } bool isEndless() const { return _isEndless; } QJsonObject getArgs() const { return _args; } signals: - void setInput(int priority, const std::vector &ledColors, int timeout_ms, bool clearEffect); - void setInputImage(int priority, const Image &image, int timeout_ms, bool clearEffect); + void setInput(int priority, const std::vector& ledColors, int timeout_ms, bool clearEffect); + void setInputImage(int priority, const Image& image, int timeout_ms, bool clearEffect); private: - void setModuleParameters(); + bool setModuleParameters(); void addImage(); - Hyperion *_hyperion; + Hyperion* _hyperion; const int _priority; @@ -95,12 +95,12 @@ class Effect : public QThread /// Buffer for colorData QVector _colors; - Logger *_log; + Logger* _log; // Reflects whenever this effects should interrupt (timeout or external request) - std::atomic _interupt {}; + std::atomic _interupt{}; QSize _imageSize; QImage _image; - QPainter *_painter; + QPainter* _painter; QVector _imageStack; }; diff --git a/include/effectengine/EffectModule.h b/include/effectengine/EffectModule.h index f76e7f623..a9c5a4fd3 100644 --- a/include/effectengine/EffectModule.h +++ b/include/effectengine/EffectModule.h @@ -8,47 +8,41 @@ class Effect; -class EffectModule: public QObject +class EffectModule : public QObject { Q_OBJECT public: - // Python 3 module def - static struct PyModuleDef moduleDef; - - // Init module - static PyObject* PyInit_hyperion(); - // Register module once static void registerHyperionExtensionModule(); // json 2 python - static PyObject * json2python(const QJsonValue & jsonData); + static PyObject* json2python(const QJsonValue& jsonData); // Wrapper methods for Python interpreter extra buildin methods static PyMethodDef effectMethods[]; - static PyObject* wrapSetColor (PyObject *self, PyObject *args); - static PyObject* wrapSetImage (PyObject *self, PyObject *args); - static PyObject* wrapGetImage (PyObject *self, PyObject *args); - static PyObject* wrapAbort (PyObject *self, PyObject *args); - static PyObject* wrapImageShow (PyObject *self, PyObject *args); - static PyObject* wrapImageLinearGradient (PyObject *self, PyObject *args); - static PyObject* wrapImageConicalGradient (PyObject *self, PyObject *args); - static PyObject* wrapImageRadialGradient (PyObject *self, PyObject *args); - static PyObject* wrapImageSolidFill (PyObject *self, PyObject *args); - static PyObject* wrapImageDrawLine (PyObject *self, PyObject *args); - static PyObject* wrapImageDrawPoint (PyObject *self, PyObject *args); - static PyObject* wrapImageDrawRect (PyObject *self, PyObject *args); - static PyObject* wrapImageDrawPolygon (PyObject *self, PyObject *args); - static PyObject* wrapImageDrawPie (PyObject *self, PyObject *args); - static PyObject* wrapImageSetPixel (PyObject *self, PyObject *args); - static PyObject* wrapImageGetPixel (PyObject *self, PyObject *args); - static PyObject* wrapImageSave (PyObject *self, PyObject *args); - static PyObject* wrapImageMinSize (PyObject *self, PyObject *args); - static PyObject* wrapImageWidth (PyObject *self, PyObject *args); - static PyObject* wrapImageHeight (PyObject *self, PyObject *args); - static PyObject* wrapImageCRotate (PyObject *self, PyObject *args); - static PyObject* wrapImageCOffset (PyObject *self, PyObject *args); - static PyObject* wrapImageCShear (PyObject *self, PyObject *args); - static PyObject* wrapImageResetT (PyObject *self, PyObject *args); + static PyObject* wrapSetColor(PyObject* self, PyObject* args); + static PyObject* wrapSetImage(PyObject* self, PyObject* args); + static PyObject* wrapGetImage(PyObject* self, PyObject* args); + static PyObject* wrapAbort(PyObject* self, PyObject* args); + static PyObject* wrapImageShow(PyObject* self, PyObject* args); + static PyObject* wrapImageLinearGradient(PyObject* self, PyObject* args); + static PyObject* wrapImageConicalGradient(PyObject* self, PyObject* args); + static PyObject* wrapImageRadialGradient(PyObject* self, PyObject* args); + static PyObject* wrapImageSolidFill(PyObject* self, PyObject* args); + static PyObject* wrapImageDrawLine(PyObject* self, PyObject* args); + static PyObject* wrapImageDrawPoint(PyObject* self, PyObject* args); + static PyObject* wrapImageDrawRect(PyObject* self, PyObject* args); + static PyObject* wrapImageDrawPolygon(PyObject* self, PyObject* args); + static PyObject* wrapImageDrawPie(PyObject* self, PyObject* args); + static PyObject* wrapImageSetPixel(PyObject* self, PyObject* args); + static PyObject* wrapImageGetPixel(PyObject* self, PyObject* args); + static PyObject* wrapImageSave(PyObject* self, PyObject* args); + static PyObject* wrapImageMinSize(PyObject* self, PyObject* args); + static PyObject* wrapImageWidth(PyObject* self, PyObject* args); + static PyObject* wrapImageHeight(PyObject* self, PyObject* args); + static PyObject* wrapImageCRotate(PyObject* self, PyObject* args); + static PyObject* wrapImageCOffset(PyObject* self, PyObject* args); + static PyObject* wrapImageCShear(PyObject* self, PyObject* args); + static PyObject* wrapImageResetT(PyObject* self, PyObject* args); }; diff --git a/include/python/PythonInit.h b/include/python/PythonInit.h index d3d5ce473..bb3091160 100644 --- a/include/python/PythonInit.h +++ b/include/python/PythonInit.h @@ -1,5 +1,9 @@ #pragma once +#undef slots +#include +#define slots Q_SLOTS + /// /// @brief Handle the PythonInit, module registers and DeInit /// @@ -10,4 +14,6 @@ class PythonInit PythonInit(); ~PythonInit(); + + void handlePythonError(PyStatus status, PyConfig& config); }; diff --git a/include/python/PythonProgram.h b/include/python/PythonProgram.h index b161dcd6d..4b3d05fea 100644 --- a/include/python/PythonProgram.h +++ b/include/python/PythonProgram.h @@ -9,6 +9,8 @@ #include "Python.h" #define slots +#include + class Logger; class PythonProgram @@ -17,9 +19,15 @@ class PythonProgram PythonProgram(const QString & name, Logger * log); ~PythonProgram(); + operator PyThreadState* () + { + return _tstate; + } + void execute(const QByteArray &python_code); private: + QString _name; Logger* _log; PyThreadState* _tstate; diff --git a/libsrc/effectengine/Effect.cpp b/libsrc/effectengine/Effect.cpp index a03ed5701..c01f26432 100644 --- a/libsrc/effectengine/Effect.cpp +++ b/libsrc/effectengine/Effect.cpp @@ -13,7 +13,7 @@ // python utils #include -Effect::Effect(Hyperion *hyperion, int priority, int timeout, const QString &script, const QString &name, const QJsonObject &args, const QString &imageData) +Effect::Effect(Hyperion* hyperion, int priority, int timeout, const QString& script, const QString& name, const QJsonObject& args, const QString& imageData) : QThread() , _hyperion(hyperion) , _priority(priority) @@ -26,7 +26,7 @@ Effect::Effect(Hyperion *hyperion, int priority, int timeout, const QString &scr , _endTime(-1) , _interupt(false) , _imageSize(hyperion->getLedGridSize()) - , _image(_imageSize,QImage::Format_ARGB32_Premultiplied) + , _image(_imageSize, QImage::Format_ARGB32_Premultiplied) { _colors.resize(_hyperion->getLedCount()); _colors.fill(ColorRgb::BLACK); @@ -61,41 +61,81 @@ int Effect::getRemaining() const if (timeout >= 0) { - timeout = static_cast( _endTime - QDateTime::currentMSecsSinceEpoch()); + timeout = static_cast(_endTime - QDateTime::currentMSecsSinceEpoch()); } return timeout; } -void Effect::setModuleParameters() +bool Effect::setModuleParameters() { // import the buildtin Hyperion module - PyObject * module = PyImport_ImportModule("hyperion"); + PyObject* module = PyImport_ImportModule("hyperion"); - // add a capsule containing 'this' to the module to be able to retrieve the effect from the callback function - PyModule_AddObject(module, "__effectObj", PyCapsule_New((void*)this, "hyperion.__effectObj", nullptr)); + if (module == nullptr) { + PyErr_Print(); // Print error if module import fails + return false; + } + + // Add a capsule containing 'this' to the module for callback access + PyObject* capsule = PyCapsule_New((void*)this, "hyperion.__effectObj", nullptr); + if (capsule == nullptr || PyModule_AddObject(module, "__effectObj", capsule) < 0) { + PyErr_Print(); // Print error if adding capsule fails + Py_XDECREF(module); // Clean up if capsule addition fails + Py_XDECREF(capsule); + return false; + } - // add ledCount variable to the interpreter + // Add ledCount variable to the interpreter int ledCount = 0; QMetaObject::invokeMethod(_hyperion, "getLedCount", Qt::BlockingQueuedConnection, Q_RETURN_ARG(int, ledCount)); - PyObject_SetAttrString(module, "ledCount", Py_BuildValue("i", ledCount)); + PyObject* ledCountObj = Py_BuildValue("i", ledCount); + if (PyObject_SetAttrString(module, "ledCount", ledCountObj) < 0) { + PyErr_Print(); // Print error if setting attribute fails + } + Py_XDECREF(ledCountObj); - // add minimumWriteTime variable to the interpreter + // Add minimumWriteTime variable to the interpreter int latchTime = 0; QMetaObject::invokeMethod(_hyperion, "getLatchTime", Qt::BlockingQueuedConnection, Q_RETURN_ARG(int, latchTime)); - PyObject_SetAttrString(module, "latchTime", Py_BuildValue("i", latchTime)); + PyObject* latchTimeObj = Py_BuildValue("i", latchTime); + if (PyObject_SetAttrString(module, "latchTime", latchTimeObj) < 0) { + PyErr_Print(); // Print error if setting attribute fails + } + Py_XDECREF(latchTimeObj); - // add a args variable to the interpreter - PyObject_SetAttrString(module, "args", EffectModule::json2python(_args)); + // Add args variable to the interpreter + PyObject* argsObj = EffectModule::json2python(_args); + if (PyObject_SetAttrString(module, "args", argsObj) < 0) { + PyErr_Print(); // Print error if setting attribute fails + } + Py_XDECREF(argsObj); - // decref the module + // Decrement module reference Py_XDECREF(module); + + return true; } void Effect::run() { PythonProgram program(_name, _log); - setModuleParameters(); +#if (PY_VERSION_HEX < 0x030C0000) + PyThreadState* prev_thread_state = PyThreadState_Swap(program); +#endif + + if (!setModuleParameters()) + { + Error(_log, "Failed to set Module parameters. Effect will not be executed."); +#if (PY_VERSION_HEX < 0x030C0000) + PyThreadState_Swap(prev_thread_state); +#endif + return; + } + +#if (PY_VERSION_HEX < 0x030C0000) + PyThreadState_Swap(prev_thread_state); +#endif // Set the end time if applicable if (_timeout > 0) @@ -104,7 +144,7 @@ void Effect::run() } // Run the effect script - QFile file (_script); + QFile file(_script); if (file.open(QIODevice::ReadOnly)) { program.execute(file.readAll()); diff --git a/libsrc/effectengine/EffectModule.cpp b/libsrc/effectengine/EffectModule.cpp index bfa4a4c41..f80035d3c 100644 --- a/libsrc/effectengine/EffectModule.cpp +++ b/libsrc/effectengine/EffectModule.cpp @@ -17,25 +17,81 @@ #include #include +// Define a struct for per-interpreter state +typedef struct { +} hyperion_module_state; + +// Macro to access the module state +#define GET_HYPERION_STATE(module) ((hyperion_module_state*)PyModule_GetState(module)) + // Get the effect from the capsule #define getEffect() static_cast((Effect*)PyCapsule_Import("hyperion.__effectObj", 0)) -// create the hyperion module -struct PyModuleDef EffectModule::moduleDef = { +// Module execution function for multi-phase init +static int hyperion_exec(PyObject* module) { + // Initialize per-interpreter state + hyperion_module_state* state = GET_HYPERION_STATE(module); + if (state == NULL) + { + return -1; + } + return 0; +} + +// Module creation function for multi-phase init, used in Py_mod_create slot +static PyObject* hyperion_create(PyModuleDef* def, PyObject* args) { + PyObject* module = PyModule_Create(def); + if (!module) + { + return NULL; + } + + // Execute any additional module initialization logic + if (hyperion_exec(module) < 0) + { + Py_DECREF(module); + return NULL; + } + + return module; +} + +// Module deallocation function to clean up per-interpreter state +static void hyperion_free(void* module) +{ + // No specific cleanup required in this example +} + +static PyModuleDef_Slot hyperion_slots[] = { + {Py_mod_exec, hyperion_exec}, +#if (PY_VERSION_HEX >= 0x030C0000) + {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED}, +#endif + {0, NULL} +}; + +// Module definition with multi-phase and per-interpreter state +static struct PyModuleDef hyperion_module = { PyModuleDef_HEAD_INIT, - "hyperion", /* m_name */ - "Hyperion module", /* m_doc */ - -1, /* m_size */ - EffectModule::effectMethods, /* m_methods */ - NULL, /* m_reload */ - NULL, /* m_traverse */ - NULL, /* m_clear */ - NULL, /* m_free */ + "hyperion", // Module name + "Hyperion module", // Module docstring + sizeof(hyperion_module_state), // Size of per-interpreter state + EffectModule::effectMethods, // Methods array + NULL, // Slots array (will be added in PyInit_hyperion) + NULL, // Traverse function (optional) + NULL, // Clear function (optional) + hyperion_free // Free function }; -PyObject* EffectModule::PyInit_hyperion() +// initialize function for the hyperion module +PyMODINIT_FUNC PyInit_hyperion(void) { - return PyModule_Create(&moduleDef); + + // assign slots to the module definition + hyperion_module.m_slots = hyperion_slots; + + // return a new module definition instance + return PyModuleDef_Init(&hyperion_module); } void EffectModule::registerHyperionExtensionModule() @@ -43,54 +99,77 @@ void EffectModule::registerHyperionExtensionModule() PyImport_AppendInittab("hyperion", &PyInit_hyperion); } -PyObject *EffectModule::json2python(const QJsonValue &jsonData) +PyObject* EffectModule::json2python(const QJsonValue& jsonData) { switch (jsonData.type()) { - case QJsonValue::Null: - Py_RETURN_NONE; - case QJsonValue::Undefined: - Py_RETURN_NOTIMPLEMENTED; - case QJsonValue::Double: + case QJsonValue::Null: + Py_RETURN_NONE; + + case QJsonValue::Undefined: + Py_RETURN_NOTIMPLEMENTED; + + case QJsonValue::Double: + { + double value = jsonData.toDouble(); + if (value == static_cast(value)) // If no fractional part, value is equal to its integer representation { - double doubleIntegratlPart; - double doubleFractionalPart = std::modf(jsonData.toDouble(), &doubleIntegratlPart); - if (doubleFractionalPart > std::numeric_limits::epsilon()) - { - return Py_BuildValue("d", jsonData.toDouble()); - } - return Py_BuildValue("i", jsonData.toInt()); + return Py_BuildValue("i", static_cast(value)); } - case QJsonValue::Bool: - return Py_BuildValue("i", jsonData.toBool() ? 1 : 0); - case QJsonValue::String: - return Py_BuildValue("s", jsonData.toString().toUtf8().constData()); - case QJsonValue::Object: + return Py_BuildValue("d", value); + } + + case QJsonValue::Bool: + return PyBool_FromLong(jsonData.toBool() ? 1 : 0); + + case QJsonValue::String: + return PyUnicode_FromString(jsonData.toString().toUtf8().constData()); + + case QJsonValue::Array: + { + QJsonArray arrayData = jsonData.toArray(); + PyObject* list = PyList_New(arrayData.size()); + int index = 0; + for (QJsonArray::iterator i = arrayData.begin(); i != arrayData.end(); ++i, ++index) { - PyObject * dict= PyDict_New(); - QJsonObject objectData = jsonData.toObject(); - for (QJsonObject::iterator i = objectData.begin(); i != objectData.end(); ++i) - { - PyObject * obj = json2python(*i); - PyDict_SetItemString(dict, i.key().toStdString().c_str(), obj); - Py_XDECREF(obj); - } - return dict; + PyObject* obj = json2python(*i); + Py_INCREF(obj); + PyList_SetItem(list, index, obj); + Py_XDECREF(obj); } - case QJsonValue::Array: - { - QJsonArray arrayData = jsonData.toArray(); - PyObject * list = PyList_New(arrayData.size()); - int index = 0; - for (QJsonArray::iterator i = arrayData.begin(); i != arrayData.end(); ++i, ++index) - { - PyObject * obj = json2python(*i); - Py_INCREF(obj); - PyList_SetItem(list, index, obj); - Py_XDECREF(obj); + return list; + } + + case QJsonValue::Object: { + // Python's dict + QJsonObject jsonObject = jsonData.toObject(); + PyObject* pyDict = PyDict_New(); + for (auto it = jsonObject.begin(); it != jsonObject.end(); ++it) { + // Convert key + PyObject* pyKey = PyUnicode_FromString(it.key().toUtf8().constData()); + if (!pyKey) { + Py_XDECREF(pyDict); + return nullptr; // Error occurred, return null + } + // Convert value + PyObject* pyValue = json2python(it.value()); + if (!pyValue) { + Py_XDECREF(pyKey); + Py_XDECREF(pyDict); + return nullptr; // Error occurred, return null } - return list; + // Add to dictionary + PyDict_SetItem(pyDict, pyKey, pyValue); + Py_XDECREF(pyKey); + Py_XDECREF(pyValue); } + return pyDict; + } + + default: + // Unsupported type + PyErr_SetString(PyExc_TypeError, "Unsupported QJsonValue type."); + return nullptr; } assert(false); @@ -126,7 +205,7 @@ PyMethodDef EffectModule::effectMethods[] = { {NULL, NULL, 0, NULL} }; -PyObject* EffectModule::wrapSetColor(PyObject *self, PyObject *args) +PyObject* EffectModule::wrapSetColor(PyObject* self, PyObject* args) { // check the number of arguments int argCount = PyTuple_Size(args); @@ -138,7 +217,7 @@ PyObject* EffectModule::wrapSetColor(PyObject *self, PyObject *args) { getEffect()->_colors.fill(color); QVector _cQV = getEffect()->_colors; - emit getEffect()->setInput(getEffect()->_priority, std::vector( _cQV.begin(), _cQV.end() ), getEffect()->getRemaining(), false); + emit getEffect()->setInput(getEffect()->_priority, std::vector(_cQV.begin(), _cQV.end()), getEffect()->getRemaining(), false); Py_RETURN_NONE; } return nullptr; @@ -146,7 +225,7 @@ PyObject* EffectModule::wrapSetColor(PyObject *self, PyObject *args) else if (argCount == 1) { // bytearray of values - PyObject * bytearray = nullptr; + PyObject* bytearray = nullptr; if (PyArg_ParseTuple(args, "O", &bytearray)) { if (PyByteArray_Check(bytearray)) @@ -154,10 +233,10 @@ PyObject* EffectModule::wrapSetColor(PyObject *self, PyObject *args) size_t length = PyByteArray_Size(bytearray); if (length == 3 * static_cast(getEffect()->_hyperion->getLedCount())) { - char * data = PyByteArray_AS_STRING(bytearray); + char* data = PyByteArray_AS_STRING(bytearray); memcpy(getEffect()->_colors.data(), data, length); QVector _cQV = getEffect()->_colors; - emit getEffect()->setInput(getEffect()->_priority, std::vector( _cQV.begin(), _cQV.end() ), getEffect()->getRemaining(), false); + emit getEffect()->setInput(getEffect()->_priority, std::vector(_cQV.begin(), _cQV.end()), getEffect()->getRemaining(), false); Py_RETURN_NONE; } else @@ -184,12 +263,12 @@ PyObject* EffectModule::wrapSetColor(PyObject *self, PyObject *args) } } -PyObject* EffectModule::wrapSetImage(PyObject *self, PyObject *args) +PyObject* EffectModule::wrapSetImage(PyObject* self, PyObject* args) { // bytearray of values int width = 0; int height = 0; - PyObject * bytearray = nullptr; + PyObject* bytearray = nullptr; if (PyArg_ParseTuple(args, "iiO", &width, &height, &bytearray)) { if (PyByteArray_Check(bytearray)) @@ -198,7 +277,7 @@ PyObject* EffectModule::wrapSetImage(PyObject *self, PyObject *args) if (length == 3 * width * height) { Image image(width, height); - char * data = PyByteArray_AS_STRING(bytearray); + char* data = PyByteArray_AS_STRING(bytearray); memcpy(image.memptr(), data, length); emit getEffect()->setInputImage(getEffect()->_priority, image, getEffect()->getRemaining(), false); Py_RETURN_NONE; @@ -225,11 +304,11 @@ PyObject* EffectModule::wrapSetImage(PyObject *self, PyObject *args) return nullptr; } -PyObject* EffectModule::wrapGetImage(PyObject *self, PyObject *args) +PyObject* EffectModule::wrapGetImage(PyObject* self, PyObject* args) { QBuffer buffer; QImageReader reader; - char *source; + char* source; int cropLeft = 0, cropTop = 0, cropRight = 0, cropBottom = 0; int grayscale = false; @@ -237,7 +316,7 @@ PyObject* EffectModule::wrapGetImage(PyObject *self, PyObject *args) { Q_INIT_RESOURCE(EffectEngine); - if(!PyArg_ParseTuple(args, "s|iiiip", &source, &cropLeft, &cropTop, &cropRight, &cropBottom, &grayscale)) + if (!PyArg_ParseTuple(args, "s|iiiip", &source, &cropLeft, &cropTop, &cropRight, &cropBottom, &grayscale)) { PyErr_SetString(PyExc_TypeError, "String required"); return nullptr; @@ -246,8 +325,8 @@ PyObject* EffectModule::wrapGetImage(PyObject *self, PyObject *args) const QUrl url = QUrl(source); if (url.isValid()) { - QNetworkAccessManager *networkManager = new QNetworkAccessManager(); - QNetworkReply * networkReply = networkManager->get(QNetworkRequest(url)); + QNetworkAccessManager* networkManager = new QNetworkAccessManager(); + QNetworkReply* networkReply = networkManager->get(QNetworkRequest(url)); QEventLoop eventLoop; connect(networkReply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit); @@ -262,14 +341,14 @@ PyObject* EffectModule::wrapGetImage(PyObject *self, PyObject *args) } delete networkReply; - delete networkManager; + delete networkManager; } else { QString file = QString::fromUtf8(source); - if (file.mid(0, 1) == ":") - file = ":/effects/"+file.mid(1); + if (file.mid(0, 1) == ":") + file = ":/effects/" + file.mid(1); reader.setDecideFormatFromContent(true); reader.setFileName(file); @@ -286,7 +365,7 @@ PyObject* EffectModule::wrapGetImage(PyObject *self, PyObject *args) if (reader.canRead()) { - PyObject *result = PyList_New(reader.imageCount()); + PyObject* result = PyList_New(reader.imageCount()); for (int i = 0; i < reader.imageCount(); ++i) { @@ -312,18 +391,18 @@ PyObject* EffectModule::wrapGetImage(PyObject *self, PyObject *args) } QByteArray binaryImage; - for (int i = 0; i(qimage.scanLine(i)); - const QRgb *end = scanline + qimage.width(); + const QRgb* scanline = reinterpret_cast(qimage.scanLine(i)); + const QRgb* end = scanline + qimage.width(); for (; scanline != end; scanline++) { - binaryImage.append(!grayscale ? (char) qRed(scanline[0]) : (char) qGray(scanline[0])); - binaryImage.append(!grayscale ? (char) qGreen(scanline[1]) : (char) qGray(scanline[1])); - binaryImage.append(!grayscale ? (char) qBlue(scanline[2]) : (char) qGray(scanline[2])); + binaryImage.append(!grayscale ? (char)qRed(scanline[0]) : (char)qGray(scanline[0])); + binaryImage.append(!grayscale ? (char)qGreen(scanline[1]) : (char)qGray(scanline[1])); + binaryImage.append(!grayscale ? (char)qBlue(scanline[2]) : (char)qGray(scanline[2])); } } - PyList_SET_ITEM(result, i, Py_BuildValue("{s:i,s:i,s:O}", "imageWidth", width, "imageHeight", height, "imageData", PyByteArray_FromStringAndSize(binaryImage.constData(),binaryImage.size()))); + PyList_SET_ITEM(result, i, Py_BuildValue("{s:i,s:i,s:O}", "imageWidth", width, "imageHeight", height, "imageData", PyByteArray_FromStringAndSize(binaryImage.constData(), binaryImage.size()))); } else { @@ -341,13 +420,13 @@ PyObject* EffectModule::wrapGetImage(PyObject *self, PyObject *args) } } -PyObject* EffectModule::wrapAbort(PyObject *self, PyObject *) +PyObject* EffectModule::wrapAbort(PyObject* self, PyObject*) { return Py_BuildValue("i", getEffect()->isInterruptionRequested() ? 1 : 0); } -PyObject* EffectModule::wrapImageShow(PyObject *self, PyObject *args) +PyObject* EffectModule::wrapImageShow(PyObject* self, PyObject* args) { int argCount = PyTuple_Size(args); int imgId = -1; @@ -357,27 +436,27 @@ PyObject* EffectModule::wrapImageShow(PyObject *self, PyObject *args) argsOk = true; } - if ( ! argsOk || (imgId>-1 && imgId >= getEffect()->_imageStack.size())) + if (!argsOk || (imgId > -1 && imgId >= getEffect()->_imageStack.size())) { return nullptr; } - QImage * qimage = (imgId<0) ? &(getEffect()->_image) : &(getEffect()->_imageStack[imgId]); + QImage* qimage = (imgId < 0) ? &(getEffect()->_image) : &(getEffect()->_imageStack[imgId]); int width = qimage->width(); int height = qimage->height(); Image image(width, height); QByteArray binaryImage; - for (int i = 0; i(qimage->scanLine(i)); - for (int j = 0; j< width; ++j) + const QRgb* scanline = reinterpret_cast(qimage->scanLine(i)); + for (int j = 0; j < width; ++j) { - binaryImage.append((char) qRed(scanline[j])); - binaryImage.append((char) qGreen(scanline[j])); - binaryImage.append((char) qBlue(scanline[j])); + binaryImage.append((char)qRed(scanline[j])); + binaryImage.append((char)qGreen(scanline[j])); + binaryImage.append((char)qBlue(scanline[j])); } } @@ -387,27 +466,27 @@ PyObject* EffectModule::wrapImageShow(PyObject *self, PyObject *args) return Py_BuildValue(""); } -PyObject* EffectModule::wrapImageLinearGradient(PyObject *self, PyObject *args) +PyObject* EffectModule::wrapImageLinearGradient(PyObject* self, PyObject* args) { int argCount = PyTuple_Size(args); - PyObject * bytearray = nullptr; + PyObject* bytearray = nullptr; int startRX = 0; int startRY = 0; int startX = 0; int startY = 0; int width = getEffect()->_imageSize.width(); - int endX {width}; + int endX{ width }; int height = getEffect()->_imageSize.height(); - int endY {height}; + int endY{ height }; int spread = 0; bool argsOK = false; - if ( argCount == 10 && PyArg_ParseTuple(args, "iiiiiiiiOi", &startRX, &startRY, &width, &height, &startX, &startY, &endX, &endY, &bytearray, &spread) ) + if (argCount == 10 && PyArg_ParseTuple(args, "iiiiiiiiOi", &startRX, &startRY, &width, &height, &startX, &startY, &endX, &endY, &bytearray, &spread)) { argsOK = true; } - if ( argCount == 6 && PyArg_ParseTuple(args, "iiiiOi", &startX, &startY, &endX, &endY, &bytearray, &spread) ) + if (argCount == 6 && PyArg_ParseTuple(args, "iiiiOi", &startX, &startY, &endX, &endY, &bytearray, &spread)) { argsOK = true; } @@ -420,20 +499,20 @@ PyObject* EffectModule::wrapImageLinearGradient(PyObject *self, PyObject *args) const unsigned arrayItemLength = 5; if (length % arrayItemLength == 0) { - QRect myQRect(startRX,startRY,width,height); - QLinearGradient gradient(QPoint(startX,startY), QPoint(endX,endY)); - char * data = PyByteArray_AS_STRING(bytearray); + QRect myQRect(startRX, startRY, width, height); + QLinearGradient gradient(QPoint(startX, startY), QPoint(endX, endY)); + char* data = PyByteArray_AS_STRING(bytearray); - for (int idx=0; idx(spread)); @@ -456,29 +535,29 @@ PyObject* EffectModule::wrapImageLinearGradient(PyObject *self, PyObject *args) return nullptr; } -PyObject* EffectModule::wrapImageConicalGradient(PyObject *self, PyObject *args) +PyObject* EffectModule::wrapImageConicalGradient(PyObject* self, PyObject* args) { int argCount = PyTuple_Size(args); - PyObject * bytearray = nullptr; + PyObject* bytearray = nullptr; int centerX = 0; int centerY = 0; int angle = 0; int startX = 0; int startY = 0; - int width = getEffect()->_imageSize.width(); + int width = getEffect()->_imageSize.width(); int height = getEffect()->_imageSize.height(); bool argsOK = false; - if ( argCount == 8 && PyArg_ParseTuple(args, "iiiiiiiO", &startX, &startY, &width, &height, ¢erX, ¢erY, &angle, &bytearray) ) + if (argCount == 8 && PyArg_ParseTuple(args, "iiiiiiiO", &startX, &startY, &width, &height, ¢erX, ¢erY, &angle, &bytearray)) { argsOK = true; } - if ( argCount == 4 && PyArg_ParseTuple(args, "iiiO", ¢erX, ¢erY, &angle, &bytearray) ) + if (argCount == 4 && PyArg_ParseTuple(args, "iiiO", ¢erX, ¢erY, &angle, &bytearray)) { argsOK = true; } - angle = qMax(qMin(angle,360),0); + angle = qMax(qMin(angle, 360), 0); if (argsOK) { @@ -488,20 +567,20 @@ PyObject* EffectModule::wrapImageConicalGradient(PyObject *self, PyObject *args) const unsigned arrayItemLength = 5; if (length % arrayItemLength == 0) { - QRect myQRect(startX,startY,width,height); - QConicalGradient gradient(QPoint(centerX,centerY), angle ); - char * data = PyByteArray_AS_STRING(bytearray); + QRect myQRect(startX, startY, width, height); + QConicalGradient gradient(QPoint(centerX, centerY), angle); + char* data = PyByteArray_AS_STRING(bytearray); - for (int idx=0; idx_painter->fillRect(myQRect, gradient); @@ -524,44 +603,44 @@ PyObject* EffectModule::wrapImageConicalGradient(PyObject *self, PyObject *args) } -PyObject* EffectModule::wrapImageRadialGradient(PyObject *self, PyObject *args) +PyObject* EffectModule::wrapImageRadialGradient(PyObject* self, PyObject* args) { int argCount = PyTuple_Size(args); - PyObject * bytearray = nullptr; + PyObject* bytearray = nullptr; int centerX = 0; int centerY = 0; int radius = 0; int focalX = 0; int focalY = 0; - int focalRadius =0; + int focalRadius = 0; int spread = 0; int startX = 0; int startY = 0; - int width = getEffect()->_imageSize.width(); + int width = getEffect()->_imageSize.width(); int height = getEffect()->_imageSize.height(); bool argsOK = false; - if ( argCount == 12 && PyArg_ParseTuple(args, "iiiiiiiiiiOi", &startX, &startY, &width, &height, ¢erX, ¢erY, &radius, &focalX, &focalY, &focalRadius, &bytearray, &spread) ) + if (argCount == 12 && PyArg_ParseTuple(args, "iiiiiiiiiiOi", &startX, &startY, &width, &height, ¢erX, ¢erY, &radius, &focalX, &focalY, &focalRadius, &bytearray, &spread)) { - argsOK = true; + argsOK = true; } - if ( argCount == 9 && PyArg_ParseTuple(args, "iiiiiiiOi", &startX, &startY, &width, &height, ¢erX, ¢erY, &radius, &bytearray, &spread) ) + if (argCount == 9 && PyArg_ParseTuple(args, "iiiiiiiOi", &startX, &startY, &width, &height, ¢erX, ¢erY, &radius, &bytearray, &spread)) { - argsOK = true; - focalX = centerX; - focalY = centerY; + argsOK = true; + focalX = centerX; + focalY = centerY; focalRadius = radius; } - if ( argCount == 8 && PyArg_ParseTuple(args, "iiiiiiOi", ¢erX, ¢erY, &radius, &focalX, &focalY, &focalRadius, &bytearray, &spread) ) + if (argCount == 8 && PyArg_ParseTuple(args, "iiiiiiOi", ¢erX, ¢erY, &radius, &focalX, &focalY, &focalRadius, &bytearray, &spread)) { argsOK = true; } - if ( argCount == 5 && PyArg_ParseTuple(args, "iiiOi", ¢erX, ¢erY, &radius, &bytearray, &spread) ) + if (argCount == 5 && PyArg_ParseTuple(args, "iiiOi", ¢erX, ¢erY, &radius, &bytearray, &spread)) { - argsOK = true; - focalX = centerX; - focalY = centerY; + argsOK = true; + focalX = centerX; + focalY = centerY; focalRadius = radius; } @@ -573,19 +652,19 @@ PyObject* EffectModule::wrapImageRadialGradient(PyObject *self, PyObject *args) if (length % 4 == 0) { - QRect myQRect(startX,startY,width,height); - QRadialGradient gradient(QPoint(centerX,centerY), qMax(radius,0) ); - char * data = PyByteArray_AS_STRING(bytearray); + QRect myQRect(startX, startY, width, height); + QRadialGradient gradient(QPoint(centerX, centerY), qMax(radius, 0)); + char* data = PyByteArray_AS_STRING(bytearray); - for (int idx=0; idx(spread)); @@ -608,9 +687,9 @@ PyObject* EffectModule::wrapImageRadialGradient(PyObject *self, PyObject *args) return nullptr; } -PyObject* EffectModule::wrapImageDrawPolygon(PyObject *self, PyObject *args) +PyObject* EffectModule::wrapImageDrawPolygon(PyObject* self, PyObject* args) { - PyObject * bytearray = nullptr; + PyObject* bytearray = nullptr; int argCount = PyTuple_Size(args); int r = 0; @@ -620,11 +699,11 @@ PyObject* EffectModule::wrapImageDrawPolygon(PyObject *self, PyObject *args) bool argsOK = false; - if ( argCount == 5 && PyArg_ParseTuple(args, "Oiiii", &bytearray, &r, &g, &b, &a) ) + if (argCount == 5 && PyArg_ParseTuple(args, "Oiiii", &bytearray, &r, &g, &b, &a)) { argsOK = true; } - if ( argCount == 4 && PyArg_ParseTuple(args, "Oiii", &bytearray, &r, &g, &b) ) + if (argCount == 4 && PyArg_ParseTuple(args, "Oiii", &bytearray, &r, &g, &b)) { argsOK = true; } @@ -637,18 +716,18 @@ PyObject* EffectModule::wrapImageDrawPolygon(PyObject *self, PyObject *args) if (length % 2 == 0) { QVector points; - char * data = PyByteArray_AS_STRING(bytearray); + char* data = PyByteArray_AS_STRING(bytearray); - for (int idx=0; idx_painter; + QPainter* painter = getEffect()->_painter; QPen oldPen = painter->pen(); - QPen newPen(QColor(r,g,b,a)); + QPen newPen(QColor(r, g, b, a)); painter->setPen(newPen); - painter->setBrush(QBrush(QColor(r,g,b,a), Qt::SolidPattern)); + painter->setBrush(QBrush(QColor(r, g, b, a), Qt::SolidPattern)); painter->drawPolygon(points); painter->setPen(oldPen); Py_RETURN_NONE; @@ -668,9 +747,9 @@ PyObject* EffectModule::wrapImageDrawPolygon(PyObject *self, PyObject *args) return nullptr; } -PyObject* EffectModule::wrapImageDrawPie(PyObject *self, PyObject *args) +PyObject* EffectModule::wrapImageDrawPie(PyObject* self, PyObject* args) { - PyObject * bytearray = nullptr; + PyObject* bytearray = nullptr; QString brush; int argCount = PyTuple_Size(args); @@ -686,30 +765,30 @@ PyObject* EffectModule::wrapImageDrawPie(PyObject *self, PyObject *args) bool argsOK = false; - if ( argCount == 9 && PyArg_ParseTuple(args, "iiiiiiiii", ¢erX, ¢erY, &radius, &startAngle, &spanAngle, &r, &g, &b, &a) ) + if (argCount == 9 && PyArg_ParseTuple(args, "iiiiiiiii", ¢erX, ¢erY, &radius, &startAngle, &spanAngle, &r, &g, &b, &a)) { argsOK = true; } - if ( argCount == 8 && PyArg_ParseTuple(args, "iiiiiiii", ¢erX, ¢erY, &radius, &startAngle, &spanAngle, &r, &g, &b) ) + if (argCount == 8 && PyArg_ParseTuple(args, "iiiiiiii", ¢erX, ¢erY, &radius, &startAngle, &spanAngle, &r, &g, &b)) { argsOK = true; } - if ( argCount == 7 && PyArg_ParseTuple(args, "iiiiisO", ¢erX, ¢erY, &radius, &startAngle, &spanAngle, &brush, &bytearray) ) + if (argCount == 7 && PyArg_ParseTuple(args, "iiiiisO", ¢erX, ¢erY, &radius, &startAngle, &spanAngle, &brush, &bytearray)) { argsOK = true; } - if ( argCount == 5 && PyArg_ParseTuple(args, "iiisO", ¢erX, ¢erY, &radius, &brush, &bytearray) ) + if (argCount == 5 && PyArg_ParseTuple(args, "iiisO", ¢erX, ¢erY, &radius, &brush, &bytearray)) { argsOK = true; } if (argsOK) { - QPainter * painter = getEffect()->_painter; - startAngle = qMax(qMin(startAngle,360),0); - spanAngle = qMax(qMin(spanAngle,360),-360); + QPainter* painter = getEffect()->_painter; + startAngle = qMax(qMin(startAngle, 360), 0); + spanAngle = qMax(qMin(spanAngle, 360), -360); - if( argCount == 7 || argCount == 5 ) + if (argCount == 7 || argCount == 5) { a = 0; if (PyByteArray_Check(bytearray)) @@ -718,21 +797,21 @@ PyObject* EffectModule::wrapImageDrawPie(PyObject *self, PyObject *args) if (length % 5 == 0) { - QConicalGradient gradient(QPoint(centerX,centerY), startAngle); + QConicalGradient gradient(QPoint(centerX, centerY), startAngle); - char * data = PyByteArray_AS_STRING(bytearray); + char* data = PyByteArray_AS_STRING(bytearray); - for (int idx=0; idxsetBrush(gradient); @@ -752,10 +831,10 @@ PyObject* EffectModule::wrapImageDrawPie(PyObject *self, PyObject *args) } else { - painter->setBrush(QBrush(QColor(r,g,b,a), Qt::SolidPattern)); + painter->setBrush(QBrush(QColor(r, g, b, a), Qt::SolidPattern)); } QPen oldPen = painter->pen(); - QPen newPen(QColor(r,g,b,a)); + QPen newPen(QColor(r, g, b, a)); painter->setPen(newPen); painter->drawPie(centerX - radius, centerY - radius, centerX + radius, centerY + radius, startAngle * 16, spanAngle * 16); painter->setPen(oldPen); @@ -764,7 +843,7 @@ PyObject* EffectModule::wrapImageDrawPie(PyObject *self, PyObject *args) return nullptr; } -PyObject* EffectModule::wrapImageSolidFill(PyObject *self, PyObject *args) +PyObject* EffectModule::wrapImageSolidFill(PyObject* self, PyObject* args) { int argCount = PyTuple_Size(args); int r = 0; @@ -773,39 +852,39 @@ PyObject* EffectModule::wrapImageSolidFill(PyObject *self, PyObject *args) int a = 255; int startX = 0; int startY = 0; - int width = getEffect()->_imageSize.width(); + int width = getEffect()->_imageSize.width(); int height = getEffect()->_imageSize.height(); bool argsOK = false; - if ( argCount == 8 && PyArg_ParseTuple(args, "iiiiiiii", &startX, &startY, &width, &height, &r, &g, &b, &a) ) + if (argCount == 8 && PyArg_ParseTuple(args, "iiiiiiii", &startX, &startY, &width, &height, &r, &g, &b, &a)) { argsOK = true; } - if ( argCount == 7 && PyArg_ParseTuple(args, "iiiiiii", &startX, &startY, &width, &height, &r, &g, &b) ) + if (argCount == 7 && PyArg_ParseTuple(args, "iiiiiii", &startX, &startY, &width, &height, &r, &g, &b)) { argsOK = true; } - if ( argCount == 4 && PyArg_ParseTuple(args, "iiii",&r, &g, &b, &a) ) + if (argCount == 4 && PyArg_ParseTuple(args, "iiii", &r, &g, &b, &a)) { argsOK = true; } - if ( argCount == 3 && PyArg_ParseTuple(args, "iii",&r, &g, &b) ) + if (argCount == 3 && PyArg_ParseTuple(args, "iii", &r, &g, &b)) { argsOK = true; } if (argsOK) { - QRect myQRect(startX,startY,width,height); - getEffect()->_painter->fillRect(myQRect, QColor(r,g,b,a)); + QRect myQRect(startX, startY, width, height); + getEffect()->_painter->fillRect(myQRect, QColor(r, g, b, a)); Py_RETURN_NONE; } return nullptr; } -PyObject* EffectModule::wrapImageDrawLine(PyObject *self, PyObject *args) +PyObject* EffectModule::wrapImageDrawLine(PyObject* self, PyObject* args) { int argCount = PyTuple_Size(args); int r = 0; @@ -814,27 +893,27 @@ PyObject* EffectModule::wrapImageDrawLine(PyObject *self, PyObject *args) int a = 255; int startX = 0; int startY = 0; - int thick = 1; - int endX = getEffect()->_imageSize.width(); - int endY = getEffect()->_imageSize.height(); + int thick = 1; + int endX = getEffect()->_imageSize.width(); + int endY = getEffect()->_imageSize.height(); bool argsOK = false; - if ( argCount == 9 && PyArg_ParseTuple(args, "iiiiiiiii", &startX, &startY, &endX, &endY, &thick, &r, &g, &b, &a) ) + if (argCount == 9 && PyArg_ParseTuple(args, "iiiiiiiii", &startX, &startY, &endX, &endY, &thick, &r, &g, &b, &a)) { argsOK = true; } - if ( argCount == 8 && PyArg_ParseTuple(args, "iiiiiiii", &startX, &startY, &endX, &endY, &thick, &r, &g, &b) ) + if (argCount == 8 && PyArg_ParseTuple(args, "iiiiiiii", &startX, &startY, &endX, &endY, &thick, &r, &g, &b)) { argsOK = true; } if (argsOK) { - QPainter * painter = getEffect()->_painter; + QPainter* painter = getEffect()->_painter; QRect myQRect(startX, startY, endX, endY); QPen oldPen = painter->pen(); - QPen newPen(QColor(r,g,b,a)); + QPen newPen(QColor(r, g, b, a)); newPen.setWidth(thick); painter->setPen(newPen); painter->drawLine(startX, startY, endX, endY); @@ -845,7 +924,7 @@ PyObject* EffectModule::wrapImageDrawLine(PyObject *self, PyObject *args) return nullptr; } -PyObject* EffectModule::wrapImageDrawPoint(PyObject *self, PyObject *args) +PyObject* EffectModule::wrapImageDrawPoint(PyObject* self, PyObject* args) { int argCount = PyTuple_Size(args); int r = 0; @@ -854,24 +933,24 @@ PyObject* EffectModule::wrapImageDrawPoint(PyObject *self, PyObject *args) int x = 0; int y = 0; int a = 255; - int thick = 1; + int thick = 1; bool argsOK = false; - if ( argCount == 7 && PyArg_ParseTuple(args, "iiiiiii", &x, &y, &thick, &r, &g, &b, &a) ) + if (argCount == 7 && PyArg_ParseTuple(args, "iiiiiii", &x, &y, &thick, &r, &g, &b, &a)) { argsOK = true; } - if ( argCount == 6 && PyArg_ParseTuple(args, "iiiiii", &x, &y, &thick, &r, &g, &b) ) + if (argCount == 6 && PyArg_ParseTuple(args, "iiiiii", &x, &y, &thick, &r, &g, &b)) { argsOK = true; } if (argsOK) { - QPainter * painter = getEffect()->_painter; + QPainter* painter = getEffect()->_painter; QPen oldPen = painter->pen(); - QPen newPen(QColor(r,g,b,a)); + QPen newPen(QColor(r, g, b, a)); newPen.setWidth(thick); painter->setPen(newPen); painter->drawPoint(x, y); @@ -882,7 +961,7 @@ PyObject* EffectModule::wrapImageDrawPoint(PyObject *self, PyObject *args) return nullptr; } -PyObject* EffectModule::wrapImageDrawRect(PyObject *self, PyObject *args) +PyObject* EffectModule::wrapImageDrawRect(PyObject* self, PyObject* args) { int argCount = PyTuple_Size(args); int r = 0; @@ -891,27 +970,27 @@ PyObject* EffectModule::wrapImageDrawRect(PyObject *self, PyObject *args) int a = 255; int startX = 0; int startY = 0; - int thick = 1; - int width = getEffect()->_imageSize.width(); - int height = getEffect()->_imageSize.height(); + int thick = 1; + int width = getEffect()->_imageSize.width(); + int height = getEffect()->_imageSize.height(); bool argsOK = false; - if ( argCount == 9 && PyArg_ParseTuple(args, "iiiiiiiii", &startX, &startY, &width, &height, &thick, &r, &g, &b, &a) ) + if (argCount == 9 && PyArg_ParseTuple(args, "iiiiiiiii", &startX, &startY, &width, &height, &thick, &r, &g, &b, &a)) { argsOK = true; } - if ( argCount == 8 && PyArg_ParseTuple(args, "iiiiiiii", &startX, &startY, &width, &height, &thick, &r, &g, &b) ) + if (argCount == 8 && PyArg_ParseTuple(args, "iiiiiiii", &startX, &startY, &width, &height, &thick, &r, &g, &b)) { argsOK = true; } if (argsOK) { - QPainter * painter = getEffect()->_painter; - QRect myQRect(startX,startY,width,height); + QPainter* painter = getEffect()->_painter; + QRect myQRect(startX, startY, width, height); QPen oldPen = painter->pen(); - QPen newPen(QColor(r,g,b,a)); + QPen newPen(QColor(r, g, b, a)); newPen.setWidth(thick); painter->setPen(newPen); painter->drawRect(startX, startY, width, height); @@ -923,7 +1002,7 @@ PyObject* EffectModule::wrapImageDrawRect(PyObject *self, PyObject *args) } -PyObject* EffectModule::wrapImageSetPixel(PyObject *self, PyObject *args) +PyObject* EffectModule::wrapImageSetPixel(PyObject* self, PyObject* args) { int argCount = PyTuple_Size(args); int r = 0; @@ -932,9 +1011,9 @@ PyObject* EffectModule::wrapImageSetPixel(PyObject *self, PyObject *args) int x = 0; int y = 0; - if ( argCount == 5 && PyArg_ParseTuple(args, "iiiii", &x, &y, &r, &g, &b ) ) + if (argCount == 5 && PyArg_ParseTuple(args, "iiiii", &x, &y, &r, &g, &b)) { - getEffect()->_image.setPixel(x,y,qRgb(r,g,b)); + getEffect()->_image.setPixel(x, y, qRgb(r, g, b)); Py_RETURN_NONE; } @@ -942,43 +1021,43 @@ PyObject* EffectModule::wrapImageSetPixel(PyObject *self, PyObject *args) } -PyObject* EffectModule::wrapImageGetPixel(PyObject *self, PyObject *args) +PyObject* EffectModule::wrapImageGetPixel(PyObject* self, PyObject* args) { int argCount = PyTuple_Size(args); int x = 0; int y = 0; - if ( argCount == 2 && PyArg_ParseTuple(args, "ii", &x, &y) ) + if (argCount == 2 && PyArg_ParseTuple(args, "ii", &x, &y)) { - QRgb rgb = getEffect()->_image.pixel(x,y); - return Py_BuildValue("iii",qRed(rgb),qGreen(rgb),qBlue(rgb)); + QRgb rgb = getEffect()->_image.pixel(x, y); + return Py_BuildValue("iii", qRed(rgb), qGreen(rgb), qBlue(rgb)); } return nullptr; } -PyObject* EffectModule::wrapImageSave(PyObject *self, PyObject *args) +PyObject* EffectModule::wrapImageSave(PyObject* self, PyObject* args) { QImage img(getEffect()->_image.copy()); getEffect()->_imageStack.append(img); - return Py_BuildValue("i", getEffect()->_imageStack.size()-1); + return Py_BuildValue("i", getEffect()->_imageStack.size() - 1); } -PyObject* EffectModule::wrapImageMinSize(PyObject *self, PyObject *args) +PyObject* EffectModule::wrapImageMinSize(PyObject* self, PyObject* args) { int argCount = PyTuple_Size(args); int w = 0; int h = 0; - int width = getEffect()->_imageSize.width(); - int height = getEffect()->_imageSize.height(); + int width = getEffect()->_imageSize.width(); + int height = getEffect()->_imageSize.height(); - if ( argCount == 2 && PyArg_ParseTuple(args, "ii", &w, &h) ) + if (argCount == 2 && PyArg_ParseTuple(args, "ii", &w, &h)) { - if (width_painter; - getEffect()->_image = getEffect()->_image.scaled(qMax(width,w),qMax(height,h), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); + getEffect()->_image = getEffect()->_image.scaled(qMax(width, w), qMax(height, h), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); getEffect()->_imageSize = getEffect()->_image.size(); getEffect()->_painter = new QPainter(&(getEffect()->_image)); } @@ -987,60 +1066,60 @@ PyObject* EffectModule::wrapImageMinSize(PyObject *self, PyObject *args) return nullptr; } -PyObject* EffectModule::wrapImageWidth(PyObject *self, PyObject *args) +PyObject* EffectModule::wrapImageWidth(PyObject* self, PyObject* args) { return Py_BuildValue("i", getEffect()->_imageSize.width()); } -PyObject* EffectModule::wrapImageHeight(PyObject *self, PyObject *args) +PyObject* EffectModule::wrapImageHeight(PyObject* self, PyObject* args) { return Py_BuildValue("i", getEffect()->_imageSize.height()); } -PyObject* EffectModule::wrapImageCRotate(PyObject *self, PyObject *args) +PyObject* EffectModule::wrapImageCRotate(PyObject* self, PyObject* args) { int argCount = PyTuple_Size(args); int angle; - if ( argCount == 1 && PyArg_ParseTuple(args, "i", &angle ) ) + if (argCount == 1 && PyArg_ParseTuple(args, "i", &angle)) { - angle = qMax(qMin(angle,360),0); + angle = qMax(qMin(angle, 360), 0); getEffect()->_painter->rotate(angle); Py_RETURN_NONE; } return nullptr; } -PyObject* EffectModule::wrapImageCOffset(PyObject *self, PyObject *args) +PyObject* EffectModule::wrapImageCOffset(PyObject* self, PyObject* args) { int offsetX = 0; int offsetY = 0; int argCount = PyTuple_Size(args); - if ( argCount == 2 ) + if (argCount == 2) { - PyArg_ParseTuple(args, "ii", &offsetX, &offsetY ); + PyArg_ParseTuple(args, "ii", &offsetX, &offsetY); } - getEffect()->_painter->translate(QPoint(offsetX,offsetY)); + getEffect()->_painter->translate(QPoint(offsetX, offsetY)); Py_RETURN_NONE; } -PyObject* EffectModule::wrapImageCShear(PyObject *self, PyObject *args) +PyObject* EffectModule::wrapImageCShear(PyObject* self, PyObject* args) { int sh = 0; int sv = 0; int argCount = PyTuple_Size(args); - if ( argCount == 2 && PyArg_ParseTuple(args, "ii", &sh, &sv )) + if (argCount == 2 && PyArg_ParseTuple(args, "ii", &sh, &sv)) { - getEffect()->_painter->shear(sh,sv); + getEffect()->_painter->shear(sh, sv); Py_RETURN_NONE; } return nullptr; } -PyObject* EffectModule::wrapImageResetT(PyObject *self, PyObject *args) +PyObject* EffectModule::wrapImageResetT(PyObject* self, PyObject* args) { getEffect()->_painter->resetTransform(); Py_RETURN_NONE; diff --git a/libsrc/python/PythonInit.cpp b/libsrc/python/PythonInit.cpp index 5684c24d5..31375ad13 100644 --- a/libsrc/python/PythonInit.cpp +++ b/libsrc/python/PythonInit.cpp @@ -18,7 +18,7 @@ #include #ifdef _WIN32 - #include +#include #endif #define STRINGIFY2(x) #x @@ -44,14 +44,14 @@ PythonInit::PythonInit() #if (PY_VERSION_HEX >= 0x03080000) status = PyConfig_SetString(&config, &config.program_name, programName); if (PyStatus_Exception(status)) { - goto exception; + handlePythonError(status, config); + return; } - else #else Py_SetProgramName(programName); #endif { - // set Python module path when exists + // set Python module path when it exists QString py_path = QDir::cleanPath(qApp->applicationDirPath() + "/../lib/python" + STRINGIFY(PYTHON_VERSION_MAJOR) + "." + STRINGIFY(PYTHON_VERSION_MINOR)); QString py_file = QDir::cleanPath(qApp->applicationDirPath() + "/python" + STRINGIFY(PYTHON_VERSION_MAJOR) + STRINGIFY(PYTHON_VERSION_MINOR) + ".zip"); QString py_framework = QDir::cleanPath(qApp->applicationDirPath() + "/../Frameworks/Python.framework/Versions/Current/lib/python" + STRINGIFY(PYTHON_VERSION_MAJOR) + "." + STRINGIFY(PYTHON_VERSION_MINOR)); @@ -59,21 +59,23 @@ PythonInit::PythonInit() if (QFile(py_file).exists() || QDir(py_path).exists() || QDir(py_framework).exists()) { #if (PY_VERSION_HEX >= 0x030C0000) - config.site_import = 0; + config.site_import = 0; #else - Py_NoSiteFlag++; + Py_NoSiteFlag++; #endif if (QFile(py_file).exists()) // Windows { #if (PY_VERSION_HEX >= 0x03080000) status = PyConfig_SetBytesString(&config, &config.home, QSTRING_CSTR(py_file)); if (PyStatus_Exception(status)) { - goto exception; + handlePythonError(status, config); + return; } config.module_search_paths_set = 1; status = PyWideStringList_Append(&config.module_search_paths, const_cast(py_file.toStdWString().c_str())); if (PyStatus_Exception(status)) { - goto exception; + handlePythonError(status, config); + return; } #else Py_SetPythonHome(Py_DecodeLocale(py_file.toLatin1().data(), nullptr)); @@ -85,18 +87,21 @@ PythonInit::PythonInit() #if (PY_VERSION_HEX >= 0x03080000) status = PyConfig_SetBytesString(&config, &config.home, QSTRING_CSTR(QDir::cleanPath(qApp->applicationDirPath() + "/../"))); if (PyStatus_Exception(status)) { - goto exception; + handlePythonError(status, config); + return; } config.module_search_paths_set = 1; status = PyWideStringList_Append(&config.module_search_paths, const_cast(QDir(py_path).absolutePath().toStdWString().c_str())); if (PyStatus_Exception(status)) { - goto exception; + handlePythonError(status, config); + return; } status = PyWideStringList_Append(&config.module_search_paths, const_cast(QDir(py_path + "/lib-dynload").absolutePath().toStdWString().c_str())); if (PyStatus_Exception(status)) { - goto exception; + handlePythonError(status, config); + return; } #else QStringList python_paths; @@ -114,18 +119,21 @@ PythonInit::PythonInit() #if (PY_VERSION_HEX >= 0x03080000) status = PyConfig_SetBytesString(&config, &config.home, QSTRING_CSTR(QDir::cleanPath(qApp->applicationDirPath() + "/../Frameworks/Python.framework/Versions/Current"))); if (PyStatus_Exception(status)) { - goto exception; + handlePythonError(status, config); + return; } config.module_search_paths_set = 1; status = PyWideStringList_Append(&config.module_search_paths, const_cast(QDir(py_framework).absolutePath().toStdWString().c_str())); if (PyStatus_Exception(status)) { - goto exception; + handlePythonError(status, config); + return; } status = PyWideStringList_Append(&config.module_search_paths, const_cast(QDir(py_framework + "/lib-dynload").absolutePath().toStdWString().c_str())); if (PyStatus_Exception(status)) { - goto exception; + handlePythonError(status, config); + return; } #else QStringList python_paths; @@ -146,7 +154,8 @@ PythonInit::PythonInit() #if (PY_VERSION_HEX >= 0x03080000) status = Py_InitializeFromConfig(&config); if (PyStatus_Exception(status)) { - goto exception; + handlePythonError(status, config); + return; } PyConfig_Clear(&config); #endif @@ -154,7 +163,8 @@ PythonInit::PythonInit() // init Python Debug(Logger::getInstance("DAEMON"), "Initializing Python interpreter"); Py_InitializeEx(0); - if ( !Py_IsInitialized() ) + + if (!Py_IsInitialized()) { throw std::runtime_error("Initializing Python failed!"); } @@ -165,20 +175,25 @@ PythonInit::PythonInit() #endif mainThreadState = PyEval_SaveThread(); - return; +} -#if (PY_VERSION_HEX >= 0x03080000) -exception: +// Error handling function to replace goto exception +void PythonInit::handlePythonError(PyStatus status, PyConfig& config) +{ Error(Logger::getInstance("DAEMON"), "Initializing Python config failed with error [%s]", status.err_msg); PyConfig_Clear(&config); - throw std::runtime_error("Initializing Python failed!"); -#endif } PythonInit::~PythonInit() { Debug(Logger::getInstance("DAEMON"), "Cleaning up Python interpreter"); + +#if (PY_VERSION_HEX < 0x030C0000) PyEval_RestoreThread(mainThreadState); - Py_Finalize(); +#else + PyThreadState_Swap(mainThreadState); +#endif + + int rc = Py_FinalizeEx(); } diff --git a/libsrc/python/PythonProgram.cpp b/libsrc/python/PythonProgram.cpp index e39839f2d..805744742 100644 --- a/libsrc/python/PythonProgram.cpp +++ b/libsrc/python/PythonProgram.cpp @@ -1,173 +1,184 @@ #include #include + #include #include PyThreadState* mainThreadState; -PythonProgram::PythonProgram(const QString & name, Logger * log) : - _name(name), _log(log), _tstate(nullptr) +PythonProgram::PythonProgram(const QString& name, Logger* log) : + _name(name) + , _log(log) + , _tstate(nullptr) { // we probably need to wait until mainThreadState is available - while(mainThreadState == nullptr){}; + QThread::msleep(10); + while (mainThreadState == nullptr) + { + QThread::msleep(10); // Wait with delay to avoid busy waiting + } + // Create a new subinterpreter for this thread +#if (PY_VERSION_HEX < 0x030C0000) // get global lock PyEval_RestoreThread(mainThreadState); - - // Initialize a new thread state _tstate = Py_NewInterpreter(); - if(_tstate == nullptr) +#else + PyThreadState* prev = PyThreadState_Swap(NULL); + + // Create a new interpreter configuration object + PyInterpreterConfig config{}; + + // Set configuration options + config.use_main_obmalloc = 0; + config.allow_fork = 0; + config.allow_exec = 0; + config.allow_threads = 1; + config.allow_daemon_threads = 0; + config.check_multi_interp_extensions = 1; + config.gil = PyInterpreterConfig_OWN_GIL; + Py_NewInterpreterFromConfig(&_tstate, &config); +#endif + + if (_tstate == nullptr) { -#if (PY_VERSION_HEX >= 0x03020000) PyThreadState_Swap(mainThreadState); +#if (PY_VERSION_HEX < 0x030C0000) PyEval_SaveThread(); -#else - PyEval_ReleaseLock(); #endif - Error(_log, "Failed to get thread state for %s",QSTRING_CSTR(_name)); + Error(_log, "Failed to get thread state for %s", QSTRING_CSTR(_name)); return; } +#if (PY_VERSION_HEX < 0x030C0000) PyThreadState_Swap(_tstate); +#endif } PythonProgram::~PythonProgram() { if (!_tstate) - return; - - // stop sub threads if needed - for (PyThreadState* s = PyInterpreterState_ThreadHead(_tstate->interp), *old = nullptr; s;) { - if (s == _tstate) - { - s = s->next; - continue; - } - if (old != s) - { - Debug(_log,"ID %s: Waiting on thread %u", QSTRING_CSTR(_name), s->thread_id); - old = s; - } - - Py_BEGIN_ALLOW_THREADS; - QThread::msleep(100); - Py_END_ALLOW_THREADS; - - s = PyInterpreterState_ThreadHead(_tstate->interp); + return; } +#if (PY_VERSION_HEX < 0x030C0000) + PyThreadState* prev_thread_state = PyThreadState_Swap(_tstate); +#endif + // Clean up the thread state Py_EndInterpreter(_tstate); -#if (PY_VERSION_HEX >= 0x03020000) - PyThreadState_Swap(mainThreadState); + +#if (PY_VERSION_HEX < 0x030C0000) + PyThreadState_Swap(prev_thread_state); PyEval_SaveThread(); -#else - PyEval_ReleaseLock(); #endif } -void PythonProgram::execute(const QByteArray & python_code) +void PythonProgram::execute(const QByteArray& python_code) { if (!_tstate) + { return; + } + + PyThreadState* prev_thread_state = PyThreadState_Swap(_tstate); - PyObject *main_module = PyImport_ImportModule("__main__"); // New Reference - PyObject *main_dict = PyModule_GetDict(main_module); // Borrowed reference - Py_INCREF(main_dict); // Incref "main_dict" to use it in PyRun_String(), because PyModule_GetDict() has decref "main_dict" - Py_DECREF(main_module); // // release "main_module" when done - PyObject *result = PyRun_String(python_code.constData(), Py_file_input, main_dict, main_dict); // New Reference + PyObject* main_module = PyImport_ImportModule("__main__"); + if (!main_module) + { + // Restore the previous thread state +#if (PY_VERSION_HEX < 0x030C0000) + PyThreadState_Swap(mainThreadState); +#else + PyThreadState_Swap(prev_thread_state); +#endif + return; + } + PyObject* main_dict = PyModule_GetDict(main_module); // Borrowed reference to globals + PyObject* result = PyRun_String(python_code.constData(), Py_file_input, main_dict, main_dict); if (!result) { - if (PyErr_Occurred()) // Nothing needs to be done for a borrowed reference + if (PyErr_Occurred()) { - Error(_log,"###### PYTHON EXCEPTION ######"); - Error(_log,"## In effect '%s'", QSTRING_CSTR(_name)); - /* Objects all initialized to NULL for Py_XDECREF */ - PyObject *errorType = NULL, *errorValue = NULL, *errorTraceback = NULL; + Error(_log, "###### PYTHON EXCEPTION ######"); + Error(_log, "## In effect '%s'", QSTRING_CSTR(_name)); + + PyObject* errorType = NULL, * errorValue = NULL, * errorTraceback = NULL; - PyErr_Fetch(&errorType, &errorValue, &errorTraceback); // New Reference or NULL + PyErr_Fetch(&errorType, &errorValue, &errorTraceback); PyErr_NormalizeException(&errorType, &errorValue, &errorTraceback); - // Extract exception message from "errorValue" - if(errorValue) + if (errorValue) { QString message; - if(PyObject_HasAttrString(errorValue, "__class__")) + if (PyObject_HasAttrString(errorValue, "__class__")) { - PyObject *classPtr = PyObject_GetAttrString(errorValue, "__class__"); // New Reference - PyObject *class_name = NULL; /* Object "class_name" initialized to NULL for Py_XDECREF */ - class_name = PyObject_GetAttrString(classPtr, "__name__"); // New Reference or NULL + PyObject* classPtr = PyObject_GetAttrString(errorValue, "__class__"); + PyObject* class_name = classPtr ? PyObject_GetAttrString(classPtr, "__name__") : NULL; - if(class_name && PyUnicode_Check(class_name)) + if (class_name && PyUnicode_Check(class_name)) message.append(PyUnicode_AsUTF8(class_name)); - Py_DECREF(classPtr); // release "classPtr" when done - Py_XDECREF(class_name); // Use Py_XDECREF() to ignore NULL references + Py_XDECREF(class_name); + Py_DECREF(classPtr); } - // Object "class_name" initialized to NULL for Py_XDECREF - PyObject *valueString = NULL; - valueString = PyObject_Str(errorValue); // New Reference or NULL + PyObject* valueString = PyObject_Str(errorValue); - if(valueString && PyUnicode_Check(valueString)) + if (valueString && PyUnicode_Check(valueString)) { - if(!message.isEmpty()) + if (!message.isEmpty()) message.append(": "); - message.append(PyUnicode_AsUTF8(valueString)); } - Py_XDECREF(valueString); // Use Py_XDECREF() to ignore NULL references + Py_XDECREF(valueString); Error(_log, "## %s", QSTRING_CSTR(message)); } - // Extract exception message from "errorTraceback" - if(errorTraceback) + if (errorTraceback) { - // Object "tracebackList" initialized to NULL for Py_XDECREF - PyObject *tracebackModule = NULL, *methodName = NULL, *tracebackList = NULL; - QString tracebackMsg; + PyObject* tracebackModule = PyImport_ImportModule("traceback"); + PyObject* methodName = PyUnicode_FromString("format_exception"); + PyObject* tracebackList = tracebackModule && methodName + ? PyObject_CallMethodObjArgs(tracebackModule, methodName, errorType, errorValue, errorTraceback, NULL) + : NULL; - tracebackModule = PyImport_ImportModule("traceback"); // New Reference or NULL - methodName = PyUnicode_FromString("format_exception"); // New Reference or NULL - tracebackList = PyObject_CallMethodObjArgs(tracebackModule, methodName, errorType, errorValue, errorTraceback, NULL); // New Reference or NULL - - if(tracebackList) + if (tracebackList) { - PyObject* iterator = PyObject_GetIter(tracebackList); // New Reference - + PyObject* iterator = PyObject_GetIter(tracebackList); PyObject* item; - while( (item = PyIter_Next(iterator)) ) // New Reference + while ((item = PyIter_Next(iterator))) { - Error(_log, "## %s",QSTRING_CSTR(QString(PyUnicode_AsUTF8(item)).trimmed())); - Py_DECREF(item); // release "item" when done + Error(_log, "## %s", QSTRING_CSTR(QString(PyUnicode_AsUTF8(item)).trimmed())); + Py_DECREF(item); } - Py_DECREF(iterator); // release "iterator" when done + Py_DECREF(iterator); } - // Use Py_XDECREF() to ignore NULL references Py_XDECREF(tracebackModule); Py_XDECREF(methodName); Py_XDECREF(tracebackList); - // Give the exception back to python and print it to stderr in case anyone else wants it. - Py_XINCREF(errorType); - Py_XINCREF(errorValue); - Py_XINCREF(errorTraceback); - PyErr_Restore(errorType, errorValue, errorTraceback); - //PyErr_PrintEx(0); // Remove this line to switch off stderr output } - Error(_log,"###### EXCEPTION END ######"); + Error(_log, "###### EXCEPTION END ######"); } } else { - Py_DECREF(result); // release "result" when done + Py_DECREF(result); // Release result when done } - Py_DECREF(main_dict); // release "main_dict" when done + Py_DECREF(main_module); + + // Restore the previous thread state +#if (PY_VERSION_HEX < 0x030C0000) + PyThreadState_Swap(mainThreadState); +#else + PyThreadState_Swap(prev_thread_state); +#endif } From 64811f0457c7c5b39241c53a5140b9aed60e46a9 Mon Sep 17 00:00:00 2001 From: Lord-Grey Date: Thu, 14 Nov 2024 23:04:49 +0100 Subject: [PATCH 05/13] Windows add bcrypt until mbedtls is fixed (https://github.com/Mbed-TLS/mbedtls/pull/9554) --- libsrc/leddevice/CMakeLists.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libsrc/leddevice/CMakeLists.txt b/libsrc/leddevice/CMakeLists.txt index 6a03e3b80..e281ca97d 100644 --- a/libsrc/leddevice/CMakeLists.txt +++ b/libsrc/leddevice/CMakeLists.txt @@ -115,7 +115,11 @@ if(ENABLE_DEV_NETWORK) if(NOT DEFAULT_USE_SYSTEM_MBEDTLS_LIBS) if(MBEDTLS_LIBRARIES) include_directories(${MBEDTLS_INCLUDE_DIR}) - target_link_libraries(leddevice ${MBEDTLS_LIBRARIES}) + target_link_libraries( + leddevice + ${MBEDTLS_LIBRARIES} + $<$:bcrypt.lib> + ) target_include_directories(leddevice PRIVATE ${MBEDTLS_INCLUDE_DIR}) endif (MBEDTLS_LIBRARIES) endif() From d021a1bc77a960cf22e071eed05e026123ecae51 Mon Sep 17 00:00:00 2001 From: LordGrey <48840279+Lord-Grey@users.noreply.github.com> Date: Fri, 15 Nov 2024 12:37:43 +0100 Subject: [PATCH 06/13] Corrections --- include/python/PythonInit.h | 2 ++ libsrc/effectengine/EffectModule.cpp | 4 ++-- libsrc/python/PythonInit.cpp | 3 +++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/include/python/PythonInit.h b/include/python/PythonInit.h index bb3091160..316d84642 100644 --- a/include/python/PythonInit.h +++ b/include/python/PythonInit.h @@ -15,5 +15,7 @@ class PythonInit PythonInit(); ~PythonInit(); +#if (PY_VERSION_HEX >= 0x03080000) void handlePythonError(PyStatus status, PyConfig& config); +#endif }; diff --git a/libsrc/effectengine/EffectModule.cpp b/libsrc/effectengine/EffectModule.cpp index f80035d3c..69c01f1f0 100644 --- a/libsrc/effectengine/EffectModule.cpp +++ b/libsrc/effectengine/EffectModule.cpp @@ -57,13 +57,13 @@ static PyObject* hyperion_create(PyModuleDef* def, PyObject* args) { } // Module deallocation function to clean up per-interpreter state -static void hyperion_free(void* module) +static void hyperion_free(void* /* module */) { // No specific cleanup required in this example } static PyModuleDef_Slot hyperion_slots[] = { - {Py_mod_exec, hyperion_exec}, + {Py_mod_exec, reinterpret_cast(hyperion_exec)}, #if (PY_VERSION_HEX >= 0x030C0000) {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED}, #endif diff --git a/libsrc/python/PythonInit.cpp b/libsrc/python/PythonInit.cpp index 31375ad13..5f8067fbf 100644 --- a/libsrc/python/PythonInit.cpp +++ b/libsrc/python/PythonInit.cpp @@ -178,12 +178,14 @@ PythonInit::PythonInit() } // Error handling function to replace goto exception +#if (PY_VERSION_HEX >= 0x03080000) void PythonInit::handlePythonError(PyStatus status, PyConfig& config) { Error(Logger::getInstance("DAEMON"), "Initializing Python config failed with error [%s]", status.err_msg); PyConfig_Clear(&config); throw std::runtime_error("Initializing Python failed!"); } +#endif PythonInit::~PythonInit() { @@ -196,4 +198,5 @@ PythonInit::~PythonInit() #endif int rc = Py_FinalizeEx(); + Debug(Logger::getInstance("DAEMON"), "Cleaning up Python interpreter %s", rc == 0 ? "succeeded" : "failed"); } From b76ccd200b4a4f172ef55b79ae3fd6e1b76bf9a1 Mon Sep 17 00:00:00 2001 From: Paulchen-Panther <16664240+Paulchen-Panther@users.noreply.github.com> Date: Sat, 16 Nov 2024 16:18:40 +0100 Subject: [PATCH 07/13] Use ScreenCaptureKit under macOS 15 and above --- CMakePresets.json | 10 -- cmake/osxbundle/Info.plist.in | 2 + include/grabber/osx/OsxFrameGrabber.h | 2 +- libsrc/grabber/osx/CMakeLists.txt | 20 ++- ...OsxFrameGrabber.cpp => OsxFrameGrabber.mm} | 138 ++++++++++++++---- 5 files changed, 131 insertions(+), 41 deletions(-) rename libsrc/grabber/osx/{OsxFrameGrabber.cpp => OsxFrameGrabber.mm} (56%) diff --git a/CMakePresets.json b/CMakePresets.json index 83ed2dc3c..1f0dc222d 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -51,7 +51,6 @@ "name": "hyperion-bare-minimum", "hidden": true, "cacheVariables": { - // Disable Grabbers "ENABLE_AMLOGIC": "OFF", "ENABLE_DDA": "OFF", "ENABLE_DISPMANX": "OFF", @@ -64,8 +63,6 @@ "ENABLE_X11": "OFF", "ENABLE_XCB": "OFF", "ENABLE_AUDIO": "OFF", - - // LED-Devices "ENABLE_DEV_FTDI": "OFF", "ENABLE_DEV_NETWORK": "OFF", "ENABLE_DEV_SERIAL": "ON", @@ -73,23 +70,16 @@ "ENABLE_DEV_TINKERFORGE": "OFF", "ENABLE_DEV_USB_HID": "OFF", "ENABLE_DEV_WS281XPWM": "OFF", - - // Disable Input Servers "ENABLE_BOBLIGHT_SERVER": "OFF", "ENABLE_CEC": "OFF", "ENABLE_FLATBUF_SERVER": "OFF", "ENABLE_PROTOBUF_SERVER": "OFF", - - // Disable Output Connectors "ENABLE_FORWARDER": "OFF", "ENABLE_FLATBUF_CONNECT": "OFF", - - // Disable Services "ENABLE_EXPERIMENTAL": "OFF", "ENABLE_MDNS": "OFF", "ENABLE_REMOTE_CTL": "OFF", "ENABLE_EFFECTENGINE": "OFF", - "ENABLE_JSONCHECKS": "ON", "ENABLE_DEPLOY_DEPENDENCIES": "ON" } diff --git a/cmake/osxbundle/Info.plist.in b/cmake/osxbundle/Info.plist.in index 9774dd79c..d6c77d509 100644 --- a/cmake/osxbundle/Info.plist.in +++ b/cmake/osxbundle/Info.plist.in @@ -26,6 +26,8 @@ APPL LSUIElement 1 + NSCameraUsageDescription + Hyperion uses this access to record screencasts NSHumanReadableCopyright ${MACOSX_BUNDLE_COPYRIGHT} Source Code diff --git a/include/grabber/osx/OsxFrameGrabber.h b/include/grabber/osx/OsxFrameGrabber.h index afb430fc5..1adc36964 100644 --- a/include/grabber/osx/OsxFrameGrabber.h +++ b/include/grabber/osx/OsxFrameGrabber.h @@ -1,6 +1,6 @@ #pragma once -// OSX includes +// CoreGraphics #include // Utils includes diff --git a/libsrc/grabber/osx/CMakeLists.txt b/libsrc/grabber/osx/CMakeLists.txt index 7d18e8d3f..a5f01acb0 100644 --- a/libsrc/grabber/osx/CMakeLists.txt +++ b/libsrc/grabber/osx/CMakeLists.txt @@ -1,10 +1,28 @@ add_library(osx-grabber ${CMAKE_SOURCE_DIR}/include/grabber/osx/OsxFrameGrabber.h ${CMAKE_SOURCE_DIR}/include/grabber/osx/OsxWrapper.h - ${CMAKE_SOURCE_DIR}/libsrc/grabber/osx/OsxFrameGrabber.cpp + ${CMAKE_SOURCE_DIR}/libsrc/grabber/osx/OsxFrameGrabber.mm ${CMAKE_SOURCE_DIR}/libsrc/grabber/osx/OsxWrapper.cpp ) target_link_libraries(osx-grabber hyperion + "$" ) + +file(WRITE ${CMAKE_BINARY_DIR}/tmp/SDK15Available.c + "#include + #if __MAC_OS_X_VERSION_MAX_ALLOWED < 150000 + #error __MAC_OS_X_VERSION_MAX_ALLOWED < 150000 + #endif + int main(int argc, char** argv) + { + return 0; + }" +) + +try_compile(SDK_15_AVAILABLE ${CMAKE_BINARY_DIR} SOURCES ${CMAKE_BINARY_DIR}/tmp/SDK15Available.c) +if(SDK_15_AVAILABLE) + target_compile_definitions(osx-grabber PRIVATE SDK_15_AVAILABLE) + target_link_libraries(osx-grabber "$") +endif() diff --git a/libsrc/grabber/osx/OsxFrameGrabber.cpp b/libsrc/grabber/osx/OsxFrameGrabber.mm similarity index 56% rename from libsrc/grabber/osx/OsxFrameGrabber.cpp rename to libsrc/grabber/osx/OsxFrameGrabber.mm index 916402c29..ef537c165 100644 --- a/libsrc/grabber/osx/OsxFrameGrabber.cpp +++ b/libsrc/grabber/osx/OsxFrameGrabber.mm @@ -1,10 +1,16 @@ // STL includes #include #include +#include -// Local includes +// Header #include +// ScreenCaptureKit +#if defined(SDK_15_AVAILABLE) +#include +#endif + //Qt #include #include @@ -15,9 +21,70 @@ namespace { const bool verbose = false; } //End of constants +#if defined(SDK_15_AVAILABLE) + static CGImageRef capture15(CGDirectDisplayID id, CGRect diIntersectDisplayLocal) + { + dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); + __block CGImageRef image1 = nil; + [SCShareableContent getShareableContentWithCompletionHandler:^(SCShareableContent* content, NSError* error) + { + @autoreleasepool + { + if (error || !content) + { + dispatch_semaphore_signal(semaphore); + return; + } + + SCDisplay* target = nil; + for (SCDisplay *display in content.displays) + { + if (display.displayID == id) + { + target = display; + break; + } + } + if (!target) + { + dispatch_semaphore_signal(semaphore); + return; + } + + SCContentFilter* filter = [[SCContentFilter alloc] initWithDisplay:target excludingWindows:@[]]; + SCStreamConfiguration* config = [[SCStreamConfiguration alloc] init]; + config.queueDepth = 5; + config.sourceRect = diIntersectDisplayLocal; + config.scalesToFit = false; + config.captureResolution = SCCaptureResolutionBest; + + CGDisplayModeRef modeRef = CGDisplayCopyDisplayMode(id); + double sysScale = CGDisplayModeGetPixelWidth(modeRef) / CGDisplayModeGetWidth(modeRef); + config.width = diIntersectDisplayLocal.size.width * sysScale; + config.height = diIntersectDisplayLocal.size.height * sysScale; + + [SCScreenshotManager captureImageWithFilter:filter + configuration:config + completionHandler:^(CGImageRef img, NSError* error) + { + if (!error) + { + image1 = CGImageCreateCopyWithColorSpace(img, CGColorSpaceCreateDeviceRGB()); + } + dispatch_semaphore_signal(semaphore); + }]; + } + }]; + + dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); + dispatch_release(semaphore); + return image1; + } +#endif + OsxFrameGrabber::OsxFrameGrabber(int display) : Grabber("GRABBER-OSX") - , _screenIndex(display) + , _screenIndex(display) { _isEnabled = false; _useImageResampler = true; @@ -31,6 +98,15 @@ bool OsxFrameGrabber::setupDisplay() { bool rc (false); +#if defined(SDK_15_AVAILABLE) + if (!CGPreflightScreenCaptureAccess()) + { + if(!CGRequestScreenCaptureAccess()) + Error(_log, "Screen capture permission required to start the grabber"); + return false; + } +#endif + rc = setDisplayIndex(_screenIndex); return rc; @@ -41,39 +117,38 @@ int OsxFrameGrabber::grabFrame(Image & image) int rc = 0; if (_isEnabled && !_isDeviceInError) { - CGImageRef dispImage; - CFDataRef imgData; - unsigned char * pImgData; - unsigned dspWidth; - unsigned dspHeight; - dispImage = CGDisplayCreateImage(_display); + #if defined(SDK_15_AVAILABLE) + dispImage = capture15(_display, CGDisplayBounds(_display)); + #else + dispImage = CGDisplayCreateImageForRect(_display, CGDisplayBounds(_display)); + #endif // display lost, use main if (dispImage == nullptr && _display != 0) { - dispImage = CGDisplayCreateImage(kCGDirectMainDisplay); - // no displays connected, return - if (dispImage == nullptr) - { - Error(_log, "No display connected..."); - return -1; - } + #if defined(SDK_15_AVAILABLE) + dispImage = capture15(kCGDirectMainDisplay, CGDisplayBounds(kCGDirectMainDisplay)); + #else + dispImage = CGDisplayCreateImageForRect(kCGDirectMainDisplay, CGDisplayBounds(kCGDirectMainDisplay)); + #endif } - imgData = CGDataProviderCopyData(CGImageGetDataProvider(dispImage)); - pImgData = (unsigned char*) CFDataGetBytePtr(imgData); - dspWidth = CGImageGetWidth(dispImage); - dspHeight = CGImageGetHeight(dispImage); - - _imageResampler.processImage( pImgData, - static_cast(dspWidth), - static_cast(dspHeight), - static_cast(CGImageGetBytesPerRow(dispImage)), - PixelFormat::BGR32, - image); - - CFRelease(imgData); + + // no displays connected, return + if (dispImage == nullptr) + { + Error(_log, "No display connected..."); + return -1; + } + + CFDataRef imgData = CGDataProviderCopyData(CGImageGetDataProvider(dispImage)); + if (imgData != nullptr) + { + _imageResampler.processImage((uint8_t *)CFDataGetBytePtr(imgData), static_cast(CGImageGetWidth(dispImage)), static_cast(CGImageGetHeight(dispImage)), static_cast(CGImageGetBytesPerRow(dispImage)), PixelFormat::BGR32, image); + CFRelease(imgData); + } + CGImageRelease(dispImage); } @@ -108,7 +183,12 @@ bool OsxFrameGrabber::setDisplayIndex(int index) { _display = activeDspys[_screenIndex]; - image = CGDisplayCreateImage(_display); + #if defined(SDK_15_AVAILABLE) + image = capture15(_display, CGDisplayBounds(_display)); + #else + image = CGDisplayCreateImageForRect(_display, CGDisplayBounds(_display)); + #endif + if(image == nullptr) { setEnabled(false); From 2b926b969e544153ad7628727bfa657578caad14 Mon Sep 17 00:00:00 2001 From: Paulchen-Panther <16664240+Paulchen-Panther@users.noreply.github.com> Date: Sat, 16 Nov 2024 18:59:12 +0100 Subject: [PATCH 08/13] ReSigning macOS package --- cmake/Dependencies.cmake | 60 ++++++++++++++++++++------------ cmake/osxbundle/AppleScript.scpt | 5 --- 2 files changed, 37 insertions(+), 28 deletions(-) diff --git a/cmake/Dependencies.cmake b/cmake/Dependencies.cmake index caf5663dd..01142373e 100644 --- a/cmake/Dependencies.cmake +++ b/cmake/Dependencies.cmake @@ -9,14 +9,15 @@ macro(DeployMacOS TARGET) OUTPUT_STRIP_TRAILING_WHITESPACE ) - install(CODE "set(TARGET_FILE \"${TARGET_FILE}\")" COMPONENT "Hyperion") - install(CODE "set(TARGET_BUNDLE_NAME \"${TARGET}.app\")" COMPONENT "Hyperion") - install(CODE "set(PLUGIN_DIR \"${QT_PLUGIN_DIR}\")" COMPONENT "Hyperion") - install(CODE "set(BUILD_DIR \"${CMAKE_BINARY_DIR}\")" COMPONENT "Hyperion") + install(CODE "set(TARGET_FILE \"${TARGET_FILE}\")" COMPONENT "Hyperion") + install(CODE "set(TARGET_BUNDLE_NAME \"${TARGET}.app\")" COMPONENT "Hyperion") + install(CODE "set(PLUGIN_DIR \"${QT_PLUGIN_DIR}\")" COMPONENT "Hyperion") install(CODE "set(ENABLE_EFFECTENGINE \"${ENABLE_EFFECTENGINE}\")" COMPONENT "Hyperion") install(CODE [[ + set(BUNDLE_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/${TARGET_BUNDLE_NAME}") + file(GET_RUNTIME_DEPENDENCIES EXECUTABLES ${TARGET_FILE} RESOLVED_DEPENDENCIES_VAR resolved_deps @@ -28,13 +29,13 @@ macro(DeployMacOS TARGET) if (${_index} GREATER -1) file(INSTALL FILES "${dependency}" - DESTINATION "${CMAKE_INSTALL_PREFIX}/${TARGET_BUNDLE_NAME}/Contents/Frameworks" + DESTINATION "${BUNDLE_INSTALL_DIR}/Contents/Frameworks" TYPE SHARED_LIBRARY ) else() file(INSTALL FILES "${dependency}" - DESTINATION "${CMAKE_INSTALL_PREFIX}/${TARGET_BUNDLE_NAME}/Contents/lib" + DESTINATION "${BUNDLE_INSTALL_DIR}/Contents/lib" TYPE SHARED_LIBRARY FOLLOW_SYMLINK_CHAIN ) @@ -58,7 +59,7 @@ macro(DeployMacOS TARGET) foreach(DEPENDENCY ${PLUGINS}) file(INSTALL - DESTINATION "${CMAKE_INSTALL_PREFIX}/${TARGET_BUNDLE_NAME}/Contents/lib" + DESTINATION "${BUNDLE_INSTALL_DIR}/Contents/lib" TYPE SHARED_LIBRARY FILES ${DEPENDENCY} FOLLOW_SYMLINK_CHAIN @@ -66,10 +67,10 @@ macro(DeployMacOS TARGET) endforeach() get_filename_component(singleQtLib ${file} NAME) - list(APPEND QT_PLUGINS "${CMAKE_INSTALL_PREFIX}/${TARGET_BUNDLE_NAME}/Contents/plugins/${PLUGIN}/${singleQtLib}") + list(APPEND QT_PLUGINS "${BUNDLE_INSTALL_DIR}/Contents/plugins/${PLUGIN}/${singleQtLib}") file(INSTALL FILES ${file} - DESTINATION "${CMAKE_INSTALL_PREFIX}/${TARGET_BUNDLE_NAME}/Contents/plugins/${PLUGIN}" + DESTINATION "${BUNDLE_INSTALL_DIR}/Contents/plugins/${PLUGIN}" TYPE SHARED_LIBRARY ) @@ -78,10 +79,10 @@ macro(DeployMacOS TARGET) endforeach() include(BundleUtilities) - fixup_bundle("${CMAKE_INSTALL_PREFIX}/${TARGET_BUNDLE_NAME}" "${QT_PLUGINS}" "${CMAKE_INSTALL_PREFIX}/${TARGET_BUNDLE_NAME}/Contents/lib" IGNORE_ITEM "python;python3;Python;Python3;.Python;.Python3") + fixup_bundle("${BUNDLE_INSTALL_DIR}" "${QT_PLUGINS}" "${BUNDLE_INSTALL_DIR}/Contents/lib" IGNORE_ITEM "python;python3;Python;Python3;.Python;.Python3") + file(REMOVE_RECURSE "${BUNDLE_INSTALL_DIR}/Contents/lib") if(ENABLE_EFFECTENGINE) - # Detect the Python version and modules directory if(NOT CMAKE_VERSION VERSION_LESS "3.12") find_package(Python3 COMPONENTS Interpreter Development REQUIRED) @@ -98,24 +99,37 @@ macro(DeployMacOS TARGET) # Copy Python modules to '/../Frameworks/Python.framework/Versions/Current/lib/PythonMAJOR.MINOR' and ignore the unnecessary stuff listed below if (PYTHON_MODULES_DIR) + set(PYTHON_FRAMEWORK "${BUNDLE_INSTALL_DIR}/Contents/Frameworks/Python.framework") file( COPY ${PYTHON_MODULES_DIR}/ - DESTINATION "${CMAKE_INSTALL_PREFIX}/${TARGET_BUNDLE_NAME}/Contents/Frameworks/Python.framework/Versions/Current/lib/python${PYTHON_VERSION_MAJOR_MINOR}" - PATTERN "*.pyc" EXCLUDE # compiled bytecodes - PATTERN "__pycache__" EXCLUDE # any cache - PATTERN "config-${PYTHON_VERSION_MAJOR_MINOR}*" EXCLUDE # static libs - PATTERN "lib2to3" EXCLUDE # automated Python 2 to 3 code translation - PATTERN "tkinter" EXCLUDE # Tk interface - PATTERN "turtledemo" EXCLUDE # Tk demo folder - PATTERN "turtle.py" EXCLUDE # Tk demo file - PATTERN "test" EXCLUDE # unittest module - PATTERN "sitecustomize.py" EXCLUDE # site-specific configs + DESTINATION "${PYTHON_FRAMEWORK}/Versions/Current/lib/python${PYTHON_VERSION_MAJOR_MINOR}" + PATTERN "*.pyc" EXCLUDE # compiled bytecodes + PATTERN "__pycache__" EXCLUDE # any cache + PATTERN "config-${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}*" EXCLUDE # static libs + PATTERN "lib2to3" EXCLUDE # automated Python 2 to 3 code translation + PATTERN "tkinter" EXCLUDE # Tk interface + PATTERN "lib-dynload/_tkinter.*" EXCLUDE + PATTERN "idlelib" EXCLUDE + PATTERN "turtle.py" EXCLUDE # Tk demo + PATTERN "test" EXCLUDE # unittest module + PATTERN "sitecustomize.py" EXCLUDE # site-specific configs ) endif(PYTHON_MODULES_DIR) endif(ENABLE_EFFECTENGINE) - file(REMOVE_RECURSE "${CMAKE_INSTALL_PREFIX}/${TARGET_BUNDLE_NAME}/Contents/lib") - file(REMOVE_RECURSE "${CMAKE_INSTALL_PREFIX}/share") + file(GLOB_RECURSE LIBS FOLLOW_SYMLINKS "${BUNDLE_INSTALL_DIR}/*.dylib") + file(GLOB FRAMEWORKS FOLLOW_SYMLINKS LIST_DIRECTORIES ON "${BUNDLE_INSTALL_DIR}/Contents/Frameworks/*") + foreach(item ${LIBS} ${FRAMEWORKS} ${PYTHON_FRAMEWORK} ${BUNDLE_INSTALL_DIR}) + set(cmd codesign --deep --force --sign - "${item}") + execute_process( + COMMAND ${cmd} + RESULT_VARIABLE codesign_result + ) + + if(NOT codesign_result EQUAL 0) + message(WARNING "macOS signing failed; ${cmd} returned ${codesign_result}") + endif() + endforeach() ]] COMPONENT "Hyperion") diff --git a/cmake/osxbundle/AppleScript.scpt b/cmake/osxbundle/AppleScript.scpt index f3a08e217..9e1f3288c 100644 --- a/cmake/osxbundle/AppleScript.scpt +++ b/cmake/osxbundle/AppleScript.scpt @@ -49,11 +49,6 @@ on run argv delay 1 close - -- one last open and close so you can see everything looks correct - open - delay 5 - close - end tell delay 1 From ee921e4f1284fe3d984d9422b24a1c56c6916c21 Mon Sep 17 00:00:00 2001 From: Paulchen-Panther <16664240+Paulchen-Panther@users.noreply.github.com> Date: Sun, 17 Nov 2024 12:34:12 +0100 Subject: [PATCH 09/13] Python 3.11.10 test --- libsrc/python/PythonProgram.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/libsrc/python/PythonProgram.cpp b/libsrc/python/PythonProgram.cpp index 805744742..3571cb384 100644 --- a/libsrc/python/PythonProgram.cpp +++ b/libsrc/python/PythonProgram.cpp @@ -88,11 +88,12 @@ void PythonProgram::execute(const QByteArray& python_code) if (!main_module) { // Restore the previous thread state -#if (PY_VERSION_HEX < 0x030C0000) - PyThreadState_Swap(mainThreadState); -#else +// #if (PY_VERSION_HEX < 0x030C0000) +// PyThreadState_Swap(mainThreadState); +// #else PyThreadState_Swap(prev_thread_state); -#endif + qDebug() << "PyThreadState_Swap(prev_thread_state);"; +// #endif return; } @@ -175,10 +176,11 @@ void PythonProgram::execute(const QByteArray& python_code) Py_DECREF(main_module); // Restore the previous thread state -#if (PY_VERSION_HEX < 0x030C0000) - PyThreadState_Swap(mainThreadState); -#else +// #if (PY_VERSION_HEX < 0x030C0000) +// PyThreadState_Swap(mainThreadState); +// #else PyThreadState_Swap(prev_thread_state); -#endif + qDebug() << "PyThreadState_Swap(prev_thread_state);"; +// #endif } From 07bf243a44ee4381a73e9ebabf27b2f7b104acfb Mon Sep 17 00:00:00 2001 From: Lord-Grey Date: Wed, 20 Nov 2024 19:28:53 +0100 Subject: [PATCH 10/13] Revert "Python 3.11.10 test" This reverts commit ee921e4f1284fe3d984d9422b24a1c56c6916c21. --- libsrc/python/PythonProgram.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/libsrc/python/PythonProgram.cpp b/libsrc/python/PythonProgram.cpp index 3571cb384..805744742 100644 --- a/libsrc/python/PythonProgram.cpp +++ b/libsrc/python/PythonProgram.cpp @@ -88,12 +88,11 @@ void PythonProgram::execute(const QByteArray& python_code) if (!main_module) { // Restore the previous thread state -// #if (PY_VERSION_HEX < 0x030C0000) -// PyThreadState_Swap(mainThreadState); -// #else +#if (PY_VERSION_HEX < 0x030C0000) + PyThreadState_Swap(mainThreadState); +#else PyThreadState_Swap(prev_thread_state); - qDebug() << "PyThreadState_Swap(prev_thread_state);"; -// #endif +#endif return; } @@ -176,11 +175,10 @@ void PythonProgram::execute(const QByteArray& python_code) Py_DECREF(main_module); // Restore the previous thread state -// #if (PY_VERSION_HEX < 0x030C0000) -// PyThreadState_Swap(mainThreadState); -// #else +#if (PY_VERSION_HEX < 0x030C0000) + PyThreadState_Swap(mainThreadState); +#else PyThreadState_Swap(prev_thread_state); - qDebug() << "PyThreadState_Swap(prev_thread_state);"; -// #endif +#endif } From f808b43573f4ef8611f28442662fa29f545cac82 Mon Sep 17 00:00:00 2001 From: Lord-Grey Date: Wed, 20 Nov 2024 21:03:41 +0100 Subject: [PATCH 11/13] Handle defined exits from python scripts --- effects/collision.py | 106 +++++++++++++++++++------------- libsrc/python/PythonProgram.cpp | 58 ++++++++++++++++- 2 files changed, 118 insertions(+), 46 deletions(-) diff --git a/effects/collision.py b/effects/collision.py index 5c22fc83f..09afa3111 100644 --- a/effects/collision.py +++ b/effects/collision.py @@ -1,5 +1,3 @@ -# Two projectiles are sent from random positions and collide with each other -# Template from https://github.com/nickpesce/lit/blob/master/lit/effects/collision.py import hyperion, time, colorsys, random # Get parameters @@ -7,6 +5,11 @@ trailLength = max(3, int(hyperion.args.get('trailLength', 5))) explodeRadius = int(hyperion.args.get('explodeRadius', 8)) +# Ensure that the range for pixel indices stays within bounds +maxPixelIndex = hyperion.ledCount - 1 +if trailLength > maxPixelIndex or explodeRadius > maxPixelIndex: + exit(f"Error: Color length ({trailLength}) and detonation range ({explodeRadius}) must be less than number of LEDs configured ({hyperion.ledCount})") + # Create additional variables increment = None projectiles = [] @@ -14,47 +17,64 @@ # Initialize the led data ledData = bytearray() for i in range(hyperion.ledCount): - ledData += bytearray((0,0,0)) + ledData += bytearray((0,0,0)) # Start the write data loop while not hyperion.abort(): - if (len(projectiles) != 2): - projectiles = [ [0, 1, random.uniform(0.0, 1.0)], [hyperion.ledCount-1, -1, random.uniform(0.0, 1.0)] ] - increment = -random.randint(0, hyperion.ledCount-1) if random.choice([True, False]) else random.randint(0, hyperion.ledCount-1) - - ledDataBuf = ledData[:] - for i, v in enumerate(projectiles): - projectiles[i][0] = projectiles[i][0]+projectiles[i][1] - for t in range(0, trailLength): - pixel = v[0] - v[1]*t - if pixel + 2 < 0: - pixel += hyperion.ledCount - if pixel + 2 > hyperion.ledCount-1: - pixel -= hyperion.ledCount-1 - rgb = colorsys.hsv_to_rgb(v[2], 1, (trailLength - 1.0*t)/trailLength) - ledDataBuf[3*pixel ] = int(255*rgb[0]) - ledDataBuf[3*pixel + 1] = int(255*rgb[1]) - ledDataBuf[3*pixel + 2] = int(255*rgb[2]) - - hyperion.setColor(ledDataBuf[-increment:] + ledDataBuf[:-increment]) - - for i1, p1 in enumerate(projectiles): - for i2, p2 in enumerate(projectiles): - if (p1 is not p2): - prev1 = p1[0] - p1[1] - prev2 = p2[0] - p2[1] - if (prev1 - prev2 < 0) != (p1[0] - p2[0] < 0): - for d in range(0, explodeRadius): - for pixel in range(p1[0] - d, p1[0] + d): - rgb = colorsys.hsv_to_rgb(random.choice([p1[2], p2[2]]), 1, (1.0 * explodeRadius - d) / explodeRadius) - ledDataBuf[3*pixel ] = int(255*rgb[0]) - ledDataBuf[3*pixel + 1] = int(255*rgb[1]) - ledDataBuf[3*pixel + 2] = int(255*rgb[2]) - - hyperion.setColor(ledDataBuf[-increment:] + ledDataBuf[:-increment]) - time.sleep(sleepTime) - - projectiles.remove(p1) - projectiles.remove(p2) - - time.sleep(sleepTime) + if len(projectiles) != 2: + projectiles = [ + [0, 1, random.uniform(0.0, 1.0)], # Start positions of projectiles + [hyperion.ledCount-1, -1, random.uniform(0.0, 1.0)] + ] + increment = -random.randint(0, hyperion.ledCount-1) if random.choice([True, False]) else random.randint(0, hyperion.ledCount-1) + + # Backup the LED data + ledDataBuf = ledData[:] + for i, v in enumerate(projectiles): + # Update projectile positions + projectiles[i][0] = projectiles[i][0] + projectiles[i][1] + + for t in range(0, trailLength): + # Calculate pixel index for the trail + pixel = v[0] - v[1] * t + if pixel < 0: + pixel += hyperion.ledCount + if pixel >= hyperion.ledCount: + pixel -= hyperion.ledCount + + # Make sure pixel is within bounds + if pixel < 0 or pixel >= hyperion.ledCount: + continue + + rgb = colorsys.hsv_to_rgb(v[2], 1, (trailLength - 1.0 * t) / trailLength) + ledDataBuf[3*pixel] = int(255 * rgb[0]) + ledDataBuf[3*pixel + 1] = int(255 * rgb[1]) + ledDataBuf[3*pixel + 2] = int(255 * rgb[2]) + + hyperion.setColor(ledDataBuf[-increment:] + ledDataBuf[:-increment]) + + # Check for collision and handle explosion + for i1, p1 in enumerate(projectiles): + for i2, p2 in enumerate(projectiles): + if p1 is not p2: + prev1 = p1[0] - p1[1] + prev2 = p2[0] - p2[1] + if (prev1 - prev2 < 0) != (p1[0] - p2[0] < 0): + for d in range(0, explodeRadius): + for pixel in range(p1[0] - d, p1[0] + d): + # Check if pixel is out of bounds + if pixel < 0 or pixel >= hyperion.ledCount: + continue + + rgb = colorsys.hsv_to_rgb(random.choice([p1[2], p2[2]]), 1, (1.0 * explodeRadius - d) / explodeRadius) + ledDataBuf[3 * pixel] = int(255 * rgb[0]) + ledDataBuf[3 * pixel + 1] = int(255 * rgb[1]) + ledDataBuf[3 * pixel + 2] = int(255 * rgb[2]) + + hyperion.setColor(ledDataBuf[-increment:] + ledDataBuf[:-increment]) + time.sleep(sleepTime) + + projectiles.remove(p1) + projectiles.remove(p2) + + time.sleep(sleepTime) diff --git a/libsrc/python/PythonProgram.cpp b/libsrc/python/PythonProgram.cpp index 805744742..38a27934d 100644 --- a/libsrc/python/PythonProgram.cpp +++ b/libsrc/python/PythonProgram.cpp @@ -102,16 +102,66 @@ void PythonProgram::execute(const QByteArray& python_code) { if (PyErr_Occurred()) { - Error(_log, "###### PYTHON EXCEPTION ######"); - Error(_log, "## In effect '%s'", QSTRING_CSTR(_name)); - PyObject* errorType = NULL, * errorValue = NULL, * errorTraceback = NULL; PyErr_Fetch(&errorType, &errorValue, &errorTraceback); PyErr_NormalizeException(&errorType, &errorValue, &errorTraceback); + // Check if the exception is a SystemExit + PyObject* systemExitType = PyExc_SystemExit; + bool isSystemExit = PyObject_IsInstance(errorValue, systemExitType); + + if (isSystemExit) + { + // Extract the exit argument + PyObject* exitArg = PyObject_GetAttrString(errorValue, "code"); + + if (exitArg) + { + QString logErrorText; + if (PyTuple_Check(exitArg)) { + PyObject* errorMessage = PyTuple_GetItem(exitArg, 0); // Borrowed reference + PyObject* exitCode = PyTuple_GetItem(exitArg, 1); // Borrowed reference + + if (exitCode && PyLong_Check(exitCode)) + { + logErrorText = QString("[%1]: ").arg(PyLong_AsLong(exitCode)); + } + + if (errorMessage && PyUnicode_Check(errorMessage)) { + logErrorText.append(PyUnicode_AsUTF8(errorMessage)); + } + } + else if (PyUnicode_Check(exitArg)) { + // If the code is just a string, treat it as an error message + logErrorText.append(PyUnicode_AsUTF8(exitArg)); + } + else if (PyLong_Check(exitArg)) { + // If the code is just an integer, treat it as an exit code + logErrorText = QString("[%1]").arg(PyLong_AsLong(exitArg)); + } + + Error(_log, "Effect '%s' failed with error %s", QSTRING_CSTR(_name), QSTRING_CSTR(logErrorText)); + + Py_DECREF(exitArg); // Release the reference + } + else + { + Debug(_log, "No 'code' attribute found on SystemExit exception."); + } + // Clear the error so it won't propagate + PyErr_Clear(); + + Py_DECREF(systemExitType); + return; + } + Py_DECREF(systemExitType); + if (errorValue) { + Error(_log, "###### PYTHON EXCEPTION ######"); + Error(_log, "## In effect '%s'", QSTRING_CSTR(_name)); + QString message; if (PyObject_HasAttrString(errorValue, "__class__")) { @@ -166,6 +216,8 @@ void PythonProgram::execute(const QByteArray& python_code) } Error(_log, "###### EXCEPTION END ######"); } + // Clear the error so it won't propagate + PyErr_Clear(); } else { From 5eb32f852b73a4e2c92a9680e6211283719b405c Mon Sep 17 00:00:00 2001 From: LordGrey <48840279+Lord-Grey@users.noreply.github.com> Date: Sun, 1 Dec 2024 15:33:22 +0100 Subject: [PATCH 12/13] Update change.log --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 903371037..d605e4ec1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Support for ftdi chip based LED-devices with ws2812, sk6812 apa102 LED types (Many thanks to @nurikk) (#1746) -- Support for Skydimo devices (being an Adalight variant) +- Support for Skydimo devices - Support gaps on Matrix Layout (#1696) - Windows: Added a new grabber that uses the DXGI DDA (Desktop Duplication API). This has much better performance than the DX grabber as it does more of its work on the GPU. @@ -41,6 +41,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed: Philip Hue APIv2 support without Entertainment group defined (#1742) - Refactored: Database access layer - Refactored: Hyperion's configuration database is validated before start-up (and migrated, if required) +- Refactored: Python to enable parallel effect processing under Python 3.12 +- Fixed: Python 3.12 crashes (#1747) +- osX Grabber: Use ScreenCaptureKit under macOS 15 and above **JSON-API** - Refactored JSON-API to ensure consistent authorization behaviour across sessions and single requests with token authorization. From 2d405baa8a4f3107984317455c65ea73a17c4292 Mon Sep 17 00:00:00 2001 From: LordGrey <48840279+Lord-Grey@users.noreply.github.com> Date: Sun, 1 Dec 2024 16:21:39 +0100 Subject: [PATCH 13/13] CodeQL findings --- libsrc/effectengine/EffectModule.cpp | 18 ------------------ libsrc/python/PythonProgram.cpp | 6 ++++-- 2 files changed, 4 insertions(+), 20 deletions(-) diff --git a/libsrc/effectengine/EffectModule.cpp b/libsrc/effectengine/EffectModule.cpp index 69c01f1f0..d3ce4a495 100644 --- a/libsrc/effectengine/EffectModule.cpp +++ b/libsrc/effectengine/EffectModule.cpp @@ -38,24 +38,6 @@ static int hyperion_exec(PyObject* module) { return 0; } -// Module creation function for multi-phase init, used in Py_mod_create slot -static PyObject* hyperion_create(PyModuleDef* def, PyObject* args) { - PyObject* module = PyModule_Create(def); - if (!module) - { - return NULL; - } - - // Execute any additional module initialization logic - if (hyperion_exec(module) < 0) - { - Py_DECREF(module); - return NULL; - } - - return module; -} - // Module deallocation function to clean up per-interpreter state static void hyperion_free(void* /* module */) { diff --git a/libsrc/python/PythonProgram.cpp b/libsrc/python/PythonProgram.cpp index 38a27934d..6f0b08c92 100644 --- a/libsrc/python/PythonProgram.cpp +++ b/libsrc/python/PythonProgram.cpp @@ -81,9 +81,11 @@ void PythonProgram::execute(const QByteArray& python_code) { return; } - +#if (PY_VERSION_HEX < 0x030C0000) + PyThreadState_Swap(_tstate); +#else PyThreadState* prev_thread_state = PyThreadState_Swap(_tstate); - +#endif PyObject* main_module = PyImport_ImportModule("__main__"); if (!main_module) {