Skip to content

Commit 5107841

Browse files
committed
add data about input devices
1 parent cb6b309 commit 5107841

File tree

10 files changed

+207
-9
lines changed

10 files changed

+207
-9
lines changed

include/gf2/core/KeyboardTypes.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: Zlib
2+
// Copyright (c) 2023-2025 Julien Bernard
3+
#ifndef GF_KEYBOARD_TYPES_H
4+
#define GF_KEYBOARD_TYPES_H
5+
6+
#include <cstdint>
7+
8+
namespace gf {
9+
10+
enum class KeyboardId : uint32_t { };
11+
12+
}
13+
14+
#endif // GF_KEYBOARD_TYPES_H

include/gf2/graphics/Cursor.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ namespace gf {
4444
SDL_Cursor* m_cursor = nullptr;
4545
};
4646

47-
void capture_mouse(bool captured = true);
48-
4947
}
5048

5149
#endif // GF_CURSOR_H

include/gf2/graphics/Keyboard.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,25 @@
33
#ifndef GF_KEYBOARD_H
44
#define GF_KEYBOARD_H
55

6+
#include <vector>
67
#include <tuple>
78

89
#include <gf2/core/Flags.h>
10+
#include <gf2/core/KeyboardTypes.h>
911
#include <gf2/core/Keycode.h>
1012
#include <gf2/core/Modifier.h>
1113
#include <gf2/core/Scancode.h>
14+
#include <gf2/core/ZString.h>
1215

1316
#include "GraphicsApi.h"
1417

