spek

Acoustic spectrum analyser
git clone http://git.hanabi.in/repos/spek.git
Log | Files | Refs | README

commit 95788c11e3eedc689fa2648b57a1d6c53772408c
parent c9e4146536101d6af357a08042999d1de1ce8d4a
Author: Alexander Kojevnikov <alexander@kojevnikov.com>
Date:   Sun, 11 Jul 2010 14:25:36 +1000

[win] Fix Unicode arguments

Diffstat:
M.gitignore | 1+
Asrc/spek-platform.c | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/spek-platform.h | 3+++
Msrc/spek.vala | 11++++++++++-
Mvapi/spek-platform.vapi | 1+
5 files changed, 81 insertions(+), 1 deletion(-)

diff --git a/.gitignore b/.gitignore @@ -30,6 +30,7 @@ samples/ src/*.c !src/spek-audio.c !src/spek-fft.c +!src/spek-platform.c src/*.o src/*.stamp src/spek diff --git a/src/spek-platform.c b/src/spek-platform.c @@ -0,0 +1,66 @@ +/* spek-platform.c + * + * Copyright (C) 2010 Alexander Kojevnikov <alexander@kojevnikov.com> + * + * Spek 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. + * + * Spek 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 Spek. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <glib.h> +#include <gtk/gtk.h> + +#ifdef G_OS_WIN32 +#include <windows.h> +#include <shellapi.h> +#endif + +#include "spek-platform.h" + +void spek_platform_fix_args (gchar **argv, gint argc) { +#ifdef G_OS_WIN32 + /* Because MinGW does not support Unicode arguments we are going to + * get them using Windows API. In addition, GLib's option parser + * doesn't work well with utf-8 strings on Windows, converting + * them to URIs works around this problem. + */ + int i; + gchar *s, *t; + wchar_t **wargv; + int wargc; + wargv = CommandLineToArgvW (GetCommandLineW (), &wargc); + for (i = 0; i < argc; i++) { + s = g_utf16_to_utf8 (wargv[i], -1, NULL, NULL, NULL); + if (s) { + t = g_filename_to_uri (s, NULL, NULL); + g_free (s); + if (t) { + g_free (argv[i]); + argv[i] = t; + } + } + } + LocalFree (wargv); +#endif +} + +void spek_platform_show_uri (const gchar *uri) { +#ifdef G_OS_WIN32 + /* gtk_show_uri doesn't work on Windows */ + ShellExecuteA (NULL, "open", uri, "", NULL, SW_SHOWNORMAL); +#else + GError *error = NULL; + if (!gtk_show_uri (NULL, uri, gtk_get_current_event_time (), &error)) { + g_error_free (error); + } +#endif +} diff --git a/src/spek-platform.h b/src/spek-platform.h @@ -21,6 +21,9 @@ #include <glib.h> +/* Convert from UTF-16 to UTF-8 when running on Windows */ +void spek_platform_fix_args (gchar **argv, gint argc); + /* Open a link in the browser */ void spek_platform_show_uri (const gchar *uri); diff --git a/src/spek.vala b/src/spek.vala @@ -28,6 +28,8 @@ namespace Spek { }; int main (string[] args) { + Platform.fix_args (args); + Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR); Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8"); Intl.textdomain (Config.GETTEXT_PACKAGE); @@ -52,7 +54,14 @@ namespace Spek { } Audio.init (); - var window = new Window (files == null ? null : files[0]); + var file_name = files == null ? null : files[0]; + if (file_name != null && file_name.has_prefix ("file://")) { + try { + file_name = Filename.from_uri (file_name); + } catch (ConvertError e) { + } + } + var window = new Window (file_name); Gtk.main (); window.destroy (); return 0; diff --git a/vapi/spek-platform.vapi b/vapi/spek-platform.vapi @@ -1,4 +1,5 @@ [CCode (cprefix = "SpekPlatform", lower_case_cprefix = "spek_platform_", cheader_filename = "spek-platform.h")] namespace Spek.Platform { + public static void fix_args (string[] args); public static void show_uri (string uri); }