Skip to content

Commit

Permalink
Adjust for linux
Browse files Browse the repository at this point in the history
  • Loading branch information
raub committed Oct 30, 2023
1 parent 21b7b8a commit 753ae32
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 97 deletions.
2 changes: 0 additions & 2 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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;
90 changes: 53 additions & 37 deletions examples/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';


Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
};
Expand Down
7 changes: 5 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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).
*
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -207,7 +210,7 @@ declare module "qml-raub" {
constructor(opts: TOptsProperty<T>);

get value(): T | null;
set value(v: T);
set value(v: T | null);
}

export type TOptsMethod = Readonly<{
Expand Down
10 changes: 6 additions & 4 deletions js/view.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
19 changes: 0 additions & 19 deletions src/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -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'",
Expand All @@ -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__'],
}],
Expand Down
27 changes: 27 additions & 0 deletions src/cpp/bindings.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
#ifdef __linux__
#include <dlfcn.h>
#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;
}
Expand Down
31 changes: 3 additions & 28 deletions src/cpp/view.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#ifdef __linux__
#include <dlfcn.h>
#endif

#include <map>
#include <qml-ui.hpp>

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 753ae32

Please sign in to comment.