2017-06-20 21:29:27 +02:00
|
|
|
#include <stdio.h>
|
2017-06-20 18:27:05 +02:00
|
|
|
#include <assert.h>
|
2017-06-19 22:36:40 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <wayland-client.h>
|
2017-06-20 18:27:05 +02:00
|
|
|
#include <GLES3/gl3.h>
|
2017-06-21 16:27:45 +02:00
|
|
|
#include <wlr/interfaces/wlr_output.h>
|
2017-06-21 18:10:07 +02:00
|
|
|
#include <wlr/util/log.h>
|
2017-06-19 22:36:40 +02:00
|
|
|
#include "backend/wayland.h"
|
|
|
|
|
2017-06-20 18:27:05 +02:00
|
|
|
static struct wl_callback_listener frame_listener;
|
|
|
|
|
|
|
|
static void surface_frame_callback(void *data, struct wl_callback *cb, uint32_t time) {
|
|
|
|
struct wlr_output_state *output = data;
|
|
|
|
assert(output);
|
|
|
|
|
2017-06-26 17:34:22 +02:00
|
|
|
struct wlr_output *wlr_output = output->wlr_output;
|
|
|
|
wl_signal_emit(&wlr_output->events.frame, wlr_output);
|
|
|
|
wl_callback_destroy(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct wl_callback_listener frame_listener = {
|
|
|
|
.done = surface_frame_callback
|
|
|
|
};
|
|
|
|
|
|
|
|
static void wlr_wl_output_make_current(struct wlr_output_state *output) {
|
2017-06-20 18:27:05 +02:00
|
|
|
if (!eglMakeCurrent(output->backend->egl.display,
|
|
|
|
output->egl_surface, output->egl_surface,
|
|
|
|
output->backend->egl.context)) {
|
|
|
|
wlr_log(L_ERROR, "eglMakeCurrent failed: %s", egl_error());
|
|
|
|
}
|
2017-06-26 17:34:22 +02:00
|
|
|
}
|
2017-06-20 18:27:05 +02:00
|
|
|
|
2017-06-26 17:34:22 +02:00
|
|
|
static void wlr_wl_output_swap_buffers(struct wlr_output_state *output) {
|
2017-06-20 18:27:05 +02:00
|
|
|
output->frame_callback = wl_surface_frame(output->surface);
|
|
|
|
wl_callback_add_listener(output->frame_callback, &frame_listener, output);
|
|
|
|
if (!eglSwapBuffers(output->backend->egl.display, output->egl_surface)) {
|
|
|
|
wlr_log(L_ERROR, "eglSwapBuffers failed: %s", egl_error());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:36:40 +02:00
|
|
|
static void wlr_wl_output_transform(struct wlr_output_state *output,
|
|
|
|
enum wl_output_transform transform) {
|
2017-06-21 00:22:21 +02:00
|
|
|
output->wlr_output->transform = transform;
|
2017-06-19 22:36:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void wlr_wl_output_destroy(struct wlr_output_state *output) {
|
2017-08-12 17:43:36 +02:00
|
|
|
wl_signal_emit(&output->backend->backend.events.output_remove, output->wlr_output);
|
|
|
|
if (output->frame_callback) {
|
|
|
|
wl_callback_destroy(output->frame_callback);
|
|
|
|
}
|
2017-06-20 19:01:20 +02:00
|
|
|
eglDestroySurface(output->backend->egl.display, output->surface);
|
|
|
|
wl_egl_window_destroy(output->egl_window);
|
2017-06-19 22:36:40 +02:00
|
|
|
wl_shell_surface_destroy(output->shell_surface);
|
|
|
|
wl_surface_destroy(output->surface);
|
|
|
|
free(output);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct wlr_output_impl output_impl = {
|
|
|
|
.transform = wlr_wl_output_transform,
|
|
|
|
.destroy = wlr_wl_output_destroy,
|
2017-06-26 17:34:22 +02:00
|
|
|
.make_current = wlr_wl_output_make_current,
|
|
|
|
.swap_buffers = wlr_wl_output_swap_buffers,
|
2017-06-19 22:36:40 +02:00
|
|
|
};
|
|
|
|
|
2017-06-20 18:27:05 +02:00
|
|
|
void handle_ping(void* data, struct wl_shell_surface* ssurface, uint32_t serial) {
|
|
|
|
struct wlr_output_state *output = data;
|
|
|
|
assert(output && output->shell_surface == ssurface);
|
|
|
|
wl_shell_surface_pong(ssurface, serial);
|
|
|
|
}
|
|
|
|
|
|
|
|
void handle_configure(void *data, struct wl_shell_surface *wl_shell_surface,
|
|
|
|
uint32_t edges, int32_t width, int32_t height){
|
2017-06-20 23:51:45 +02:00
|
|
|
struct wlr_output_state *ostate = data;
|
|
|
|
assert(ostate && ostate->shell_surface == wl_shell_surface);
|
2017-06-21 00:22:21 +02:00
|
|
|
struct wlr_output *output = ostate->wlr_output;
|
2017-06-20 23:51:45 +02:00
|
|
|
wl_egl_window_resize(ostate->egl_window, width, height, 0, 0);
|
|
|
|
output->width = width;
|
|
|
|
output->height = height;
|
|
|
|
wlr_output_update_matrix(output);
|
|
|
|
wl_signal_emit(&output->events.resolution, output);
|
2017-06-20 18:27:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void handle_popup_done(void *data, struct wl_shell_surface *wl_shell_surface) {
|
2017-06-20 23:51:45 +02:00
|
|
|
wlr_log(L_ERROR, "Unexpected wl_shell_surface.popup_done event");
|
2017-06-20 18:27:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct wl_shell_surface_listener shell_surface_listener = {
|
|
|
|
.ping = handle_ping,
|
|
|
|
.configure = handle_configure,
|
|
|
|
.popup_done = handle_popup_done
|
|
|
|
};
|
|
|
|
|
2017-06-21 00:22:21 +02:00
|
|
|
struct wlr_output *wlr_wl_output_create(struct wlr_backend *_backend) {
|
|
|
|
assert(wlr_backend_is_wl(_backend));
|
2017-08-12 17:43:36 +02:00
|
|
|
struct wlr_wl_backend *backend = (struct wlr_wl_backend *)_backend;
|
2017-06-21 00:22:21 +02:00
|
|
|
if (!backend->remote_display) {
|
|
|
|
++backend->requested_outputs;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:36:40 +02:00
|
|
|
struct wlr_output_state *ostate;
|
|
|
|
if (!(ostate = calloc(sizeof(struct wlr_output_state), 1))) {
|
|
|
|
wlr_log(L_ERROR, "Failed to allocate wlr_output_state");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wlr_output *wlr_output = wlr_output_create(&output_impl, ostate);
|
|
|
|
if (!wlr_output) {
|
|
|
|
free(ostate);
|
|
|
|
wlr_log_errno(L_ERROR, "Allocation failed");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-06-20 23:51:45 +02:00
|
|
|
wlr_output->width = 640;
|
|
|
|
wlr_output->height = 480;
|
2017-06-19 22:36:40 +02:00
|
|
|
wlr_output->scale = 1;
|
2017-06-20 21:29:27 +02:00
|
|
|
strncpy(wlr_output->make, "wayland", sizeof(wlr_output->make));
|
|
|
|
strncpy(wlr_output->model, "wayland", sizeof(wlr_output->model));
|
2017-06-21 00:22:21 +02:00
|
|
|
snprintf(wlr_output->name, sizeof(wlr_output->name), "WL-%zd",
|
|
|
|
backend->outputs->length + 1);
|
2017-08-10 19:27:14 +02:00
|
|
|
wlr_output_update_matrix(wlr_output);
|
2017-06-19 22:36:40 +02:00
|
|
|
|
2017-06-20 18:27:05 +02:00
|
|
|
ostate->backend = backend;
|
2017-06-21 00:22:21 +02:00
|
|
|
ostate->wlr_output = wlr_output;
|
2017-06-20 18:27:05 +02:00
|
|
|
|
|
|
|
// TODO: error handling
|
2017-06-19 22:36:40 +02:00
|
|
|
ostate->surface = wl_compositor_create_surface(backend->compositor);
|
|
|
|
ostate->shell_surface = wl_shell_get_shell_surface(backend->shell, ostate->surface);
|
2017-06-20 18:27:05 +02:00
|
|
|
|
|
|
|
wl_shell_surface_set_class(ostate->shell_surface, "sway");
|
|
|
|
wl_shell_surface_set_title(ostate->shell_surface, "sway-wl");
|
|
|
|
wl_shell_surface_add_listener(ostate->shell_surface, &shell_surface_listener, ostate);
|
2017-06-20 18:44:11 +02:00
|
|
|
wl_shell_surface_set_toplevel(ostate->shell_surface);
|
2017-06-20 18:27:05 +02:00
|
|
|
|
2017-06-20 23:51:45 +02:00
|
|
|
ostate->egl_window = wl_egl_window_create(ostate->surface,
|
|
|
|
wlr_output->width, wlr_output->height);
|
2017-06-19 22:36:40 +02:00
|
|
|
ostate->egl_surface = wlr_egl_create_surface(&backend->egl, ostate->egl_window);
|
|
|
|
|
2017-06-20 23:51:45 +02:00
|
|
|
// start rendering loop per callbacks by rendering first frame
|
|
|
|
if (!eglMakeCurrent(ostate->backend->egl.display,
|
|
|
|
ostate->egl_surface, ostate->egl_surface,
|
|
|
|
ostate->backend->egl.context)) {
|
|
|
|
wlr_log(L_ERROR, "eglMakeCurrent failed: %s", egl_error());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
glViewport(0, 0, wlr_output->width, wlr_output->height);
|
|
|
|
glClearColor(1.0, 1.0, 1.0, 1.0);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
|
|
|
ostate->frame_callback = wl_surface_frame(ostate->surface);
|
|
|
|
wl_callback_add_listener(ostate->frame_callback, &frame_listener, ostate);
|
|
|
|
|
|
|
|
if (!eglSwapBuffers(ostate->backend->egl.display, ostate->egl_surface)) {
|
|
|
|
wlr_log(L_ERROR, "eglSwapBuffers failed: %s", egl_error());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-22 20:26:02 +02:00
|
|
|
wlr_output_create_global(wlr_output, backend->local_display);
|
2017-06-21 00:22:21 +02:00
|
|
|
list_add(backend->outputs, wlr_output);
|
2017-08-12 17:43:36 +02:00
|
|
|
wl_signal_emit(&backend->backend.events.output_add, wlr_output);
|
2017-06-19 22:36:40 +02:00
|
|
|
return wlr_output;
|
|
|
|
}
|