From cf6a572d130b9b2e619d97b537075d5826dd5fe3 Mon Sep 17 00:00:00 2001
From: Mattias Andrée <maandree@kth.se>
Date: Wed, 17 Aug 2016 18:58:28 +0200
Subject: Fix errors and start on test
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Mattias Andrée <maandree@kth.se>
---
 src/libcoopgamma.py             | 41 +++++++++++++----------
 src/libcoopgamma_native.pyx.gpp | 36 +++++++++++++--------
 src/test                        | 72 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 118 insertions(+), 31 deletions(-)
 create mode 100755 src/test

(limited to 'src')

diff --git a/src/libcoopgamma.py b/src/libcoopgamma.py
index ee0ec63..3519881 100644
--- a/src/libcoopgamma.py
+++ b/src/libcoopgamma.py
@@ -232,18 +232,21 @@ class Gamut:
     @variable  red:GamutPoint    The red stimuli
     @variable  green:GamutPoint  The green stimuli
     @variable  blue:GamutPoint   The blue stimuli
+    @variable  white:GamutPoint  The default whitepoint
     '''
-    def __init__(self, red = None, green = None, blue = None):
+    def __init__(self, red = None, green = None, blue = None, white = None):
         '''
         Constructor
         
         @param  red:GamutPoint|(int, int)    The red stimuli
         @param  green:GamutPoint|(int, int)  The green stimuli
         @param  blue:GamutPoint|(int, int)   The blue stimuli
+        @param  white:GamutPoint|(int, int)  The default whitepoint
         '''
         self.red   = GamutPoint(*red)   if isinstance(red,   tuple) else red
         self.green = GamutPoint(*green) if isinstance(green, tuple) else green
         self.blue  = GamutPoint(*blue)  if isinstance(blue,  tuple) else blue
+        self.white = GamutPoint(*white) if isinstance(white, tuple) else white
     
     def clone(self, shallow = True):
         '''
@@ -252,8 +255,8 @@ class Gamut:
         @param  shallow:bool  Create a shallow copy?
         '''
         if shallow:
-            return Gamut(self.red, self.green, self.blue)
-        return Gamut(self.red.clone(), self.green.clone(), self.blue.clone())
+            return Gamut(self.red, self.green, self.blue, self.white)
+        return Gamut(self.red.clone(), self.green.clone(), self.blue.clone(), self.white.clone())
     
     def __repr__(self):
         '''
@@ -261,7 +264,7 @@ class Gamut:
         
         @return  :str  Parsable representation of the instance
         '''
-        params = (repr(self.red), repr(self.green), repr(self.blue))
+        params = (repr(self.red), repr(self.green), repr(self.blue), repr(self.white))
         return 'libcoopgamma.Gamut(%s)' % ', '.join(repr(p) for p in params)
 
 
@@ -354,7 +357,8 @@ class FilterQuery:
     @variable  crtc:str           The CRTC for which the the current filters shall returned
     @variable  coalesce:bool      Whether to coalesce all filters into one gamma ramp triplet
     '''
-    def __init__(self, high_priority, low_priority, crtc, coalesce):
+    def __init__(self, high_priority = 9223372036854775807,
+                 low_priority = -9223372036854775808, crtc = None, coalesce = False):
         '''
         Constructor
         
@@ -546,11 +550,12 @@ class ErrorReport:
         if isinstance(error, int):
             import os
             return OSError(error, os.strerror(error))
-        elif not error.custom and not error.server_side:
+        error = ErrorReport(*error)
+        if not error.custom and not error.server_side:
             import os
             return OSError(error.number, os.strerror(error))
         else:
-            return LibcoopgammaError(ErrorReport(*error))
+            return LibcoopgammaError(error)
 
 
 class LibcoopgammaError(Exception):
@@ -687,6 +692,7 @@ class Context:
         @param  fd:int      File descriptor for the socket
         @param  buf:bytes?  Buffer to unmarshal
         '''
+        self.address = None
         self.fd = fd
         if buf is None:
             (successful, value) = libcoopgamma_native.libcoopgamma_native_context_create()
@@ -706,7 +712,8 @@ class Context:
         '''
         Destructor
         '''
