Move clients to subdirectory

This commit is contained in:
Kristian Høgsberg 2010-06-04 21:46:50 -04:00
parent c9e208899b
commit b2a432ef18
12 changed files with 108 additions and 77 deletions

27
clients/Makefile Normal file
View file

@ -0,0 +1,27 @@
include ../config.mk
egl_clients = gears
cairo_clients = flower screenshot terminal image view
all : $(egl_clients) $(cairo_clients)
clean :
rm -f $(egl_clients) $(cairo_clients) *.o
flower : flower.o wayland-glib.o
gears : gears.o window.o wayland-glib.o cairo-util.o
screenshot : screenshot.o wayland-glib.o
terminal : terminal.o window.o wayland-glib.o cairo-util.o
image : image.o window.o wayland-glib.o cairo-util.o
view : view.o window.o wayland-glib.o cairo-util.o
terminal : LDLIBS += -lutil
image : CFLAGS += $(GDK_PIXBUF_CFLAGS)
image : LDLIBS += $(GDK_PIXBUF_LIBS)
view : CFLAGS += $(POPPLER_CFLAGS)
view : LDLIBS += $(POPPLER_LIBS)
$(egl_clients) : CFLAGS += $(EGL_CLIENT_CFLAGS)
$(egl_clients) : LDLIBS += -L.. -lwayland $(EGL_CLIENT_LIBS) -lrt
$(cairo_clients) : CFLAGS += $(CAIRO_CLIENT_CFLAGS)
$(cairo_clients) : LDLIBS += -L.. -lwayland $(CAIRO_CLIENT_LIBS) -lrt

195
clients/flower.c Normal file
View file

@ -0,0 +1,195 @@
/*
* Copyright © 2008 Kristian Høgsberg
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting documentation, and
* that the name of the copyright holders not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. The copyright holders make no representations
* about the suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include <cairo.h>
#include <glib.h>
#include <cairo-drm.h>
#include "wayland-client.h"
#include "wayland-glib.h"
static const char gem_device[] = "/dev/dri/card0";
static const char socket_name[] = "\0wayland";
static void
set_random_color(cairo_t *cr)
{
cairo_set_source_rgba(cr,
0.5 + (random() % 50) / 49.0,
0.5 + (random() % 50) / 49.0,
0.5 + (random() % 50) / 49.0,
0.5 + (random() % 100) / 99.0);
}
static void
draw_stuff(cairo_surface_t *surface, int width, int height)
{
const int petal_count = 3 + random() % 5;
const double r1 = 60 + random() % 35;
const double r2 = 20 + random() % 40;
const double u = (10 + random() % 90) / 100.0;
const double v = (random() % 90) / 100.0;
cairo_t *cr;
int i;
double t, dt = 2 * M_PI / (petal_count * 2);
double x1, y1, x2, y2, x3, y3;
cr = cairo_create(surface);
cairo_translate(cr, width / 2, height / 2);
cairo_move_to(cr, cos(0) * r1, sin(0) * r1);
for (t = 0, i = 0; i < petal_count; i++, t += dt * 2) {
x1 = cos(t) * r1;
y1 = sin(t) * r1;
x2 = cos(t + dt) * r2;
y2 = sin(t + dt) * r2;
x3 = cos(t + 2 * dt) * r1;
y3 = sin(t + 2 * dt) * r1;
cairo_curve_to(cr,
x1 - y1 * u, y1 + x1 * u,
x2 + y2 * v, y2 - x2 * v,
x2, y2);
cairo_curve_to(cr,
x2 - y2 * v, y2 + x2 * v,
x3 + y3 * u, y3 - x3 * u,
x3, y3);
}
cairo_close_path(cr);
set_random_color(cr);
cairo_fill_preserve(cr);
set_random_color(cr);
cairo_stroke(cr);
cairo_destroy(cr);
}
struct flower {
struct wl_compositor *compositor;
struct wl_surface *surface;
int x, y, width, height;
int offset;
};
static void
handle_acknowledge(void *data,
struct wl_compositor *compositor,
uint32_t key, uint32_t frame)
{
}
static void
handle_frame(void *data,
struct wl_compositor *compositor,
uint32_t frame, uint32_t timestamp)
{
struct flower *flower = data;
wl_surface_map(flower->surface,
flower->x + cos((flower->offset + timestamp) / 400.0) * 400 - flower->width / 2,
flower->y + sin((flower->offset + timestamp) / 320.0) * 300 - flower->height / 2,
flower->width, flower->height);
wl_compositor_commit(flower->compositor, 0);
}
static const struct wl_compositor_listener compositor_listener = {
handle_acknowledge,
handle_frame,
};
int main(int argc, char *argv[])
{
struct wl_display *display;
struct wl_visual *visual;
int fd;
cairo_device_t *device;
cairo_surface_t *s;
struct timespec ts;
GMainLoop *loop;
GSource *source;
struct flower flower;
fd = open(gem_device, O_RDWR);
if (fd < 0) {
fprintf(stderr, "drm open failed: %m\n");
return -1;
}
loop = g_main_loop_new(NULL, FALSE);
display = wl_display_create(socket_name, sizeof socket_name);
if (display == NULL) {
fprintf(stderr, "failed to create display: %m\n");
return -1;
}
source = wl_glib_source_new(display);
g_source_attach(source, NULL);
/* Process connection events. */
wl_display_iterate(display, WL_DISPLAY_READABLE);
flower.compositor = wl_display_get_compositor(display);
flower.x = 512;
flower.y = 384;
flower.width = 200;
flower.height = 200;
flower.surface = wl_compositor_create_surface(flower.compositor);
clock_gettime(CLOCK_MONOTONIC, &ts);
srandom(ts.tv_nsec);
flower.offset = random();
device = cairo_drm_device_get_for_fd(fd);
s = cairo_drm_surface_create(device,
CAIRO_CONTENT_COLOR_ALPHA,
flower.width, flower.height);
draw_stuff(s, flower.width, flower.height);
visual = wl_display_get_premultiplied_argb_visual(display);
wl_surface_attach(flower.surface,
cairo_drm_surface_get_name(s),
flower.width, flower.height,
cairo_drm_surface_get_stride(s),
visual);
wl_compositor_add_listener(flower.compositor,
&compositor_listener, &flower);
wl_compositor_commit(flower.compositor, 0);
g_main_loop_run(loop);
return 0;
}

