[tasque/viewmodel: 25/78] Move "set process title" fix to Program.cs



commit 072818328e6cc44c545539be2d9336a0b197b305
Author: Antonius Riha <antoniusriha gmail com>
Date:   Sat Jul 28 22:58:59 2012 +0200

    Move "set process title" fix to Program.cs
    
    Also got the latest version of this fix from banshee.

 src/Tasque.Gtk/GnomeApplication.cs |   24 ------------------------
 src/tasque/Program.cs              |   31 +++++++++++++++++++++++++++++++
 src/tasque/tasque.csproj           |    1 +
 3 files changed, 32 insertions(+), 24 deletions(-)
---
diff --git a/src/Tasque.Gtk/GnomeApplication.cs b/src/Tasque.Gtk/GnomeApplication.cs
index 6578904..6874630 100644
--- a/src/Tasque.Gtk/GnomeApplication.cs
+++ b/src/Tasque.Gtk/GnomeApplication.cs
@@ -6,7 +6,6 @@ using System.IO;
 using System.Xml;
 
 using Mono.Unix;
-using Mono.Unix.Native;
 
 namespace Tasque
 {
@@ -28,9 +27,6 @@ namespace Tasque
 		public override void Initialize (string [] args)
 		{
 			Mono.Unix.Catalog.Init ("tasque", GlobalDefines.LocaleDir);
-			try {
-				SetProcessName ("Tasque");
-			} catch {} // Ignore exception if fail (not needed to run)
 
 			Gtk.Application.Init ();
 			program = new Gnome.Program ("Tasque",
@@ -48,26 +44,6 @@ namespace Tasque
 		{
 			Gtk.Main.Quit ();
 		}
-
-		[DllImport("libc")]
-		private static extern int prctl (int option,
-			                                 byte [] arg2,
-			                                 IntPtr arg3,
-			                                 IntPtr arg4,
-			                                 IntPtr arg5);
-
-		// From Banshee: Banshee.Base/Utilities.cs
-		private void SetProcessName (string name)
-		{
-			if (prctl (15 /* PR_SET_NAME */,
-			                Encoding.ASCII.GetBytes (name + "\0"),
-			                IntPtr.Zero,
-			                IntPtr.Zero,
-			                IntPtr.Zero) != 0)
-				throw new ApplicationException (
-				        "Error setting process name: " +
-				        Mono.Unix.Native.Stdlib.GetLastError ());
-		}
 		
 		public override void OpenUrlInBrowser (string url)
 		{
diff --git a/src/tasque/Program.cs b/src/tasque/Program.cs
index ba149d4..0ba8262 100644
--- a/src/tasque/Program.cs
+++ b/src/tasque/Program.cs
@@ -25,6 +25,9 @@
 // THE SOFTWARE.
 using System;
 using System.Diagnostics;
+using System.Runtime.InteropServices;
+using System.Text;
+using Mono.Unix.Native;
 using Tasque.UIModel.Legacy;
 
 namespace Tasque
@@ -42,6 +45,31 @@ namespace Tasque
 			return app;
 		}
 		
+		#region Set process name
+		// From Banshee: Hyena/ApplicationContext.cs
+		[DllImport ("libc")] // Linux
+        static extern int prctl (int option, byte [] arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5);
+
+        [DllImport ("libc")] // BSD
+        static extern void setproctitle (byte [] fmt, byte [] str_arg);
+
+        static void SetProcessName (string name)
+        {
+            if (Environment.OSVersion.Platform != PlatformID.Unix)
+                return;
+			
+			try {
+				var retVal = prctl (15 /* PR_SET_NAME */, Encoding.ASCII.GetBytes (name + "\0"),
+				                    IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
+				if (retVal != 0)
+					throw new ApplicationException ("Error setting process name: " +
+                        Stdlib.GetLastError ());
+            } catch (EntryPointNotFoundException) {
+                setproctitle (Encoding.ASCII.GetBytes ("%s\0"), Encoding.ASCII.GetBytes (name + "\0"));
+            }
+        }
+		#endregion
+		
 		static void SetupDebugAndTrace ()
 		{
 			var consoleTraceListener = new ConsoleTraceListener ();
@@ -63,6 +91,9 @@ namespace Tasque
 				
 				SetupDebugAndTrace ();
 				
+				// Fix process name not set on Unix
+				SetProcessName ("Tasque");
+				
 				application.Initialize (args);
 				application.StartMainLoop ();
 			} catch (Exception e) {
diff --git a/src/tasque/tasque.csproj b/src/tasque/tasque.csproj
index 6c14221..6d4f979 100644
--- a/src/tasque/tasque.csproj
+++ b/src/tasque/tasque.csproj
@@ -82,6 +82,7 @@
   </PropertyGroup>
   <ItemGroup>
     <Reference Include="System" />
+    <Reference Include="Mono.Posix" />
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Properties\AssemblyInfo.cs" />



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]