Is there any api or method in qt to put the device in sleep mode after a specific time if no response is coming from user? - linux

I am creating an application for linux and want to put the device screen in sleep mode after a particular time like 5 minutes, 10 minutes etc if no response or event occur by the user side.

Piggybacking on the other answer a bit here.
You could install an eventFilter at a top-level in your application to monitor any input events as they occur (whether that be a mouse click or move event, keyboard event, etc.). At the same scope as that eventFilter, you could then use some QTimer with the interval set to your sleep timeout duration, which you restart every time an input occurs. That QTimer's timeout signal could be hooked up to a slot where you put the system to sleep via a QProcess call out to sh for example.

I do not think that there is a possibility for qt to do that, because a sleep mode is handled by the desktop environment like GNOME, Xfce, and so on. Qt itself isn't able to do this, but maybe you could use a function which just sends a command to the os, which has the opportunity to set the system to sleep. Maybe this could help you:
https://linuxer.eu/put-linux-into-sleep-from-command-line/

Related

make python wait for external trigger

I'm trying to set up a sort of basic screensaver on a Raspberry PI.
I'm using python because I like it a lot more than bash.
The script uses popen to call feh and display a slideshow.
There's also NodeRed running on this Pi, and that manages the actions bound to a few GPIO buttons.
I would like to intercept those button inputs to stop the slideshow.
I thought of 3 ways:
use NodeRed to kill (-15 or -9) feh. But this leaves behind a "defunct" process, and python should rely on the absence of the spawned process before spawning a new one
use the RPi.GPIO library and bind a callback event to the button press. This doesn't work because it throws the error that the channel (i.e. the GPIO pin) is already in use, and the RPI crashes and has to be power cycled.
use a OS environment variable. This could work, but I don't want to continually be polling the variable. The feh process needs to stop immediately upon a button press.
So how else could this be achieved?
Is there a way to bind a function to some external trigger?
I realized that since the script logic is very simple and only has one task, it would have been easy to use a socket (synchronous and blocking) and wait on that for NodeRed to send its command. And indeed it was

What can cause a thread to be throttled to 25% on Mac OS?

