Skip to content

Commit

Permalink
minor dominant axis code fixes
Browse files Browse the repository at this point in the history
  - fixed scope of new variables to static.
  - fixed C99 struct initialization.
  - removed unnecessary timecmp/timediff and oneline threshold function.
  • Loading branch information
jtsiomb committed Feb 22, 2023
1 parent 3bcaf90 commit d186c5d
Showing 1 changed file with 25 additions and 30 deletions.
55 changes: 25 additions & 30 deletions src/event.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
spacenavd - a free software replacement driver for 6dof space-mice.
Copyright (C) 2007-2022 John Tsiombikas <nuclear@member.fsf.org>
Copyright (C) 2007-2023 John Tsiombikas <nuclear@member.fsf.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "event.h"
#include "client.h"
#include "proto_unix.h"
Expand Down Expand Up @@ -47,9 +48,8 @@ struct dev_event {
struct dev_event *next;
};

struct dev_input inp_dominant = { -1, {0}, -1, 0 };
struct timeval dominant_offset = { 0, 100000 }; // 0.1s
int dominant_axis_treshold = 2;
static struct dev_input inp_dom = { -1, {0}, -1, 0 };
static int dom_axis_thres = 2;

static struct dev_event *add_dev_event(struct device *dev);
static struct dev_event *device_event_in_use(struct device *dev);
Expand All @@ -60,7 +60,7 @@ static unsigned int msec_dif(struct timeval tv1, struct timeval tv2);

static struct dev_event *dev_ev_list = NULL;

static int disable_translation, disable_rotation, dominant_axis;
static int disable_translation, disable_rotation, dom_axis_mode;


static struct dev_event *add_dev_event(struct device *dev)
Expand Down Expand Up @@ -144,10 +144,6 @@ static inline int map_axis(int devaxis)
return axis;
}

int dominant_threshold(int val, int threshold) {
return val <= -threshold || val >= threshold;
}

/* process_input processes an device input event, and dispatches
* spacenav events to the clients by calling dispatch_event.
* relative inputs (INP_MOTION) are accumulated, and dispatched when
Expand All @@ -158,7 +154,7 @@ void process_input(struct device *dev, struct dev_input *inp)
{
int sign, axis;
struct dev_event *dev_ev;
float sens_rot, sens_trans, axis_sensitivity;
float sens_rot, sens_trans, axis_sens;
spnav_event ev;

switch(inp->type) {
Expand All @@ -178,30 +174,29 @@ void process_input(struct device *dev, struct dev_input *inp)

sens_rot = disable_rotation ? 0 : cfg.sens_rot[axis - 3];
sens_trans = disable_translation ? 0 : cfg.sens_trans[axis];
axis_sensitivity = axis < 3 ? sens_trans : sens_rot;

if(dominant_axis) {
if(inp_dominant.idx != -1) {
struct timeval diff;
timersub(&(inp->tm), &(inp_dominant.tm), &diff);
if (timercmp(&diff, &dominant_offset, >=)) {
inp_dominant.idx = -1;
inp_dominant.tm = (struct timeval){0};
inp_dominant.type = INP_FLUSH;
inp_dominant.val = 0;
axis_sens = axis < 3 ? sens_trans : sens_rot;

if(dom_axis_mode) {
if(inp_dom.idx != -1) {
/* if more than 100ms have passed ... */
if(inp->tm.tv_sec > inp_dom.tm.tv_sec || inp->tm.tv_usec - inp_dom.tm.tv_usec >= 100000) {
inp_dom.idx = -1;
memset(&inp_dom.tm, 0, sizeof inp_dom.tm);
inp_dom.type = INP_FLUSH;
inp_dom.val = 0;
}
}
if((inp_dominant.idx == -1 && dominant_threshold(inp->val, dominant_axis_treshold))
|| inp_dominant.idx == axis) {
inp_dominant.idx = axis;
inp_dominant.tm = inp->tm;
inp_dominant.type = inp->type;
inp_dominant.val = inp->val;
if((inp_dom.idx == -1 && (inp->val <= dom_axis_thres || inp->val >= dom_axis_thres))
|| inp_dom.idx == axis) {
inp_dom.idx = axis;
inp_dom.tm = inp->tm;
inp_dom.type = inp->type;
inp_dom.val = inp->val;
} else {
axis_sensitivity = 0;
axis_sens = 0;
}
}
inp->val = (int)((float)inp->val * cfg.sensitivity * axis_sensitivity);
inp->val = (int)((float)inp->val * cfg.sensitivity * axis_sens);

dev_ev = device_event_in_use(dev);
if(verbose && dev_ev == NULL)
Expand Down Expand Up @@ -312,7 +307,7 @@ static void handle_button_action(int act, int pressed)
}
break;
case BNACT_DOMINANT_AXIS:
dominant_axis = !dominant_axis;
dom_axis_mode = !dom_axis_mode;
break;
}
}
Expand Down

0 comments on commit d186c5d

Please sign in to comment.