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/strings/stringprintf.h" | 41 #include "base/strings/stringprintf.h" |
41 #include "base/strings/utf_string_conversions.h" | 42 #include "base/strings/utf_string_conversions.h" |
43 #include "base/threading/thread.h" | |
42 #include "components/exo/buffer.h" | 44 #include "components/exo/buffer.h" |
43 #include "components/exo/display.h" | 45 #include "components/exo/display.h" |
46 #include "components/exo/gamepad.h" | |
47 #include "components/exo/gamepad_delegate.h" | |
44 #include "components/exo/keyboard.h" | 48 #include "components/exo/keyboard.h" |
45 #include "components/exo/keyboard_delegate.h" | 49 #include "components/exo/keyboard_delegate.h" |
46 #include "components/exo/notification_surface.h" | 50 #include "components/exo/notification_surface.h" |
47 #include "components/exo/notification_surface_manager.h" | 51 #include "components/exo/notification_surface_manager.h" |
48 #include "components/exo/pointer.h" | 52 #include "components/exo/pointer.h" |
49 #include "components/exo/pointer_delegate.h" | 53 #include "components/exo/pointer_delegate.h" |
50 #include "components/exo/shared_memory.h" | 54 #include "components/exo/shared_memory.h" |
51 #include "components/exo/shell_surface.h" | 55 #include "components/exo/shell_surface.h" |
52 #include "components/exo/sub_surface.h" | 56 #include "components/exo/sub_surface.h" |
53 #include "components/exo/surface.h" | 57 #include "components/exo/surface.h" |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
118 DestroyUserData<T>); | 122 DestroyUserData<T>); |
119 } | 123 } |
120 | 124 |
121 // Convert a timestamp to a time value that can be used when interfacing | 125 // Convert a timestamp to a time value that can be used when interfacing |
122 // with wayland. Note that we cast a int64_t value to uint32_t which can | 126 // with wayland. Note that we cast a int64_t value to uint32_t which can |
123 // potentially overflow. | 127 // potentially overflow. |
124 uint32_t TimeTicksToMilliseconds(base::TimeTicks ticks) { | 128 uint32_t TimeTicksToMilliseconds(base::TimeTicks ticks) { |
125 return (ticks - base::TimeTicks()).InMilliseconds(); | 129 return (ticks - base::TimeTicks()).InMilliseconds(); |
126 } | 130 } |
127 | 131 |
132 uint32_t NowInMilliseconds() { | |
133 return TimeTicksToMilliseconds(base::TimeTicks::Now()); | |
134 } | |
135 | |
128 // A property key containing the surface resource that is associated with | 136 // A property key containing the surface resource that is associated with |
129 // window. If unset, no surface resource is associated with window. | 137 // window. If unset, no surface resource is associated with window. |
130 DEFINE_SURFACE_PROPERTY_KEY(wl_resource*, kSurfaceResourceKey, nullptr); | 138 DEFINE_SURFACE_PROPERTY_KEY(wl_resource*, kSurfaceResourceKey, nullptr); |
131 | 139 |
132 // A property key containing a boolean set to true if a viewport is associated | 140 // A property key containing a boolean set to true if a viewport is associated |
133 // with window. | 141 // with window. |
134 DEFINE_SURFACE_PROPERTY_KEY(bool, kSurfaceHasViewportKey, false); | 142 DEFINE_SURFACE_PROPERTY_KEY(bool, kSurfaceHasViewportKey, false); |
135 | 143 |
136 // A property key containing a boolean set to true if a security object is | 144 // A property key containing a boolean set to true if a security object is |
137 // associated with window. | 145 // associated with window. |
(...skipping 2622 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2760 void* data, | 2768 void* data, |
2761 uint32_t version, | 2769 uint32_t version, |
2762 uint32_t id) { | 2770 uint32_t id) { |
2763 wl_resource* resource = | 2771 wl_resource* resource = |
2764 wl_resource_create(client, &zwp_alpha_compositing_v1_interface, 1, id); | 2772 wl_resource_create(client, &zwp_alpha_compositing_v1_interface, 1, id); |
2765 | 2773 |
2766 wl_resource_set_implementation(resource, &alpha_compositing_implementation, | 2774 wl_resource_set_implementation(resource, &alpha_compositing_implementation, |
2767 data, nullptr); | 2775 data, nullptr); |
2768 } | 2776 } |
2769 | 2777 |
2778 //////////////////////////////////////////////////////////////////////////////// | |
2779 // gaming_input_interface: | |
2780 | |
2781 // Gamepad delegate class that forwards gamepad events to the client resource. | |
2782 class WaylandGamepadDelegate : public GamepadDelegate { | |
2783 public: | |
2784 explicit WaylandGamepadDelegate(wl_resource* gamepad_resource) | |
2785 : gamepad_resource_(gamepad_resource) {} | |
2786 | |
2787 // Overridden from GamepadDelegate: | |
2788 void OnGamepadDestroying(Gamepad* gamepad) override { delete this; } | |
2789 bool CanAcceptGamepadEventsForSurface(Surface* surface) const override { | |
2790 wl_resource* surface_resource = GetSurfaceResource(surface); | |
2791 return surface_resource && | |
2792 wl_resource_get_client(surface_resource) == client(); | |
2793 } | |
2794 void OnStateChange(bool connected) override { | |
2795 uint32_t status = connected ? ZWP_GAMEPAD_V1_GAMEPAD_STATE_ON | |
2796 : ZWP_GAMEPAD_V1_GAMEPAD_STATE_OFF; | |
2797 zwp_gamepad_v1_send_state_change(gamepad_resource_, status); | |
2798 wl_client_flush(client()); | |
2799 } | |
2800 void OnAxis(int axis, double value) override { | |
2801 zwp_gamepad_v1_send_axis(gamepad_resource_, NowInMilliseconds(), axis, | |
2802 wl_fixed_from_double(value)); | |
2803 } | |
2804 void OnButton(int button, bool pressed, double value) override { | |
2805 uint32_t state = pressed ? ZWP_GAMEPAD_V1_BUTTON_STATE_PRESSED | |
2806 : ZWP_GAMEPAD_V1_BUTTON_STATE_RELEASED; | |
2807 zwp_gamepad_v1_send_button(gamepad_resource_, NowInMilliseconds(), button, | |
2808 state, wl_fixed_from_double(value)); | |
2809 } | |
2810 void OnFrame() override { | |
2811 zwp_gamepad_v1_send_frame(gamepad_resource_, NowInMilliseconds()); | |
2812 wl_client_flush(client()); | |
2813 } | |
2814 | |
2815 private: | |
2816 // The client who own this gamepad instance. | |
2817 wl_client* client() const { | |
2818 return wl_resource_get_client(gamepad_resource_); | |
2819 } | |
2820 | |
2821 // The gamepad resource associated with the gamepad. | |
2822 wl_resource* const gamepad_resource_; | |
2823 | |
2824 DISALLOW_COPY_AND_ASSIGN(WaylandGamepadDelegate); | |
2825 }; | |
2826 | |
2827 void gamepad_destroy(wl_client* client, wl_resource* resource) { | |
2828 wl_resource_destroy(resource); | |
2829 } | |
2830 | |
2831 const struct zwp_gamepad_v1_interface gamepad_implementation = { | |
2832 gamepad_destroy}; | |
2833 | |
2834 void gaming_input_get_gamepad(wl_client* client, | |
2835 wl_resource* resource, | |
2836 uint32_t id, | |
2837 wl_resource* seat) { | |
2838 wl_resource* gamepad_resource = wl_resource_create( | |
2839 client, &zwp_gamepad_v1_interface, wl_resource_get_version(resource), id); | |
2840 | |
2841 base::Thread* gamepad_thread = GetUserDataAs<base::Thread>(resource); | |
reveman
2016/07/11 10:50:45
nit: gaming_input_thread
denniskempin
2016/07/11 17:43:29
Done.
| |
2842 | |
2843 SetImplementation( | |
2844 gamepad_resource, &gamepad_implementation, | |
2845 base::WrapUnique(new Gamepad(new WaylandGamepadDelegate(gamepad_resource), | |
2846 gamepad_thread->task_runner()))); | |
2847 } | |
2848 | |
2849 const struct zwp_gaming_input_v1_interface gaming_input_implementation = { | |
2850 gaming_input_get_gamepad}; | |
2851 | |
2852 void bind_gaming_input(wl_client* client, | |
2853 void* data, | |
2854 uint32_t version, | |
2855 uint32_t id) { | |
2856 wl_resource* resource = | |
2857 wl_resource_create(client, &zwp_gaming_input_v1_interface, version, id); | |
2858 | |
2859 std::unique_ptr<base::Thread> gamepad_thread( | |
reveman
2016/07/11 10:50:45
nit: gaming_input_thread
denniskempin
2016/07/11 17:43:29
Done.
| |
2860 new base::Thread("Exo gamepad polling thread.")); | |
reveman
2016/07/11 10:50:45
nit: "Exo gaming input polling thread."
denniskempin
2016/07/11 17:43:29
Done.
| |
2861 gamepad_thread->StartWithOptions( | |
2862 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); | |
2863 | |
2864 SetImplementation(resource, &gaming_input_implementation, | |
2865 std::move(gamepad_thread)); | |
2866 } | |
2867 | |
2770 } // namespace | 2868 } // namespace |
2771 | 2869 |
2772 //////////////////////////////////////////////////////////////////////////////// | 2870 //////////////////////////////////////////////////////////////////////////////// |
2773 // Server, public: | 2871 // Server, public: |
2774 | 2872 |
2775 Server::Server(Display* display) | 2873 Server::Server(Display* display) |
2776 : display_(display), wl_display_(wl_display_create()) { | 2874 : display_(display), wl_display_(wl_display_create()) { |
2777 wl_global_create(wl_display_.get(), &wl_compositor_interface, | 2875 wl_global_create(wl_display_.get(), &wl_compositor_interface, |
2778 compositor_version, display_, bind_compositor); | 2876 compositor_version, display_, bind_compositor); |
2779 wl_global_create(wl_display_.get(), &wl_shm_interface, 1, display_, bind_shm); | 2877 wl_global_create(wl_display_.get(), &wl_shm_interface, 1, display_, bind_shm); |
(...skipping 18 matching lines...) Expand all Loading... | |
2798 wl_global_create(wl_display_.get(), &wl_seat_interface, seat_version, | 2896 wl_global_create(wl_display_.get(), &wl_seat_interface, seat_version, |
2799 display_, bind_seat); | 2897 display_, bind_seat); |
2800 wl_global_create(wl_display_.get(), &wp_viewporter_interface, 1, display_, | 2898 wl_global_create(wl_display_.get(), &wp_viewporter_interface, 1, display_, |
2801 bind_viewporter); | 2899 bind_viewporter); |
2802 wl_global_create(wl_display_.get(), &zwp_secure_output_v1_interface, 1, | 2900 wl_global_create(wl_display_.get(), &zwp_secure_output_v1_interface, 1, |
2803 display_, bind_secure_output); | 2901 display_, bind_secure_output); |
2804 wl_global_create(wl_display_.get(), &zwp_alpha_compositing_v1_interface, 1, | 2902 wl_global_create(wl_display_.get(), &zwp_alpha_compositing_v1_interface, 1, |
2805 display_, bind_alpha_compositing); | 2903 display_, bind_alpha_compositing); |
2806 wl_global_create(wl_display_.get(), &zwp_remote_shell_v1_interface, | 2904 wl_global_create(wl_display_.get(), &zwp_remote_shell_v1_interface, |
2807 remote_shell_version, display_, bind_remote_shell); | 2905 remote_shell_version, display_, bind_remote_shell); |
2906 wl_global_create(wl_display_.get(), &zwp_gaming_input_v1_interface, 1, | |
2907 display_, bind_gaming_input); | |
2808 } | 2908 } |
2809 | 2909 |
2810 Server::~Server() {} | 2910 Server::~Server() {} |
2811 | 2911 |
2812 // static | 2912 // static |
2813 std::unique_ptr<Server> Server::Create(Display* display) { | 2913 std::unique_ptr<Server> Server::Create(Display* display) { |
2814 std::unique_ptr<Server> server(new Server(display)); | 2914 std::unique_ptr<Server> server(new Server(display)); |
2815 | 2915 |
2816 char* runtime_dir = getenv("XDG_RUNTIME_DIR"); | 2916 char* runtime_dir = getenv("XDG_RUNTIME_DIR"); |
2817 if (!runtime_dir) { | 2917 if (!runtime_dir) { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2869 DCHECK(event_loop); | 2969 DCHECK(event_loop); |
2870 wl_event_loop_dispatch(event_loop, timeout.InMilliseconds()); | 2970 wl_event_loop_dispatch(event_loop, timeout.InMilliseconds()); |
2871 } | 2971 } |
2872 | 2972 |
2873 void Server::Flush() { | 2973 void Server::Flush() { |
2874 wl_display_flush_clients(wl_display_.get()); | 2974 wl_display_flush_clients(wl_display_.get()); |
2875 } | 2975 } |
2876 | 2976 |
2877 } // namespace wayland | 2977 } // namespace wayland |
2878 } // namespace exo | 2978 } // namespace exo |
OLD | NEW |