Skip to content

Commit

Permalink
wm: add keyboard events
Browse files Browse the repository at this point in the history
  • Loading branch information
ffwff committed Sep 9, 2019
1 parent c81f306 commit dad1f14
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 12 deletions.
12 changes: 9 additions & 3 deletions pkgs/wm/cterm.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ void cterm_draw(struct cterm_state *state) {
state->root_height = state->sprite.height - state->root_y - 1;

// calculate characters and buffer
state->cwidth = (state->root_width + FONT_WIDTH) / FONT_WIDTH;
state->cheight = (state->root_height + FONT_HEIGHT) / FONT_HEIGHT;
state->cwidth = state->root_width / FONT_WIDTH;
state->cheight = state->root_height / FONT_HEIGHT;
size_t new_len = state->cwidth * state->cheight;
if(new_len != state->buffer_len) {
state->buffer = realloc(state->buffer, new_len);
Expand Down Expand Up @@ -217,7 +217,7 @@ int main(int argc, char **argv) {
char *spawn_argv[] = {"/hd0/main", NULL};
spawnxv(&s_info, "/hd0/main", (char **)spawn_argv);

wmc_connection_obtain(&state.wmc_conn, ATOM_MOUSE_EVENT_MASK);
wmc_connection_obtain(&state.wmc_conn, ATOM_MOUSE_EVENT_MASK | ATOM_KEYBOARD_EVENT_MASK);

// event loop
int mouse_drag = 0;
Expand Down Expand Up @@ -280,6 +280,12 @@ int main(int argc, char **argv) {
wmc_send_atom(&state.wmc_conn, &respond_atom);
break;
}
case ATOM_KEYBOARD_EVENT_TYPE: {
cterm_add_character(&state, atom.keyboard_event.ch);
needs_redraw = 1;
wmc_send_atom(&state.wmc_conn, &respond_atom);
break;
}
}
wait:
wmc_wait_atom(&state.wmc_conn);
Expand Down
19 changes: 19 additions & 0 deletions pkgs/wm/wm.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <sys/gfx.h>
#include <sys/ioctl.h>
#include <sys/mouse.h>
#include <sys/kbd.h>
#include <syscalls.h>

#include "wm.h"
Expand Down Expand Up @@ -66,6 +67,7 @@ struct wm_state {
int needs_redraw;
int mouse_fd;
struct wm_window *mouse_win; // weakref
int kbd_fd;
struct wm_atom last_mouse_atom;
struct wm_window *windows;
int nwindows;
Expand Down Expand Up @@ -242,6 +244,11 @@ int main(int argc, char **argv) {
wm.mouse_win->z_index = (size_t)-1;
}

// keyboard
{
wm.kbd_fd = open("/kbd/raw", 0);
}

wm_sort_windows_by_z(&wm);

// sample win
Expand Down Expand Up @@ -318,6 +325,18 @@ int main(int argc, char **argv) {

wm.last_mouse_atom = mouse_atom;
}
{
// keyboard
struct keyboard_packet keyboard_packet = { 0 };
read(wm.kbd_fd, (char *)&keyboard_packet, sizeof(keyboard_packet));
if(keyboard_packet.ch) {
struct wm_atom keyboard_atom = { 0 };
keyboard_atom.type = ATOM_KEYBOARD_EVENT_TYPE;
keyboard_atom.keyboard_event.ch = keyboard_packet.ch;
keyboard_atom.keyboard_event.modifiers = keyboard_packet.modifiers;
wm_add_queue(&wm, &keyboard_atom);
}
}
for (int i = 0; i < wm.nwindows; i++) {
struct wm_window *win = &wm.windows[i];
switch (win->type) {
Expand Down
26 changes: 18 additions & 8 deletions pkgs/wm/wm.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,35 @@ struct wm_atom_mouse_event {
int delta_x, delta_y;
};

// Keyboard event

struct wm_atom_keyboard_event {
int ch;
int modifiers;
};

struct wm_atom {
int type;
union {
struct wm_atom_redraw redraw;
struct wm_atom_move move;
struct wm_atom_respond respond;
struct wm_atom_mouse_event mouse_event;
struct wm_atom_keyboard_event keyboard_event;
};
};

#define ATOM_REDRAW_TYPE 0
#define ATOM_RESPOND_TYPE 1
#define ATOM_MOVE_TYPE 2
#define ATOM_MOUSE_EVENT_TYPE 3
#define ATOM_REDRAW_TYPE 0
#define ATOM_RESPOND_TYPE 1
#define ATOM_MOVE_TYPE 2
#define ATOM_MOUSE_EVENT_TYPE 3
#define ATOM_KEYBOARD_EVENT_TYPE 4

#define ATOM_REDRAW_MASK (1 << ATOM_REDRAW_TYPE)
#define ATOM_RESPOND_MASK (1 << ATOM_RESPOND_TYPE)
#define ATOM_MOVE_MASK (1 << ATOM_MOVE_TYPE)
#define ATOM_MOUSE_EVENT_MASK (1 << ATOM_MOUSE_EVENT_TYPE)
#define ATOM_REDRAW_MASK (1 << ATOM_REDRAW_TYPE)
#define ATOM_RESPOND_MASK (1 << ATOM_RESPOND_TYPE)
#define ATOM_MOVE_MASK (1 << ATOM_MOVE_TYPE)
#define ATOM_MOUSE_EVENT_MASK (1 << ATOM_MOUSE_EVENT_TYPE)
#define ATOM_KEYBOARD_EVENT_MASK (1 << ATOM_KEYBOARD_EVENT_TYPE)

static inline int wm_atom_eq(struct wm_atom *a, struct wm_atom *b) {
if(a->type != b->type)
Expand Down
2 changes: 1 addition & 1 deletion userspace/libc/src/include/sys/kbd.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

struct mouse_packet {
struct keyboard_packet {
int ch;
int modifiers;
} __attribute__((packed));

0 comments on commit dad1f14

Please sign in to comment.