Making everything snake case...

This commit is contained in:
Twaik Yont 2023-02-01 01:17:06 +02:00
parent 4a37640b5c
commit caa1edc690
6 changed files with 188 additions and 286 deletions

View File

@ -301,12 +301,6 @@ public class LorieService extends Service {
return listener;
}
void terminate() {
terminate(compositor);
compositor = 0;
Log.e("LorieService", "terminate");
}
@Override
public IBinder onBind(Intent intent) {
return null;
@ -476,40 +470,18 @@ public class LorieService extends Service {
};
}
private void windowChanged(Surface s, int w, int h, int pw, int ph) {windowChanged(compositor, s, w, h, pw, ph);}
private native void windowChanged(long compositor, Surface surface, int width, int height, int mmWidth, int mmHeight);
private void touchDown(int id, float x, float y) { touchDown(compositor, id, (int) x, (int) y); }
private native void touchDown(long compositor, int id, int x, int y);
private void touchMotion(int id, float x, float y) { touchMotion(compositor, id, (int) x, (int) y); }
private native void touchMotion(long compositor, int id, int x, int y);
private void touchUp(int id) { touchUp(compositor, id); }
private native void touchUp(long compositor, int id);
private void touchFrame() { touchFrame(compositor); }
private native void touchFrame(long compositor);
private void pointerMotion(float x, float y) { pointerMotion(compositor, (int) x, (int) y); }
private native void pointerMotion(long compositor, int x, int y);
private void pointerScroll(int axis, float value) { pointerScroll(compositor, axis, value); }
private native void pointerScroll(long compositor, int axis, float value);
private void pointerButton(int button, int type) { pointerButton(compositor, button, type); }
private native void pointerButton(long compositor, int button, int type);
private void keyboardKey(int key, int type, int shift, String characters) {keyboardKey(compositor, key, type, shift, characters);}
private native void keyboardKey(long compositor, int key, int type, int shift, String characters);
private void passWaylandFD(int fd) {passWaylandFD(compositor, fd);}
private native void passWaylandFD(long compositor, int fd);
private native void windowChanged(Surface surface, int width, int height, int mmWidth, int mmHeight);
private native void touchDown(int id, float x, float y);
private native void touchMotion(int id, float x, float y);
private native void touchUp(int id);
private native void touchFrame();
private native void pointerMotion(int x, int y);
private native void pointerScroll(int axis, float value);
private native void pointerButton(int button, int type);
private native void keyboardKey(int key, int type, int shift, String characters);
private native void passWaylandFD(int fd);
private native long createLorieThread();
private native void terminate(long compositor);
private native void terminate();
public static native void startLogcatForFd(int fd);
static {

View File

@ -17,9 +17,32 @@
#pragma ide diagnostic ignored "hicpp-signed-bitwise"
#define unused __attribute__((__unused__))
class LorieBackendAndroid : public LorieCompositor {
static jfieldID lorie_service_compositor_field_id = nullptr;
template<class F, F f, auto defaultValue = 0> struct wrapper_impl;
template<class R, class C, class... A, R(C::*f)(A...), auto defaultValue>
struct wrapper_impl<R(C::*)(A...), f, defaultValue> {
// Be careful and do passing jobjects here!!!
static inline R execute(JNIEnv* env, jobject obj, A... args) {
auto native = reinterpret_cast<C*>(env->GetLongField(obj, lorie_service_compositor_field_id));
if (native != nullptr)
return (native->*f)(args...);
return static_cast<R>(defaultValue);
}
static inline void queue(JNIEnv* env, jobject obj, A... args) {
auto native = reinterpret_cast<C*>(env->GetLongField(obj, lorie_service_compositor_field_id));
if (native != nullptr)
native->post([=]{ (native->*f)(args...); });
}
};
template<auto f, auto defaultValue = 0>
auto execute = wrapper_impl<decltype(f), f, defaultValue>::execute;
template<auto f, auto defaultValue = 0>
auto queue = wrapper_impl<decltype(f), f, defaultValue>::queue;
class lorie_backend_android : public lorie_compositor {
public:
LorieBackendAndroid();
lorie_backend_android();
void backend_init() override;
void swap_buffers() override;
@ -29,27 +52,21 @@ public:
void on_egl_init();
void unused on_egl_uninit();
LorieEGLHelper helper;
int keymap_fd = -1;
std::thread self;
};
LorieBackendAndroid::LorieBackendAndroid()
: self(&LorieCompositor::start, this) {
lorie_backend_android::lorie_backend_android()
: self(&lorie_compositor::start, this) {
}
void LorieBackendAndroid::on_egl_init() {
void lorie_backend_android::on_egl_init() {
renderer.init();
}
void unused LorieBackendAndroid::on_egl_uninit() {
renderer.uninit();
}
void LorieBackendAndroid::backend_init() {
void lorie_backend_android::backend_init() {
if (!helper.init(EGL_DEFAULT_DISPLAY)) {
LOGE("Failed to initialize EGL context");
}
@ -58,11 +75,11 @@ void LorieBackendAndroid::backend_init() {
}
void LorieBackendAndroid::swap_buffers () {
void lorie_backend_android::swap_buffers () {
helper.swap();
}
void LorieBackendAndroid::get_keymap(int *fd, int *size) {
void lorie_backend_android::get_keymap(int *fd, int *size) {
if (keymap_fd != -1)
close(keymap_fd);
@ -73,15 +90,16 @@ void LorieBackendAndroid::get_keymap(int *fd, int *size) {
*fd = keymap_fd;
}
void LorieBackendAndroid::window_change_callback(EGLNativeWindowType win, uint32_t width, uint32_t height, uint32_t physical_width, uint32_t physical_height) {
LOGE("WindowChangeCallback");
void lorie_backend_android::window_change_callback(EGLNativeWindowType win, uint32_t width, uint32_t height, uint32_t physical_width, uint32_t physical_height) {
LOGV("JNI: window is changed: %p %dx%d (%dmm x %dmm)", win, width, height, physical_width, physical_height);
helper.setWindow(win);
post([=, this]() {
output_resize(width, height, physical_width, physical_height);
});
}
void LorieBackendAndroid::passfd(int fd) {
void lorie_backend_android::passfd(int fd) {
LOGI("JNI: got fd %d", fd);
listen(fd, 128);
wl_display_add_socket_fd(display, fd);
}
@ -93,14 +111,8 @@ void LorieBackendAndroid::passfd(int fd) {
#define JNI_DECLARE(classname, methodname) \
JNI_DECLARE_INNER(com_termux_x11, classname, methodname)
#define WL_POINTER_MOTION 2
static LorieBackendAndroid* fromLong(jlong v) {
return reinterpret_cast<LorieBackendAndroid*>(v);
}
extern "C" JNIEXPORT jlong JNICALL
JNI_DECLARE(LorieService, createLorieThread)(unused JNIEnv *env, unused jobject instance) {
JNI_DECLARE(LorieService, createLorieThread)(JNIEnv *env, jobject thiz) {
#if 0
// It is needed to redirect stderr to logcat
setenv("WAYLAND_DEBUG", "1", 1);
@ -118,135 +130,69 @@ JNI_DECLARE(LorieService, createLorieThread)(unused JNIEnv *env, unused jobject
}
});
#endif
return (jlong) new LorieBackendAndroid;
lorie_service_compositor_field_id = env->GetFieldID(env->GetObjectClass(thiz), "compositor", "J");
return (jlong) new lorie_backend_android;
}
extern "C" JNIEXPORT void JNICALL
JNI_DECLARE(LorieService, passWaylandFD)(unused JNIEnv *env, unused jobject instance, jlong jcompositor, jint fd) {
if (jcompositor == 0) return;
LorieBackendAndroid *b = fromLong(jcompositor);
char path[256] = {0};
char realpath[256] = {0};
sprintf(path, "/proc/self/fd/%d", fd);
readlink(path, realpath, sizeof(realpath)); // NOLINT(bugprone-unused-return-value)
LOGI("JNI: got fd %d (%s)", fd, realpath);
b->passfd(fd);
JNI_DECLARE(LorieService, passWaylandFD)(JNIEnv *env, jobject thiz, jint fd) {
execute<&lorie_backend_android::passfd>(env, thiz, fd);
}
extern "C" JNIEXPORT void JNICALL
JNI_DECLARE(LorieService, terminate)(unused JNIEnv *env, unused jobject instance, jlong jcompositor) {
if (jcompositor == 0) return;
LorieBackendAndroid *b = fromLong(jcompositor);
LOGI("JNI: requested termination");
b->post([b](){
JNI_DECLARE(LorieService, terminate)(JNIEnv *env, jobject obj) {
auto b = reinterpret_cast<lorie_backend_android*>(env->GetLongField(obj, lorie_service_compositor_field_id));
b->terminate();
});
b->self.join();
}
extern "C" JNIEXPORT void JNICALL
JNI_DECLARE(LorieService, windowChanged)(JNIEnv *env, unused jobject instance, jlong jcompositor, jobject jsurface, jint width, jint height, jint mmWidth, jint mmHeight) {
if (jcompositor == 0) return;
LorieBackendAndroid *b = fromLong(jcompositor);
JNI_DECLARE(LorieService, windowChanged)(JNIEnv *env, jobject thiz, jobject jsurface, jint width, jint height, jint mm_width, jint mm_height) {
EGLNativeWindowType win = ANativeWindow_fromSurface(env, jsurface);
b->post([b, win, width, height, mmWidth, mmHeight](){
b->window_change_callback(win, width, height, mmWidth, mmHeight);
});
LOGV("JNI: window is changed: %p(%p) %dx%d (%dmm x %dmm)", win, jsurface, width, height, mmWidth, mmHeight);
queue<&lorie_backend_android::window_change_callback>(env, thiz, win, width, height, mm_width, mm_height);
}
extern "C" JNIEXPORT void JNICALL
JNI_DECLARE(LorieService, touchDown)(unused JNIEnv *env, unused jobject instance, jlong jcompositor, jint id, jint x, jint y) {
if (jcompositor == 0) return;
LorieBackendAndroid *b = fromLong(jcompositor);
LOGV("JNI: touch down");
b->post([b, id, x, y]() {
b->touch_down(static_cast<uint32_t>(id), static_cast<uint32_t>(x), static_cast<uint32_t>(y));
});
JNI_DECLARE(LorieService, touchDown)(JNIEnv *env, jobject thiz, jint id, jfloat x, jfloat y) {
queue<&lorie_backend_android::touch_down>(env, thiz, static_cast<uint32_t>(id), static_cast<uint32_t>(x), static_cast<uint32_t>(y));
}
extern "C" JNIEXPORT void JNICALL
JNI_DECLARE(LorieService, touchMotion)(unused JNIEnv *env, unused jobject instance, jlong jcompositor, jint id, jint x, jint y) {
if (jcompositor == 0) return;
LorieBackendAndroid *b = fromLong(jcompositor);
LOGV("JNI: touch motion");
b->post([b, id, x, y]() {
b->touch_motion(static_cast<uint32_t>(id), static_cast<uint32_t>(x),
static_cast<uint32_t>(y));
});
JNI_DECLARE(LorieService, touchMotion)(JNIEnv *env, jobject thiz, jint id, jfloat x, jfloat y) {
queue<&lorie_backend_android::touch_motion>(env, thiz, static_cast<uint32_t>(id), static_cast<uint32_t>(x), static_cast<uint32_t>(y));
}
extern "C" JNIEXPORT void JNICALL
JNI_DECLARE(LorieService, touchUp)(unused JNIEnv *env, unused jobject instance, jlong jcompositor, jint id) {
if (jcompositor == 0) return;
LorieBackendAndroid *b = fromLong(jcompositor);
LOGV("JNI: touch up");
b->post([b, id]() {
b->touch_up(static_cast<uint32_t>(id));
});
JNI_DECLARE(LorieService, touchUp)(JNIEnv *env, jobject thiz, jint id) {
queue<&lorie_backend_android::touch_up>(env, thiz, id);
}
extern "C" JNIEXPORT void JNICALL
JNI_DECLARE(LorieService, touchFrame)(unused JNIEnv *env, unused jobject instance, jlong jcompositor) {
if (jcompositor == 0) return;
LorieBackendAndroid *b = fromLong(jcompositor);
LOGV("JNI: touch frame");
b->post([b]() {
b->touch_frame();
});
JNI_DECLARE(LorieService, touchFrame)(JNIEnv *env, jobject thiz) {
queue<&lorie_backend_android::touch_frame>(env, thiz);
}
extern "C" JNIEXPORT void JNICALL
JNI_DECLARE(LorieService, pointerMotion)(unused JNIEnv *env, unused jobject instance, jlong jcompositor, jint x, jint y) {
if (jcompositor == 0) return;
LorieBackendAndroid *b = fromLong(jcompositor);
LOGV("JNI: pointer motion %dx%d", x, y);
b->post([b, x, y](){
b->pointer_motion(static_cast<uint32_t>(x), static_cast<uint32_t>(y));
});
JNI_DECLARE(LorieService, pointerMotion)(JNIEnv *env, jobject thiz, jint x, jint y) {
queue<&lorie_backend_android::pointer_motion>(env, thiz, static_cast<uint32_t>(x), static_cast<uint32_t>(y));
}
extern "C" JNIEXPORT void JNICALL
JNI_DECLARE(LorieService, pointerScroll)(unused JNIEnv *env, unused jobject instance, jlong jcompositor, jint axis, jfloat value) {
if (jcompositor == 0) return;
LorieBackendAndroid *b = fromLong(jcompositor);
LOGV("JNI: pointer scroll %d %f", axis, value);
b->post([b, axis, value]() {
b->pointer_scroll(static_cast<uint32_t>(axis), value);
});
JNI_DECLARE(LorieService, pointerScroll)(JNIEnv *env, jobject thiz, jint axis, jfloat value) {
queue<&lorie_backend_android::pointer_scroll>(env, thiz, static_cast<uint32_t>(axis), value);
}
extern "C" JNIEXPORT void JNICALL
JNI_DECLARE(LorieService, pointerButton)(unused JNIEnv *env, unused jobject instance, jlong jcompositor, jint button, jint type) {
if (jcompositor == 0) return;
LorieBackendAndroid *b = fromLong(jcompositor);
LOGV("JNI: pointer button %d type %d", button, type);
b->post([b, button, type]() {
b->pointer_button(static_cast<uint32_t>(button), static_cast<uint32_t>(type));
});
JNI_DECLARE(LorieService, pointerButton)(JNIEnv *env, jobject thiz, jint button, jint type) {
queue<&lorie_backend_android::pointer_button>(env, thiz, static_cast<uint32_t>(button), static_cast<uint32_t>(type));
}
extern "C" void get_character_data(char** layout, int *shift, int *ec, char *ch);
extern "C" void android_keycode_get_eventcode(int kc, int *ec, int *shift);
extern "C" JNIEXPORT void JNICALL
JNI_DECLARE(LorieService, keyboardKey)(JNIEnv *env, unused jobject instance,
jlong jcompositor, jint type,
jint key_code, jint jshift,
jstring characters_) {
if (jcompositor == 0) return;
LorieBackendAndroid *b = fromLong(jcompositor);
JNI_DECLARE(LorieService, keyboardKey)(JNIEnv *env, jobject thiz,
jint type, jint key_code, jint jshift, jstring characters_) {
char *characters = nullptr;
int event_code = 0;
@ -263,24 +209,16 @@ JNI_DECLARE(LorieService, keyboardKey)(JNIEnv *env, unused jobject instance,
LOGE("Keyboard input: keyCode: %d; eventCode: %d; characters: %s; shift: %d, type: %d", key_code, event_code, characters, shift, type);
if (shift || jshift)
b->post([b]() {
b->keyboard_key(42, WL_KEYBOARD_KEY_STATE_PRESSED); // Send KEY_LEFTSHIFT
});
queue<&lorie_backend_android::keyboard_key>(env, thiz, 42, wayland::keyboard_key_state::pressed);
// For some reason Android do not send ACTION_DOWN for non-English characters
if (characters)
b->post([b, event_code]() {
b->keyboard_key(static_cast<uint32_t>(event_code), WL_KEYBOARD_KEY_STATE_PRESSED);
});
queue<&lorie_backend_android::keyboard_key>(env, thiz, event_code, wayland::keyboard_key_state::pressed);
b->post([b, event_code, type]() {
b->keyboard_key(static_cast<uint32_t>(event_code), static_cast<uint32_t>(type));
});
queue<&lorie_backend_android::keyboard_key>(env, thiz, event_code, wayland::keyboard_key_state(type));
if (shift || jshift)
b->post([b]() {
b->keyboard_key(42, WL_KEYBOARD_KEY_STATE_RELEASED); // Send KEY_LEFTSHIFT
});
queue<&lorie_backend_android::keyboard_key>(env, thiz, 42, wayland::keyboard_key_state::released);
if (characters_ != nullptr) env->ReleaseStringUTFChars(characters_, characters);
}

View File

@ -7,7 +7,7 @@
using namespace wayland;
LorieCompositor::LorieCompositor() :
lorie_compositor::lorie_compositor() :
display(dpy),
global_compositor(dpy),
global_seat(dpy),
@ -137,14 +137,14 @@ cursor(renderer.cursor_surface) {
}
int proc(int fd, uint32_t mask, void *data) {
LorieCompositor *b = static_cast<LorieCompositor*>(data);
lorie_compositor *b = static_cast<lorie_compositor*>(data);
if (b == nullptr) {LOGF("b == nullptr"); return 0;}
b->queue.run();
return 0;
};
void LorieCompositor::start() {
void lorie_compositor::start() {
LogInit();
LOGV("Starting compositor");
wl_display_add_socket_auto(display);
@ -158,85 +158,78 @@ void LorieCompositor::start() {
wl_display_run(display);
}
void LorieCompositor::post(std::function<void()> f) {
void lorie_compositor::post(std::function<void()> f) {
queue.write(f);
}
struct wl_event_source* LorieCompositor::add_fd_listener(int fd, uint32_t mask, wl_event_loop_fd_func_t func, void *data) {
LOGV("Adding fd %d to event loop", fd);
struct wl_event_loop* loop = nullptr;
if (display != nullptr)
loop = wl_display_get_event_loop(display);
if (loop != nullptr)
return wl_event_loop_add_fd(loop, fd, mask, func, data);
return nullptr;
}
void LorieCompositor::terminate() {
LOGV("Terminating compositor");
void lorie_compositor::terminate() {
LOGI("JNI: requested termination");
if (display != nullptr)
wl_display_terminate(display);
}
void LorieCompositor::output_resize(int width, int height, uint32_t physical_width, uint32_t physical_height) {
void lorie_compositor::output_resize(int width, int height, uint32_t physical_width, uint32_t physical_height) {
// Xwayland segfaults without that line
if (width == 0 || height == 0 || physical_width == 0 || physical_height == 0) return;
renderer.resize(width, height, physical_width, physical_height);
post([this]() {
renderer.requestRedraw();
renderer.request_redraw();
});
}
void LorieCompositor::report_mode(wayland::output_t* output) const {
void lorie_compositor::report_mode(wayland::output_t* output) const {
output->geometry(0, 0, renderer.physical_width, renderer.physical_height, output_subpixel::unknown, "Lorie", "none", output_transform::normal);
output->scale(1.0);
output->mode(output_mode::current | output_mode::preferred, renderer.width, renderer.height, 60000);
output->done();
}
void LorieCompositor::touch_down(uint32_t id, uint32_t x, uint32_t y) {
void lorie_compositor::touch_down(uint32_t id, uint32_t x, uint32_t y) {
LOGV("JNI: touch down");
if (!toplevel)
return;
auto data = any_cast<client_data*>(toplevel->client()->user_data());
data->touch->down(next_serial(), resource_t::timestamp(), toplevel, id, x, y);
renderer.setCursorVisibility(false);
renderer.set_cursor_visibility(false);
}
void LorieCompositor::touch_motion(uint32_t id, uint32_t x, uint32_t y) {
void lorie_compositor::touch_motion(uint32_t id, uint32_t x, uint32_t y) {
LOGV("JNI: touch motion");
if (!toplevel)
return;
auto data = any_cast<client_data*>(toplevel->client()->user_data());
data->touch->motion(resource_t::timestamp(), id, x, y);
renderer.setCursorVisibility(false);
renderer.set_cursor_visibility(false);
}
void LorieCompositor::touch_up(uint32_t id) {
void lorie_compositor::touch_up(uint32_t id) {
LOGV("JNI: touch up");
if (!toplevel)
return;
auto data = any_cast<client_data*>(toplevel->client()->user_data());
data->touch->up(next_serial(), resource_t::timestamp(), id);
renderer.setCursorVisibility(false);
renderer.set_cursor_visibility(false);
}
void LorieCompositor::touch_frame() {
void lorie_compositor::touch_frame() {
LOGV("JNI: touch frame");
if (!toplevel)
return;
auto data = any_cast<client_data*>(toplevel->client()->user_data());
data->touch->frame();
renderer.setCursorVisibility(false);
renderer.set_cursor_visibility(false);
}
void LorieCompositor::pointer_motion(uint32_t x, uint32_t y) {
void lorie_compositor::pointer_motion(uint32_t x, uint32_t y) {
LOGV("JNI: pointer motion %dx%d", x, y);
if (!toplevel)
return;
@ -245,11 +238,12 @@ void LorieCompositor::pointer_motion(uint32_t x, uint32_t y) {
data->pointer->motion(resource_t::timestamp(), x, y);
data->pointer->frame();
renderer.setCursorVisibility(true);
renderer.cursorMove(x, y);
renderer.set_cursor_visibility(true);
renderer.cursor_move(x, y);
}
void LorieCompositor::pointer_scroll(uint32_t axis, float value) {
void lorie_compositor::pointer_scroll(uint32_t axis, float value) {
LOGV("JNI: pointer scroll %d %f", axis, value);
if (!toplevel)
return;
@ -258,10 +252,11 @@ void LorieCompositor::pointer_scroll(uint32_t axis, float value) {
data->pointer->axis_discrete(pointer_axis(axis), (value >= 0) ? 1 : -1);
data->pointer->axis(resource_t::timestamp(), pointer_axis(axis), value);
data->pointer->frame();
renderer.setCursorVisibility(true);
renderer.set_cursor_visibility(true);
}
void LorieCompositor::pointer_button(uint32_t button, uint32_t state) {
void lorie_compositor::pointer_button(uint32_t button, uint32_t state) {
LOGV("JNI: pointer button %d type %d", button, state);
if (!toplevel)
return;
@ -270,19 +265,18 @@ void LorieCompositor::pointer_button(uint32_t button, uint32_t state) {
LOGI("pointer button: %d %d", button, state);
data->pointer->button(next_serial(), resource_t::timestamp(), button, pointer_button_state(state));
data->pointer->frame();
renderer.setCursorVisibility(true);
renderer.set_cursor_visibility(true);
}
void LorieCompositor::keyboard_key(uint32_t key, uint32_t state) {
void lorie_compositor::keyboard_key(uint32_t key, keyboard_key_state state) {
if (!toplevel)
return;
auto data = any_cast<client_data*>(toplevel->client()->user_data());
data->kbd->key (next_serial(), resource_t::timestamp(), key, keyboard_key_state(state));
}
uint32_t LorieCompositor::next_serial() const {
uint32_t lorie_compositor::next_serial() const {
if (display == nullptr) return 0;
return wl_display_next_serial(display);

View File

@ -5,13 +5,12 @@
#include <lorie_message_queue.hpp>
#include "log.h"
class LorieCompositor {
class lorie_compositor {
public:
LorieCompositor();
lorie_compositor();
// compositor features
void start();
void post(std::function<void()> f);
struct wl_event_source* add_fd_listener(int fd, uint32_t mask, wl_event_loop_fd_func_t func, void *data);
void terminate();
void output_resize(int width, int height, uint32_t physical_width, uint32_t physical_height);
@ -24,7 +23,7 @@ public:
void pointer_motion(uint32_t x, uint32_t y); // absolute values
void pointer_scroll(uint32_t axis, float value);
void pointer_button(uint32_t button, uint32_t state);
void keyboard_key(uint32_t key, uint32_t state);
void keyboard_key(uint32_t key, wayland::keyboard_key_state state);
struct client_data {
wayland::output_t* output = nullptr;
@ -35,12 +34,12 @@ public:
struct surface_data {
uint32_t x = 0, y = 0;
LorieTexture texture;
lorie_texture texture;
wayland::buffer_t *buffer = NULL;
wayland::callback_t *frame_callback = NULL;
};
LorieRenderer renderer;
lorie_renderer renderer;
wayland::surface_t*& toplevel;
wayland::surface_t*& cursor;
@ -49,7 +48,7 @@ public:
virtual void backend_init() = 0;
virtual void swap_buffers() = 0;
virtual void get_keymap(int *fd, int *size) = 0;
virtual ~LorieCompositor() {};
virtual ~lorie_compositor() {};
//private:
wayland::display_t dpy;

View File

@ -2,16 +2,16 @@
#include <GLES2/gl2.h>
#include <limits.h>
class LorieRenderer;
class LorieTexture {
class lorie_renderer;
class lorie_texture {
private:
LorieRenderer* r = nullptr;
lorie_renderer* r = nullptr;
bool damaged = false;
public:
LorieTexture();
lorie_texture();
int width{}, height{};
void *data{};
void set_data(LorieRenderer* renderer, uint32_t width, uint32_t height, void *data);
void set_data(lorie_renderer* renderer, uint32_t width, uint32_t height, void *data);
void damage(int32_t x, int32_t y, int32_t width, int32_t height);
void uninit();
void reinit();
@ -20,15 +20,14 @@ private:
GLuint id = UINT_MAX;
void draw(float x0, float y0, float x1, float y1);
friend class LorieRenderer;
friend class lorie_renderer;
};
class LorieCompositor;
class LorieSurface;
class LorieRenderer {
class lorie_compositor;
class lorie_renderer {
public:
LorieRenderer(LorieCompositor& compositor);
void requestRedraw();
lorie_renderer(lorie_compositor& compositor);
void request_redraw();
void init();
void uninit();
@ -37,16 +36,16 @@ public:
uint32_t physical_width = 270;
uint32_t physical_height = 158;
bool cursorVisible = false;
bool cursor_visible = false;
uint32_t hotspot_x{}, hotspot_y{};
void resize(int w, int h, uint32_t pw, uint32_t ph);
void cursorMove(uint32_t x, uint32_t y);
void setCursorVisibility(bool visibility);
~LorieRenderer();
void cursor_move(uint32_t x, uint32_t y);
void set_cursor_visibility(bool visibility);
~lorie_renderer();
private:
LorieCompositor& compositor;
lorie_compositor& compositor;
void set_toplevel(wayland::surface_t* surface);
void set_cursor(wayland::surface_t* surface, uint32_t hotspot_x, uint32_t hotspot_y);
@ -55,14 +54,14 @@ private:
wayland::surface_t* cursor_surface = nullptr;
struct wl_event_source *idle = NULL;
void drawCursor();
void draw_cursor();
void redraw();
GLuint gTextureProgram = 0;
GLuint gvPos = 0;
GLuint gvCoords = 0;
GLuint gvTextureSamplerHandle = 0;
GLuint g_texture_program = 0;
GLuint gv_pos = 0;
GLuint gv_coords = 0;
GLuint gv_texture_sampler_handle = 0;
bool ready = false;
friend class LorieTexture;
friend class LorieCompositor;
friend class lorie_texture;
friend class lorie_compositor;
};

View File

@ -6,7 +6,7 @@
using namespace wayland;
static const char gSimpleVS[] = R"(
static const char vertex_shader[] = R"(
attribute vec4 position;
attribute vec2 texCoords;
varying vec2 outTexCoords;
@ -15,7 +15,7 @@ static const char gSimpleVS[] = R"(
gl_Position = position;
}
)";
static const char gSimpleFS[] = R"(
static const char fragment_shader[] = R"(
precision mediump float;
varying vec2 outTexCoords;
uniform sampler2D texture;
@ -24,7 +24,7 @@ static const char gSimpleFS[] = R"(
}
)";
static GLuint loadShader(GLenum shaderType, const char* pSource) {
static GLuint load_shader(GLenum shaderType, const char* pSource) {
GLuint shader = glCreateShader(shaderType);
if (shader) {
glShaderSource(shader, 1, &pSource, nullptr);
@ -49,13 +49,13 @@ static GLuint loadShader(GLenum shaderType, const char* pSource) {
return shader;
}
static GLuint createProgram(const char* pVertexSource, const char* pFragmentSource) {
GLuint vertexShader = loadShader(GL_VERTEX_SHADER, pVertexSource);
static GLuint create_program(const char* p_vertex_source, const char* p_fragment_source) {
GLuint vertexShader = load_shader(GL_VERTEX_SHADER, p_vertex_source);
if (!vertexShader) {
return 0;
}
GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, pFragmentSource);
GLuint pixelShader = load_shader(GL_FRAGMENT_SHADER, p_fragment_source);
if (!pixelShader) {
return 0;
}
@ -85,39 +85,39 @@ static GLuint createProgram(const char* pVertexSource, const char* pFragmentSour
return program;
}
static inline LorieCompositor::surface_data* data(surface_t* sfc) {
return any_cast<LorieCompositor::surface_data*>(sfc->user_data());
static inline lorie_compositor::surface_data* data(surface_t* sfc) {
return any_cast<lorie_compositor::surface_data*>(sfc->user_data());
}
LorieRenderer::LorieRenderer(LorieCompositor& compositor) : compositor(compositor) {}
lorie_renderer::lorie_renderer(lorie_compositor& compositor) : compositor(compositor) {}
void LorieRenderer::init() {
void lorie_renderer::init() {
LOGV("Initializing renderer (tid %d)", ::gettid());
gTextureProgram = createProgram(gSimpleVS, gSimpleFS);
if (!gTextureProgram) {
g_texture_program = create_program(vertex_shader, fragment_shader);
if (!g_texture_program) {
LOGE("GLESv2: Unable to create shader program");
return;
}
gvPos = (GLuint) glGetAttribLocation(gTextureProgram, "position");
gvCoords = (GLuint) glGetAttribLocation(gTextureProgram, "texCoords");
gvTextureSamplerHandle = (GLuint) glGetUniformLocation(gTextureProgram, "texture");
gv_pos = (GLuint) glGetAttribLocation(g_texture_program, "position");
gv_coords = (GLuint) glGetAttribLocation(g_texture_program, "texCoords");
gv_texture_sampler_handle = (GLuint) glGetUniformLocation(g_texture_program, "texture");
ready = true;
redraw();
}
void LorieRenderer::requestRedraw() {
void lorie_renderer::request_redraw() {
//LOGV("Requesting redraw");
if (idle) return;
idle = wl_event_loop_add_idle(
wl_display_get_event_loop(compositor.display),
[](void* data) { reinterpret_cast<LorieRenderer*>(data)->redraw(); },
[](void* data) { reinterpret_cast<lorie_renderer*>(data)->redraw(); },
this
);
}
void LorieRenderer::resize(int w, int h, uint32_t pw, uint32_t ph) {
void lorie_renderer::resize(int w, int h, uint32_t pw, uint32_t ph) {
LOGV("Resizing renderer to %dx%d (%dmm x %dmm)", w, h, pw, ph);
if (w == width &&
h == height &&
@ -131,32 +131,32 @@ void LorieRenderer::resize(int w, int h, uint32_t pw, uint32_t ph) {
glViewport(0, 0, w, h);
if (toplevel_surface) {
auto data = any_cast<LorieCompositor::client_data*>(toplevel_surface->client()->user_data());
auto data = any_cast<lorie_compositor::client_data*>(toplevel_surface->client()->user_data());
compositor.report_mode(data->output);
}
}
void LorieRenderer::cursorMove(uint32_t x, uint32_t y) {
void lorie_renderer::cursor_move(uint32_t x, uint32_t y) {
if (compositor.cursor == nullptr) return;
data(cursor_surface)->x = x;
data(cursor_surface)->y = y;
requestRedraw();
request_redraw();
}
void LorieRenderer::setCursorVisibility(bool visibility) {
if (cursorVisible != visibility)
cursorVisible = visibility;
void lorie_renderer::set_cursor_visibility(bool visibility) {
if (cursor_visible != visibility)
cursor_visible = visibility;
}
void LorieRenderer::set_toplevel(surface_t* surface) {
void lorie_renderer::set_toplevel(surface_t* surface) {
LOGV("Setting surface %p as toplevel", surface);
if (toplevel_surface) data(toplevel_surface)->texture.uninit();
toplevel_surface = surface;
requestRedraw();
request_redraw();
}
void LorieRenderer::set_cursor(surface_t* surface, uint32_t hotspot_x, uint32_t hotspot_y) {
void lorie_renderer::set_cursor(surface_t* surface, uint32_t hotspot_x, uint32_t hotspot_y) {
LOGV("Setting surface %p as cursor", surface);
if (cursor_surface)
data(cursor_surface)->texture.uninit();
@ -164,10 +164,10 @@ void LorieRenderer::set_cursor(surface_t* surface, uint32_t hotspot_x, uint32_t
this->hotspot_x = hotspot_x;
this->hotspot_y = hotspot_y;
requestRedraw();
request_redraw();
}
void LorieRenderer::drawCursor() {
void lorie_renderer::draw_cursor() {
if (cursor_surface == nullptr) return;
auto toplevel = data(toplevel_surface);
@ -189,7 +189,7 @@ void LorieRenderer::drawCursor() {
glDisable(GL_BLEND);
}
void LorieRenderer::redraw() {
void lorie_renderer::redraw() {
//LOGV("Redrawing screen");
idle = NULL;
if (!ready) return;
@ -201,33 +201,33 @@ void LorieRenderer::redraw() {
data(toplevel_surface)->texture.reinit();
data(toplevel_surface)->texture.draw(-1.0, -1.0, 1.0, 1.0);
if (cursorVisible)
drawCursor();
if (cursor_visible)
draw_cursor();
}
compositor.swap_buffers();
}
void LorieRenderer::uninit() {
void lorie_renderer::uninit() {
ready = false;
LOGV("Destroying renderer (tid %d)", ::gettid());
glUseProgram(0);
glDeleteProgram(gTextureProgram);
gTextureProgram = gvPos = gvCoords = gvTextureSamplerHandle = 0;
glDeleteProgram(g_texture_program);
g_texture_program = gv_pos = gv_coords = gv_texture_sampler_handle = 0;
if (toplevel_surface) data(toplevel_surface)->texture.uninit();
if (cursor_surface) data(cursor_surface)->texture.uninit();
}
LorieRenderer::~LorieRenderer() {
lorie_renderer::~lorie_renderer() {
uninit();
}
LorieTexture::LorieTexture(){
lorie_texture::lorie_texture(){
uninit();
}
void LorieTexture::uninit() {
void lorie_texture::uninit() {
if (valid()) {
glDeleteTextures(1, &id);
}
@ -236,7 +236,7 @@ void LorieTexture::uninit() {
r = nullptr;
}
void LorieTexture::set_data(LorieRenderer* renderer, uint32_t width, uint32_t height, void *data) {
void lorie_texture::set_data(lorie_renderer* renderer, uint32_t width, uint32_t height, void *data) {
uninit();
LOGV("Reinitializing texture to %dx%d", width, height);
this->r = renderer;
@ -246,7 +246,7 @@ void LorieTexture::set_data(LorieRenderer* renderer, uint32_t width, uint32_t he
reinit();
}
void LorieTexture::reinit() {
void lorie_texture::reinit() {
glClearColor(0.f, 0.f, 0.f, 0.f);
glClear(GL_COLOR_BUFFER_BIT);
@ -260,16 +260,16 @@ void LorieTexture::reinit() {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
}
void LorieTexture::damage(int32_t x, int32_t y, int32_t width, int32_t height) {
void lorie_texture::damage(int32_t x, int32_t y, int32_t width, int32_t height) {
damaged = true;
r->requestRedraw();
r->request_redraw();
}
bool LorieTexture::valid() {
bool lorie_texture::valid() {
return (width != 0 && height != 0 && id != UINT_MAX && r != nullptr);
}
void LorieTexture::draw(float x0, float y0, float x1, float y1) {
void lorie_texture::draw(float x0, float y0, float x1, float y1) {
if (!valid()) return;
float coords[20] = {
x0, -y0, 0.f, 0.f, 0.f,
@ -279,7 +279,7 @@ void LorieTexture::draw(float x0, float y0, float x1, float y1) {
};
//LOGV("Drawing texture %p", this);
glActiveTexture(GL_TEXTURE0);
glUseProgram(r->gTextureProgram);
glUseProgram(r->g_texture_program);
glBindTexture(GL_TEXTURE_2D, id);
if (damaged) {
@ -287,10 +287,10 @@ void LorieTexture::draw(float x0, float y0, float x1, float y1) {
damaged = false;
}
glVertexAttribPointer(r->gvPos, 3, GL_FLOAT, GL_FALSE, 20, coords);
glVertexAttribPointer(r->gvCoords, 2, GL_FLOAT, GL_FALSE, 20, &coords[3]);
glEnableVertexAttribArray(r->gvPos);
glEnableVertexAttribArray(r->gvCoords);
glVertexAttribPointer(r->gv_pos, 3, GL_FLOAT, GL_FALSE, 20, coords);
glVertexAttribPointer(r->gv_coords, 2, GL_FLOAT, GL_FALSE, 20, &coords[3]);
glEnableVertexAttribArray(r->gv_pos);
glEnableVertexAttribArray(r->gv_coords);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindTexture(GL_TEXTURE_2D, 0);