From 753ae322b62eae109e1b231989c97be0e93ae73a Mon Sep 17 00:00:00 2001 From: Luis Blanco Date: Mon, 30 Oct 2023 22:31:55 +0400 Subject: [PATCH] Adjust for linux --- core.js | 2 - examples/main.ts | 90 ++++++++++++++++++++++++++------------------ index.d.ts | 7 +++- js/view.js | 10 +++-- package-lock.json | 8 ++-- package.json | 2 +- src/binding.gyp | 19 ---------- src/cpp/bindings.cpp | 27 +++++++++++++ src/cpp/view.cpp | 31 ++------------- 9 files changed, 99 insertions(+), 97 deletions(-) diff --git a/core.js b/core.js index c5af3fc..7207157 100644 --- a/core.js +++ b/core.js @@ -2,7 +2,6 @@ // process.env.QT_DEBUG_PLUGINS = '1'; require('segfault-raub'); - const { getBin } = require('addon-tools-raub'); const depUi = require('deps-qmlui-raub'); @@ -11,6 +10,5 @@ const core = require(`./${getBin()}/qml`); core.View._plugins(`${depUi.core.bin}/plugins`); core.View._plugins(`${depUi.gui.bin}/plugins`); core.View._plugins(`${depUi.qml.bin}/plugins`); -core.View._plugins(`${process.cwd()}/plugins`); module.exports = core; diff --git a/examples/main.ts b/examples/main.ts index b2c068a..4adaf9d 100644 --- a/examples/main.ts +++ b/examples/main.ts @@ -2,7 +2,7 @@ import Img from 'image-raub'; import gl from 'webgl-raub'; -import { Document } from 'glfw-raub'; +import glfw, { Document, platformDevice } from 'glfw-raub'; import { View } from 'qml-raub'; @@ -13,40 +13,47 @@ const icon = new Img(__dirname + '/qml.png'); icon.on('load', () => { document.icon = (icon as unknown as typeof document.icon); }); document.title = 'QML'; +console.log('p', document.platformContext, document.platformWindow, document.platformDevice); + const release = () => document.makeCurrent(); -View.init(process.cwd(), document.platformWindow, document.platformContext); release(); +View.init(process.cwd(), document.platformWindow, document.platformContext, document.platformDevice); -const ui = new View({ width: document.w, height: document.h, file: 'qml/gui.qml' }); - -document.on('mousedown', ui.mousedown.bind(ui)); -document.on('mouseup', ui.mouseup.bind(ui)); -document.on('mousemove', ui.mousemove.bind(ui)); -document.on('keydown', ui.keydown.bind(ui)); -document.on('keyup', ui.keyup.bind(ui)); -document.on('wheel', ui.wheel.bind(ui)); - -document.on('resize', ({width, height}) => ui.wh = [width, height]); - -ui.on('mousedown', e => console.log('[>mousedown]', e)); -ui.on('mouseup', e => console.log('[>mouseup]', e)); -// ui.on('mousemove', e => console.log('[mousemove]', e)); -ui.on('keydown', e => console.log('[>keydown]', e)); -ui.on('keyup', e => console.log('[>keyup]', e)); -ui.on('wheel', e => console.log('[>wheel]', e)); - -ui.on('ohai', data => { - console.log('RECV', data); - ui.set('myButton1', 'text', `${Date.now()}`); - ui.invoke('myButton1', 'func', [{ uid: 'dwad2312414', value: 17 }]); -}); +// const ui = new View({ width: document.w, height: document.h, file: 'qml/gui.qml' }); +release(); -let texture = new gl.WebGLTexture(ui.textureId || 0); -ui.on('reset', (texId) => { - release(); - texture = new gl.WebGLTexture(texId || 0); -}); +// document.on('mousedown', ui.mousedown.bind(ui)); +// document.on('mouseup', ui.mouseup.bind(ui)); +// document.on('mousemove', ui.mousemove.bind(ui)); +// document.on('keydown', ui.keydown.bind(ui)); +// document.on('keyup', ui.keyup.bind(ui)); +// document.on('wheel', ui.wheel.bind(ui)); + +// document.on('resize', ({width, height}) => ui.wh = [width, height]); + +// ui.on('mousedown', e => console.log('[>mousedown]', e)); +// ui.on('mouseup', e => console.log('[>mouseup]', e)); +// // ui.on('mousemove', e => console.log('[mousemove]', e)); +// ui.on('keydown', e => console.log('[>keydown]', e)); +// ui.on('keyup', e => console.log('[>keyup]', e)); +// ui.on('wheel', e => console.log('[>wheel]', e)); + +// ui.on('ohai', data => { +// console.log('RECV', data); +// ui.set('myButton1', 'text', `${Date.now()}`); +// ui.invoke('myButton1', 'func', [{ uid: 'dwad2312414', value: 17 }]); +// }); + +let texture = gl.createTexture(); +// let texture = ui.textureId === null ? gl.createTexture() : new gl.WebGLTexture(ui.textureId); +// console.log('tid0', ui.textureId); +// // let texture = new gl.WebGLTexture(ui.textureId || 0); +// ui.on('reset', (texId) => { +// console.log('tid0', texId); +// release(); +// texture = texId ? new gl.WebGLTexture(texId) : gl.createTexture(); +// }); const requestAnimFrame = document.requestAnimationFrame; @@ -74,12 +81,12 @@ const shaders = { } `, 'shader-fs' : ` - precision mediump float; uniform sampler2D tex; uniform vec2 size; void main() { vec2 uv = gl_FragCoord.xy / size.xy; - gl_FragColor = texture2D(tex, uv); + // gl_FragColor = texture2D(tex, uv); + gl_FragColor = (vec4(1.0, 0.0, 0.0, 1.0) + texture2D(tex, uv)) * 0.5; } `, } as const; @@ -185,19 +192,28 @@ const drawScene = () => { const tick = () => { - View.update(); + setTimeout(tick, 16); + + // View.update(); release(); + drawScene(); - requestAnimFrame(tick); -}; + // (glfw.testScene as (w: number, h: number) => void)(document.width, document.height); + document.swapBuffers(); + + glfw.pollEvents(); + + + // requestAnimFrame(tick); +}; const start = () => { initShaders(); initBuffers(); - gl.clearColor(0.0, 0.0, 0.0, 1.0); - gl.enable(gl.DEPTH_TEST); + // gl.clearColor(0.0, 1.0, 0.0, 1.0); + // gl.enable(gl.DEPTH_TEST); tick(); }; diff --git a/index.d.ts b/index.d.ts index b7dce5e..cb2a043 100644 --- a/index.d.ts +++ b/index.d.ts @@ -129,6 +129,7 @@ declare module "qml-raub" { * @param cwd base directory for QML file resolution. Usually, `process.cwd()`. * @param wnd platform window handle (e.g. HWND on Windows). * @param ctx the OpenGL context to which the QML render texture will be made available. + * @param device optional system display device, used only on Linux. * * @see [OpenGL context](https://www.khronos.org/opengl/wiki/OpenGL_Context). * @@ -143,6 +144,7 @@ declare module "qml-raub" { * // window - is returned by glfw.createWindow(...) call * const hwnd = glfw.platformWindow(window); * const ctx = glfw.platformContext(window); + * const device = glfw.platformDevice(); * ``` * * or @@ -151,9 +153,10 @@ declare module "qml-raub" { * // window - is an instance of glfw.Window * const hwnd = window.platformWindow; * const ctx = window.platformContext; + * const device = window.platformDevice; * ``` */ - static init(cwd: string, wnd: number, ctx: number): void; + static init(cwd: string, wnd: number, ctx: number, device?: number): void; /** Register a QML "library" directory. */ static libs(l: string): void; @@ -207,7 +210,7 @@ declare module "qml-raub" { constructor(opts: TOptsProperty); get value(): T | null; - set value(v: T); + set value(v: T | null); } export type TOptsMethod = Readonly<{ diff --git a/js/view.js b/js/view.js index e1f3f4c..95c7dcd 100644 --- a/js/view.js +++ b/js/view.js @@ -1,7 +1,7 @@ 'use strict'; -const path = require('path'); -const { inspect, inherits } = require('util'); +const path = require('node:path'); +const { inspect, inherits } = require('node:util'); const Emitter = require('events'); const { View } = require('../core'); @@ -236,10 +236,12 @@ class JsView extends View { return parseJsonSafe(this._get(name, key)); } - static init(cwd, wnd, ctx) { + static init(cwd, wnd, ctx, device = 0) { inited = true; qmlCwd = cwd.replace(/\\/g, '/'); - View._init(qmlCwd, wnd, ctx, parseJsonSafe); + + View._plugins(`${qmlCwd}/plugins`); + View._init(qmlCwd, wnd, ctx, device, parseJsonSafe); } static libs(l) { diff --git a/package-lock.json b/package-lock.json index a43dd12..de5f88f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "license": "MIT", "dependencies": { "addon-tools-raub": "^7.4.0", - "deps-qmlui-raub": "^3.0.0", + "deps-qmlui-raub": "^3.1.0", "segfault-raub": "^2.1.2" }, "devDependencies": { @@ -361,9 +361,9 @@ "dev": true }, "node_modules/deps-qmlui-raub": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/deps-qmlui-raub/-/deps-qmlui-raub-3.0.0.tgz", - "integrity": "sha512-JLfxQds+75TplFGz6004NY+dnqJf/reVzSgJ7J1uan2PFwOaQiuECx8M45GeIg1ShfjN7ZWgR+gFhEok4EV7qQ==", + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/deps-qmlui-raub/-/deps-qmlui-raub-3.1.0.tgz", + "integrity": "sha512-Qzsfr4e80UyHaQhNN8ROcXBcf3BR3qKugsWC4J86uFHJc/zhvvTJMOqINdfMGN7cfIAs+qCrw4p7FsF2EtVz4Q==", "hasInstallScript": true, "dependencies": { "addon-tools-raub": "^7.4.0", diff --git a/package.json b/package.json index dab44c4..73ca8ea 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ }, "dependencies": { "addon-tools-raub": "^7.4.0", - "deps-qmlui-raub": "^3.0.0", + "deps-qmlui-raub": "^3.1.0", "segfault-raub": "^2.1.2" }, "devDependencies": { diff --git a/src/binding.gyp b/src/binding.gyp index 2553168..48e9db7 100644 --- a/src/binding.gyp +++ b/src/binding.gyp @@ -24,7 +24,6 @@ 'conditions': [ ['OS=="linux"', { 'libraries': [ - '<(qmlui_bin)/libqmlui.so', "-Wl,-rpath,'$$ORIGIN'", "-Wl,-rpath,'$$ORIGIN/../node_modules/deps-qt-core-raub/bin-linux'", "-Wl,-rpath,'$$ORIGIN/../node_modules/deps-qt-gui-raub/bin-linux'", @@ -34,24 +33,6 @@ "-Wl,-rpath,'$$ORIGIN/../../deps-qt-gui-raub/bin-linux'", "-Wl,-rpath,'$$ORIGIN/../../deps-qt-qml-raub/bin-linux'", "-Wl,-rpath,'$$ORIGIN/../../deps-qmlui-raub/bin-linux'", - '<(qt_core_bin)/libicui18n.so.56', - '<(qt_core_bin)/libicuuc.so.56', - '<(qt_core_bin)/libicudata.so.56', - '<(qt_core_bin)/libicuio.so.56', - '<(qt_core_bin)/libicule.so.56', - '<(qt_core_bin)/libicutu.so.56', - '<(qt_core_bin)/libQt5Core.so.5', - '<(qt_core_bin)/libQt5Network.so.5', - '<(qt_core_bin)/libQt5DBus.so.5', - '<(qt_gui_bin)/libQt5Gui.so.5', - '<(qt_gui_bin)/libQt5OpenGL.so.5', - '<(qt_gui_bin)/libQt5Widgets.so.5', - '<(qt_gui_bin)/libQt5XcbQpa.so.5', - '<(qt_qml_bin)/libQt5Qml.so.5', - '<(qt_qml_bin)/libQt5Quick.so.5', - '<(qt_qml_bin)/libQt5QuickControls2.so.5', - '<(qt_qml_bin)/libQt5QuickTemplates2.so.5', - '<(qt_qml_bin)/libQt5QuickWidgets.so.5', ], 'defines': ['__linux__'], }], diff --git a/src/cpp/bindings.cpp b/src/cpp/bindings.cpp index e939c95..a8830c5 100644 --- a/src/cpp/bindings.cpp +++ b/src/cpp/bindings.cpp @@ -1,7 +1,34 @@ +#ifdef __linux__ + #include +#endif + #include "view.hpp" Napi::Object initModule(Napi::Env env, Napi::Object exports) { + // Preload the libs with OUR @RPATH, not some junk builtin rpaths + #ifdef __linux__ + dlopen("libicui18n.so.56", RTLD_NOW | RTLD_GLOBAL); + dlopen("libicuuc.so.56", RTLD_NOW | RTLD_GLOBAL); + dlopen("libicudata.so.56", RTLD_NOW | RTLD_GLOBAL); + dlopen("libicuio.so.56", RTLD_NOW | RTLD_GLOBAL); + dlopen("libicule.so.56", RTLD_NOW | RTLD_GLOBAL); + dlopen("libicutu.so.56", RTLD_NOW | RTLD_GLOBAL); + dlopen("libQt5Core.so.5", RTLD_NOW | RTLD_GLOBAL); + dlopen("libQt5Network.so.5", RTLD_NOW | RTLD_GLOBAL); + dlopen("libQt5DBus.so.5", RTLD_NOW | RTLD_GLOBAL); + dlopen("libQt5Gui.so.5", RTLD_NOW | RTLD_GLOBAL); + dlopen("libQt5OpenGL.so.5", RTLD_NOW | RTLD_GLOBAL); + dlopen("libQt5Widgets.so.5", RTLD_NOW | RTLD_GLOBAL); + dlopen("libQt5XcbQpa.so.5", RTLD_NOW | RTLD_GLOBAL); + dlopen("libQt5Qml.so.5", RTLD_NOW | RTLD_GLOBAL); + dlopen("libQt5Quick.so.5", RTLD_NOW | RTLD_GLOBAL); + dlopen("libQt5QuickControls2.so.5", RTLD_NOW | RTLD_GLOBAL); + dlopen("libQt5QuickTemplates2.so.5", RTLD_NOW | RTLD_GLOBAL); + dlopen("libQt5QuickWidgets.so.5", RTLD_NOW | RTLD_GLOBAL); + dlopen("libqmlui.so", RTLD_NOW | RTLD_GLOBAL); + #endif + View::initClass(env, exports); return exports; } diff --git a/src/cpp/view.cpp b/src/cpp/view.cpp index db27f7e..785c261 100644 --- a/src/cpp/view.cpp +++ b/src/cpp/view.cpp @@ -1,7 +1,3 @@ -#ifdef __linux__ - #include -#endif - #include #include @@ -104,34 +100,13 @@ JS_METHOD(View::_init) { NAPI_ENV; REQ_STR_ARG(0, cwdOwn); REQ_OFFS_ARG(1, wnd); REQ_OFFS_ARG(2, ctx); - REQ_FUN_ARG(3, converter); + REQ_OFFS_ARG(3, device); + REQ_FUN_ARG(4, converter); _converter.Reset(converter, 1); _converter.SuppressDestruct(); - // Preload the libs with OUR @RPATH, not some junk builtin rpaths - #ifdef __linux__ - dlopen("libicui18n.so.56", RTLD_NOW | RTLD_GLOBAL); - dlopen("libicuuc.so.56", RTLD_NOW | RTLD_GLOBAL); - dlopen("libicudata.so.56", RTLD_NOW | RTLD_GLOBAL); - dlopen("libicuio.so.56", RTLD_NOW | RTLD_GLOBAL); - dlopen("libicule.so.56", RTLD_NOW | RTLD_GLOBAL); - dlopen("libicutu.so.56", RTLD_NOW | RTLD_GLOBAL); - dlopen("libQt5Core.so.5", RTLD_NOW | RTLD_GLOBAL); - dlopen("libQt5Network.so.5", RTLD_NOW | RTLD_GLOBAL); - dlopen("libQt5DBus.so.5", RTLD_NOW | RTLD_GLOBAL); - dlopen("libQt5Gui.so.5", RTLD_NOW | RTLD_GLOBAL); - dlopen("libQt5OpenGL.so.5", RTLD_NOW | RTLD_GLOBAL); - dlopen("libQt5Widgets.so.5", RTLD_NOW | RTLD_GLOBAL); - dlopen("libQt5XcbQpa.so.5", RTLD_NOW | RTLD_GLOBAL); - dlopen("libQt5Qml.so.5", RTLD_NOW | RTLD_GLOBAL); - dlopen("libQt5Quick.so.5", RTLD_NOW | RTLD_GLOBAL); - dlopen("libQt5QuickControls2.so.5", RTLD_NOW | RTLD_GLOBAL); - dlopen("libQt5QuickTemplates2.so.5", RTLD_NOW | RTLD_GLOBAL); - dlopen("libQt5QuickWidgets.so.5", RTLD_NOW | RTLD_GLOBAL); - #endif - - QmlUi::init(cwdOwn.c_str(), wnd, ctx, commonCb); + QmlUi::init2(cwdOwn.c_str(), wnd, ctx, device, commonCb); RET_UNDEFINED; }