-        libcoopgamma_native.libcoopgamma_native_context_free(self.address)
+        if self.address is not None:
+            libcoopgamma_native.libcoopgamma_native_context_free(self.address)
     
     def __repr__(self):
         '''
@@ -873,7 +880,7 @@ class Context:
         async = AsyncContext()
         (successful, value) = libcoopgamma_native.libcoopgamma_native_get_gamma_info_send(
                                   crtc, self.address, async.address)
-        if successful:
+        if not successful:
             del async
             raise ErrorReport.create_error(value)
         return async
@@ -889,7 +896,7 @@ class Context:
         if isinstance(value, int):
             raise ErrorReport.create_error(value)
         (successful, value) = value
-        if successful:
+        if not successful:
             raise ErrorReport.create_error(value)
         return CRTCInfo(*value)
     
@@ -905,11 +912,11 @@ class Context:
         @param   crtc:str   The name of the CRT
         @return  :CRTCInfo  Information about the CRTC
         '''
-        value = libcoopgamma_native.libcoopgamma_native_get_gamma_info_sync(crtc, self.address, async.address)
+        value = libcoopgamma_native.libcoopgamma_native_get_gamma_info_sync(crtc, self.address)
         if isinstance(value, int):
             raise ErrorReport.create_error(value)
         (successful, value) = value
-        if successful:
+        if not successful:
             raise ErrorReport.create_error(value)
         return CRTCInfo(*value)
     
@@ -924,7 +931,7 @@ class Context:
         async = AsyncContext()
         (successful, value) = libcoopgamma_native.libcoopgamma_native_get_gamma_send(
                                   query, self.address, async.address)
-        if successful:
+        if not successful:
             del async
             raise ErrorReport.create_error(value)
         return async
@@ -940,7 +947,7 @@ class Context:
         if isinstance(value, int):
             raise ErrorReport.create_error(value)
         (successful, value) = value
-        if successful:
+        if not successful:
             raise ErrorReport.create_error(value)
         return FilterTable(*value)
     
@@ -956,11 +963,11 @@ class Context:
         @param   query:FilterQuery  The query to send
         @return  :FilterTable       Filter table
         '''
-        value = libcoopgamma_native.libcoopgamma_native_get_gamma_sync(query, self.address, async.address)
+        value = libcoopgamma_native.libcoopgamma_native_get_gamma_sync(query, self.address)
         if isinstance(value, int):
             raise ErrorReport.create_error(value)
         (successful, value) = value
-        if successful:
+        if not successful:
             raise ErrorReport.create_error(value)
         return FilterTable(*value)
     
@@ -1119,7 +1126,7 @@ def get_socket_file(method = None, site = None):
     '''
     if method is not None:
         method = str(method)
-    ret = libcoopgamma_native.libcoopgamma_native_get_pid_file(method, site)
+    ret = libcoopgamma_native.libcoopgamma_native_get_socket_file(method, site)
     if ret is not None and isinstance(ret, int):
         raise ErrorReport.create_error(ret)
     return ret
diff --git a/src/libcoopgamma_native.pyx.gpp b/src/libcoopgamma_native.pyx.gpp
index b673340..694e8f5 100644
--- a/src/libcoopgamma_native.pyx.gpp
+++ b/src/libcoopgamma_native.pyx.gpp
@@ -1006,9 +1006,10 @@ def libcoopgamma_native_context_unmarshal(buf : bytes):
                             Element 1: If [0] = 0:  The address of the unmarshalled instance
                                        If [0] = -1: The value of `errno`
     '''
-    cdef size_t ret1 = 0
+    cdef size_t _n = 0
     cdef libcoopgamma_context_t* this
     cdef char* bs = NULL
+    success = False
     this = <libcoopgamma_context_t*>calloc(1, sizeof(libcoopgamma_context_t))
     if this is NULL:
         return (-1, int(errno))
@@ -1016,15 +1017,18 @@ def libcoopgamma_native_context_unmarshal(buf : bytes):
         if libcoopgamma_context_initialise(this) < 0:
             return (-1, int(errno))
         bs = <char*>malloc(len(buf) * sizeof(char))
-        if bs is not NULL:
+        if bs is NULL:
             return (-1, int(errno))
         for i in range(len(buf)):
-            bs[i] = <char>(buf[i])
-        ret0 = <int>libcoopgamma_context_unmarshal(this, bs, &ret1)
-        return (ret0, <int>ret1)
+            bs[i] = <char><unsigned char>(buf[i])
+        ret = int(libcoopgamma_context_unmarshal(this, bs, &_n))
+        ret = (ret, int(<intptr_t><void*>this))
+        success = True
+        return ret
     finally:
-        libcoopgamma_context_destroy(this, <int>1)
-        free(this)
+        if not success:
+            libcoopgamma_context_destroy(this, <int>1)
+            free(this)
         free(bs)
 
 
@@ -1103,9 +1107,10 @@ def libcoopgamma_native_async_context_unmarshal(buf : bytes):
                             Element 1: If [0] = 0:  The address of the unmarshalled instance
                                        If [0] = -1: The value of `errno`
     '''
