Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: components/exo/wayland/server.cc

Issue 2076013002: exo: Implement wayland gamepad support (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@serv
Patch Set: fixed polling thread. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
42 #include "components/exo/buffer.h" 43 #include "components/exo/buffer.h"
43 #include "components/exo/display.h" 44 #include "components/exo/display.h"
45 #include "components/exo/gamepad.h"
46 #include "components/exo/gamepad_delegate.h"
44 #include "components/exo/keyboard.h" 47 #include "components/exo/keyboard.h"
45 #include "components/exo/keyboard_delegate.h" 48 #include "components/exo/keyboard_delegate.h"
46 #include "components/exo/notification_surface.h" 49 #include "components/exo/notification_surface.h"
47 #include "components/exo/notification_surface_manager.h" 50 #include "components/exo/notification_surface_manager.h"
48 #include "components/exo/pointer.h" 51 #include "components/exo/pointer.h"
49 #include "components/exo/pointer_delegate.h" 52 #include "components/exo/pointer_delegate.h"
50 #include "components/exo/shared_memory.h" 53 #include "components/exo/shared_memory.h"
51 #include "components/exo/shell_surface.h" 54 #include "components/exo/shell_surface.h"
52 #include "components/exo/sub_surface.h" 55 #include "components/exo/sub_surface.h"
53 #include "components/exo/surface.h" 56 #include "components/exo/surface.h"
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 DestroyUserData<T>); 121 DestroyUserData<T>);
119 } 122 }
120 123
121 // Convert a timestamp to a time value that can be used when interfacing 124 // 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 125 // with wayland. Note that we cast a int64_t value to uint32_t which can
123 // potentially overflow. 126 // potentially overflow.
124 uint32_t TimeTicksToMilliseconds(base::TimeTicks ticks) { 127 uint32_t TimeTicksToMilliseconds(base::TimeTicks ticks) {
125 return (ticks - base::TimeTicks()).InMilliseconds(); 128 return (ticks - base::TimeTicks()).InMilliseconds();
126 } 129 }
127 130
131 uint32_t NowInMilliseconds() {
132 return TimeTicksToMilliseconds(base::TimeTicks::Now());
133 }
134
128 // A property key containing the surface resource that is associated with 135 // A property key containing the surface resource that is associated with
129 // window. If unset, no surface resource is associated with window. 136 // window. If unset, no surface resource is associated with window.
130 DEFINE_SURFACE_PROPERTY_KEY(wl_resource*, kSurfaceResourceKey, nullptr); 137 DEFINE_SURFACE_PROPERTY_KEY(wl_resource*, kSurfaceResourceKey, nullptr);
131 138
132 // A property key containing a boolean set to true if a viewport is associated 139 // A property key containing a boolean set to true if a viewport is associated
133 // with window. 140 // with window.
134 DEFINE_SURFACE_PROPERTY_KEY(bool, kSurfaceHasViewportKey, false); 141 DEFINE_SURFACE_PROPERTY_KEY(bool, kSurfaceHasViewportKey, false);
135 142
136 // A property key containing a boolean set to true if a security object is 143 // A property key containing a boolean set to true if a security object is
137 // associated with window. 144 // associated with window.
(...skipping 2589 matching lines...) Expand 10 before | Expand all | Expand 10 after
2727 void* data, 2734 void* data,
2728 uint32_t version, 2735 uint32_t version,
2729 uint32_t id) { 2736 uint32_t id) {
2730 wl_resource* resource = 2737 wl_resource* resource =
2731 wl_resource_create(client, &zwp_alpha_compositing_v1_interface, 1, id); 2738 wl_resource_create(client, &zwp_alpha_compositing_v1_interface, 1, id);
2732 2739
2733 wl_resource_set_implementation(resource, &alpha_compositing_implementation, 2740 wl_resource_set_implementation(resource, &alpha_compositing_implementation,
2734 data, nullptr); 2741 data, nullptr);
2735 } 2742 }
2736 2743
2744 ////////////////////////////////////////////////////////////////////////////////
2745 // zwp_gamepad_v1:
reveman 2016/06/29 21:55:45 nit: convention is "gaming_input_interface:". not
denniskempin 2016/06/30 00:29:22 Done.
2746
2747 // Gamepad delegate class that forwards gamepad events to the client resource.
2748 class WaylandGamepadDelegate : public GamepadDelegate {
2749 public:
2750 explicit WaylandGamepadDelegate(wl_resource* gamepad_resource)
2751 : gamepad_resource_(gamepad_resource) {}
2752
2753 // Overridden from GamepadDelegate:
2754 void OnGamepadDestroying(Gamepad* gamepad) override { delete this; }
2755
reveman 2016/06/29 21:55:45 nit: remove this blank line
denniskempin 2016/06/30 00:29:22 Done.
2756 void OnStateChange(bool connected) override {
2757 uint32_t status = connected ? ZWP_GAMEPAD_V1_GAMEPAD_STATE_ON
2758 : ZWP_GAMEPAD_V1_GAMEPAD_STATE_OFF;
2759 zwp_gamepad_v1_send_state_change(gamepad_resource_, status);
2760 wl_client_flush(client());
2761 }
2762 void OnAxis(int axis, double value) override {
2763 zwp_gamepad_v1_send_axis(gamepad_resource_, NowInMilliseconds(), axis,
2764 wl_fixed_from_double(value));
2765 }
2766 void OnButton(int button, bool pressed, double value) override {
2767 uint32_t state = pressed ? ZWP_GAMEPAD_V1_BUTTON_STATE_PRESSED
2768 : ZWP_GAMEPAD_V1_BUTTON_STATE_RELEASED;
2769 zwp_gamepad_v1_send_button(gamepad_resource_, NowInMilliseconds(), button,
2770 state, wl_fixed_from_double(value));
2771 }
2772 void OnFrame() override {
2773 zwp_gamepad_v1_send_frame(gamepad_resource_, NowInMilliseconds());
2774 wl_client_flush(client());
2775 }
2776
2777 private:
2778 // The client who own this gamepad instance.
2779 wl_client* client() const {
2780 return wl_resource_get_client(gamepad_resource_);
2781 }
2782
2783 // The gamepad resource associated with the gamepad.
2784 wl_resource* const gamepad_resource_;
2785
2786 DISALLOW_COPY_AND_ASSIGN(WaylandGamepadDelegate);
2787 };
2788
2789 void gamepad_destroy(wl_client* client, wl_resource* resource) {
2790 wl_resource_destroy(resource);
2791 }
2792
2793 const struct zwp_gamepad_v1_interface gamepad_implementation = {
2794 gamepad_destroy};
2795
2796 void gaming_input_get_gamepad(wl_client* client,
2797 wl_resource* resource,
2798 uint32_t id,
2799 wl_resource* seat) {
2800 wl_resource* gamepad_resource = wl_resource_create(
2801 client, &zwp_gamepad_v1_interface, wl_resource_get_version(resource), id);
2802
2803 auto gamepad = new Gamepad(0, new WaylandGamepadDelegate(gamepad_resource));
reveman 2016/06/29 21:55:45 nit: remove this temporary variable for consistenc
denniskempin 2016/06/30 00:29:22 Done.
2804 SetImplementation(gamepad_resource, &gamepad_implementation,
2805 base::WrapUnique(gamepad));
2806 }
2807
2808 const struct zwp_gaming_input_v1_interface gaming_input_implementation = {
2809 gaming_input_get_gamepad};
2810
2811 void bing_gaming_input(wl_client* client,
reveman 2016/06/29 21:55:45 s/bing/bind/
denniskempin 2016/06/30 00:29:22 sorry about the trademark infringement ;) done.
2812 void* data,
2813 uint32_t version,
2814 uint32_t id) {
2815 wl_resource* resource =
2816 wl_resource_create(client, &zwp_gaming_input_v1_interface, version, id);
2817 wl_resource_set_implementation(resource, &gaming_input_implementation, data,
2818 nullptr);
2819 }
2820
2737 } // namespace 2821 } // namespace
2738 2822
2739 //////////////////////////////////////////////////////////////////////////////// 2823 ////////////////////////////////////////////////////////////////////////////////
2740 // Server, public: 2824 // Server, public:
2741 2825
2742 Server::Server(Display* display) 2826 Server::Server(Display* display)
2743 : display_(display), wl_display_(wl_display_create()) { 2827 : display_(display), wl_display_(wl_display_create()) {
2744 wl_global_create(wl_display_.get(), &wl_compositor_interface, 2828 wl_global_create(wl_display_.get(), &wl_compositor_interface,
2745 compositor_version, display_, bind_compositor); 2829 compositor_version, display_, bind_compositor);
2746 wl_global_create(wl_display_.get(), &wl_shm_interface, 1, display_, bind_shm); 2830 wl_global_create(wl_display_.get(), &wl_shm_interface, 1, display_, bind_shm);
(...skipping 18 matching lines...) Expand all
2765 wl_global_create(wl_display_.get(), &wl_seat_interface, seat_version, 2849 wl_global_create(wl_display_.get(), &wl_seat_interface, seat_version,
2766 display_, bind_seat); 2850 display_, bind_seat);
2767 wl_global_create(wl_display_.get(), &wp_viewporter_interface, 1, display_, 2851 wl_global_create(wl_display_.get(), &wp_viewporter_interface, 1, display_,
2768 bind_viewporter); 2852 bind_viewporter);
2769 wl_global_create(wl_display_.get(), &zwp_secure_output_v1_interface, 1, 2853 wl_global_create(wl_display_.get(), &zwp_secure_output_v1_interface, 1,
2770 display_, bind_secure_output); 2854 display_, bind_secure_output);
2771 wl_global_create(wl_display_.get(), &zwp_alpha_compositing_v1_interface, 1, 2855 wl_global_create(wl_display_.get(), &zwp_alpha_compositing_v1_interface, 1,
2772 display_, bind_alpha_compositing); 2856 display_, bind_alpha_compositing);
2773 wl_global_create(wl_display_.get(), &zwp_remote_shell_v1_interface, 2857 wl_global_create(wl_display_.get(), &zwp_remote_shell_v1_interface,
2774 remote_shell_version, display_, bind_remote_shell); 2858 remote_shell_version, display_, bind_remote_shell);
2859 wl_global_create(wl_display_.get(), &zwp_gaming_input_v1_interface, 1,
2860 display_, bing_gaming_input);
2775 } 2861 }
2776 2862
2777 Server::~Server() {} 2863 Server::~Server() {}
2778 2864
2779 // static 2865 // static
2780 std::unique_ptr<Server> Server::Create(Display* display) { 2866 std::unique_ptr<Server> Server::Create(Display* display) {
2781 std::unique_ptr<Server> server(new Server(display)); 2867 std::unique_ptr<Server> server(new Server(display));
2782 2868
2783 char* runtime_dir = getenv("XDG_RUNTIME_DIR"); 2869 char* runtime_dir = getenv("XDG_RUNTIME_DIR");
2784 if (!runtime_dir) { 2870 if (!runtime_dir) {
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
2836 DCHECK(event_loop); 2922 DCHECK(event_loop);
2837 wl_event_loop_dispatch(event_loop, timeout.InMilliseconds()); 2923 wl_event_loop_dispatch(event_loop, timeout.InMilliseconds());
2838 } 2924 }
2839 2925
2840 void Server::Flush() { 2926 void Server::Flush() {
2841 wl_display_flush_clients(wl_display_.get()); 2927 wl_display_flush_clients(wl_display_.get());
2842 } 2928 }
2843 2929
2844 } // namespace wayland 2930 } // namespace wayland
2845 } // namespace exo 2931 } // namespace exo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698