-
Notifications
You must be signed in to change notification settings - Fork 146
/
osc.c
2980 lines (2482 loc) · 72.3 KB
/
osc.c
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (C) 2012-2013 Analog Devices, Inc.
*
* Licensed under the GPL-2.
*
**/
#include <gtk/gtk.h>
#include <gtkdatabox.h>
#include <gtkdatabox_grid.h>
#include <gtkdatabox_points.h>
#include <gtkdatabox_lines.h>
#include <gtkdatabox_markers.h>
#include <math.h>
#include <stdint.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <iio.h>
#include "compat.h"
#include "libini2.h"
#include "osc.h"
#include "datatypes.h"
#include "config.h"
#include "osc_plugin.h"
#include "iio_utils.h"
GSList *plugin_list = NULL;
gint capture_function = 0;
static bool restart_capture = FALSE;
static GList *plot_list = NULL;
static int num_capturing_plots;
G_LOCK_DEFINE_STATIC(buffer_full);
static gboolean stop_capture;
static struct plugin_check_fct *setup_check_functions = NULL;
static int num_check_fcts = 0;
static GSList *plugin_lib_list = NULL;
static GSList *dplugin_list = NULL;
static struct osc_plugin *spect_analyzer_plugin = NULL;
static OscPreferences *osc_preferences = NULL;
GtkWidget *notebook;
GtkWidget *infobar;
GtkWidget *tooltips_en;
GtkWidget *versioncheck_en;
GtkWidget *main_window;
struct iio_context *ctx = NULL;
static unsigned int num_devices = 0;
bool ctx_destroyed_by_do_quit;
bool ini_capture_timeout_loaded = FALSE;
unsigned int ini_capture_timeout = 0;
static void gfunc_save_plot_data_to_ini(gpointer data, gpointer user_data);
static void plugin_restore_ini_state(const char *plugin_name,
const char *attribute, int value);
static void plot_init(GtkWidget *plot);
static void plot_destroyed_cb(OscPlot *plot);
static void capture_profile_save(const char *filename);
static int load_profile(const char *filename, bool load_plugins);
static int capture_setup(void);
static void capture_start(void);
static void stop_sampling(void);
static char * dma_devices[] = {
"ad9122",
"ad9144",
"ad9250",
"ad9361",
"ad9643",
"ad9680",
"ad9371"
};
#define DMA_DEVICES_COUNT (sizeof(dma_devices) / sizeof(dma_devices[0]))
#ifdef __APPLE__
const void *memrchr(const void *src, int c, size_t length)
{
const unsigned char *cp;
if (length != 0) {
cp = (const unsigned char *)src + length;
do {
if (*(--cp) == (unsigned char)c)
return (const void *)cp;
} while (--length != 0);
}
return NULL;
}
#endif
static const char * get_adi_part_code(const char *device_name)
{
const char *ad = NULL;
if (!device_name)
return NULL;
ad = strstr(device_name, "ad");
if (!ad || strlen(ad) < strlen("adxxxx"))
return NULL;
if (g_ascii_isdigit(ad[2]) &&
g_ascii_isdigit(ad[3]) &&
g_ascii_isdigit(ad[4]) &&
g_ascii_isdigit(ad[5])) {
return ad;
}
return NULL;
}
bool dma_valid_selection(const char *device, unsigned mask, unsigned channel_count)
{
static const unsigned long eight_channel_masks[] = {
0x01, 0x02, 0x04, 0x08, 0x03, 0x0C, /* 1 & 2 chan */
0x10, 0x20, 0x40, 0x80, 0x30, 0xC0, /* 1 & 2 chan */
0x33, 0xCC, 0xC3, 0x3C, 0x0F, 0xF0, /* 4 chan */
0xFF, /* 8chan */
0x00
};
static const unsigned long four_channel_masks[] = {
0x01, 0x02, 0x04, 0x08, 0x03, 0x0C, 0x0F,
0x00
};
bool ret = true;
unsigned int i;
device = get_adi_part_code(device);
if (!device)
return true;
for (i = 0; i < DMA_DEVICES_COUNT; i++) {
if (!strncmp(device, dma_devices[i], strlen(dma_devices[i])))
break;
}
/* Skip validation for devices that are not in the list */
if (i == DMA_DEVICES_COUNT)
return true;
if (channel_count == 8) {
ret = false;
for (i = 0; i < sizeof(eight_channel_masks) / sizeof(eight_channel_masks[0]); i++)
if (mask == eight_channel_masks[i])
return true;
} else if (channel_count == 4) {
ret = false;
for (i = 0; i < sizeof(four_channel_masks) / sizeof(four_channel_masks[0]); i++)
if (mask == four_channel_masks[i])
return true;
}
return ret;
}
unsigned global_enabled_channels_mask(struct iio_device *dev)
{
unsigned mask = 0;
int scan_i = 0;
unsigned int i = 0;
for (; i < iio_device_get_channels_count(dev); i++) {
struct iio_channel *chn = iio_device_get_channel(dev, i);
if (iio_channel_is_scan_element(chn)) {
if (iio_channel_is_enabled(chn))
mask |= 1 << scan_i;
scan_i++;
}
}
return mask;
}
/* Couple helper functions from fru parsing */
void printf_warn (const char * fmt, ...)
{
return;
}
void printf_err (const char * fmt, ...)
{
va_list ap;
va_start(ap,fmt);
vfprintf(stderr,fmt,ap);
va_end(ap);
}
void * x_calloc (size_t nmemb, size_t size)
{
unsigned int *ptr;
ptr = calloc(nmemb, size);
if (ptr == NULL)
printf_err("memory error - calloc returned zero\n");
return (void *)ptr;
}
static void gfunc_restart_plot(gpointer data, gpointer user_data)
{
GtkWidget *plot = data;
osc_plot_restart(OSC_PLOT(plot));
}
static void gfunc_close_plot(gpointer data, gpointer user_data)
{
GtkWidget *plot = data;
osc_plot_draw_stop(OSC_PLOT(plot));
}
static void gfunc_destroy_plot(gpointer data, gpointer user_data)
{
GtkWidget *plot = data;
osc_plot_destroy(OSC_PLOT(plot));
}
static void update_plot(struct iio_buffer *buf)
{
GList *node;
for (node = plot_list; node; node = g_list_next(node)) {
OscPlot *plot = (OscPlot *) node->data;
if (osc_plot_get_buffer(plot) == buf) {
osc_plot_data_update(plot);
}
}
}
static void restart_all_running_plots(void)
{
g_list_foreach(plot_list, gfunc_restart_plot, NULL);
}
static void close_all_plots(void)
{
g_list_foreach(plot_list, gfunc_close_plot, NULL);
}
static void destroy_all_plots(void)
{
g_list_foreach(plot_list, gfunc_destroy_plot, NULL);
}
static void disable_all_channels(struct iio_device *dev)
{
unsigned int i, nb_channels = iio_device_get_channels_count(dev);
for (i = 0; i < nb_channels; i++)
iio_channel_disable(iio_device_get_channel(dev, i));
}
static void close_active_buffers(void)
{
unsigned int i;
for (i = 0; i < num_devices; i++) {
struct iio_device *dev = iio_context_get_device(ctx, i);
struct extra_dev_info *info = iio_device_get_data(dev);
if (info->buffer) {
iio_buffer_destroy(info->buffer);
info->buffer = NULL;
}
disable_all_channels(dev);
}
}
static void stop_sampling(void)
{
stop_capture = TRUE;
close_active_buffers();
G_TRYLOCK(buffer_full);
G_UNLOCK(buffer_full);
}
static void detach_plugin(GtkToolButton *btn, gpointer data);
static GtkWidget* plugin_tab_add_detach_btn(GtkWidget *page, const struct detachable_plugin *d_plugin)
{
GtkWidget *tab_box;
GtkWidget *tab_label;
GtkWidget *tab_detach_btn;
const struct osc_plugin *plugin = d_plugin->plugin;
const char *plugin_name = plugin->name;
tab_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
tab_label = gtk_label_new(plugin_name);
// TO DO: since "gtk-disconnect" is no longer available, maybe use a custom image
tab_detach_btn = (GtkWidget *)gtk_tool_button_new(
gtk_image_new_from_icon_name("window-new", GTK_ICON_SIZE_SMALL_TOOLBAR), NULL);
gtk_widget_set_size_request(tab_detach_btn, 25, 25);
gtk_container_add(GTK_CONTAINER(tab_box), tab_label);
gtk_container_add(GTK_CONTAINER(tab_box), tab_detach_btn);
gtk_box_set_spacing(GTK_BOX(tab_box), 10);
gtk_widget_show_all(tab_box);
gtk_notebook_set_tab_label(GTK_NOTEBOOK(notebook), page, tab_box);
g_signal_connect(tab_detach_btn, "clicked",
G_CALLBACK(detach_plugin), (gpointer)d_plugin);
return tab_detach_btn;
}
static void plugin_make_detachable(struct detachable_plugin *d_plugin)
{
GtkWidget *page;
int num_pages = 0;
num_pages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(notebook));
page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(notebook), num_pages - 1);
d_plugin->window = NULL;
d_plugin->detached_state = FALSE;
d_plugin->detach_attach_button = plugin_tab_add_detach_btn(page, d_plugin);
}
static void attach_plugin(GtkWidget *window, struct detachable_plugin *d_plugin)
{
GtkWidget *plugin_page;
GtkWidget *detach_btn;
const struct osc_plugin *plugin = d_plugin->plugin;
gint plugin_page_index;
GtkWidget *hbox;
GList *hbox_elems;
GList *first;
hbox = gtk_bin_get_child(GTK_BIN(window));
hbox_elems = gtk_container_get_children(GTK_CONTAINER(hbox));
first = g_list_first(hbox_elems);
plugin_page = first->data;
gtk_container_remove(GTK_CONTAINER(hbox), plugin_page);
gtk_widget_destroy(window);
plugin_page_index = gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
plugin_page, NULL);
gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), plugin_page_index);
detach_btn = plugin_tab_add_detach_btn(plugin_page, d_plugin);
if (plugin->update_active_page)
plugin->update_active_page((struct osc_plugin *)plugin, plugin_page_index, FALSE);
d_plugin->detached_state = FALSE;
d_plugin->detach_attach_button = detach_btn;
d_plugin->window = NULL;
}
static void debug_window_delete_cb(GtkWidget *w, GdkEvent *e, gpointer data)
{
attach_plugin(w, (struct detachable_plugin *)data);
}
static GtkWidget * extract_label_from_box(GtkWidget *box)
{
GList *children;
GList *first;
GtkWidget *label;
children = gtk_container_get_children(GTK_CONTAINER(box));
first = g_list_first(children);
label = first->data;
g_list_free(children);
return label;
}
static void detach_plugin(GtkToolButton *btn, gpointer data)
{
struct detachable_plugin *d_plugin = (struct detachable_plugin *)data;
const struct osc_plugin *plugin = d_plugin->plugin;
const char *plugin_name = plugin->name;
const char *page_name = NULL;
GtkWidget *page = NULL;
GtkWidget *box;
GtkWidget *label;
GtkWidget *window;
GtkWidget *hbox;
int num_pages;
int i;
/* Find the page that belongs to a plugin, using the plugin name */
num_pages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(notebook));
for (i = 0; i < num_pages; i++) {
page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(notebook), i);
box = gtk_notebook_get_tab_label(GTK_NOTEBOOK(notebook), page);
if (GTK_IS_BOX(box))
label = extract_label_from_box(box);
else
label = box;
page_name = gtk_label_get_text(GTK_LABEL(label));
if (!strcmp(page_name, plugin_name))
break;
}
if (i == num_pages) {
fprintf(stderr, "Could not find %s plugin in the notebook\n",
plugin_name);
return;
}
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
if (plugin->get_preferred_size) {
int width = -1, height = -1;
plugin->get_preferred_size(plugin, &width, &height);
gtk_window_set_default_size(GTK_WINDOW(window), width, height);
}
hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
gtk_window_set_title(GTK_WINDOW(window), page_name);
gtk_container_remove(GTK_CONTAINER(notebook), page);
gtk_container_add(GTK_CONTAINER(hbox), page);
gtk_container_add(GTK_CONTAINER(window), hbox);
g_signal_connect(window, "delete-event",
G_CALLBACK(debug_window_delete_cb), (gpointer)d_plugin);
if (plugin->update_active_page)
plugin->update_active_page((struct osc_plugin *)plugin, -1, TRUE);
d_plugin->detached_state = TRUE;
d_plugin->detach_attach_button = NULL;
d_plugin->window = window;
gtk_widget_show(window);
gtk_widget_show(hbox);
}
static const char * device_name_check(const char *name)
{
struct iio_device *dev;
if (!name)
return NULL;
dev = iio_context_find_device(ctx, name);
if (!dev)
return NULL;
return get_iio_device_label_or_name(dev);
}
/*
* helper functions for plugins which want to look at data
*/
struct iio_context *get_context_from_osc(void)
{
return ctx;
}
/*
* Helper function to move window to valid coordinate on screen
*/
void move_gtk_window_on_screen(GtkWindow *window,
gint x,
gint y)
{
// get screen dimensions
GdkWindow *gdk_window = gtk_widget_get_window(GTK_WIDGET(window));
GdkScreen *screen = gdk_window_get_screen(gdk_window);
GdkDisplay *display = gdk_screen_get_display(screen);
GdkMonitor *monitor = gdk_display_get_monitor_at_window(display, gdk_window);
GdkRectangle geometry;
gdk_monitor_get_geometry(monitor, &geometry);
gint screen_w = geometry.width;
gint screen_h = geometry.height;
gint window_w;
gint window_h;
gtk_window_get_size(window, &window_w, &window_h);
/* Make sure the window fully fits inside the screen area */
if ( (x >= 0 && x + window_w <= screen_w) &&
(y >= 0 && y + window_h <= screen_h) ) {
gtk_window_move(window, x, y);
} else {
gtk_window_set_position(window, GTK_WIN_POS_CENTER);
}
}
const void * plugin_get_device_by_reference(const char * device_name)
{
return device_name_check(device_name);
}
OscPlot * plugin_find_plot_with_domain(int domain)
{
OscPlot *plot;
GList *node;
if (!plot_list)
return NULL;
for (node = plot_list; node; node = g_list_next(node)) {
plot = node->data;
if (osc_plot_get_plot_domain(plot) == domain)
return plot;
}
return NULL;
}
enum marker_types plugin_get_plot_marker_type(OscPlot *plot, const char *device)
{
if (!plot || !device)
return MARKER_NULL;
if (!strcmp(osc_plot_get_active_device(plot), device))
return MARKER_NULL;
return osc_plot_get_marker_type(plot);
}
void plugin_set_plot_marker_type(OscPlot *plot, const char *device, enum marker_types type)
{
int plot_domain;
if (!plot || !device)
return;
plot_domain = osc_plot_get_plot_domain(plot);
if (plot_domain == FFT_PLOT || plot_domain == XY_PLOT)
if (!strcmp(osc_plot_get_active_device(plot), device))
return;
osc_plot_set_marker_type(plot, type);
}
gdouble plugin_get_plot_fft_avg(OscPlot *plot, const char *device)
{
if (!plot || !device)
return 0;
if (!strcmp(osc_plot_get_active_device(plot), device))
return 0;
return osc_plot_get_fft_avg(plot);
}
int plugin_data_capture_size(const char *device)
{
struct extra_dev_info *info;
struct iio_device *dev;
if (!device)
return 0;
dev = iio_context_find_device(ctx, device);
if (!dev)
return 0;
info = iio_device_get_data(dev);
return info->sample_count * iio_device_get_sample_size(dev);
}
int plugin_data_capture_num_active_channels(const char *device)
{
int nb_active = 0;
unsigned int i, nb_channels;
struct iio_device *dev;
if (!device)
return 0;
dev = iio_context_find_device(ctx, device);
if (!dev)
return 0;
nb_channels = iio_device_get_channels_count(dev);
for (i = 0; i < nb_channels; i++) {
struct iio_channel *chn = iio_device_get_channel(dev, i);
if (iio_channel_is_enabled(chn))
nb_active++;
}
return nb_active;
}
int plugin_data_capture_bytes_per_sample(const char *device)
{
struct iio_device *dev;
if (!device)
return 0;
dev = iio_context_find_device(ctx, device);
if (!dev)
return 0;
return iio_device_get_sample_size(dev);
}
int plugin_data_capture_of_plot(OscPlot *plot, const char *device, gfloat ***cooked_data,
struct marker_type **markers_cp)
{
struct iio_device *dev, *tmp_dev = NULL;
struct extra_dev_info *dev_info;
struct marker_type *markers_copy = NULL;
GMutex *markers_lock;
unsigned int i, j;
bool new = FALSE;
const char *tmp = NULL;
if (device == NULL)
dev = NULL;
else
dev = iio_context_find_device(ctx, device);
if (plot) {
tmp = osc_plot_get_active_device(plot);
tmp_dev = iio_context_find_device(ctx, tmp);
}
/* if there isn't anything to send, clear everything */
if (dev == NULL) {
if (cooked_data && *cooked_data) {
if (tmp_dev)
for (i = 0; i < iio_device_get_channels_count(tmp_dev); i++)
if ((*cooked_data)[i]) {
g_free((*cooked_data)[i]);
(*cooked_data)[i] = NULL;
}
g_free(*cooked_data);
*cooked_data = NULL;
}
if (markers_cp && *markers_cp) {
if (*markers_cp)
g_free(*markers_cp);
*markers_cp = NULL;
}
return -ENXIO;
}
if (!dev)
return -ENXIO;
if (osc_plot_running_state(plot) == FALSE)
return -ENXIO;
if (osc_plot_get_marker_type(plot) == MARKER_OFF ||
osc_plot_get_marker_type(plot) == MARKER_NULL)
return -ENXIO;
if (cooked_data) {
dev_info = iio_device_get_data(dev);
/* One consumer at a time */
if (dev_info->channels_data_copy)
return -EBUSY;
/* make sure space is allocated */
if (*cooked_data) {
*cooked_data = g_renew(gfloat *, *cooked_data,
iio_device_get_channels_count(dev));
new = false;
} else {
*cooked_data = g_new(gfloat *, iio_device_get_channels_count(dev));
new = true;
}
if (!*cooked_data)
goto capture_malloc_fail;
for (i = 0; i < iio_device_get_channels_count(dev); i++) {
if (new)
(*cooked_data)[i] = g_new(gfloat,
dev_info->sample_count);
else
(*cooked_data)[i] = g_renew(gfloat,
(*cooked_data)[i],
dev_info->sample_count);
if (!(*cooked_data)[i])
goto capture_malloc_fail;
for (j = 0; j < dev_info->sample_count; j++)
(*cooked_data)[i][j] = 0.0f;
}
/* where to put the copy */
dev_info->channels_data_copy = *cooked_data;
/* Wait til the buffer is full */
G_LOCK(buffer_full);
/* if the lock is released, but the copy is still there
* that's because someone else broke the lock
*/
if (dev_info->channels_data_copy) {
dev_info->channels_data_copy = NULL;
return -EINTR;
}
}
if (plot) {
markers_copy = (struct marker_type *)osc_plot_get_markers_copy(plot);
markers_lock = osc_plot_get_marker_lock(plot);
}
if (markers_cp) {
if (!plot) {
if (*markers_cp) {
g_free(*markers_cp);
*markers_cp = NULL;
}
return 0;
}
/* One consumer at a time */
if (markers_copy)
return -EBUSY;
/* make sure space is allocated */
if (*markers_cp)
*markers_cp = g_renew(struct marker_type, *markers_cp, MAX_MARKERS + 2);
else
*markers_cp = g_new(struct marker_type, MAX_MARKERS + 2);
if (!*markers_cp)
goto capture_malloc_fail;
/* where to put the copy */
osc_plot_set_markers_copy(plot, *markers_cp);
/* Wait til the copy is complete */
g_mutex_lock(markers_lock);
/* if the lock is released, but the copy is still here
* that's because someone else broke the lock
*/
if (markers_copy) {
osc_plot_set_markers_copy(plot, NULL);
return -EINTR;
}
}
return 0;
capture_malloc_fail:
fprintf(stderr, "%s:%s malloc failed\n", __FILE__, __func__);
return -ENOMEM;
}
OscPlot * plugin_get_new_plot(void)
{
return OSC_PLOT(new_plot_cb(NULL, NULL));
}
void plugin_osc_stop_capture(void)
{
stop_sampling();
}
void plugin_osc_start_capture(void)
{
capture_setup();
capture_start();
}
bool plugin_osc_running_state(void)
{
return !!capture_function;
}
void plugin_osc_stop_all_plots(void)
{
close_all_plots();
}
static bool force_plugin(const char *name)
{
const char *force_plugin = getenv("OSC_FORCE_PLUGIN");
const char *pos;
if (!force_plugin)
return false;
if (strcmp(force_plugin, "all") == 0)
return true;
#ifdef __GLIBC__
pos = strcasestr(force_plugin, name);
#else
pos = strstr(force_plugin, name);
#endif
if (pos) {
switch (*(pos + strlen(name))) {
case ' ':
case '\0':
return true;
default:
break;
}
}
return false;
}
static void close_plugins(const char *ini_fn)
{
GSList *node;
for (node = dplugin_list; node; node = g_slist_next(node)) {
struct detachable_plugin *d_plugin = node->data;
struct osc_plugin *plugin = (struct osc_plugin *)d_plugin->plugin;
if (d_plugin->window)
gtk_widget_destroy(d_plugin->window);
if (plugin) {
printf("Closing plugin: %s\n", plugin->name);
if (plugin->destroy)
plugin->destroy(plugin, ini_fn);
if (plugin->dynamically_created)
g_free(plugin);
}
g_free(d_plugin);
}
/* Closing the shared library handles after we're done wit the plugins */
for (node = plugin_lib_list; node; node = g_slist_next(node)) {
void *lib_handle = node->data;
dlclose(lib_handle);
}
g_slist_free(dplugin_list);
dplugin_list = NULL;
g_slist_free(plugin_list);
plugin_list = NULL;
g_slist_free(plugin_lib_list);
plugin_lib_list = NULL;
}
static void close_plugin(struct osc_plugin *plugin)
{
plugin_list = g_slist_remove(plugin_list, plugin);
plugin_lib_list = g_slist_remove(plugin_lib_list, plugin->handle);
if (plugin->dynamically_created) {
dlclose(plugin->handle);
g_free(plugin);
} else {
dlclose(plugin->handle);
}
}
static struct osc_plugin * get_plugin_from_name(const char *name)
{
GSList *node;
for (node = plugin_list; node; node = g_slist_next(node)) {
struct osc_plugin *plugin = node->data;
if (plugin && !strcmp(plugin->name, name))
return plugin;
}
return NULL;
}
bool plugin_installed(const char *name)
{
return !!get_plugin_from_name(name);
}
void * plugin_dlsym(const char *name, const char *symbol)
{
GSList *node;
struct osc_plugin *plugin = NULL;
void *fcn;
char *buf;
#ifndef __MINGW__
Dl_info info;
#endif
for (node = plugin_list; node; node = g_slist_next(node)) {
plugin = node->data;
if (plugin && !strcmp(plugin->name, name)) {
dlerror();
fcn = dlsym(plugin->handle, symbol);
buf = dlerror();
if (buf) {
fprintf(stderr, "%s:%s(): found plugin %s, error looking up %s\n"
"\t%s\n", __FILE__, __func__, name, symbol, buf);
#ifndef __MINGW__
if (dladdr(__builtin_return_address(0), &info))
fprintf(stderr, "\tcalled from %s:%s()\n", info.dli_fname, info.dli_sname);
#endif
}
return fcn;
}
}
fprintf(stderr, "%s:%s : No plugin with matching name %s\n", __FILE__, __func__, name);
#ifndef __MINGW__
if (dladdr(__builtin_return_address(0), &info))
fprintf(stderr, "\tcalled from %s:%s()\n", info.dli_fname, info.dli_sname);
#endif
return NULL;
}
void osc_plugin_context_free_resources(struct osc_plugin_context *ctx)
{
if (ctx->plugin_name)
g_free(ctx->plugin_name);
if (ctx->required_devices)
g_list_free_full(ctx->required_devices, (GDestroyNotify)g_free);
}
bool str_endswith(const char *str, const char *needle)
{
const char *pos;
pos = strstr(str, needle);
if (pos == NULL)
return false;
return *(pos + strlen(needle)) == '\0';
}
static void * init_plugin(void *data)
{
GtkWidget *widget;
struct {
struct osc_plugin *plugin;
GtkWidget *notebook;
const char *ini_fn;
} *params = data;
widget = params->plugin->init(params->plugin, params->notebook, params->ini_fn);
free(data);
return widget;
}
static void load_plugin_finish(GtkNotebook *notebook,
GtkWidget *widget, struct osc_plugin *plugin)
{
struct detachable_plugin *d_plugin;
gint page;
page = gtk_notebook_append_page(notebook, widget, NULL);
gtk_notebook_set_tab_label_text(notebook, widget, plugin->name);
if (plugin->update_active_page)
plugin->update_active_page(plugin, page, FALSE);
d_plugin = malloc(sizeof(*d_plugin));
d_plugin->plugin = plugin;
dplugin_list = g_slist_append(dplugin_list, (gpointer) d_plugin);
plugin_make_detachable(d_plugin);
}
static void generic_dac_handle(struct osc_plugin *generic_dac, void *generic_lib, const char *ini_fn) {
struct params {
struct osc_plugin *plugin;
GtkWidget *notebook;
const char *ini_fn;
} *params;
if (!generic_dac || !generic_lib)
return;
plugin_list = g_slist_append (plugin_list, (gpointer) generic_dac);
plugin_lib_list = g_slist_append(plugin_lib_list, generic_lib);
params = malloc(sizeof(*params));
params->plugin = generic_dac;
params->notebook = notebook;
params->ini_fn = ini_fn;
GtkWidget *widget = init_plugin(params);
if (widget) {
load_plugin_finish(GTK_NOTEBOOK(notebook), widget, generic_dac);
printf("Loaded plugin: %s\n", generic_dac->name);
}
}
static void load_plugin_complete(gpointer data, gpointer user_data)
{
struct osc_plugin *plugin = data;
GtkWidget *widget;
/* not sure if plugin can actually be null but... */
if (!plugin || !plugin->thd)
return;
widget = g_thread_join(plugin->thd);
if (!widget) {