Mouse movement events in NCurses - c

I wonder if there is such a thing as mouse movement events in NCurses, and if there is a way to catch them. Following the Interfacing with the mouse (from the NCurses programming HOWTO) it seems that by enabling the REPORT_MOUSE_POSITION bit in the call to mousemask, one can indeed catch mouse movement events.
So, I tried that and it does not seem to work. I have something like this:
int ch, count=0;
mmask_t old;
initscr ();
noecho ();
cbreak ();
mousemask (ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, &old);
keypad (stdscr, TRUE);
while ((ch = getchar ()) != 'q')
{
count++;
if (ch == KEY_MOUSE)
{
MEVENT event;
assert (getmouse (&event) == OK);
mvprintw (0, 0, "Mouse Event!\n");
}
mvprintw (1, 1, "Event number %4d",count);
}
...
I expected that as I'll move my mouse cursor, I'll see the event counter increasing. But it didn't. I also tried moving it while mouse button 1 is down to see if generates "drag" events, and it also didn't do anything. The question is, if it's simply a problem of my terminal emulator? Or maybe I'm misunderstanding what NCurses considers as mouse movement events? All the other mouse events were received (and I can operate programs in the console that use the mouse).
I tried gnome-terminal, xterm, and some other stuff. I also tried a textual environment (without X) by going to the tty's of my linux machine (Fedora 15, Ctrl+Alt+F2) and that did not work.
Finally, assuming I do get this right and those events should be reported, what is the bstate field of a MEVENT for a mouse movement evenet?
Many thanks in advance!

You need:
a terminal which supports mouse event reporting;
$TERM pointing to a terminfo entry which has an appropriate XM entry to initialise the terminal correctly.
xterm at least satisfies (1); for (2), it's likely that you'll need to set a different value for TERM.
Try:
TERM=xterm-1002 to get a position event when the cursor moves to a different cell while a button is being held down; or
TERM=xterm-1003 to always get a position event whenever the cursor moves to a different cell, even if no button is pressed.
The resulting events have the REPORT_MOUSE_POSITION bit set on the bstate field.
(The "PORTABILITY" section of the curs_mouse(3x) man page describes the terminal initialisation, and the "Mouse Tracking" section of the Xterm Control Sequences documentation describes the relevant "private mode" extensions.)
The code that you've given above needs to use getch(), not getchar(); and needs a refresh() inside the loop! Other than that, it works for me with xterm when using one of the appropriate TERM settings.

Related

Enabling mouse in Allegro5

I am starting using Allegro in my program in C, but I'm having difficulties creating the buttons. I am using this kind of logic:
if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP)
{
if ((event.mouse.x >= 442) &&(event.mouse.x <= 471) &&(event.mouse.y >= 202) &&(event.mouse.y <= 238))
{
dig = '1';
entr = 1;
}
But this spaces defined by the axis are non 'clickable'.Somebody here has some tip about the typo of command I should use?
I can only guess what was wrong, but there is no answer yet, so I will provide some tips about the possible problem. Your thread is titled enabling the mouse in allegro 5, so I can only assume you're not getting mouse input.
1) You need to install the mouse driver before you will get any mouse input :
if (!al_install_mouse()) {Fail();}
2) The mouse needs to be registered with your event queue.
al_register_event_source(event_queue , al_get_mouse_event_source());
In a typical GUI, most buttons are only considered 'pressed' if they receive both a mouse button down event over their click area, AND a mouse button up event over the same area. This way you don't get a button press when you click on something else, move the mouse over your button and release it. You also prevent button events from pressing the mouse, moving it off the click area and releasing it.

NCURSES doesn't show scrollbars in elementryOS terminal

I've been using NCURSES library for my assignment, but there is one big problem with it.
I can't make it to show the scrollbars on the terminal window. I've tried anything I found here but none of them seems to work.
I found this code portion in StackExhcange but even this doesn't show schrollbars.
#include <ncurses.h>
int main(void)
{
int i = 0;
initscr();
scrollok(stdscr,TRUE);
while(i<500) {
printw("%3d - lots and lots of lines flowing down the terminal\n", i);
++i;
refresh();
}
getchar();
endwin();
return 0;
}
Is it a problem with elementaryOS because I've heard it's been modified.
Two possibilities:
The terminal description is probably using the xterm alternate screen feature. Some terminals don't display scrollbars when using the alternate screen because scrolling is disabled. For instance, VTE, which is the real terminal you're using by default has (hardcoded) behavior in this mode to translate your scrolling into up/down cursor keys.
OP expects curses applications to display scrollbars. They don't, unless the application simulates scrollbars.
For example, dialog does the latter. Here's an example:

