1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00

Add header for working with the PS/2 mouse protocol.

This commit is contained in:
Juhani Krekelä 2021-12-06 09:19:20 +02:00
parent 8b57a79567
commit c6e989909f
3 changed files with 90 additions and 17 deletions

View file

@ -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

View file

@ -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 <sortix/ps2mouse.h>
#endif

View file

@ -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 <sys/display.h>
#include <sys/kernelinfo.h>
#include <sys/ioctl.h>
#include <sys/ps2mouse.h>
#include <sys/termmode.h>
#include <assert.h>
@ -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;