Skip to content

Commit

Permalink
Redesign the preferences API to be const correct and prevent memory l…
Browse files Browse the repository at this point in the history
…eaks.
  • Loading branch information
erikd committed Jan 26, 2012
1 parent 1e78cf1 commit e3eb3d9
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 185 deletions.
53 changes: 25 additions & 28 deletions src/driver.c
Expand Up @@ -71,81 +71,78 @@ static sw_driver * dialog_driver = &_driver_null;

static char * prefs_driver_key = "driver";

char *
const char *
pcmio_get_default_main_dev (void)
{
static char name [128];
GList * names = NULL, * gl;

if (dialog_driver->get_names)
names = dialog_driver->get_names ();

if ((gl = names) != NULL) {
return (char *)gl->data;
strncpy (name, gl->data, sizeof (name));
return name;
}

return "Default";
}

char *
const char *
pcmio_get_default_monitor_dev (void)
{
static char name [128];
GList * names = NULL, * gl;

if (dialog_driver->get_names)
names = dialog_driver->get_names ();

if ((gl = names) != NULL) {
if ((gl = gl->next) != NULL) {
return (char *)gl->data;
strncpy (name, gl->data, sizeof (name));
return name;
}
}

return "Default";
}

char *
const char *
pcmio_get_main_dev (void)
{
char * main_dev;
static char main_dev [128];

main_dev = prefs_get_string (dialog_driver->primary_device_key);
prefs_get_string (dialog_driver->primary_device_key, main_dev, sizeof (main_dev), "");

if (main_dev == NULL) return pcmio_get_default_main_dev();
if (main_dev [0] == 0)
return pcmio_get_default_main_dev();

return main_dev;
}

char *
const char *
pcmio_get_monitor_dev (void)
{
char * monitor_dev;
static char monitor_dev [128];

monitor_dev = prefs_get_string (dialog_driver->monitor_device_key);
prefs_get_string (dialog_driver->monitor_device_key, monitor_dev, sizeof (monitor_dev), "");

if (monitor_dev == NULL) return pcmio_get_default_monitor_dev ();
if (monitor_dev [0] == 0)
return pcmio_get_default_monitor_dev ();

return monitor_dev;
}

gboolean
pcmio_get_use_monitor (void)
{
int * use_monitor;

use_monitor = prefs_get_int (USE_MONITOR_KEY);

if (use_monitor == NULL) return DEFAULT_USE_MONITOR;
else return (*use_monitor != 0);
return prefs_get_int (USE_MONITOR_KEY, DEFAULT_USE_MONITOR);
}

int
pcmio_get_log_frags (void)
{
int * log_frags;

log_frags = prefs_get_int (dialog_driver->log_frags_key);
if (log_frags == NULL) return DEFAULT_LOG_FRAGS;
else return (*log_frags);
return prefs_get_int (dialog_driver->log_frags_key, DEFAULT_LOG_FRAGS);
}

