Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/pfps/yoga-laptop
Browse files Browse the repository at this point in the history
  • Loading branch information
pfps committed Sep 15, 2014
2 parents 59537b6 + 14d7489 commit 666b48e
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 56 deletions.
8 changes: 4 additions & 4 deletions sensors/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ GROUP := $(shell /usr/bin/id -gn `/usr/bin/logname`)
all: generic_buffer orientation light

generic_buffer: generic_buffer.c iio_utils.h
cc generic_buffer.c -o generic_buffer
cc generic_buffer.c $(CFLAGS) -o generic_buffer

orientation: orientation.c iio_utils.h
cc orientation.c -o orientation
cc orientation.c $(CFLAGS) -o orientation

hinge: hinge.c iio_utils.h
cc hinge.c -o hinge
cc hinge.c $(CFLAGS) -o hinge

light: light.c
cc `pkg-config --cflags glib-2.0 --libs glib-2.0` -o light light.c
cc `pkg-config --cflags glib-2.0 --libs glib-2.0` $(CFLAGS) -o light light.c

permissions:
@echo Making the control files writable by you
Expand Down
1 change: 0 additions & 1 deletion sensors/generic_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ static int enable_sensors(const char *device_dir)
{
DIR *dp;
FILE *sysfsfp;
int i;
int ret;
const struct dirent *ent;
char *scan_el_dir;
Expand Down
1 change: 1 addition & 0 deletions sensors/libs/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ extern "C" {
#include <errno.h>
#include <sys/stat.h>
#include <sys/dir.h>
#include <sys/wait.h>
#include <linux/types.h>
#include <string.h>
#include <poll.h>
Expand Down
4 changes: 2 additions & 2 deletions sensors/libs/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ extern "C" {
// Times to run the script
int iterations;
// Device name
char* device_name;
const char* device_name;

/* Orientation config*/
char *or_touchScreenName;
const char *or_touchScreenName;

/* Light config */
// Top value used for scaling
Expand Down
8 changes: 5 additions & 3 deletions sensors/libs/iio_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ int write_sysfs_int2(char *filename, char *basedir, int val, int val2) {
return _write_sysfs_int(filename, basedir, val, 0, 1, val2);
}

int _write_sysfs_string(char *filename, char *basedir, char *val, int verify) {
int _write_sysfs_string(const char *filename, const char *basedir, const char *val, int verify) {
int ret = 0;
FILE *sysfsfp;
char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
Expand Down Expand Up @@ -603,11 +603,11 @@ int _write_sysfs_string(char *filename, char *basedir, char *val, int verify) {
* @basedir: the sysfs directory in which the file is to be found
* @val: the string to write
**/
int write_sysfs_string_and_verify(char *filename, char *basedir, char *val) {
int write_sysfs_string_and_verify(const char *filename, const char *basedir, const char *val) {
return _write_sysfs_string(filename, basedir, val, 1);
}

int write_sysfs_string(char *filename, char *basedir, char *val) {
int write_sysfs_string(const char *filename, const char *basedir, const char *val) {
return _write_sysfs_string(filename, basedir, val, 0);
}

Expand Down Expand Up @@ -659,6 +659,8 @@ typedef struct SensorData_s {
char* data;
} SensorData;

int size_from_channelarray(struct iio_channel_info *channels, int num_channels);

int prepare_output(Device_info* info, char * dev_dir_name, char * trigger_name,
int (*callback)(SensorData, Device_info, Config), Config config) {
char * buffer_access;
Expand Down
1 change: 0 additions & 1 deletion sensors/libs/sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ extern "C" {
static int enable_sensors(const char *device_dir) {
DIR *dp;
FILE *sysfsfp;
int i;
int ret;
const struct dirent *ent;
char *scan_el_dir;
Expand Down
9 changes: 4 additions & 5 deletions sensors/light.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
**/
int process_scan(SensorData data, Device_info info, Config config) {
if (info.channels_count != 1 || info.channels[0].bytes != 4) {
return;
return 0;
}
struct iio_channel_info channel = info.channels[0];
if (channel.is_signed) {
Expand Down Expand Up @@ -76,7 +76,7 @@ void sigint_callback_handler(int signum) {

int main(int argc, char **argv) {
/* Configuration variables */
char *trigger_name = NULL, *config_file = "conf/light.ini";
char *trigger_name = NULL;
Config config = Config_default;
static int version_flag = 0, help_flag = 0;

Expand All @@ -93,7 +93,7 @@ int main(int argc, char **argv) {
}

/* Arguments definition */
static char* version = "light version 0.3\n";
static const char* version = "light version 0.3\n";
static char* help;
asprintf(&help, "light monitors ambient light sensor and adjusts backlight acordingly\
\n\
Expand Down Expand Up @@ -180,7 +180,7 @@ Options:\n\
if (config.debug_level > DEBUG_ALL) printf("iio device number being used is %d\n", info.device_id);

/* enable the sensors in the device */
asprintf(&dev_dir_name, "%siio:device%d", iio_dir, info.device_id);
ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, info.device_id);
if (ret < 0) {
ret = -ENOMEM;
goto error_ret;
Expand Down Expand Up @@ -223,7 +223,6 @@ Options:\n\

error_free_triggername:
free(trigger_name);
error_free_dev_dir_name:
free(dev_dir_name);
error_ret:
return ret;
Expand Down
84 changes: 44 additions & 40 deletions sensors/orientation.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,27 @@
* This code exec's xrandr and xinput as root.
*/

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif // if !defined(_GNU_SOURCE)

#include "libs/common.h"

static char* touchScreenName = "";
typedef enum {
INVALID = -1, FLAT = 0, TOP, RIGHT, BOTTOM, LEFT
} OrientationPositions; /* various orientations */

static const char* touchScreenName = "";
static int debug_level = -1;
static bool orientation_lock = false;
static int screen_orientation = -1;
static OrientationPositions screen_orientation = INVALID;
static int previous_orientation = -1;
static char *dev_dir_name;
static time_t last_sigusr_time = 0;


typedef enum {
FLAT, TOP, RIGHT, BOTTOM, LEFT
} OrientationPositions; /* various orientations */

int rotate_left_orientation(OrientationPositions orientation) {
return (LEFT == orientation) ? TOP : orientation + 1;
OrientationPositions rotate_left_orientation(OrientationPositions orientation) {
return (LEFT == orientation) ? TOP : (OrientationPositions)((int)orientation + 1);
}

/**
Expand All @@ -57,7 +59,7 @@ int rotate_left_orientation(OrientationPositions orientation) {
* ch_present: whether the channel is present
**/
void process_scan_1(char *data, struct iio_channel_info *channels, int num_channels,
char *ch_name, int *ch_val, bool *ch_present) {
const char *ch_name, int *ch_val, bool *ch_present) {
int k;
for (k = 0; k < num_channels; k++) {
if (0 == strcmp(channels[k].name, ch_name)) {
Expand Down Expand Up @@ -92,16 +94,17 @@ void process_scan_1(char *data, struct iio_channel_info *channels, int num_chann
* process_scan_3() - get three integer values - see above
**/
void process_scan_3(char *data, struct iio_channel_info *channels, int num_channels,
char *ch_name_1, int *ch_val_1, bool *ch_present_1,
char *ch_name_2, int *ch_val_2, bool *ch_present_2,
char *ch_name_3, int *ch_val_3, bool *ch_present_3) {
const char *ch_name_1, int *ch_val_1, bool *ch_present_1,
const char *ch_name_2, int *ch_val_2, bool *ch_present_2,
const char *ch_name_3, int *ch_val_3, bool *ch_present_3) {
process_scan_1(data, channels, num_channels, ch_name_1, ch_val_1, ch_present_1);
process_scan_1(data, channels, num_channels, ch_name_2, ch_val_2, ch_present_2);
process_scan_1(data, channels, num_channels, ch_name_3, ch_val_3, ch_present_3);
}

int process_scan(SensorData data, Device_info info, Config config) {
int orientation = FLAT, i;
OrientationPositions orientation = FLAT;
int i;

int accel_x, accel_y, accel_z;
bool present_x, present_y, present_z;
Expand Down Expand Up @@ -129,12 +132,12 @@ int process_scan(SensorData data, Device_info info, Config config) {
if (config.debug_level > 1) printf("Orientation %d, x:%5d, y:%5d, z:%5d\n",
orientation, accel_x, accel_y, accel_z);
}
return orientation;
return (int)orientation;
}

/* symbolic orientation as used in xrandr */
char * symbolic_orientation(OrientationPositions orientation) {
char * orient;
const char * symbolic_orientation(OrientationPositions orientation) {
const char * orient;
switch (orientation) {
case FLAT: orient = "flat";
break;
Expand All @@ -146,23 +149,25 @@ char * symbolic_orientation(OrientationPositions orientation) {
break;
case RIGHT: orient = "right";
break;
default: orient = "invalid";
break;
}
return orient;
}

void rotate_to(OrientationPositions orient) {
char * xrandr = "/usr/bin/xrandr";
char * xinput = "/usr/bin/xinput";
char * tsnormal[] = {xinput, "set-prop", touchScreenName, "Coordinate Transformation Matrix",
const char * xrandr = "/usr/bin/xrandr";
const char * xinput = "/usr/bin/xinput";
const char * const tsnormal[] = {xinput, "set-prop", touchScreenName, "Coordinate Transformation Matrix",
"1", "0", "0", "0", "1", "0", "0", "0", "1", (char *) NULL};
char * tsright[] = {xinput, "set-prop", touchScreenName, "Coordinate Transformation Matrix",
const char * const tsright[] = {xinput, "set-prop", touchScreenName, "Coordinate Transformation Matrix",
"0", "1", "0", "-1", "0", "1", "0", "0", "1", (char *) NULL};
char * tsleft[] = {xinput, "set-prop", touchScreenName, "Coordinate Transformation Matrix",
const char * const tsleft[] = {xinput, "set-prop", touchScreenName, "Coordinate Transformation Matrix",
"0", "-1", "1", "1", "0", "0", "0", "0", "1", (char *) NULL};
char * tsinverted[] = {xinput, "set-prop", touchScreenName, "Coordinate Transformation Matrix",
const char * const tsinverted[] = {xinput, "set-prop", touchScreenName, "Coordinate Transformation Matrix",
"-1", "0", "1", "0", "-1", "1", "0", "0", "1", (char *) NULL};
int status, pid;
char *orientation = symbolic_orientation(orient);
int status = 0, pid;
const char *orientation = symbolic_orientation(orient);

printf("ROTATE to %s\n", orientation);
screen_orientation = orient;
Expand All @@ -174,13 +179,16 @@ void rotate_to(OrientationPositions orient) {

if (0 != strlen(touchScreenName) && 0 == (pid = fork())) { /* rotate the touchscreen */
switch (orient) {
case TOP: execv(xinput, tsnormal);
case TOP: execv(xinput, (char * const *)(tsnormal));
break;
case BOTTOM: execv(xinput, (char * const *)(tsinverted));
break;
case BOTTOM: execv(xinput, tsinverted);
case LEFT: execv(xinput, (char * const *)(tsleft));
break;
case LEFT: execv(xinput, tsleft);
case RIGHT: execv(xinput, (char * const *)(tsright));
break;
case RIGHT: execv(xinput, tsright);
case FLAT:
default:
break;
}
} else {
Expand All @@ -201,11 +209,8 @@ void sigint_callback_handler(int signum) {
}

void sigusr_callback_handler(int signum) {
int now = time(NULL), pid, status, bufflen;
char * buffer;
char * notify = "/usr/bin/notify-send"; // -i /home/buri/.utils/yoga/rotateon.png "Rotace aktivována"";
char * tslockon[] = {notify, "-i", "/home/buri/.utils/yoga/rotateon.png", "Autorotate enabled", (char*) NULL};
char * tslockoff[] = {notify, "-i", "/home/buri/.utils/yoga/rotateoff.png", "Autorotate disabled", (char*) NULL};
(void)(signum); // unused
int now = time(NULL), pid, status;

previous_orientation = screen_orientation;
if (now <= last_sigusr_time + 1) {
Expand Down Expand Up @@ -242,7 +247,8 @@ void sigusr_callback_handler(int signum) {
int main(int argc, char **argv) {
/* Configuration variables */
Config config = Config_default;
char *trigger_name = NULL, *device_name = "accel_3d";
char * trigger_name = NULL;
const char *device_name = "accel_3d";


// Update default settings
Expand All @@ -252,7 +258,7 @@ int main(int argc, char **argv) {

/* Arguments definition */
static int version_flag = 0, help_flag = 0;
static char* version = "orientation version 0.3\n";
static const char* version = "orientation version 0.3\n";
static char* help;
asprintf(&help, "orientation monitors the Yoga accelerometer and\n\
rotates the screen and touchscreen to match\n\
Expand Down Expand Up @@ -281,8 +287,7 @@ Use via something like\n\
int ret, c, i;
char * dummy;

unsigned int sleeping = 1000000;
int orientation = 0;
OrientationPositions orientation = FLAT;

static struct option long_options[] = {
{"version", no_argument, &version_flag, 1},
Expand Down Expand Up @@ -347,7 +352,7 @@ Use via something like\n\
if (debug_level > -1) printf("iio device number being used is %d\n", info.device_id);

/* enable the sensors in the device */
asprintf(&dev_dir_name, "%siio:device%d", iio_dir, info.device_id);
ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, info.device_id);
if (ret < 0) {
ret = -ENOMEM;
goto error_ret;
Expand Down Expand Up @@ -383,7 +388,7 @@ Use via something like\n\

for (i = 0; i != config.iterations; i++) {
if (config.debug_level > 2) printf("Finding orientation %d\n", orientation);
if ((orientation = prepare_output(&info, dev_dir_name, trigger_name, &process_scan, config)) < 0) break;
if ((int)(orientation = (OrientationPositions)(prepare_output(&info, dev_dir_name, trigger_name, &process_scan, config))) < 0) break;
if (config.debug_level > 2) printf("Found orientation: or:%d, prev:%d, scr:%d, %d\n", orientation, previous_orientation, screen_orientation, FLAT);
if (config.debug_level > 0) printf("Orientation at %3.1f is %s\n", ((double) config.poll_timeout / 1000000.0) * i, symbolic_orientation(orientation));
if (previous_orientation == orientation /* only rotate when stable */ &&
Expand All @@ -398,7 +403,6 @@ Use via something like\n\

error_free_triggername:
free(trigger_name);
error_free_dev_dir_name:
free(dev_dir_name);
error_ret:
return ret;
Expand Down

0 comments on commit 666b48e

Please sign in to comment.