AiCPlayer
Interface of aic vm - for rendering aspect, sensors, video records
sdl_events.c
Go to the documentation of this file.
1 
4 #include <pthread.h>
5 #include <sys/time.h> // for timeval, gettimeofday
6 #include <time.h> // for time_t
7 #include <linux/input.h>
8 
9 #include "buffer_sizes.h"
10 #include "logger.h"
11 #include "grabber.h"
12 #include "sdl_events.h"
13 #include "sdl_translate.h"
14 
15 #define LOG_TAG "sdl_events"
16 
17 extern int g_width;
18 extern int g_height;
19 extern float g_rotation;
20 
23 static void grab_time(char* stringtime)
24 {
25  char T[30];
26  time_t curtime;
27  struct timeval tv;
28  gettimeofday(&tv, NULL);
29  curtime = tv.tv_sec;
30 
31  struct tm cur_localtime;
32  localtime_r(&curtime, &cur_localtime);
33  strftime(T, 30, "%Y-%d-%mT%H:%M:%S", &cur_localtime);
34  snprintf(stringtime, 30, "%s.%ld", T, tv.tv_usec);
35 }
36 
40 static void convert_mouse_position(int* x, int* y)
41 {
42  int tmp;
43 
44  switch ((int) g_rotation)
45  {
46  case 90:
47  tmp = *x;
48  *x = g_width - *y;
49  *y = tmp;
50  break;
51  case 180:
52  *x = g_width - *x;
53  break;
54  case 270:
55  tmp = *x;
56  *x = *y;
57  *y = g_height - tmp;
58  break;
59  default:
60  break;
61  }
62 }
63 
64 int sdl_key(SDL_Event* event, socket_t input_socket)
65 {
66  static int moreFrames = 0;
67  int xtkey, ret = 0;
68 
69  xtkey = sdl_translate_event(event->key.keysym.scancode, event->key.keysym.sym);
70  LOGI("Got SDL_KEY with state=%d code=%d keysym=%d xtkey=%d input_socket=%d", event->key.state,
71  event->key.keysym.scancode, event->key.keysym.sym, xtkey, input_socket);
72 
73  static struct thread_args grab_args;
74  static pthread_t pgrab_Thread;
75  char strTime[BUF_SIZE];
76  if (xtkey == KEY_F7 && moreFrames == 0) /* F7, start recording */
77  {
78  char strPrefix[] = "log/video_F7_";
79  memset(&grab_args, 0, sizeof(struct thread_args));
80  grab_time(strTime);
81  snprintf(grab_args.record_filename, BUF_SIZE, "%s%s.mp4", strPrefix, strTime);
82  moreFrames = 1;
83  pthread_mutex_init(&grab_args.mtx, NULL);
84  pthread_mutex_lock(&grab_args.mtx);
85  pthread_create(&pgrab_Thread, NULL, (void*) &ffmpeg_grabber, &grab_args);
86  }
87  else if (xtkey == KEY_F8 && moreFrames == 1) /* F8, stop recording */
88  {
89  moreFrames = 0;
90  pthread_mutex_unlock(&grab_args.mtx);
91  pthread_join(pgrab_Thread, NULL);
92  }
93  else if (event->type == SDL_KEYDOWN && xtkey == KEY_F6) /* F6, take screenshot */
94  {
95  char snap_filename[BUF_SIZE];
96  grab_time(strTime);
97  snprintf(snap_filename, sizeof(snap_filename), "log/snap_F6_%s.bmp", strTime);
98  LOGI("Saving snapshot %s", snap_filename);
99  grab_snapshot(snap_filename);
100  }
101 
102  if (input_socket)
103  {
104  char buffer[BUF_SIZE];
105  snprintf(buffer, BUF_SIZE, "%s:%d:%d\n",
106  (event->key.state == SDL_PRESSED) ? "KBDPR" : "KBDRL", xtkey, event->key.state);
107  ret = send(input_socket, buffer, strlen(buffer), 0);
108  }
109  return ret;
110 }
111 
112 int sdl_mouse_motion(SDL_Event* event, socket_t input_socket)
113 {
114  int x = event->motion.x;
115  int y = event->motion.y;
116  convert_mouse_position(&x, &y);
117  if (input_socket)
118  {
119  char buffer[BUF_SIZE];
120  snprintf(buffer, BUF_SIZE, "MOUSE:%d:%d\n", x, y);
121  return send(input_socket, buffer, strlen(buffer), 0);
122  }
123  return 0;
124 }
125 
126 int sdl_mouse_wheel(SDL_Event* event, socket_t input_socket)
127 {
128  char buffer[BUF_SIZE];
129  int x, y;
130  SDL_GetMouseState(&x, &y);
131  convert_mouse_position(&x, &y);
132  if (event->wheel.y > 0)
133  snprintf(buffer, BUF_SIZE, "WHEEL:%d:%d:1:0\n", x, y);
134  else if (event->wheel.y < 0)
135  snprintf(buffer, BUF_SIZE, "WHEEL:%d:%d:-1:0\n", x, y);
136  else
137  return 0;
138  return send(input_socket, buffer, strlen(buffer), 0);
139 }
140 
141 int sdl_mouse_button(SDL_Event* event, socket_t input_socket)
142 {
143  int x = event->motion.x;
144  int y = event->motion.y;
145  convert_mouse_position(&x, &y);
146  if (input_socket)
147  {
148  char buffer[BUF_SIZE];
149  switch (event->button.button)
150  {
151  case SDL_BUTTON_LEFT:
152  if (event->button.state == SDL_PRESSED)
153  snprintf(buffer, BUF_SIZE, "MSBPR:%d:%d\n", x, y);
154  else
155  snprintf(buffer, BUF_SIZE, "MSBRL:%d:%d\n", x, y);
156  break;
157  default:
158  return 0;
159  }
160  return send(input_socket, buffer, strlen(buffer), 0);
161  }
162  return SOCKET_ERROR;
163 }
#define BUF_SIZE
Small, fixed-size buffers.
Definition: buffer_sizes.h:9
Struct holding the state of a recording (filename/lock)
Definition: grabber.h:60
int sdl_mouse_wheel(SDL_Event *event, socket_t input_socket)
Produce a mouse wheel event to the virtual input.
Definition: sdl_events.c:126
int socket_t
Alias to differenciate between regular ints and socket fds.
Definition: socket.h:13
void grab_snapshot(char *snap_filename)
Definition: grabber.c:584
int g_width
Global variable for the window width.
Definition: main.c:93
int ffmpeg_grabber(void *arg)
Function passed to a thread in order to detect format encoding - encode frame- and write in a file...
Definition: grabber.c:420
int sdl_translate_event(SDL_Scancode scancode, SDL_Keycode keysym)
Convert an SDL scancode to a value read by the virtual input daemon.
Logging macros.
Convert sdl events to uinput codes.
char record_filename[BUF_SIZE]
Definition: grabber.h:63
grabber records videos or snapshots from ampq messages
Produce virtual input events from SDL events.
#define SOCKET_ERROR
Alias for the recv() return value in case of error.
Definition: socket.h:10
float g_rotation
Definition: main.c:100
Define common buffer sizes.
pthread_mutex_t mtx
Definition: grabber.h:62
#define LOGI(...)
Log at INFO level.
Definition: logger.h:23
int sdl_key(SDL_Event *event, socket_t input_socket)
Produce a key press/release event to the virtual input.
Definition: sdl_events.c:64
int g_height
Global variable for the window height.
Definition: main.c:87
int sdl_mouse_motion(SDL_Event *event, socket_t input_socket)
Produce a mouse motion event to the virtual input.
Definition: sdl_events.c:112
int sdl_mouse_button(SDL_Event *event, socket_t input_socket)
Produce a mouse click event to the virtual input.
Definition: sdl_events.c:141