Merge pull request #140 from jonls/osx-support
OSX support. Adds gamma adjustment method `quartz` and location provider `corelocation`.
This commit is contained in:
+100
-1
@@ -5,14 +5,31 @@ AC_PREREQ([2.69])
|
||||
AC_INIT([redshift], [1.9.1], [https://github.com/jonls/redshift/issues])
|
||||
AC_CONFIG_SRCDIR([src/redshift.c])
|
||||
AC_CONFIG_HEADERS([config.h])
|
||||
AC_CONFIG_MACRO_DIR([m4])
|
||||
AM_INIT_AUTOMAKE([foreign subdir-objects dist-xz])
|
||||
|
||||
m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])
|
||||
|
||||
# Checks for programs.
|
||||
AC_PROG_CC_C99
|
||||
AC_PROG_LIBTOOL
|
||||
AC_PROG_OBJC # For OSX support modules
|
||||
AC_LANG([C])
|
||||
|
||||
AC_PROG_INTLTOOL([0.50])
|
||||
|
||||
# Test whether Objective C compiler works
|
||||
AC_MSG_CHECKING([whether Objective C compiler works])
|
||||
AC_LANG_PUSH([Objective C])
|
||||
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([])], [
|
||||
AC_MSG_RESULT([yes])
|
||||
have_objc_compiler=yes
|
||||
], [
|
||||
AC_MSG_RESULT([no])
|
||||
have_objc_compiler=no
|
||||
])
|
||||
AC_LANG_POP([Objective C])
|
||||
|
||||
# Checks for libraries.
|
||||
AM_GNU_GETTEXT_VERSION([0.17])
|
||||
AM_GNU_GETTEXT([external])
|
||||
@@ -34,6 +51,30 @@ PKG_CHECK_MODULES([GLIB], [glib-2.0 gobject-2.0], [have_glib=yes], [have_glib=no
|
||||
PKG_CHECK_MODULES([GEOCLUE], [geoclue], [have_geoclue=yes], [have_geoclue=no])
|
||||
PKG_CHECK_MODULES([GEOCLUE2], [glib-2.0 gio-2.0 >= 2.26], [have_geoclue2=yes], [have_geoclue2=no])
|
||||
|
||||
# OSX headers
|
||||
AC_CHECK_HEADER([ApplicationServices/ApplicationServices.h], [have_appserv_h=yes], [have_appserv_h=no])
|
||||
|
||||
# CoreLocation.h is an Objective C header. Only test if
|
||||
# Objective C compiler works. AC_CHECK_HEADER does not
|
||||
# appear to work if the Ojective C compiler is not
|
||||
# available so we need a custom test.
|
||||
AC_MSG_CHECKING([whether CoreLocation/CoreLocation.h is usable])
|
||||
AS_IF([test "x$have_objc_compiler" = xyes], [
|
||||
AC_LANG_PUSH([Objective C])
|
||||
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#import <CoreLocation/CoreLocation.h>]],[[]])], [
|
||||
AC_MSG_RESULT([yes])
|
||||
have_corelocation_h=yes
|
||||
], [
|
||||
AC_MSG_RESULT([no])
|
||||
have_corelocation_h=no
|
||||
])
|
||||
AC_LANG_POP([Objective C])
|
||||
], [
|
||||
AC_MSG_RESULT([no Objective C compiler available])
|
||||
have_corelocation_h=no
|
||||
])
|
||||
|
||||
# Windows header
|
||||
AC_CHECK_HEADER([windows.h], [have_windows_h=yes], [have_windows_h=no])
|
||||
|
||||
# Check for Python
|
||||
@@ -111,6 +152,34 @@ AS_IF([test "x$enable_vidmode" != xno], [
|
||||
])
|
||||
AM_CONDITIONAL([ENABLE_VIDMODE], [test "x$enable_vidmode" = xyes])
|
||||
|
||||
# Check Quartz (OSX) method
|
||||
AC_MSG_CHECKING([whether to enable Quartz method])
|
||||
AC_ARG_ENABLE([quartz], [AC_HELP_STRING([--enable-quartz],
|
||||
[enable Quartz (OSX) method])],
|
||||
[enable_quartz=$enableval],[enable_quartz=maybe])
|
||||
AS_IF([test "x$enable_quartz" != xno], [
|
||||
AS_IF([test $have_appserv_h = yes], [
|
||||
QUARTZ_CFLAGS=""
|
||||
QUARTZ_LIBS="-framework ApplicationServices"
|
||||
AC_DEFINE([ENABLE_QUARTZ], 1,
|
||||
[Define to 1 to enable Quartz method])
|
||||
AC_MSG_RESULT([yes])
|
||||
enable_quartz=yes
|
||||
], [
|
||||
AC_MSG_RESULT([missing dependencies])
|
||||
AS_IF([test "x$enable_quartz" = xyes], [
|
||||
AC_MSG_ERROR([missing Quartz headers])
|
||||
])
|
||||
enable_quartz=no
|
||||
])
|
||||
], [
|
||||
AC_MSG_RESULT([no])
|
||||
enable_quartz=no
|
||||
])
|
||||
AM_CONDITIONAL([ENABLE_QUARTZ], [test "x$enable_quartz" = xyes])
|
||||
AC_SUBST([QUARTZ_CFLAGS])
|
||||
AC_SUBST([QUARTZ_LIBS])
|
||||
|
||||
# Check Windows GDI method
|
||||
AC_MSG_CHECKING([whether to enable WinGDI method])
|
||||
AC_ARG_ENABLE([wingdi], [AC_HELP_STRING([--enable-wingdi],
|
||||
@@ -184,6 +253,34 @@ AS_IF([test "x$enable_geoclue2" != xno], [
|
||||
])
|
||||
AM_CONDITIONAL([ENABLE_GEOCLUE2], [test "x$enable_geoclue2" = xyes])
|
||||
|
||||
# Check CoreLocation (OSX) provider
|
||||
AC_MSG_CHECKING([whether to enable CoreLocation method])
|
||||
AC_ARG_ENABLE([corelocation], [AC_HELP_STRING([--enable-corelocation],
|
||||
[enable CoreLocation (OSX) provider])],
|
||||
[enable_corelocation=$enableval],[enable_corelocation=maybe])
|
||||
AS_IF([test "x$enable_corelocation" != xno], [
|
||||
AS_IF([test "x$have_corelocation_h" = xyes], [
|
||||
CORELOCATION_CFLAGS=""
|
||||
CORELOCATION_LIBS="-framework Foundation -framework CoreLocation"
|
||||
AC_DEFINE([ENABLE_CORELOCATION], 1,
|
||||
[Define to 1 to enable CoreLocation provider])
|
||||
AC_MSG_RESULT([yes])
|
||||
enable_corelocation=yes
|
||||
], [
|
||||
AC_MSG_RESULT([missing dependencies])
|
||||
AS_IF([test "x$enable_corelocation" = xyes], [
|
||||
AC_MSG_ERROR([missing CoreLocation headers])
|
||||
])
|
||||
enable_corelocation=no
|
||||
])
|
||||
], [
|
||||
AC_MSG_RESULT([no])
|
||||
enable_corelocation=no
|
||||
])
|
||||
AM_CONDITIONAL([ENABLE_CORELOCATION], [test "x$enable_corelocation" = xyes])
|
||||
AC_SUBST([CORELOCATION_CFLAGS])
|
||||
AC_SUBST([CORELOCATION_LIBS])
|
||||
|
||||
|
||||
# Check for GUI status icon
|
||||
AC_MSG_CHECKING([whether to enable GUI status icon])
|
||||
@@ -270,11 +367,13 @@ echo "
|
||||
DRM: ${enable_drm}
|
||||
RANDR: ${enable_randr}
|
||||
VidMode: ${enable_vidmode}
|
||||
WinGDI: ${enable_wingdi}
|
||||
Quartz (OSX): ${enable_quartz}
|
||||
WinGDI (Windows): ${enable_wingdi}
|
||||
|
||||
Location providers:
|
||||
Geoclue: ${enable_geoclue}
|
||||
Geoclue2: ${enable_geoclue2}
|
||||
CoreLocation (OSX) ${enable_corelocation}
|
||||
|
||||
GUI: ${enable_gui}
|
||||
Ubuntu icons: ${enable_ubuntu}
|
||||
|
||||
@@ -10,11 +10,13 @@ src/config-ini.c
|
||||
src/gamma-drm.c
|
||||
src/gamma-randr.c
|
||||
src/gamma-vidmode.c
|
||||
src/gamma-quartz.c
|
||||
src/gamma-w32gdi.c
|
||||
src/gamma-dummy.c
|
||||
|
||||
src/location-geoclue.c
|
||||
src/location-geoclue2.c
|
||||
src/location-corelocation.m
|
||||
src/location-manual.c
|
||||
|
||||
src/redshift-gtk/statusicon.py
|
||||
|
||||
@@ -21,6 +21,7 @@ EXTRA_redshift_SOURCES = \
|
||||
gamma-drm.c gamma-drm.h \
|
||||
gamma-randr.c gamma-randr.h \
|
||||
gamma-vidmode.c gamma-vidmode.h \
|
||||
gamma-quartz.c gamma-quartz.h \
|
||||
gamma-w32gdi.c gamma-w32gdi.h \
|
||||
location-geoclue.c location-geoclue.h
|
||||
|
||||
@@ -51,6 +52,13 @@ redshift_LDADD += \
|
||||
$(XF86VM_LIBS) $(XF86VM_CFLAGS)
|
||||
endif
|
||||
|
||||
if ENABLE_QUARTZ
|
||||
redshift_SOURCES += gamma-quartz.c gamma-quartz.h
|
||||
AM_CFLAGS += $(QUARTZ_CFLAGS)
|
||||
redshift_LDADD += \
|
||||
$(QUARTZ_LIBS) $(QUARTZ_CFLAGS)
|
||||
endif
|
||||
|
||||
if ENABLE_WINGDI
|
||||
redshift_SOURCES += gamma-w32gdi.c gamma-w32gdi.h
|
||||
redshift_LDADD += -lgdi32
|
||||
@@ -74,3 +82,18 @@ AM_CFLAGS += \
|
||||
redshift_LDADD += \
|
||||
$(GEOCLUE2_LIBS) $(GEOCLUE2_CFLAGS)
|
||||
endif
|
||||
|
||||
# Build CoreLocation module as a separate convenience
|
||||
# library since it is using a separate compiler
|
||||
# (Objective C).
|
||||
|
||||
if ENABLE_CORELOCATION
|
||||
noinst_LTLIBRARIES = liblocation-corelocation.la
|
||||
liblocation_corelocation_la_SOURCES = \
|
||||
location-corelocation.m location-corelocation.h
|
||||
liblocation_corelocation_la_OBJCFLAGS = \
|
||||
$(CORELOCATION_CFLAGS)
|
||||
liblocation_corelocation_la_LIBADD = \
|
||||
$(CORELOCATION_CFLAGS) $(CORELOCATION_LIBS)
|
||||
redshift_LDADD += liblocation-corelocation.la
|
||||
endif
|
||||
|
||||
+25
-3
@@ -14,7 +14,7 @@
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Redshift. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright (c) 2013 Jon Lund Steffensen <jonlst@gmail.com>
|
||||
Copyright (c) 2013-2014 Jon Lund Steffensen <jonlst@gmail.com>
|
||||
Copyright (c) 2013 Ingo Thies <ithies@astro.uni-bonn.de>
|
||||
*/
|
||||
|
||||
@@ -280,6 +280,9 @@ interpolate_color(float a, const float *c1, const float *c2, float *c)
|
||||
c[2] = (1.0-a)*c1[2] + a*c2[2];
|
||||
}
|
||||
|
||||
/* Helper macro used in the fill functions */
|
||||
#define F(Y, C) pow((Y) * brightness * white_point[C], 1.0/gamma[C])
|
||||
|
||||
void
|
||||
colorramp_fill(uint16_t *gamma_r, uint16_t *gamma_g, uint16_t *gamma_b,
|
||||
int size, int temp, float brightness, const float gamma[3])
|
||||
@@ -291,11 +294,30 @@ colorramp_fill(uint16_t *gamma_r, uint16_t *gamma_g, uint16_t *gamma_b,
|
||||
interpolate_color(alpha, &blackbody_color[temp_index],
|
||||
&blackbody_color[temp_index+3], white_point);
|
||||
|
||||
#define F(Y, C) pow((Y) * brightness * white_point[C], 1.0/gamma[C])
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
gamma_r[i] = F((float)i/size, 0) * (UINT16_MAX+1);
|
||||
gamma_g[i] = F((float)i/size, 1) * (UINT16_MAX+1);
|
||||
gamma_b[i] = F((float)i/size, 2) * (UINT16_MAX+1);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
colorramp_fill_float(float *gamma_r, float *gamma_g, float *gamma_b,
|
||||
int size, int temp, float brightness,
|
||||
const float gamma[3])
|
||||
{
|
||||
/* Approximate white point */
|
||||
float white_point[3];
|
||||
float alpha = (temp % 100) / 100.0;
|
||||
int temp_index = ((temp - 1000) / 100)*3;
|
||||
interpolate_color(alpha, &blackbody_color[temp_index],
|
||||
&blackbody_color[temp_index+3], white_point);
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
gamma_r[i] = F((float)i/size, 0);
|
||||
gamma_g[i] = F((float)i/size, 1);
|
||||
gamma_b[i] = F((float)i/size, 2);
|
||||
}
|
||||
}
|
||||
|
||||
#undef F
|
||||
|
||||
+4
-1
@@ -14,7 +14,7 @@
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Redshift. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright (c) 2010 Jon Lund Steffensen <jonlst@gmail.com>
|
||||
Copyright (c) 2010-2014 Jon Lund Steffensen <jonlst@gmail.com>
|
||||
*/
|
||||
|
||||
#ifndef REDSHIFT_COLORRAMP_H
|
||||
@@ -24,5 +24,8 @@
|
||||
|
||||
void colorramp_fill(uint16_t *gamma_r, uint16_t *gamma_g, uint16_t *gamma_b,
|
||||
int size, int temp, float brightness, const float gamma[3]);
|
||||
void colorramp_fill_float(float *gamma_r, float *gamma_g, float *gamma_b,
|
||||
int size, int temp, float brightness,
|
||||
const float gamma[3]);
|
||||
|
||||
#endif /* ! REDSHIFT_COLORRAMP_H */
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
/* gamma-quartz.c -- Quartz (OSX) gamma adjustment
|
||||
This file is part of Redshift.
|
||||
|
||||
Redshift 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 Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Redshift 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 Redshift. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright (c) 2014 Jon Lund Steffensen <jonlst@gmail.com>
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <ApplicationServices/ApplicationServices.h>
|
||||
|
||||
#ifdef ENABLE_NLS
|
||||
# include <libintl.h>
|
||||
# define _(s) gettext(s)
|
||||
#else
|
||||
# define _(s) s
|
||||
#endif
|
||||
|
||||
#include "gamma-quartz.h"
|
||||
#include "colorramp.h"
|
||||
|
||||
|
||||
int
|
||||
quartz_init(quartz_state_t *state)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
quartz_start(quartz_state_t *state)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
quartz_restore(quartz_state_t *state)
|
||||
{
|
||||
CGDisplayRestoreColorSyncSettings();
|
||||
}
|
||||
|
||||
void
|
||||
quartz_free(quartz_state_t *state)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
quartz_print_help(FILE *f)
|
||||
{
|
||||
fputs(_("Adjust gamma ramps on OSX using Quartz.\n"), f);
|
||||
fputs("\n", f);
|
||||
}
|
||||
|
||||
int
|
||||
quartz_set_option(quartz_state_t *state, const char *key, const char *value)
|
||||
{
|
||||
fprintf(stderr, _("Unknown method parameter: `%s'.\n"), key);
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void
|
||||
quartz_set_temperature_for_display(CGDirectDisplayID display, int temp,
|
||||
float brightness, const float gamma[3])
|
||||
{
|
||||
uint32_t ramp_size = CGDisplayGammaTableCapacity(display);
|
||||
if (ramp_size == 0) {
|
||||
fprintf(stderr, _("Gamma ramp size too small: %i\n"),
|
||||
ramp_size);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Create new gamma ramps */
|
||||
float *gamma_ramps = malloc(3*ramp_size*sizeof(float));
|
||||
if (gamma_ramps == NULL) {
|
||||
perror("malloc");
|
||||
return;
|
||||
}
|
||||
|
||||
float *gamma_r = &gamma_ramps[0*ramp_size];
|
||||
float *gamma_g = &gamma_ramps[1*ramp_size];
|
||||
float *gamma_b = &gamma_ramps[2*ramp_size];
|
||||
|
||||
colorramp_fill_float(gamma_r, gamma_g, gamma_b, ramp_size,
|
||||
temp, brightness, gamma);
|
||||
|
||||
CGError error =
|
||||
CGSetDisplayTransferByTable(display, ramp_size,
|
||||
gamma_r, gamma_g, gamma_b);
|
||||
if (error != kCGErrorSuccess) {
|
||||
free(gamma_ramps);
|
||||
return;
|
||||
}
|
||||
|
||||
free(gamma_ramps);
|
||||
}
|
||||
|
||||
int
|
||||
quartz_set_temperature(quartz_state_t *state, int temp, float brightness,
|
||||
const float gamma[3])
|
||||
{
|
||||
int r;
|
||||
CGError error;
|
||||
uint32_t display_count;
|
||||
|
||||
error = CGGetOnlineDisplayList(0, NULL, &display_count);
|
||||
if (error != kCGErrorSuccess) return -1;
|
||||
|
||||
CGDirectDisplayID* displays =
|
||||
malloc(sizeof(CGDirectDisplayID)*display_count);
|
||||
if (displays == NULL) {
|
||||
perror("malloc");
|
||||
return -1;
|
||||
}
|
||||
|
||||
error = CGGetOnlineDisplayList(display_count, displays,
|
||||
&display_count);
|
||||
if (error != kCGErrorSuccess) {
|
||||
free(displays);
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < display_count; i++) {
|
||||
quartz_set_temperature_for_display(displays[i], temp,
|
||||
brightness, gamma);
|
||||
}
|
||||
|
||||
free(displays);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/* gamma-quartz.h -- Quartz (OSX) gamma adjustment header
|
||||
This file is part of Redshift.
|
||||
|
||||
Redshift 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 Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Redshift 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 Redshift. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright (c) 2014 Jon Lund Steffensen <jonlst@gmail.com>
|
||||
*/
|
||||
|
||||
#ifndef REDSHIFT_GAMMA_QUARTZ_H
|
||||
#define REDSHIFT_GAMMA_QUARTZ_H
|
||||
|
||||
#include "redshift.h"
|
||||
|
||||
|
||||
typedef struct {
|
||||
int dummy;
|
||||
} quartz_state_t;
|
||||
|
||||
|
||||
int quartz_init(quartz_state_t *state);
|
||||
int quartz_start(quartz_state_t *state);
|
||||
void quartz_free(quartz_state_t *state);
|
||||
|
||||
void quartz_print_help(FILE *f);
|
||||
int quartz_set_option(quartz_state_t *state, const char *key,
|
||||
const char *value);
|
||||
|
||||
void quartz_restore(quartz_state_t *state);
|
||||
int quartz_set_temperature(quartz_state_t *state, int temp, float brightness,
|
||||
const float gamma[3]);
|
||||
|
||||
|
||||
#endif /* ! REDSHIFT_GAMMA_QUARTZ_H */
|
||||
@@ -0,0 +1,38 @@
|
||||
/* location-corelocation.h -- CoreLocation (OSX) location provider header
|
||||
This file is part of Redshift.
|
||||
|
||||
Redshift 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 Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Redshift 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 Redshift. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright (c) 2014 Jon Lund Steffense <jonlst@gmail.com>
|
||||
*/
|
||||
|
||||
#ifndef REDSHIFT_LOCATION_CORELOCATION_H
|
||||
#define REDSHIFT_LOCATION_CORELOCATION_H
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
int location_corelocation_init(void *state);
|
||||
int location_corelocation_start(void *state);
|
||||
void location_corelocation_free(void *state);
|
||||
|
||||
void location_corelocation_print_help(FILE *f);
|
||||
int location_corelocation_set_option(void *state,
|
||||
const char *key, const char *value);
|
||||
|
||||
int location_corelocation_get_location(void *state,
|
||||
float *lat, float *lon);
|
||||
|
||||
|
||||
#endif /* ! REDSHIFT_LOCATION_CORELOCATION_H */
|
||||
@@ -0,0 +1,163 @@
|
||||
/* location-corelocation.m -- CoreLocation (OSX) location provider source
|
||||
This file is part of Redshift.
|
||||
|
||||
Redshift 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 Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Redshift 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 Redshift. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright (c) 2014 Jon Lund Steffensen <jonlst@gmail.com>
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <CoreLocation/CoreLocation.h>
|
||||
|
||||
#include "location-corelocation.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef ENABLE_NLS
|
||||
# include <libintl.h>
|
||||
# define _(s) gettext(s)
|
||||
#else
|
||||
# define _(s) s
|
||||
#endif
|
||||
|
||||
|
||||
@interface Delegate : NSObject <CLLocationManagerDelegate>
|
||||
@property (strong, nonatomic) CLLocationManager *locationManager;
|
||||
@property (nonatomic) BOOL success;
|
||||
@property (nonatomic) float latitude;
|
||||
@property (nonatomic) float longitude;
|
||||
@end
|
||||
|
||||
@implementation Delegate;
|
||||
|
||||
- (void)start
|
||||
{
|
||||
self.locationManager = [[CLLocationManager alloc] init];
|
||||
self.locationManager.delegate = self;
|
||||
|
||||
CLAuthorizationStatus authStatus =
|
||||
[CLLocationManager authorizationStatus];
|
||||
|
||||
if (authStatus != kCLAuthorizationStatusNotDetermined &&
|
||||
authStatus != kCLAuthorizationStatusAuthorized) {
|
||||
fputs(_("Not authorized to obtain location"
|
||||
"from CoreLocation.\n"), stderr);
|
||||
CFRunLoopStop(CFRunLoopGetCurrent());
|
||||
}
|
||||
|
||||
[self.locationManager startUpdatingLocation];
|
||||
}
|
||||
|
||||
- (void)stop
|
||||
{
|
||||
[self.locationManager stopUpdatingLocation];
|
||||
CFRunLoopStop(CFRunLoopGetCurrent());
|
||||
}
|
||||
|
||||
- (void)locationManager:(CLLocationManager *)manager
|
||||
didUpdateLocations:(NSArray *)locations
|
||||
{
|
||||
CLLocation *newLocation = [locations firstObject];
|
||||
self.latitude = newLocation.coordinate.latitude;
|
||||
self.longitude = newLocation.coordinate.longitude;
|
||||
self.success = YES;
|
||||
|
||||
[self stop];
|
||||
}
|
||||
|
||||
- (void)locationManager:(CLLocationManager *)manager
|
||||
didFailWithError:(NSError *)error
|
||||
{
|
||||
fprintf(stderr, _("Error obtaining location from CoreLocation: %s\n"),
|
||||
[[error localizedDescription] UTF8String]);
|
||||
[self stop];
|
||||
}
|
||||
|
||||
- (void)locationManager:(CLLocationManager *)manager
|
||||
didChangeAuthorizationStatus:(CLAuthorizationStatus)status
|
||||
{
|
||||
if (status == kCLAuthorizationStatusNotDetermined) {
|
||||
fputs(_("Waiting for authorization to obtain location...\n"),
|
||||
stderr);
|
||||
} else if (status != kCLAuthorizationStatusAuthorized) {
|
||||
fputs(_("Request for location was not authorized!\n"),
|
||||
stderr);
|
||||
[self stop];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
int
|
||||
location_corelocation_init(void *state)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
location_corelocation_start(void *state)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
location_corelocation_free(void *state)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
location_corelocation_print_help(FILE *f)
|
||||
{
|
||||
fputs(_("Use the location as discovered by the Corelocation provider.\n"), f);
|
||||
fputs("\n", f);
|
||||
|
||||
fputs(_("NOTE: currently redshift doesn't recheck CoreLocation once started,\n"
|
||||
"which means it has to be restarted to take notice after travel.\n"),
|
||||
f);
|
||||
fputs("\n", f);
|
||||
}
|
||||
|
||||
int
|
||||
location_corelocation_set_option(void *state,
|
||||
const char *key, const char *value)
|
||||
{
|
||||
fprintf(stderr, _("Unknown method parameter: `%s'.\n"), key);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
location_corelocation_get_location(void *state,
|
||||
float *lat, float *lon)
|
||||
{
|
||||
int result = -1;
|
||||
|
||||
@autoreleasepool {
|
||||
Delegate *delegate = [[Delegate alloc] init];
|
||||
[delegate performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
|
||||
CFRunLoopRun();
|
||||
|
||||
if (delegate.success) {
|
||||
*lat = delegate.latitude;
|
||||
*lon = delegate.longitude;
|
||||
result = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -65,6 +65,10 @@
|
||||
# include "gamma-vidmode.h"
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_QUARTZ
|
||||
# include "gamma-quartz.h"
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_WINGDI
|
||||
# include "gamma-w32gdi.h"
|
||||
#endif
|
||||
@@ -80,6 +84,10 @@
|
||||
# include "location-geoclue2.h"
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_CORELOCATION
|
||||
# include "location-corelocation.h"
|
||||
#endif
|
||||
|
||||
|
||||
/* Union of state data for gamma adjustment methods */
|
||||
typedef union {
|
||||
@@ -92,6 +100,9 @@ typedef union {
|
||||
#ifdef ENABLE_VIDMODE
|
||||
vidmode_state_t vidmode;
|
||||
#endif
|
||||
#ifdef ENABLE_QUARTZ
|
||||
quartz_state_t quartz;
|
||||
#endif
|
||||
#ifdef ENABLE_WINGDI
|
||||
w32gdi_state_t w32gdi;
|
||||
#endif
|
||||
@@ -136,6 +147,18 @@ static const gamma_method_t gamma_methods[] = {
|
||||
(gamma_method_set_temperature_func *)vidmode_set_temperature
|
||||
},
|
||||
#endif
|
||||
#ifdef ENABLE_QUARTZ
|
||||
{
|
||||
"quartz", 1,
|
||||
(gamma_method_init_func *)quartz_init,
|
||||
(gamma_method_start_func *)quartz_start,
|
||||
(gamma_method_free_func *)quartz_free,
|
||||
(gamma_method_print_help_func *)quartz_print_help,
|
||||
(gamma_method_set_option_func *)quartz_set_option,
|
||||
(gamma_method_restore_func *)quartz_restore,
|
||||
(gamma_method_set_temperature_func *)quartz_set_temperature
|
||||
},
|
||||
#endif
|
||||
#ifdef ENABLE_WINGDI
|
||||
{
|
||||
"wingdi", 1,
|
||||
@@ -200,6 +223,20 @@ static const location_provider_t location_providers[] = {
|
||||
(location_provider_get_location_func *)
|
||||
location_geoclue2_get_location
|
||||
},
|
||||
#endif
|
||||
#ifdef ENABLE_CORELOCATION
|
||||
{
|
||||
"corelocation",
|
||||
(location_provider_init_func *)location_corelocation_init,
|
||||
(location_provider_start_func *)location_corelocation_start,
|
||||
(location_provider_free_func *)location_corelocation_free,
|
||||
(location_provider_print_help_func *)
|
||||
location_corelocation_print_help,
|
||||
(location_provider_set_option_func *)
|
||||
location_corelocation_set_option,
|
||||
(location_provider_get_location_func *)
|
||||
location_corelocation_get_location
|
||||
},
|
||||
#endif
|
||||
{
|
||||
"manual",
|
||||
|
||||
Reference in New Issue
Block a user