Skip to content

Commit

Permalink
Add input methods API
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenwardy committed May 22, 2022
1 parent af973ac commit 8ccd734
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 17 deletions.
10 changes: 10 additions & 0 deletions doc/lua_api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4754,6 +4754,16 @@ Utilities
-- Equal to the setting `hud_scaling` multiplied by `dpi / 96`
real_hud_scaling = 1,
}
* `minetest.get_player_input_methods(player_name)`: Will return nil if the information isn't available.
Note: it's possible to laptops to have touchscreens, and for phones to have physical keyboards!

{
has_touchscreen_controls = false, -- whether the touchscreen joystick / in-game controls are enabled
has_touchscreen_gui = true, -- whether formspecs should be optimised for touchscreen use
has_keyboard = true, -- whether they have a physical keyboard
has_mouse = true, -- whether they have a physical mouse / touchpad
has_gamepad = false, -- whether they have a gamepad / controller
}

* `minetest.mkdir(path)`: returns success.
* Creates a directory specified by `path`, creating parent directories
Expand Down
5 changes: 4 additions & 1 deletion src/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1371,10 +1371,13 @@ void Client::sendHaveMedia(const std::vector<u32> &tokens)

void Client::sendUpdateClientInfo(const ClientDynamicInfo& info)
{
NetworkPacket pkt(TOSERVER_UPDATE_CLIENT_INFO, 4*2 + 4 + 4);
unsigned long input_methods = info.input_methods.to_ulong();

NetworkPacket pkt(TOSERVER_UPDATE_CLIENT_INFO, 4*2 + 4 + 4 + 1);
pkt << (u32)info.render_target_size.X << (u32)info.render_target_size.Y;
pkt << info.real_gui_scaling;
pkt << info.real_hud_scaling;
pkt << (u8)input_methods;

Send(&pkt);
}
Expand Down
12 changes: 11 additions & 1 deletion src/client/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2478,7 +2478,17 @@ ClientDynamicInfo Game::getCurrentDynamicInfo() const
f32 gui_scaling = g_settings->getFloat("gui_scaling") * density;
f32 hud_scaling = g_settings->getFloat("hud_scaling") * density;

return { screen_size, gui_scaling, hud_scaling };
std::bitset<5> input_methods;
#ifdef HAVE_TOUCHSCREENGUI
input_methods.set(ClientDynamicInfo::IM_TOUCHSCREEN_CONTROLS, true);
input_methods.set(ClientDynamicInfo::IM_TOUCHSCREEN_GUI, true);
#else
input_methods.set(ClientDynamicInfo::IM_KEYBOARD, true);
input_methods.set(ClientDynamicInfo::IM_MOUSE, true);
#endif
input_methods.set(ClientDynamicInfo::IM_GAMEPAD, input->joystick.isConnected());

return { screen_size, gui_scaling, hud_scaling, input_methods };
}

void Game::updateCameraOrientation(CameraOrientation *cam, float dtime)
Expand Down
1 change: 1 addition & 0 deletions src/client/joystick_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ void JoystickController::onJoystickConnect(const std::vector<irr::SJoystickInfo>
setLayoutFromControllerName(joystick_infos[id].Name.c_str());
else
setLayoutFromControllerName(layout);
m_joystick_connected = true;
}