I have a C++ application on Mac OS X. The app runs an event processing with the glfw library on the main thread and reads input and execute commands on a background C++ std::thread.
I am observing a frustrating phenomenon that I cannot explain so far.
If I make a call to a long running function on the background thread, initially that thread is using 100% of a core. But, after it has used a few seconds of CPU (10 seems to be the magic threshold), it gets throttled down to 25%.
If I start a computation run on a thread in the background before starting the glfw event processing loop (the event processing is essentially stuck waiting for events, as I don't even open a window), then it can use 100% for as long as it wants.
My biggest problem is that I have no idea what could be causing this nor how to figure it out. I've tested retrieving the pthread sched_param and changing the sched_priority from what seems to be default 31 to various values between 20 and 60 and it does not help.
I have identified one more condition for the phenomenon to happen:
The background thread has to have read from the terminal. It happens when I run the following background thread and enter a line for the computation to take place:
std::thread cmd([argc, argv, &scriptingRunner] {
for (std::string line; std::getline(std::cin, line); ) {
longComputation();
}
Perhaps App Nap is throttling your application to save energy. To check, open the Activity Monitor program and right-click on the header of the processes table to bring up the context menu, and click on "App Nap" in the context menu to enable the App Nap column; then look at your process in the table and see if its value in the App Nap column switches to "Yes" when the fault occurs.
If you want to disable app nap for your app, see the code listed in the question here.

Tkinter after() fails when system time is changed

Is there a way to cancel and reschedule tkinter after() events when the system time is updated, or preemptively update the after() scheduler?
Description
I have a custom GUI created in tkinter using Python3.7 on Raspbian (fork of Debian-Linux for Raspberri Pi). I created a screen to adjust the system time.
If the date & time are set to the future, everything is OK, but if it's set to the past, anything using the tkinter after() method to schedule events never happens. I believe after() works by polling system time, and the scheduled times are now far in the future.
I verified this by setting the time for a minute into the past. Initially, any elements using the after() function stopped updating, but the GUI was still responsive to click events. A minute later, those elements continued to update.
Related
This Question back in 2010 is about the same problem, but has no solutions.
Side Note - the user running the GUI doesn't have privileges to set the time, so it is actually starting a service (which it has the privilege to do), and writing a time string to a FIFO pipe. The service running as root reads the pipe and updates the time. I have verified the time is correctly set with this method.
Update
I never came up with a good way for this to work. So I have a message box to say that application would need to restart after the time change so the user at least knew what was about to happen. If they were OK with that, the application terminated and relaunched itself.

Equivalent of NSRunLoop under linux (Raspberry Pi)?

OS X has the NSRunLoop which just sits around waiting for timers and sources to fire. Then Apple switch to Grand Central Dispatch (GCD) where you have dispatch_main() to keep the app alive and a bunch of dispatch_source_'s to schedule stuff or to get notifications (from sockets or user actions). If nothing is happening, the app is just idle and doesn't use any CPU power.
Now I want to learn how to write a driver with c++. So I have a raspberry pi (so linux) and once in a while data is coming in from a socket or interrupt.
Instead of polling I want to work with events.
So I am searching for the equivalent of NSRunLoop for c++ and linux.
Though I would also like to learn how something like that is or can be implemented. In pseudocode the run loop acts something like this, as far as I know,
timeout = 0
while (true) {
wait(timeout) || wait for source event other than timer
loop all timers
if timer fired
run timer action
loop all timers
timeout = min(timeout, timer.timeNextEvent)
loop all sources
if source hasData
run source action
}
The thing I don't get is the wait function at the top. How do you wait for a source lets say a timer to fire without going into sleep mode?
I have found many examples of polling and timers that go into sleep mode. But I want to avoid sleep and just wait on interrupts or signals or a user generated event like keyboard input on the command line.
Any pointers on how to proceed?
Probably it's late for this answer but in case someone ends up here, epoll should be the answer.
https://man7.org/linux/man-pages/man7/epoll.7.html

Keeping main thread (Qt Application) responsive while some other thread is processing (POSIX)

System / software Details :
Qt Version 4.7.4
Linux
Kernel : 2.6.31 (Custom kernel built for IMX25)
Peripherals :
Graphic LCD (64x128)
Quectel (M 12) GPRS module
Thermal printer
Database : Sqlite3
I am a beginner don't have much experience either in Qt or programming with Linux. I have developed an application where user enter some data manually and that data gets saved in sqlite database. So what I am trying to do is that after certain time lets say 90 seconds data from database should get transferred to the server using the GPRS.
So I am using the Qt's signal and slots mechanism to do the timed data transfer. I have created a slot which gets fired every 90 seconds and as the slot gets fired I am creating/launching a POSIX thread which is suppose to transfer the data to the server.
So what that thread does is it launches the "pppd" and once the "pppd" is up it queries the database for the data and sends the data to the server. And once the data transfer is done I kill the "pppd". The functionality works fine.
But the problem is that the "pppd" takes time to launch so I had to introduce some delay. i.e sleep of 12 seconds is there in order to let the pppd launch successfully. But as the sleep is blocking it makes the main program/thread non responsive until the "pppd" is launched (i.e. it halts/stops all the activities like printing etc.). And as the "pppd" is launched the main thread becomes responsive again.
So please suggest me some solution in order to keep the main thread responsive when "pppd" is launching or please suggest me if there is any other alternative for the same. Also guide me if there's anything wrong with my approach..
Thanks in advance. And I am sorry if I have not followed your standards..
There are several options available to you. It looks like you're using a thread, but then calling sleep in the main thread, which is basically the same as not using thread to do your work at all! You need to leverage Qt processes as illustrated below:
You can use a QProcess in which you start() and use the signal finished() to notify the main thread that it is complete. This is probably what you want to use. All you have to do is:
QProcess *proc = new QProcess;
proc->start("theProcess", QStringList() << "the" << "arguments");
connect(proc, SIGNAL(finished()), this, SLOT(someSlotToUse()));
This sequence of code will start a new process with your arguments, and then call someSlotToUse() when the process is complete.
You can use a QThread along the same pattern.
Basically, what you're trying to do is do the work in another thread, keeping the GUI reactor free to process GUI events rather than long queries. This is a classic problem in Qt and there is a lot of literature out there.
Alternately, you can use a QProcess::concurrent() call, which allows you to run a function in another process, but I've never tested it.
Here are some references for you to look at: Qt Concurrent, QProcess, and QThread

Resources