[mednaffe] 76/99: Added the ability to select mednafen executable if it is not found in path

Stephen Kitt skitt at moszumanska.debian.org
Tue Aug 2 21:00:09 UTC 2016


This is an automated email from the git hooks/post-receive script.

skitt pushed a commit to branch master
in repository mednaffe.

commit e8ee7f872665221255773518addd700db8118418
Author: AmatCoder <amatcoder at gmail.com>
Date:   Sun Jul 19 11:37:31 2015 +0200

    Added the ability to select mednafen executable if it is not found in path
---
 src/mednaffe.c | 157 +++++++++++++++++++++++++++++++++++++--------------------
 src/prefs.c    | 153 ++++++++++++++++++++++++++++---------------------------
 src/prefs.h    |  14 ++---
 src/toggles.c  |   3 ++
 4 files changed, 191 insertions(+), 136 deletions(-)

diff --git a/src/mednaffe.c b/src/mednaffe.c
index 0d466a3..2f111c4 100644
--- a/src/mednaffe.c
+++ b/src/mednaffe.c
@@ -57,6 +57,42 @@ void show_error(const gchar *message)
   printf("%s", message);
 }
 
+gchar* show_chooser(const gchar *message)
+{
+  GtkWidget *exe;
+  gchar *filename = NULL;
+  GtkWidget *dialog;
+
+  dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_DESTROY_WITH_PARENT,
+                                   GTK_MESSAGE_QUESTION,
+                                   GTK_BUTTONS_YES_NO,
+                                   "%s", message);
+
+  if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_NO)
+  {
+    gtk_widget_destroy (dialog);
+    return NULL;
+  }
+  gtk_widget_destroy (dialog);
+
+  exe = gtk_file_chooser_dialog_new(
+    "Choose a mednafen executable...", NULL,
+    GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL,
+    GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL );
+
+  GtkFileFilter* filter = gtk_file_filter_new();
+  gtk_file_filter_add_pattern(filter, "mednafen.exe");
+  gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(exe), filter);
+
+  if (gtk_dialog_run(GTK_DIALOG(exe)) == GTK_RESPONSE_ACCEPT)
+  {
+    filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(exe));
+  }
+  gtk_widget_destroy(exe);
+
+  return filename;
+}
+
 void global_selected(GtkTreeSelection *treeselection, guidata *gui)
 {
   GtkTreeIter iter;
@@ -299,59 +335,6 @@ int main(int argc, char **argv)
   gtk_init(&argc, &argv);
   printf("[Mednaffe] Starting Mednaffe 0.7...\n");
 
-  /* Search for mednafen executable */
-  gui.binpath = g_find_program_in_path("mednafen");
-  if (gui.binpath==NULL)
-  {
-    show_error(
-      "Error: Mednafen executable is not installed in path!\n");
-    return 1;
-  }
-
-  /* Search for HOME variable */
-
-  #ifdef G_OS_WIN32
-    home = g_path_get_dirname(gui.binpath);
-  #else
-    home = g_getenv ("HOME");
-    if (!home) home = g_get_home_dir();
-  #endif
-    if (!home)
-    {
-      show_error("Error searching for home variable!\n");
-      return 1;
-    }
-
-  #ifdef G_OS_WIN32
-    gchar *path = g_strconcat(home, "\\stdout.txt", NULL);
-    gchar *qbin = g_strconcat("\"", gui.binpath, "\"", NULL);
-    gchar *cfg_path = g_strconcat(home, "\\mednafen-09x.cfg", NULL);
-
-    if ((g_file_get_contents(path, &stout, NULL, NULL)) && (g_file_test(cfg_path, G_FILE_TEST_IS_REGULAR)))
-    {}
-    else
-    {
-    system(qbin);
-      //Sleep(1000); /* race condition? */
-      g_file_get_contents(path, &stout, NULL, NULL);
-    }
-    g_free(cfg_path);
-    g_free(qbin);
-    g_free(path);
-
-  #else
-    g_spawn_command_line_sync(gui.binpath, &stout, NULL, NULL, NULL);
-    //sleep (1); /* race condition? */
-  #endif
-
-  /* Search mednafen configuration file */
-  gui.cfgfile = get_cfg(home);
-  if (!gui.cfgfile)
-  {
-    show_error("No mednafen configuration file found...\n");
-    return 1;
-  }
-
   /* Create new GtkBuilder objects */
   gui.builder = gtk_builder_new();
   if (!gtk_builder_add_from_string(gui.builder, mednaffe_glade, -1, NULL))
@@ -460,6 +443,7 @@ int main(int argc, char **argv)
   gui.state = 0;
   gui.executing = FALSE;
   gui.changed = FALSE;
+  gui.binpath = NULL;
   gui.fullpath = NULL;
   gui.rompath = NULL;
   gui.rom = NULL;
@@ -505,11 +489,75 @@ int main(int argc, char **argv)
 
   g_signal_connect(celltoggle, "toggled", G_CALLBACK(on_cell_toggled), &gui);
 
+  /* Load preferences */
+  load_prefs(&gui);
+
+ /* Search for mednafen executable */
+  if (gui.binpath==NULL)
+    gui.binpath = g_find_program_in_path("mednafen");
+  if (gui.binpath==NULL)
+  {
+    gui.binpath = show_chooser(
+      "Warning: Mednafen executable is not installed in path.\nDo you \
+want to select the file manually?\n");
+    if (gui.binpath==NULL)
+    {
+      show_error(
+      "Error: Mednafen executable not found.\n");
+      return 1;
+    }
+  }
+
+  /* Search for HOME variable */
+  #ifdef G_OS_WIN32
+    home = g_path_get_dirname(gui.binpath);
+  #else
+    home = g_getenv ("HOME");
+    if (!home) home = g_get_home_dir();
+  #endif
+    if (!home)
+    {
+      show_error("Error searching for home variable.\n");
+      return 1;
+    }
+
+  #ifdef G_OS_WIN32
+    gchar *path = g_strconcat(home, "\\stdout.txt", NULL);
+    gchar *qbin = g_strconcat("\"", gui.binpath, "\"", NULL);
+    gchar *cfg_path = g_strconcat(home, "\\mednafen-09x.cfg", NULL);
+
+    if ((g_file_get_contents(path, &stout, NULL, NULL)) && 
+        (g_file_test(cfg_path, G_FILE_TEST_IS_REGULAR)))
+    {}
+    else
+    {
+    system(qbin);
+      //Sleep(1000); /* race condition? */
+      g_file_get_contents(path, &stout, NULL, NULL);
+    }
+    g_free(cfg_path);
+    g_free(qbin);
+    g_free(path);
+
+  #else
+    g_spawn_command_line_sync(gui.binpath, &stout, NULL, NULL, NULL);
+    //sleep (1); /* race condition? */
+  #endif
+
+  /* Search mednafen configuration file */
+  gui.cfgfile = get_cfg(home);
+  if (!gui.cfgfile)
+  {
+    show_error("Error: No mednafen configuration file found.\n");
+    return 1;
+  }
+
   /* Check mednafen version */
   if (!check_version(stout, &gui))
    {
     show_error(
-    "Mednafen version is not compatible.\nYou need 0.9.36 version or above.\n");
+    "Error: Mednafen version is not compatible.\nYou need 0.9.36.2 \
+version or above.\n");
     return 1; /* Items are not freed here */
   }
   g_free(stout);
