commit
6eeeb30021
@ -0,0 +1,393 @@ |
||||
/*
|
||||
* Copyright © 2011 Benjamin Franzke |
||||
* |
||||
* 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 <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <stddef.h> |
||||
#include <stdint.h> |
||||
#include <stdbool.h> |
||||
#include <math.h> |
||||
#include <assert.h> |
||||
|
||||
#include <fcntl.h> |
||||
|
||||
#include <wayland-client.h> |
||||
#include <xf86drm.h> |
||||
|
||||
#define GL_GLEXT_PROTOTYPES |
||||
#define EGL_EGLEXT_PROTOTYPES |
||||
#include <GLES2/gl2.h> |
||||
#include <GLES2/gl2ext.h> |
||||
#include <EGL/egl.h> |
||||
#include <EGL/eglext.h> |
||||
|
||||
struct display { |
||||
struct wl_display *display; |
||||
struct { |
||||
struct wl_compositor *compositor; |
||||
struct wl_drm *drm; |
||||
} interface; |
||||
struct { |
||||
EGLDisplay dpy; |
||||
EGLContext ctx; |
||||
} egl; |
||||
struct { |
||||
int fd; |
||||
const char *device_name; |
||||
bool authenticated; |
||||
} drm; |
||||
uint32_t mask; |
||||
}; |
||||
|
||||
struct window { |
||||
struct display *display; |
||||
struct { |
||||
int width, height; |
||||
} geometry; |
||||
struct { |
||||
GLuint fbo; |
||||
GLuint color_rbo; |
||||
|
||||
GLuint program; |
||||
GLuint rotation_uniform; |
||||
|
||||
GLuint pos; |
||||
GLuint col; |
||||
} gl; |
||||
struct { |
||||
struct wl_buffer *buffer; |
||||
struct wl_surface *surface; |
||||
EGLImageKHR image; |
||||
} drm_surface; |
||||
}; |
||||
|
||||
static const char *vert_shader_text = |
||||
"uniform mat4 rotation;\n" |
||||
"attribute vec4 pos;\n" |
||||
"attribute vec4 color;\n" |
||||
"varying vec4 v_color;\n" |
||||
"void main() {\n" |
||||
" gl_Position = rotation * pos;\n" |
||||
" v_color = color;\n" |
||||
"}\n"; |
||||
|
||||
static const char *frag_shader_text = |
||||
"precision mediump float;\n" |
||||
"varying vec4 v_color;\n" |
||||
"void main() {\n" |
||||
" gl_FragColor = v_color;\n" |
||||
"}\n"; |
||||
|
||||
static void |
||||
init_egl(struct display *display) |
||||
{ |
||||
static const EGLint context_attribs[] = { |
||||
EGL_CONTEXT_CLIENT_VERSION, 2, |
||||
EGL_NONE |
||||
}; |
||||
|
||||
EGLint major, minor; |
||||
EGLBoolean ret; |
||||
|
||||
display->egl.dpy = eglGetDRMDisplayMESA(display->drm.fd); |
||||
assert(display->egl.dpy); |
||||
|
||||
ret = eglInitialize(display->egl.dpy, &major, &minor); |
||||
assert(ret == EGL_TRUE); |
||||
ret = eglBindAPI(EGL_OPENGL_ES_API); |
||||
assert(ret == EGL_TRUE); |
||||
|
||||
display->egl.ctx = eglCreateContext(display->egl.dpy, NULL, |
||||
EGL_NO_CONTEXT, context_attribs); |
||||
assert(display->egl.ctx); |
||||
ret = eglMakeCurrent(display->egl.dpy, NULL, NULL, display->egl.ctx); |
||||
assert(ret == EGL_TRUE); |
||||
} |
||||
|
||||
static GLuint |
||||
create_shader(struct window *window, const char *source, GLenum shader_type) |
||||
{ |
||||
GLuint shader; |
||||
GLint status; |
||||
|
||||
shader = glCreateShader(shader_type); |
||||
assert(shader != 0); |
||||
|
||||
glShaderSource(shader, 1, (const char **) &source, NULL); |
||||
glCompileShader(shader); |
||||
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &status); |
||||
if (!status) { |
||||
char log[1000]; |
||||
GLsizei len; |
||||
glGetShaderInfoLog(shader, 1000, &len, log); |
||||
fprintf(stderr, "Error: compiling %s: %*s\n", |
||||
shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment", |
||||
len, log); |
||||
exit(1); |
||||
} |
||||
|
||||
return shader; |
||||
} |
||||
|
||||
static void |
||||
init_gl(struct window *window) |
||||
{ |
||||
GLfloat ar; |
||||
GLuint frag, vert; |
||||
GLint status; |
||||
|
||||
glGenFramebuffers(1, &window->gl.fbo); |
||||
glBindFramebuffer(GL_FRAMEBUFFER, window->gl.fbo); |
||||
|
||||
glGenRenderbuffers(1, &window->gl.color_rbo); |
||||
|
||||
glViewport(0, 0, window->geometry.width, window->geometry.height); |
||||
ar = (GLfloat)window->geometry.width / (GLfloat)window->geometry.height; |
||||
|
||||
frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER); |
||||
vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER); |
||||
|
||||
window->gl.program = glCreateProgram(); |
||||
glAttachShader(window->gl.program, frag); |
||||
glAttachShader(window->gl.program, vert); |
||||
glLinkProgram(window->gl.program); |
||||
|
||||
glGetProgramiv(window->gl.program, GL_LINK_STATUS, &status); |
||||
if (!status) { |
||||
char log[1000]; |
||||
GLsizei len; |
||||
glGetProgramInfoLog(window->gl.program, 1000, &len, log); |
||||
fprintf(stderr, "Error: linking:\n%*s\n", len, log); |
||||
exit(1); |
||||
} |
||||
|
||||
glUseProgram(window->gl.program); |
||||
|
||||
window->gl.pos = 0; |
||||
window->gl.pos = 1; |
||||
|
||||
glBindAttribLocation(window->gl.program, window->gl.pos, "pos"); |
||||
glBindAttribLocation(window->gl.program, window->gl.col, "color"); |
||||
glLinkProgram(window->gl.program); |
||||
|
||||
window->gl.rotation_uniform = |
||||
glGetUniformLocation(window->gl.program, "rotation"); |
||||
} |
||||
|
||||
static void |
||||
create_surface(struct window *window) |
||||
{ |
||||
struct display *display = window->display; |
||||
struct wl_visual *visual; |
||||
EGLint name, stride; |
||||
EGLint image_attribs[] = { |
||||
EGL_WIDTH, 0, |
||||
EGL_HEIGHT, 0, |
||||
EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA, |
||||
EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA, |
||||
EGL_NONE |
||||
}; |
||||
|
||||
window->drm_surface.surface = |
||||
wl_compositor_create_surface(display->interface.compositor); |
||||
|
||||
image_attribs[1] = window->geometry.width; |
||||
image_attribs[3] = window->geometry.height; |
||||
|
||||
window->drm_surface.image = eglCreateDRMImageMESA(display->egl.dpy, |
||||
image_attribs); |
||||
eglExportDRMImageMESA(display->egl.dpy, window->drm_surface.image, |
||||
&name, NULL, &stride); |
||||
visual = wl_display_get_premultiplied_argb_visual(display->display); |
||||
|
||||
window->drm_surface.buffer = |
||||
wl_drm_create_buffer(display->interface.drm, name, |
||||
window->geometry.width, |
||||
window->geometry.height, |
||||
stride, visual); |
||||
|
||||
wl_surface_attach(window->drm_surface.surface, |
||||
window->drm_surface.buffer, 0, 0); |
||||
wl_surface_map_toplevel(window->drm_surface.surface); |
||||
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, window->gl.color_rbo); |
||||
glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER, |
||||
window->drm_surface.image); |
||||
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
||||
GL_COLOR_ATTACHMENT0, |
||||
GL_RENDERBUFFER, |
||||
window->gl.color_rbo); |
||||
|
||||
assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == |
||||
GL_FRAMEBUFFER_COMPLETE); |
||||
} |
||||
|
||||
static void |
||||
redraw(void *data, uint32_t time) |
||||
{ |
||||
struct window *window = data; |
||||
static const GLfloat verts[3][2] = { |
||||
{ -0.5, -0.5 }, |
||||
{ 0.5, -0.5 }, |
||||
{ 0, 0.5 } |
||||
}; |
||||
static const GLfloat colors[3][3] = { |
||||
{ 1, 0, 0 }, |
||||
{ 0, 1, 0 }, |
||||
{ 0, 0, 1 } |
||||
}; |
||||
GLfloat angle; |
||||
GLfloat rotation[4][4] = { |
||||
{ 1, 0, 0, 0 }, |
||||
{ 0, 1, 0, 0 }, |
||||
{ 0, 0, 1, 0 }, |
||||
{ 0, 0, 0, 1 } |
||||
}; |
||||
static const int32_t speed_div = 5; |
||||
static uint32_t start_time = 0; |
||||
|
||||
if (start_time == 0) |
||||
start_time = time; |
||||
|
||||
angle = ((time-start_time) / speed_div) % 360 * M_PI / 180.0; |
||||
rotation[0][0] = cos(angle); |
||||
rotation[0][2] = sin(angle); |
||||
rotation[2][0] = -sin(angle); |
||||
rotation[2][2] = cos(angle); |
||||
|
||||
glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE, |
||||
(GLfloat *) rotation); |
||||
|
||||
glClearColor(0.0, 0.0, 0.0, 0.5); |
||||
glClear(GL_COLOR_BUFFER_BIT); |
||||
|
||||
glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts); |
||||
glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors); |
||||
glEnableVertexAttribArray(window->gl.pos); |
||||
glEnableVertexAttribArray(window->gl.col); |
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3); |
||||
|
||||
glDisableVertexAttribArray(window->gl.pos); |
||||
glDisableVertexAttribArray(window->gl.col); |
||||
|
||||
glFlush(); |
||||
|
||||
wl_surface_damage(window->drm_surface.surface, 0, 0, |
||||
window->geometry.width, window->geometry.height); |
||||
|
||||
wl_display_frame_callback(window->display->display, redraw, window); |
||||
} |
||||
|
||||
static void |
||||
drm_handle_device(void *data, struct wl_drm *drm, const char *device) |
||||
{ |
||||
struct display *d = data; |
||||
|
||||
d->drm.device_name = strdup(device); |
||||
} |
||||
|
||||
static void |
||||
drm_handle_authenticated(void *data, struct wl_drm *drm) |
||||
{ |
||||
struct display *d = data; |
||||
|
||||
d->drm.authenticated = true; |
||||
} |
||||
|
||||
static const struct wl_drm_listener drm_listener = { |
||||
drm_handle_device, |
||||
drm_handle_authenticated |
||||
}; |
||||
|
||||
static void |
||||
display_handle_global(struct wl_display *display, uint32_t id, |
||||
const char *interface, uint32_t version, void *data) |
||||
{ |
||||
struct display *d = data; |
||||
|
||||
if (strcmp(interface, "compositor") == 0) { |
||||
d->interface.compositor = wl_compositor_create(display, id); |
||||
} else if (strcmp(interface, "drm") == 0) { |
||||
d->interface.drm = wl_drm_create(display, id); |
||||
wl_drm_add_listener(d->interface.drm, &drm_listener, d); |
||||
} |
||||
} |
||||
|
||||
static int |
||||
event_mask_update(uint32_t mask, void *data) |
||||
{ |
||||
struct display *d = data; |
||||
|
||||
d->mask = mask; |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
int |
||||
main(int argc, char **argv) |
||||
{ |
||||
struct display display = { 0 }; |
||||
struct window window = { 0 }; |
||||
drm_magic_t magic; |
||||
int ret; |
||||
|
||||
memset(&display, 0, sizeof display); |
||||
memset(&window, 0, sizeof window); |
||||
|
||||
window.display = &display; |
||||
window.geometry.width = 250; |
||||
window.geometry.height = 250; |
||||
|
||||
display.display = wl_display_connect(NULL); |
||||
assert(display.display); |
||||
|
||||
wl_display_add_global_listener(display.display, |
||||
display_handle_global, &display); |
||||
/* process connection events */ |
||||
wl_display_iterate(display.display, WL_DISPLAY_READABLE); |
||||
|
||||
display.drm.fd = open(display.drm.device_name, O_RDWR); |
||||
assert(display.drm.fd >= 0); |
||||
|
||||
ret = drmGetMagic(display.drm.fd, &magic); |
||||
assert(ret == 0); |
||||
wl_drm_authenticate(display.interface.drm, magic); |
||||
wl_display_iterate(display.display, WL_DISPLAY_WRITABLE); |
||||
while (!display.drm.authenticated) |
||||
wl_display_iterate(display.display, WL_DISPLAY_READABLE); |
||||
|
||||
init_egl(&display); |
||||
init_gl(&window); |
||||
create_surface(&window); |
||||
|
||||
wl_display_frame_callback(display.display, redraw, &window); |
||||
|
||||
wl_display_get_fd(display.display, event_mask_update, &display); |
||||
while (true) |
||||
wl_display_iterate(display.display, display.mask); |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,239 @@ |
||||
/*
|
||||
* Copyright © 2010 Intel Corporation |
||||
* |
||||
* 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 <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <linux/input.h> |
||||
#include <unistd.h> |
||||
#include <fcntl.h> |
||||
|
||||
#include "compositor.h" |
||||
|
||||
struct evdev_input { |
||||
struct wlsc_input_device base; |
||||
}; |
||||
|
||||
struct evdev_input_device { |
||||
struct evdev_input *master; |
||||
struct wl_event_source *source; |
||||
int tool, new_x, new_y; |
||||
int base_x, base_y; |
||||
int fd; |
||||
int min_x, max_x, min_y, max_y; |
||||
}; |
||||
|
||||
static void evdev_input_device_data(int fd, uint32_t mask, void *data) |
||||
{ |
||||
struct wlsc_compositor *ec; |
||||
struct evdev_input_device *device = data; |
||||
struct input_event ev[8], *e, *end; |
||||
int len, value, dx, dy, absolute_event; |
||||
int x, y; |
||||
uint32_t time; |
||||
|
||||
/* FIXME: Obviously we need to not hardcode these here, but
|
||||
* instead get the values from the output it's associated with. */ |
||||
const int screen_width = 1024, screen_height = 600; |
||||
|
||||
ec = (struct wlsc_compositor *) |
||||
device->master->base.input_device.compositor; |
||||
if (!ec->focus) |
||||
return; |
||||
|
||||
dx = 0; |
||||
dy = 0; |
||||
absolute_event = 0; |
||||
x = device->master->base.input_device.x; |
||||
y = device->master->base.input_device.y; |
||||
|
||||
len = read(fd, &ev, sizeof ev); |
||||
if (len < 0 || len % sizeof e[0] != 0) { |
||||
/* FIXME: handle error... reopen device? */; |
||||
return; |
||||
} |
||||
|
||||
e = ev; |
||||
end = (void *) ev + len; |
||||
for (e = ev; e < end; e++) { |
||||
/* Get the signed value, earlier kernels had this as unsigned */ |
||||
value = e->value; |
||||
time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000; |
||||
|
||||
switch (e->type) { |
||||
case EV_REL: |
||||
switch (e->code) { |
||||
case REL_X: |
||||
dx += value; |
||||
break; |
||||
|
||||
case REL_Y: |
||||
dy += value; |
||||
break; |
||||
} |
||||
break; |
||||
|
||||
case EV_ABS: |
||||
switch (e->code) { |
||||
case ABS_X: |
||||
absolute_event = device->tool; |
||||
x = (value - device->min_x) * screen_width / |
||||
(device->max_x - device->min_x); |
||||
break; |
||||
case ABS_Y: |
||||
absolute_event = device->tool; |
||||
y = (value - device->min_y) * screen_height / |
||||
(device->max_y - device->min_y); |
||||
break; |
||||
} |
||||
break; |
||||
|
||||
case EV_KEY: |
||||
if (value == 2) |
||||
break; |
||||
|
||||
switch (e->code) { |
||||
case BTN_TOUCH: |
||||
case BTN_TOOL_PEN: |
||||
case BTN_TOOL_RUBBER: |
||||
case BTN_TOOL_BRUSH: |
||||
case BTN_TOOL_PENCIL: |
||||
case BTN_TOOL_AIRBRUSH: |
||||
case BTN_TOOL_FINGER: |
||||
case BTN_TOOL_MOUSE: |
||||
case BTN_TOOL_LENS: |
||||
device->tool = value ? e->code : 0; |
||||
break; |
||||
|
||||
case BTN_LEFT: |
||||
case BTN_RIGHT: |
||||
case BTN_MIDDLE: |
||||
case BTN_SIDE: |
||||
case BTN_EXTRA: |
||||
case BTN_FORWARD: |
||||
case BTN_BACK: |
||||
case BTN_TASK: |
||||
notify_button(&device->master->base.input_device, |
||||
time, e->code, value); |
||||
break; |
||||
|
||||
default: |
||||
notify_key(&device->master->base.input_device, |
||||
time, e->code, value); |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
|
||||
if (dx != 0 || dy != 0) |
||||
notify_motion(&device->master->base.input_device, |
||||
time, x + dx, y + dy); |
||||
if (absolute_event) |
||||
notify_motion(&device->master->base.input_device, time, x, y); |
||||
} |
||||
|
||||
#define TEST_BIT(b, i) (b[(i) / 32] & (1 << (i & 31))) |
||||
|
||||
static struct evdev_input_device * |
||||
evdev_input_device_create(struct evdev_input *master, |
||||
struct wl_display *display, const char *path) |
||||
{ |
||||
struct evdev_input_device *device; |
||||
struct wl_event_loop *loop; |
||||
struct input_absinfo absinfo; |
||||
uint32_t ev_bits[EV_MAX]; |
||||
uint32_t key_bits[KEY_MAX]; |
||||
|
||||
device = malloc(sizeof *device); |
||||
if (device == NULL) |
||||
return NULL; |
||||
|
||||
device->tool = 1; |
||||
device->new_x = 1; |
||||
device->new_y = 1; |
||||
device->master = master; |
||||
|
||||
device->fd = open(path, O_RDONLY); |
||||
if (device->fd < 0) { |
||||
free(device); |
||||
fprintf(stderr, "couldn't create pointer for %s: %m\n", path); |
||||
return NULL; |
||||
} |
||||
|
||||
ioctl(device->fd, EVIOCGBIT(0, EV_MAX), ev_bits); |
||||
if (TEST_BIT(ev_bits, EV_ABS)) { |
||||
ioctl(device->fd, EVIOCGBIT(EV_ABS, EV_MAX), key_bits); |
||||
if (TEST_BIT(key_bits, ABS_X)) { |
||||
ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo); |
||||
device->min_x = absinfo.minimum; |
||||
device->max_x = absinfo.maximum; |
||||
} |
||||
if (TEST_BIT(key_bits, ABS_Y)) { |
||||
ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo); |
||||
device->min_y = absinfo.minimum; |
||||
device->max_y = absinfo.maximum; |
||||
} |
||||
} |
||||
|
||||
loop = wl_display_get_event_loop(display); |
||||
device->source = wl_event_loop_add_fd(loop, device->fd, |
||||
WL_EVENT_READABLE, |
||||
evdev_input_device_data, device); |
||||
if (device->source == NULL) { |
||||
close(device->fd); |
||||
free(device); |
||||
return NULL; |
||||
} |
||||
|
||||
return device; |
||||
} |
||||
|
||||
void |
||||
evdev_input_add_devices(struct wlsc_compositor *c, struct udev *udev) |
||||
{ |
||||
struct evdev_input *input; |
||||
struct udev_enumerate *e; |
||||
struct udev_list_entry *entry; |
||||
struct udev_device *device; |
||||
const char *path; |
||||
|
||||
input = malloc(sizeof *input); |
||||
if (input == NULL) |
||||
return; |
||||
|
||||
memset(input, 0, sizeof *input); |
||||
wlsc_input_device_init(&input->base, c); |
||||
|
||||
e = udev_enumerate_new(udev); |
||||
udev_enumerate_add_match_subsystem(e, "input"); |
||||
udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1"); |
||||
udev_enumerate_scan_devices(e); |
||||
udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
||||
path = udev_list_entry_get_name(entry); |
||||
device = udev_device_new_from_syspath(udev, path); |
||||
evdev_input_device_create(input, c->wl_display, |
||||
udev_device_get_devnode(device)); |
||||
} |
||||
udev_enumerate_unref(e); |
||||
|
||||
c->input_device = &input->base.input_device; |
||||
} |
@ -0,0 +1,640 @@ |
||||
/*
|
||||
* Copyright © 2010 Intel Corporation |
||||
* |
||||
* 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 <stdlib.h> |
||||
#include <string.h> |
||||
#include <unistd.h> |
||||
|
||||
#include "wayland-server.h" |
||||
#include "compositor.h" |
||||
|
||||
struct wlsc_move_grab { |
||||
struct wl_grab grab; |
||||
int32_t dx, dy; |
||||
}; |
||||
|
||||
static void |
||||
move_grab_motion(struct wl_grab *grab, |
||||
uint32_t time, int32_t x, int32_t y) |
||||
{ |
||||
struct wlsc_move_grab *move = (struct wlsc_move_grab *) grab; |
||||
struct wlsc_surface *es = |
||||
(struct wlsc_surface *) grab->input_device->pointer_focus; |
||||
|
||||
es->x = x + move->dx; |
||||
es->y = y + move->dy; |
||||
wlsc_surface_update_matrix(es); |
||||
} |
||||
|
||||
static void |
||||
move_grab_button(struct wl_grab *grab, |
||||
uint32_t time, int32_t button, int32_t state) |
||||
{ |
||||
} |
||||
|
||||
static void |
||||
move_grab_end(struct wl_grab *grab, uint32_t time) |
||||
{ |
||||
free(grab); |
||||
} |
||||
|
||||
static const struct wl_grab_interface move_grab_interface = { |
||||
move_grab_motion, |
||||
move_grab_button, |
||||
move_grab_end |
||||
}; |
||||
|
||||
void |
||||
shell_move(struct wl_client *client, struct wl_shell *shell, |
||||
struct wl_surface *surface, |
||||
struct wl_input_device *device, uint32_t time) |
||||
{ |
||||
struct wlsc_input_device *wd = (struct wlsc_input_device *) device; |
||||
struct wlsc_surface *es = (struct wlsc_surface *) surface; |
||||
struct wlsc_move_grab *move; |
||||
|
||||
move = malloc(sizeof *move); |
||||
if (!move) { |
||||
wl_client_post_no_memory(client); |
||||
return; |
||||
} |
||||
|
||||
move->grab.interface = &move_grab_interface; |
||||
move->dx = es->x - wd->input_device.grab_x; |
||||
move->dy = es->y - wd->input_device.grab_y; |
||||
|
||||
if (wl_input_device_update_grab(&wd->input_device, |
||||
&move->grab, surface, time) < 0) |
||||
return; |
||||
|
||||
wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING); |
||||
} |
||||
|
||||
struct wlsc_resize_grab { |
||||
struct wl_grab grab; |
||||
uint32_t edges; |
||||
int32_t dx, dy, width, height; |
||||
}; |
||||
|
||||
static void |
||||
resize_grab_motion(struct wl_grab *grab, |
||||
uint32_t time, int32_t x, int32_t y) |
||||
{ |
||||
struct wlsc_resize_grab *resize = (struct wlsc_resize_grab *) grab; |
||||
struct wl_input_device *device = grab->input_device; |
||||
struct wlsc_compositor *ec = |
||||
(struct wlsc_compositor *) device->compositor; |
||||
struct wl_surface *surface = device->pointer_focus; |
||||
int32_t width, height; |
||||
|
||||
if (resize->edges & WL_GRAB_RESIZE_LEFT) { |
||||
width = device->grab_x - x + resize->width; |
||||
} else if (resize->edges & WL_GRAB_RESIZE_RIGHT) { |
||||
width = x - device->grab_x + resize->width; |
||||
} else { |
||||
width = resize->width; |
||||
} |
||||
|
||||
if (resize->edges & WL_GRAB_RESIZE_TOP) { |
||||
height = device->grab_y - y + resize->height; |
||||
} else if (resize->edges & WL_GRAB_RESIZE_BOTTOM) { |
||||
height = y - device->grab_y + resize->height; |
||||
} else { |
||||
height = resize->height; |
||||
} |
||||
|
||||
wl_client_post_event(surface->client, &ec->shell.object, |
||||
WL_SHELL_CONFIGURE, time, resize->edges, |
||||
surface, width, height); |
||||
} |
||||
|
||||
static void |
||||
resize_grab_button(struct wl_grab *grab, |
||||
uint32_t time, int32_t button, int32_t state) |
||||
{ |
||||
} |
||||
|
||||
static void |
||||
resize_grab_end(struct wl_grab *grab, uint32_t time) |
||||
{ |
||||
free(grab); |
||||
} |
||||
|
||||
static const struct wl_grab_interface resize_grab_interface = { |
||||
resize_grab_motion, |
||||
resize_grab_button, |
||||
resize_grab_end |
||||
}; |
||||
|
||||
void |
||||
shell_resize(struct wl_client *client, struct wl_shell *shell, |
||||
struct wl_surface *surface, |
||||
struct wl_input_device *device, uint32_t time, uint32_t edges) |
||||
{ |
||||
struct wlsc_input_device *wd = (struct wlsc_input_device *) device; |
||||
struct wlsc_resize_grab *resize; |
||||
enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR; |
||||
struct wlsc_surface *es = (struct wlsc_surface *) surface; |
||||
|
||||
resize = malloc(sizeof *resize); |
||||
if (!resize) { |
||||
wl_client_post_no_memory(client); |
||||
return; |
||||
} |
||||
|
||||
resize->grab.interface = &resize_grab_interface; |
||||
resize->edges = edges; |
||||
resize->dx = es->x - wd->input_device.grab_x; |
||||
resize->dy = es->y - wd->input_device.grab_y; |
||||
resize->width = es->width; |
||||
resize->height = es->height; |
||||
|
||||
if (edges == 0 || edges > 15 || |
||||
(edges & 3) == 3 || (edges & 12) == 12) |
||||
return; |
||||
|
||||
switch (edges) { |
||||
case WL_GRAB_RESIZE_TOP: |
||||
pointer = WLSC_POINTER_TOP; |
||||
break; |
||||
case WL_GRAB_RESIZE_BOTTOM: |
||||
pointer = WLSC_POINTER_BOTTOM; |
||||
break; |
||||
case WL_GRAB_RESIZE_LEFT: |
||||
pointer = WLSC_POINTER_LEFT; |
||||
break; |
||||
case WL_GRAB_RESIZE_TOP_LEFT: |
||||
pointer = WLSC_POINTER_TOP_LEFT; |
||||
break; |
||||
case WL_GRAB_RESIZE_BOTTOM_LEFT: |
||||
pointer = WLSC_POINTER_BOTTOM_LEFT; |
||||
break; |
||||
case WL_GRAB_RESIZE_RIGHT: |
||||
pointer = WLSC_POINTER_RIGHT; |
||||
break; |
||||
case WL_GRAB_RESIZE_TOP_RIGHT: |
||||
pointer = WLSC_POINTER_TOP_RIGHT; |
||||
break; |
||||
case WL_GRAB_RESIZE_BOTTOM_RIGHT: |
||||
pointer = WLSC_POINTER_BOTTOM_RIGHT; |
||||
break; |
||||
} |
||||
|
||||
if (wl_input_device_update_grab(&wd->input_device, |
||||
&resize->grab, surface, time) < 0) |
||||
return; |
||||
|
||||
wlsc_input_device_set_pointer_image(wd, pointer); |
||||
} |
||||
|
||||
static void |
||||
destroy_drag(struct wl_resource *resource, struct wl_client *client) |
||||
{ |
||||
struct wl_drag *drag = |
||||
container_of(resource, struct wl_drag, resource); |
||||
|
||||
wl_list_remove(&drag->drag_focus_listener.link); |
||||
if (drag->grab.input_device) |
||||
wl_input_device_end_grab(drag->grab.input_device, get_time()); |
||||
|
||||
free(drag); |
||||
} |
||||
|
||||
|
||||
static void |
||||
wl_drag_set_pointer_focus(struct wl_drag *drag, |
||||
struct wl_surface *surface, uint32_t time, |
||||
int32_t x, int32_t y, int32_t sx, int32_t sy) |
||||
{ |
||||
char **p, **end; |
||||
|
||||
if (drag->drag_focus == surface) |
||||
return; |
||||
|
||||
if (drag->drag_focus && |
||||
(!surface || drag->drag_focus->client != surface->client)) |
||||
wl_client_post_event(drag->drag_focus->client, |
||||
&drag->drag_offer.object, |
||||
WL_DRAG_OFFER_POINTER_FOCUS, |
||||
time, NULL, 0, 0, 0, 0); |
||||
|
||||
if (surface && |
||||
(!drag->drag_focus || |
||||
drag->drag_focus->client != surface->client)) { |
||||
wl_client_post_global(surface->client, |
||||
&drag->drag_offer.object); |
||||
|
||||
end = drag->types.data + drag->types.size; |
||||
for (p = drag->types.data; p < end; p++) |
||||
wl_client_post_event(surface->client, |
||||
&drag->drag_offer.object, |
||||
WL_DRAG_OFFER_OFFER, *p); |
||||
} |
||||
|
||||
if (surface) { |
||||
wl_client_post_event(surface->client, |
||||
&drag->drag_offer.object, |
||||
WL_DRAG_OFFER_POINTER_FOCUS, |
||||
time, surface, |
||||
x, y, sx, sy); |
||||
|
||||
} |
||||
|
||||
drag->drag_focus = surface; |
||||
drag->pointer_focus_time = time; |
||||
drag->target = NULL; |
||||
|
||||
wl_list_remove(&drag->drag_focus_listener.link); |
||||
if (surface) |
||||
wl_list_insert(surface->destroy_listener_list.prev, |
||||
&drag->drag_focus_listener.link); |
||||
} |
||||
|
||||
static void |
||||
drag_offer_accept(struct wl_client *client, |
||||
struct wl_drag_offer *offer, uint32_t time, const char *type) |
||||
{ |
||||
struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer); |
||||
char **p, **end; |
||||
|
||||
/* If the client responds to drag pointer_focus or motion
|
||||
* events after the pointer has left the surface, we just |
||||
* discard the accept requests. The drag source just won't |
||||
* get the corresponding 'target' events and eventually the |
||||
* next surface/root will start sending events. */ |
||||
if (time < drag->pointer_focus_time) |
||||
return; |
||||
|
||||
drag->target = client; |
||||
drag->type = NULL; |
||||
end = drag->types.data + drag->types.size; |
||||
for (p = drag->types.data; p < end; p++) |
||||
if (type && strcmp(*p, type) == 0) |
||||
drag->type = *p; |
||||
|
||||
wl_client_post_event(drag->source->client, &drag->resource.object, |
||||
WL_DRAG_TARGET, drag->type); |
||||
} |
||||
|
||||
static void |
||||
drag_offer_receive(struct wl_client *client, |
||||
struct wl_drag_offer *offer, int fd) |
||||
{ |
||||
struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer); |
||||
|
||||
wl_client_post_event(drag->source->client, &drag->resource.object, |
||||
WL_DRAG_FINISH, fd); |
||||
close(fd); |
||||
} |
||||
|
||||
static void |
||||
drag_offer_reject(struct wl_client *client, struct wl_drag_offer *offer) |
||||
{ |
||||
struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer); |
||||
|
||||
wl_client_post_event(drag->source->client, &drag->resource.object, |
||||
WL_DRAG_REJECT); |
||||
} |
||||
|
||||
static const struct wl_drag_offer_interface drag_offer_interface = { |
||||
drag_offer_accept, |
||||
drag_offer_receive, |
||||
drag_offer_reject |
||||
}; |
||||
|
||||
static void |
||||
drag_offer(struct wl_client *client, struct wl_drag *drag, const char *type) |
||||
{ |
||||
char **p; |
||||
|
||||
p = wl_array_add(&drag->types, sizeof *p); |
||||
if (p) |
||||
*p = strdup(type); |
||||
if (!p || !*p) |
||||
wl_client_post_no_memory(client); |
||||
} |
||||
|
||||
static void |
||||
drag_grab_motion(struct wl_grab *grab, |
||||
uint32_t time, int32_t x, int32_t y) |
||||
{ |
||||
struct wl_drag *drag = container_of(grab, struct wl_drag, grab); |
||||
struct wlsc_surface *es; |
||||
int32_t sx, sy; |
||||
|
||||
es = pick_surface(grab->input_device, &sx, &sy); |
||||
wl_drag_set_pointer_focus(drag, &es->surface, time, x, y, sx, sy); |
||||
if (es) |
||||
wl_client_post_event(es->surface.client, |
||||
&drag->drag_offer.object, |
||||
WL_DRAG_OFFER_MOTION, |
||||
time, x, y, sx, sy); |
||||
} |
||||
|
||||
static void |
||||
drag_grab_button(struct wl_grab *grab, |
||||
uint32_t time, int32_t button, int32_t state) |
||||
{ |
||||
} |
||||
|
||||
static void |
||||
drag_grab_end(struct wl_grab *grab, uint32_t time) |
||||
{ |
||||
struct wl_drag *drag = container_of(grab, struct wl_drag, grab); |
||||
|
||||
if (drag->target) |
||||
wl_client_post_event(drag->target, |
||||
&drag->drag_offer.object, |
||||
WL_DRAG_OFFER_DROP); |
||||
|
||||
wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0); |
||||
} |
||||
|
||||
static const struct wl_grab_interface drag_grab_interface = { |
||||
drag_grab_motion, |
||||
drag_grab_button, |
||||
drag_grab_end |
||||
}; |
||||
|
||||
static void |
||||
drag_activate(struct wl_client *client, |
||||
struct wl_drag *drag, |
||||
struct wl_surface *surface, |
||||
struct wl_input_device *device, uint32_t time) |
||||
{ |
||||
struct wl_display *display = wl_client_get_display (client); |
||||
struct wlsc_surface *target; |
||||
int32_t sx, sy; |
||||
|
||||
if (wl_input_device_update_grab(device, |
||||
&drag->grab, surface, time) < 0) |
||||
return; |
||||
|
||||
drag->grab.interface = &drag_grab_interface; |
||||
|
||||
drag->source = surface; |
||||
|
||||
drag->drag_offer.object.interface = &wl_drag_offer_interface; |
||||
drag->drag_offer.object.implementation = |
||||
(void (**)(void)) &drag_offer_interface; |
||||
|
||||
wl_display_add_object(display, &drag->drag_offer.object); |
||||
|
||||
target = pick_surface(device, &sx, &sy); |
||||
wl_drag_set_pointer_focus(drag, &target->surface, time, |
||||
device->x, device->y, sx, sy); |
||||
} |
||||
|
||||
static void |
||||
drag_destroy(struct wl_client *client, struct wl_drag *drag) |
||||
{ |
||||
wl_resource_destroy(&drag->resource, client); |
||||
} |
||||
|
||||
static const struct wl_drag_interface drag_interface = { |
||||
drag_offer, |
||||
drag_activate, |
||||
drag_destroy, |
||||
}; |
||||
|
||||
static void |
||||
drag_handle_surface_destroy(struct wl_listener *listener, |
||||
struct wl_surface *surface, uint32_t time) |
||||
{ |
||||
struct wl_drag *drag = |
||||
container_of(listener, struct wl_drag, drag_focus_listener); |
||||
|
||||
if (drag->drag_focus == surface) |
||||
wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0); |
||||
} |
||||
|
||||
static void |
||||
shell_create_drag(struct wl_client *client, |
||||
struct wl_shell *shell, uint32_t id) |
||||
{ |
||||
struct wl_drag *drag; |
||||
|
||||
drag = malloc(sizeof *drag); |
||||
if (drag == NULL) { |
||||
wl_client_post_no_memory(client); |
||||
return; |
||||
} |
||||
|
||||
memset(drag, 0, sizeof *drag); |
||||
drag->resource.object.id = id; |
||||
drag->resource.object.interface = &wl_drag_interface; |
||||
drag->resource.object.implementation = |
||||
(void (**)(void)) &drag_interface; |
||||
|
||||
drag->resource.destroy = destroy_drag; |
||||
|
||||
drag->drag_focus_listener.func = drag_handle_surface_destroy; |
||||
wl_list_init(&drag->drag_focus_listener.link); |
||||
|
||||
wl_client_add_resource(client, &drag->resource); |
||||
} |
||||
|
||||
void |
||||
wlsc_selection_set_focus(struct wl_selection *selection, |
||||
struct wl_surface *surface, uint32_t time) |
||||
{ |
||||
char **p, **end; |
||||
|
||||
if (selection->selection_focus == surface) |
||||
return; |
||||
|
||||
if (selection->selection_focus != NULL) |
||||
wl_client_post_event(selection->selection_focus->client, |
||||
&selection->selection_offer.object, |
||||
WL_SELECTION_OFFER_KEYBOARD_FOCUS, |
||||
NULL); |
||||
|
||||
if (surface) { |
||||
wl_client_post_global(surface->client, |
||||
&selection->selection_offer.object); |
||||
|
||||
end = selection->types.data + selection->types.size; |
||||
for (p = selection->types.data; p < end; p++) |
||||
wl_client_post_event(surface->client, |
||||
&selection->selection_offer.object, |
||||
WL_SELECTION_OFFER_OFFER, *p); |
||||
|
||||
wl_list_remove(&selection->selection_focus_listener.link); |
||||
wl_list_insert(surface->destroy_listener_list.prev, |
||||
&selection->selection_focus_listener.link); |
||||
|
||||
wl_client_post_event(surface->client, |
||||
&selection->selection_offer.object, |
||||
WL_SELECTION_OFFER_KEYBOARD_FOCUS, |
||||
selection->input_device); |
||||
} |
||||
|
||||
selection->selection_focus = surface; |
||||
|
||||
wl_list_remove(&selection->selection_focus_listener.link); |
||||
if (surface) |
||||
wl_list_insert(surface->destroy_listener_list.prev, |
||||
&selection->selection_focus_listener.link); |
||||
} |
||||
|
||||
static void |
||||
selection_offer_receive(struct wl_client *client, |
||||
struct wl_selection_offer *offer, |
||||
const char *mime_type, int fd) |
||||
{ |
||||
struct wl_selection *selection = |
||||
container_of(offer, struct wl_selection, selection_offer); |
||||
|
||||
wl_client_post_event(selection->client, |
||||
&selection->resource.object, |
||||
WL_SELECTION_SEND, mime_type, fd); |
||||
close(fd); |
||||
} |
||||
|
||||
static const struct wl_selection_offer_interface selection_offer_interface = { |
||||
selection_offer_receive |
||||
}; |
||||
|
||||
static void |
||||
selection_offer(struct wl_client *client, |
||||
struct wl_selection *selection, const char *type) |
||||
{ |
||||
char **p; |
||||
|
||||
p = wl_array_add(&selection->types, sizeof *p); |
||||
if (p) |
||||
*p = strdup(type); |
||||
if (!p || !*p) |
||||
wl_client_post_no_memory(client); |
||||
} |
||||
|
||||
static void |
||||
selection_activate(struct wl_client *client, |
||||
struct wl_selection *selection, |
||||
struct wl_input_device *device, uint32_t time) |
||||
{ |
||||
struct wlsc_input_device *wd = (struct wlsc_input_device *) device; |
||||
struct wl_display *display = wl_client_get_display (client); |
||||
|
||||
selection->input_device = device; |
||||
|
||||
selection->selection_offer.object.interface = |
||||
&wl_selection_offer_interface; |
||||
selection->selection_offer.object.implementation = |
||||
(void (**)(void)) &selection_offer_interface; |
||||
|
||||
wl_display_add_object(display, &selection->selection_offer.object); |
||||
|
||||
if (wd->selection) { |
||||
wl_client_post_event(wd->selection->client, |
||||
&wd->selection->resource.object, |
||||
WL_SELECTION_CANCELLED); |
||||
} |
||||
wd->selection = selection; |
||||
|
||||
wlsc_selection_set_focus(selection, device->keyboard_focus, time); |
||||
} |
||||
|
||||
static void |
||||
selection_destroy(struct wl_client *client, struct wl_selection *selection) |
||||
{ |
||||
wl_resource_destroy(&selection->resource, client); |
||||
} |
||||
|
||||
static const struct wl_selection_interface selection_interface = { |
||||
selection_offer, |
||||
selection_activate, |
||||
selection_destroy |
||||
}; |
||||
|
||||
static void |
||||
destroy_selection(struct wl_resource *resource, struct wl_client *client) |
||||
{ |
||||
struct wl_selection *selection = |
||||
container_of(resource, struct wl_selection, resource); |
||||
struct wlsc_input_device *wd = |
||||
(struct wlsc_input_device *) selection->input_device; |
||||
|
||||
if (wd && wd->selection == selection) { |
||||
wd->selection = NULL; |
||||
wlsc_selection_set_focus(selection, NULL, get_time()); |
||||
} |
||||
|
||||
wl_list_remove(&selection->selection_focus_listener.link); |
||||
free(selection); |
||||
} |
||||
|
||||
static void |
||||
selection_handle_surface_destroy(struct wl_listener *listener, |
||||
struct wl_surface *surface, uint32_t time) |
||||
{ |
||||
} |
||||
|
||||
static void |
||||
shell_create_selection(struct wl_client *client, |
||||
struct wl_shell *shell, uint32_t id) |
||||
{ |
||||
struct wl_selection *selection; |
||||
|
||||
selection = malloc(sizeof *selection); |
||||
if (selection == NULL) { |
||||
wl_client_post_no_memory(client); |
||||
return; |
||||
} |
||||
|
||||
memset(selection, 0, sizeof *selection); |
||||
selection->resource.object.id = id; |
||||
selection->resource.object.interface = &wl_selection_interface; |
||||
selection->resource.object.implementation = |
||||
(void (**)(void)) &selection_interface; |
||||
|
||||
selection->client = client; |
||||
selection->resource.destroy = destroy_selection; |
||||
selection->selection_focus = NULL; |
||||
|
||||
selection->selection_focus_listener.func = |
||||
selection_handle_surface_destroy; |
||||
wl_list_init(&selection->selection_focus_listener.link); |
||||
|
||||
wl_client_add_resource(client, &selection->resource); |
||||
} |
||||
|
||||
const static struct wl_shell_interface shell_interface = { |
||||
shell_move, |
||||
shell_resize, |
||||
shell_create_drag, |
||||
shell_create_selection |
||||
}; |
||||
|
||||
int |
||||
wlsc_shell_init(struct wlsc_compositor *ec) |
||||
{ |
||||
struct wl_shell *shell = &ec->shell; |
||||
|
||||
shell->object.interface = &wl_shell_interface; |
||||
shell->object.implementation = (void (**)(void)) &shell_interface; |
||||
wl_display_add_object(ec->wl_display, &shell->object); |
||||
if (wl_display_add_global(ec->wl_display, &shell->object, NULL)) |
||||
return -1; |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,163 @@ |
||||
/*
|
||||
* Copyright © 2010 Intel Corporation |
||||
* |
||||
* 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 <termios.h> |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <unistd.h> |
||||
#include <fcntl.h> |
||||
#include <signal.h> |
||||
#include <linux/kd.h> |
||||
#include <linux/vt.h> |
||||
#include <sys/ioctl.h> |
||||
|
||||
#include "compositor.h" |
||||
|
||||
struct tty { |
||||
struct wlsc_compositor *compositor; |
||||
int fd; |
||||
struct termios terminal_attributes; |
||||
|
||||
struct wl_event_source *input_source; |
||||
struct wl_event_source *enter_vt_source; |
||||
struct wl_event_source *leave_vt_source; |
||||
}; |
||||
|
||||
static void on_enter_vt(int signal_number, void *data) |
||||
{ |
||||
struct tty *tty = data; |
||||
int ret; |
||||
|
||||
fprintf(stderr, "enter vt\n"); |
||||
|
||||
ioctl(tty->fd, VT_RELDISP, VT_ACKACQ); |
||||
ret = ioctl(tty->fd, KDSETMODE, KD_GRAPHICS); |
||||
if (ret) |
||||
fprintf(stderr, "failed to set KD_GRAPHICS mode on console: %m\n"); |
||||
tty->compositor->focus = 1; |
||||
} |
||||
|
||||
static void on_leave_vt(int signal_number, void *data) |
||||
{ |
||||
struct tty *tty = data; |
||||
int ret; |
||||
|
||||
ioctl (tty->fd, VT_RELDISP, 1); |
||||
ret = ioctl(tty->fd, KDSETMODE, KD_TEXT); |
||||
if (ret) |
||||
fprintf(stderr, |
||||
"failed to set KD_TEXT mode on console: %m\n"); |
||||
|
||||
tty->compositor->focus = 0; |
||||
} |
||||
|
||||
static void |
||||
on_tty_input(int fd, uint32_t mask, void *data) |
||||
{ |
||||
struct tty *tty = data; |
||||
|
||||
/* Ignore input to tty. We get keyboard events from evdev
|
||||
*/ |
||||
tcflush(tty->fd, TCIFLUSH); |
||||
} |
||||
|
||||
struct tty * |
||||
tty_create(struct wlsc_compositor *compositor) |
||||
{ |
||||
struct termios raw_attributes; |
||||
struct vt_mode mode = { 0 }; |
||||
int ret; |
||||
struct tty *tty; |
||||
struct wl_event_loop *loop; |
||||
|
||||
tty = malloc(sizeof *tty); |
||||
if (tty == NULL) |
||||
return NULL; |
||||
|
||||
memset(tty, 0, sizeof *tty); |
||||
tty->compositor = compositor; |
||||
tty->fd = open("/dev/tty0", O_RDWR | O_NOCTTY); |
||||
if (tty->fd <= 0) { |
||||
fprintf(stderr, "failed to open active tty: %m\n"); |
||||
return NULL; |
||||
} |
||||
|
||||
if (tcgetattr(tty->fd, &tty->terminal_attributes) < 0) { |
||||
fprintf(stderr, "could not get terminal attributes: %m\n"); |
||||
return NULL; |
||||
} |
||||
|
||||
/* Ignore control characters and disable echo */ |
||||
raw_attributes = tty->terminal_attributes; |
||||
cfmakeraw(&raw_attributes); |
||||
|
||||
/* Fix up line endings to be normal (cfmakeraw hoses them) */ |
||||
raw_attributes.c_oflag |= OPOST | OCRNL; |
||||
|
||||
if (tcsetattr(tty->fd, TCSANOW, &raw_attributes) < 0) |
||||
fprintf(stderr, "could not put terminal into raw mode: %m\n"); |
||||
|
||||
loop = wl_display_get_event_loop(compositor->wl_display); |
||||
tty->input_source = |
||||
wl_event_loop_add_fd(loop, tty->fd, |
||||
WL_EVENT_READABLE, on_tty_input, tty); |
||||
|
||||
ret = ioctl(tty->fd, KDSETMODE, KD_GRAPHICS); |
||||
if (ret) { |
||||
fprintf(stderr, "failed to set KD_GRAPHICS mode on tty: %m\n"); |
||||
return NULL; |
||||
} |
||||
|
||||
tty->compositor->focus = 1; |
||||
mode.mode = VT_PROCESS; |
||||
mode.relsig = SIGUSR1; |
||||
mode.acqsig = SIGUSR2; |
||||
if (!ioctl(tty->fd, VT_SETMODE, &mode) < 0) { |
||||
fprintf(stderr, "failed to take control of vt handling\n"); |
||||
return NULL; |
||||
} |
||||
|
||||
tty->leave_vt_source = |
||||
wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, tty); |
||||
tty->enter_vt_source = |
||||
wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, tty); |
||||
|
||||
return tty; |
||||
} |
||||
|
||||
void |
||||
tty_destroy(struct tty *tty) |
||||
{ |
||||
int ret; |
||||
|
||||
ret = ioctl(tty->fd, KDSETMODE, KD_TEXT); |
||||
if (ret) |
||||
fprintf(stderr, |
||||
"failed to set KD_GRAPHICS mode on tty: %m\n"); |
||||
|
||||
if (tcsetattr(tty->fd, TCSANOW, &tty->terminal_attributes) < 0) |
||||
fprintf(stderr, |
||||
"could not restore terminal to canonical mode\n"); |
||||
|
||||
free(tty); |
||||
} |
Loading…
Reference in new issue