Skip to content

Commit

Permalink
Merge pull request #23 from FedeDP/devel
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
FedeDP authored Mar 3, 2018
2 parents 42747ad + a4c4679 commit 4f8efac
Show file tree
Hide file tree
Showing 31 changed files with 591 additions and 938 deletions.
5 changes: 2 additions & 3 deletions Arch/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ arch=('i686' 'x86_64')
url="https://github.com/FedeDP/${_gitname}"
license=('GPL')
backup=(etc/default/clight.conf)
depends=('systemd' 'popt' 'libconfig' 'gsl' 'clightd-git')
depends=('systemd>=221' 'popt' 'libconfig' 'gsl' 'clightd-git>=2.0')
makedepends=('git')
optdepends=('geoclue2: to retrieve user location through geoclue2.'
'upower: to save energy by increasing timeouts between captures while on battery.'
'networkmanager: to manage connection state changes.')
'upower: to save energy by increasing timeouts between captures while on battery.')
source=("git://github.com/FedeDP/${_gitname}.git")
install=clight.install
sha256sums=("SKIP")
Expand Down
2 changes: 1 addition & 1 deletion DEBIAN/control
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ Section: base
Priority: optional
Architecture: amd64
Depends: libpopt0, libsystemd0 (>= 221), libconfig9, libgsl2, clightd (>= 1.4)
Suggests: geoclue-2.0, upower, network-manager
Suggests: geoclue-2.0, upower
Maintainer: Federico Di Pierro <nierro92@gmail.com>
Description: A C daemon that turns your webcam into a light sensor. It can also change display gamma temperature, dim your screen and set your dpms.
49 changes: 33 additions & 16 deletions Extra/clight.conf
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
## in the corresponding AC state.
# dimmer_timeouts = [ 300, 45 ];

## Weather timeouts on AC/on BATT.
## Defaults to 1 hr / 3 hrs
# weather_timeouts = [ 3600, 10800 ];

## Timeouts for dpms on AC
## Set any of these to <= 0 to disable dpms on AC
# ac_dpms_timeouts = [ 900, 1200, 1800 ];
Expand All @@ -40,11 +36,8 @@
# DISABLE MODULES #
###################

## Uncomment to disable gamma smooth transitions
# no_smooth_gamma_transition = 1;

## Uncomment to disable dimmer smooth transitions
# no_smooth_dimmer_transition = 1;
## Uncomment to disable brightness support
# no_brightness = 1;

## Uncomment to disable gamma support
# no_gamma = 1;
Expand All @@ -59,8 +52,36 @@
## Uncomment to disable dimmer tool
# no_dimmer = 1;

## Uncomment to disable weather support
# no_weather = 1;
##################
# SMOOTH OPTIONS #
##################

## Uncomment to disable backlight smooth transitions
# no_smooth_backlight_transition = 1;

## Uncomment to disable gamma smooth transitions
# no_smooth_gamma_transition = 1;

## Uncomment to disable dimmer smooth transitions
# no_smooth_dimmer_transition = 1;

## Backlight transition step in percentage
# backlight_trans_step = 0.05;

## Backlight transition step
# gamma_trans_step = 50;

## Dimmer transition step in percentage
# dimmer_trans_step = 0.05;

## Backlight transition timeout in ms
# backlight_trans_timeout = 30;

## Backlight transition timeout in ms
# gamma_trans_timeout = 300;

## Dimmer transition timeout in ms
# dimmer_trans_timeout = 30;

####################
# BACKLIGHT CURVES #
Expand Down Expand Up @@ -104,11 +125,7 @@
# event_duration = 1800

## Change dimmer backlight level, in percentage
# dimmer_pct = 20;

## OpenWeatherMap apikey to be used for weather support:
## it will reduce timeouts between captures depending on cloudiness on your location.
# weather_apikey = "";
# dimmer_pct = 0.2;

