Skip to content

Commit

Permalink
WindowServer: Added configurable mouse acceleration and scroll length
Browse files Browse the repository at this point in the history
The settings are also saved to the config file to survive reboots.
  • Loading branch information
IdanHo authored and awesomekling committed Dec 30, 2020
1 parent 0c51778 commit db409db
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Base/etc/WindowServer/WindowServer.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ Height=768
[Theme]
Name=Default

[Mouse]
AccelerationFactor=1.0
ScrollStepSize=4

[Cursor]
Hidden=/res/cursors/hidden.png
Arrow=/res/cursors/arrow.x2y2.png
Expand Down
16 changes: 14 additions & 2 deletions Services/WindowServer/Screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,23 @@ void Screen::set_buffer(int index)
ASSERT(rc == 0);
}

void Screen::set_acceleration_factor(double factor)
{
ASSERT(factor >= mouse_accel_min && factor <= mouse_accel_max);
m_acceleration_factor = factor;
}

void Screen::set_scroll_step_size(unsigned step_size)
{
ASSERT(step_size >= scroll_step_size_min);
m_scroll_step_size = step_size;
}

void Screen::on_receive_mouse_data(const MousePacket& packet)
{
auto prev_location = m_cursor_location;
if (packet.is_relative) {
m_cursor_location.move_by(packet.x, packet.y);
m_cursor_location.move_by(packet.x * m_acceleration_factor, packet.y * m_acceleration_factor);
#ifdef WSSCREEN_DEBUG
dbgprintf("Screen: New Relative mouse point @ X %d, Y %d\n", m_cursor_location.x(), m_cursor_location.y());
#endif
Expand Down Expand Up @@ -154,7 +166,7 @@ void Screen::on_receive_mouse_data(const MousePacket& packet)
}

if (packet.z) {
auto message = make<MouseEvent>(Event::MouseWheel, m_cursor_location, buttons, MouseButton::None, m_modifiers, packet.z);
auto message = make<MouseEvent>(Event::MouseWheel, m_cursor_location, buttons, MouseButton::None, m_modifiers, packet.z * m_scroll_step_size);
Core::EventLoop::current().post_event(WindowManager::the(), move(message));
}

Expand Down
12 changes: 12 additions & 0 deletions Services/WindowServer/Screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ struct MousePacket;

namespace WindowServer {

const double mouse_accel_max = 3.5;
const double mouse_accel_min = 0.5;
const unsigned scroll_step_size_min = 1;

class Screen {
public:
Screen(unsigned width, unsigned height);
Expand All @@ -57,6 +61,12 @@ class Screen {
Gfx::IntPoint cursor_location() const { return m_cursor_location; }
unsigned mouse_button_state() const { return m_mouse_button_state; }

double acceleration_factor() const { return m_acceleration_factor; }
void set_acceleration_factor(double);

unsigned scroll_step_size() const { return m_scroll_step_size; }
void set_scroll_step_size(unsigned);

void on_receive_mouse_data(const MousePacket&);
void on_receive_keyboard_data(::KeyEvent);

Expand All @@ -76,6 +86,8 @@ class Screen {
Gfx::IntPoint m_cursor_location;
unsigned m_mouse_button_state { 0 };
unsigned m_modifiers { 0 };
double m_acceleration_factor { 1.0 };
unsigned m_scroll_step_size { 1 };
};

inline Gfx::RGBA32* Screen::scanline(int y)
Expand Down
16 changes: 16 additions & 0 deletions Services/WindowServer/WindowManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,22 @@ Gfx::IntSize WindowManager::resolution() const
return Screen::the().size();
}

void WindowManager::set_acceleration_factor(double factor)
{
Screen::the().set_acceleration_factor(factor);
dbgln("Saving acceleration factor {} to config file at {}", factor, m_config->file_name());
m_config->write_entry("Mouse", "AccelerationFactor", String::formatted("{}", factor));
m_config->sync();
}

void WindowManager::set_scroll_step_size(unsigned step_size)
{
Screen::the().set_scroll_step_size(step_size);
dbgln("Saving scroll step size {} to config file at {}", step_size, m_config->file_name());
m_config->write_entry("Mouse", "ScrollStepSize", String::number(step_size));
m_config->sync();
}

void WindowManager::add_window(Window& window)
{
bool is_first_window = m_windows_in_order.is_empty();
Expand Down
3 changes: 3 additions & 0 deletions Services/WindowServer/WindowManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ class WindowManager : public Core::Object {
bool set_resolution(int width, int height);
Gfx::IntSize resolution() const;

void set_acceleration_factor(double);
void set_scroll_step_size(unsigned);

Window* set_active_input_window(Window*);
void restore_active_input_window(Window*);
void set_active_window(Window*, bool make_input = true);
Expand Down
2 changes: 2 additions & 0 deletions Services/WindowServer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ int main(int, char**)

WindowServer::Screen screen(wm_config->read_num_entry("Screen", "Width", 1024),
wm_config->read_num_entry("Screen", "Height", 768));
screen.set_acceleration_factor(atof(wm_config->read_entry("Mouse", "AccelerationFactor", "1.0").characters()));
screen.set_scroll_step_size(wm_config->read_num_entry("Mouse", "ScrollStepSize", 4));
WindowServer::Compositor::the();
auto wm = WindowServer::WindowManager::construct(*palette);
auto am = WindowServer::AppletManager::construct();
Expand Down

0 comments on commit db409db

Please sign in to comment.