Skip to content

Commit

Permalink
Add focus stealing prevention plugin (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
soreau authored Mar 3, 2023
1 parent d8e6e39 commit 619bcae
Show file tree
Hide file tree
Showing 4 changed files with 288 additions and 0 deletions.
23 changes: 23 additions & 0 deletions metadata/focus-steal-prevent.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0"?>
<wayfire>
<plugin name="focus-steal-prevent">
<_short>Focus Stealing Prevention</_short>
<category>Window Management</category>
<option name="timeout" type="int">
<_short>The timeout (in ms) to prevent focus stealing</_short>
<_long>A timer will be reset to the timeout (in ms) on every keystroke. If the timer is active when another window would normally gain focus, the focus is reset to the window that is accepting input.</_long>
<default>1000</default>
<min>0</min>
</option>
<option name="deny_focus_views" type="string">
<_short>Deny focus to views matching this criteria</_short>
<_long>If a view matches when mapped, focus is reset to the last view with focus.</_long>
<default>none</default>
</option>
<option name="cancel_keys" type="string">
<_short>Cancel focus stealing prevention on these keys</_short>
<_long>Cancel focus stealing prevention when any of these keys are pressed. The string consists of keycode names delimited by | character.</_long>
<default>KEY_ENTER</default>
</option>
</plugin>
</wayfire>
1 change: 1 addition & 0 deletions metadata/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ install_data('autorotate-iio.xml', install_dir: wayfire.get_variable(pkgconfig:
install_data('background-view.xml', install_dir: wayfire.get_variable(pkgconfig: 'metadatadir'))
install_data('bench.xml', install_dir: wayfire.get_variable(pkgconfig: 'metadatadir'))
install_data('crosshair.xml', install_dir: wayfire.get_variable(pkgconfig: 'metadatadir'))
install_data('focus-steal-prevent.xml', install_dir: wayfire.get_variable(pkgconfig: 'metadatadir'))
install_data('follow-focus.xml', install_dir: wayfire.get_variable(pkgconfig: 'metadatadir'))
install_data('force-fullscreen.xml', install_dir: wayfire.get_variable(pkgconfig: 'metadatadir'))
install_data('hide-cursor.xml', install_dir: wayfire.get_variable(pkgconfig: 'metadatadir'))
Expand Down
260 changes: 260 additions & 0 deletions src/focus-steal-prevent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
/*
* Copyright © 2023 Scott Moreau
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <wayfire/per-output-plugin.hpp>
#include <wayfire/workspace-manager.hpp>
#include <wayfire/matcher.hpp>
#include <set>
#include <linux/input-event-codes.h>
#include <libevdev/libevdev.h>


namespace focus_steal_prevent
{
class wayfire_focus_steal_prevent : public wf::per_output_plugin_instance_t
{
private:
wayfire_view focus_view = nullptr;
wayfire_view last_focus_view = nullptr;
bool prevent_focus_steal = false;
int modifiers_pressed = 0;
std::multiset<uint32_t> pressed_keys;
std::multiset<uint32_t> cancel_keycodes;
wf::wl_timer timer;

wf::option_wrapper_t<int> timeout{"focus-steal-prevent/timeout"};
wf::view_matcher_t deny_focus_views{"focus-steal-prevent/deny_focus_views"};
wf::option_wrapper_t<std::string> cancel_keys{"focus-steal-prevent/cancel_keys"};

std::string ltrim(const std::string & s)
{
size_t start = s.find_first_not_of(' ');
return (start == std::string::npos) ? "" : s.substr(start);
}

std::string rtrim(const std::string & s)
{
size_t end = s.find_last_not_of(' ');
return (end == std::string::npos) ? "" : s.substr(0, end + 1);
}

std::string trim(const std::string & s)
{
return rtrim(ltrim(s));
}

std::multiset<uint32_t> get_cancel_keycodes(std::string s)
{
std::multiset<uint32_t> keycodes;
/* Split string with | delimiter */
std::stringstream ss(s);
std::string name;
while (!ss.eof())
{
getline(ss, name, '|');
/* Strip whitespace from beginning and end of string */
name = trim(name);
/* Check if string is a valid keycode */
auto keycode = libevdev_event_code_from_name(EV_KEY, name.c_str());
if (keycode != -1)
{
/* Add it to the vector */
keycodes.insert(keycode);
}
}

return keycodes;
}

bool is_cancel_key(uint32_t keycode)
{
for (auto k : cancel_keycodes)
{
if (k == keycode)
{
return true;
}
}

return false;
}

void cancel()
{
focus_view = nullptr;
prevent_focus_steal = false;
}

void reset_timeout()
{
timer.disconnect();
timer.set_timeout(timeout, [=] ()
{
cancel();
return false; // disconnect
});
}

bool is_modifier(uint32_t keycode)
{
if ((keycode == KEY_LEFTCTRL) ||
(keycode == KEY_RIGHTCTRL) ||
(keycode == KEY_LEFTMETA) ||
(keycode == KEY_RIGHTMETA) ||
(keycode == KEY_LEFTALT) ||
(keycode == KEY_RIGHTALT))
{
return true;
}

return false;
}

wf::signal::connection_t<wf::post_input_event_signal<wlr_keyboard_key_event>> on_key_event =
[=] (wf::post_input_event_signal<wlr_keyboard_key_event> *ev)
{
if (ev->event->state == WL_KEYBOARD_KEY_STATE_PRESSED)
{
pressed_keys.insert(ev->event->keycode);
if (is_modifier(ev->event->keycode))
{
modifiers_pressed++;
}
}

if (ev->event->state == WL_KEYBOARD_KEY_STATE_RELEASED)
{
if (pressed_keys.count(ev->event->keycode))
{
pressed_keys.erase(pressed_keys.find(ev->event->keycode));
}

if (is_modifier(ev->event->keycode))
{
modifiers_pressed--;
/* Might happen if modifier is held when plugin is loaded */
if (modifiers_pressed < 0)
{
modifiers_pressed = 0;
}
}

if (!modifiers_pressed && pressed_keys.empty())
{
reset_timeout();
}

return;
}

if (modifiers_pressed || is_cancel_key(ev->event->keycode))
{
timer.disconnect();
cancel();
return;
}

focus_view = output->get_active_view();
prevent_focus_steal = true;
timer.disconnect();
};

wf::signal::connection_t<wf::input_event_signal<wlr_pointer_button_event>> on_button_event =
[=] (wf::input_event_signal<wlr_pointer_button_event> *ev)
{
if ((ev->event->state == WLR_BUTTON_RELEASED) || !prevent_focus_steal)
{
return;
}

focus_view = wf::get_core().get_cursor_focus_view();
reset_timeout();
};

wf::signal::connection_t<wf::pre_focus_view_signal> pre_view_focused = [=] (wf::pre_focus_view_signal *ev)
{
if (ev->view && deny_focus_views.matches(ev->view))
{
ev->can_focus = false;
if (last_focus_view)
{
output->workspace->bring_to_front(last_focus_view);
}
}

last_focus_view = ev->view;

if (!prevent_focus_steal)
{
return;
}

if (ev->view != focus_view)
{
pre_view_focused.disconnect();

if (focus_view)
{
ev->can_focus = false;
output->workspace->bring_to_front(focus_view);
}

if (ev->view)
{
/** Emit the view-hints-changed signal for use with panels */
wf::view_hints_changed_signal hints_signal;
hints_signal.view = ev->view;
hints_signal.demands_attention = true;
ev->view->emit(&hints_signal);
wf::get_core().emit(&hints_signal);
}

output->connect(&pre_view_focused);
}
};

wf::config::option_base_t::updated_callback_t cancel_keys_changed = [=] ()
{
cancel_keycodes = get_cancel_keycodes(cancel_keys);
};

public:
void init() override
{
cancel_keys.set_callback(cancel_keys_changed);
output->connect(&pre_view_focused);
wf::get_core().connect(&on_key_event);
wf::get_core().connect(&on_button_event);
cancel_keys_changed();
}

void fini() override
{
timer.disconnect();
on_key_event.disconnect();
on_button_event.disconnect();
pre_view_focused.disconnect();
}
};

DECLARE_WAYFIRE_PLUGIN(wf::per_output_plugin_t<wayfire_focus_steal_prevent>);
}
4 changes: 4 additions & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ crosshair = shared_module('crosshair', 'crosshair.cpp',
dependencies: [wayfire],
install: true, install_dir: join_paths(get_option('libdir'), 'wayfire'))

focus_steal_prevent = shared_module('focus-steal-prevent', 'focus-steal-prevent.cpp',
dependencies: [wayfire],
install: true, install_dir: join_paths(get_option('libdir'), 'wayfire'))

follow_focus = shared_module('follow-focus', 'follow-focus.cpp',
dependencies: [wayfire],
install: true, install_dir: join_paths(get_option('libdir'), 'wayfire'))
Expand Down

0 comments on commit 619bcae

Please sign in to comment.