Interactive drawing in c++ - c++

I try to create an interactive window. I read an image from my pc and show it using opencv.I use cvSetMouseCallback function to get mouse position and events. I write a code that creates a rectangle on the image when a user selects points of edges of rectangle.I want to redraw it, the user click+move. But I can't find how I handle mouse release. Is there an event for this such as EVENT_LBUTTONDOWN?
I try to do this :

Related

Drawing a custom mouse cursor

I am in need of drawing a custom mouse cursor using C++ for my application. I am using a device to track hand movements and translate them into a cursor position on the screen.
However, for this application the system cursor may or may not be displayed, so I need to somehow draw onto the screen. I also need to interact with any window. Because of this, I'm not sure of my course of action - I believe I can create a "transparent" window to draw on while passing clicks/drags through onto the next window but am not sure how to go about this.
EDIT: Clarification - This application might be run on a touchscreen device where the cursor is hidden by some means. I'm trying my best to create this application as "hands-off" of the host system as possible - this program is meant to be a drag and drop application that will just work on a system with none or very very minimal actions done to the settings of the host system itself.
Currently, I have the following which draws a cursor at the location of the "mouse" but I cannot figure out how to refresh the screen or draw over the old "cursor" before drawing the new one. This causes a repeated cursor across the screen:
// Some thread
DWORD WINAPI Cursor(LPVOID lpParam) {
auto dc = GetDC(NULL);
std::optional<POINT> point;
while (true) {
if (point.has_value()) {
auto p = point.value();
RoundRect(......); // Paint over old cursor
}
POINT p;
if (GetCursorPos(&p)) {
point.emplace(p);
RoundRect(......); // Paint new cursor
}
}
I've read that simply calling another RoundRect on the same location will clear it but that's obviously not the case or I'm doing it wrong.
I believe my program can have super privileges if it needs to. Working with WINAPI seems to be kind of a hassle compared to this kind of stuff on linux, regarding permissions of working with different windows and allowing clicks and such to go through....
Any suggestions for how to proceed?

cursor shape on capture screen using DXGI

I have written a screen capture program using windows screen capture API in DXGI。It works just OK。
But on some cases (such as when I drag a window to move it), the result contains a cursor shape on it.
]
This is not what I expected.
Does ayone know how can I do to get rid of that.
As far as i know there is no way to prevent this within DXGI. Your graphics adapter will decide wether to draw the cursor seperatly (you have to do the drawing yourself) or directly in to the screen image.
So basically your options are:
Write your own graphics adapter
Check if the cursor is invisible with FrameInfo->PointerPosition.Visible (that means the cursor is already drawn in the frame) and replace the cursor region with a previously saved frame -> overdraw it
Select a fully transparent cursor

Sending an event using XSendEvent when a pointer is grabbed

I develop a plugin for compiz window manager. I want to draw a transformed window texture and send an event to that window. When a transformed window is renderer I grab pointer to take control off all xevents, the main part of compiz function which grabs pointer looks like this:
status = XGrabPointer (privateScreen.dpy, privateScreen.eventManager.getGrabWindow(), true,
POINTER_GRAB_MASK,
GrabModeAsync, GrabModeAsync,
privateScreen.rootWindow(), cursor,
CurrentTime);
When a pointer is grabbed I recalculate button press coordinates and I use XSendEvent to send events to the destination window. It works fine for google chrome window or for a simple xwindow application like this: link Unfortunately it doesn't work correctly for a window which performs OpenGL rendering - I have tested SDL and GLFW. Such window receives click events but with different parameters (xbutton.x_root, xbutton.y_root, xbutton.x, xbutton.y) than I specified in XSendEvent. Each time when I send an event those parameters contains the same values which seem to be the mouse position before xgrabpointer was called.
When a pointer is not grabbed events (from XSendEvent) are received correctly. There must be some specific relation between xsendevent, xgrabpointer and a window which performs OpenGL rendering. Moreover clients (windows) implementation may differ because my code doesn't work only for that specific windows or maybe I do something wrong ?
Edit1
Let's consider following example: I have a fullscreen window, then I can use a plugin to draw transformed window texture (for example scale transformation (0.5, 0.5, 1.0)), but X11 still sees a fullscreen window, so when I click on the region outside a transformed texture event go to the window. When I grab pointer in the plugin I'm the only receiver off all events, then I can recalculate cooridnates based on my window transfomation and send them to the window.
Edit2
When I use Freeglut all events are sent correctly to the destination window when pointer is grabbed. There have to be some differences between library implementations.
OpenGL is not concerned with input events, it just draws things, without even knowing what X11 or pointer events are.
This must be something both SDL and GLFW do in their window setup.
However I wonder why you are grabbing the pointer at all? The X Composite extension, which is the foundation on which Compiz and other compositing WMs are built already has a dedicated API for pointer input transformation, see http://www.x.org/archive/X11R7.5/doc/compositeproto/compositeproto.txt RedirectCoordinate and TransformCoordinate. You should use those functions instead of messing with grabbing the pointer. Grabbing the pointer is a really, really bad idea.
Okay, it seems that the copy of the X composite extension I linked (and I have on my computer) is an earlier draft still with the RedirectCoordinate in it. There's a newer version (which unfortunately carries the same date in its head – WTF?) where coordinate redirection has been removed.
SDL updates a mouse position when handling MouseMotion event. It doesn't use coordinates which are stored in ButtonPress event. When a window manager grabs a pointer and sends ButtonPress event to the SDL window, a mouse position inside receiver is not updated. Here is example solution (following code should be added to the section after ButtonPress label in SDL_x11events.c:
if(xevent.xany.send_event)
{
SDL_Mouse *mouse = SDL_GetMouse();
mouse->x = xevent.xbutton.x;
mouse->y = xevent.xbutton.y;
}

