From c6e989909fe9f099a43c1d0fe3b3e15f3c3b7290 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Mon, 6 Dec 2021 09:19:20 +0200 Subject: [PATCH] Add header for working with the PS/2 mouse protocol. --- kernel/include/sortix/ps2mouse.h | 51 ++++++++++++++++++++++++++++++++ libc/include/sys/ps2mouse.h | 25 ++++++++++++++++ login/graphical.c | 31 +++++++++---------- 3 files changed, 90 insertions(+), 17 deletions(-) create mode 100644 kernel/include/sortix/ps2mouse.h create mode 100644 libc/include/sys/ps2mouse.h diff --git a/kernel/include/sortix/ps2mouse.h b/kernel/include/sortix/ps2mouse.h new file mode 100644 index 00000000..d1797263 --- /dev/null +++ b/kernel/include/sortix/ps2mouse.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2021 Juhani 'nortti' Krekelä. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * sortix/ps2mouse.h + * Defines macros for the PS/2 mouse protocol. + */ + +#ifndef INCLUDE_SORTIX_PS2MOUSE_H +#define INCLUDE_SORTIX_PS2MOUSE_H + +#define MOUSE_PACKET_SIZE 3 + +#define MOUSE_BUTTON_LEFT (1 << 0) +#define MOUSE_BUTTON_RIGHT (1 << 1) +#define MOUSE_BUTTON_MIDDLE (1 << 2) + +#define MOUSE_ALWAYS_1 (1 << 3) + +#define MOUSE_X_SIGN (1 << 4) +#define MOUSE_Y_SIGN (1 << 5) + +#define MOUSE_X_OVERFLOW (1 << 6) +#define MOUSE_Y_OVERFLOW (1 << 7) + +#define MOUSE_X(bytes) \ +( \ + (bytes)[0] & (MOUSE_X_OVERFLOW | MOUSE_Y_OVERFLOW) ? \ + 0 : \ + (bytes)[1] - ((bytes)[0] & MOUSE_X_SIGN ? 256 : 0) \ +) + +#define MOUSE_Y(bytes) \ +(\ + (bytes)[0] & (MOUSE_X_OVERFLOW | MOUSE_Y_OVERFLOW) ? \ + 0 : \ + -((bytes)[2] - ((bytes)[0] & MOUSE_Y_SIGN ? 256 : 0)) \ +) + +#endif diff --git a/libc/include/sys/ps2mouse.h b/libc/include/sys/ps2mouse.h new file mode 100644 index 00000000..8185ff31 --- /dev/null +++ b/libc/include/sys/ps2mouse.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2021 Juhani 'nortti' Krekelä. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * sys/ps2mouse.h + * Defines macros for the PS/2 mouse protocol. + */ + +#ifndef INCLUDE_SYS_PS2MOUSE_H +#define INCLUDE_SYS_PS2MOUSE_H + +#include + +#endif diff --git a/login/graphical.c b/login/graphical.c index f7d6f23e..94bd9ff8 100644 --- a/login/graphical.c +++ b/login/graphical.c @@ -1,5 +1,6 @@ /* * Copyright (c) 2014, 2015, 2016 Jonas 'Sortie' Termansen. + * Copyright (c) 2021 Juhani 'nortti' Krekelä. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -21,6 +22,7 @@ #include #include #include +#include #include #include @@ -100,7 +102,7 @@ struct glogin int pointer_x; int pointer_y; size_t mouse_byte_count; - uint8_t mouse_bytes[3]; + uint8_t mouse_bytes[MOUSE_PACKET_SIZE]; enum stage stage; bool animating; const char* warning; @@ -670,28 +672,21 @@ static void keyboard_event(struct glogin* state, uint32_t codepoint) static void mouse_event(struct glogin* state, unsigned char byte) { state->pointer_working = true; - if ( state->mouse_byte_count == 0 && !(byte & 1 << 3) ) + if ( state->mouse_byte_count == 0 && !(byte & MOUSE_ALWAYS_1) ) return; - if ( state->mouse_byte_count < 3 ) + if ( state->mouse_byte_count < MOUSE_PACKET_SIZE ) state->mouse_bytes[state->mouse_byte_count++] = byte; - if ( state->mouse_byte_count < 3 ) + if ( state->mouse_byte_count < MOUSE_PACKET_SIZE ) return; state->mouse_byte_count = 0; unsigned char* bytes = state->mouse_bytes; - int xm = bytes[1]; - int ym = bytes[2]; - if ( xm && bytes[0] & (1 << 4) ) - xm = xm - 256; - if ( ym && bytes[0] & (1 << 5) ) - ym = ym - 256; - if ( (bytes[0] & (1 << 6)) || (bytes[0] & (1 << 7)) ) - { - xm = 0; - ym = 0; - } - ym = -ym; + + int xm = MOUSE_X(bytes); + int ym = MOUSE_Y(bytes); + int old_pointer_x = state->pointer_x; int old_pointer_y = state->pointer_y; + if ( xm*xm + ym*ym >= 2*2 ) { xm *= 2; @@ -704,6 +699,7 @@ static void mouse_event(struct glogin* state, unsigned char byte) } state->pointer_x += xm; state->pointer_y += ym; + if ( state->pointer_x < 0 ) state->pointer_x = 0; if ( state->pointer_y < 0 ) @@ -714,7 +710,8 @@ static void mouse_event(struct glogin* state, unsigned char byte) state->pointer_y = state->mode.view_yres; xm = state->pointer_x - old_pointer_x; ym = state->pointer_y - old_pointer_y; - if ( (bytes[0] & 1 << 0) ) + + if ( bytes[0] & MOUSE_BUTTON_LEFT ) { (void) xm; (void) ym;