From a7ab7860a57cdd41944355a400f62581f8b19d03 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 16 Feb 2014 19:19:36 +0100 Subject: use randr MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- .gitignore | 2 +- Makefile | 10 +++++----- src/__main__.py | 4 +++- src/_blueshift_randr.pyx | 40 ---------------------------------------- src/blueshift_randr.pyx | 40 ++++++++++++++++++++++++++++++++++++++++ src/monitor.py | 23 +++++++++++++++++++++++ 6 files changed, 72 insertions(+), 47 deletions(-) delete mode 100644 src/_blueshift_randr.pyx create mode 100644 src/blueshift_randr.pyx diff --git a/.gitignore b/.gitignore index 87b68f2..91cb8fb 100644 --- a/.gitignore +++ b/.gitignore @@ -10,5 +10,5 @@ obj/ *.out *.gch __pycache__/ -/src/_blueshift_randr.c +/src/blueshift_randr.c diff --git a/Makefile b/Makefile index df938e1..e964aae 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ FLAGS = $$($(PKGCONFIG) --cflags --libs $(LIBS)) -std=$(STD) $(WARN) $(OPTIMISE) all: bin/blueshift_randr.so -bin/blueshift_randr.so: obj/_blueshift_randr.o obj/blueshift_randr_c.o +bin/blueshift_randr.so: obj/blueshift_randr.o obj/blueshift_randr_c.o @mkdir -p bin $(CC) $(FLAGS) -shared -o $@ $^ @@ -23,13 +23,13 @@ obj/%.o: obj/%.c @mkdir -p obj $(CC) $(FLAGS) -c -o $@ $< -obj/_blueshift_randr.c: src/_blueshift_randr.pyx +obj/blueshift_randr.c: src/blueshift_randr.pyx @mkdir -p obj - if ! cython -3 -v $<; then src/_blueshift_randr.c ; false ; fi - mv src/_blueshift_randr.c $@ + if ! cython -3 -v $<; then src/blueshift_randr.c ; false ; fi + mv src/blueshift_randr.c $@ .PHONY: all clean: - -rm -r bin obj src/_blueshift_randr.c + -rm -r bin obj src/blueshift_randr.c diff --git a/src/__main__.py b/src/__main__.py index 400c675..f8db7a2 100755 --- a/src/__main__.py +++ b/src/__main__.py @@ -150,7 +150,6 @@ if config_file is not None: code = code.decode('utf8', 'error') + '\n' code = compile(code, file, 'exec') exec(code, globals) - break else: print('No configuration file found') sys.exit(1) @@ -168,6 +167,7 @@ if periodically is not None: running = False start_over() monitor_controller() + close_c_bindings() sys.exit(0) running = False signal.signal(signal.SIGTERM, signal_SIGTERM) @@ -205,3 +205,5 @@ if periodically is not None: start_over() monitor_controller() +close_c_bindings() + diff --git a/src/_blueshift_randr.pyx b/src/_blueshift_randr.pyx deleted file mode 100644 index e022714..0000000 --- a/src/_blueshift_randr.pyx +++ /dev/null @@ -1,40 +0,0 @@ -# -*- python -*- -cimport cython -from libc.stdlib cimport malloc, free - - -cdef extern int blueshift_randr_open(int use_screen) -cdef extern int blueshift_randr_apply(unsigned long long int use_crtcs, - unsigned short int* r_curve, - unsigned short int* g_curve, - unsigned short int* b_curve) -cdef extern void blueshift_randr_close() - - -def randr_open(int use_screen): - return blueshift_randr_open(use_screen) - - -def randr_apply(unsigned long long use_crtcs, r_curve, g_curve, b_curve): - cdef unsigned short int* r - cdef unsigned short int* g - cdef unsigned short int* b - r = malloc(256 * 2) - g = malloc(256 * 2) - b = malloc(256 * 2) - if (r is NULL) or (g is NULL) or (b is NULL): - raise MemoryError() - for i in range(256): - r[i] = r_curve[i] - g[i] = g_curve[i] - b[i] = b_curve[i] - rc = blueshift_randr_apply(use_crtcs, r, g, b) - free(r) - free(g) - free(b) - return rc - - -def randr_close(): - blueshift_randr_close() - diff --git a/src/blueshift_randr.pyx b/src/blueshift_randr.pyx new file mode 100644 index 0000000..e022714 --- /dev/null +++ b/src/blueshift_randr.pyx @@ -0,0 +1,40 @@ +# -*- python -*- +cimport cython +from libc.stdlib cimport malloc, free + + +cdef extern int blueshift_randr_open(int use_screen) +cdef extern int blueshift_randr_apply(unsigned long long int use_crtcs, + unsigned short int* r_curve, + unsigned short int* g_curve, + unsigned short int* b_curve) +cdef extern void blueshift_randr_close() + + +def randr_open(int use_screen): + return blueshift_randr_open(use_screen) + + +def randr_apply(unsigned long long use_crtcs, r_curve, g_curve, b_curve): + cdef unsigned short int* r + cdef unsigned short int* g + cdef unsigned short int* b + r = malloc(256 * 2) + g = malloc(256 * 2) + b = malloc(256 * 2) + if (r is NULL) or (g is NULL) or (b is NULL): + raise MemoryError() + for i in range(256): + r[i] = r_curve[i] + g[i] = g_curve[i] + b[i] = b_curve[i] + rc = blueshift_randr_apply(use_crtcs, r, g, b) + free(r) + free(g) + free(b) + return rc + + +def randr_close(): + blueshift_randr_close() + diff --git a/src/monitor.py b/src/monitor.py index 37f6b00..b8f5cff 100644 --- a/src/monitor.py +++ b/src/monitor.py @@ -15,6 +15,15 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import sys + +# /usr/lib +LIBDIR = 'bin' +sys.path.append(LIBDIR) + +from blueshift_randr import * +randr_opened = False + def translate_to_integers(): ''' @@ -31,17 +40,31 @@ def translate_to_integers(): return (R_curve, G_curve, B_curve) +def close_c_bindings(): + global randr_opened + if randr_opened: + randr_close() + + def randr(*crtcs): ''' Applies colour curves using the X11 extension randr @param *crtcs The CRT controllers to use, all are used if none are specified ''' + global randr_opened crtcs = sum([1 << i for i in list(crtcs)]) if crtcs == 0: crtcs = -1; (R_curve, G_curve, B_curve) = translate_to_integers() + if not randr_opened: + if randr_open(0) == 0: ## TODO support specifying screen + randr_opened = True + else: + sys.exit(1) + if not randr_apply(crtcs, R_curve, G_curve, B_curve) == 0: + sys.exit(1) def print_curves(*crtcs): -- cgit v1.2.3-70-g09d2