c++ X11 global change cursor shape

I'm writing a mouse emulation program for Linux based on input from external hardware (Leap Motion Controller). For proper operation, I want to change the shape of the cursor to convey status information to the user related to their operation of the cursor. I'm currently writing a user space program to handle this. Mouse movement and mouse clicks are handled using the XTest library, but right now I can't find a way to change the shape of the cursor.
Using XDefineCursor() I've been able to change the cursor to a custom shape on a window owned by my program, but I need it in the full graphical environment, since this program will remain
hidden in the background most of the time.
I'd also prefer to not write a device driver and stay in user space for now if possible. Also I'd prefer to not have to change the code for the Display manager (ie Unity or LXDE).
Edit:
The hardware is the Leap Motion Controller. I need to display to the user if they are still in the sensor's range, or outside the range.
You can watch for cursor changes with XFixes protocol and then modify cursor each time it is changed adding your graphics to whatever window tried to set it ( SelectCursorInput/GetCursorImage/CursorNotify event)

Getting mouse position unbounded by screen size, c++ & windows

I'm currently writing a c++ console application that grabs the mouse position at regular intervals and sends it to another visual application where it is used to drive some 3d graphics in real time. The visual app is closed source and cannot be altered outside it's limited plug-in functionality.
Currently I'm using the GetCursorPos() function which is easy and fast enough, but I'm running into the issue that all of the data is clipped based on the current screen resolution of 1920x1600 so that all x values are between 0 and 1920 and all y values are between 0 and 1600 no matter how far the mouse is physically moved.
I need to get the mouse position before it's clipped at the edge of the screen, or possibly the deltas which I could use to calculate the current position.
I've seen some references to the windows MouseMove event but I would really not want to implement a window to make it work or especially have it as the active to receive those events.
I'm working in a windows environment and a language change is not feasible.
I might be wrong, but in Win32 land you don't get mouse move messages when the mouse is at the edge of the screen because, well, the mouse isn't moving. The usual way to get an infinite mouse area is to do the following:
Hide the mouse, get exclusive access and record position
Centre mouse to window
When mouse moves, get delta from centre of screen to current position
Centre mouse to window again
The next mouse move should have a delta of (0,0), so ignore it
Go to 3 until end of mouse move operation
Reset position, show the mouse and release exclusive access
If you didn't hide the mouse, then you'd see the mouse moving a small distance and then snapping back to the centre position, which looks nasty.
This method does require a message pump for the mouse move messages so the console application idea probably won't work with this. Can you create a full screen invisible window for grabbing the mouse?
Just get the position, and move it to the center and return the delta yourself
This is how FPS games do it
I don't have any direct experience with raw input, which is probably what you need to tap into. According to MSDN, you have to register the device, then setup your winproc to accept the WM_INPUT messages and then do your calculations based on the raw data.
Here's another relevant link.