Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run start command on system load #12

Open
skopz356 opened this issue Jul 9, 2023 · 3 comments
Open

Run start command on system load #12

skopz356 opened this issue Jul 9, 2023 · 3 comments
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@skopz356
Copy link

skopz356 commented Jul 9, 2023

Hi, first of all thank you for creating this library, I am a huge fan of easystroke but it stopped working on Debian 12 (gnome 43). So because of that I found your library and it is awesome.

I have a question if it is possible to launch the start command on system load so user can use mouse actions without starting any script.
I tried to create custom service in systemd but I got KeyboardError.

Thank you for your work and have a nice day!

@jersou
Copy link
Owner

jersou commented Jul 16, 2023

Hi, thank you for your comment !
Personally, I added a ".desktop" file in the folder "~/.config/autostart/", but MA could create this file indeed, I leave open this ticket to treat it in the future.

Your desktop environment (gnome) should probably allow you to configure this graphically, maiby : https://help.gnome.org/users/gnome-help/stable/shell-apps-auto-start.html.en ?

@pallaswept
Copy link

pallaswept commented Jul 16, 2023

Jersou's answer is the easiest way - use your DE's GUI to add it as a startup item.

If you want a different approach (which will also unload it automatically when you leave the GUI) I have a pretty raw service unit file in my OBS build, this is just something I've been working on in the sidelines and isn't necessarily production-ready, but it might get you started, it definitely works (maybe it is production-ready after all, honestly):

[Unit]
Description=mouse-actions

[Service]
ExecStart=/usr/bin/mouse-actions start
ExecStop=/usr/bin/mouse-actions stop
ExecReload=/usr/bin/mouse-actions stop && /usr/bin/mouse-actions start

[Install]
WantedBy=graphical-session.target

Create this file as /usr/lib/systemd/user/mouse-actions.service
Run systemctl --user enable --now mouse-actions
Now mouse-actions will start and will always do so when your GUI does and stop when you leave the GUI (say you log out of gnome and are working in the TTY shell for some reason)
You can stop and start it as you would any systemd user service ie
systemctl --user stop mouse-actions

You may need to change the paths depending on your distro, but put it wherever it puts user unit files and change the Exec* lines in the unit file to wherever you have installed mouse-actions (and append -gui if you aren't installing the daemon-only version but only the GUI version). This one is setup for a standard installation in OpenSUSE TW because it comes from an OpenSUSE package, so change it as you require.

(and yes, I will eventually be using the OBS service to build for other distros and DE's so you can have a deb file and a repo... one day in the future ;) )

@PhobosK
Copy link

PhobosK commented Mar 17, 2024

Here is another proposition for that...
It is a bit ugly and probably not the best, but it is a workaround that lessens the security risk of the requirement of the app, the user to be in the input/plugdev group...

  1. You will need to make sure that you have the udev rule for uinput access described in the apps requirement. Do this by:
sudo tee /etc/udev/rules.d/80-mouse-actions.rules <<<'KERNEL=="uinput", SUBSYSTEM=="misc", TAG+="uaccess", OPTIONS+="static_node=uinput"'
  1. Create a user service that provides some user environment variables which are important for the app to run. Put this file in /etc/systemd/user/ and name it export-user-environment.service. Thus you will have: /etc/systemd/user/export-user-environment.service. The content of the file:
[Unit]
Description=Export user environment for usage with X11 apps user systemd services


[Service]
Environment="U_HOME=%h"

ExecStart=bash --login -c 'env | grep -P -e "(XDG_|DISPLAY|GDK_|GTK_|LANG|LC_|SESSION_|XAUT|KDE)" | sort > $U_HOME/.magic-environment-file' 2>/dev/null


[Install]
WantedBy=default.target
  1. Then create the user systemd service file for starting mouse-actions as user. The file should be: /etc/systemd/user/mouse-actions-control.service with this content:
[Unit]
Description=Starts mouse-actions as user using sudo
After=export-user-environment.service
Requires=export-user-environment.service


[Service]
RemainAfterExit=true

# Give some seconds for DE to load. You may comment this out if you do not need it.
ExecStartPre=/bin/sleep 10

ExecStart=sudo /usr/bin/systemctl start mouse-actions@%u.service
ExecStop=sudo /usr/bin/systemctl stop mouse-actions@%u.service
ExecReload=sudo /usr/bin/systemctl stop mouse-actions@%u.service && sudo /usr/bin/systemctl start mouse-actions@%u.service


[Install]
WantedBy=graphical-session.target
  1. Now create the real app starter but as a system service, so we can use the systemd SupplementaryGroups magic... Put the file in /etc/systemd/system/ and name it [email protected]. Thus you will have: /etc/systemd/system/[email protected], The content:
[Unit]
Description=Starts mouse-actions as user, so no security risk of being in input group

StartLimitIntervalSec=600
StartLimitBurst=5


[Service]
User=%i
# Depending on your distro you may need to replace this with plugdev
SupplementaryGroups=input

EnvironmentFile=/home/%i/.magic-environment-file

ExecStart=/usr/bin/mouse-actions start
ExecStop=/usr/bin/mouse-actions stop
ExecReload=/usr/bin/mouse-actions stop && /usr/bin/mouse-actions start

# Ensure auto restart if the app crashes or if the GUI is used to start/stop it
RestartSec=5s
Restart=on-failure

[Install]
WantedBy=graphical-session.target
  1. Finally add the necessary entries for sudo, so system service to be able to start automatically with no password needed. Depending on the distribution you should put this either in /etc/sudoers.d/ as a separate file, or inside your /etc/sudoers file at its end. As a separate file it should be for example: /etc/sudoers.d/003_systemd_mouse_actions The content:
ALL ALL=NOPASSWD:/usr/bin/systemctl start mouse-actions@*.service
ALL ALL=NOPASSWD:/usr/bin/systemctl stop mouse-actions@*.service
ALL ALL=NOPASSWD:/usr/bin/systemctl restart mouse-actions@*.service
ALL ALL=NOPASSWD:/usr/bin/systemctl reload mouse-actions@*.service
ALL ALL=NOPASSWD:/usr/bin/systemctl status mouse-actions@*.service

  1. Now enable the user service with this command (do this as your user, not as root):
systemctl --user enable mouse-actions-control.service
  1. Now best restart your computer, so everything could work as expected.

All this is in order the user not to be in the input/plugdev group which increases the security risks of other programs to be able to "listen" to all your input devices.
The proposed workaround makes so, that only the mouse-action app can listen to all inputs.

The actual system service file, monitors the app for crashes and restarts it automatically. It does restart it also when the GUI is used to change the config (it is because the GUI kills the app and the service talks that as a crash)..

Any suggestions for changes are welcome.

Hope this helps.

@jersou jersou added documentation Improvements or additions to documentation enhancement New feature or request labels Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants