clear all; close all; clc;
%%
pyenv('Version', '/home/varsha/anaconda3/envs/pupilET/bin/python', ...
      'ExecutionMode', 'OutOfProcess');
py.importlib.import_module('zmq');

if count(py.sys.path, '') == 0
    insert(py.sys.path, int32(0), '');
end
disp('Loading the Python library...');

%%
ctx = py.zmq.Context();
pupil_remote = py.zmq.Socket(ctx, py.zmq.REQ);
pupil_remote.connect('tcp://127.0.0.1:50020');

pupil_remote.send_string("SUB_PORT");
sub_port = pupil_remote.recv_string();

sub = ctx.socket(py.zmq.SUB);
sub.connect(['tcp://127.0.0.1:', char(sub_port)]);
sub.setsockopt_string(py.zmq.SUBSCRIBE, 'surface');

surface_name = 'screen';
count = 1;
%%
while count<10 %    true
    topic = sub.recv_string();
    msg = sub.recv();
    surfaces = py.msgpack.loads(msg, pyargs('raw', false));
    surfaces = struct(surfaces);
    
    if strcmp(char(surfaces.name), surface_name)
        gaze_positions = cell(surfaces.gaze_on_surfaces);
        for gc = 1:numel(gaze_positions)
            norm_pos = double(gaze_positions{gc}{'norm_pos'});
            norm_gp_x = norm_pos(1);
            norm_gp_y = norm_pos(2);
    
            disp(['gaze on surface: ', num2str(surface_name), ', normalized coordinates: ', num2str(norm_gp_x), ',', num2str(norm_gp_y)])
        end
    
        count = count + 1;
    end
end