Atlas - SDL_wscons_mouse.c

Home / ext / SDL / src / core / openbsd Lines: 1 | Size: 3977 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20*/ 21 22#include "SDL_internal.h" 23#include <sys/time.h> 24#include <dev/wscons/wsconsio.h> 25#include <unistd.h> 26#include <sys/ioctl.h> 27#include <stdlib.h> 28#include <fcntl.h> 29 30#include "../../events/SDL_mouse_c.h" 31 32typedef struct SDL_WSCONS_mouse_input_data 33{ 34 int fd; 35 SDL_MouseID mouseID; 36} SDL_WSCONS_mouse_input_data; 37 38SDL_WSCONS_mouse_input_data *SDL_WSCONS_Init_Mouse(void) 39{ 40#ifdef WSMOUSEIO_SETVERSION 41 int version = WSMOUSE_EVENT_VERSION; 42#endif 43 SDL_WSCONS_mouse_input_data *input = SDL_calloc(1, sizeof(SDL_WSCONS_mouse_input_data)); 44 45 if (!input) { 46 return NULL; 47 } 48 input->fd = open("/dev/wsmouse", O_RDWR | O_NONBLOCK | O_CLOEXEC); 49 if (input->fd == -1) { 50 SDL_free(input); 51 return NULL; 52 } 53 54 input->mouseID = SDL_GetNextObjectID(); 55 SDL_AddMouse(input->mouseID, NULL); 56 57#ifdef WSMOUSEIO_SETMODE 58 ioctl(input->fd, WSMOUSEIO_SETMODE, WSMOUSE_COMPAT); 59#endif 60#ifdef WSMOUSEIO_SETVERSION 61 ioctl(input->fd, WSMOUSEIO_SETVERSION, &version); 62#endif 63 return input; 64} 65 66static Uint64 GetEventTimestamp(struct timespec *time) 67{ 68 // FIXME: Get the event time in the SDL tick time base 69 return SDL_GetTicksNS(); 70} 71 72void updateMouse(SDL_WSCONS_mouse_input_data *input) 73{ 74 struct wscons_event events[64]; 75 int n; 76 SDL_Mouse *mouse = SDL_GetMouse(); 77 78 if ((n = read(input->fd, events, sizeof(events))) > 0) { 79 int i; 80 n /= sizeof(struct wscons_event); 81 for (i = 0; i < n; i++) { 82 Uint64 timestamp = GetEventTimestamp(&events[i].time); 83 int type = events[i].type; 84 switch (type) { 85 case WSCONS_EVENT_MOUSE_DOWN: 86 case WSCONS_EVENT_MOUSE_UP: 87 { 88 Uint8 button = SDL_BUTTON_LEFT + events[i].value; 89 bool down = (type == WSCONS_EVENT_MOUSE_DOWN); 90 SDL_SendMouseButton(timestamp, mouse->focus, input->mouseID, button, down); 91 break; 92 } 93 case WSCONS_EVENT_MOUSE_DELTA_X: 94 { 95 SDL_SendMouseMotion(timestamp, mouse->focus, input->mouseID, true, (float)events[i].value, 0.0f); 96 break; 97 } 98 case WSCONS_EVENT_MOUSE_DELTA_Y: 99 { 100 SDL_SendMouseMotion(timestamp, mouse->focus, input->mouseID, true, 0.0f, -(float)events[i].value); 101 break; 102 } 103 case WSCONS_EVENT_MOUSE_DELTA_W: 104 { 105 SDL_SendMouseWheel(timestamp, mouse->focus, input->mouseID, events[i].value, 0, SDL_MOUSEWHEEL_NORMAL); 106 break; 107 } 108 case WSCONS_EVENT_MOUSE_DELTA_Z: 109 { 110 SDL_SendMouseWheel(timestamp, mouse->focus, input->mouseID, 0, -events[i].value, SDL_MOUSEWHEEL_NORMAL); 111 break; 112 } 113 default: 114 break; 115 } 116 } 117 } 118} 119 120void SDL_WSCONS_Quit_Mouse(SDL_WSCONS_mouse_input_data *input) 121{ 122 if (!input) { 123 return; 124 } 125 close(input->fd); 126 SDL_free(input); 127} 128
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.