1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "host/libs/wayland/wayland_seat.h"
18 
19 #include <android-base/logging.h>
20 
21 #include <wayland-server-core.h>
22 #include <wayland-server-protocol.h>
23 
24 namespace wayland {
25 namespace {
26 
pointer_set_cursor(wl_client *,wl_resource * pointer,uint32_t serial,wl_resource * surface,int32_t x,int32_t y)27 void pointer_set_cursor(wl_client*,
28                         wl_resource* pointer,
29                         uint32_t serial,
30                         wl_resource* surface,
31                         int32_t x,
32                         int32_t y) {
33   LOG(VERBOSE) << __FUNCTION__
34                << " pointer=" << pointer
35                << " serial=" << serial
36                << " surface=" << surface
37                << " x=" << x
38                << " y=" << y;
39 }
40 
pointer_release(wl_client *,wl_resource * pointer)41 void pointer_release(wl_client*, wl_resource* pointer) {
42   LOG(VERBOSE) << __FUNCTION__
43                << " pointer=" << pointer;
44 
45   wl_resource_destroy(pointer);
46 }
47 
48 const struct wl_pointer_interface pointer_implementation = {
49   .set_cursor = pointer_set_cursor,
50   .release = pointer_release
51 };
52 
keyboard_release(wl_client *,wl_resource * keyboard)53 void keyboard_release(wl_client*, wl_resource* keyboard) {
54   LOG(VERBOSE) << __FUNCTION__
55                << " keyboard=" << keyboard;
56 
57   wl_resource_destroy(keyboard);
58 }
59 
60 const struct wl_keyboard_interface keyboard_implementation = {
61   .release = keyboard_release
62 };
63 
touch_release(wl_client *,wl_resource * touch)64 void touch_release(wl_client*, wl_resource* touch) {
65   LOG(VERBOSE) << __FUNCTION__
66                << " touch=" << touch;
67 
68   wl_resource_destroy(touch);
69 }
70 
71 const struct wl_touch_interface touch_implementation = {
72   .release = touch_release
73 };
74 
pointer_destroy_resource_callback(struct wl_resource *)75 void pointer_destroy_resource_callback(struct wl_resource*) {}
76 
seat_get_pointer(wl_client * client,wl_resource * seat,uint32_t id)77 void seat_get_pointer(wl_client* client, wl_resource* seat, uint32_t id) {
78   LOG(VERBOSE) << __FUNCTION__
79                << " seat=" << seat
80                << " id=" << id;
81 
82   wl_resource* pointer_resource =
83       wl_resource_create(client, &wl_pointer_interface,
84                          wl_resource_get_version(seat), id);
85 
86   wl_resource_set_implementation(pointer_resource, &pointer_implementation,
87                                  nullptr, pointer_destroy_resource_callback);
88 }
89 
keyboard_destroy_resource_callback(struct wl_resource *)90 void keyboard_destroy_resource_callback(struct wl_resource*) {}
91 
seat_get_keyboard(wl_client * client,wl_resource * seat,uint32_t id)92 void seat_get_keyboard(wl_client* client, wl_resource* seat, uint32_t id) {
93   LOG(VERBOSE) << __FUNCTION__
94                << " seat=" << seat
95                << " id=" << id;
96 
97   wl_resource* keyboard_resource =
98       wl_resource_create(client, &wl_keyboard_interface,
99                          wl_resource_get_version(seat), id);
100 
101   wl_resource_set_implementation(keyboard_resource, &keyboard_implementation,
102                                  nullptr, keyboard_destroy_resource_callback);
103 }
104 
touch_destroy_resource_callback(struct wl_resource *)105 void touch_destroy_resource_callback(struct wl_resource*) {}
106 
seat_get_touch(wl_client * client,wl_resource * seat,uint32_t id)107 void seat_get_touch(wl_client* client, wl_resource* seat, uint32_t id) {
108   LOG(VERBOSE) << __FUNCTION__
109                << " seat=" << seat
110                << " id=" << id;
111 
112   wl_resource* touch_resource =
113       wl_resource_create(client, &wl_touch_interface,
114                          wl_resource_get_version(seat), id);
115 
116   wl_resource_set_implementation(touch_resource, &touch_implementation, nullptr,
117                                  touch_destroy_resource_callback);
118 }
119 
seat_release(wl_client *,wl_resource * resource)120 void seat_release(wl_client*, wl_resource* resource) {
121   LOG(VERBOSE) << __FUNCTION__
122                << " resource=" << resource;
123 
124   wl_resource_destroy(resource);
125 }
126 
127 constexpr const uint32_t kSeatVersion = 6;
128 
129 const struct wl_seat_interface seat_implementation = {
130     .get_pointer = seat_get_pointer,
131     .get_keyboard = seat_get_keyboard,
132     .get_touch = seat_get_touch,
133     .release = seat_release
134 };
135 
bind_seat(wl_client * client,void * data,uint32_t version,uint32_t id)136 void bind_seat(wl_client* client, void* data, uint32_t version, uint32_t id) {
137   wl_resource* resource =
138       wl_resource_create(client, &wl_seat_interface,
139                          std::min(version, kSeatVersion), id);
140 
141   wl_resource_set_implementation(resource, &seat_implementation, data, nullptr);
142 }
143 
144 }  // namespace
145 
BindSeatInterface(wl_display * display)146 void BindSeatInterface(wl_display* display) {
147   wl_global_create(display, &wl_seat_interface, kSeatVersion, nullptr,
148                    bind_seat);
149 }
150 
151 }  // namespace wayland