#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wayland-client.h>
#include <EGL/egl.h>
#include <GLES2/gl2.h>
#include "wayland-egl.h"
static const GLfloat vertices[] = {
-1, -1,
1, -1,
1, 1,
-1, 1,
};
static const GLfloat texCoords[] = {
0, 0,
1, 0,
1, 1,
0, 1,
};
// Vertex Shader
const char *vertex_shader_src =
"attribute vec2 position;\n"
"attribute vec2 texcoord;\n"
"varying vec2 v_texcoord;\n"
"void main() {\n"
" gl_Position = vec4(position, 0.0, 1.0);\n"
" v_texcoord = texcoord;\n"
"}\n";
// Fragment Shader
const char *fragment_shader_src =
"precision mediump float;\n"
"varying vec2 v_texcoord;\n"
"uniform sampler2D texture;\n"
"void main() {\n"
" gl_FragColor = texture2D(texture, v_texcoord);\n"
"}\n";
struct wayland_info {
struct wl_display *display;
struct wl_registry *registry;
struct wl_compositor *compositor;
struct wl_surface *surface;
struct wl_egl_window *egl_window;
struct wl_shell *shell;
struct wl_shell_surface *shell_surface;
};
struct egl_info {
EGLDisplay egl_display;
EGLConfig egl_config;
EGLContext egl_context;
EGLSurface egl_surface;
};
GLuint load_shader(GLenum type, const char *source) {
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);
GLint status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE) {
char buffer[512];
glGetShaderInfoLog(shader, 512, NULL, buffer);
fprintf(stderr, "Shader compile error: %s\n", buffer);
exit(EXIT_FAILURE);
}
return shader;
}
GLuint load_program(const char *vertex_shader_src, const char *fragment_shader_src) {
GLuint vertex_shader = load_shader(GL_VERTEX_SHADER, vertex_shader_src);
GLuint fragment_shader = load_shader(GL_FRAGMENT_SHADER, fragment_shader_src);
GLuint program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
GLint status;
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (status == GL_FALSE) {
char buffer[512];
glGetProgramInfoLog(program, 512, NULL, buffer);
fprintf(stderr, "Program link error: %s\n", buffer);
exit(EXIT_FAILURE);
}
return program;
}
void init_wayland(struct wayland_info *wayland) {
wayland->display = wl_display_connect(NULL);
if (!wayland->display) {
fprintf(stderr, "Cannot connect to display\n");
exit(EXIT_FAILURE);
}
wayland->registry = wl_display_get_registry(wayland->display);
wl_display_roundtrip(wayland->display);
}
void init_egl(struct wayland_info *wayland, struct egl_info *egl) {
egl->egl_display = eglGetDisplay((EGLNativeDisplayType)wayland->display);
eglInitialize(egl->egl_display, NULL, NULL);
eglBindAPI(EGL_OPENGL_ES_API);
EGLint config_attributes[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_DEPTH_SIZE, 8,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_NONE
};
int num_configs;
eglChooseConfig(egl->egl_display, config_attributes, &egl->egl_config, 1, &num_configs);
EGLint context_attributes[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
egl->egl_context = eglCreateContext(egl->egl_display, egl->egl_config, EGL_NO_CONTEXT, context_attributes);
wayland->egl_window = wl_egl_window_create(wayland->surface, 640, 480);
egl->egl_surface = eglCreateWindowSurface(egl->egl_display, egl->egl_config, (EGLNativeWindowType)wayland->egl_window, NULL);
eglMakeCurrent(egl->egl_display, egl->egl_surface, egl->egl_surface, egl->egl_context);
}
void init_gl(GLuint *program) {
*program = load_program(vertex_shader_src, fragment_shader_src);
glUseProgram(*program);
}
void render(GLuint program) {
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
// Load and generate the texture (this should be replaced with actually loading image data)
unsigned char texData[] = {255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 255, 255, 255, 0, 255}; // Dummy checkerboard
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, texData);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
GLuint position = glGetAttribLocation(program, "position");
GLuint texcoord = glGetAttribLocation(program, "texcoord");
glEnableVertexAttribArray(position);
glEnableVertexAttribArray(texcoord);
glVertexAttribPointer(position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glVertexAttribPointer(texcoord, 2, GL_FLOAT, GL_FALSE, 0, texCoords);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
eglSwapBuffers(eglGetCurrentDisplay(), eglGetCurrentSurface(EGL_DRAW));
}
void cleanup(struct wayland_info *wayland, struct egl_info *egl) {
wl_egl_window_destroy(wayland->egl_window);
eglDestroySurface(egl->egl_display, egl->egl_surface);
eglDestroyContext(egl->egl_display, egl->egl_context);
eglTerminate(egl->egl_display);
wl_display_disconnect(wayland->display);
}
int main(int argc, char **argv) {
struct wayland_info wayland = {0};
struct egl_info egl = {0};
GLuint program;
init_wayland(&wayland);
init_egl(&wayland, &egl);
init_gl(&program);
while (1) {
wl_display_dispatch_pending(wayland.display);
render(program);
}
cleanup(&wayland, &egl);
return 0;
}