Skip to content

Commit c52b7a8

Browse files
committed
plugins: fmcomms2_adv: Refactor deprecated use of gdk_threads_enter/leave
Signed-off-by: Ioan Dragomir <[email protected]>
1 parent 4a646ab commit c52b7a8

File tree

1 file changed

+83
-27
lines changed

1 file changed

+83
-27
lines changed

plugins/fmcomms2_adv.c

+83-27
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ struct w_info {
8585
const char * const name;
8686
};
8787

88+
/* Container for passing both the progress bar and fraction to
89+
* set_calibration_progress_ui.
90+
* Allocated by caller, freed by callee. */
91+
struct set_calibration_progress_params {
92+
GtkProgressBar *pbar;
93+
float fraction;
94+
};
95+
8896
static struct w_info attrs[] = {
8997
{SPINBUTTON, "adi,agc-adc-large-overload-exceed-counter"},
9098
{SPINBUTTON, "adi,agc-adc-large-overload-inc-steps"},
@@ -814,17 +822,76 @@ static int get_cal_samples(long long cal_tone, long long cal_freq)
814822
return env_samples;
815823
}
816824

825+
static bool set_calibration_progress_ui(struct set_calibration_progress_params *params) {
826+
if (gtk_widget_get_visible(GTK_WIDGET(params->pbar))) {
827+
char ptext[64];
828+
829+
snprintf(ptext, sizeof(ptext), "Calibration Progress (%.2f %%)", params->fraction * 100);
830+
gtk_progress_bar_set_text(params->pbar, ptext);
831+
gtk_progress_bar_set_fraction(params->pbar, params->fraction);
832+
}
833+
834+
free(params);
835+
836+
return false;
837+
}
838+
817839
static void set_calibration_progress(GtkProgressBar *pbar, float fraction)
818840
{
819-
if (gtk_widget_get_visible(GTK_WIDGET(pbar))) {
820-
char ptext[64];
841+
struct set_calibration_progress_params *params = malloc(sizeof(struct set_calibration_progress_params));
842+
params->pbar = pbar;
843+
params->fraction = fraction;
844+
845+
gdk_threads_add_idle(G_SOURCE_FUNC(set_calibration_progress_ui), params);
846+
}
821847

822-
gdk_threads_enter();
823-
snprintf(ptext, sizeof(ptext), "Calibration Progress (%.2f %%)", fraction * 100);
824-
gtk_progress_bar_set_text(pbar, ptext);
825-
gtk_progress_bar_set_fraction(pbar, fraction);
826-
gdk_threads_leave();
848+
struct calibration_failed_ui_param {
849+
gpointer button;
850+
int ret;
851+
};
852+
853+
/* UI actions after a failed calibration
854+
Parameter struct is allocated by caller and freed by callee
855+
Not thread safe by itself - should be queued using gdk_threads_add_idle(), not g_idle_add_full() */
856+
static gboolean calibration_failed_ui(gpointer p) {
857+
struct calibration_failed_ui_param *param = (struct calibration_failed_ui_param *) p;
858+
859+
reload_settings();
860+
861+
if (param->ret) {
862+
create_blocking_popup(GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE,
863+
"FMCOMMS5", "Calibration failed");
864+
auto_calibrate = -1;
865+
} else {
866+
/* set completed flag for testing */
867+
auto_calibrate = 1;
827868
}
869+
870+
osc_plot_destroy(plot_xcorr_4ch);
871+
if (param->button)
872+
gtk_widget_show(GTK_WIDGET(param->button));
873+
874+
free(p);
875+
876+
return false;
877+
}
878+
879+
struct calibration_prepare_plot_param {
880+
int samples;
881+
};
882+
883+
/* Prepare xcorr_4ch plot
884+
Parameter struct is allocated by caller and freed by callee
885+
Not thread safe by itself - should be queued using gdk_threads_add_idle(), not g_idle_add_full() */
886+
static gboolean calibration_prepare_plot(gpointer p) {
887+
struct calibration_prepare_plot_param *param = (struct calibration_prepare_plot_param *) p;
888+
889+
osc_plot_set_sample_count(plot_xcorr_4ch, param->samples);
890+
osc_plot_draw_start(plot_xcorr_4ch);
891+
892+
free(p);
893+
894+
return false;
828895
}
829896

830897
static void calibrate (gpointer button)
@@ -834,6 +901,8 @@ static void calibrate (gpointer button)
834901
struct iio_channel *in0, *in0_slave;
835902
long long cal_tone, cal_freq;
836903
int ret, samples;
904+
struct calibration_prepare_plot_param *calib_prep_param;
905+
struct calibration_failed_ui_param *calib_failed_param;
837906

838907
in0 = iio_device_find_channel(dev, "voltage0", false);
839908
in0_slave = iio_device_find_channel(dev_slave, "voltage0", false);
@@ -877,10 +946,9 @@ static void calibrate (gpointer button)
877946

878947
DBG("cal_tone %lld cal_freq %lld samples %d", cal_tone, cal_freq, samples);
879948

880-
gdk_threads_enter();
881-
osc_plot_set_sample_count(plot_xcorr_4ch, samples);
882-
osc_plot_draw_start(plot_xcorr_4ch);
883-
gdk_threads_leave();
949+
calib_prep_param = malloc(sizeof(struct calibration_prepare_plot_param));
950+
calib_prep_param->samples = samples;
951+
gdk_threads_add_idle(calibration_prepare_plot, (gpointer) calib_prep_param);
884952

885953
/* Turn off quadrature tracking while the sync is going on */
886954
iio_channel_attr_write(in0, "quadrature_tracking_en", "0");
@@ -957,22 +1025,10 @@ static void calibrate (gpointer button)
9571025
iio_channel_attr_write(in0_slave, "quadrature_tracking_en", "1");
9581026
}
9591027

960-
gdk_threads_enter();
961-
reload_settings();
962-
963-
if (ret) {
964-
create_blocking_popup(GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE,
965-
"FMCOMMS5", "Calibration failed");
966-
auto_calibrate = -1;
967-
} else {
968-
/* set completed flag for testing */
969-
auto_calibrate = 1;
970-
}
971-
972-
osc_plot_destroy(plot_xcorr_4ch);
973-
if (button)
974-
gtk_widget_show(GTK_WIDGET(button));
975-
gdk_threads_leave();
1028+
calib_failed_param = malloc(sizeof(struct calibration_failed_ui_param));
1029+
calib_failed_param->button = button;
1030+
calib_failed_param->ret = ret;
1031+
gdk_threads_add_idle(calibration_failed_ui, (gpointer) calib_failed_param);
9761032

9771033
/* reset progress bar */
9781034
gtk_progress_bar_set_fraction(calib_progress, 0.0);

0 commit comments

Comments
 (0)