How do I detect desktop transition effects? - c++

I want to minimise my application, take a screenshot of the current desktop and return my application back to its original state.
This has been working fine under windows XP, however under testing on different Vista machines the minimise time of 200 milliseconds is no longer valid.
Is there a way to ask the operating system when it has finished these fancy effects or lookup how long it has been given to perform the operation?

While I don't know of a way to do what you ask, I have a suggestion: instead of minimizing your application's window, why not hide it (with ShowWindow(SW_HIDE))? That will not be subject to the animation effects, so should be pretty much instantaneous.

Maybe instead minimizing you should bring desktop to front?

The closest I can find is SPI_GETUIEFFECTS, which tells you if such effects are enabled at all.
If enabled, you could of course use SPI_SETUIEFFECTS to turn them off. But that's a rather shotgun method - how would you restore them? It's probably better to temporarily turn off the ones that bother you most.

Related

Windows 7 applications run slower when not focused

I'm attempting to run two applications simultaneously on windows 7, however, I'm finding that when I do this, whichever has focus runs at a normal speed but the other is clearly running at a far slower speed. (For reference, one is a unity application and the other is a C++ direct X application). Has anyone ever encountered something like this? Is there a way to allow both applications to run at full speed? The system ought to have the resources to run both, neither are very complex. When I monitor the system resources, etc, everything looks good.
Windows automatically offers less system resources to unfocused programs no matter their complexity or requirements. I don't believe you can disable that.
That makes sense. I looked into a bit deeper and found that the Desktop Window Manager was the one causing the headache. I stopped the service, set the processor affinity for each application, and everything was golden after that.

C++ GetAsyncKeyState and GetCursorPos vs Windows Messaging

I am working on a application that involves remote control. The keyboard and mouse state gets updated about 100 times a second, saved on arrays, sent on the internet, and reproduced. Perfect reproducing timing is required. Since now I only coded the keyboard part and it was actually easier to program than windows messaging. All I had to do is call GetAsyncKeystate every 9 milliseconds on the host, and then, on the client, use SendInput every 9 milliseconds to get perfect timing. The other side of the medal is, I will have to manually check if the host window is highlighted, and if is not, avoid calling GetAsyncKeyState. But now that i'm about to code the mouse part, I have a doubt about what method to use, since perfect timing for mouse will be difficult to achieve even without window messaging. That's why I am asking to programmers that are more experienced than me:
In this case, is it better to use a combination of GetAsyncKeyState and GetCursorPos or is it better to use Windows Messaging? What are the positives and negatives of both? Thanks in advance.
You'll want to use a Windows hook. See SetWindowsHookEx and related documentation. This can be used for keyboard events as well.
On recent Windows versions there is also a newer, asynchronous input capture technology available whose name escapes me at the moment. Google for that as well.
EDIT:
I remember now: The other technology is known as event hooking. See the SetWinEventHook function.
GetAsyncKeyState and GetCursorPos is your best choice. The simpler the code, the faster your program will be able to be. Considering that sending windows messages through functions like SendMessage() gives you the option to fiddle with a lot of different aspects of different programs, this usually means that there are more processes happening inside the SendMessage() function and that speed is taken away in order to provide practicality for other applications.
I would also like to point out that you will never be able to get perfect timing since you are in fact collecting data from the source computer, passing it to an array and sending it online.
Summary:
Pros of using GetAsyncKeyState and GetCursorPos:
You won't find any functions retrieving data faster than these functions given that they are functions that only look to do small basic tasks. SendMessage type functions will be slightly slower since they have more coding inside them, allowing them to work with more than just mouse and keyboard functionality.
Cons... I don't really see any.

Move Window Linux C

