1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2017 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <dirent.h>
#include <linux/input.h>
#include <linux/kd.h>
#include <termios.h>
#include <unistd.h>
#include "linux_common.h"
#include "verbosity.h"
#include <retro_assert.h>
#include <retro_timers.h>
#include <rthreads/rthreads.h>
/* We can assume that pthreads are available on Linux. */
#include <pthread.h>
#include <retro_dirent.h>
#include <streams/file_stream.h>
#include <string.h>
#define IIO_DEVICES_DIR "/sys/bus/iio/devices"
#define IIO_ILLUMINANCE_SENSOR "in_illuminance_input"
#define DEFAULT_POLL_RATE 5
/* TODO/FIXME - static globals */
static struct termios old_term, new_term;
static long old_kbmd = 0xffff;
static bool linux_stdin_claimed = false;
struct linux_illuminance_sensor
{
sthread_t *thread;
/* Poll rate in Hz (i.e. in queries per second) */
volatile unsigned poll_rate;
/* We store the lux reading in millilux (as an int)
* so that we can make the access atomic and avoid locks.
* A little bit of precision is lost, but not enough to matter.
*/
volatile int millilux;
/* If true, the associated thread must finish its work and exit. */
volatile bool done;
char path[PATH_MAX_LENGTH];
};
static double linux_read_illuminance_sensor(const linux_illuminance_sensor_t *sensor);
void linux_terminal_restore_input(void)
{
if (old_kbmd == 0xffff)
return;
if (ioctl(0, KDSKBMODE, old_kbmd) < 0)
return;
tcsetattr(0, TCSAFLUSH, &old_term);
old_kbmd = 0xffff;
linux_stdin_claimed = false;
}
/* Disables input */
static bool linux_terminal_init(void)
{
if (old_kbmd != 0xffff)
return false;
if (tcgetattr(0, &old_term) < 0)
return false;
new_term = old_term;
new_term.c_lflag &= ~(ECHO | ICANON | ISIG);
new_term.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
new_term.c_cc[VMIN] = 0;
new_term.c_cc[VTIME] = 0;
/* Be careful about recovering the terminal. */
if (ioctl(0, KDGKBMODE, &old_kbmd) < 0)
return false;
if (tcsetattr(0, TCSAFLUSH, &new_term) < 0)
return false;
return true;
}
/* We need to disable use of stdin command interface if
* stdin is supposed to be used for input. */
void linux_terminal_claim_stdin(void)
{
linux_stdin_claimed = true;
}
bool linux_terminal_grab_stdin(void *data)
{
return linux_stdin_claimed;
}
static void linux_terminal_restore_signal(int sig)
{
linux_terminal_restore_input();
kill(getpid(), sig);
}
bool linux_terminal_disable_input(void)
{
struct sigaction sa = {0};
/* Avoid accidentally typing stuff. */
if (!isatty(0))
return false;
if (!linux_terminal_init())
return false;
if (ioctl(0, KDSKBMODE, K_MEDIUMRAW) < 0)
{
tcsetattr(0, TCSAFLUSH, &old_term);
return false;
}
sa.sa_handler = linux_terminal_restore_signal;
sa.sa_flags = SA_RESTART | SA_RESETHAND;
sigemptyset(&sa.sa_mask);
/* Trap some standard termination codes so we
* can restore the keyboard before we lose control. */
sigaction(SIGABRT, &sa, NULL);
sigaction(SIGBUS, &sa, NULL);
sigaction(SIGFPE, &sa, NULL);
sigaction(SIGILL, &sa, NULL);
sigaction(SIGQUIT, &sa, NULL);
sigaction(SIGSEGV, &sa, NULL);
atexit(linux_terminal_restore_input);
return true;
}
static void linux_poll_illuminance_sensor(void *data)
{
linux_illuminance_sensor_t *sensor = data;
if (!data)
return;
while (!sensor->done)
{ /* Aligned int reads are atomic on most CPUs */
double lux;
int millilux;
unsigned poll_rate = sensor->poll_rate;
/* Don't allow cancellation inside the critical section,
* as it opens up a file; we don't want to leak it! */
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
lux = linux_read_illuminance_sensor(sensor);
millilux = (int)(lux * 1000.0);
retro_assert(poll_rate != 0);
sensor->millilux = millilux;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
/* Allow cancellation here so that the main thread doesn't block
* while waiting for this thread to wake up and exit. */
retro_sleep(1000 / poll_rate);
if (errno == EINTR)
{
RARCH_ERR("Illuminance sensor thread interrupted.\n");
break;
}
}
RARCH_DBG("Illuminance sensor thread for %s exiting.\n", sensor->path);
}
linux_illuminance_sensor_t *linux_open_illuminance_sensor(unsigned rate)
{
RDIR *device = NULL;
linux_illuminance_sensor_t *sensor = (linux_illuminance_sensor_t *)
malloc(sizeof(*sensor));
if (!sensor)
goto error;
device = retro_opendir(IIO_DEVICES_DIR);
if (!device)
goto error;
sensor->millilux = 0;
sensor->poll_rate = rate ? rate : DEFAULT_POLL_RATE;
sensor->thread = NULL; /* We'll spawn a thread later, once we find a sensor */
sensor->done = false;
while (retro_readdir(device))
{ /* For each IIO device... */
const char *name = retro_dirent_get_name(device);
double lux = 0.0;
if (!name)
{
RARCH_ERR("Error reading " IIO_DEVICES_DIR ".\n");
goto error;
}
if (name[0] == '.')
/* Skip hidden files, ".", and ".." */
continue;
/* If that worked out, look to see if this device represents an illuminance sensor */
snprintf(sensor->path, sizeof(sensor->path), IIO_DEVICES_DIR "/%s/" IIO_ILLUMINANCE_SENSOR, name);
lux = linux_read_illuminance_sensor(sensor);
if (lux >= 0)
{ /* If we found an illuminance sensor that works... */
sensor->millilux = (int)(lux * 1000.0); /* Set the first reading */
sensor->thread = sthread_create(linux_poll_illuminance_sensor, sensor);
if (!sensor->thread)
{
RARCH_ERR("Failed to spawn thread for illuminance sensor.\n");
goto error;
}
RARCH_LOG("Opened illuminance sensor at %s, polling at %u Hz.\n", sensor->path, sensor->poll_rate);
retro_closedir(device);
return sensor;
}
}
error:
RARCH_ERR("Failed to find an illuminance sensor in " IIO_DEVICES_DIR ".\n");
retro_closedir(device);
free(sensor);
return NULL;
}
void linux_close_illuminance_sensor(linux_illuminance_sensor_t *sensor)
{
if (!sensor)
return;
if (sensor->thread)
{
pthread_t thread = (pthread_t)sthread_get_thread_id(sensor->thread);
sensor->done = true;
if (pthread_cancel(thread) != 0)
{
int err = errno;
char errmesg[NAME_MAX_LENGTH];
strerror_r(err, errmesg, sizeof(errmesg));
RARCH_ERR("Failed to cancel illuminance sensor thread: %s.\n", errmesg);
/* this doesn't happen often, it should probably be logged */
}
sthread_join(sensor->thread);
/* sthread_join will free the thread */
}
free(sensor);
}
float linux_get_illuminance_reading(const linux_illuminance_sensor_t *sensor)
{
int millilux;
if (!sensor)
return -1.0f;
/* Reading an int is atomic on most CPUs */
millilux = sensor->millilux;
return (float)millilux / 1000.0f;
}
void linux_set_illuminance_sensor_rate(linux_illuminance_sensor_t *sensor, unsigned rate)
{
if (!sensor)
return;
/* Set a default rate of 5 Hz if none is provided */
rate = rate ? rate : DEFAULT_POLL_RATE;
sensor->poll_rate = rate;
}
static double linux_read_illuminance_sensor(const linux_illuminance_sensor_t *sensor)
{
char buffer[256];
double illuminance = 0.0;
RFILE *in_illuminance_input = NULL;
int err = 0;
if (!sensor || sensor->path[0] == '\0')
return -1.0;
in_illuminance_input = filestream_open(sensor->path, RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE);
if (!in_illuminance_input)
{
RARCH_ERR("Failed to open \"%s\".\n", sensor->path);
return 0.0;
}
/* Read the illuminance value from the file. If that fails... */
if (!filestream_gets(in_illuminance_input, buffer, sizeof(buffer)))
{
RARCH_ERR("Illuminance sensor read failed.\n");
filestream_close(in_illuminance_input);
return -1.0;
}
filestream_close(in_illuminance_input);
/* Clear any existing error so we'll know if strtod fails */
errno = 0;
/* TODO: This may be locale-sensitive */
illuminance = strtod(buffer, NULL);
err = errno;
if (err != 0)
{
RARCH_ERR("Failed to parse input \"%s\" into a floating-point value: %s.\n", buffer, strerror(err));
return -1.0;
}
return illuminance;
}
|