480
clients/gears.c Normal file
View file

@ -0,0 +1,480 @@
/*
* Copyright © 2008 Kristian Høgsberg
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting documentation, and
* that the name of the copyright holders not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. The copyright holders make no representations
* about the suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include <cairo.h>
#include <glib.h>
#include <cairo-drm.h>
#define GL_GLEXT_PROTOTYPES
#define EGL_EGLEXT_PROTOTYPES
#include <GL/gl.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include "wayland-util.h"
#include "wayland-client.h"
#include "wayland-glib.h"
#include "window.h"
static const char gem_device[] = "/dev/dri/card0";
static const char socket_name[] = "\0wayland";
struct gears {
struct window *window;
struct display *d;
struct wl_compositor *compositor;
struct rectangle rectangle;
EGLDisplay display;
EGLContext context;
EGLImageKHR image;
int drm_fd;
int resized;
GLfloat angle;
cairo_surface_t *cairo_surface;
GLint gear_list[3];
GLuint fbo, color_rbo, depth_rbo;
};
struct gear_template {
GLfloat material[4];
GLfloat inner_radius;
GLfloat outer_radius;
GLfloat width;
GLint teeth;
GLfloat tooth_depth;
};
const static struct gear_template gear_templates[] = {
{ { 0.8, 0.1, 0.0, 1.0 }, 1.0, 4.0, 1.0, 20, 0.7 },
{ { 0.0, 0.8, 0.2, 1.0 }, 0.5, 2.0, 2.0, 10, 0.7 },
{ { 0.2, 0.2, 1.0, 1.0 }, 1.3, 2.0, 0.5, 10, 0.7 },
};
static GLfloat light_pos[4] = {5.0, 5.0, 10.0, 0.0};
static void die(const char *msg)
{
fprintf(stderr, "%s", msg);
exit(EXIT_FAILURE);
}
static void
make_gear(const struct gear_template *t)
{
GLint i;
GLfloat r0, r1, r2;
GLfloat angle, da;
GLfloat u, v, len;
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, t->material);
r0 = t->inner_radius;
r1 = t->outer_radius - t->tooth_depth / 2.0;
r2 = t->outer_radius + t->tooth_depth / 2.0;
da = 2.0 * M_PI / t->teeth / 4.0;
glShadeModel(GL_FLAT);
glNormal3f(0.0, 0.0, 1.0);
/* draw front face */
glBegin(GL_QUAD_STRIP);
for (i = 0; i <= t->teeth; i++) {
angle = i * 2.0 * M_PI / t->teeth;
glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
if (i < t->teeth) {
glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
}
}
glEnd();
/* draw front sides of teeth */
glBegin(GL_QUADS);
da = 2.0 * M_PI / t->teeth / 4.0;
for (i = 0; i < t->teeth; i++) {
angle = i * 2.0 * M_PI / t->teeth;
glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), t->width * 0.5);
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), t->width * 0.5);
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
}
glEnd();
glNormal3f(0.0, 0.0, -1.0);
/* draw back face */
glBegin(GL_QUAD_STRIP);
for (i = 0; i <= t->teeth; i++) {
angle = i * 2.0 * M_PI / t->teeth;
glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
if (i < t->teeth) {
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
}
}
glEnd();
/* draw back sides of teeth */
glBegin(GL_QUADS);
da = 2.0 * M_PI / t->teeth / 4.0;
for (i = 0; i < t->teeth; i++) {
angle = i * 2.0 * M_PI / t->teeth;
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -t->width * 0.5);
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -t->width * 0.5);
glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
}
glEnd();
/* draw outward faces of teeth */
glBegin(GL_QUAD_STRIP);
for (i = 0; i < t->teeth; i++) {
angle = i * 2.0 * M_PI / t->teeth;
glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
u = r2 * cos(angle + da) - r1 * cos(angle);
v = r2 * sin(angle + da) - r1 * sin(angle);
len = sqrt(u * u + v * v);
u /= len;
v /= len;
glNormal3f(v, -u, 0.0);
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), t->width * 0.5);
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -t->width * 0.5);
glNormal3f(cos(angle), sin(angle), 0.0);
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), t->width * 0.5);
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -t->width * 0.5);
u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
glNormal3f(v, -u, 0.0);
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
glNormal3f(cos(angle), sin(angle), 0.0);
}
glVertex3f(r1 * cos(0), r1 * sin(0), t->width * 0.5);
glVertex3f(r1 * cos(0), r1 * sin(0), -t->width * 0.5);
glEnd();
glShadeModel(GL_SMOOTH);
/* draw inside radius cylinder */
glBegin(GL_QUAD_STRIP);
for (i = 0; i <= t->teeth; i++) {
angle = i * 2.0 * M_PI / t->teeth;
glNormal3f(-cos(angle), -sin(angle), 0.0);
glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
}
glEnd();
}
static void
draw_gears(struct gears *gears)
{
GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.0, 0.0, -50);
glRotatef(view_rotx, 1.0, 0.0, 0.0);
glRotatef(view_roty, 0.0, 1.0, 0.0);
glRotatef(view_rotz, 0.0, 0.0, 1.0);
glPushMatrix();
glTranslatef(-3.0, -2.0, 0.0);
glRotatef(gears->angle, 0.0, 0.0, 1.0);
glCallList(gears->gear_list[0]);
glPopMatrix();
glPushMatrix();
glTranslatef(3.1, -2.0, 0.0);
glRotatef(-2.0 * gears->angle - 9.0, 0.0, 0.0, 1.0);
glCallList(gears->gear_list[1]);
glPopMatrix();
glPushMatrix();
glTranslatef(-3.1, 4.2, 0.0);
glRotatef(-2.0 * gears->angle - 25.0, 0.0, 0.0, 1.0);
glCallList(gears->gear_list[2]);
glPopMatrix();
glPopMatrix();
glFlush();
}
static void
resize_window(struct gears *gears)
{
EGLint attribs[] = {
EGL_IMAGE_WIDTH_INTEL, 0,
EGL_IMAGE_HEIGHT_INTEL, 0,
EGL_IMAGE_FORMAT_INTEL, EGL_FORMAT_RGBA_8888_KHR,
EGL_IMAGE_USE_INTEL, EGL_IMAGE_USE_SHARE_INTEL |
EGL_IMAGE_USE_SCANOUT_INTEL,
EGL_NONE
};
/* Constrain child size to be square and at least 300x300 */
window_get_child_rectangle(gears->window, &gears->rectangle);
if (gears->rectangle.width > gears->rectangle.height)
gears->rectangle.height = gears->rectangle.width;
else
gears->rectangle.width = gears->rectangle.height;
if (gears->rectangle.width < 300) {
gears->rectangle.width = 300;
gears->rectangle.height = 300;
}
window_set_child_size(gears->window, &gears->rectangle);
window_draw(gears->window);
if (gears->image)
eglDestroyImageKHR(gears->display, gears->image);
attribs[1] = gears->rectangle.width;
attribs[3] = gears->rectangle.height;
gears->image = eglCreateImageKHR(gears->display, gears->context,
EGL_SYSTEM_IMAGE_INTEL,
NULL, attribs);
glBindRenderbuffer(GL_RENDERBUFFER_EXT, gears->color_rbo);
glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER, gears->image);
glBindRenderbuffer(GL_RENDERBUFFER_EXT, gears->depth_rbo);
glRenderbufferStorage(GL_RENDERBUFFER_EXT,
GL_DEPTH_COMPONENT,
gears->rectangle.width,
gears->rectangle.height);
glViewport(0, 0, gears->rectangle.width, gears->rectangle.height);
gears->resized = 0;
}
static void
resize_handler(struct window *window, void *data)
{
struct gears *gears = data;
/* Right now, resizing the window from the per-frame callback
* is fine, since the window drawing code is so slow that we
* can't draw more than one window per frame anyway. However,
* once we implement faster resizing, this will show lag
* between pointer motion and window size even if resizing is
* fast. We need to keep processing motion events and posting
* new frames as fast as possible so when the server
* composites the next frame it will have the most recent size
* possible, like what we do for window moves. */
gears->resized = 1;
}
static void
keyboard_focus_handler(struct window *window,
struct wl_input_device *device, void *data)
{
struct gears *gears = data;
gears->resized = 1;
}
static void
handle_acknowledge(void *data,
struct wl_compositor *compositor,
uint32_t key, uint32_t frame)
{
struct gears *gears = data;
if (key == 10) {
if (gears->resized)
resize_window(gears);
draw_gears(gears);
}
}
static void
handle_frame(void *data,
struct wl_compositor *compositor,
uint32_t frame, uint32_t timestamp)
{
struct gears *gears = data;
EGLint name, handle, stride;
eglShareImageINTEL(gears->display, gears->context,
gears->image, 0, &name, &handle, &stride);
window_copy(gears->window, &gears->rectangle, name, stride);
window_commit(gears->window, 10);
gears->angle = (GLfloat) (timestamp % 8192) * 360 / 8192.0;
}
static const struct wl_compositor_listener compositor_listener = {
handle_acknowledge,
handle_frame,
};
static struct gears *
gears_create(struct display *display, int drm_fd)
{
const int x = 200, y = 200, width = 450, height = 500;
EGLint major, minor;
EGLDisplayTypeDRMMESA drm_display;
struct gears *gears;
int i;
gears = malloc(sizeof *gears);
memset(gears, 0, sizeof *gears);
gears->d = display;
gears->window = window_create(display, "Wayland Gears",
x, y, width, height);
drm_display.type = EGL_DISPLAY_TYPE_DRM_MESA;
drm_display.device = NULL;
drm_display.fd = drm_fd;
gears->display = eglGetDisplay((EGLNativeDisplayType) &drm_display);
if (gears->display == NULL)
die("failed to create egl display\n");
if (!eglInitialize(gears->display, &major, &minor))
die("failed to initialize display\n");
gears->context = eglCreateContext(gears->display, NULL, NULL, NULL);
if (gears->context == NULL)
die("failed to create context\n");
if (!eglMakeCurrent(gears->display, NULL, NULL, gears->context))
die("faile to make context current\n");
glGenFramebuffers(1, &gears->fbo);
glBindFramebuffer(GL_FRAMEBUFFER_EXT, gears->fbo);
glGenRenderbuffers(1, &gears->color_rbo);
glBindRenderbuffer(GL_RENDERBUFFER_EXT, gears->color_rbo);
glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER_EXT,
GL_COLOR_ATTACHMENT0_EXT,
GL_RENDERBUFFER_EXT,
gears->color_rbo);
glGenRenderbuffers(1, &gears->depth_rbo);
glBindRenderbuffer(GL_RENDERBUFFER_EXT, gears->depth_rbo);
glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER_EXT,
GL_DEPTH_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT,
gears->depth_rbo);
for (i = 0; i < 3; i++) {
gears->gear_list[i] = glGenLists(1);
glNewList(gears->gear_list[i], GL_COMPILE);
make_gear(&gear_templates[i]);
glEndList();
}
glEnable(GL_NORMALIZE);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 200.0);
glMatrixMode(GL_MODELVIEW);
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glClearColor(0, 0, 0, 0.92);
if (glCheckFramebufferStatus (GL_FRAMEBUFFER_EXT) != GL_FRAMEBUFFER_COMPLETE)
fprintf(stderr, "framebuffer incomplete\n");
gears->compositor = display_get_compositor(display);
resize_window(gears);
draw_gears(gears);
handle_frame(gears, gears->compositor, 0, 0);
window_set_resize_handler(gears->window, resize_handler, gears);
window_set_keyboard_focus_handler(gears->window, keyboard_focus_handler, gears);
wl_compositor_add_listener(gears->compositor,
&compositor_listener, gears);
return gears;
}
int main(int argc, char *argv[])
{
struct wl_display *display;
struct display *d;
int fd;
GMainLoop *loop;
GSource *source;
struct gears *gears;
fd = open(gem_device, O_RDWR);
if (fd < 0) {
fprintf(stderr, "drm open failed: %m\n");
return -1;
}
display = wl_display_create(socket_name, sizeof socket_name);
if (display == NULL) {
fprintf(stderr, "failed to create display: %m\n");
return -1;
}
d = display_create(display, fd);
loop = g_main_loop_new(NULL, FALSE);
source = wl_glib_source_new(display);
g_source_attach(source, NULL);
gears = gears_create(d, fd);
g_main_loop_run(loop);
return 0;
}