@@ -523,7 +571,6 @@ int main(int argc, char **argv)
   }
 
   /* Set values into gui */
-  load_prefs(&gui);
   set_values(gui.builder, &gui);
   set_values(gui.specific, &gui);
   select_rows(&gui);
diff --git a/src/prefs.c b/src/prefs.c
index be034a3..f9bb1e7 100644
--- a/src/prefs.c
+++ b/src/prefs.c
@@ -30,15 +30,15 @@ void save_combo(GKeyFile *key_file, guidata *gui)
   gchar **array;
   gboolean valid;
   gint i=0;
-  
+
   combostore = gtk_combo_box_get_model(GTK_COMBO_BOX(gui->cbpath));
   a_item = gtk_combo_box_get_active(GTK_COMBO_BOX(gui->cbpath));
   n_items = gtk_tree_model_iter_n_children(combostore, NULL);
 
   /*const gchar* array[n_items];*/
   array = g_new(gchar *, n_items+1);
-  array[n_items]=NULL; 
-  
+  array[n_items]=NULL;
+
   valid = gtk_tree_model_get_iter_first (combostore, &iter);
   while (valid)
   {
@@ -49,9 +49,9 @@ void save_combo(GKeyFile *key_file, guidata *gui)
 
   g_key_file_set_string_list(key_file, "GUI", "Folders",
                                         (const gchar **)array, n_items);
-                                        
+
   g_key_file_set_integer(key_file, "GUI", "Last Folder", a_item);
-  
+
   g_strfreev(array);
 }
 
@@ -62,7 +62,7 @@ void save_systems_showed(GKeyFile *key_file, guidata *gui)
   gboolean blist[13];
   gboolean valid;
   gint i=0;
-    
+
   model = GTK_TREE_MODEL(gtk_builder_get_object(gui->builder, "liststore3"));
   valid = gtk_tree_model_get_iter_first (model, &iter);
   while (valid)
@@ -78,20 +78,20 @@ void save_emu_options(GKeyFile *key_file, guidata *gui)
 {
   GList *list = NULL;
   GList *iterator = NULL;
-  
+
   list = g_hash_table_get_keys(gui->clist);
 
   for (iterator = list; iterator; iterator = iterator->next)
   {
     gchar *value;
-    
+
     iterator->data = ((gchar *)iterator->data)+1;
     value = g_strdup(g_hash_table_lookup(gui->hash, iterator->data));
     iterator->data = ((gchar *)iterator->data)-1;
     g_key_file_set_string(key_file, "Emulator", iterator->data, value);
     g_free(value);
   }
-  g_list_free(list); 
+  g_list_free(list);
 }
 
 void save_prefs(guidata *gui)
@@ -103,7 +103,7 @@ void save_prefs(guidata *gui)
   GtkWidget *option;
 
   #ifdef G_OS_WIN32
-    conf_file=g_strconcat(g_path_get_dirname(gui->binpath), "\\mednaffe.ini", NULL);
+    conf_file=g_strconcat(g_win32_get_package_installation_directory_of_module(NULL), "\\mednaffe.ini", NULL);
   #else
     conf_file=g_strconcat(g_get_user_config_dir(),"/mednaffe.conf", NULL);
   #endif
@@ -113,39 +113,41 @@ void save_prefs(guidata *gui)
 
   g_key_file_set_comment(key_file, NULL, NULL, " Version 0.7\n \
 Do not edit this file!", NULL);
-  
+
+  g_key_file_set_string(key_file, "GUI", "Bin", gui->binpath);
+
   save_combo(key_file, gui);
-                     
-  option = GTK_WIDGET(gtk_builder_get_object(gui->settings,"showtooltips"));         
+
+  option = GTK_WIDGET(gtk_builder_get_object(gui->settings,"showtooltips"));
   g_key_file_set_boolean(key_file, "GUI", "Tooltips",
                          gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(option)));
-                         
-  option = GTK_WIDGET(gtk_builder_get_object(gui->settings,"remembersize"));                         
+
+  option = GTK_WIDGET(gtk_builder_get_object(gui->settings,"remembersize"));
   g_key_file_set_boolean(key_file, "GUI", "RememberSize",
-                         gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(option))); 
-  if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(option))) 
+                         gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(option)));
+  if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(option)))
+  {
+  gint width;
+  gint height;
+
+  gtk_window_get_size(GTK_WINDOW(gui->topwindow), &width, &height);
+  if (width && height)
   {
-	gint width;
-	gint height;
-	  
-	gtk_window_get_size(GTK_WINDOW(gui->topwindow), &width, &height);
-	if (width && height)
-	{
-      g_key_file_set_integer(key_file, "GUI", "Width", width);                   
+      g_key_file_set_integer(key_file, "GUI", "Width", width);
       g_key_file_set_integer(key_file, "GUI", "Height", height);
     }
-  } 
-	                     
-  g_key_file_set_integer(key_file, "GUI", "Filter", gui->filter);                   
-  g_key_file_set_integer(key_file, "GUI", "View Mode", gui->listmode);                     
+  }
+
+  g_key_file_set_integer(key_file, "GUI", "Filter", gui->filter);
+  g_key_file_set_integer(key_file, "GUI", "View Mode", gui->listmode);
   g_key_file_set_integer(key_file, "GUI", "ActionLaunch", gui->state);
 
-  if (gtk_tree_view_column_get_sort_order(gui->column) == GTK_SORT_DESCENDING)                                  
+  if (gtk_tree_view_column_get_sort_order(gui->column) == GTK_SORT_DESCENDING)
     g_key_file_set_boolean(key_file, "GUI", "Reverse Sort", TRUE);
 
   save_systems_showed(key_file, gui);
   save_emu_options(key_file, gui);
-  
+
   conf = g_key_file_to_data(key_file, NULL, NULL);
 
   file=fopen(conf_file, "w");
@@ -160,16 +162,16 @@ Do not edit this file!", NULL);
 void load_combo(GKeyFile *key_file, guidata *gui)
 {
   GtkTreeModel *combostore;
-  GtkTreeIter iter; 
+  GtkTreeIter iter;
   gsize n_items = 0;
   gint a_item;
   gchar **folders = NULL;
-  
+
   combostore = gtk_combo_box_get_model(GTK_COMBO_BOX(gui->cbpath));
 
   folders = g_key_file_get_string_list(key_file, "GUI", "Folders",
                                                         &n_items, NULL);
-                                                        
+
   a_item = g_key_file_get_integer(key_file, "GUI", "Last Folder", NULL);
 
   if (folders!=NULL)
@@ -181,9 +183,9 @@ void load_combo(GKeyFile *key_file, guidata *gui)
       gtk_list_store_set(GTK_LIST_STORE(combostore), &iter, 0, folders[n_items], -1);
     }
   }
-  
+
   gtk_combo_box_set_active(GTK_COMBO_BOX(gui->cbpath), a_item);
-    
+
   g_strfreev(folders);
 }
 
@@ -193,7 +195,7 @@ void load_systems_showed(GKeyFile *key_file, guidata *gui)
   gboolean *showed;
   gsize length = 0;
   gint n_items = 0;
-  
+
   showed = g_key_file_get_boolean_list(key_file, "GUI", "Show Systems",
                                                         &length, NULL);
   if (showed)
@@ -201,10 +203,10 @@ void load_systems_showed(GKeyFile *key_file, guidata *gui)
     GtkListStore *store;
     GtkTreeModel *model;
 
-    store = GTK_LIST_STORE(gtk_builder_get_object(gui->builder, "liststore3"));                                                    
+    store = GTK_LIST_STORE(gtk_builder_get_object(gui->builder, "liststore3"));
     model = gtk_tree_view_get_model(GTK_TREE_VIEW(gtk_builder_get_object(
                                           gui->settings, "se_treeview")));
-                                          
+
     gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
     gtk_tree_model_get_iter_first(model, &iter2);
     while (n_items<14)
@@ -223,7 +225,7 @@ void load_emu_options(GKeyFile *key_file, guidata *gui)
 {
   gchar **ffekeys;
   gsize length = 0;
-  
+
   ffekeys = g_key_file_get_keys(key_file, "Emulator", &length, NULL);
 
   if (length>0)
@@ -236,7 +238,7 @@ void load_emu_options(GKeyFile *key_file, guidata *gui)
 
       ffecopy = g_strdup((ffekeys[i])+1);
       g_hash_table_insert(gui->hash, ffecopy,
-                          g_key_file_get_string(key_file, "Emulator", 
+                          g_key_file_get_string(key_file, "Emulator",
                           ffekeys[i], NULL));
 
       g_hash_table_replace(gui->clist, ffekeys[i], ffekeys[i]);
@@ -248,14 +250,14 @@ void load_emu_options(GKeyFile *key_file, guidata *gui)
 
 void load_prefs(guidata *gui)
 {
-  gchar *conf_file;  
+  gchar *conf_file;
   GKeyFile *key_file;
   gboolean value;
   gint state;
   GError *err = NULL;
 
   #ifdef G_OS_WIN32
-    conf_file=g_strconcat(g_path_get_dirname(gui->binpath), "\\mednaffe.ini", NULL);
+    conf_file=g_strconcat(g_win32_get_package_installation_directory_of_module(NULL), "\\mednaffe.ini", NULL);
   #else
     conf_file=g_strconcat(g_get_user_config_dir(), "/mednaffe.conf", NULL);
   #endif
@@ -265,13 +267,16 @@ void load_prefs(guidata *gui)
 
   if (g_key_file_load_from_file(key_file, conf_file,
                                 G_KEY_FILE_NONE, NULL))
-  {  
-	load_combo(key_file, gui);                            
+  {
+    gui->binpath = g_key_file_get_string(key_file, "GUI", "Bin", NULL);
+
+    load_combo(key_file, gui);
+
     value = g_key_file_get_boolean(key_file, "GUI", "Reverse Sort", &err);
-    
+
      if (err==NULL)
      {
-      if (value) 
+      if (value)
         gtk_tree_view_column_clicked(gui->column);
      }
     else
@@ -279,12 +284,12 @@ void load_prefs(guidata *gui)
       g_error_free (err);
       err=NULL;
     }
-    
+
     GtkWidget *option;
-    
+
     option = GTK_WIDGET(gtk_builder_get_object(gui->settings,
-                                               "showtooltips"));      
-    value = g_key_file_get_boolean(key_file, "GUI", "Tooltips", &err);    
+                                               "showtooltips"));
+    value = g_key_file_get_boolean(key_file, "GUI", "Tooltips", &err);
     if (err==NULL)
       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(option),value);
     else
@@ -292,10 +297,10 @@ void load_prefs(guidata *gui)
       g_error_free (err);
       err=NULL;
     }
