Console does not record mouse wheel events - c

I am working on a TUI program with console on Windows 10. I would like to implement widgets that allow vertical scrolling when the user interacts with the mouse wheel. However the console does not record any of the mouse wheel events.
The sample code below from Microsoft does not record mouse wheel events even though my mouse wheel is working since windows manage to scroll the console content. All other mouse input events are working (click, move...).
#include <windows.h>
#include <stdio.h>
HANDLE hStdin;
DWORD fdwSaveOldMode;
VOID ErrorExit(LPCSTR);
VOID KeyEventProc(KEY_EVENT_RECORD); // the "case MOUSE_WHEELED:" in this function never happens
VOID MouseEventProc(MOUSE_EVENT_RECORD);
VOID ResizeEventProc(WINDOW_BUFFER_SIZE_RECORD);
int main(VOID)
{
DWORD cNumRead, fdwMode, i;
INPUT_RECORD irInBuf[128];
int counter=0;
// Get the standard input handle.
hStdin = GetStdHandle(STD_INPUT_HANDLE);
if (hStdin == INVALID_HANDLE_VALUE)
ErrorExit("GetStdHandle");
// Save the current input mode, to be restored on exit.
if (! GetConsoleMode(hStdin, &fdwSaveOldMode) )
ErrorExit("GetConsoleMode");
// Enable the window and mouse input events.
fdwMode = ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT;
if (! SetConsoleMode(hStdin, fdwMode) )
ErrorExit("SetConsoleMode");
// Loop to read and handle the next 100 input events.
while (counter++ <= 100)
{
// Wait for the events.
if (! ReadConsoleInput(
hStdin, // input buffer handle
irInBuf, // buffer to read into
128, // size of read buffer
&cNumRead) ) // number of records read
ErrorExit("ReadConsoleInput");
// Dispatch the events to the appropriate handler.
for (i = 0; i < cNumRead; i++)
{
switch(irInBuf[i].EventType)
{
case KEY_EVENT: // keyboard input
KeyEventProc(irInBuf[i].Event.KeyEvent);
break;
case MOUSE_EVENT: // mouse input
MouseEventProc(irInBuf[i].Event.MouseEvent);
break;
case WINDOW_BUFFER_SIZE_EVENT: // scrn buf. resizing
ResizeEventProc( irInBuf[i].Event.WindowBufferSizeEvent );
break;
case FOCUS_EVENT: // disregard focus events
case MENU_EVENT: // disregard menu events
break;
default:
ErrorExit("Unknown event type");
break;
}
}
}
// Restore input mode on exit.
SetConsoleMode(hStdin, fdwSaveOldMode);
return 0;
}
VOID ErrorExit (LPCSTR lpszMessage)
{
fprintf(stderr, "%s\n", lpszMessage);
// Restore input mode on exit.
SetConsoleMode(hStdin, fdwSaveOldMode);
ExitProcess(0);
}
VOID KeyEventProc(KEY_EVENT_RECORD ker)
{
printf("Key event: ");
if(ker.bKeyDown)
printf("key pressed\n");
else printf("key released\n");
}
VOID MouseEventProc(MOUSE_EVENT_RECORD mer)
{
#ifndef MOUSE_HWHEELED
#define MOUSE_HWHEELED 0x0008
#endif
printf("Mouse event: ");
switch(mer.dwEventFlags)
{
case 0:
if(mer.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
{
printf("left button press \n");
}
else if(mer.dwButtonState == RIGHTMOST_BUTTON_PRESSED)
{
printf("right button press \n");
}
else
{
printf("button press\n");
}
break;
case DOUBLE_CLICK:
printf("double click\n");
break;
case MOUSE_HWHEELED:
printf("horizontal mouse wheel\n");
break;
case MOUSE_MOVED:
printf("mouse moved\n");
break;
case MOUSE_WHEELED:// this case never shows up when I use the wheel
printf("vertical mouse wheel\n");
break;
default:
printf("unknown\n");
break;
}
}
VOID ResizeEventProc(WINDOW_BUFFER_SIZE_RECORD wbsr)
{
printf("Resize event\n");
printf("Console screen buffer is %d columns by %d rows.\n", wbsr.dwSize.X, wbsr.dwSize.Y);
}

Related

Track X11 enter / leave events

I need to be notified when mouse enters or leaves a window (any window, that's why I'm grabbing on the root window). I have a solution using xcb_input_xi_grab_device() but it works only half way. When I run the app, I do get enter/leave events but the mouse pointer disappears and it consumes all mouse events and other windows stop (children of the root window) reacting to clicks.
Using XCB_INPUT_GRAB_OWNER_OWNER should allow passing-through the events but it doesn't work.
app.c:
#include <stdio.h>
#include <xcb/xcb.h>
#include <xcb/xinput.h>
xcb_input_device_id_t find_device(xcb_connection_t *conn)
{
xcb_input_xi_query_device_reply_t *reply = xcb_input_xi_query_device_reply(
conn, xcb_input_xi_query_device(conn, XCB_INPUT_DEVICE_ALL), NULL);
xcb_input_xi_device_info_iterator_t iter =
xcb_input_xi_query_device_infos_iterator(reply);
xcb_input_device_id_t device_id;
int found = 0;
while (iter.rem) {
xcb_input_xi_device_info_t *device = iter.data;
switch (device->type) {
case XCB_INPUT_DEVICE_TYPE_MASTER_POINTER:
case XCB_INPUT_DEVICE_TYPE_SLAVE_POINTER:
case XCB_INPUT_DEVICE_TYPE_FLOATING_SLAVE:
device_id = device->deviceid;
found = 1;
break;
}
if (found) {
break;
}
xcb_input_xi_device_info_next(&iter);
}
free(reply);
return device_id;
}
int main()
{
xcb_connection_t *conn = xcb_connect(NULL, NULL);
xcb_screen_t *screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
uint32_t mask =
XCB_INPUT_XI_EVENT_MASK_ENTER | XCB_INPUT_XI_EVENT_MASK_LEAVE;
xcb_generic_error_t *error;
xcb_input_xi_grab_device_reply_t *reply = xcb_input_xi_grab_device_reply(
conn,
xcb_input_xi_grab_device(conn, screen->root, XCB_CURRENT_TIME,
XCB_CURSOR_NONE, find_device(conn),
XCB_INPUT_GRAB_MODE_22_ASYNC,
XCB_INPUT_GRAB_MODE_22_ASYNC,
XCB_INPUT_GRAB_OWNER_OWNER, 1, &mask),
&error);
free(reply);
if (error) {
printf("failed to grab device\n");
free(error);
return -1;
}
xcb_flush(conn);
xcb_generic_event_t *event;
while ((event = xcb_wait_for_event(conn))) {
xcb_ge_event_t *generic_event = event;
switch (generic_event->event_type) {
case XCB_INPUT_ENTER:
printf("enter\n");
break;
case XCB_INPUT_LEAVE:
printf("leave\n");
break;
}
free(event);
}
return -1; // never reached
}
cc -O2 app.c -o app `pkg-config --cflags --libs xcb xcb-xinput`
I also tried to achieve it with xcb_grab_button(), xcb_input_xi_select_events(), xcb_input_xi_passive_grab_device() or xcb_grab_pointer() but then the problem is the same. Either the mouse events are not passed-through or I don't receive enter/leave events at all.

Recording multiple key presses in x11

I want to record simultaneous key presses and test a function in x11 using C. For example, I was able to set something like,
if 'Q' is pressed,
let the window resize.
But I could not find a method for doing the same with a key combination such as Ctrl+Enter, etc, so that when 'Ctrl+Enter' is pressed, the window resizes.
Is there any event type or mask or function in x11 for recording these simultaneous key events ?
The code below is what I have written so far for recording single keys and performing specified action.
// USES KEYBOARD KEY TO RESIZE A WINDOW
// Compile : gcc -o go key_and_win.c -lX11
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
Display *d;
Window window;
XEvent event, ev;
int s;
/* open connection with the server */
d = XOpenDisplay(NULL);
if (d == NULL)
{
fprintf(stderr, "Cannot open d\n");
exit(1);
}
s = DefaultScreen(d);
/* create window */
window = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 200, 200, 1,
BlackPixel(d, s), BlackPixel(d, s));
/* select kind of events we are interested in */
XSelectInput(d, window, StructureNotifyMask | ExposureMask | KeyPressMask | KeyReleaseMask );
/* map (show) the window */
XMapWindow(d, window);
/* event loop */
while (1)
{
XNextEvent(d, &event);
/* keyboard events */
if (event.type == KeyPress)
{
printf( "KeyPress: %x\n", event.xkey.keycode );
if(event.xkey.keycode == 0x18) // Resize on pressing Q as, key Q => 0x18
{
printf("Here in Q\n");
int r = XResizeWindow(d, window, 100, 200); // Resizing the window through Q keypress
if(r==BadValue || r==BadWindow)
printf("Error in resizing\n");
XNextEvent(d, &event); // To get ConfigureNotify event
if(event.type == ConfigureNotify)
printf("Resized!\n");
else
printf("Not resized\n");
//XMapWindow(d, window); // Map the resized window (not necessary)
}
/* exit on ESC key press */
if ( event.xkey.keycode == 0x09 )
break;
}
}
/* close connection to server */
XCloseDisplay(d);
return 0;
}
you have to look at event.xkey.state
from 10.5.2 Keyboard and Pointer Events :
The state member is set to indicate the logical state of the pointer buttons and modifier keys just prior to the event, which is the bitwise inclusive OR of one or more of the button or modifier key masks: Button1Mask, Button2Mask, Button3Mask, Button4Mask, Button5Mask, ShiftMask, LockMask, ControlMask, Mod1Mask, Mod2Mask, Mod3Mask, Mod4Mask, and Mod5Mask.