346
clients/image.c Normal file
View file

@ -0,0 +1,346 @@
/*
* Copyright © 2008 Kristian Høgsberg
* Copyright © 2009 Chris Wilson
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting documentation, and
* that the name of the copyright holders not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. The copyright holders make no representations
* about the suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include <cairo.h>
#include <cairo-drm.h>
#include <glib.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include "wayland-client.h"
#include "wayland-glib.h"
#include "window.h"
static const char gem_device[] = "/dev/dri/card0";
static const char socket_name[] = "\0wayland";
struct image {
struct window *window;
struct display *display;
struct wl_compositor *compositor;
uint32_t key;
gboolean redraw_scheduled;
gboolean redraw_pending;
cairo_surface_t *surface;
gchar *filename;
};
static void
set_source_pixbuf(cairo_t *cr,
const GdkPixbuf *pixbuf,
double src_x,
double src_y,
double src_width,
double src_height)
{
gint width = gdk_pixbuf_get_width (pixbuf);
gint height = gdk_pixbuf_get_height (pixbuf);
guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
int cairo_stride;
guchar *cairo_pixels;
cairo_format_t format;
cairo_surface_t *surface;
int j;
if (n_channels == 3)
format = CAIRO_FORMAT_RGB24;
else
format = CAIRO_FORMAT_ARGB32;
surface = cairo_image_surface_create(format, width, height);
if (cairo_surface_status(surface)) {
cairo_set_source_surface(cr, surface, 0, 0);
return;
}
cairo_stride = cairo_image_surface_get_stride(surface);
cairo_pixels = cairo_image_surface_get_data(surface);
for (j = height; j; j--) {
guchar *p = gdk_pixels;
guchar *q = cairo_pixels;
if (n_channels == 3) {
guchar *end = p + 3 * width;
while (p < end) {
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
q[0] = p[2];
q[1] = p[1];
q[2] = p[0];
#else
q[1] = p[0];
q[2] = p[1];
q[3] = p[2];
#endif
p += 3;
q += 4;
}
} else {
guchar *end = p + 4 * width;
guint t1,t2,t3;
#define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } G_STMT_END
while (p < end) {
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
MULT(q[0], p[2], p[3], t1);
MULT(q[1], p[1], p[3], t2);
MULT(q[2], p[0], p[3], t3);
q[3] = p[3];
#else
q[0] = p[3];
MULT(q[1], p[0], p[3], t1);
MULT(q[2], p[1], p[3], t2);
MULT(q[3], p[2], p[3], t3);
#endif
p += 4;
q += 4;
}
#undef MULT
}
gdk_pixels += gdk_rowstride;
cairo_pixels += cairo_stride;
}
cairo_surface_mark_dirty(surface);
cairo_set_source_surface(cr, surface,
src_x + .5 * (src_width - width),
src_y + .5 * (src_height - height));
cairo_surface_destroy(surface);
}
static void
image_draw(struct image *image)
{
struct rectangle rectangle;
GdkPixbuf *pb;
cairo_t *cr;
image->redraw_pending = 0;
window_draw(image->window);
window_get_child_rectangle(image->window, &rectangle);
pb = gdk_pixbuf_new_from_file_at_size(image->filename,
rectangle.width,
rectangle.height,
NULL);
if (pb == NULL)
return;
image->surface =
window_create_surface(image->window, &rectangle);
cr = cairo_create(image->surface);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_set_source_rgba(cr, 0, 0, 0, 1);
cairo_paint(cr);
set_source_pixbuf(cr, pb,
0, 0,
rectangle.width, rectangle.height);
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
cairo_paint(cr);
cairo_destroy(cr);
g_object_unref(pb);
window_copy_surface(image->window,
&rectangle,
image->surface);
window_commit(image->window, image->key);
}
static gboolean
image_idle_redraw(void *data)
{
struct image *image = data;
image_draw(image);
return FALSE;
}
static void
image_schedule_redraw(struct image *image)
{
if (!image->redraw_scheduled) {
image->redraw_scheduled = 1;
g_idle_add(image_idle_redraw, image);
} else {
image->redraw_pending = 1;
}
}
static void
resize_handler(struct window *window, void *data)
{
struct image *image = data;
image_schedule_redraw(image);
}
static void
keyboard_focus_handler(struct window *window,
struct wl_input_device *device, void *data)
{
struct image *image = data;
image_schedule_redraw(image);
}
static void
handle_acknowledge(void *data,
struct wl_compositor *compositor,
uint32_t key, uint32_t frame)
{
struct image *image = data;
if (image->key != key)
return;
cairo_surface_destroy(image->surface);
image->redraw_scheduled = 0;
if (image->redraw_pending) {
image->redraw_pending = 0;
image_schedule_redraw(image);
}
}
static void
handle_frame(void *data,
struct wl_compositor *compositor,
uint32_t frame, uint32_t timestamp)
{
}
static const struct wl_compositor_listener compositor_listener = {
handle_acknowledge,
handle_frame,
};
static struct image *
image_create(struct display *display, uint32_t key, const char *filename)
{
struct image *image;
gchar *basename;
gchar *title;
image = malloc(sizeof *image);
if (image == NULL)
return image;
memset(image, 0, sizeof *image);
basename = g_path_get_basename(filename);
title = g_strdup_printf("Wayland Image - %s", basename);
g_free(basename);
image->filename = g_strdup(filename);
image->window = window_create(display, title, 100 * key, 100 * key, 500, 400);
image->display = display;
/* FIXME: Window uses key 1 for moves, need some kind of
* allocation scheme here. Or maybe just a real toolkit. */
image->key = key + 100;
image->redraw_scheduled = 1;
image->compositor = display_get_compositor(display);
window_set_resize_handler(image->window, resize_handler, image);
window_set_keyboard_focus_handler(image->window, keyboard_focus_handler, image);
wl_compositor_add_listener(image->compositor, &compositor_listener, image);
image_draw(image);
return image;
}
static const GOptionEntry option_entries[] = {
{ NULL }
};
int
main(int argc, char *argv[])
{
struct wl_display *display;
int fd;
GMainLoop *loop;
GSource *source;
struct display *d;
GOptionContext *context;
GError *error;
int i;
context = g_option_context_new(NULL);
g_option_context_add_main_entries(context, option_entries, "Wayland Image");
if (!g_option_context_parse(context, &argc, &argv, &error)) {
fprintf(stderr, "option parsing failed: %s\n", error->message);
exit(EXIT_FAILURE);
}
fd = open(gem_device, O_RDWR);
if (fd < 0) {
fprintf(stderr, "drm open failed: %m\n");
return -1;
}
display = wl_display_create(socket_name, sizeof socket_name);
if (display == NULL) {
fprintf(stderr, "failed to create display: %m\n");
return -1;
}
d = display_create(display, fd);
loop = g_main_loop_new(NULL, FALSE);
source = wl_glib_source_new(display);
g_source_attach(source, NULL);
g_type_init ();
for (i = 1; i < argc; i++) {
struct image *image;
image = image_create (d, i, argv[i]);
}
g_main_loop_run(loop);
return 0;
}