1518
namespace gf {
1619

1720
struct GF_GRAPHICS_API Keyboard {
21+
static bool connected();
22+
static std::vector<KeyboardId> devices();
23+
static ZString name(KeyboardId id);
24+
1825
static const char* scancode_name(Scancode scancode);
1926
static Scancode scancode_from_name(const char* name);
2027

include/gf2/graphics/Mouse.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// SPDX-License-Identifier: Zlib
2+
// Copyright (c) 2023-2025 Julien Bernard
3+
#ifndef GF_MOUSE_H
4+
#define GF_MOUSE_H
5+
6+
#include <vector>
7+
8+
#include <gf2/core/MouseTypes.h>
9+
#include <gf2/core/ZString.h>
10+
11+
#include "GraphicsApi.h"
12+
13+
namespace gf {
14+
15+
struct GF_GRAPHICS_API Mouse {
16+
17+
static bool connected();
18+
static std::vector<MouseId> devices();
19+
static ZString name(MouseId id);
20+
static void capture(bool captured = true);
21+
22+
};
23+
24+
}
25+
26+
#endif // GF_MOUSE_H

include/gf2/graphics/Touch.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-License-Identifier: Zlib
2+
// Copyright (c) 2023-2025 Julien Bernard
3+
#ifndef GF_TOUCH_H
4+
#define GF_TOUCH_H
5+
6+
#include <vector>
7+
8+
#include <gf2/core/TouchTypes.h>
9+
#include <gf2/core/ZString.h>
10+
11+
#include "GraphicsApi.h"
12+
13+
namespace gf {
14+
15+
struct GF_GRAPHICS_API Touch {
16+
17+
static std::vector<TouchId> devices();
18+
static ZString name(TouchId id);
19+
static TouchDeviceType type(TouchId id);
20+
21+
};
22+
23+
}
24+
25+
#endif // GF_TOUCH_H

library/core/KeyboardTypes.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// SPDX-License-Identifier: Zlib
2+
// Copyright (c) 2023-2025 Julien Bernard
3+
4+
#include <gf2/core/KeyboardTypes.h>

library/graphics/Cursor.cc

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,4 @@ namespace gf {
8383
SDL_SetCursor(cursor->m_cursor);
8484
}
8585

86-
void capture_mouse(bool captured)
87-
{
88-
if (!SDL_CaptureMouse(captured)) {
89-
Log::error("Failed to capture the mouse: {}", SDL_GetError());
90-
}
91-
}
92-
9386
}

library/graphics/Keyboard.cc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@
33

44
#include <gf2/graphics/Keyboard.h>
55

6+
#include <memory>
67
#include <type_traits>
78

89
#include <SDL3/SDL.h>
910

11+
#include <gf2/core/KeyboardTypes.h>
12+
#include <gf2/core/Log.h>
13+
1014
namespace gf {
1115

1216
namespace {
1317

18+
static_assert(std::is_same_v<std::underlying_type_t<KeyboardId>, SDL_KeyboardID>);
1419
static_assert(std::is_same_v<std::underlying_type_t<Modifier>, SDL_Keymod>);
1520
static_assert(std::is_same_v<std::underlying_type_t<Keycode>, SDL_Keycode>);
1621
static_assert(std::is_same_v<std::underlying_type_t<Scancode>, std::make_unsigned_t<SDL_Scancode>>);
@@ -477,8 +482,45 @@ namespace gf {
477482
keycode_check<Keycode::Mode, SDLK_MODE>();
478483
}
479484

485+
struct KeyboardIdDeleter {
486+
void operator()(SDL_KeyboardID* ids)
487+
{
488+
SDL_free(ids);
489+
}
490+
};
491+
480492
} // namespace
481493

494+
bool Keyboard::connected()
495+
{
496+
return SDL_HasKeyboard();
497+
}
498+
499+
std::vector<KeyboardId> Keyboard::devices()
500+
{
501+
int count = 0;
502+
std::unique_ptr<SDL_KeyboardID[], KeyboardIdDeleter> raw_devices(SDL_GetKeyboards(&count));
503+
504+
std::vector<KeyboardId> devices;
505+
devices.reserve(count);
506+
507+
for (int i = 0; i < count; ++i) {
508+
devices.push_back(KeyboardId{ raw_devices[i] });
509+
}
510+
511+
return devices;
512+
}
513+
514+
ZString Keyboard::name(KeyboardId id)
515+
{
516+
if (const char* name = SDL_GetKeyboardNameForID(static_cast<SDL_KeyboardID>(id)); name != nullptr) {
517+
return name;
518+
}
519+
520+
Log::error("Failed to get keyboard name: {}", SDL_GetError());
521+
return "";
522+
}
523+
482524
const char* Keyboard::scancode_name(Scancode scancode)
483525
{
484526
return SDL_GetScancodeName(static_cast<SDL_Scancode>(scancode));

library/graphics/Mouse.cc

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
// SPDX-License-Identifier: Zlib
22
// Copyright (c) 2023-2025 Julien Bernard
33

4+
#include <gf2/graphics/Mouse.h>
5+
6+
#include <memory>
47
#include <type_traits>
58

69
#include <SDL3/SDL.h>
710

11+
#include <gf2/core/Log.h>
812
#include <gf2/core/MouseTypes.h>
913

1014
namespace gf {
@@ -30,6 +34,50 @@ namespace gf {
3034
mouse_button_check<MouseButton::XButton2, SDL_BUTTON_X2>();
3135
}
3236

37+
struct MouseIdDeleter {
38+
void operator()(SDL_MouseID* ids)
39+
{
40+
SDL_free(ids);
41+
}
42+
};
43+
3344
} // namespace
3445

46+
bool Mouse::connected()
47+
{
48+
return SDL_HasMouse();
49+
}
50+
51+
std::vector<MouseId> Mouse::devices()
52+
{
53+
int count = 0;
54+
std::unique_ptr<SDL_MouseID[], MouseIdDeleter> raw_devices(SDL_GetMice(&count));
55+
56+
std::vector<MouseId> devices;
57+
devices.reserve(count);
58+
59+
for (int i = 0; i < count; ++i) {
60+
devices.push_back(MouseId{raw_devices[i]});
61+
}
62+
63+
return devices;
64+
}
65+
66+
ZString Mouse::name(MouseId id)
67+
{
68+
if (const char* name = SDL_GetMouseNameForID(static_cast<SDL_MouseID>(id)); name != nullptr) {
69+
return name;
70+
}
71+
72+
Log::error("Failed to get mouse name: {}", SDL_GetError());
73+
return "";
74+
}
75+
76+
void Mouse::capture(bool captured)
77+
{
78+
if (!SDL_CaptureMouse(captured)) {
79+
Log::error("Failed to capture the mouse: {}", SDL_GetError());
80+
}
81+
}
82+
3583
} // namespace gf

library/graphics/Touch.cc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
// SPDX-License-Identifier: Zlib
22
// Copyright (c) 2023-2025 Julien Bernard
33

4+
#include <gf2/graphics/Touch.h>
5+
6+
#include <memory>
47
#include <type_traits>
58

69
#include <SDL3/SDL.h>
710

11+
#include <gf2/core/Log.h>
812
#include <gf2/core/TouchTypes.h>
913

1014
namespace gf {
@@ -30,6 +34,43 @@ namespace gf {
3034
touch_device_type_check<TouchDeviceType::IndirectRelative, SDL_TOUCH_DEVICE_INDIRECT_RELATIVE>();
3135
}
3236

37+
struct TouchIdDeleter {
38+
void operator()(SDL_TouchID* ids)
39+
{
40+
SDL_free(ids);
41+
}
42+
};
43+
3344
} // namespace
3445

46+
std::vector<TouchId> Touch::devices()
47+
{
48+
int count = 0;
49+
std::unique_ptr<SDL_TouchID[], TouchIdDeleter> raw_devices(SDL_GetTouchDevices(&count));
50+
51+
std::vector<TouchId> devices;
52+
devices.reserve(count);
53+
54+
for (int i = 0; i < count; ++i) {
55+
devices.push_back(TouchId{ raw_devices[i] });
56+
}
57+
58+
return devices;
59+
}
60+
61+
ZString Touch::name(TouchId id)
62+
{
63+
if (const char* name = SDL_GetTouchDeviceName(static_cast<SDL_TouchID>(id)); name != nullptr) {
64+
return name;
65+
}
66+
67+
Log::error("Failed to get touch name: {}", SDL_GetError());
68+
return "";
69+
}
70+
71+
TouchDeviceType Touch::type(TouchId id)
72+
{
73+
return TouchDeviceType{ SDL_GetTouchDeviceType(static_cast<SDL_TouchID>(id)) };
74+
}
75+
3576
} // namespace gf

0 commit comments

Comments
 (0)