m_joystick_id = id;
Expand Down
5 changes: 5 additions & 0 deletions src/client/joystick_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ class JoystickController {
bool handleEvent(const irr::SEvent::SJoystickEvent &ev);
void clear();

bool isConnected() const {
return m_joystick_connected;
}

bool wasKeyDown(GameKeyType b)
{
bool r = m_past_keys_pressed[b];
Expand Down Expand Up @@ -159,6 +163,7 @@ class JoystickController {
s16 m_axes_vals[JA_COUNT];

u8 m_joystick_id = 0;
bool m_joystick_connected = false;

std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_keys_down;
std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_keys_pressed;
Expand Down
20 changes: 18 additions & 2 deletions src/clientdynamicinfo.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
#pragma once

#include "irrTypes.h"

#include <bitset>

struct ClientDynamicInfo
{
enum InputMethodId
{
IM_TOUCHSCREEN_CONTROLS = 0,
IM_TOUCHSCREEN_GUI,
IM_KEYBOARD,
IM_MOUSE,
IM_GAMEPAD,
};

v2u32 render_target_size;
f32 real_gui_scaling;
f32 real_hud_scaling;

std::bitset<5> input_methods;

inline bool hasInputMethod(InputMethodId method) const {
return input_methods.test(method);
}

bool equal(const ClientDynamicInfo &other) const {
return render_target_size == other.render_target_size &&
abs(real_gui_scaling - other.real_gui_scaling) < 0.001f &&
abs(real_hud_scaling - other.real_hud_scaling) < 0.001f;
abs(real_hud_scaling - other.real_hud_scaling) < 0.001f &&
input_methods == other.input_methods;
}
};
1 change: 1 addition & 0 deletions src/network/networkprotocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,7 @@ enum ToServerCommand
v2s16 render_target_size
f32 gui_scaling
f32 hud_scaling
u8 input_methods
*/

TOSERVER_NUM_MSG_TYPES = 0x54,
Expand Down
4 changes: 4 additions & 0 deletions src/network/serverpackethandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,10 @@ void Server::handleCommand_UpdateClientInfo(NetworkPacket *pkt)
*pkt >> info.real_gui_scaling;
*pkt >> info.real_hud_scaling;

u8 input_methods;
*pkt >> input_methods;
info.input_methods = input_methods;

session_t peer_id = pkt->getPeerId();
RemoteClient *client = getClient(peer_id, CS_Invalid);
client->setDynamicInfo(info);
Expand Down
71 changes: 58 additions & 13 deletions src/script/lua_api/l_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,23 +282,67 @@ int ModApiServer::l_get_player_window_information(lua_State *L)
return 0;

auto dynamic = server->getClientDynamicInfo(player->getPeerId());
if (!dynamic || dynamic->render_target_size == v2u32())
return 0;

if (dynamic && dynamic->render_target_size != v2u32()) {
lua_newtable(L);
int dyn_table = lua_gettop(L);
lua_newtable(L);
int table = lua_gettop(L);

lua_pushstring(L, "size");
push_v2u32(L, dynamic->render_target_size);
lua_settable(L, dyn_table);
lua_pushstring(L, "size");
push_v2u32(L, dynamic->render_target_size);
lua_settable(L, table);

lua_pushstring(L, "real_gui_scaling");
lua_pushnumber(L, dynamic->real_gui_scaling);
lua_settable(L, dyn_table);
lua_pushstring(L, "real_gui_scaling");
lua_pushnumber(L, dynamic->real_gui_scaling);
lua_settable(L, table);

lua_pushstring(L, "real_hud_scaling");
lua_pushnumber(L, dynamic->real_hud_scaling);
lua_settable(L, dyn_table);
}
lua_pushstring(L, "real_hud_scaling");
lua_pushnumber(L, dynamic->real_hud_scaling);
lua_settable(L, table);

return 1;
}

// l_get_player_input_methods
int ModApiServer::l_get_player_input_methods(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;

Server *server = getServer(L);

const char *name = luaL_checkstring(L, 1);
RemotePlayer *player = server->getEnv().getPlayer(name);
if (!player)
return 0;

auto dynamic = server->getClientDynamicInfo(player->getPeerId());
if (!dynamic)
return 0;

lua_newtable(L);
int table = lua_gettop(L);

lua_pushstring(L, "has_touchscreen_controls");
lua_pushboolean(L,
dynamic->hasInputMethod(ClientDynamicInfo::IM_TOUCHSCREEN_CONTROLS));
lua_settable(L, table);

lua_pushstring(L, "has_touchscreen_gui");
lua_pushboolean(L,
dynamic->hasInputMethod(ClientDynamicInfo::IM_TOUCHSCREEN_GUI));
lua_settable(L, table);

lua_pushstring(L, "has_keyboard");
lua_pushboolean(L, dynamic->hasInputMethod(ClientDynamicInfo::IM_KEYBOARD));
lua_settable(L, table);

lua_pushstring(L, "has_mouse");
lua_pushboolean(L, dynamic->hasInputMethod(ClientDynamicInfo::IM_MOUSE));
lua_settable(L, table);

lua_pushstring(L, "has_gamepad");
lua_pushboolean(L, dynamic->hasInputMethod(ClientDynamicInfo::IM_GAMEPAD));
lua_settable(L, table);

return 1;
}
Expand Down Expand Up @@ -656,6 +700,7 @@ void ModApiServer::Initialize(lua_State *L, int top)

API_FCT(get_player_information);
API_FCT(get_player_window_information);
API_FCT(get_player_input_methods);
API_FCT(get_player_privs);
API_FCT(get_player_ip);
API_FCT(get_ban_list);
Expand Down
3 changes: 3 additions & 0 deletions src/script/lua_api/l_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ class ModApiServer : public ModApiBase
// get_player_window_information(name)
static int l_get_player_window_information(lua_State *L);

// get_player_input_methods(name)
static int l_get_player_input_methods(lua_State *L);

// get_ban_list()
static int l_get_ban_list(lua_State *L);

Expand Down

0 comments on commit 8ccd734

Please sign in to comment.