107
clients/screenshot.c Normal file
View file

@ -0,0 +1,107 @@
/*
* Copyright © 2008 Kristian Høgsberg
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting documentation, and
* that the name of the copyright holders not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. The copyright holders make no representations
* about the suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <glib.h>
#include "wayland-client.h"
#include "wayland-glib.h"
/* The screenshooter is a good example of a custom object exposed by
* the compositor and serves as a test bed for implementing client
* side marshalling outside libwayland.so */
static const char socket_name[] = "\0wayland";
struct screenshooter {
uint32_t id;
struct wl_display *display;
};
static struct screenshooter *
screenshooter_create(struct wl_display *display)
{
struct screenshooter *screenshooter;
uint32_t id;
id = wl_display_get_object_id(display, "screenshooter", 1);
if (id == 0) {
fprintf(stderr, "server doesn't support screenshooter interface\n");
return NULL;
}
screenshooter = malloc(sizeof screenshooter);
if (screenshooter == NULL)
return NULL;
screenshooter->id = id;
screenshooter->display = display;
return screenshooter;
}
#define SCREENSHOOTER_SHOOT 0
static void
screenshooter_shoot(struct screenshooter *screenshooter)
{
uint32_t request[2];
request[0] = screenshooter->id;
request[1] = SCREENSHOOTER_SHOOT | ((sizeof request) << 16);
wl_display_write(screenshooter->display,
request, sizeof request);
}
int main(int argc, char *argv[])
{
struct wl_display *display;
GMainLoop *loop;
GSource *source;
struct screenshooter *s;
display = wl_display_create(socket_name, sizeof socket_name);
if (display == NULL) {
fprintf(stderr, "failed to create display: %m\n");
return -1;
}
wl_display_iterate(display, WL_DISPLAY_READABLE);
loop = g_main_loop_new(NULL, FALSE);
source = wl_glib_source_new(display);
g_source_attach(source, NULL);
s = screenshooter_create(display);
if (s == NULL)
exit(-1);
screenshooter_shoot(s);
g_idle_add((GSourceFunc) g_main_loop_quit, loop);
g_main_loop_run(loop);
return 0;
}