-    cdef size_t ret1 = 0
+    cdef size_t _n = 0
     cdef libcoopgamma_async_context_t* this
     cdef char* bs = NULL
+    successful = False
     this = <libcoopgamma_async_context_t*>malloc(sizeof(libcoopgamma_async_context_t))
     try:
         if this is NULL:
@@ -1113,15 +1118,18 @@ def libcoopgamma_native_async_context_unmarshal(buf : bytes):
         if libcoopgamma_async_context_initialise(this) < 0:
             return (-1, int(errno))
         bs = <char*>malloc(len(buf) * sizeof(char))
-        if bs is not NULL:
+        if bs is NULL:
             return (-1, int(errno))
         for i in range(len(buf)):
-            bs[i] = <char>(buf[i])
-        ret0 = <int>libcoopgamma_async_context_unmarshal(this, bs, &ret1)
-        return (ret0, <int>ret1)
+            bs[i] = <char><unsigned char>(buf[i])
+        ret = int(libcoopgamma_async_context_unmarshal(this, bs, &_n))
+        ret = (ret, int(<intptr_t><void*>this))
+        success = True
+        return ret
     finally:
-        libcoopgamma_async_context_destroy(this)
-        free(this)
+        if not success:
+            libcoopgamma_async_context_destroy(this)
+            free(this)
         free(bs)
 
 
diff --git a/src/test b/src/test
new file mode 100755
index 0000000..7f3c997
--- /dev/null
+++ b/src/test
@@ -0,0 +1,72 @@
+#!/usr/bin/env python3
+'''
+pylibcoopgamma -- Python library for interfacing with cooperative gamma servers
+Copyright (C) 2016  Mattias Andrée (maandree@kth.se)
+
+This library 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.
+
+This library 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 this library.  If not, see <http://www.gnu.org/licenses/>.
+'''
+
+import os, sys
+
+os.chdir('/'.join(sys.argv[0].split('/')[:-1]))
+sys.path.append('../bin')
+
+import libcoopgamma
+cg = libcoopgamma
+
+if len(sys.argv) == 1:
+    
+    print('\033[1m%s:\033[m' % 'Methods')
+    for m in cg.get_methods():
+        print(m)
+    print()
+    
+    print('\033[1m%s:\033[m' % 'Method')
+    print(cg.get_method_and_site()[0])
+    print()
+    
+    print('\033[1m%s:\033[m' % 'Site')
+    print(cg.get_method_and_site()[1])
+    print()
+    
+    print('\033[1m%s:\033[m' % 'PID file')
+    print(cg.get_pid_file())
+    print()
+    
+    print('\033[1m%s:\033[m' % 'Socket')
+    print(cg.get_socket_file())
+    print()
+    
+    g = cg.Context()
+    g.connect()
+    g.detach()
+    gstr = repr(g)
+    del g
+    
+    argv0 = './' + sys.argv[0].split('/')[-1]
+    os.execl(argv0, argv0, gstr)
+else:
+    g = eval(sys.argv[1])
+    g.attach()
+
+print('\033[1m%s:\033[m' % 'CRTC:s')
+for crtc in g.get_crtcs_sync():
+    print(crtc)
+print()
+
+print(g.get_gamma_info_sync(crtc))
+print(g.get_gamma_sync(cg.FilterQuery(crtc = crtc)))
+
+del g
+
-- 
cgit v1.2.3-70-g09d2