i'd like to make a function that can move a window in Linux in C++ by its PID. So I've tryed in under Windows. But I have trouble to compile it for Linux.
Is there any mean to do it with Qt ? Since I haven't found one, I've tryed to compile for Linux.
I'm using the MoveWindow function, which is part of the Windows API. Is there any Linux equivalent ?
You don't have to do that by hand if you don't really want to as there already are lots of tools out there, that can perform such tasks as moving, resizing, maximizing and whatever windows.
One tool you might want to take a closer look upon goes by the name of wmctrl even if you don't intend to use maybe you'll find some interesting tricks by taking a look into the sources.
The task of moving a window only known by the pid of the client that created the window might not be the easiest tasks of all for a couple of reasons.
First of all you really shouldn't try to do this as in the X Windows philosophy it is the job of the window manager to arrange the windows on the screen.
As well ICCCM (see: http://de.wikipedia.org/wiki/Inter-Client_Communication_Conventions_Manual) as the EWM spec (see: http://standards.freedesktop.org/wm-spec/wm-spec-latest.html) strongly discourage any client from trying to move resize or whatever on its own. Most probably moving windows "owned" by another client might be considered even bigger evil.
The second problem you might face is, that the X 11 protocol doesn't have any notion of pid.
As it was designed to be used over a network you never can't be really sure the program runs on the same machine as the one you are currently sitting in front of. As such there isn't much sense in something like a pid as by chance there might be any number of clients with identical pids displaying windows on the same X Server if they ran on different machines.
Fortunately enough it is not all that bad, as the EWMH spec encourages any client to set the _NET_WM_PID property on its top level window the the pid of the client that created the window.
Again adhering to the EWMH spec isn't enforced by the X Server in any way so that while practically propably almost all clients will set it there's still no guarantee you'll find the window belonging to a specific pid.
Possibilities
While the whole points mentioned until here might seem rather limiting in fact most probably rather the opposite is true. Even because practically it is relatively easy to totally mess up any other client running in an X session the whole set of rules about how to be a good citizen in the X word were introduced.
As the X11 protocol itself is a network protocol (well not 100% true as locally running clients most probably will be communicating with the X Server via a UNIX domain socket) there isn't any specific library required to talk to the X Server.
Talking about C as mentioned in your question the Xlib has long been the one and only one implementation in wide use but there's also another binding called xcb. With a slightly changed API in comparison to the Xlib.
Xlib
Speaking Xlib I've never ever used any xcb until now, so I can't tell you too much about it might be the following methods that might be of use.
XOpenDisplay - open connection to the X server
XQueryTree - aquire the tree of windows currently alive on the server
XInternAtom - no fear it isn't dangerous. Just read about it in the manuals as you'll need it the get the "atom" mapping to _NET_WM_PID mentioned above
XListProperties - search for the _NET_WM_PID property with the value you are looking for
XConfigureWindow, XMoveWindow, XResizeWindow, ... - to finally perform whatever you wish to do.
All functions mentioned above should be documented in the manual pages. Just use man XOpenDisplay for example.
Oh, and be sure to learn about all the other tools at your disposal to further investigate about the X Window world. Run xlsatoms, check what xwininfo reports an take a list at the output of xprop for one single (!) window alone. Try to set some yourself to see what happens xprop will even do that for you if you ask politely.

Why does windows let you draw on the whole screen?

I've been playing a big with the DC obtained with CreateDC(L"DISPLAY",NULL,NULL,NULL) and I've been wondering why does windows let you draw on the whole screen that easily, cause I think you could do some pretty evil stuff with that like putting a TIMER at 1ms and drawing a black rectangle on the whole screen every time the timer ticks.
The fact that you could do some pretty evil stuff doesn't mean windows shouldn't let you do it. Just think of all the other evil things you could do:
Run in an infinite loop and eat up all the cpu time.
Write random bits to a file until you fill up the whole hard disk.
Delete random files all over the place.
Allocate memory like crazy until the computer slows to a crawl.
Just because you CAN do those things doesn't mean windows should prevent you from writing to the hard drive or allocating memory or deleting files.
The purpose of Windows is to provide an environment in which programs can run. The more flexible they make that environment, the more interesting (and, unfortunately, devious) programs it makes possible for developers to create.
If they started putting in arbitrary restrictions on what you can do because you might abuse it... well, then it wouldn't be windows, it would be an iPhone :)
why does windows let you write to the hard drive so easily?
you could do some pretty evil stuff like overwrite every file on the hard drive.
The security of the desktop is given to the user running the desktop, you can't draw on it if you are not a privileged user.
Note that one doesn't usually CreateDC() on the desktop, but usually GetDC() for a particular window during the WM_PAINT message handler.
A program can also delete the file system, or destroy the registry (if suitably permissioned), the desktop is a user-permissioned resource like any other. If they run an application with their security credentials, they can do what they wish.
However in practice, one would create a window and paint within it.
Because it should be that easy.
It is that easy because to put rules and controls in place would mean that you would be cutting down the things you can do with the language and the windows framework. If this happened then there would be screams from the other side of the fence shouting at how you can't do this and that.
It is these abilities which make the language powerful, but with that power comes the danger. Just because you can do something, doesn't mean that you should. You can format you hard drive... doesn't mean that you should do this when you launch the clock application.
If you are not happy with this level of 'responsibility' then pick a different language or framework to write in.
Everything is a Window and Every Window has a HANDLE. So, if you have got DesktopHandle, then you can draw anything on it. What is the problem with it.
Offcourse, the application that is doing evil stuff(like you said) has been allowed to run on the machine by yourself, therefore, it can do more eviler stuff than this such as formatting your hard-drive etc.
If the method you're using (getting the screen DC) was disabled, it wouldn't stop people from doing the following.
You can create a window, you can paint in the window, you can set the size of the window to cover the whole screen, therefore you can paint on the whole screen.
And you can grab a bitmap of the whole screen, so you can paint the underlying screen content in the window and then make adjustments to it.
So it would be very easy to simulate the same effect using a combination of things that, on their own, are perfectly valid and extremely useful.
Because there may be a time when you need to do these things. I am sure at the moment you can't think of any but writing on the screen may be useful.
On OS X there are many applications who write directly on the screen. Useful information like CPU time or even a calender. That's cool!
But not everything that can be done must be done.
One of the primary reasons Windows is so afflicted with malware is the lack of security around such things as you describe. Others have cited examples such as filling the hard drive, erasing random files, or eating CPU time... all of these things are security concerns, and all of them are prevented by the other two major operating systems (Linux and OSX). This doesn't mean that you can't do similar things on those operating systems, but it means that a normal user can't do it. They'd have to be granted the right permissions, and usually also forced to use a very restrictive API to limit what they can do. So the answer to your question is "because it wasn't designed with security in mind". This allows programmers significantly more flexibility, and these powers can be used for good, but IMHO it more often breeds laziness (people use the brute force way instead of spending the time figuring out the "right" way to do something) and opens the door for security problems (malware).

Running background services on a PocketPC

I've recently bought myself a new cellphone, running Windows Mobile 6.1 Professional. And of course I am currently looking into doing some coding for it, on a hobby basis. My plan is to have a service running as a DLL, loaded by Services.exe. This needs to gather som data, and do som processing at regular intervals (every 5-10 minutes).
Since I need to run this at regular intervals, it is a bit of a problem for me, that the system typically goes to sleep (suspend) after a short period of inactivity by the user.
I have been reading all the documentation I could find on MSDN, and MSDN blogs about this subject, and it seems to me, that there are three possible solutions to this problem:
Keep the system in an "Always On"-state, by calling SystemIdleTimerReset periodically. This seems a bit excessive, and is therefore out of the question.
Have the system periodically waken up with CeRunAppAtTime, and enter the unattended state, to do my processing.
Use the unattended state instead of going into a full suspend. This would be transparent to the user, but the system would never go into sleep.
The second approach seems to be preferred, however, this would require an executable to be called by the system on wake up, with the only task of notifying my service that it should commence processing. This seems a bit unnecessary and I would like to avoid this extra executable. I could of course move all my processing into this extra executable, but I would like to use some of the facilities provided when running as a service, and also not have a program pop up (even if its in the background) whenever processing starts.
At first glance, the third approach seems to have the same basic problem as the first. However, I have read on some of the MSDN blogs, that it might be possible to actually conserve battery consumption with this approach, instead of going in and out of suspend mode often (The arguments for this was that the nature of the WM platform is to have a very little battery consumption, when the system is idle. And that going in and out of suspend require quite a bit of processing).
So I guess my questions are as following:
Which approach would you recommend in my situation? With respect to keeping a minimum battery consumption, and a nice clean implementation.
In the case of approach number two, is it possible to eliminate the need for a notifying executable? Either through alternative API functions, or existing generic applications on the platform?
In the case of approach number three, do you know of any information/statistics relevant to the claim, that it is possible to extend the battery lifetime when using unattended mode over going into suspend. E.g. how often do you need to pull the system out of suspend, before unattended mode is to be preferred.
Implementation specific (bonus) question: Is it necessary to regularly call SystemIdleTimerReset to stay in unattended mode?
And finally, if you think I have prematurely eliminated approach number one, please tell me why.
Please include in your response whether you base your response on knowledge, or are merely guessing (the latter is also very welcome!).
Please leave a comment, if you think I need to clarify any parts of this question.
CERunAppAtTime is a much-misunderstood API (largely because of the terrible name). It doesn't have to run an app. It can simply set a named system event (see the description of the pwszAppName parameter in the MSDN docs). If you care to know when it has fired (to lat your app put the device to sleep again when it's done processing) simply have a worker thread that is doing a WaitForSingleObject on that same named event.
Unattended state is often used for devices that need to keep an app running continuously (like an MP3 player) but conserve power by shutting down the backlight (probably the single most power consuming subsystem).
Obviously unattended mode uses significantly more powr than suspend, becasue in suspend the only power draw is for RAM self-refresh. In unattended mode the processor is stuill powered and running (and several peripherals may be too - depends on how the OEM defined their unattended mode).
SystemIdleTimerReset simply prevents the power manager from putting the device into low-power mode due to inactivity. This mode, whether suspended, unattended, flight or other, is defined by the OEM. Use it sparingly because when your do it impacts the power consumption of the device. Doing it in unattended mode is especially problematic from a user perspective because they might think the device is off (it looks that way) but now their battery life has gone south.
I had a whole long post detailing how you shouldn't expect to be able to get acceptable battery life because WM is not designed to support what you're trying to do, but -- you could signal your service on wakeup, do your processing, then use the methods in this post to put the device back to sleep immediately. You should be able to keep the ratio of on-time-to-sleep-time very low this way -- but as you say, I'm only guessing.
See also:
Power-Efficient Apps (MSDN)
Power To The People (Developers 1, Developers 2, Devices)
Power-Efficient WM Apps (blog post)