fennerm / flashfocus

Simple focus animations for tiling window managers.
MIT License
748 stars 10 forks source link

Add systemd service #56

Closed maximbaz closed 3 years ago

maximbaz commented 3 years ago

This service is for those who would like to run flashfocus as a systemd service, will not have any impact who don't want to do so 🙂

When enabled, the service will auto-start after graphical session starts, and gracefully exit when graphical session is closed.

In your PKGBUILD, you should place this file in /usr/lib/systemd/user/ folder, then it will be available for optional activation.

By the way, this will help with https://github.com/fennerm/flashfocus/issues/40, although in case of sway the user would have to additionally configure their sway-session.target as explained in sway wiki.

fennerm commented 3 years ago

Thanks for the PR! I'm trying to test this on my machine and haven't managed to get it working under sway. Here's what I've tried (lmk if I'm missing a step):

In the systemd logs I see:

Aug 09 12:07:49 noam flashfocus[2638]:     from flashfocus.compat import DisplayProtocol, get_display_protocol
Aug 09 12:07:49 noam flashfocus[2638]:   File "/data/code/flashfocus/flashfocus/compat.py", line 30, in <module>
Aug 09 12:07:49 noam flashfocus[2638]:     from flashfocus.display_protocols.sway import (
Aug 09 12:07:49 noam flashfocus[2638]:   File "/data/code/flashfocus/flashfocus/display_protocols/sway.py", line 21, in <module>
Aug 09 12:07:49 noam flashfocus[2638]:     SWAY = i3ipc.Connection()
Aug 09 12:07:49 noam flashfocus[2638]:   File "/usr/lib/python3.8/site-packages/i3ipc/connection.py", line 68, in __init__
Aug 09 12:07:49 noam flashfocus[2638]:     raise Exception('Failed to retrieve the i3 or sway IPC socket path')
Aug 09 12:07:49 noam flashfocus[2638]: Exception: Failed to retrieve the i3 or sway IPC socket path
Aug 09 12:07:49 noam systemd[471]: flashfocus.service: Main process exited, code=exited, status=1/FAILURE
Aug 09 12:07:49 noam systemd[471]: flashfocus.service: Failed with result 'exit-code'.

Have you managed to get this working on sway or just X?

fennerm commented 3 years ago

Sorry ignore prev message, figured out my mistake immediately afterwards..

maximbaz commented 3 years ago

Cool! Since you got it working, you can try systemctl --user stop sway-session.target (this should gracefully shut down flashfocus) and then again systemctl --user start sway-session.target (this will bring it up again)

fennerm commented 3 years ago

Yep works great! I wrote up some instructions to add to the wiki. Could you give them a once-over and see if they look okay to you?


Systemd config

Instructions for setting up flashfocus to run as a systemd service.

First make sure that usr/lib/systemd/user/flashfocus.service exists. If not you'll need to manually create the file with contents:

[Unit]
Description=Perform windows animations on focus
PartOf=graphical-session.target
After=graphical-session.target

[Service]
ExecStart=/usr/bin/flashfocus

[Install]
WantedBy=graphical-session.target

Sway

  1. Place the following systemd unit file in ~/.config/systemd/user/sway-session.target:

    [Unit]
    Description=sway compositor session
    Documentation=man:systemd.special(7)
    BindsTo=graphical-session.target
    Wants=graphical-session-pre.target
    After=graphical-session-pre.target
  2. Add exec "systemctl --user import-environment; systemctl --user start sway-session.target" to the bottom of your sway config file.

  3. systemctl --systemctl --user enable --now flashfocus

  4. Flashfocus should start up automatically next time you log in.

For more detailed instructions see https://github.com/swaywm/sway/wiki/Systemd-integration

X-based window managers

  1. Place the following systemd unit file in ~/.config/systemd/user/xsession.target:

    [Unit]
    Description=X session
    Documentation=man:systemd.special(7)
    BindsTo=graphical-session.target
    Wants=graphical-session-pre.target
    After=graphical-session-pre.target
  2. Add systemctl --user import-environment; systemctl --user start xsession.target to ~/.xinitrc. Might need to tweak this depending on setup. You just need to make sure that the xsession.target is initialized when your WM starts up.

  3. systemctl --systemctl --user enable --now flashfocus

  4. Flashfocus should start up automatically next time you log in.

For more detailed instructions see https://goral.net.pl/post/systemd-x-sessions/

maximbaz commented 3 years ago

Looks good!

I would suggest to use links as much as possible instead of writing contents directly, e.g. don't put flashfocus.service contents in wiki but link to the file in the master branch of your repo, so that if it keeps changing you don't have to remember to update wiki. Same for sway-session.target, I'm researching now if it would for example make sense to add that file in sway itself, then users would no longer need to create it by hand - again, just linking to sway wiki would probably be best, then we will have one place to keep up-to-date.

systemctl --user import-environment; systemctl --user start *.target is needed only in case user doesn't use login managers, most sway users just start sway process from .zprofile / .bash_profile, but I would imagine especially on X using GDM or lightdm or the likes is quite popular, and then things should just work out of the box without this step (I haven't tested, but relatively certain).

Otherwise, great stuff 👍

fennerm commented 3 years ago

Awesome thanks for looking! Good point on not duplicating the flashfocus.service contents. I'll also mention that step 2 might not be necessary if a login manager is being used. Agreed that it would be nice for sway to provide the target by default.

Still need to incorporate this into the PKGBUILD etc but gonna go ahead and merge in the interim.

fennerm commented 3 years ago

@jubalh would it make sense for this to be incorporated into the opensuse package as well?

fennerm commented 3 years ago

PKGBUILD updated now

jubalh commented 3 years ago

@fennerm looks like it! Thanks for pinging me I'll take a look soon. BTW: openSUSE package updated to 2.2.2 :)