607
clients/terminal.c Normal file
View file

@ -0,0 +1,607 @@
/*
* Copyright © 2008 Kristian Høgsberg
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting documentation, and
* that the name of the copyright holders not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. The copyright holders make no representations
* about the suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include <pty.h>
#include <ctype.h>
#include <cairo.h>
#include <glib.h>
#include <linux/input.h>
#include <cairo-drm.h>
#include "wayland-util.h"
#include "wayland-client.h"
#include "wayland-glib.h"
#include "window.h"
static int option_fullscreen;
static const char gem_device[] = "/dev/dri/card0";
static const char socket_name[] = "\0wayland";
#define MOD_SHIFT 0x01
#define MOD_ALT 0x02
#define MOD_CTRL 0x04
struct terminal {
struct window *window;
struct display *display;
struct wl_compositor *compositor;
int redraw_scheduled, redraw_pending;
char *data;
int width, height, start, row, column;
int fd, master;
GIOChannel *channel;
uint32_t modifiers;
char escape[64];
int escape_length;
int state;
int margin;
int fullscreen;
int focused;
struct color_scheme *color_scheme;
};
static char *
terminal_get_row(struct terminal *terminal, int row)
{
int index;
index = (row + terminal->start) % terminal->height;
return &terminal->data[index * (terminal->width + 1)];
}
static void
terminal_resize(struct terminal *terminal, int width, int height)
{
size_t size;
char *data;
int i, l, total_rows, start;
if (terminal->width == width && terminal->height == height)
return;
size = (width + 1) * height;
data = malloc(size);
memset(data, 0, size);
if (terminal->data) {
if (width > terminal->width)
l = terminal->width;
else
l = width;
if (terminal->height > height) {
total_rows = height;
start = terminal->height - height;
} else {
total_rows = terminal->height;
start = 0;
}
for (i = 0; i < total_rows; i++)
memcpy(data + (width + 1) * i,
terminal_get_row(terminal, i), l);
free(terminal->data);
}
terminal->width = width;
terminal->height = height;
terminal->data = data;
if (terminal->row >= terminal->height)
terminal->row = terminal->height - 1;
if (terminal->column >= terminal->width)
terminal->column = terminal->width - 1;
terminal->start = 0;
}
struct color_scheme { struct { double r, g, b, a; } fg, bg; }
matrix_colors = { { 0, 0.7, 0, 1 }, { 0, 0, 0, 0.9 } },
jbarnes_colors = { { 1, 1, 1, 1 }, { 0, 0, 0, 1 } };
static void
terminal_draw_contents(struct terminal *terminal)
{
struct rectangle rectangle;
cairo_t *cr;
cairo_font_extents_t extents;
int i, top_margin, side_margin;
cairo_surface_t *surface;
double d;
window_get_child_rectangle(terminal->window, &rectangle);
surface = window_create_surface(terminal->window, &rectangle);
cr = cairo_create(surface);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_set_source_rgba(cr,
terminal->color_scheme->bg.r,
terminal->color_scheme->bg.g,
terminal->color_scheme->bg.b,
terminal->color_scheme->bg.a);
cairo_paint(cr);
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
cairo_set_source_rgba(cr,
terminal->color_scheme->fg.r,
terminal->color_scheme->fg.g,
terminal->color_scheme->fg.b,
terminal->color_scheme->fg.a);
cairo_select_font_face (cr, "mono",
CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size(cr, 14);
cairo_font_extents(cr, &extents);
side_margin = (rectangle.width - terminal->width * extents.max_x_advance) / 2;
top_margin = (rectangle.height - terminal->height * extents.height) / 2;
for (i = 0; i < terminal->height; i++) {
cairo_move_to(cr, side_margin,
top_margin + extents.ascent + extents.height * i);
cairo_show_text(cr, terminal_get_row(terminal, i));
}
d = terminal->focused ? 0 : 0.5;
cairo_set_line_width(cr, 1);
cairo_move_to(cr, side_margin + terminal->column * extents.max_x_advance + d,
top_margin + terminal->row * extents.height + d);
cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
cairo_rel_line_to(cr, 0, extents.height - 2 * d);
cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
cairo_close_path(cr);
if (terminal->focused)
cairo_fill(cr);
else
cairo_stroke(cr);
cairo_destroy(cr);
window_copy_surface(terminal->window,
&rectangle,
surface);
cairo_surface_destroy(surface);
}
static void
terminal_draw(struct terminal *terminal)
{
struct rectangle rectangle;
cairo_surface_t *surface;
cairo_font_extents_t extents;
cairo_t *cr;
int32_t width, height;
window_get_child_rectangle(terminal->window, &rectangle);
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
cr = cairo_create(surface);
cairo_select_font_face (cr, "mono",
CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size(cr, 14);
cairo_font_extents(cr, &extents);
cairo_destroy(cr);
cairo_surface_destroy(surface);
width = (rectangle.width - 2 * terminal->margin) / (int32_t) extents.max_x_advance;
height = (rectangle.height - 2 * terminal->margin) / (int32_t) extents.height;
terminal_resize(terminal, width, height);
if (!terminal->fullscreen) {
rectangle.width = terminal->width * extents.max_x_advance + 2 * terminal->margin;
rectangle.height = terminal->height * extents.height + 2 * terminal->margin;
window_set_child_size(terminal->window, &rectangle);
}
window_draw(terminal->window);
terminal_draw_contents(terminal);
window_commit(terminal->window, 0);
}
static gboolean
idle_redraw(void *data)
{
struct terminal *terminal = data;
terminal_draw(terminal);
return FALSE;
}
#define STATE_NORMAL 0
#define STATE_ESCAPE 1
static void
terminal_data(struct terminal *terminal, const char *data, size_t length);
static void
terminal_schedule_redraw(struct terminal *terminal)
{
if (!terminal->redraw_scheduled) {
g_idle_add(idle_redraw, terminal);
terminal->redraw_scheduled = 1;
} else {
terminal->redraw_pending = 1;
}
}
static void
handle_escape(struct terminal *terminal)
{
char *row, *p;
int i, count;
int args[10], set[10] = { 0, };
terminal->escape[terminal->escape_length++] = '\0';
i = 0;
p = &terminal->escape[2];
while ((isdigit(*p) || *p == ';') && i < 10) {
if (*p == ';') {
p++;
i++;
} else {
args[i] = strtol(p, &p, 10);
set[i] = 1;
}
}
switch (*p) {
case 'A':
count = set[0] ? args[0] : 1;
if (terminal->row - count >= 0)
terminal->row -= count;
else
terminal->row = 0;
break;
case 'B':
count = set[0] ? args[0] : 1;
if (terminal->row + count < terminal->height)
terminal->row += count;
else
terminal->row = terminal->height;
break;
case 'C':
count = set[0] ? args[0] : 1;
if (terminal->column + count < terminal->width)
terminal->column += count;
else
terminal->column = terminal->width;
break;
case 'D':
count = set[0] ? args[0] : 1;
if (terminal->column - count >= 0)
terminal->column -= count;
else
terminal->column = 0;
break;
case 'J':
row = terminal_get_row(terminal, terminal->row);
memset(&row[terminal->column], 0, terminal->width - terminal->column);
for (i = terminal->row + 1; i < terminal->height; i++)
memset(terminal_get_row(terminal, i), 0, terminal->width);
break;
case 'G':
if (set[0])
terminal->column = args[0] - 1;
break;
case 'H':
case 'f':
terminal->row = set[0] ? args[0] - 1 : 0;
terminal->column = set[1] ? args[1] - 1 : 0;
break;
case 'K':
row = terminal_get_row(terminal, terminal->row);
memset(&row[terminal->column], 0, terminal->width - terminal->column);
break;
case 'm':
/* color, blink, bold etc*/
break;
case '?':
if (strcmp(p, "?25l") == 0) {
/* hide cursor */
} else if (strcmp(p, "?25h") == 0) {
/* show cursor */
}
break;
default:
terminal_data(terminal,
terminal->escape + 1,
terminal->escape_length - 2);
break;
}
}
static void
terminal_data(struct terminal *terminal, const char *data, size_t length)
{
int i;
char *row;
for (i = 0; i < length; i++) {
row = terminal_get_row(terminal, terminal->row);
if (terminal->state == STATE_ESCAPE) {
terminal->escape[terminal->escape_length++] = data[i];
if (terminal->escape_length == 2 && data[i] != '[') {
/* Bad escape sequence. */
terminal->state = STATE_NORMAL;
goto cancel_escape;
}
if (isalpha(data[i])) {
terminal->state = STATE_NORMAL;
handle_escape(terminal);
}
continue;
}
cancel_escape:
switch (data[i]) {
case '\r':
terminal->column = 0;
break;
case '\n':
terminal->column = 0;
if (terminal->row + 1 < terminal->height) {
terminal->row++;
} else {
terminal->start++;
if (terminal->start == terminal->height)
terminal->start = 0;
memset(terminal_get_row(terminal, terminal->row),
0, terminal->width);
}
break;
case '\t':
memset(&row[terminal->column], ' ', -terminal->column & 7);
terminal->column = (terminal->column + 7) & ~7;
break;
case '\e':
terminal->state = STATE_ESCAPE;
terminal->escape[0] = '\e';
terminal->escape_length = 1;
break;
case '\b':
if (terminal->column > 0)
terminal->column--;
break;
case '\a':
/* Bell */
break;
default:
if (terminal->column < terminal->width)
row[terminal->column++] = data[i] < 32 ? data[i] + 64 : data[i];
break;
}
}
terminal_schedule_redraw(terminal);
}
static void
resize_handler(struct window *window, void *data)
{
struct terminal *terminal = data;
terminal_schedule_redraw(terminal);
}
static void
handle_acknowledge(void *data,
struct wl_compositor *compositor,
uint32_t key, uint32_t frame)
{
struct terminal *terminal = data;
terminal->redraw_scheduled = 0;
if (terminal->redraw_pending) {
terminal->redraw_pending = 0;
terminal_schedule_redraw(terminal);
}
}
static void
handle_frame(void *data,
struct wl_compositor *compositor,
uint32_t frame, uint32_t timestamp)
{
}
static const struct wl_compositor_listener compositor_listener = {
handle_acknowledge,
handle_frame,
};
static void
key_handler(struct window *window, uint32_t key, uint32_t unicode,
uint32_t state, uint32_t modifiers, void *data)
{
struct terminal *terminal = data;
char ch = unicode;
switch (key) {
case KEY_F11:
if (!state)
break;
terminal->fullscreen ^= 1;
window_set_fullscreen(window, terminal->fullscreen);
terminal_schedule_redraw(terminal);
break;
default:
if (state && unicode)
write(terminal->master, &ch, 1);
break;
}
}
static void
keyboard_focus_handler(struct window *window,
struct wl_input_device *device, void *data)
{
struct terminal *terminal = data;
terminal->focused = (device != NULL);
terminal_schedule_redraw(terminal);
}
static struct terminal *
terminal_create(struct display *display, int fullscreen)
{
struct terminal *terminal;
terminal = malloc(sizeof *terminal);
if (terminal == NULL)
return terminal;
memset(terminal, 0, sizeof *terminal);
terminal->fullscreen = fullscreen;
terminal->color_scheme = &jbarnes_colors;
terminal->window = window_create(display, "Wayland Terminal",
500, 100, 500, 400);
terminal->display = display;
terminal->redraw_scheduled = 1;
terminal->margin = 5;
terminal->compositor = display_get_compositor(display);
window_set_fullscreen(terminal->window, terminal->fullscreen);
window_set_resize_handler(terminal->window, resize_handler, terminal);
wl_compositor_add_listener(terminal->compositor,
&compositor_listener, terminal);
window_set_key_handler(terminal->window, key_handler, terminal);
window_set_keyboard_focus_handler(terminal->window,
keyboard_focus_handler, terminal);
terminal_draw(terminal);
return terminal;
}
static gboolean
io_handler(GIOChannel *source,
GIOCondition condition,
gpointer data)
{
struct terminal *terminal = data;
gchar buffer[256];
gsize bytes_read;
GError *error = NULL;
g_io_channel_read_chars(source, buffer, sizeof buffer,
&bytes_read, &error);
terminal_data(terminal, buffer, bytes_read);
return TRUE;
}
static int
terminal_run(struct terminal *terminal, const char *path)
{
int master;
pid_t pid;
pid = forkpty(&master, NULL, NULL, NULL);
if (pid == 0) {
setenv("TERM", "vt100", 1);
if (execl(path, path, NULL)) {
printf("exec failed: %m\n");
exit(EXIT_FAILURE);
}
} else if (pid < 0) {
fprintf(stderr, "failed to fork and create pty (%m).\n");
return -1;
}
terminal->master = master;
terminal->channel = g_io_channel_unix_new(master);
fcntl(master, F_SETFL, O_NONBLOCK);
g_io_add_watch(terminal->channel, G_IO_IN,
io_handler, terminal);
return 0;
}
static const GOptionEntry option_entries[] = {
{ "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
&option_fullscreen, "Run in fullscreen mode" },
{ NULL }
};
int main(int argc, char *argv[])
{
struct wl_display *display;
int fd;
GMainLoop *loop;
GSource *source;
struct display *d;
struct terminal *terminal;
GOptionContext *context;
GError *error;
context = g_option_context_new(NULL);
g_option_context_add_main_entries(context, option_entries, "Wayland Terminal");
if (!g_option_context_parse(context, &argc, &argv, &error)) {
fprintf(stderr, "option parsing failed: %s\n", error->message);
exit(EXIT_FAILURE);
}
fd = open(gem_device, O_RDWR);
if (fd < 0) {
fprintf(stderr, "drm open failed: %m\n");
return -1;
}
display = wl_display_create(socket_name, sizeof socket_name);
if (display == NULL) {
fprintf(stderr, "failed to create display: %m\n");
return -1;
}
d = display_create(display, fd);
loop = g_main_loop_new(NULL, FALSE);
source = wl_glib_source_new(display);
g_source_attach(source, NULL);
terminal = terminal_create(d, option_fullscreen);
if (terminal_run(terminal, "/bin/bash"))
exit(EXIT_FAILURE);
g_main_loop_run(loop);
return 0;
}

