aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--backend/drm/drm.c52
-rw-r--r--example/CMakeLists.txt15
-rw-r--r--example/rotation.c356
-rw-r--r--example/simple.c (renamed from example/main.c)8
-rw-r--r--include/types.h5
-rw-r--r--include/wlr/types.h8
-rw-r--r--types/wlr_output.c85
7 files changed, 485 insertions, 44 deletions
diff --git a/backend/drm/drm.c b/backend/drm/drm.c
index d949cca8..90695175 100644
--- a/backend/drm/drm.c
+++ b/backend/drm/drm.c
@@ -178,6 +178,28 @@ static int find_id(const void *item, const void *cmp_to) {
}
}
+static void wlr_drm_output_enable(struct wlr_output_state *output, bool enable) {
+ struct wlr_backend_state *state =
+ wl_container_of(output->renderer, state, renderer);
+ if (output->state != DRM_OUTPUT_CONNECTED) {
+ return;
+ }
+
+ if (enable) {
+ drmModeConnectorSetProperty(state->fd, output->connector, output->props.dpms,
+ DRM_MODE_DPMS_ON);
+
+ // Start rendering loop again by drawing a black frame
+ wlr_drm_output_begin(output);
+ glClearColor(0.0, 0.0, 0.0, 1.0);
+ glClear(GL_COLOR_BUFFER_BIT);
+ wlr_drm_output_end(output);
+ } else {
+ drmModeConnectorSetProperty(state->fd, output->connector, output->props.dpms,
+ DRM_MODE_DPMS_STANDBY);
+ }
+}
+
static bool wlr_drm_output_set_mode(struct wlr_output_state *output,
struct wlr_output_mode *mode) {
struct wlr_backend_state *state =
@@ -233,8 +255,8 @@ static bool wlr_drm_output_set_mode(struct wlr_output_state *output,
}
output->state = DRM_OUTPUT_CONNECTED;
- output->width = mode->width;
- output->height = mode->height;
+ output->width = output->wlr_output->width = mode->width;
+ output->height = output->wlr_output->height = mode->height;
output->wlr_output->current_mode = mode;
if (!display_init_renderer(&state->renderer, output)) {
@@ -251,26 +273,9 @@ error:
return false;
}
-static void wlr_drm_output_enable(struct wlr_output_state *output, bool enable) {
- struct wlr_backend_state *state =
- wl_container_of(output->renderer, state, renderer);
- if (output->state != DRM_OUTPUT_CONNECTED) {
- return;
- }
-
- if (enable) {
- drmModeConnectorSetProperty(state->fd, output->connector, output->props.dpms,
- DRM_MODE_DPMS_ON);
-
- // Start rendering loop again by drawing a black frame
- wlr_drm_output_begin(output);
- glClearColor(0.0, 0.0, 0.0, 1.0);
- glClear(GL_COLOR_BUFFER_BIT);
- wlr_drm_output_end(output);
- } else {
- drmModeConnectorSetProperty(state->fd, output->connector, output->props.dpms,
- DRM_MODE_DPMS_STANDBY);
- }
+static void wlr_drm_output_transform(struct wlr_output_state *output,
+ enum wl_output_transform transform) {
+ output->wlr_output->transform = transform;
}
static void wlr_drm_output_destroy(struct wlr_output_state *output) {
@@ -279,8 +284,9 @@ static void wlr_drm_output_destroy(struct wlr_output_state *output) {
}
static struct wlr_output_impl output_impl = {
- .set_mode = wlr_drm_output_set_mode,
.enable = wlr_drm_output_enable,
+ .set_mode = wlr_drm_output_set_mode,
+ .transform = wlr_drm_output_transform,
.destroy = wlr_drm_output_destroy,
};
diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt
index f619b97f..ba96cc02 100644
--- a/example/CMakeLists.txt
+++ b/example/CMakeLists.txt
@@ -2,11 +2,20 @@ include_directories(
${DRM_INCLUDE_DIRS}
)
-add_executable(example
- main.c
+add_executable(simple
+ simple.c
)
-target_link_libraries(example
+target_link_libraries(simple
+ wlr-backend
+ wlr-session
+)
+
+add_executable(rotation
+ rotation.c
+)
+
+target_link_libraries(rotation
wlr-backend
wlr-session
)
diff --git a/example/rotation.c b/example/rotation.c
new file mode 100644
index 00000000..d19af224
--- /dev/null
+++ b/example/rotation.c
@@ -0,0 +1,356 @@
+#define _POSIX_C_SOURCE 199309L
+#define _XOPEN_SOURCE 500
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <string.h>
+#include <unistd.h>
+#include <wayland-server.h>
+#include <wayland-server-protocol.h>
+#include <GLES3/gl3.h>
+#include <wlr/backend.h>
+#include <wlr/session.h>
+#include <wlr/types.h>
+#include <math.h>
+
+static const GLchar vert_src[] =
+"#version 310 es\n"
+"precision mediump float;\n"
+"layout(location = 0) uniform mat4 transform;\n"
+"layout(location = 0) in vec2 pos;\n"
+"layout(location = 1) in float angle;\n"
+"out float v_angle;\n"
+"void main() {\n"
+" v_angle = angle;\n"
+" gl_Position = transform * vec4(pos, 0.0, 1.0);\n"
+"}\n";
+
+static const GLchar frag_src[] =
+"#version 310 es\n"
+"precision mediump float;\n"
+"in float v_angle;\n"
+"out vec4 f_color;\n"
+"void main() {\n"
+" f_color = vec4(\n"
+" cos(v_angle) / 2.0 + 0.5,\n"
+" cos(v_angle + 2.0944) / 2.0 + 0.5,\n"
+" cos(v_angle + 4.18879) / 2.0 + 0.5,\n"
+" 1.0);\n"
+"}\n";
+
+struct state {
+ float angle;
+ struct timespec last_frame;
+ struct wl_listener output_add;
+ struct wl_listener output_remove;
+ struct wl_list outputs;
+ struct wl_list config;
+
+ struct gl {
+ GLuint prog;
+ GLuint vao;
+ GLuint vbo;
+ } gl;
+};
+
+struct output_state {
+ struct wl_list link;
+ struct wlr_output *output;
+ struct state *state;
+ struct wl_listener frame;
+};
+
+struct output_config {
+ char *name;
+ enum wl_output_transform transform;
+
+ struct wl_list link;
+};
+
+static GLuint create_shader(GLenum type, const GLchar *src, GLint len) {
+ GLuint shader = glCreateShader(type);
+ glShaderSource(shader, 1, &src, &len);
+ glCompileShader(shader);
+
+ GLint success;
+ glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
+
+ if (success == GL_FALSE) {
+ GLint loglen;
+ glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &loglen);
+
+ GLchar msg[loglen];
+ glGetShaderInfoLog(shader, loglen, &loglen, msg);
+
+ fprintf(stderr, "Failed to create shader: %s\n", msg);
+ exit(1);
+ }
+
+ return shader;
+}
+
+static void init_gl(struct gl *gl) {
+ GLuint vert = create_shader(GL_VERTEX_SHADER, vert_src, strlen(vert_src));
+ GLuint frag = create_shader(GL_FRAGMENT_SHADER, frag_src, strlen(frag_src));
+
+ gl->prog = glCreateProgram();
+ glAttachShader(gl->prog, vert);
+ glAttachShader(gl->prog, frag);
+ glLinkProgram(gl->prog);
+
+ GLint success;
+ glGetProgramiv(gl->prog, GL_LINK_STATUS, &success);
+
+ if (success == GL_FALSE) {
+ GLint len;
+ glGetProgramiv(gl->prog, GL_INFO_LOG_LENGTH, &len);
+
+ GLchar msg[len];
+ glGetProgramInfoLog(gl->prog, len, &len, msg);
+
+ fprintf(stderr, "Failed to link program: %s\n", msg);
+ exit(1);
+ }
+
+ glDeleteProgram(vert);
+ glDeleteProgram(frag);
+
+ glGenVertexArrays(1, &gl->vao);
+ glGenBuffers(1, &gl->vbo);
+
+ glBindVertexArray(gl->vao);
+ glBindBuffer(GL_ARRAY_BUFFER, gl->vbo);
+
+ glEnableVertexAttribArray(0);
+ glEnableVertexAttribArray(1);
+ glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (void *)0);
+ glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (void *)(2 * sizeof(GLfloat)));
+}
+
+static void cleanup_gl(struct gl *gl) {
+ glDeleteProgram(gl->prog);
+ glDeleteVertexArrays(1, &gl->vao);
+ glDeleteBuffers(1, &gl->vbo);
+}
+
+static void output_frame(struct wl_listener *listener, void *data) {
+ struct output_state *ostate = wl_container_of(listener, ostate, frame);
+ struct state *s = ostate->state;
+
+ int32_t width = ostate->output->width;
+ int32_t height = ostate->output->height;
+ glViewport(0, 0, width, height);
+
+ // All of the odd numbered transformations involve a 90 or 270 degree rotation
+ if (ostate->output->transform % 2 == 1) {
+ float tmp = width;
+ width = height;
+ height = tmp;
+ }
+
+ int32_t mid_w = width / 2;
+ int32_t mid_h = height / 2;
+
+ GLfloat vert_data[] = {
+ mid_w - 200, mid_h - 200, s->angle,
+ mid_w, mid_h + 200, s->angle + M_PI * 2.0f / 3.0f, // 120 deg
+ mid_w + 200, mid_h - 200, s->angle + M_PI * 4.0f / 3.0f, // 240 deg
+ };
+
+ glUseProgram(s->gl.prog);
+
+ glBindVertexArray(s->gl.vao);
+ glBindBuffer(GL_ARRAY_BUFFER, s->gl.vbo);
+ glBufferData(GL_ARRAY_BUFFER, sizeof(vert_data), vert_data, GL_STATIC_DRAW);
+
+ glUniformMatrix4fv(0, 1, GL_TRUE, ostate->output->transform_matrix);
+
+ glClearColor(0.0, 0.0, 0.0, 1.0);
+ glClear(GL_COLOR_BUFFER_BIT);
+
+ glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
+
+ struct timespec now;
+ clock_gettime(CLOCK_MONOTONIC, &now);
+
+ long ms = (now.tv_sec - s->last_frame.tv_sec) * 1000 +
+ (now.tv_nsec - s->last_frame.tv_nsec) / 1000000;
+
+ s->last_frame = now;
+ s->angle += ms / 200.0f;
+ if (s->angle > 2 * M_PI) {
+ s->angle = 0.0f;
+ }
+}
+
+static void output_add(struct wl_listener *listener, void *data) {
+ struct wlr_output *output = data;
+ struct state *state = wl_container_of(listener, state, output_add);
+
+ fprintf(stderr, "Output '%s' added\n", output->name);
+ wlr_output_set_mode(output, output->modes->items[0]);
+
+ struct output_state *ostate = calloc(1, sizeof(struct output_state));
+
+ ostate->output = output;
+ ostate->state = state;
+ ostate->frame.notify = output_frame;
+
+ struct output_config *conf;
+ wl_list_for_each(conf, &state->config, link) {
+ if (strcmp(conf->name, output->name) == 0) {
+ wlr_output_transform(ostate->output, conf->transform);
+ break;
+ }
+ }
+
+ wl_list_init(&ostate->frame.link);
+ wl_signal_add(&output->events.frame, &ostate->frame);
+ wl_list_insert(&state->outputs, &ostate->link);
+}
+
+static void output_remove(struct wl_listener *listener, void *data) {
+ struct wlr_output *output = data;
+ struct state *state = wl_container_of(listener, state, output_remove);
+ struct output_state *ostate;
+
+ wl_list_for_each(ostate, &state->outputs, link) {
+ if (ostate->output == output) {
+ wl_list_remove(&ostate->link);
+ wl_list_remove(&ostate->frame.link);
+ free(ostate);
+ break;
+ }
+ }
+}
+
+static int timer_done(void *data) {
+ *(bool *)data = true;
+ return 1;
+}
+
+static void usage(const char *name, int ret) {
+ fprintf(stderr,
+ "usage: %s [-d <name> [-r <rotation> | -f]]*\n"
+ "\n"
+ " -o <output> The name of the DRM display. e.g. DVI-I-1.\n"
+ " -r <rotation> The rotation counter clockwise. Valid values are 90, 180, 270.\n"
+ " -f Flip the output along the vertical axis.\n", name);
+
+ exit(ret);
+}
+
+static void parse_args(int argc, char *argv[], struct wl_list *config) {
+ struct output_config *oc = NULL;
+
+ int c;
+ while ((c = getopt(argc, argv, "o:r:fh")) != -1) {
+ switch (c) {
+ case 'o':
+ oc = calloc(1, sizeof(*oc));
+ oc->name = optarg;
+ oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
+ wl_list_insert(config, &oc->link);
+ break;
+ case 'r':
+ if (!oc) {
+ fprintf(stderr, "You must specify an output first\n");
+ usage(argv[0], 1);
+ }
+
+ if (oc->transform != WL_OUTPUT_TRANSFORM_NORMAL
+ && oc->transform != WL_OUTPUT_TRANSFORM_FLIPPED) {
+ fprintf(stderr, "Rotation for %s already specified\n", oc->name);
+ usage(argv[0], 1);
+ }
+
+ if (strcmp(optarg, "90") == 0) {
+ oc->transform += WL_OUTPUT_TRANSFORM_90;
+ } else if (strcmp(optarg, "180") == 0) {
+ oc->transform += WL_OUTPUT_TRANSFORM_180;
+ } else if (strcmp(optarg, "270") == 0) {
+ oc->transform += WL_OUTPUT_TRANSFORM_270;
+ } else {
+ fprintf(stderr, "Invalid rotation '%s'\n", optarg);
+ usage(argv[0], 1);
+ }
+ break;
+ case 'f':
+ if (!oc) {
+ fprintf(stderr, "You must specify an output first\n");
+ usage(argv[0], 1);
+ }
+
+ if (oc->transform >= WL_OUTPUT_TRANSFORM_FLIPPED) {
+ fprintf(stderr, "Flip for %s already specified\n", oc->name);
+ usage(argv[0], 1);
+ }
+
+ oc->transform += WL_OUTPUT_TRANSFORM_FLIPPED;
+ break;
+ case 'h':
+ case '?':
+ usage(argv[0], c != 'h');
+ }
+ }
+}
+
+int main(int argc, char *argv[]) {
+ struct state state = {
+ .angle = 0.0,
+ .output_add = { .notify = output_add },
+ .output_remove = { .notify = output_remove }
+ };
+
+ wl_list_init(&state.outputs);
+ wl_list_init(&state.config);
+ wl_list_init(&state.output_add.link);
+ wl_list_init(&state.output_remove.link);
+ clock_gettime(CLOCK_MONOTONIC, &state.last_frame);
+
+ parse_args(argc, argv, &state.config);
+
+ struct wl_display *display = wl_display_create();
+ struct wl_event_loop *event_loop = wl_display_get_event_loop(display);
+
+ struct wlr_session *session = wlr_session_start(display);
+ if (!session) {
+ return 1;
+ }
+
+ struct wlr_backend *wlr = wlr_backend_autocreate(display, session);
+ if (!wlr) {
+ return 1;
+ }
+
+ wl_signal_add(&wlr->events.output_add, &state.output_add);
+ wl_signal_add(&wlr->events.output_remove, &state.output_remove);
+
+ if (!wlr_backend_init(wlr)) {
+ return 1;
+ }
+
+ init_gl(&state.gl);
+
+ bool done = false;
+ struct wl_event_source *timer = wl_event_loop_add_timer(event_loop,
+ timer_done, &done);
+
+ wl_event_source_timer_update(timer, 10000);
+
+ while (!done) {
+ wl_event_loop_dispatch(event_loop, 0);
+ }
+
+ cleanup_gl(&state.gl);
+
+ wl_event_source_remove(timer);
+ wlr_backend_destroy(wlr);
+ wlr_session_finish(session);
+ wl_display_destroy(display);
+
+ struct output_config *ptr, *tmp;
+ wl_list_for_each_safe(ptr, tmp, &state.config, link) {
+ free(ptr);
+ }
+}
diff --git a/example/main.c b/example/simple.c
index 87999e11..21f333c7 100644
--- a/example/main.c
+++ b/example/simple.c
@@ -110,14 +110,6 @@ int disable_outputs(void *data) {
}
int main() {
- if (getenv("DISPLAY")) {
- fprintf(stderr, "Detected that X is running. Run this in its own virtual terminal.\n");
- return 1;
- } else if (getenv("WAYLAND_DISPLAY")) {
- fprintf(stderr, "Detected that Wayland is running. Run this in its own virtual terminal.\n");
- return 1;
- }
-
struct state state = {
.color = { 1.0, 0.0, 0.0 },
.dec = 0,
diff --git a/include/types.h b/include/types.h
index 5513e936..d2c8d008 100644
--- a/include/types.h
+++ b/include/types.h
@@ -6,8 +6,11 @@
#include <stdbool.h>
struct wlr_output_impl {
- bool (*set_mode)(struct wlr_output_state *state, struct wlr_output_mode *mode);
void (*enable)(struct wlr_output_state *state, bool enable);
+ bool (*set_mode)(struct wlr_output_state *state,
+ struct wlr_output_mode *mode);
+ void (*transform)(struct wlr_output_state *state,
+ enum wl_output_transform transform);
void (*destroy)(struct wlr_output_state *state);
};
diff --git a/include/wlr/types.h b/include/wlr/types.h
index 02c2f087..a3afb258 100644
--- a/include/wlr/types.h
+++ b/include/wlr/types.h
@@ -26,10 +26,13 @@ struct wlr_output {
char make[48];
char model[16];
uint32_t scale;
+ int32_t width, height;
int32_t phys_width, phys_height; // mm
int32_t subpixel; // enum wl_output_subpixel
int32_t transform; // enum wl_output_transform
+ float transform_matrix[16];
+
list_t *modes;
struct wlr_output_mode *current_mode;
@@ -38,8 +41,11 @@ struct wlr_output {
} events;
};
-bool wlr_output_set_mode(struct wlr_output *output, struct wlr_output_mode *mode);
void wlr_output_enable(struct wlr_output *output, bool enable);
+bool wlr_output_set_mode(struct wlr_output *output,
+ struct wlr_output_mode *mode);
+void wlr_output_transform(struct wlr_output *output,
+ enum wl_output_transform transform);
void wlr_output_destroy(struct wlr_output *output);
#endif
diff --git a/types/wlr_output.c b/types/wlr_output.c
index a9f91c4d..128ed8e1 100644
--- a/types/wlr_output.c
+++ b/types/wlr_output.c
@@ -1,19 +1,96 @@
#include <stdlib.h>
+#include <string.h>
+#include <tgmath.h>
#include <wayland-server.h>
#include <wlr/types.h>
#include <wlr/common/list.h>
#include "types.h"
+static const float transforms[][4] = {
+ [WL_OUTPUT_TRANSFORM_NORMAL] = {
+ 1.0f, 0.0f,
+ 0.0f, 1.0f,
+ },
+ [WL_OUTPUT_TRANSFORM_90] = {
+ 0.0f, -1.0f,
+ 1.0f, 0.0f,
+ },
+ [WL_OUTPUT_TRANSFORM_180] = {
+ -1.0f, 0.0f,
+ 0.0f, -1.0f,
+ },
+ [WL_OUTPUT_TRANSFORM_270] = {
+ 0.0f, 1.0f,
+ -1.0f, 0.0f,
+ },
+ [WL_OUTPUT_TRANSFORM_FLIPPED] = {
+ -1.0f, 0.0f,
+ 0.0f, 1.0f,
+ },
+ [WL_OUTPUT_TRANSFORM_FLIPPED_90] = {
+ 0.0f, 1.0f,
+ 1.0f, 0.0f,
+ },
+ [WL_OUTPUT_TRANSFORM_FLIPPED_180] = {
+ 1.0f, 0.0f,
+ 0.0f, -1.0f,
+ },
+ [WL_OUTPUT_TRANSFORM_FLIPPED_270] = {
+ 0.0f, -1.0f,
+ -1.0f, 0.0f,
+ },
+};
+
+// Equivilent to glOrtho(0, width, 0, height, 1, -1) with the transform applied
+static void set_matrix(float mat[static 16], int32_t width, int32_t height,
+ enum wl_output_transform transform) {
+ memset(mat, 0, sizeof(*mat) * 16);
+
+ const float *t = transforms[transform];
+ float x = 2.0f / width;
+ float y = 2.0f / height;
+
+ // Rotation + relection
+ mat[0] = x * t[0];
+ mat[1] = x * t[1];
+ mat[4] = y * t[2];
+ mat[5] = y * t[3];
+
+ // Translation
+ mat[3] = -copysign(1.0f, mat[0] + mat[1]);
+ mat[7] = -copysign(1.0f, mat[4] + mat[5]);
+
+ // Identity
+ mat[10] = 1.0f;
+ mat[15] = 1.0f;
+}
+
struct wlr_output *wlr_output_create(struct wlr_output_impl *impl,
struct wlr_output_state *state) {
struct wlr_output *output = calloc(1, sizeof(struct wlr_output));
output->impl = impl;
output->state = state;
output->modes = list_create();
+ output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
wl_signal_init(&output->events.frame);
return output;
}
+void wlr_output_enable(struct wlr_output *output, bool enable) {
+ output->impl->enable(output->state, enable);
+}
+
+bool wlr_output_set_mode(struct wlr_output *output, struct wlr_output_mode *mode) {
+ set_matrix(output->transform_matrix, mode->width, mode->height, output->transform);
+ return output->impl->set_mode(output->state, mode);
+}
+
+void wlr_output_transform(struct wlr_output *output,
+ enum wl_output_transform transform) {
+ set_matrix(output->transform_matrix, output->width, output->height, transform);
+ output->impl->transform(output->state, transform);
+}
+
void wlr_output_destroy(struct wlr_output *output) {
if (!output) return;
output->impl->destroy(output->state);
@@ -23,11 +100,3 @@ void wlr_output_destroy(struct wlr_output *output) {
list_free(output->modes);
free(output);
}
-
-bool wlr_output_set_mode(struct wlr_output *output, struct wlr_output_mode *mode) {
- return output->impl->set_mode(output->state, mode);
-}
-
-void wlr_output_enable(struct wlr_output *output, bool enable) {
- output->impl->enable(output->state, enable);
-}