-    
+
     option = GTK_WIDGET(gtk_builder_get_object(gui->settings,
-                                               "remembersize"));    
-    value = g_key_file_get_boolean(key_file, "GUI", "RememberSize", &err); 
+                                               "remembersize"));
+    value = g_key_file_get_boolean(key_file, "GUI", "RememberSize", &err);
     if (err==NULL)
       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(option),value);
     else
@@ -305,34 +310,34 @@ void load_prefs(guidata *gui)
     }
     if (value)
     {
-	  gint width;
-	  gint height;
-	  
-	  width = g_key_file_get_integer(key_file, "GUI", "Width", NULL);
-	  height = g_key_file_get_integer(key_file, "GUI", "Height", NULL);
-	  
-	  if (width && height)
-	    gtk_window_resize(GTK_WINDOW(gui->topwindow), width, height);
-	}
+    gint width;
+    gint height;
+
+    width = g_key_file_get_integer(key_file, "GUI", "Width", NULL);
+    height = g_key_file_get_integer(key_file, "GUI", "Height", NULL);
+
+    if (width && height)
+      gtk_window_resize(GTK_WINDOW(gui->topwindow), width, height);
+  }
 
     state=g_key_file_get_integer(key_file, "GUI", "Filter", NULL);
 
     switch (state)
     {
       case 1:
-        option = GTK_WIDGET(gtk_builder_get_object(gui->builder, "radiomenuzip"));            
+        option = GTK_WIDGET(gtk_builder_get_object(gui->builder, "radiomenuzip"));
         gtk_menu_item_activate (GTK_MENU_ITEM(option));
       break;
-      
+
       case 2:
-        option = GTK_WIDGET(gtk_builder_get_object(gui->builder, "radiomenucue"));            
+        option = GTK_WIDGET(gtk_builder_get_object(gui->builder, "radiomenucue"));
         gtk_menu_item_activate (GTK_MENU_ITEM(option));
       break;
-      
+
       default:
       break;
     }
-    
+
     state=g_key_file_get_integer(key_file, "GUI", "ActionLaunch", NULL);
 
     switch (state)
@@ -341,29 +346,29 @@ void load_prefs(guidata *gui)
         option = GTK_WIDGET(gtk_builder_get_object(gui->settings, "rbhide"));
         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(option), TRUE);
       break;
-      
+
       case 1:
-        option = GTK_WIDGET(gtk_builder_get_object(gui->settings, "rbminimize"));            
+        option = GTK_WIDGET(gtk_builder_get_object(gui->settings, "rbminimize"));
         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(option), TRUE);
       break;
-      
+
       default:
       break;
     }
-    
+
     state=g_key_file_get_integer(key_file, "GUI", "View Mode", NULL);
 
     switch (state)
     {
       case 1:
-        option = GTK_WIDGET(gtk_builder_get_object(gui->builder, "recursivemenu"));            
+        option = GTK_WIDGET(gtk_builder_get_object(gui->builder, "recursivemenu"));
         gtk_menu_item_activate (GTK_MENU_ITEM(option));
       break;
-      
+
       default:
       break;
     }
-    
+
     load_systems_showed(key_file, gui);
     load_emu_options(key_file, gui);
   }
diff --git a/src/prefs.h b/src/prefs.h
index a686fb5..5cfb2cd 100644
--- a/src/prefs.h
+++ b/src/prefs.h
@@ -1,25 +1,25 @@
 /*
  * prefs.h
- * 
+ *
  * Copyright 2013-2015 AmatCoder
- * 
+ *
  * This file is part of Mednaffe.
- * 
+ *
  * Mednaffe is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
- * 
+ *
  * Mednaffe is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with Mednaffe; if not, see <http://www.gnu.org/licenses/>.
- * 
+ *
  */
- 
+
 #ifndef PREFS_H
 #define PREFS_H
 
diff --git a/src/toggles.c b/src/toggles.c
index 714e9c3..d2da66b 100644
--- a/src/toggles.c
+++ b/src/toggles.c
@@ -379,6 +379,9 @@ gboolean check_version(gchar *stout, guidata *gui)
 
     GtkStatusbar *sbversion = GTK_STATUSBAR(gtk_builder_get_object(gui->builder, "sbversion"));
     gtk_statusbar_push(GTK_STATUSBAR(sbversion), 1, version);
+
+    gtk_widget_set_tooltip_text(GTK_WIDGET(sbversion), gui->binpath);
+
     g_strfreev(achar);
     g_strfreev(aline);
     g_free(version);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-games/mednaffe.git



More information about the Pkg-games-commits mailing list