mirror of
https://gitlab.freedesktop.org/wayland/wayland.git
synced 2025-11-04 13:29:51 -05:00
Factor out common cairo code, add blur function.
This commit is contained in:
parent
e4feb56316
commit
2f2cfae227
5 changed files with 244 additions and 170 deletions
95
pointer.c
95
pointer.c
|
|
@ -2,8 +2,6 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <i915_drm.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <math.h>
|
||||
|
|
@ -13,64 +11,31 @@
|
|||
#include "wayland-client.h"
|
||||
#include "wayland-glib.h"
|
||||
|
||||
#include "cairo-util.h"
|
||||
|
||||
static const char gem_device[] = "/dev/dri/card0";
|
||||
static const char socket_name[] = "\0wayland";
|
||||
|
||||
static uint32_t name_cairo_surface(int fd, cairo_surface_t *surface)
|
||||
static void
|
||||
pointer_path(cairo_t *cr, int x, int y)
|
||||
{
|
||||
struct drm_i915_gem_create create;
|
||||
struct drm_gem_flink flink;
|
||||
struct drm_i915_gem_pwrite pwrite;
|
||||
int32_t width, height, stride;
|
||||
void *data;
|
||||
const int end = 4, tx = 2, ty = 7, dx = 3, dy = 5;
|
||||
const int width = 16, height = 16;
|
||||
|
||||
width = cairo_image_surface_get_width(surface);
|
||||
height = cairo_image_surface_get_height(surface);
|
||||
stride = cairo_image_surface_get_stride(surface);
|
||||
data = cairo_image_surface_get_data(surface);
|
||||
|
||||
memset(&create, 0, sizeof(create));
|
||||
create.size = height * stride;
|
||||
|
||||
if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
|
||||
fprintf(stderr, "gem create failed: %m\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
pwrite.handle = create.handle;
|
||||
pwrite.offset = 0;
|
||||
pwrite.size = height * stride;
|
||||
pwrite.data_ptr = (uint64_t) (uintptr_t) data;
|
||||
if (ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite) < 0) {
|
||||
fprintf(stderr, "gem pwrite failed: %m\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
flink.handle = create.handle;
|
||||
if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
|
||||
fprintf(stderr, "gem flink failed: %m\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if 0
|
||||
/* We need to hold on to the handle until the server has received
|
||||
* the attach request... we probably need a confirmation event.
|
||||
* I guess the breadcrumb idea will suffice. */
|
||||
struct drm_gem_close close;
|
||||
close.handle = create.handle;
|
||||
if (ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close) < 0) {
|
||||
fprintf(stderr, "gem close failed: %m\n");
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
return flink.name;
|
||||
cairo_move_to(cr, x, y);
|
||||
cairo_line_to(cr, x + tx, y + ty);
|
||||
cairo_line_to(cr, x + dx, y + dy);
|
||||
cairo_line_to(cr, x + width - end, y + height);
|
||||
cairo_line_to(cr, x + width, y + height - end);
|
||||
cairo_line_to(cr, x + dy, y + dx);
|
||||
cairo_line_to(cr, x + ty, y + tx);
|
||||
cairo_close_path(cr);
|
||||
}
|
||||
|
||||
static void *
|
||||
draw_pointer(int width, int height)
|
||||
{
|
||||
const int d = 2, end = 6, tx = 2, ty = 7, dx = 3, dy = 5;
|
||||
const int hotspot_x = 16, hotspot_y = 16;
|
||||
cairo_surface_t *surface;
|
||||
cairo_t *cr;
|
||||
|
||||
|
|
@ -78,20 +43,17 @@ draw_pointer(int width, int height)
|
|||
width, height);
|
||||
|
||||
cr = cairo_create(surface);
|
||||
cairo_set_line_width (cr, d);
|
||||
cairo_move_to(cr, d, d);
|
||||
cairo_line_to(cr, d + tx, d + ty);
|
||||
cairo_line_to(cr, d + dx, d + dy);
|
||||
cairo_line_to(cr, width - end, height - d);
|
||||
cairo_line_to(cr, width - d, height - end);
|
||||
cairo_line_to(cr, d + dy, d + dx);
|
||||
cairo_line_to(cr, d + ty, d + tx);
|
||||
cairo_close_path(cr);
|
||||
pointer_path(cr, hotspot_x + 3, hotspot_y + 2);
|
||||
cairo_set_line_width (cr, 2);
|
||||
cairo_set_source_rgb(cr, 0, 0, 0);
|
||||
cairo_stroke_preserve(cr);
|
||||
cairo_fill(cr);
|
||||
blur_surface(surface);
|
||||
|
||||
pointer_path(cr, hotspot_x, hotspot_y);
|
||||
cairo_stroke_preserve(cr);
|
||||
cairo_set_source_rgb(cr, 1, 1, 1);
|
||||
cairo_fill(cr);
|
||||
|
||||
cairo_destroy(cr);
|
||||
|
||||
return surface;
|
||||
|
|
@ -118,10 +80,10 @@ int main(int argc, char *argv[])
|
|||
struct wl_display *display;
|
||||
struct pointer pointer;
|
||||
int fd;
|
||||
uint32_t name;
|
||||
cairo_surface_t *s;
|
||||
GMainLoop *loop;
|
||||
GSource *source;
|
||||
struct buffer *buffer;
|
||||
|
||||
fd = open(gem_device, O_RDWR);
|
||||
if (fd < 0) {
|
||||
|
|
@ -139,16 +101,15 @@ int main(int argc, char *argv[])
|
|||
source = wayland_source_new(display);
|
||||
g_source_attach(source, NULL);
|
||||
|
||||
pointer.width = 16;
|
||||
pointer.height = 16;
|
||||
pointer.width = 48;
|
||||
pointer.height = 48;
|
||||
pointer.surface = wl_display_create_surface(display);
|
||||
|
||||
s = draw_pointer(pointer.width, pointer.height);
|
||||
name = name_cairo_surface(fd, s);
|
||||
buffer = buffer_create_from_cairo_surface(fd, s);
|
||||
|
||||
wl_surface_attach(pointer.surface, name,
|
||||
pointer.width, pointer.height,
|
||||
cairo_image_surface_get_stride(s));
|
||||
wl_surface_attach(pointer.surface, buffer->name,
|
||||
buffer->width, buffer->height, buffer->stride);
|
||||
wl_surface_map(pointer.surface, 512, 384, pointer.width, pointer.height);
|
||||
|
||||
wl_display_set_event_handler(display, event_handler, &pointer);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue