preventing activity to be destroyed when device sleeps - wear-os

How can I manage to keep my activity in the foreground when I come back from sleeping mode?
I tried this (but it looks like it's only used to prevent the device to sleep)
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

What do you mean by "sleep" mode? In a normal situation, your activity goes away and your watch-face takes over. If you don't want the watch-face to take over but want the activity to be in low-powered mode, you can have your activity go into ambient mode by extending WearableActivity and calling setAmbientEnabled() and use the relevant callbacks to stay informed about when your app enters the ambient mode to avoid using excessive CPU, and to resume normal operation when you exit the ambient mode.

Related

Q: Suspended .NET process leaks window management objects (aka. USER Objects)

On Windows, what are all the possible creation entry points for user objects? I have been trying to isolate a handle leak in a .NET application for quite some time now. Here is what baffles me:
• Leak only occurs in window management objects (aka. USER objects - not kernel, not GDI).
• Leak only occurs when the application has focus. Clicking on another application "pause" the leak until we click on the application window again.
• Leak occurs even if process is suspended (through task manager or debugger).
I've been trying to intercept any and all object creation methods, but my Visual Studio debugger remains suspiciously silent. I do get the occasionnal call to some user32.dll methods (as described in https://learn.microsoft.com/en-us/windows/desktop/sysinfo/user-objects), but nothing that would account for the leak or come close to the system limit of 10 000 objects.
Any suggestion? Does anyone knows if some hidden system thread remains active even when my application is suspended? Thank you.
Ok, our team has been able to find one probable cause for the issue.  Turns out that Microsoft's documentation tells only half the story.  If at some point user and kernel objects were completely separate, recent versions of Windows have blurred the line.
As such, DDE transaction, monitor, keyboard layout, keyboard file, event hook, timer, input context, device info, touch data, gesture data and HID data should all be added to Microsoft's list of user objects.  Some kernel methods may rely on user objects to provide services to apps.
To be specific, calls to RegisterRawInputDevices will result in the application receiving WM_INPUT messages.  Those messages are backed by HID data​ and must be processed in a timely manner.  If the process is suspended, unresponsive or otherwise fails to process incoming messages, there will be an accumulation and an exhaustion of system resources.  This can quickly escalate into the problem described above and lead to an application crash.

How to prevent screen power management from activating?

I am using Awesome WM, and I use xscreensaver to lock the screen if I'm away for a certain amount of time. I also enabled screen power management in the xscreensaver configuration to turn off the monitor after some time. I want to implement a "persentation mode", which disables these. I can disable screen locking by calling xscreensaver-command -deactivate periodically. However, it doesn't stop the screen from going idle. I tried blocking idle in systemd-inhibit, but that doesn't help either.
Is there any way to temporarily disable screen power management in xscreensaver? Or, alternatively, is there any alternative screen locker / power management tool that allows locking the screen, turning off the screen, and temporarily disabling itself from command line?
It's hard to tell what exactly turns off your screen. You can give xset a try. You can control the screensaver (screen going black after some while) with it. For example xset s off.
From man xset:
For screen-saver control:
s [timeout [cycle]] s default s on
s blank s noblank s off
s expose s noexpose
s activate s reset
For screen locking I love to use slock.
EDIT: Slock doen't have a auto lock capability. You need a tool (for example xautolock) which watches your X and calls slock.
xautolock -time 2 -locker slock

discontinuous BitBlt capture

I am using BitBlt heavily in my project. I create a number of threads and in each thread I capture the screen by BitBlt. It works great and as expected for now except the following problem.
The problem happens when the user clicks on a running program or for example already opened explorer on the taskbar. You know when you click on the running program on the taskbar, it either minimizes or appears on the screen. The issue that I am talking about happens just in this transition. At that moment, something like an interrupt, all threads stop capturing the screen for a fraction of a second and then they continue capturing. The same thing happen when you move down or up the thing on the volume control window. Could you please shed some light why this is happening and how I can prevent this happening?
Thanks.
Jay
It could be a scheduling issue. When you activate an app, it gets a small, momentary boost in its priority (so that it can seem responsive in the UI). This boost might last about as long as the animation and momentarily pre-empt your screen capture threads.
It's also possible that the desktop manager is serializing stuff, and your bitblts are simply stalled until the animation is over. Even if you've turned Aero off, I believe the desktop window manager may still be in compositing mode, which has the effect Hans Passant was describing in the comments.
If you're trying to make a video from the screen, I think it's going to be impossible to rely on GDI. I strongly suggest reading about the Desktop Window Manager. For example, this caveat directly applies to what you're trying to do:
Avoid reading from or writing to a display DC. Although supported by DWM, we do not recommend it because of decreased performance.
When you use GDI to try to read the screen, DWM has to stop what it's doing, possibly render a fresh copy of the desktop to video memory, and to copy data from video memory back to system memory. It's possible that the DWM treats these as lower-priority requests than an animation in progress, so by the time it responds to the BitBlt, the animation is over.
This question suggests that DirectShow with a screen capture filter might be the way to go.

Autoit anti-idle script after screen lock

I'm trying to make an simple anti-idle script (that moves the mouse or whatever) to prevent an application from stopping.
How can I keep it running after screen lock ?
It seems like this is explained in the Autoit faq :
http://www.autoitscript.com/wiki/FAQ#Why_doesn.27t_my_script_work_on_a_locked_workstation.3F
On locked station any window will never be active (active is only dialog with text "Press Ctrl+Alt+Del") In Windows locked state applications runs hidden (behind that visible dialog) and haven't focus and active status.
So generally don't use Send() MouseClick() WinActivate() WinWaitActive() WinActive() etc.
Instead use ControlSend() ControlSetText() ControlClick() WinWait() WinExists() WinMenuSelectItem() etc. This way you may have your script resistive against another active windows. It's possible to run such script from scheduler on locked Windows station.
You can't automate anything after your screen is locked. User input is simply ignored. A much easier way would be to prevent your screen from locking, for example, by moving the mouse randomly every 30 seconds.

screensaver hurts CUDA performance?

I've noticed that the running times of my CUDA kernels are almost tripled the moment the screensaver kicks in. This happens even if it's the blank screensaver.
Oddly enough, this appears to have nothing to do with the power settings. When I disable the screen saver and let the screen power off, the performance stays the same. When I set the "Turn off monitor" to "Never" and lets the screen saver kick in, it happens.
Why does this happen?
Is there a way to counteract this phenomena?
Is there a way to tell windows not to kick in the screen saver? (How do media players do it?)
I'm working on XP SP2 x64
Firstly, its interesting that CUDA is so impacted.
But here is the recipe in win32 for avoiding the screensaver kicking in:
A normal approach is to send yourself 'fake' key presses occasionally using the SendInput API, to reset the inactivity timer that triggers the screensaver.
It is possible to stop applications doing this, however, using the SPI_SETBLOCKSENDINPUTRESETS parameter for SystemParametersInfo.
Another approach is just to turn the screensaver off programmatically, using SPI_SETSCREENSAVEACTIVE for SystemParametersInfo. However, this is a global setting for the whole user - what if two programs use this overlapping? Try to avoid this!

Resources