Add utility to attach console under windows windows-port
authorKevin Wasserman <krwasserman@hotmail.com>
Mon, 4 May 2015 17:14:16 +0000 (13:14 -0400)
committerKevin Wasserman <krwasserman@hotmail.com>
Tue, 5 May 2015 20:36:25 +0000 (16:36 -0400)
src/moonshot-futils.c
src/moonshot-identity-manager-app.vala
src/moonshot-utils.vala
vapi/moonshot-msrpc.vapi

index 17e890d..a7fde4a 100644 (file)
 #include <sys/types.h>
 #include <pwd.h>
 #endif
+#ifdef OS_WIN32
+#include <io.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <windows.h>
+#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
+
index a013cfb..4efd416 100644 (file)
@@ -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);
index 72df968..d95d539 100644 (file)
@@ -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) {
index c464676..bef2d3b 100644 (file)
@@ -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();