Skip to content

Commit

Permalink
Handle keyboard and mouse events (just println! for now)
Browse files Browse the repository at this point in the history
  • Loading branch information
crsaracco committed Jun 15, 2020
1 parent c42b218 commit 7308763
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -14,6 +14,6 @@ Below is a proposed list of milestones (roughly in-order) and their status. Subj
| Cross-platform API for window spawning | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| Window uses an OpenGL surface | :heavy_check_mark: | | :heavy_check_mark: |
| Can find DPI scale factor | | | :heavy_check_mark: |
| Basic event handling (mouse, keyboard) | | | |
| Basic event handling (mouse, keyboard) | | | :heavy_check_mark: |
| Parent window support | | | |
| *(Converge on a common API for all platforms?)* | | | |
83 changes: 79 additions & 4 deletions src/x11/window.rs
Expand Up @@ -105,9 +105,11 @@ impl Window {
(
xcb::CW_EVENT_MASK,
xcb::EVENT_MASK_EXPOSURE
| xcb::EVENT_MASK_POINTER_MOTION
| xcb::EVENT_MASK_BUTTON_PRESS
| xcb::EVENT_MASK_BUTTON_RELEASE
| xcb::EVENT_MASK_BUTTON_1_MOTION,
| xcb::EVENT_MASK_KEY_PRESS
| xcb::EVENT_MASK_KEY_RELEASE,
),
(xcb::CW_COLORMAP, colormap),
];
Expand Down Expand Up @@ -251,7 +253,26 @@ impl Window {
let ev = self.xcb_connection.conn.wait_for_event();
if let Some(event) = ev {
let event_type = event.response_type() & !0x80;
//println!("{:?}", event_type);

// For all of the keyboard and mouse events, you can fetch
// `x`, `y`, `detail`, and `state`.
// - `x` and `y` are the position inside the window where the cursor currently is
// when the event happened.
// - `detail` will tell you which keycode was pressed/released (for keyboard events)
// or which mouse button was pressed/released (for mouse events).
// For mouse events, here's what the value means (at least on my current mouse):
// 1 = left mouse button
// 2 = middle mouse button (scroll wheel)
// 3 = right mouse button
// 4 = scroll wheel up
// 5 = scroll wheel down
// 8 = lower side button ("back" button)
// 9 = upper side button ("forward" button)
// Note that you *will* get a "button released" event for even the scroll wheel
// events, which you can probably ignore.
// - `state` will tell you the state of the main three mouse buttons and some of
// the keyboard modifier keys at the time of the event.
// http://rtbo.github.io/rust-xcb/src/xcb/ffi/xproto.rs.html#445

match event_type {
xcb::EXPOSE => unsafe {
Expand All @@ -262,7 +283,61 @@ impl Window {
glx::glXSwapBuffers(raw_display, window_id as xlib::XID);
glx::glXMakeCurrent(raw_display, 0, null_mut());
},
_ => {}
xcb::MOTION_NOTIFY => {
let event = unsafe { xcb::cast_event::<xcb::MotionNotifyEvent>(&event) };
let x = event.event_x();
let y = event.event_y();
let detail = event.detail();
let state = event.state();
println!("Mouse motion: ({}, {}) -- {} / {}", x, y, detail, state);
}
xcb::BUTTON_PRESS => {
let event = unsafe { xcb::cast_event::<xcb::ButtonPressEvent>(&event) };
let x = event.event_x();
let y = event.event_y();
let detail = event.detail();
let state = event.state();
println!(
"Mouse button pressed: ({}, {}) -- {} / {}",
x, y, detail, state
);
}
xcb::BUTTON_RELEASE => {
let event = unsafe { xcb::cast_event::<xcb::ButtonReleaseEvent>(&event) };
let x = event.event_x();
let y = event.event_y();
let detail = event.detail();
let state = event.state();
println!(
"Mouse button released: ({}, {}) -- {} / {}",
x, y, detail, state
);
}
xcb::KEY_PRESS => {
let event = unsafe { xcb::cast_event::<xcb::KeyPressEvent>(&event) };
let x = event.event_x();
let y = event.event_y();
let detail = event.detail();
let state = event.state();
println!(
"Keyboard key pressed: ({}, {}) -- {} / {}",
x, y, detail, state
);
}
xcb::KEY_RELEASE => {
let event = unsafe { xcb::cast_event::<xcb::KeyReleaseEvent>(&event) };
let x = event.event_x();
let y = event.event_y();
let detail = event.detail();
let state = event.state();
println!(
"Keyboard key released: ({}, {}) -- {} / {}",
x, y, detail, state
);
}
_ => {
println!("Unhandled event type: {:?}", event_type);
}
}
}
}
Expand Down Expand Up @@ -346,4 +421,4 @@ impl Window {
// TODO: choose between `xres` and `yres`? (probably both are the same?)
Some(yres)
}
}
}

0 comments on commit 7308763

Please sign in to comment.