313
clients/view.c Normal file
View file

@ -0,0 +1,313 @@
/*
* Copyright © 2008 Kristian Høgsberg
* Copyright © 2009 Chris Wilson
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting documentation, and
* that the name of the copyright holders not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. The copyright holders make no representations
* about the suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include <cairo.h>
#include <cairo-drm.h>
#include <glib.h>
#include <linux/input.h>
#include <glib/poppler-document.h>
#include <glib/poppler-page.h>
#include "wayland-util.h"
#include "wayland-client.h"
#include "wayland-glib.h"
#include "window.h"
static const char gem_device[] = "/dev/dri/card0";
static const char socket_name[] = "\0wayland";
struct view {
struct window *window;
struct display *display;
struct wl_compositor *compositor;
uint32_t key;
gboolean redraw_scheduled;
gboolean redraw_pending;
cairo_surface_t *surface;
gchar *filename;
PopplerDocument *document;
int page;
int fullscreen;
int focused;
};
static void
view_draw(struct view *view)
{
struct rectangle rectangle;
cairo_t *cr;
PopplerPage *page;
double width, height, doc_aspect, window_aspect, scale;
view->redraw_pending = 0;
window_draw(view->window);
window_get_child_rectangle(view->window, &rectangle);
page = poppler_document_get_page(view->document, view->page);
view->surface =
window_create_surface(view->window, &rectangle);
cr = cairo_create(view->surface);
cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_paint(cr);
poppler_page_get_size(page, &width, &height);
doc_aspect = width / height;
window_aspect = (double) rectangle.width / rectangle.height;
if (doc_aspect < window_aspect)
scale = rectangle.height / height;
else
scale = rectangle.width / width;
cairo_scale(cr, scale, scale);
cairo_translate(cr,
(rectangle.width - width * scale) / 2 / scale,
(rectangle.height - height * scale) / 2 / scale);
cairo_rectangle(cr, 0, 0, width, height);
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_fill(cr);
poppler_page_render(page, cr);
cairo_destroy(cr);
g_object_unref(G_OBJECT(page));
window_copy_surface(view->window,
&rectangle,
view->surface);
wl_compositor_commit(view->compositor, view->key);
}
static gboolean
view_idle_redraw(void *data)
{
struct view *view = data;
view_draw(view);
return FALSE;
}
static void
view_schedule_redraw(struct view *view)
{
if (!view->redraw_scheduled) {
view->redraw_scheduled = 1;
g_idle_add(view_idle_redraw, view);
} else {
view->redraw_pending = 1;
}
}
static void
key_handler(struct window *window, uint32_t key, uint32_t unicode,
uint32_t state, uint32_t modifiers, void *data)
{
struct view *view = data;
switch (key) {
case KEY_F11:
if (!state)
break;
view->fullscreen ^= 1;
window_set_fullscreen(window, view->fullscreen);
view_schedule_redraw(view);
break;
case KEY_SPACE:
case KEY_PAGEDOWN:
if (!state)
break;
view->page++;
view_schedule_redraw(view);
break;
case KEY_BACKSPACE:
case KEY_PAGEUP:
if (!state)
break;
view->page--;
view_schedule_redraw(view);
break;
default:
break;
}
}
static void
resize_handler(struct window *window, void *data)
{
struct view *view = data;
view_schedule_redraw(view);
}
static void
handle_acknowledge(void *data,
struct wl_compositor *compositor,
uint32_t key, uint32_t frame)
{
struct view *view = data;
if (view->key != key)
return;
cairo_surface_destroy(view->surface);
view->redraw_scheduled = 0;
if (view->redraw_pending) {
view->redraw_pending = 0;
view_schedule_redraw(view);
}
}
static void
keyboard_focus_handler(struct window *window,
struct wl_input_device *device, void *data)
{
struct view *view = data;
view->focused = (device != NULL);
view_schedule_redraw(view);
}
static void
handle_frame(void *data,
struct wl_compositor *compositor,
uint32_t frame, uint32_t timestamp)
{
}
static const struct wl_compositor_listener compositor_listener = {
handle_acknowledge,
handle_frame,
};
static struct view *
view_create(struct display *display, uint32_t key, const char *filename)
{
struct view *view;
gchar *basename;
gchar *title;
GError *error = NULL;
view = malloc(sizeof *view);
if (view == NULL)
return view;
memset(view, 0, sizeof *view);
basename = g_path_get_basename(filename);
title = g_strdup_printf("Wayland View - %s", basename);
g_free(basename);
view->filename = g_strdup(filename);
view->window = window_create(display, title,
100 * key, 100 * key, 500, 400);
view->display = display;
/* FIXME: Window uses key 1 for moves, need some kind of
* allocation scheme here. Or maybe just a real toolkit. */
view->key = key + 100;
view->redraw_scheduled = 1;
view->compositor = display_get_compositor(display);
window_set_resize_handler(view->window, resize_handler, view);
window_set_key_handler(view->window, key_handler, view);
window_set_keyboard_focus_handler(view->window,
keyboard_focus_handler, view);
wl_compositor_add_listener(view->compositor,
&compositor_listener, view);
view->document = poppler_document_new_from_file(view->filename,
NULL, &error);
view->page = 0;
view_draw(view);
return view;
}
static const GOptionEntry option_entries[] = {
{ NULL }
};
int
main(int argc, char *argv[])
{
struct wl_display *display;
int fd;
GMainLoop *loop;
GSource *source;
struct display *d;
GOptionContext *context;
GError *error = NULL;
int i;
context = g_option_context_new(NULL);
g_option_context_add_main_entries(context, option_entries, "Wayland View");
if (!g_option_context_parse(context, &argc, &argv, &error)) {
fprintf(stderr, "option parsing failed: %s\n", error->message);
exit(EXIT_FAILURE);
}
fd = open(gem_device, O_RDWR);
if (fd < 0) {
fprintf(stderr, "drm open failed: %m\n");
return -1;
}
display = wl_display_create(socket_name, sizeof socket_name);
if (display == NULL) {
fprintf(stderr, "failed to create display: %m\n");
return -1;
}
d = display_create(display, fd);
loop = g_main_loop_new(NULL, FALSE);
source = wl_glib_source_new(display);
g_source_attach(source, NULL);
g_type_init ();
for (i = 1; i < argc; i++) {
struct view *view;
view = view_create (d, i, argv[i]);
}
g_main_loop_run(loop);
return 0;
}