Method in C++ to get the available screen space on Linux - c++

I'm looking for a way to get the actual available screen space, similar to the Cocoa NSScreen visibleFrame method, but on Linux. That would be the displays resolution minus the menu-bar/dock/title-bar.
I'm using SDL2 for the windowing code, but can't find anything within the library documentation that might help.
SDL_GetWindowMaximumSize seems like the closes candidate, but this is returning 0,0 for me.
Any ideas?

Use SDL_GetDesktopDisplayMode(),( previously SDL_GetVideoInfo() in
SDL1.2)
https://wiki.libsdl.org/SDL_GetDesktopDisplayMode

Related

How to use Skia on Windows or Linux

I have built Skia. What do I do now to get an SkCanvas and draw on it? This answer does not properly answer the question because
A: It uses API that doesn't exist anymore, like bitmap.lockPixels(),
B: It uses some unkown context and draws a this from it,
C: It doesn't actually create an SkCanvas?...
Ideally I would like an example that doesn't assume anything. A full C++ program that draws something on the screen using Skia.
Follow this detailed procedure (for Linux too!).
If you have problems doing that, in the comments I have proposed solutions for problems I have encountered.

How do I collect input from an external window using SDL?

I'm currently trying to re-write my binder between Ogre and SDL in my game engine. Originally I used the method outlined in the Ogre Wiki here. I recently updated my version of SDL to 1.3 and noticed the "SDL_CreateWindowFrom()" function call and re-implemented my binder to allow Ogre to build the window, and then get the HWND from Ogre to be passed into SDL.
Only one window is made and I see everything renders properly, however no input is collected. I have no idea why. Here's the code I am currently working with (on windows):
OgreWindow = Ogre::Root::getSingleton().createRenderWindow(WindowCaption, Settings.RenderWidth, Settings.RenderHeight, Settings.Fullscreen, &Opts);
size_t Data = 0;
OgreWindow->getCustomAttribute("WINDOW",&Data);
SDLWindow = SDL_CreateWindowFrom(&Data);
SDL_SetWindowGrab(SDLWindow,SDL_TRUE);
I've tried looking around and there are a number of people that have done this to one degree of success or another(such as here or here). But no one seems to comment on handling the input after implementing this.
I originally thought that maybe since SDL does not own the window it wouldn't collect input from it by default, which is reasonable. So I searched the SDL API and only found that one function "SDL_SetWindowGrab()" that seems to relate to input capture. But calling that has no effect.
How can I get SDL to collect input from my Ogre-made window?
It has been a while, but I figured I would put in the answer for others that may need it. It turned out to be a bug/incomplete feature in SDL 1.3. The "CreateWindowFrom" method wasn't originally intended to be used exclusively as an input handler. At the time of this writing I know myself and another on my team wrote patches for Windows and Linux that permitted this use to work and submitted those patches to SDL.

OpenGL/D3D: How do I get a screen grab of a game running full screen in Windows?

