From efd11b3fa7c723f33c5cb177e976193416d9026a Mon Sep 17 00:00:00 2001 From: Kevin Wasserman Date: Mon, 4 May 2015 13:14:16 -0400 Subject: [PATCH 1/1] Add utility to attach console under windows --- src/moonshot-futils.c | 50 +++++++++++++++++++++++++++++++++- src/moonshot-identity-manager-app.vala | 14 ++++++++++ src/moonshot-utils.vala | 4 +-- vapi/moonshot-msrpc.vapi | 3 ++ 4 files changed, 68 insertions(+), 3 deletions(-) diff --git a/src/moonshot-futils.c b/src/moonshot-futils.c index 17e890d..a7fde4a 100644 --- a/src/moonshot-futils.c +++ b/src/moonshot-futils.c @@ -34,8 +34,14 @@ #include #include #endif +#ifdef OS_WIN32 +#include +#include +#include +#include +#endif -const char * GetUserName() +const char * MoonshotGetUserName() { #ifdef HAVE_GETPWUID struct passwd *pwd = getpwuid(getuid()); @@ -49,3 +55,45 @@ const char * GetFlatStoreUsersFilePath() { return MOONSHOT_FLATSTORE_USERS; } + +#ifdef OS_WIN32 +static int con_to_std(DWORD std_id, FILE *fp_out) +{ + HANDLE con_handle; + int fd; + FILE *fp; + con_handle = GetStdHandle(std_id); + if (con_handle == INVALID_HANDLE_VALUE) + return 0; + fd = _open_osfhandle((intptr_t)con_handle, _O_TEXT); + if (fd == -1) + return 0; + fp = _fdopen(fd, "w" ); + if (fp == NULL) + return 0; + *fp_out = *fp; + setvbuf(fp_out, NULL, _IONBF, 0 ); + return 1; +} + +int moonshot_attach_console() +{ +#if 0 + if (AttachConsole(ATTACH_PARENT_PROCESS) != 0) { + if (con_to_std(STD_OUTPUT_HANDLE, stdout) && + con_to_std(STD_ERROR_HANDLE, stderr)) { + return 1; + } + } +#endif + if (AllocConsole() && AttachConsole(GetCurrentProcessId())) { + if (con_to_std(STD_OUTPUT_HANDLE, stdout) && + con_to_std(STD_ERROR_HANDLE, stderr)) { + return 1; + } + } + + return 0; +} +#endif + diff --git a/src/moonshot-identity-manager-app.vala b/src/moonshot-identity-manager-app.vala index a013cfb..4efd416 100644 --- a/src/moonshot-identity-manager-app.vala +++ b/src/moonshot-identity-manager-app.vala @@ -345,7 +345,10 @@ public class IdentityManagerApp { static bool explicitly_launched = true; static bool use_flat_file_store = false; +static bool attach_console = false; const GLib.OptionEntry[] options = { + {"attach-console",0,0,GLib.OptionArg.NONE, + ref attach_console,"attach console",null}, {"dbus-launched",0,GLib.OptionFlags.REVERSE,GLib.OptionArg.NONE, ref explicitly_launched,"launch for dbus rpc use",null}, {"flat-file-store",0,0,GLib.OptionArg.NONE, @@ -353,6 +356,7 @@ const GLib.OptionEntry[] options = { {null} }; +public extern int moonshot_attach_console(); public static int main(string[] args){ #if IPC_MSRPC @@ -392,6 +396,16 @@ public static int main(string[] args){ Gtk.Settings settings = Gtk.Settings.get_default (); settings.set_string_property ("gtk-theme-name", "ms-windows", "moonshot"); settings.set_long_property ("gtk-menu-images", 0, "moonshot"); + if (attach_console) { + if (moonshot_attach_console() != 0) { + stdout.printf(_("Attached console\n")); + stdout.flush(); + } + } + else { + stdout.printf("Standard output\n"); + stdout.flush(); + } #endif Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR); diff --git a/src/moonshot-utils.vala b/src/moonshot-utils.vala index 72df968..d95d539 100644 --- a/src/moonshot-utils.vala +++ b/src/moonshot-utils.vala @@ -81,12 +81,12 @@ public Gdk.Pixbuf? find_icon (string name, int size) } } -public extern unowned string GetUserName(); +public extern unowned string MoonshotGetUserName(); public extern unowned string GetFlatStoreUsersFilePath(); public bool UserForcesFlatFileStore() { - string username = GetUserName(); + string username = MoonshotGetUserName(); string flatstore_users_filename = GetFlatStoreUsersFilePath(); FileStream flatstore_users = FileStream.open(flatstore_users_filename, "r"); if (flatstore_users == null) { diff --git a/vapi/moonshot-msrpc.vapi b/vapi/moonshot-msrpc.vapi index c464676..bef2d3b 100644 --- a/vapi/moonshot-msrpc.vapi +++ b/vapi/moonshot-msrpc.vapi @@ -16,6 +16,9 @@ namespace MoonshotRpcInterface { string always_confirm; } + [CCode (cname = "moonshot_attach_console")] + public extern uint32 attach_console(); + [CCode (cname = "c_moonshot_show_ui_rpc")] public extern void show_ui(); -- 2.1.4