extern GtkStyle * style_bw;
Expand Down Expand Up @@ -322,7 +319,7 @@ static void
pcmio_devname_reset_cb (GtkWidget * widget, gpointer data)
{
GtkWidget * dialog = GTK_WIDGET (data);
char * main_dev, * monitor_dev;
const char * main_dev, * monitor_dev;

main_dev = pcmio_get_main_dev ();
monitor_dev = pcmio_get_monitor_dev ();
Expand All @@ -339,7 +336,7 @@ static void
pcmio_devname_default_cb (GtkWidget * widget, gpointer data)
{
GtkWidget * dialog = GTK_WIDGET (data);
char * name;
const char * name;

if ((name = pcmio_get_default_main_dev ()) != NULL) {
gtk_entry_set_text (GTK_ENTRY(GTK_COMBO(main_combo)->entry), name);
Expand Down Expand Up @@ -818,7 +815,7 @@ device_close (sw_handle * handle)
void
init_devices (void)
{
const char * driver;
char driver [64];
int k = 0;

memset (driver_table, 0, sizeof (driver_table));
Expand All @@ -833,13 +830,13 @@ init_devices (void)
if (driver_solaris->name != NULL)
driver_table [k++] = driver_solaris;

driver = prefs_get_string (prefs_driver_key);
prefs_get_string (prefs_driver_key, driver, sizeof (driver), "");

/* Set a default in case preferences driver doesn't exist. */
current_driver = driver_table [0];

/* Switch to driver from preferences if possible. */
if (driver != NULL)
if (driver [0] != 0)
for (k = 0 ; driver_table [k] != NULL ; k++)
if (strcmp (driver, driver_table [k]->name) == 0)
current_driver = driver_table [k];
Expand Down
2 changes: 1 addition & 1 deletion src/driver_alsa.c
Expand Up @@ -126,7 +126,7 @@ static sw_handle *
alsa_device_open (int monitoring, int flags)
{
int err;
char * alsa_pcm_name;
const char * alsa_pcm_name;
snd_pcm_t * pcm_handle;
sw_handle * handle = &alsa_handle;
snd_pcm_stream_t stream;
Expand Down
2 changes: 1 addition & 1 deletion src/driver_oss.c
Expand Up @@ -97,7 +97,7 @@ oss_get_names (void)
static sw_handle *
oss_open (int monitoring, int flags)
{
char * dev_name;
const char * dev_name;
int dev_dsp;
sw_handle * handle = &oss_handle;
int i;
Expand Down
16 changes: 6 additions & 10 deletions src/file_dialogs.c
Expand Up @@ -274,7 +274,7 @@ sample_load_cb(GtkWidget * widget, gpointer data)
{

GtkWidget *dialog;
gchar *load_current_file;
gchar load_current_file [512];
gint win_width, win_height;
GSList *filenames, *list;

Expand All @@ -297,12 +297,10 @@ sample_load_cb(GtkWidget * widget, gpointer data)
sweep_set_window_icon (GTK_WINDOW(dialog));
attach_window_close_accel(GTK_WINDOW(dialog));

load_current_file = prefs_get_string (LAST_LOAD_KEY);
prefs_get_string (LAST_LOAD_KEY, load_current_file, sizeof (load_current_file), "");

if (load_current_file) {
if (load_current_file [0]) {
gtk_file_chooser_set_filename (GTK_FILE_CHOOSER(dialog), load_current_file);

g_free(load_current_file);
}

if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
Expand Down Expand Up @@ -686,7 +684,6 @@ sample_save_as_cb(GtkWidget * widget, gpointer data)
save_as_data * sd;

char buf[512];
char * last_save;

win_width = gdk_screen_width () / 2;
win_height = gdk_screen_height () / 2;
Expand Down Expand Up @@ -739,10 +736,11 @@ sample_save_as_cb(GtkWidget * widget, gpointer data)
gtk_widget_set_size_request (dialog, win_width, win_height);

if (strcmp (g_path_get_dirname(sample->pathname), ".") == 0) {
char last_save [512];

last_save = prefs_get_string (LAST_SAVE_KEY);
prefs_get_string (LAST_SAVE_KEY, last_save, sizeof (last_save), "");

if (last_save != NULL) {
if (last_save [0]) {
gchar * last_save_dir = g_dirname (last_save);

gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER(dialog),
Expand All @@ -751,8 +749,6 @@ sample_save_as_cb(GtkWidget * widget, gpointer data)
sample->pathname);

g_free (last_save_dir);
g_free (last_save);

}
} else {
retval = gtk_file_chooser_set_filename (GTK_FILE_CHOOSER(dialog),
Expand Down
42 changes: 9 additions & 33 deletions src/file_speex.c
Expand Up @@ -1339,63 +1339,39 @@ speex_encode_options_reset_cb (GtkWidget * widget, gpointer data)
GtkWidget * dialog;

GtkObject * adj;
int * i, features, quality, complexity, framepack;
int features, quality, complexity, framepack;

dialog = gtk_widget_get_toplevel (widget);

/* Mode menu */
speex_encode_options_mode_auto_cb (widget, data);

/* Features menu */
i = prefs_get_int (FEATURES_KEY);

if (i == NULL) {
features = DEFAULT_FEATURES;
} else {
features = *i;
}
features = prefs_get_int (FEATURES_KEY, DEFAULT_FEATURES);

speex_encode_options_set_features (dialog, features);

/* Quality */

adj = GTK_OBJECT(g_object_get_data (G_OBJECT(dialog), "quality_adj"));

i = prefs_get_int (QUALITY_KEY);

if (i == NULL) {
quality = DEFAULT_QUALITY;
} else {
quality = *i;
}
quality = prefs_get_int (QUALITY_KEY, DEFAULT_QUALITY);

gtk_adjustment_set_value (GTK_ADJUSTMENT(adj), (float)quality);

/* Complexity */

adj = GTK_OBJECT(g_object_get_data (G_OBJECT(dialog), "complexity_adj"));

i = prefs_get_int (COMPLEXITY_KEY);

if (i == NULL) {
complexity = DEFAULT_COMPLEXITY;
} else {
complexity = *i;
}
complexity = prefs_get_int (COMPLEXITY_KEY, DEFAULT_COMPLEXITY);

gtk_adjustment_set_value (GTK_ADJUSTMENT(adj), (float)complexity);

/* Framepack */

adj = GTK_OBJECT(g_object_get_data (G_OBJECT(dialog), "framepack_adj"));

i = prefs_get_int (FRAMEPACK_KEY);

if (i == NULL) {
framepack = DEFAULT_FRAMEPACK;
} else {
framepack = *i;
}
framepack = prefs_get_int (FRAMEPACK_KEY, DEFAULT_FRAMEPACK);

gtk_adjustment_set_value (GTK_ADJUSTMENT(adj), (float)framepack);

Expand Down Expand Up @@ -1528,7 +1504,7 @@ create_speex_encoding_options_dialog (sw_sample * sample, char * pathname)
/* GtkStyle * style; */

int i;
long * l;
long l;

dialog = gtk_dialog_new ();
gtk_window_set_title (GTK_WINDOW(dialog),
Expand Down Expand Up @@ -1996,15 +1972,15 @@ create_speex_encoding_options_dialog (sw_sample * sample, char * pathname)

g_object_set_data (G_OBJECT (dialog), "rem_serialno_chb", checkbutton);

l = prefs_get_long (SERIALNO_KEY);
l = prefs_get_long (SERIALNO_KEY, 0);

if (l == NULL) {
if (l == 0) {
randomise_serialno (entry);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(checkbutton), FALSE);
} else {
char temp [128];

snprintf (temp, sizeof (temp), "%ld", *l);
snprintf (temp, sizeof (temp), "%ld", l);
gtk_entry_set_text (GTK_ENTRY(entry), temp);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(checkbutton), TRUE);
}
Expand Down

0 comments on commit e3eb3d9

Please sign in to comment.