OSDN Git Service

MacGui: Fix how we get the current instances pid number since NSRunningApplication...
authordynaflash <dynaflash@b64f7644-9d1e-0410-96f1-a4d463321fa5>
Mon, 19 Jul 2010 17:02:43 +0000 (17:02 +0000)
committerdynaflash <dynaflash@b64f7644-9d1e-0410-96f1-a4d463321fa5>
Mon, 19 Jul 2010 17:02:43 +0000 (17:02 +0000)
- Many thanks to ritsuka for pointing this out.
- Also adds ability to get the full path to each running instance if we need it in the future.
- Note: Adds additional logging to hbInstances which can be removed when we see fit. But for now imho could be useful since we are early in multi-instance queue encoding.

git-svn-id: svn://localhost/HandBrake/trunk@3450 b64f7644-9d1e-0410-96f1-a4d463321fa5

macosx/Controller.h
macosx/Controller.m
macosx/HBQueueController.h
macosx/HBQueueController.mm

index 662563e..b8cebbb 100644 (file)
@@ -265,7 +265,8 @@ BOOL                        fIsDragging;
     hb_handle_t                  * fHandle;
     
     /* Queue variables */
-    hb_handle_t              * fQueueEncodeLibhb;           // libhb for HB Encoding
+    int                          hbInstanceNum; //stores the number of HandBrake instances currently running
+    hb_handle_t                  * fQueueEncodeLibhb;           // libhb for HB Encoding
        hb_title_t                   * fTitle;
     hb_title_t                   * fQueueEncodeTitle;
     int                          fEncodingQueueItem;     // corresponds to the index of fJobGroups encoding item
@@ -291,7 +292,6 @@ BOOL                        fIsDragging;
     double                         dockIconProgress;
 }
 
-- (int) getThisHBInstancePID;
 - (IBAction) showAboutPanel:(id)sender;
 
 - (void) writeToActivityLog:(const char *) format, ...;
index 66d7674..d9122b5 100644 (file)
@@ -86,9 +86,9 @@ static NSString *        ChooseSourceIdentifier             = @"Choose Source It
     [self writeToActivityLog: "%s", [versionStringFull UTF8String]];
     
     /* Get the PID number for this hb instance, used in multi instance encoding */
-    pidNum = [self getThisHBInstancePID];
+    //pidNum = [self getThisHBInstancePID];
     /* Report this pid to the activity log */
-    [self writeToActivityLog: "Pid for this instance:%d", pidNum];
+    //[self writeToActivityLog: "Pid for this instance:%d", pidNum];
     
     return self;
 }
@@ -130,6 +130,9 @@ static NSString *        ChooseSourceIdentifier             = @"Choose Source It
     
     /* Init QueueFile .plist */
     [self loadQueueFile];
+    /* Run hbInstances to get any info on other instances as well as set the
+     * pid number for this instance in the case of multi-instance encoding. */ 
+    hbInstanceNum = [self hbInstances];
     
     /* Call UpdateUI every 1/2 sec */
     
@@ -177,7 +180,7 @@ static NSString *        ChooseSourceIdentifier             = @"Choose Source It
         /* We check to see if there is already another instance of hb running.
          * Note: hbInstances == 1 means we are the only instance of HandBrake.app
          */
-        if ([self hbInstances] > 1)
+        if (hbInstanceNum > 1)
         {
         alertTitle = [NSString stringWithFormat:
                          NSLocalizedString(@"There is already an instance of HandBrake running.", @"")];
@@ -234,28 +237,45 @@ static NSString *        ChooseSourceIdentifier             = @"Choose Source It
 
 #pragma mark -
 #pragma mark Multiple Instances
+
+/* hbInstances checks to see if other instances of HB are running and also sets the pid for this instance for multi-instance queue encoding */
+ /* Note for now since we are in early phases of multi-instance I have put in quite a bit of logging. Can be removed as we see fit. */
 - (int) hbInstances
 {
     /* check to see if another instance of HandBrake.app is running */
     NSArray *runningAppDictionaries = [[NSWorkspace sharedWorkspace] launchedApplications];
     NSDictionary *runningAppsDictionary;
     int hbInstances = 0;
+    NSString * thisInstanceAppPath = [[NSBundle mainBundle] bundlePath];
+    NSString * runningInstanceAppPath;
+    int runningInstancePidNum;
+    [self writeToActivityLog: "hbInstances path to this instance: %s", [thisInstanceAppPath UTF8String]];
     for (runningAppsDictionary in runningAppDictionaries)
        {
         if ([[runningAppsDictionary valueForKey:@"NSApplicationName"] isEqualToString:@"HandBrake"])
                {
             hbInstances++;
+            /*Report the path to each active instances app path */
+            runningInstancePidNum = [[runningAppsDictionary valueForKey:@"NSApplicationProcessIdentifier"] intValue];
+            runningInstanceAppPath = [runningAppsDictionary valueForKey:@"NSApplicationPath"];
+            [self writeToActivityLog: "hbInstance found instance pidnum:%d at path: %s", runningInstancePidNum, [runningInstanceAppPath UTF8String]];
+            /* see if this is us by comparing the app path */
+            if ([runningInstanceAppPath isEqualToString: thisInstanceAppPath])
+            {
+                /* If so this is our pidnum */
+                [self writeToActivityLog: "hbInstance MATCH FOUND, our pidnum is:%d", runningInstancePidNum];
+                /* Get the PID number for this hb instance, used in multi instance encoding */
+                pidNum = runningInstancePidNum;
+                /* Report this pid to the activity log */
+                [self writeToActivityLog: "Pid for this instance:%d", pidNum];
+                /* Tell fQueueController what our pidNum is */
+                [fQueueController setPidNum:pidNum];
+            }
                }
        }
     return hbInstances;
 }
-/* Gets the Process Identifer (PID) for this instance and returns it as an integer */
-- (int) getThisHBInstancePID
-{
-    /* Get the PID of this HB instance */
-    int hbInstancePID = [[NSRunningApplication currentApplication] processIdentifier];
-    return hbInstancePID;
-}
 
 #pragma mark -
 
@@ -1934,7 +1954,7 @@ static NSString *        ChooseSourceIdentifier             = @"Choose Source It
     else
     {
         /* ONLY clear out encoded items if we are single instance */
-        if ([self hbInstances] == 1)
+        if (hbInstanceNum == 1)
         {
             [self clearQueueEncodedItems];
         }
index 8f1268e..f08415f 100644 (file)
@@ -107,6 +107,7 @@ BOOL                        fIsDragging;
 #endif
 
 }
+- (void)setPidNum: (int)myPidnum;
 - (void)setHandle: (hb_handle_t *)handle;
 - (void)setHBController: (HBController *)controller;
 
index 451d96e..09719e9 100644 (file)
@@ -226,8 +226,11 @@ static NSString*    HBQueuePauseResumeToolbarIdentifier       = @"HBQueuePauseRe
 - (void)setHBController: (HBController *)controller
 {
     fHBController = controller;
-    /* Now get this pidnum from HBController */
-    pidNum = [fHBController getThisHBInstancePID];
+}
+
+- (void)setPidNum: (int)myPidnum
+{
+    pidNum = myPidnum;
     [fHBController writeToActivityLog: "HBQueueController : My Pidnum is %d", pidNum];
 }