Capture Button Events in Xlib then passing the Event to the client

I'm working on a window manager, mainly as an exercise, and I'm facing a problem. I would like to be able to raise the clicked window to the top of the stack. Currently, I am using XGrabButton on Button1 and ControlMask to allow for moving of windows, and when I Ctrl+click the window, the desired effect is achieved. However, if I use XGrabButton on Button1 with AnyModifier, while the effect I am looking for is achieved, I can no longer interact with the client window (highlighting text, etc.) through the mouse button. I have tried Grabbing the button on EnterNotify, then ungrabbing the button as soon as the window is raised, but this appears to have no effect and the window manager acts as though I never grabbed the button at all.
My program is still relatively small, so here is the code:
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include "window_manager.h"
ewm_instance wm;
void
ewm_init()
{
wm._display = XOpenDisplay(NULL);
if (!wm._display) {
printf("Could not open display %s\n", XDisplayName(NULL));
}
wm._root = DefaultRootWindow(wm._display);
}
void
ewm_run()
{
XSelectInput(wm._display,
wm._root,
SubstructureRedirectMask | SubstructureNotifyMask |
KeyPressMask | KeyReleaseMask |
ButtonPressMask | ButtonReleaseMask);
XSync(wm._display, 0);
XGrabServer(wm._display);
Window returned_root, returned_parent;
Window *top_level_windows;
unsigned int num_top_level_windows;
XQueryTree(wm._display,
wm._root,
&returned_root,
&returned_parent,
&top_level_windows,
&num_top_level_windows);
XFree(top_level_windows);
XUngrabServer(wm._display);
XGrabButton(
wm._display,
Button1,
ControlMask,
wm._root,
0,
ButtonPressMask | ButtonReleaseMask | ButtonMotionMask,
GrabModeAsync,
GrabModeAsync,
None,
None);
XGrabButton(
wm._display,
Button1,
ControlMask,
wm._root,
0,
ButtonPressMask | ButtonReleaseMask | ButtonMotionMask,
GrabModeAsync,
GrabModeAsync,
None,
None);
for (;;) {
XEvent e;
XNextEvent(wm._display, &e);
switch (e.type) {
case CreateNotify:
printf("CreateNotify\n");
break;
case DestroyNotify:
printf("DestroyNotify\n");
break;
case ReparentNotify:
printf("ReparentNotify\n");
break;
case MapNotify:
printf("Mapping Window\n");
break;
case UnmapNotify:
printf("UnmapNotify\n");
break;
case ConfigureNotify:
printf("ConfigureNotify\n");
break;
case MapRequest:
printf("MapRequest\n");
ewm_on_map_request(&e.xmaprequest);
break;
case ConfigureRequest:
printf("ConfigureRequest\n");
break;
case ButtonPress:
printf("ButtonPress\n");
ewm_on_button_press(&e.xbutton);
break;
case ButtonRelease:
printf("ButtonRelease\n");
break;
case MotionNotify:
ewm_on_motion_notify(&e.xmotion);
break;
case KeyPress:
printf("KeyPress\n");
ewm_on_key_press(&e.xkey);
break;
case KeyRelease:
printf("KeyRelease\n");
break;
case EnterNotify:
ewm_on_enter_notify(&e.xcrossing);
break;
default:
printf("Something else\n");
}
}
}
void
ewm_on_map_request(const XMapRequestEvent *e)
{
XSelectInput(
wm._display,
e->window,
KeyPressMask | KeyReleaseMask |
EnterWindowMask | ButtonPressMask | ButtonReleaseMask | ButtonMotionMask);
XMapWindow(wm._display, e->window);
XSetInputFocus(wm._display, e->window, RevertToPointerRoot, CurrentTime);
}
void
ewm_on_enter_notify(const XEnterWindowEvent *e)
{
printf("Entered window: %lu\n", e->window);
XSetInputFocus(wm._display, e->window, RevertToParent, CurrentTime);
}
void
ewm_on_key_press(const XKeyEvent *e)
{
if ((e->state & ControlMask) &&
e->keycode == XKeysymToKeycode(wm._display, XK_q)) {
printf("Destroying window\n");
XDestroyWindow(wm._display, e->window);
}
if ((e->state & ControlMask) &&
e->keycode == XKeysymToKeycode(wm._display, XK_Return)) {
printf("Enter Works\n");
system("urxvt &");
}
}
void
ewm_on_button_press(const XButtonEvent *e)
{
if (e->subwindow != 0) {
// Save initial cursor position;
wm._cursor_start_position = (Vector){e->x_root, e->y_root};
// Save initial window info
Window returned_root;
int x, y;
unsigned int width, height, depth, border_width;
XGetGeometry(wm._display,
e->subwindow,
&returned_root,
&x, &y,
&width, &height,
&border_width,
&depth);
wm._window_start_position = (Vector){x, y};
wm._window_start_size = (Size){width, height};
XRaiseWindow(wm._display, e->subwindow);
XSetInputFocus(wm._display, e->subwindow, RevertToParent, CurrentTime);
printf("Raising window %lu\n", e->subwindow);
printf("root id: %lu\n", wm._root);
XUngrabButton(wm._display, Button1, AnyModifier, e->subwindow);
}
}
void
ewm_on_motion_notify(const XMotionEvent *e)
{
const Vector drag_pos = {e->x_root, e->y_root};
const Vector delta = {
(drag_pos.x - wm._cursor_start_position.x),
(drag_pos.y - wm._cursor_start_position.y)
};
if ((e->state & Button1Mask) && (e->state & ControlMask)) {
const Vector dest_window_pos = {
(wm._window_start_position.x + delta.x),
(wm._window_start_position.y + delta.y)
};
if (e->subwindow != 0) {
XMoveWindow(wm._display,
e->subwindow,
dest_window_pos.x,
dest_window_pos.y);
}
}
}
void
ewm_cleanup()
{
XCloseDisplay(wm._display);
}
I have also tried to use XSendEvent, but based on the results I have gotten from it I don't think I understand what it is supposed to do very well. I am very new to Xlib programming so any help is greatly appreciated.
Thank you!
I had exactly the same problem. The comments in the original question helped, but they're not quite there yet, they're missing an important detail (point 1 ahead). Ultimately I found the hint here solved it.
Grab the event synchronously (notice GrabModeSync given as pointer_mode)
XGrabButton(dis, FOCUS_BUTTON, AnyModifier, root, False, BUTTONMASK, GrabModeSync, GrabModeAsync, None, None);
Use XAllowEvents and Xsync for the pass-through effect
XAllowEvents(display, ReplayPointer, ev->xbutton.time);
XSync(display, 0);

