The question I have could be titled "How to communicate between screensaver 
threads?"
Subtitle: "Are screensaver threads really threads?"

Let me please explain the scenario (sorry for the lengthy question).

I've got a screensaver that displays images.
With two monitors, OSX starts two threads of my screensaver by creating two 
instances of my class and then calling 'animateOneFrame' for each of the 
instances in concurrently running loops (or so I thought!).

Now, the problem occurs because I want each instance to show the image in 
Finder when the user hits the return key.

Here is an outline of my code:

- (void) animateOneFrame
{
    if ( ! we_are_on_main_screen_ )
    {
        if ( [KeyEventHappened objectForKey: myDisplayNumber_] )
        {
            [self keyDown: [KeyEventHappened objectForKey: myDisplayNumber_] ];
            [KeyEventHappened removeObjectForKey: myDisplayNumber_];

        }
    }
    ...
}

- (void) keyDown: (NSEvent *) theEvent 
{
    if ( we_are_on_main_screen_ && KeyEventHappened )
        for ( NSNumber * n in allScreenIDs_ )           
            [KeyEventHappened setObject: theEvent forKey: n];
    if ( [[theEvent characters] isEqual: @"\r"] && filename_ )
    {
        ... // show image in finder
        while ( [KeyEventHappened count] > 1 )         // wait for the other 
screensaver instances to process the event
            usleep( 100*1000 );
    }
    [super keyDown: theEvent];    // will quit the screen saver, if not in 
preview mode (System Preferences),
}


So, this is a simple producer-consumer model.

Here, KeyEventHappened is a global dictionary that I declared as a static 
global variable.
And we_are_on_main_screen_ is an instance variable that is true for the 
instance which is running on the main screen.
(Apparently, only the screensaver instance on the main screen gets any keyboard 
events.)


The problem now seems to be that as soon as the first instance is in the 
while-loop waiting for the other instances,
'animateOneFrame' is never called for the *other* instance!

I have found that basically by sprinkling the code with lots of printf's (err, 
asl_log's).

Big question: is that really true?!

Small question: is there an elegant way to solve this?

All insights and suggestions will be highly appreciated.

Best regards,
Gabriel.

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to