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; return listener;
} }
void terminate() {
terminate(compositor);
compositor = 0;
Log.e("LorieService", "terminate");
}
@Override @Override
public IBinder onBind(Intent intent) { public IBinder onBind(Intent intent) {
return null; 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(Surface surface, int width, int height, int mmWidth, int mmHeight);
private native void windowChanged(long compositor, 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 void touchDown(int id, float x, float y) { touchDown(compositor, id, (int) x, (int) y); } private native void touchUp(int id);
private native void touchDown(long compositor, int id, int x, int y); private native void touchFrame();
private native void pointerMotion(int x, int y);
private void touchMotion(int id, float x, float y) { touchMotion(compositor, id, (int) x, (int) y); } private native void pointerScroll(int axis, float value);
private native void touchMotion(long compositor, int id, int x, int y); private native void pointerButton(int button, int type);
private native void keyboardKey(int key, int type, int shift, String characters);
private void touchUp(int id) { touchUp(compositor, id); } private native void passWaylandFD(int fd);
private native void touchUp(long compositor, int id); private native long createLorieThread();
private native void terminate();
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 long createLorieThread();
private native void terminate(long compositor);
public static native void startLogcatForFd(int fd); public static native void startLogcatForFd(int fd);
static { static {

View File

@ -17,9 +17,32 @@
#pragma ide diagnostic ignored "hicpp-signed-bitwise" #pragma ide diagnostic ignored "hicpp-signed-bitwise"
#define unused __attribute__((__unused__)) #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: public:
LorieBackendAndroid(); lorie_backend_android();
void backend_init() override; void backend_init() override;
void swap_buffers() override; void swap_buffers() override;
@ -29,27 +52,21 @@ public:
void on_egl_init(); void on_egl_init();
void unused on_egl_uninit();
LorieEGLHelper helper; LorieEGLHelper helper;
int keymap_fd = -1; int keymap_fd = -1;
std::thread self; std::thread self;
}; };
LorieBackendAndroid::LorieBackendAndroid() lorie_backend_android::lorie_backend_android()
: self(&LorieCompositor::start, this) { : self(&lorie_compositor::start, this) {
} }
void LorieBackendAndroid::on_egl_init() { void lorie_backend_android::on_egl_init() {
renderer.init(); renderer.init();
} }
void unused LorieBackendAndroid::on_egl_uninit() { void lorie_backend_android::backend_init() {
renderer.uninit();
}
void LorieBackendAndroid::backend_init() {
if (!helper.init(EGL_DEFAULT_DISPLAY)) { if (!helper.init(EGL_DEFAULT_DISPLAY)) {
LOGE("Failed to initialize EGL context"); 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(); helper.swap();
} }
void LorieBackendAndroid::get_keymap(int *fd, int *size) { void lorie_backend_android::get_keymap(int *fd, int *size) {
if (keymap_fd != -1) if (keymap_fd != -1)
close(keymap_fd); close(keymap_fd);
@ -73,15 +90,16 @@ void LorieBackendAndroid::get_keymap(int *fd, int *size) {
*fd = keymap_fd; *fd = keymap_fd;
} }
void LorieBackendAndroid::window_change_callback(EGLNativeWindowType win, uint32_t width, uint32_t height, uint32_t physical_width, uint32_t physical_height) { void lorie_backend_android::window_change_callback(EGLNativeWindowType win, uint32_t width, uint32_t height, uint32_t physical_width, uint32_t physical_height) {
LOGE("WindowChangeCallback"); LOGV("JNI: window is changed: %p %dx%d (%dmm x %dmm)", win, width, height, physical_width, physical_height);
helper.setWindow(win); helper.setWindow(win);
post([=, this]() { post([=, this]() {
output_resize(width, height, physical_width, physical_height); 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); listen(fd, 128);
wl_display_add_socket_fd(display, fd); wl_display_add_socket_fd(display, fd);
} }
@ -93,14 +111,8 @@ void LorieBackendAndroid::passfd(int fd) {
#define JNI_DECLARE(classname, methodname) \ #define JNI_DECLARE(classname, methodname) \
JNI_DECLARE_INNER(com_termux_x11, 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 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 #if 0
// It is needed to redirect stderr to logcat // It is needed to redirect stderr to logcat
setenv("WAYLAND_DEBUG", "1", 1); setenv("WAYLAND_DEBUG", "1", 1);
@ -118,135 +130,69 @@ JNI_DECLARE(LorieService, createLorieThread)(unused JNIEnv *env, unused jobject
} }
}); });
#endif #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 extern "C" JNIEXPORT void JNICALL
JNI_DECLARE(LorieService, passWaylandFD)(unused JNIEnv *env, unused jobject instance, jlong jcompositor, jint fd) { JNI_DECLARE(LorieService, passWaylandFD)(JNIEnv *env, jobject thiz, jint fd) {
if (jcompositor == 0) return; execute<&lorie_backend_android::passfd>(env, thiz, fd);
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);
} }
extern "C" JNIEXPORT void JNICALL extern "C" JNIEXPORT void JNICALL
JNI_DECLARE(LorieService, terminate)(unused JNIEnv *env, unused jobject instance, jlong jcompositor) { JNI_DECLARE(LorieService, terminate)(JNIEnv *env, jobject obj) {
if (jcompositor == 0) return; auto b = reinterpret_cast<lorie_backend_android*>(env->GetLongField(obj, lorie_service_compositor_field_id));
LorieBackendAndroid *b = fromLong(jcompositor); b->terminate();
LOGI("JNI: requested termination");
b->post([b](){
b->terminate();
});
b->self.join(); b->self.join();
} }
extern "C" JNIEXPORT void JNICALL 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) { JNI_DECLARE(LorieService, windowChanged)(JNIEnv *env, jobject thiz, jobject jsurface, jint width, jint height, jint mm_width, jint mm_height) {
if (jcompositor == 0) return;
LorieBackendAndroid *b = fromLong(jcompositor);
EGLNativeWindowType win = ANativeWindow_fromSurface(env, jsurface); EGLNativeWindowType win = ANativeWindow_fromSurface(env, jsurface);
b->post([b, win, width, height, mmWidth, mmHeight](){ queue<&lorie_backend_android::window_change_callback>(env, thiz, win, width, height, mm_width, mm_height);
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);
} }
extern "C" JNIEXPORT void JNICALL extern "C" JNIEXPORT void JNICALL
JNI_DECLARE(LorieService, touchDown)(unused JNIEnv *env, unused jobject instance, jlong jcompositor, jint id, jint x, jint y) { JNI_DECLARE(LorieService, touchDown)(JNIEnv *env, jobject thiz, jint id, jfloat x, jfloat y) {
if (jcompositor == 0) return; queue<&lorie_backend_android::touch_down>(env, thiz, static_cast<uint32_t>(id), static_cast<uint32_t>(x), static_cast<uint32_t>(y));
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));
});
} }
extern "C" JNIEXPORT void JNICALL extern "C" JNIEXPORT void JNICALL
JNI_DECLARE(LorieService, touchMotion)(unused JNIEnv *env, unused jobject instance, jlong jcompositor, jint id, jint x, jint y) { JNI_DECLARE(LorieService, touchMotion)(JNIEnv *env, jobject thiz, jint id, jfloat x, jfloat y) {
if (jcompositor == 0) return; queue<&lorie_backend_android::touch_motion>(env, thiz, static_cast<uint32_t>(id), static_cast<uint32_t>(x), static_cast<uint32_t>(y));
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));
});
} }
extern "C" JNIEXPORT void JNICALL extern "C" JNIEXPORT void JNICALL
JNI_DECLARE(LorieService, touchUp)(unused JNIEnv *env, unused jobject instance, jlong jcompositor, jint id) { JNI_DECLARE(LorieService, touchUp)(JNIEnv *env, jobject thiz, jint id) {
if (jcompositor == 0) return; queue<&lorie_backend_android::touch_up>(env, thiz, id);
LorieBackendAndroid *b = fromLong(jcompositor);
LOGV("JNI: touch up");
b->post([b, id]() {
b->touch_up(static_cast<uint32_t>(id));
});
} }
extern "C" JNIEXPORT void JNICALL extern "C" JNIEXPORT void JNICALL
JNI_DECLARE(LorieService, touchFrame)(unused JNIEnv *env, unused jobject instance, jlong jcompositor) { JNI_DECLARE(LorieService, touchFrame)(JNIEnv *env, jobject thiz) {
if (jcompositor == 0) return; queue<&lorie_backend_android::touch_frame>(env, thiz);
LorieBackendAndroid *b = fromLong(jcompositor);
LOGV("JNI: touch frame");
b->post([b]() {
b->touch_frame();
});
} }
extern "C" JNIEXPORT void JNICALL extern "C" JNIEXPORT void JNICALL
JNI_DECLARE(LorieService, pointerMotion)(unused JNIEnv *env, unused jobject instance, jlong jcompositor, jint x, jint y) { JNI_DECLARE(LorieService, pointerMotion)(JNIEnv *env, jobject thiz, jint x, jint y) {
if (jcompositor == 0) return; queue<&lorie_backend_android::pointer_motion>(env, thiz, static_cast<uint32_t>(x), static_cast<uint32_t>(y));
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));
});
} }
extern "C" JNIEXPORT void JNICALL extern "C" JNIEXPORT void JNICALL
JNI_DECLARE(LorieService, pointerScroll)(unused JNIEnv *env, unused jobject instance, jlong jcompositor, jint axis, jfloat value) { JNI_DECLARE(LorieService, pointerScroll)(JNIEnv *env, jobject thiz, jint axis, jfloat value) {
if (jcompositor == 0) return; queue<&lorie_backend_android::pointer_scroll>(env, thiz, static_cast<uint32_t>(axis), value);
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);
});
} }
extern "C" JNIEXPORT void JNICALL extern "C" JNIEXPORT void JNICALL
JNI_DECLARE(LorieService, pointerButton)(unused JNIEnv *env, unused jobject instance, jlong jcompositor, jint button, jint type) { JNI_DECLARE(LorieService, pointerButton)(JNIEnv *env, jobject thiz, jint button, jint type) {
if (jcompositor == 0) return; queue<&lorie_backend_android::pointer_button>(env, thiz, static_cast<uint32_t>(button), static_cast<uint32_t>(type));
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));
});
} }
extern "C" void get_character_data(char** layout, int *shift, int *ec, char *ch); 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" void android_keycode_get_eventcode(int kc, int *ec, int *shift);
extern "C" JNIEXPORT void JNICALL extern "C" JNIEXPORT void JNICALL
JNI_DECLARE(LorieService, keyboardKey)(JNIEnv *env, unused jobject instance, JNI_DECLARE(LorieService, keyboardKey)(JNIEnv *env, jobject thiz,
jlong jcompositor, jint type, jint type, jint key_code, jint jshift, jstring characters_) {
jint key_code, jint jshift,
jstring characters_) {
if (jcompositor == 0) return;
LorieBackendAndroid *b = fromLong(jcompositor);
char *characters = nullptr; char *characters = nullptr;
int event_code = 0; 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); LOGE("Keyboard input: keyCode: %d; eventCode: %d; characters: %s; shift: %d, type: %d", key_code, event_code, characters, shift, type);
if (shift || jshift) if (shift || jshift)
b->post([b]() { queue<&lorie_backend_android::keyboard_key>(env, thiz, 42, wayland::keyboard_key_state::pressed);
b->keyboard_key(42, WL_KEYBOARD_KEY_STATE_PRESSED); // Send KEY_LEFTSHIFT
});
// For some reason Android do not send ACTION_DOWN for non-English characters // For some reason Android do not send ACTION_DOWN for non-English characters
if (characters) if (characters)
b->post([b, event_code]() { queue<&lorie_backend_android::keyboard_key>(env, thiz, event_code, wayland::keyboard_key_state::pressed);
b->keyboard_key(static_cast<uint32_t>(event_code), WL_KEYBOARD_KEY_STATE_PRESSED);
});
b->post([b, event_code, type]() { queue<&lorie_backend_android::keyboard_key>(env, thiz, event_code, wayland::keyboard_key_state(type));
b->keyboard_key(static_cast<uint32_t>(event_code), static_cast<uint32_t>(type));
});
if (shift || jshift) if (shift || jshift)
b->post([b]() { queue<&lorie_backend_android::keyboard_key>(env, thiz, 42, wayland::keyboard_key_state::released);
b->keyboard_key(42, WL_KEYBOARD_KEY_STATE_RELEASED); // Send KEY_LEFTSHIFT
});
if (characters_ != nullptr) env->ReleaseStringUTFChars(characters_, characters); if (characters_ != nullptr) env->ReleaseStringUTFChars(characters_, characters);
} }
@ -346,4 +284,4 @@ Java_com_termux_x11_LorieService_startLogcatForFd(unused JNIEnv *env, unused jcl
execl("/system/bin/logcat", "logcat", nullptr); execl("/system/bin/logcat", "logcat", nullptr);
LOGE("exec logcat: %s", strerror(errno)); LOGE("exec logcat: %s", strerror(errno));
}); });
} }

