OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/common/gamepad_connection_event_message_params.h" | |
6 | |
7 #include "base/logging.h" | |
8 | |
9 namespace content { | |
10 | |
11 namespace { | |
12 | |
13 template <size_t N> | |
14 size_t StringLength(const blink::WebUChar web_string[N]) { | |
bajones
2014/03/27 18:56:49
Suprised that we don't have a common way to do thi
| |
15 size_t i = 0; | |
16 while (web_string[i] != 0) ++i; | |
17 DCHECK(i < N); | |
18 return i; | |
19 } | |
20 | |
21 } | |
22 | |
23 GamepadConnectionEventMessageParams::GamepadConnectionEventMessageParams() | |
24 : index(-1) { | |
25 } | |
26 | |
27 GamepadConnectionEventMessageParams::~GamepadConnectionEventMessageParams() { | |
28 } | |
29 | |
30 GamepadConnectionEventMessageParams::GamepadConnectionEventMessageParams( | |
31 int index, const blink::WebGamepad& gamepad) | |
32 : index(index), | |
33 timestamp(gamepad.timestamp), | |
34 axes_length(gamepad.axesLength), | |
35 buttons_length(gamepad.buttonsLength), | |
36 connected(gamepad.connected) { | |
37 size_t length = StringLength<blink::WebGamepad::idLengthCap>(gamepad.id); | |
38 const blink::WebUChar* characters = &gamepad.id[0]; | |
39 id_characters = std::vector<blink::WebUChar>(characters, | |
40 characters + length); | |
41 | |
42 length = StringLength<blink::WebGamepad::mappingLengthCap>(gamepad.mapping); | |
43 characters = &gamepad.mapping[0]; | |
44 mapping_characters = std::vector<blink::WebUChar>(characters, | |
45 characters + length); | |
46 } | |
47 | |
48 void GamepadConnectionEventMessageParams::GetWebGamepad( | |
49 blink::WebGamepad* gamepad) const { | |
50 DCHECK(index >= 0); | |
51 DCHECK(id_characters.size() < (blink::WebGamepad::idLengthCap - 1)); | |
52 | |
53 size_t num_bytes = id_characters.size() * sizeof(blink::WebUChar); | |
54 memcpy(&gamepad->id[0], &id_characters[0], num_bytes); | |
55 gamepad->id[id_characters.size()] = 0; | |
56 | |
57 DCHECK(mapping_characters.size() < (blink::WebGamepad::mappingLengthCap - 1)); | |
58 num_bytes = mapping_characters.size() * sizeof(blink::WebUChar); | |
59 memcpy(&gamepad->mapping[0], &mapping_characters[0], num_bytes); | |
60 gamepad->mapping[mapping_characters.size()] = 0; | |
61 | |
62 gamepad->timestamp = timestamp; | |
63 gamepad->axesLength = axes_length; | |
64 gamepad->buttonsLength = buttons_length; | |
65 gamepad->connected = connected; | |
66 } | |
67 | |
68 } | |
OLD | NEW |