Can't get SWT Display on Mac OS X

前端 未结 4 1981
情歌与酒
情歌与酒 2021-02-06 07:50

I\'m running Mac OS X Snow Leopard and wan\'t to access the Display from the activator in an OSGi bundle.

Below is the start method for my activator:

@O         


        
4条回答
  •  天命终不由人
    2021-02-06 08:36

    I can confirm that we successfully run SWT Carbon on Mac OS X in its own event loop kicked off by a bundle activation, so it's definitely possible! This is using -XstartOnFirstThread when launching the VM.

    But, with Cocoa SWT (64-bit), I see the same error :(

    It seems that, although the way we ran Carbon SWT worked, it was probably not kosher: we were driving the event loop through another thread, not the main one as you're supposed to. Under Cocoa SWT, this doesn't work any more, and it was probably dodgy practice anyway.

    I can fix the thread pool errors with the following hack before creating the Display (adapted from the Cocoa SWT Device constructor):

      NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
      NSThread nsthread = NSThread.currentThread();
      NSMutableDictionary dictionary = nsthread.threadDictionary();
      NSString key = NSString.stringWith("SWT_NSAutoreleasePool");
      id obj = dictionary.objectForKey(key);
      if (obj == null) {
              NSNumber nsnumber = NSNumber.numberWithInteger(pool.id);
              dictionary.setObject(nsnumber, key);
      } else {
              pool.release();
      }
    

    However, the event loop that follows hangs (i.e. the display.readAndDispatch ()/display.sleep () dance). I suspect it's just not reading UI events due not being the main thread.

    I'm not sure if there's a kosher way to fix this. In my case, we control the main JVM thread that launches OSGi, so I'm toying with the idea of adding a hook in there that can run the SWT event loop after OSGi launch.

提交回复
热议问题