Is it possible to change the mouse icon from a terminal app?

Title pretty much says it all: I'm wondering whether it's possible to change the mouse cursor icon in response to feedback in a terminal app (e.g., a click event) from the ncurses library or another library?
For example: I am running xterm under X, and a curses application inside that xterm. I may or may not be sshed into another box.
A user clicks on an element of my cursor app -- is it possible to change the mouse cursor icon from a bar to a plus sign in response to the click?
There is some information here but I'd like a more complete resource:
Mouse movement events in NCurses
I don't believe it is. ncurses can read events from the mouse but not actually change mouse cursor settings. The terminal sends mouse movement and clicks to the ncurses program as escape sequences.
Some terminals, such as putty, will change the cursor to an arrow when a region is clickable. Otherwise, a text selection cursor is shown. But I don't think this is controllable through escape sequences.

capture mouse with Xlib

I want to write a simple Xlib program changing the mouse behavior (to give an example, invert vertical movement). I have a problem with capturing the events.
I would like the code to
capture changes in the controllers position (I move mouse upward, MotionEvent)
calculate new cursor position (new_x -= difference_x)
set new cursor position ( move pointer down, XWarpPointer, prevent event generation here)
The code below should capture a motion event every time the mouse is moved, but it generates the event only when the pointer moves from one window to another... How to capture all the movement events?
#include "X11/Xlib.h"
#include "stdio.h"
int main(int argc, char *argv[])
{
Display *display;
Window root_window;
XEvent event;
display = XOpenDisplay(0);
root_window = XRootWindow(display, 0);
XSelectInput(display, root_window, PointerMotionMask );
while(1) {
XNextEvent( display, &event );
switch( event.type ) {
case MotionNotify:
printf("x %d y %d\n", event.xmotion.x, event.xmotion.y );
break;
}
}
return 0;
}
Related:
X11: How do I REALLY grab the mouse pointer?
When your program receives mouse events, it receives a copy of the events; copies are also sent to other programs that are listening for those events (see XSelectInput(3)). You cannot override this without using XGrabPointer(3) to take exclusive ownership of the mouse, which will prevent other programs from receiving any mouse events. In short, you can't actually do what you are trying to do.
Note also that if a client has specified PointerMotion in its do-not-propagate mask for one of its windows, you will not receive any pointer motion events within its window (again, unless you do a grab).
If you want to change the behavior of the mouse when it is being moved, I suggest you to play with the input properties instead of trying to do the processing in your program.
xinput --list
xinput --list-props 'USB Optical Mouse'
xinput --set-prop 'USB Optical Mouse' 'Evdev Axis Inversion' 1 0
xinput --set-prop 'USB Optical Mouse' 'Evdev Axes Swap' 1
There's also the 'Coordinate Transformation Matrix' property but for some reason it's not working for me right now.
You don't need to call the xinput program yourself: you can use Xlib calls (look at xinput's source code).

SendMessage WM_MOUSEMOVE not working as expected

When sending the WM_MOUSEMOVE message to the client area of an application (in this case Open Office Writer) the image will flicker as if the mouse is over the specified coordinates, but does not stay in that state. The code is:
PostMessage(hWndClient, WM_MOUSEMOVE, 0, MAKEWORD(x, y))
where x and y are relative to the client area.
Sending this in a loop still does not work as the area highlighted by the mouse over event will just flicker.
Any help would be appreciated.
The app could be triggering on all sorts of other events. Maybe it wants to see WM_MOUSEHOVER etc... It's been long a while, but I remember there being something really klugy about how some of these events were implemented - like there was a separate thread that polled the mouse and generated WM_MOUSELEAVE events when the mouse was no longer over the window. You could be getting bitten by something like that too. It's also possible that the application itself is polling the mouse for its true position when it receives the event.
Depending on what you're trying to do, perhaps you could programmatically move the mouse instead of just trying to fake events. Unfortunately, I don't remember the API for this, but I'm sure it's possible.
I used sth like this in my main window and looks that helps... in WM_MOUSEMOVE:
POINT Point;
GetCursorPos(&Point);
ScreenToClient(hHwnd, &Point);
int X = Point.x;
int Y = Point.y;
Try this: PostMessage(hWndClient, WM_MOUSEMOVE, MK_LBUTTON, MAKELONG(x, y)), since postion is a 32-bit integer, the lower 16 bit is x, and the higher 16 bit is y, maybe you should use MAKELONG instead of MAKEWORD.
Check WM_MOUSEMOVE.

Resources