summaryrefslogtreecommitdiff
path: root/libvxi11client/perlbits/perlglue.c
blob: 3225ca0118429ed0f69172eca26980eb573e3e38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <EXTERN.h>
#include <perl.h>
#include <glib.h>
#include "libvxi11client.h"

static GAsyncQueue* interruptqueue;

typedef struct {
	char* handle;
} Event;

static Event* lastevent = NULL;

static void freeevent(Event* event) {
	g_free(event->handle);
	g_free(event);
}

static void freelast() {
	if (lastevent != NULL) {
		freeevent(lastevent);
	}
	lastevent = NULL;
}

static void interruptcallback(char* handle) {
	Event* event = g_malloc(sizeof(Event));
	int handlelen = strlen(handle);
	event->handle = g_malloc(handlelen + 1);
	strcpy(event->handle, handle);
	g_async_queue_push(interruptqueue, event);
}

VXI11Context* glue_open(char* address, char* device) {
	VXI11Context* context = malloc(sizeof(VXI11Context));
	if (context == NULL)
		return NULL;

	int err = vxi11_open(context, address, device);

	return context;
}

int glue_start_interrupt_server() {
	interruptqueue = g_async_queue_new();
	return vxi11_start_interrupt_server(interruptcallback);
}

int glue_stop_interrupt_server() {
	int ret = vxi11_stop_interrupt_server();
	Event* event = NULL;
	// clear everything that is left over
	while ((event = (Event*) g_async_queue_try_pop(interruptqueue)) != NULL) {
		freeevent(event);
	}
	freelast();
	g_free(interruptqueue);
	return ret;
}

char* glue_wait_for_interrupt() {
	GTimeVal timeout;
	g_get_current_time(&timeout);
	g_time_val_add(&timeout, 2500);
	freelast();
	lastevent = (Event*) g_async_queue_timed_pop(interruptqueue, &timeout);
	if (lastevent != NULL) {
		return lastevent->handle;
	}
	else
		return NULL;
}