Suppose I have an OpenGL game running full screen (Left 4 Dead 2). I'd like to programmatically get a screen grab of it and then write it to a video file.
I've tried GDI, D3D, and OpenGL methods (eg glReadPixels) and either receive a blank screen or flickering in the capture stream.
Any ideas?
For what it's worth, a canonical example of something similar to what I'm trying to achieve is Fraps.
There are a few approaches to this problem. Most of them are icky, and it totally depends on what kind of graphics API you want to target, and which functions the target application uses.
Most DirectX, GDI+ and OpenGL applications are double or tripple-buffered, so they all call:
void SwapBuffers(HDC hdc)
at some point. They also generate WM_PAINT messages in their message queue whenever the window should be drawn. This gives you two options.
You can install a global hook or thread-local hook into the target process and capture WM_PAINT messages. This allows you to copy the contents from the device context just before the painting happens. The process can be found by enumerating all the processes on the system and look for a known window name, or a known module handle.
You can inject code into the target process's local copy of SwapBuffers. On Linux this would be easy to do via the LD_PRELOAD environmental variable, or by calling ld-linux.so.2 explicitly, but there is no equivalient on Windows. Luckily there is a framework from Microsoft Research which can do this for you called Detours. You can find this here: link.
The demoscene group Farbrausch made a demo-capturing tool named kkapture which makes use of the Detours library. Their tool targets applications that require no user input however, so they basically run the demos at a fixed framerate by hooking into all the possible time functions, like timeGetTime(), GetTickCount() and QueryPerformanceCounter(). It's totally rad. A presentation written by ryg (I think?) regarding kkapture's internals can be found here. I think that's of interest to you.
For more information about Windows hooks, see here and here.
EDIT:
This idea intrigued me, so I used Detours to hook into OpenGL applications and mess with the graphics. Here is Quake 2 with green fog added:
Some more information about how Detours works, since I've used it first hand now:
Detours works on two levels. The actual hooking only works in the same process space as the target process. So Detours has a function for injecting a DLL into a process and force its DLLMain to run too, as well as functions that are supposed to be used in that DLL. When DLLMain is run, the DLL should call DetourAttach() to specify the functions to hook, as well as the "detour" function, which is the code you want to override with.
So it basically works like this:
You have a launcher application who's only task is to call DetourCreateProcessWithDll(). It works the same way as CreateProcessW, only with a few extra parameters. This injects a DLL into a process and calls its DllMain().
You implement a DLL that calls the Detour functions and sets up trampoline functions. That means calling DetourTransactionBegin(), DetourUpdateThread(), DetourAttach() followed by DetourTransactionEnd().
Use the launcher to inject the DLL you implemented into a process.
There are some caveats though. When DllMain is run, libraries that are imported later with LoadLibrary() aren't visible yet. So you can't necessarily set up everything during the DLL attachment event. A workaround is to keep track of all the functions that are overridden so far, and try to initialize the others inside these functions that you can already call. This way you will discover new functions as soon as LoadLibrary have mapped them into the memory space of the process. I'm not quite sure how well this would work for wglGetProcAddress though. (Perhaps someone else here has ideas regarding this?)
Some LoadLibrary() calls seem to fail. I tested with Quake 2, and DirectSound and the waveOut API failed to initalize for some reason. I'm still investigating this.
I found a sourceforge'd project called taksi:
http://taksi.sourceforge.net/
Taksi does not provide audio capture, though.
I've written screen grabbers in the past (DirectX7-9 era). I found good old DirectDraw worked remarkably well and would reliably grab bits of hardware-accelerated/video screen content which other methods (D3D, GDI, OpenGL) seemed to leave blank or scrambled. It was very fast too.

Parse information from programs added to taskbar with C++

Basically what I am trying to do is write my own pseudo task bar in C++. The program needs to idle until another program is started up, at which point it needs to visually depict that the other program is running. For each other program that is running, the user should be able to click on the visual representation and have Windows switch focus to the selected program.
The big underlying question at this point: is this even a possibility? Or has Windows hidden most/all of its fiddly-bits to make this close to, if not completely, impossible?
[EDIT:] restructured the question
The obvious starting point would be SetWindowsHookEx(WH_SHELL,...); which will get you notifications when top-level windows are created or destroyed (along with some other related events, like a different window being activated, a window's title changing, etc.)
Think ahead to actually bringing the window to the front, as I once researched myself.
SetForegroundWindow() won't work unless issued from the foreground process - neither SwitchToThisWindow() nor the AttachThreadInput() kludge seemed to always work, but maybe I just wasn't doing it right. Anyway as far as I know there no way to make a window foreground as good as Windows does, please enlighten me if say you discover say an undocumented call which actually Works.
It seems possible to me at least in a basic way:
1. Set up a shell hook as described by Jerry
2. figure the executable file from the module handle to access it's icons using shell services
The Vista-like feature of keeping a 'live' miniature of the screen seems much more challenging.

What is the best way to take screenshots of a Window with C++ in Windows?

What is the best (easiest) way to take a screenshot of an running application with C++ under Windows?
You have to get the device context of the window (GetWindowDC()) and copy image (BitBlt()) from it. Depending on what else you know about the application you will use different methods to find which window's handle to pass into GetWindowDC().
GetDC(NULL) + BitBlt()
To capture translucent/alpha/layered windows, you must pass the CAPTUREBLT flag to BitBlt, if you do that, the cursor blinks, read this technet article to find out why.
On NT6+, you might be able to use the Magnification API to do what you want.
On the keybd_event function documentation it states that you can use it to take a screenshot and save it to the clipboard. For example:
keybd_event(VK_SNAPSHOT, 0, KEYEVENTF_SILENT, 0);
In my version (Visual Studio 2005 help installed on my computer) it states that you can take a screenshot of the whole desktop by setting the second parameter to 0, or a screen shot of just the current application by setting it to 1.
Taking it out of the clipboard buffer is left as an exercise for the reader.
However I'd think carefully before doing this as it will turf whatever image data was already present in the clipboard.
Here is an example code
You can do CaptureAnImage(GetDesktopWindow()); to make a screen capture.
Desktop is a "virtual" devices that interacts with user .Accordingly , it potentially contains privacy-related information . I suggest that a security check/warning prompt from OS is added when the executable tries to take screenshot unless the end user indicates explicitly that he(she) knows what is happening .