## Verbose mode, useful in case of bugs:
## run clight in verbose mode,
Expand Down
21 changes: 9 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,19 @@ Finally, it can dim your screen after a timeout.
It was heavily inspired by [calise](http://calise.sourceforge.net/wordpress/) in its initial intents.

## Build deps
* libsystemd >= 221 (systemd/sd-bus.h)
* libsystemd >= 221 (systemd/sd-bus.h, systemd/sd-login.h)
* libpopt (popt.h)
* gsl (gsl/gsl_multifit.h, gsl/gsl_statistics_double.h)
* libconfig (libconfig.h)
* gcc or clang

## Runtime deps:
* shared objects from build libraries
* [clightd](https://github.com/FedeDP/Clightd) >= 1.4
* [clightd](https://github.com/FedeDP/Clightd) >= 2.0

## Optional runtime deps:
* Geoclue2 to automatically retrieve user location (no geoclue and no user position specified will disable GAMMA support)
* Upower to honor timeouts between captures, to use different ambient brightness -> screen backlight matching coefficients, to change dimmer timeout and to change dpms timeouts depending on ac state.
* NetworkManager to let modules react to connection state changes. Only used by weather module for now.

## How to run it
Clight tries to be a 0-conf software; therefore, it installs a desktop file in /etc/xdg/autostart. This way, no matter what's your DE is, if it is xdg-compliant, it will automatically start clight. User has to do nothing but reboot after installing clight.
Expand All @@ -37,9 +36,9 @@ Finally, a desktop file to take a fast screen backlight recalibration ("clight -
By default Clight enables all its functions, fallbacking to disable them when they are not supported.
This means that these features are all enabled with default values:
* BRIGHTNESS: to make webcam captures and change screen backlight level to match ambient brightness
* GAMMA: to change screen temperature based on current time of day
* DIMMER: to dim screen after a certain idle time
* DPMS: to switch off screen after a certain idle time
* GAMMA: to change screen temperature based on current time of day (X-only feature)
* DIMMER: to dim screen after a certain idle time (X-only feature)
* DPMS: to switch off screen after a certain idle time (X-only feature)

**All these features but BRIGHTNESS can be turned off through cmdline and config file options.**

Expand Down Expand Up @@ -70,13 +69,11 @@ Location received will be then cached when clight exit. This way, if no internet
* UPower support, to set longer timeouts between captures while on battery, in order to save some energy.
Moreover, you can set a percentage of maximum settable brightness while on battery.
* You can specify curve points to be used to match ambient brightness to screen backlight from config file. For more info, see [Polynomial fit](https://github.com/FedeDP/Clight#polynomial-fit) section below.
* It will check if current backlight interface is enabled before changing backlight/dimming screen. It will avoid doing any frame capture at all if interface is disabled. It can happen when you use your laptop connected to an external monitor, with internal monitor switched off; thus changing backlight would be useless.
* DPMS support: it will set desired dpms timeouts for AC/batt states.
* Dpms and dimmer can be disabled while on AC, just set dimmer timeout/any dpms timeout for given AC state <= 0.
* Clight supports org.freedesktop.PowerManagement.Inhibit interface. Thus, when for example watching a youtube video from chromium, dimmer module won't dim your screen.
* Weather support: if an OpenWeatherMap apikey is provided through conf file or cmdline option, clight will adjust timeouts between captures given current cloudiness. Function that adjusts timeouts is quite simple: if cloudiness > 50 -> current timeout is shorten of (cloudiness - 50) / 100. This way, at 51 timeout will be 99% of original timeout. At 100 it will be 50%.
* NetworkManager StateChanged signal is supported, thus modules can react to connection state changes (only used by weather for now).
* Gracefully auto-disabling unsupported module (eg: GAMMA on non-X environments)
* Supports both internal laptop monitor and external monitors (thus desktop PCs too), thanks to [ddcutil](https://github.com/rockowitz/ddcutil).

### Valgrind is run with:

Expand All @@ -90,7 +87,7 @@ For cmdline options, check clight [-?|--help] [--usage].
**Please note that cmdline "--device" and "--backlight" switches require only last part of syspath** (eg: "video0" or "intel_backlight").

## Config file
A global config file is shipped with clight. It is installed in /etc/default/clight.conf and it is all commented.
A global config file is shipped with clight. It is installed in /etc/default/clight.conf and it is full of comments.
You can customize it or you can copy it in your $XDG_CONFIG_HOME folder (fallbacks to $HOME/.config/) and customize it there.
Both files are checked when clight starts, in this order: global -> user-local -> cmdline opts.

Expand All @@ -102,7 +99,7 @@ By customizing these values, you can adapt screen backlight curve to meet your n
Clight supports different curves on different ac states. In fact, by default on BATT points are different from above. See config file for more info.

## Gamma support info
*Gamma support is only available on X. Sadly on wayland there is still no standard way to achieve gamma correction. Let's way with fingers crossed.*
*Gamma support is only available on X. Sadly on wayland there is still no standard way to achieve gamma correction. Let's wait with fingers crossed.*
Consequently, on not X environments, gamma correction tool gets autodisabled.

As [clightd](https://github.com/FedeDP/Clightd#devel-info) getgamma function properly supports only 50-steps temperature values (ie if you use "setgamma 6000" and then getgamma, it will return 6000. If you use setgamma 4578, getgamma won't return exactly it; it will return 4566 or something similar.), do not set in your conf not-50-multiple temperatures.
Expand All @@ -111,7 +108,7 @@ If you run clight from wayland or from a tty, gamma support will be automaticall

## Other info
You can only run one clight instance per-user: if a clight instance is running, you cannot start another full clight instance.
Obviously you can still invoke "clight -c" from a terminal/shortcut to make a fast capture/screen brightness calibration.
Obviously you can still invoke "clight -c" from a terminal/shortcut/clight desktop file to make a fast capture/screen brightness calibration.
This is achieved through a clight.lock file placed in current user home.

Every functionality in clight is achieved through a "module". An inter-modules dependencies system has been created ad-hoc to ease development of such modules.
Expand Down
30 changes: 27 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
## 1.5
## 2.0

- [x] location_callback for weather module
- [x] each location_callback should check if distance is > N meters/kilometers to avoid useless calls.
- [x] Location module should expose a get_distance(old pos, new pos) function
- [x] add a data struct location { double lat, double lon } and use that everywhere
- [x] Check latitude/longitude values after loading them from opts/config
- [x] update to new 1.6 clightd interface
- [x] drop isinterface_enabled calls
- [x] fix dimmer with set_backlight_level with percentage
- [x] EXIT_FAILURE even when state.quit > ERR_QUIT (should never happen)
- [x] location callback should check if new position is at least LOC_DISTANCE_THRS distant from last pos
- [x] make clight work on desktop pc with webcam too (obviously through ddcutil)? -> add a setbrightnesspct_all() method to clightd that will set a pct for internal + external monitors
- [x] add a math_utils.c source file with all math functions (eg get_distance, clamp...)?
- [x] make backlight level change smoothly (#20)
- [x] drop weather and networkmanager support
- [x] update to new clightd interface -> brightness, brighntess smooth, dimmer, dimmer smooth
- [x] better check for xauthority / xsession
- [x] fix wayland? (check!)
- [x] add a Clightd module that only depends on Bus module and will check if clightd is available (and properly checks its version) (FIX!!)
- [x] intercept SIGSEGV and remove lock file
- [x] make smooth_steps and smooth_timeouts for backlight/dimmer and gamma configurable!
- [x] update readme
- [x] make brightness module optional too
- [x] automatically disable brightness module on PCs with no webcam
- [x] force brightness module enabled in single capture mode
- [x] don't run bus callbacks on disabled modules
- [ ] NEW RELEASE!

## Later/Ideas
- [ ] subscribe to "interfaceEnabledChanged" signal from clightd (as soon as it is implemented in clightd) and do a capture as soon as interface became enabled (May be disable both dimmer and brightness while interface is disabled)
## 2.1
- [ ] add a kde/gnome module to set a dark theme on sunrise/sunset (gnome: http://www.fandigital.com/2012/06/change-theme-command-line-gnome.html)
- [ ] properly add a onBusCallFail callback to bus_args, with a default one like the current check_err function
- [ ] add a small bus interface to query clight status/set new timeouts/make new fast capture ("-c" switch to follow this new api)
4 changes: 1 addition & 3 deletions inc/brightness.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
void set_brightness_self(void);
void get_current_brightness(void);
void set_backlight_level(int level);
int is_interface_enabled(void);
void set_backlight_level(const double pct, const int is_smooth, const double step, const int timeout);
1 change: 1 addition & 0 deletions inc/clightd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void set_clightd_self(void);
39 changes: 15 additions & 24 deletions inc/commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#define LOC_DISTANCE_THRS 50000 // threshold for location distances before triggering location changed events (50km)

/* List of modules indexes */
enum modules { BRIGHTNESS, LOCATION, UPOWER, GAMMA, GAMMA_SMOOTH, SIGNAL, BUS, DIMMER, DIMMER_SMOOTH, DPMS, XORG, INHIBIT, USERBUS, WEATHER, NETWORK, MODULES_NUM };
enum modules { BRIGHTNESS, LOCATION, UPOWER, GAMMA, SIGNAL, BUS, DIMMER, DPMS, XORG, INHIBIT, USERBUS, CLIGHTD, MODULES_NUM };

/*
* List of states clight can be through:
Expand Down Expand Up @@ -60,17 +60,13 @@ enum bus_type { SYSTEM, USER };
/* Quit values */
enum quit_values { NORM_QUIT = 1, ERR_QUIT };

enum NMState { NM_STATE_UNKNOWN = 0, NM_STATE_ASLEEP = 10, NM_STATE_DISCONNECTED = 20,
NM_STATE_DISCONNECTING = 30, NM_STATE_CONNECTING = 40, NM_STATE_CONNECTED_LOCAL = 50,
NM_STATE_CONNECTED_SITE = 60, NM_STATE_CONNECTED_GLOBAL = 70 };

/* Struct that holds data about a geographic location */
struct location {
double lat;
double lon;
};

/* Struct that holds global config as passed through cmdline args */
/* Struct that holds global config as passed through cmdline args/config file reading */
struct config {
int num_captures; // number of frame captured for each screen brightness compute
int single_capture_mode; // do a capture and leave
Expand All @@ -83,20 +79,18 @@ struct config {
int event_duration; // duration of an event (by default 30mins, ie: it starts 30mins before an event and ends 30mins after)
double regression_points[SIZE_AC][SIZE_POINTS]; // points used for regression through libgsl
int dimmer_timeout[SIZE_AC]; // dimmer timeout
int dimmer_pct; // pct of max brightness to be used while dimming
double dimmer_pct; // pct of max brightness to be used while dimming
int dpms_timeouts[SIZE_AC][SIZE_DPMS]; // dpms timeouts
int verbose; // whether we're in verbose mode
char weather_apikey[32 + 1]; // apikey for openweathermap
int weather_timeout[SIZE_AC]; // timeouts for weather update
};

/*
* Storage struct for our needed variables.
*/
struct brightness {
int current;
int max;
int old;
int no_smooth_backlight;
int no_smooth_dimmer;
int no_smooth_gamma;
double backlight_trans_step;
int gamma_trans_step;
double dimmer_trans_step;
int backlight_trans_timeout;
int gamma_trans_timeout;
int dimmer_trans_timeout;
};

/* Global state of program */
Expand All @@ -109,14 +103,11 @@ struct state {
enum ac_states ac_state; // is laptop on battery?
int fast_recapture; // fast recapture after huge brightness drop?
double fit_parameters[SIZE_AC][DEGREE]; // best-fit parameters
const char *xauthority; // xauthority env variable, to be used in gamma calls
const char *display; // display env variable, to be used in gamma calls
struct brightness br; // struct that hold screen backlight info
char *xauthority; // xauthority env variable, to be used in gamma calls
char *display; // display env variable, to be used in gamma calls
double current_br_pct; // current backlight pct
int is_dimmed; // whether we are currently in dimmed state
int dimmed_br; // backlight level when dimmed
int pm_inhibited; // whether powermanagement is inhibited
int cloudiness; // weather cloudiness for user location
enum NMState nmstate; // NetworkManager own states
jmp_buf quit_buf; // quit jump called by longjmp
};

Expand Down
2 changes: 0 additions & 2 deletions inc/dimmer_smooth.h

This file was deleted.

3 changes: 0 additions & 3 deletions inc/gamma_smooth.h

This file was deleted.

10 changes: 10 additions & 0 deletions inc/math_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "log.h"

double get_distance(struct location loc1, struct location loc2);
double degToRad(double angleDeg);
double radToDeg(double angleRad);
double compute_average(double *intensity, int num);
void polynomialfit(enum ac_states s);
double clamp(double value, double max, double min);
int calculate_sunrise(const float lat, const float lng, time_t *tt, int tomorrow) ;
int calculate_sunset(const float lat, const float lng, time_t *tt, int tomorrow);
4 changes: 0 additions & 4 deletions inc/network.h

This file was deleted.

1 change: 0 additions & 1 deletion inc/weather.h

This file was deleted.

Loading

0 comments on commit 4f8efac

Please sign in to comment.