Disable queueing execute in C

I have problem with my sleep function in C.
When i use in this code sleep function like this:
while(1) {
XNextEvent(display, &xevent);
switch (xevent.type) {
case MotionNotify:
break;
case ButtonPress:
printf("Button click: [%d, %d]\n", xevent.xmotion.x_root, xevent.xmotion.y_root);
sleep(5);
break;
case ButtonRelease:
break;
}
It doesn't works well for me because printf("button click") is executing all time but slower.
How to print "button click x y" once and stop listening for click for 5 second?
I think you're looking for something like:
/* ignore_click is the time until mouse click is ignored */
time_t ignore_click = 0;
while(1) {
XNextEvent(display, &xevent);
switch (xevent.type) {
case MotionNotify:
break;
case ButtonPress:
{
time_t now;
/* we read current time */
time(&now);
if (now > ignore_click)
{
/* now is after ignore_click, mous click is processed */
printf("Button click: [%d, %d]\n", xevent.xmotion.x_root, xevent.xmotion.y_root);
/* and we set ignore_click to ignore clicks for 5 seconds */
ignore_click = now + 5;
}
else
{
/* click is ignored */
}
}
break;
case ButtonRelease:
break;
}
}
The code written above will ignore clicks for 4 to 5 seconds: the time_t type is a second precision structure...
To get more acurate time, you could use struct timeval or struct timespec structure. I do not use them in my example to keep it clear.

XCB – Not receiving motion notify events on all windows

I am trying to be notified about any pointer motion. Since I don't want to run as the window manager, I need to set XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_POINTER_MOTION on all windows which I do both on startup and when I get a create notify event.
This seems to work fine in general and I receive motion notify events on all windows. However, somehow, this isn't true for Google Chrome windows. I checked the event mask by explicitly querying it afterwards and it is correctly set. I also don't see anything unusual in the propagation mask.
What could cause Google Chrome to not report motion notify events? AFAIK, the X protocol doesn't allow that except for active pointer grabs which Chrome surely doesn't have.
Here is how I register myself on all existing windows. I call register_events on the root window and whenever I receive a create notify event as well:
static void register_events(xcb_window_t window) {
xcb_void_cookie_t cookie = xcb_change_window_attributes_checked(connection,
window, XCB_CW_EVENT_MASK, (uint32_t[]) { XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_POINTER_MOTION | XCB_EVENT_MASK_LEAVE_WINDOW });
xcb_generic_error_t *error = xcb_request_check(connection, cookie);
if (error != NULL) {
xcb_disconnect(connection);
errx(EXIT_FAILURE, "could not subscribe to events on a window, bailing out");
}
}
static void register_existing_windows(void) {
xcb_query_tree_reply_t *reply;
if ((reply = xcb_query_tree_reply(connection, xcb_query_tree(connection, root), 0)) == NULL) {
return;
}
int len = xcb_query_tree_children_length(reply);
xcb_window_t *children = xcb_query_tree_children(reply);
for (int i = 0; i < len; i++) {
register_events(children[i]);
}
xcb_flush(connection);
free(reply);
}
The Chrome windows appear to be comprised of quite the tree of nested child windows. It appears you'll need to walk the tree of windows and monitor them all. This code picks up pointer motion events across the entirety of my Chrome windows:
#include <stdio.h>
#include <stdlib.h>
#include <xcb/xcb.h>
#include <X11/Xlib.h>
static void register_events(xcb_connection_t *conn,
xcb_window_t window) {
xcb_void_cookie_t cookie =
xcb_change_window_attributes_checked(conn,
window, XCB_CW_EVENT_MASK,
(uint32_t[]) {
XCB_EVENT_MASK_POINTER_MOTION });
xcb_generic_error_t *error = xcb_request_check(conn, cookie);
if (error != NULL) {
xcb_disconnect(conn);
exit(-1);
}
}
static void register_existing_windows(xcb_connection_t *conn,
xcb_window_t root) {
int i, len;
xcb_window_t *children;
xcb_query_tree_reply_t *reply;
if ((reply = xcb_query_tree_reply(conn,
xcb_query_tree(conn, root), 0))
== NULL)
{
return;
}
len = xcb_query_tree_children_length(reply);
children = xcb_query_tree_children(reply);
for (i = 0; i < len; i++) {
register_events(conn, children[i]);
register_existing_windows(conn, children[i]);
}
xcb_flush(conn);
}
void main(void) {
int i=0;
/* Open the connection to the X server */
xcb_connection_t *conn = xcb_connect (NULL, NULL);
/* Get the first screen */
xcb_screen_t *screen = xcb_setup_roots_iterator (xcb_get_setup (conn)).data;
register_existing_windows(conn, screen->root);
while(1) {
xcb_generic_event_t *evt;
evt = xcb_wait_for_event(conn);
printf("%i\n", i++);
}
}
(That's just intended as proof of concept, and not very nice.)
While #Jay Kominek's answer was helpful and valid, I've come to realize now that using the Xinput extension provides a much better approach as it won't interfere with applications whatsoever.
Simply selecting on the entire tree causes all kinds of issues, e.g., hover doesn't work in Chrome anymore.
xcb provides xcb_grab_pointer to capture pointer event without registe on specific window.
#include <stdlib.h>
#include <stdio.h>
#include <xcb/xcb.h>
void
print_modifiers (uint32_t mask)
{
const char **mod, *mods[] = {
"Shift", "Lock", "Ctrl", "Alt",
"Mod2", "Mod3", "Mod4", "Mod5",
"Button1", "Button2", "Button3", "Button4", "Button5"
};
printf ("Modifier mask: ");
for (mod = mods ; mask; mask >>= 1, mod++)
if (mask & 1)
printf(*mod);
putchar ('\n');
}
int
main ()
{
xcb_connection_t *c;
xcb_screen_t *screen;
xcb_window_t win;
xcb_generic_event_t *e;
uint32_t mask = 0;
/* Open the connection to the X server */
c = xcb_connect (NULL, NULL);
/* Get the first screen */
screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
mask = XCB_EVENT_MASK_BUTTON_PRESS |
XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION;
xcb_grab_pointer(c, false, screen->root, mask, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE, XCB_CURRENT_TIME);
xcb_flush (c);
while ((e = xcb_wait_for_event (c))) {
switch (e->response_type & ~0x80) {
case XCB_BUTTON_PRESS: {
xcb_button_press_event_t *ev = (xcb_button_press_event_t *)e;
print_modifiers(ev->state);
switch (ev->detail) {
case 4:
printf ("Wheel Button up in window %ld, at coordinates (%d,%d)\n",
ev->event, ev->event_x, ev->event_y);
break;
case 5:
printf ("Wheel Button down in window %ld, at coordinates (%d,%d)\n",
ev->event, ev->event_x, ev->event_y);
break;
default:
printf ("Button %d pressed in window %ld, at coordinates (%d,%d)\n",
ev->detail, ev->event, ev->event_x, ev->event_y);
}
break;
}
case XCB_BUTTON_RELEASE: {
xcb_button_release_event_t *ev = (xcb_button_release_event_t *)e;
print_modifiers(ev->state);
printf ("Button %d released in window %ld, at coordinates (%d,%d)\n",
ev->detail, ev->event, ev->event_x, ev->event_y);
break;
}
case XCB_MOTION_NOTIFY: {
xcb_motion_notify_event_t *ev = (xcb_motion_notify_event_t *)e;
printf ("Mouse moved in window %ld, at coordinates (%d,%d)\n",
ev->event, ev->event_x, ev->event_y);
break;
}
default:
/* Unknown event type, ignore it */
printf("Unknown event: %d\n", e->response_type);
break;
}
/* Free the Generic Event */
free (e);
}
return 0;
}

Resources