weixin_39689347
weixin_39689347
2020-11-21 17:08

Incorporate optional system sleep and shutdown to Sleep timer

As the title says, it would nice to have an automated way to turn off your PC once the Sleep timer hits its mark.

I know this is not an easy task to implement since different operating systems and even different distros have different power management mechanisms, but even if the feature would work partially for most of the systems, I'd appreciate it. In case of failing to shutdown or sleep the user's PC, the Sleep timer would simply work normally without this additional feature and a message could be sent to user stating that automated shutdown is not supported on the user's system.

I'd like to hear what's the developers' take on this idea in general since I could do some tests on my own and possibly send a pull request if I get something reliable working.

EDIT:

Another way to circumvent the issue of cross-platform shutdown would be by offering the user a way to execute a custom script on the event when Sleep timer finishes. This way the user could configure a shutdown script that will certainly work on his/her system.

该提问来源于开源项目:geigi/cozy

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

17条回答

  • weixin_39816062 weixin_39816062 5月前

    Thanks for great work. I really like Cozy. As an addition, i can gladly say that shutdown timer implemention of is also working flawless with Manjaro XFCE.

    Ekran-g-r-nt-s-2020-02-03-16-50-09

    点赞 评论 复制链接分享
  • weixin_39689347 weixin_39689347 5月前

    Thanks for the feedback, guys!

    Nice to hear that the feature is working and it is probably already somewhat useful for you. But as we discussed with before, it would be better if the sleep/shutdown would happen only after the fading of the audio has finished. Now the audio stops very suddenly when the computer turns off, and this can in worst cases be enough to wake the user if the intention was to fall asleep using Cozy.

    I'll have to leave the problem of figuring out what needs to be changed for the fading to work with sleep for now. I may be able to implement it if it becomes clear what needs to be done, but I am currently pretty busy with school and other upcoming projects.

    I agree that the UI could use some fine-tuning. Radio buttons may not be prettiest solution here. An UI element that would look like a button but would work like a radio button would probably be more cleaner solution, if such thing exists.

    点赞 评论 复制链接分享
  • weixin_39816062 weixin_39816062 5月前

    thanks for nice reply. I got it better now. I m sure it will be much more better with fade away function during shutting down. Will be following up you and . Thanks again for this great thing,i m getting into sleep much more easier then after tough working hours :)

    点赞 评论 复制链接分享
  • weixin_39567870 weixin_39567870 5月前

    Awesome, thanks a lot for testing!

    点赞 评论 复制链接分享
  • weixin_39567870 weixin_39567870 5月前

    sure, I'll look after finishing this feature so we can ship it to the users out there. I'm also quite busy at the moment but hopefully I'm able to ship it this week. Thanks again for all your work, I'm really happy about your contribution! 💐 Could you create a pull-request of the current state of the feature?

    点赞 评论 复制链接分享
  • weixin_39567870 weixin_39567870 5月前

    I just cherry-picked the commit from your fork and fixed the fadeout bug. Sorry for the massive delay and thanks again for your contribution! I'll publish a new version today.

    点赞 评论 复制链接分享
  • weixin_39567870 weixin_39567870 5月前

    Sorry for the late answer... Awesome, thanks for posting the link.

    I'll have a look at the fading/sleep problem soon. At first glance your code looks fine!

    点赞 评论 复制链接分享
  • weixin_39689347 weixin_39689347 5月前

    Thanks for the details, I am getting a hang of the project. Debugging and running from PyCharm are working now.

    So I got the basics of suspend and shutdown working on my Linux machine that is using systemd. The interface looks like this at the moment:

    image

    However, there's still some work to do and things to consider. I am not sure what the sleep and suspend commands should be for Mac. There's also the fact that not every Linux system is running systemd, so systemctl suspend and systemctl poweroff might not be available. I tried to look at getting meaningful return value from os.system("systemctl suspend") to determine if the system command was successful or if it is available on the user's system, but it seemed a bit complicated involving checking outputs from subprocesses and what not.

    At the moment, the power event will be performed on __sleep_timer_fired(self), which means that the suspend or shutdown happens before cozy has chance to fade off the stream. This is not good, and I could not figure out how to hook up the power event (suspend or shutdown) to that exact moment when the fading thread dies or stops. Is it possible to make a thread fire a function when it dies without checking repeatedly if the thread is still alive with another thread, or should we look for another solution?

    About the shutdown and its potential damage: By notification area, do you mean the bar where playback position gets switched occasionally to show info messages? If you think that will be enough, sure. Another way could be prompting the user with a dialog when the user presses Enable system power control: switch. That dialog could inform the user about the fact that loss of data may occur and that the system will indeed attempt to shutdown or suspend. There could be options like Cancel and I understand along with a checkbox Don't show this again. on the dialog. Making use of the checkbox next time would obviously require a way to store preferences on disk and I have not looked into that stuff yet so I can't implement it.

    Do you want me to make a pull request now so you can evaluate the current situation?

    点赞 评论 复制链接分享
  • weixin_39567870 weixin_39567870 5月前

    This looks cool, I like it! Maybe we need to adjust the padding on some items a bit between the labels and the switch and between the slider and the settings, but otherwise it looks nice!

    Regarding the systemctl problem: I assume almost all users that are running cozy use systemctl. I don't know of a recent, major linux distro that doesn't use it. Therefore I would only support systemctl at first. If people actually run into this problem we can still think of a solution for this problem.

    I think most of cozy's users install it through flatpak. This is a scenario we have to test...

    Probably the method __stop_playback might be the right place. The fadeout happens here (if it is enabled). After the fadeout the playback is paused. After that we could fire the systemd command (line 182). What do you think?

    Regarding the notification: I thought of the desktop environments notification system. This is not used in cozy yet, so we have to think about how to integrate this with cozy (maybe a wrapper class for easily creating notifications?) Here is some information I found about this: https://www.devdungeon.com/content/desktop-notifications-linux-python

    点赞 评论 复制链接分享
  • weixin_39567870 weixin_39567870 5月前

    You can also post a link to your branch (if it is public on github) :)

    点赞 评论 复制链接分享
  • weixin_39689347 weixin_39689347 5月前

    Sorry, I have been busy with other things. The changes are now up for inspection in my fork: https://github.com/kaphula/cozy/commit/3a4093f3ffdb530aec6f83fd3c0adca1ed4b2968

    I did not get the fading and sleeping working yet. This commit show __handle_system_power_event calls in two places, but even if you remove the other one that is inside __sleep_timer_fired the fading does not work.

    点赞 评论 复制链接分享
  • weixin_39567870 weixin_39567870 5月前

    I believe the build process does compile the ui files into a resource file and stores it in the according location. Every file compiled into this resource is then available to the application during runtime. As soon as you add the file to git_repo/data/ui/gresource.xml the build process includes the ui file into the gresource. Did you add an entry for your new file to this xml file?

    You'll still have to run ninja after each of your changes. The meson command is only needed for initialization.

    Pycharm: I couldn't find any option to export the Run settings I'm using. I've setup pycharm that with a click on the play button it runs the build process and launches cozy. I've created screenshots for the run configuration, I hope this helps you in configuring it.

    Regarding the automatic shutdown: I think maybe showing a notification using the notification system might be a good idea: "Cozy will shutdown your computer in one minute. Click abort to cancel" or something like that. Another idea for the switch to enable this feature: maybe the sleep timer popover is also a good place. If we put the switch there the user always sees whether the automatic shutdown is currently enabled or not. There might be less surprises if a user forgot he had enabled this feature in the settings... run meson ninja

    点赞 评论 复制链接分享
  • weixin_39567870 weixin_39567870 5月前

    This is a nice idea, I thought about it in the past but it was not on my priority list yet. I think it's possible to use systemd which would support most modern linux distros (I think it would support all recent and most used distros which is what I would focus on).

    Possible commands:

    
    systemctl poweroff
    systemctl suspend
    

    I believe these commands can be run as a normal user but I'm not entirely sure here. If you want to give it a try to implement this, feel free! I'll support you. I'm currently refactoring a lot of cozy's code because it is quite a mess at some places, but a small feature like that will probably don't have a big impact on this.

    点赞 评论 复制链接分享
  • weixin_39689347 weixin_39689347 5月前

    Just a quick question regarding the .ui files, if you wish to help. I could not find the answer online but I also did not spend any time reading GTK3 manuals:

    I assume the .ui files were created using Glade3 designer? I have no prior experience working with GTK3 apps or with Glade3, so I tried to open the the timer_popover.ui and add a simple element for starters to get a hang of things. However, no matter in which container under timer_popever I try to add any gtk element, Glade will give me this error message: Widgets of type <container_type> need placeholders to add childer.. I don't get it. What is a placeholder? How can timer_popover have 2 nested elements inside it, GtkBox and GtkGrid, and there's no way to add third element next to them?

    点赞 评论 复制链接分享
  • weixin_39567870 weixin_39567870 5月前

    Firstly: nice that you're diving into this! Yes I'm using Glade3. The timer_popover itself can only have one child. This is the case for almost every component in gtk. To house multiple elements in a specific order you'll need to use e.g. GtkBox. The Box and Grid you've found is not directly housed inside the timer_popover, but in another GtkBox. If you want to add another item below the already existing items you'll need to adjust the first GtkBox: timer_popover -> GtkBox

    Then on the right side on General you'll find a property "Number of items". If you increase this, you can add another item below the others. You can also rearrange the childs if you select them and jump into "Packing" on the right side.

    I hope this helps, if it doesn't I'll have a look for a tutorial or provide some screenshots ;)

    点赞 评论 复制链接分享
  • weixin_39689347 weixin_39689347 5月前

    "Number of items"

    Of course, I did not expect this to be a separate thing that you need to set yourself. Thanks! I'll update this thread with similar "complex" questions if I hit the wall again.

    点赞 评论 复制链接分享
  • weixin_39689347 weixin_39689347 5月前

    So I realized I need to run the local build separately with special path environment variables. The local build starts, but since I have added new .ui file to the project for testing purposes and I am trying to load it in sleep_timer.py, cozy says it cannot find the new .ui file. Does the build process wrap all of the .ui files inside this file:

    installation/share/com.github.geigi.cozy/com.github.geigi.cozy.ui.gresource

    and fetch them from there during runtime? If so, how does the build process decide where the .ui files will be collected and packed? I have my custom timer_popover_power.ui file placed in cozy/data/ui where cozy/ is the project root directory.

    Here's the command I use to run the local build, I have a .sh file in the root of cozy.

    
    XDG_DATA_DIRS=$PWD/installation/share:/usr/share PYTHONPATH=$PWD/installation/lib/python3.8/site-packages installation/bin/com.github.geigi.cozy
    

    Is there a way to develop, debug and run the project in PyCharm Community Edition straight up without these extra build steps?

    EDIT:

    Another thing regarding the user interface. Automatic shutdown is a pretty drastic feature and it can potentially lead to unwanted loss of data if the user is careless. That's why I think there should be some safety measure or prompt before the shutdown feature would be enabled. By default, maybe the shutdown and sleep feature should be disabled, and it could be enabled, for example, from Preferences -> Behaviour -> Sleep timer -> Allow system shutdown and sleep etc.

    点赞 评论 复制链接分享

相关推荐