OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/exo/wayland/server.h" | 5 #include "components/exo/wayland/server.h" |
6 | 6 |
7 #include <grp.h> | 7 #include <grp.h> |
8 #include <linux/input.h> | 8 #include <linux/input.h> |
9 #include <stddef.h> | 9 #include <stddef.h> |
10 #include <stdint.h> | 10 #include <stdint.h> |
11 #include <viewporter-server-protocol.h> | 11 #include <viewporter-server-protocol.h> |
12 #include <wayland-server-core.h> | 12 #include <wayland-server-core.h> |
13 #include <wayland-server-protocol-core.h> | 13 #include <wayland-server-protocol-core.h> |
14 | 14 |
15 // Note: core wayland headers need to be included before protocol headers. | 15 // Note: core wayland headers need to be included before protocol headers. |
16 #include <alpha-compositing-unstable-v1-server-protocol.h> // NOLINT | 16 #include <alpha-compositing-unstable-v1-server-protocol.h> // NOLINT |
| 17 #include <gaming-input-unstable-v1-server-protocol.h> // NOLINT |
17 #include <remote-shell-unstable-v1-server-protocol.h> // NOLINT | 18 #include <remote-shell-unstable-v1-server-protocol.h> // NOLINT |
18 #include <secure-output-unstable-v1-server-protocol.h> // NOLINT | 19 #include <secure-output-unstable-v1-server-protocol.h> // NOLINT |
19 #include <xdg-shell-unstable-v5-server-protocol.h> // NOLINT | 20 #include <xdg-shell-unstable-v5-server-protocol.h> // NOLINT |
20 #include <vsync-feedback-unstable-v1-server-protocol.h> // NOLINT | 21 #include <vsync-feedback-unstable-v1-server-protocol.h> // NOLINT |
21 | 22 |
22 #include <algorithm> | 23 #include <algorithm> |
23 #include <cstdlib> | 24 #include <cstdlib> |
24 #include <iterator> | 25 #include <iterator> |
25 #include <string> | 26 #include <string> |
26 #include <utility> | 27 #include <utility> |
27 | 28 |
28 #include "ash/common/display/display_info.h" | 29 #include "ash/common/display/display_info.h" |
29 #include "ash/common/shell_observer.h" | 30 #include "ash/common/shell_observer.h" |
30 #include "ash/common/shell_window_ids.h" | 31 #include "ash/common/shell_window_ids.h" |
31 #include "ash/common/wm_shell.h" | 32 #include "ash/common/wm_shell.h" |
32 #include "ash/display/display_manager.h" | 33 #include "ash/display/display_manager.h" |
33 #include "ash/shell.h" | 34 #include "ash/shell.h" |
34 #include "base/bind.h" | 35 #include "base/bind.h" |
35 #include "base/cancelable_callback.h" | 36 #include "base/cancelable_callback.h" |
36 #include "base/files/file_path.h" | 37 #include "base/files/file_path.h" |
37 #include "base/macros.h" | 38 #include "base/macros.h" |
38 #include "base/memory/free_deleter.h" | 39 #include "base/memory/free_deleter.h" |
39 #include "base/memory/ptr_util.h" | 40 #include "base/memory/ptr_util.h" |
40 #include "base/memory/weak_ptr.h" | 41 #include "base/memory/weak_ptr.h" |
41 #include "base/strings/stringprintf.h" | 42 #include "base/strings/stringprintf.h" |
42 #include "base/strings/utf_string_conversions.h" | 43 #include "base/strings/utf_string_conversions.h" |
| 44 #include "base/threading/thread.h" |
43 #include "base/threading/thread_task_runner_handle.h" | 45 #include "base/threading/thread_task_runner_handle.h" |
44 #include "components/exo/buffer.h" | 46 #include "components/exo/buffer.h" |
45 #include "components/exo/display.h" | 47 #include "components/exo/display.h" |
| 48 #include "components/exo/gamepad.h" |
| 49 #include "components/exo/gamepad_delegate.h" |
46 #include "components/exo/keyboard.h" | 50 #include "components/exo/keyboard.h" |
47 #include "components/exo/keyboard_delegate.h" | 51 #include "components/exo/keyboard_delegate.h" |
48 #include "components/exo/notification_surface.h" | 52 #include "components/exo/notification_surface.h" |
49 #include "components/exo/notification_surface_manager.h" | 53 #include "components/exo/notification_surface_manager.h" |
50 #include "components/exo/pointer.h" | 54 #include "components/exo/pointer.h" |
51 #include "components/exo/pointer_delegate.h" | 55 #include "components/exo/pointer_delegate.h" |
52 #include "components/exo/shared_memory.h" | 56 #include "components/exo/shared_memory.h" |
53 #include "components/exo/shell_surface.h" | 57 #include "components/exo/shell_surface.h" |
54 #include "components/exo/sub_surface.h" | 58 #include "components/exo/sub_surface.h" |
55 #include "components/exo/surface.h" | 59 #include "components/exo/surface.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 DestroyUserData<T>); | 128 DestroyUserData<T>); |
125 } | 129 } |
126 | 130 |
127 // Convert a timestamp to a time value that can be used when interfacing | 131 // Convert a timestamp to a time value that can be used when interfacing |
128 // with wayland. Note that we cast a int64_t value to uint32_t which can | 132 // with wayland. Note that we cast a int64_t value to uint32_t which can |
129 // potentially overflow. | 133 // potentially overflow. |
130 uint32_t TimeTicksToMilliseconds(base::TimeTicks ticks) { | 134 uint32_t TimeTicksToMilliseconds(base::TimeTicks ticks) { |
131 return (ticks - base::TimeTicks()).InMilliseconds(); | 135 return (ticks - base::TimeTicks()).InMilliseconds(); |
132 } | 136 } |
133 | 137 |
| 138 uint32_t NowInMilliseconds() { |
| 139 return TimeTicksToMilliseconds(base::TimeTicks::Now()); |
| 140 } |
| 141 |
134 // A property key containing the surface resource that is associated with | 142 // A property key containing the surface resource that is associated with |
135 // window. If unset, no surface resource is associated with window. | 143 // window. If unset, no surface resource is associated with window. |
136 DEFINE_SURFACE_PROPERTY_KEY(wl_resource*, kSurfaceResourceKey, nullptr); | 144 DEFINE_SURFACE_PROPERTY_KEY(wl_resource*, kSurfaceResourceKey, nullptr); |
137 | 145 |
138 // A property key containing a boolean set to true if a viewport is associated | 146 // A property key containing a boolean set to true if a viewport is associated |
139 // with window. | 147 // with window. |
140 DEFINE_SURFACE_PROPERTY_KEY(bool, kSurfaceHasViewportKey, false); | 148 DEFINE_SURFACE_PROPERTY_KEY(bool, kSurfaceHasViewportKey, false); |
141 | 149 |
142 // A property key containing a boolean set to true if a security object is | 150 // A property key containing a boolean set to true if a security object is |
143 // associated with window. | 151 // associated with window. |
(...skipping 2643 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2787 void* data, | 2795 void* data, |
2788 uint32_t version, | 2796 uint32_t version, |
2789 uint32_t id) { | 2797 uint32_t id) { |
2790 wl_resource* resource = | 2798 wl_resource* resource = |
2791 wl_resource_create(client, &zwp_alpha_compositing_v1_interface, 1, id); | 2799 wl_resource_create(client, &zwp_alpha_compositing_v1_interface, 1, id); |
2792 | 2800 |
2793 wl_resource_set_implementation(resource, &alpha_compositing_implementation, | 2801 wl_resource_set_implementation(resource, &alpha_compositing_implementation, |
2794 data, nullptr); | 2802 data, nullptr); |
2795 } | 2803 } |
2796 | 2804 |
| 2805 //////////////////////////////////////////////////////////////////////////////// |
| 2806 // gaming_input_interface: |
| 2807 |
| 2808 // Gamepad delegate class that forwards gamepad events to the client resource. |
| 2809 class WaylandGamepadDelegate : public GamepadDelegate { |
| 2810 public: |
| 2811 explicit WaylandGamepadDelegate(wl_resource* gamepad_resource) |
| 2812 : gamepad_resource_(gamepad_resource) {} |
| 2813 |
| 2814 // Overridden from GamepadDelegate: |
| 2815 void OnGamepadDestroying(Gamepad* gamepad) override { delete this; } |
| 2816 bool CanAcceptGamepadEventsForSurface(Surface* surface) const override { |
| 2817 wl_resource* surface_resource = GetSurfaceResource(surface); |
| 2818 return surface_resource && |
| 2819 wl_resource_get_client(surface_resource) == client(); |
| 2820 } |
| 2821 void OnStateChange(bool connected) override { |
| 2822 uint32_t status = connected ? ZWP_GAMEPAD_V1_GAMEPAD_STATE_ON |
| 2823 : ZWP_GAMEPAD_V1_GAMEPAD_STATE_OFF; |
| 2824 zwp_gamepad_v1_send_state_change(gamepad_resource_, status); |
| 2825 wl_client_flush(client()); |
| 2826 } |
| 2827 void OnAxis(int axis, double value) override { |
| 2828 zwp_gamepad_v1_send_axis(gamepad_resource_, NowInMilliseconds(), axis, |
| 2829 wl_fixed_from_double(value)); |
| 2830 } |
| 2831 void OnButton(int button, bool pressed, double value) override { |
| 2832 uint32_t state = pressed ? ZWP_GAMEPAD_V1_BUTTON_STATE_PRESSED |
| 2833 : ZWP_GAMEPAD_V1_BUTTON_STATE_RELEASED; |
| 2834 zwp_gamepad_v1_send_button(gamepad_resource_, NowInMilliseconds(), button, |
| 2835 state, wl_fixed_from_double(value)); |
| 2836 } |
| 2837 void OnFrame() override { |
| 2838 zwp_gamepad_v1_send_frame(gamepad_resource_, NowInMilliseconds()); |
| 2839 wl_client_flush(client()); |
| 2840 } |
| 2841 |
| 2842 private: |
| 2843 // The client who own this gamepad instance. |
| 2844 wl_client* client() const { |
| 2845 return wl_resource_get_client(gamepad_resource_); |
| 2846 } |
| 2847 |
| 2848 // The gamepad resource associated with the gamepad. |
| 2849 wl_resource* const gamepad_resource_; |
| 2850 |
| 2851 DISALLOW_COPY_AND_ASSIGN(WaylandGamepadDelegate); |
| 2852 }; |
| 2853 |
| 2854 void gamepad_destroy(wl_client* client, wl_resource* resource) { |
| 2855 wl_resource_destroy(resource); |
| 2856 } |
| 2857 |
| 2858 const struct zwp_gamepad_v1_interface gamepad_implementation = { |
| 2859 gamepad_destroy}; |
| 2860 |
| 2861 void gaming_input_get_gamepad(wl_client* client, |
| 2862 wl_resource* resource, |
| 2863 uint32_t id, |
| 2864 wl_resource* seat) { |
| 2865 wl_resource* gamepad_resource = wl_resource_create( |
| 2866 client, &zwp_gamepad_v1_interface, wl_resource_get_version(resource), id); |
| 2867 |
| 2868 base::Thread* gaming_input_thread = GetUserDataAs<base::Thread>(resource); |
| 2869 |
| 2870 SetImplementation( |
| 2871 gamepad_resource, &gamepad_implementation, |
| 2872 base::WrapUnique(new Gamepad(new WaylandGamepadDelegate(gamepad_resource), |
| 2873 gaming_input_thread->task_runner().get()))); |
| 2874 } |
| 2875 |
| 2876 const struct zwp_gaming_input_v1_interface gaming_input_implementation = { |
| 2877 gaming_input_get_gamepad}; |
| 2878 |
| 2879 void bind_gaming_input(wl_client* client, |
| 2880 void* data, |
| 2881 uint32_t version, |
| 2882 uint32_t id) { |
| 2883 wl_resource* resource = |
| 2884 wl_resource_create(client, &zwp_gaming_input_v1_interface, version, id); |
| 2885 |
| 2886 std::unique_ptr<base::Thread> gaming_input_thread( |
| 2887 new base::Thread("Exo gaming input polling thread.")); |
| 2888 gaming_input_thread->StartWithOptions( |
| 2889 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
| 2890 |
| 2891 SetImplementation(resource, &gaming_input_implementation, |
| 2892 std::move(gaming_input_thread)); |
| 2893 } |
| 2894 |
2797 } // namespace | 2895 } // namespace |
2798 | 2896 |
2799 //////////////////////////////////////////////////////////////////////////////// | 2897 //////////////////////////////////////////////////////////////////////////////// |
2800 // Server, public: | 2898 // Server, public: |
2801 | 2899 |
2802 Server::Server(Display* display) | 2900 Server::Server(Display* display) |
2803 : display_(display), wl_display_(wl_display_create()) { | 2901 : display_(display), wl_display_(wl_display_create()) { |
2804 wl_global_create(wl_display_.get(), &wl_compositor_interface, | 2902 wl_global_create(wl_display_.get(), &wl_compositor_interface, |
2805 compositor_version, display_, bind_compositor); | 2903 compositor_version, display_, bind_compositor); |
2806 wl_global_create(wl_display_.get(), &wl_shm_interface, 1, display_, bind_shm); | 2904 wl_global_create(wl_display_.get(), &wl_shm_interface, 1, display_, bind_shm); |
(...skipping 18 matching lines...) Expand all Loading... |
2825 wl_global_create(wl_display_.get(), &wl_seat_interface, seat_version, | 2923 wl_global_create(wl_display_.get(), &wl_seat_interface, seat_version, |
2826 display_, bind_seat); | 2924 display_, bind_seat); |
2827 wl_global_create(wl_display_.get(), &wp_viewporter_interface, 1, display_, | 2925 wl_global_create(wl_display_.get(), &wp_viewporter_interface, 1, display_, |
2828 bind_viewporter); | 2926 bind_viewporter); |
2829 wl_global_create(wl_display_.get(), &zwp_secure_output_v1_interface, 1, | 2927 wl_global_create(wl_display_.get(), &zwp_secure_output_v1_interface, 1, |
2830 display_, bind_secure_output); | 2928 display_, bind_secure_output); |
2831 wl_global_create(wl_display_.get(), &zwp_alpha_compositing_v1_interface, 1, | 2929 wl_global_create(wl_display_.get(), &zwp_alpha_compositing_v1_interface, 1, |
2832 display_, bind_alpha_compositing); | 2930 display_, bind_alpha_compositing); |
2833 wl_global_create(wl_display_.get(), &zwp_remote_shell_v1_interface, | 2931 wl_global_create(wl_display_.get(), &zwp_remote_shell_v1_interface, |
2834 remote_shell_version, display_, bind_remote_shell); | 2932 remote_shell_version, display_, bind_remote_shell); |
| 2933 wl_global_create(wl_display_.get(), &zwp_gaming_input_v1_interface, 1, |
| 2934 display_, bind_gaming_input); |
2835 } | 2935 } |
2836 | 2936 |
2837 Server::~Server() {} | 2937 Server::~Server() {} |
2838 | 2938 |
2839 // static | 2939 // static |
2840 std::unique_ptr<Server> Server::Create(Display* display) { | 2940 std::unique_ptr<Server> Server::Create(Display* display) { |
2841 std::unique_ptr<Server> server(new Server(display)); | 2941 std::unique_ptr<Server> server(new Server(display)); |
2842 | 2942 |
2843 char* runtime_dir = getenv("XDG_RUNTIME_DIR"); | 2943 char* runtime_dir = getenv("XDG_RUNTIME_DIR"); |
2844 if (!runtime_dir) { | 2944 if (!runtime_dir) { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2896 DCHECK(event_loop); | 2996 DCHECK(event_loop); |
2897 wl_event_loop_dispatch(event_loop, timeout.InMilliseconds()); | 2997 wl_event_loop_dispatch(event_loop, timeout.InMilliseconds()); |
2898 } | 2998 } |
2899 | 2999 |
2900 void Server::Flush() { | 3000 void Server::Flush() { |
2901 wl_display_flush_clients(wl_display_.get()); | 3001 wl_display_flush_clients(wl_display_.get()); |
2902 } | 3002 } |
2903 | 3003 |
2904 } // namespace wayland | 3004 } // namespace wayland |
2905 } // namespace exo | 3005 } // namespace exo |
OLD | NEW |