OpenGL SDL2 & Mouse Event - Raspberry Pi Forums


hello,

try build photobooth using pi, picam & touchscreen act usb mouse.

build under sdl2 & opengl (fullscreen application). here problem :

- detect screen touch, want detect mouse move.
- sdl2 opengl doesn't work under x, have run program console
- sdl2 catch no event in configuration : no mouse & no keyboard (my code below)

how can mouse event if i'm not running x ? have mannualy manage /dev/input/event or /dev/input/mice (hard stuff) ?

help

code read event :

init sdl :

code: select all

	window = sdl_createwindow("phototwix",sdl_windowpos_undefined, 														  sdl_windowpos_undefined, 														  screen_width, 														  screen_height, 														  sdl_window_fullscreen | sdl_window_opengl);
event pooling :

code: select all

while (sdl_pollevent(&event)) //retrieve mouse move 		 {              std::cout << "event : " << event.type << std::endl; 			switch(event.type)             { 				case sdl_mousemotion: 					std::cout << "mouse moved " << event.motion.xrel << "," << event.motion.yrel << "to (" << event.motion.x << "," << "event.motion.y" << std::endl; 					break; 					 				case sdl_mousebuttondown: 					break; 					 				case sdl_keydown: 				switch (event.key.keysym.sym) 				{ 					case sdlk_left:  std::cout << "left" << std::endl ; break; 					case sdlk_right: std::cout << "right" << std::endl ; break; 					case sdlk_up:    std::cout << "up" << std::endl ; break; 					case sdlk_down:  std::cout << "down" << std::endl ; break; 				} 				break;             }         } 

just in case of need answer. here code read directly /dev/input/event0 touch actions

idea read contently event file in separate thread , store result in static variable (shared different threads)

control.h

code: select all

 #ifndef __control_h__ # define __control_h_  # define screen_input "/dev/input/event0"  # define screen_touch_max_x	4096 # define screen_touch_max_y	4096  struct mouse_position { 	mouse_position() : cur_x(-1), cur_y(-1), start_x(-1), start_y(-1), press(false) {} 	int 	cur_x; 	int 	cur_y; 	int		start_x; 	int		start_y; 	bool	press; };  static mouse_position g_mouse;  static bool g_finish_mouse_thread;  void init_control();  void exec_control();  #endif 
control.cpp

code: select all

#include <linux/input.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <thread> #include <iostream>  #include "control.h"  void task_mouse() { //read /dev/input/event0 touch event 	int fd = open(screen_input, o_rdonly);     struct input_event ev; 	 	while (1)     {         if (g_finish_mouse_thread) { 			break; 		} 		 		read(fd, &ev, sizeof(struct input_event));          if (ev.type == 3) { 			if (ev.code == 53) { 				g_mouse.cur_x = ev.value; 				if (g_mouse.start_x == -1) { 					g_mouse.start_x = ev.value; 				} 				//printf("x:%d\n", ev.value); 			} 			 			if (ev.code == 54) { 				g_mouse.cur_y = ev.value; 				if (g_mouse.start_y == -1) { 					g_mouse.start_y = ev.value; 				} 				//printf("y:%d\n", ev.value); 			} 			 			if (ev.code == 57 && ev.value > 0) { 				g_mouse.press = true; 				g_mouse.start_x = -1; 				g_mouse.start_y = -1; 				//printf("press\n"); 			} 			 			if (ev.code == 57 && ev.value == -1) { 				g_mouse.press = false; 				//printf("release\n"); 			} 		}     } }  void init_control() { 	//init mouse in different thread 	g_finish_mouse_thread = false;  	std::thread t1(task_mouse); 	t1.detach(); }  void exec_control() { 	if (g_mouse.press == true) { 		if (g_mouse.start_x == -1) { //first press 			std::cout << "press x:" << g_mouse.cur_x << " y:" << g_mouse.cur_y << std::endl; 		} else { //it move 			std::cout << "move x:" << g_mouse.start_x << " y:" << g_mouse.start_y << " x:" << g_mouse.cur_x << " y:" << g_mouse.cur_y << std::endl; 		} 	} } 


raspberrypi



Comments

Popular posts from this blog

Convierte tu Raspberry en un NAS. Firmware fvdw-sl 15.3 - Raspberry Pi Forums

How to format a Get Request

avrdude: verification error, first mismatch at byte 0x0000 0x0c != 0x62