View File

@ -7,7 +7,7 @@
using namespace wayland; using namespace wayland;
LorieCompositor::LorieCompositor() : lorie_compositor::lorie_compositor() :
display(dpy), display(dpy),
global_compositor(dpy), global_compositor(dpy),
global_seat(dpy), global_seat(dpy),
@ -137,14 +137,14 @@ cursor(renderer.cursor_surface) {
} }
int proc(int fd, uint32_t mask, void *data) { 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;} if (b == nullptr) {LOGF("b == nullptr"); return 0;}
b->queue.run(); b->queue.run();
return 0; return 0;
}; };
void LorieCompositor::start() { void lorie_compositor::start() {
LogInit(); LogInit();
LOGV("Starting compositor"); LOGV("Starting compositor");
wl_display_add_socket_auto(display); wl_display_add_socket_auto(display);
@ -158,85 +158,78 @@ void LorieCompositor::start() {
wl_display_run(display); wl_display_run(display);
} }
void LorieCompositor::post(std::function<void()> f) { void lorie_compositor::post(std::function<void()> f) {
queue.write(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) { void lorie_compositor::terminate() {
LOGV("Adding fd %d to event loop", fd); LOGI("JNI: requested termination");
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");
if (display != nullptr) if (display != nullptr)
wl_display_terminate(display); 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 // Xwayland segfaults without that line
if (width == 0 || height == 0 || physical_width == 0 || physical_height == 0) return; if (width == 0 || height == 0 || physical_width == 0 || physical_height == 0) return;
renderer.resize(width, height, physical_width, physical_height); renderer.resize(width, height, physical_width, physical_height);
post([this]() { 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->geometry(0, 0, renderer.physical_width, renderer.physical_height, output_subpixel::unknown, "Lorie", "none", output_transform::normal);
output->scale(1.0); output->scale(1.0);
output->mode(output_mode::current | output_mode::preferred, renderer.width, renderer.height, 60000); output->mode(output_mode::current | output_mode::preferred, renderer.width, renderer.height, 60000);
output->done(); 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) if (!toplevel)
return; return;
auto data = any_cast<client_data*>(toplevel->client()->user_data()); auto data = any_cast<client_data*>(toplevel->client()->user_data());
data->touch->down(next_serial(), resource_t::timestamp(), toplevel, id, x, y); 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) if (!toplevel)
return; return;
auto data = any_cast<client_data*>(toplevel->client()->user_data()); auto data = any_cast<client_data*>(toplevel->client()->user_data());
data->touch->motion(resource_t::timestamp(), id, x, y); 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) if (!toplevel)
return; return;
auto data = any_cast<client_data*>(toplevel->client()->user_data()); auto data = any_cast<client_data*>(toplevel->client()->user_data());
data->touch->up(next_serial(), resource_t::timestamp(), id); 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) if (!toplevel)
return; return;
auto data = any_cast<client_data*>(toplevel->client()->user_data()); auto data = any_cast<client_data*>(toplevel->client()->user_data());
data->touch->frame(); 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) if (!toplevel)
return; 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->motion(resource_t::timestamp(), x, y);
data->pointer->frame(); data->pointer->frame();
renderer.setCursorVisibility(true); renderer.set_cursor_visibility(true);
renderer.cursorMove(x, y); 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) if (!toplevel)
return; 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_discrete(pointer_axis(axis), (value >= 0) ? 1 : -1);
data->pointer->axis(resource_t::timestamp(), pointer_axis(axis), value); data->pointer->axis(resource_t::timestamp(), pointer_axis(axis), value);
data->pointer->frame(); 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) if (!toplevel)
return; return;
@ -270,19 +265,18 @@ void LorieCompositor::pointer_button(uint32_t button, uint32_t state) {
LOGI("pointer button: %d %d", button, state); LOGI("pointer button: %d %d", button, state);
data->pointer->button(next_serial(), resource_t::timestamp(), button, pointer_button_state(state)); data->pointer->button(next_serial(), resource_t::timestamp(), button, pointer_button_state(state));
data->pointer->frame(); 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) if (!toplevel)
return; return;
auto data = any_cast<client_data*>(toplevel->client()->user_data()); auto data = any_cast<client_data*>(toplevel->client()->user_data());
data->kbd->key (next_serial(), resource_t::timestamp(), key, keyboard_key_state(state)); 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; if (display == nullptr) return 0;
return wl_display_next_serial(display); return wl_display_next_serial(display);

View File

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

View File

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

View File

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