XBoard for Mac: Zippy problems

Discussion of chess software programming and technical issues.

Moderators: hgm, Dann Corbit, Harvey Williamson

User avatar
hgm
Posts: 27701
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: MacPorts and XQuartz

Post by hgm »

No hurry. I just would like to know what is written on the log file for various XBoard commands with many arguments.
JoshPettus
Posts: 730
Joined: Fri Oct 19, 2012 2:23 am

Re: MacPorts and XQuartz

Post by JoshPettus »

Sorry it took me a little while, kept making little mistakes. Fortunately even though the build script doesn't make a full app, it makes enough for the test.

Code: Select all

-icsUp true -ics -icshost freechess.org
got me:

Code: Select all

3842: start new, suppress = 0, path='freechess.org'
3842: new instance, argc=6 argv='-icsUp'
User avatar
hgm
Posts: 27701
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: MacPorts and XQuartz

Post by hgm »

JoshPettus wrote:Sorry it took me a little while, kept making little mistakes. Fortunately even though the build script doesn't make a full app, it makes enough for the test.

Code: Select all

-icsUp true -ics -icshost freechess.org
got me:

Code: Select all

3842: start new, suppress = 0, path='freechess.org'
3842: new instance, argc=6 argv='-icsUp'
Is that all? This is the output from one instance (process ID 3842). But it seems to receive an OpenFile signal while suppress=0, so I think it should have launched at least a second instance. Apparently this is an instance that does not print to the log yet.

But this already confirms what we thought: the fact that the instance you started from the terminal gets its full command line (argc=6) is apparently no guarantee that it will not get a spurious OpenFile signal.

The new instance that is launched because of that would be invoked as "xboard freechess.org", and might or might not also receive the OpenFile signal with path=freechess.org. But because the first argument now does not start with '-' we programmed it to ignore that OpenFile signal, by setting suppress=1. So whether it gets it or not does not matter; it will just try to open freechess.org as loadGameFile (which of course will fail miserably).

Now I am curious what happens if you use -ncp as first argument?
JoshPettus
Posts: 730
Joined: Fri Oct 19, 2012 2:23 am

Re: MacPorts and XQuartz

Post by JoshPettus »

Yah, sorry It did launch 2 instances. thought I mentioned that

really strange..

Code: Select all

-ncp -icsUp true -ics -icshost freechess.org
got 3 instances.

Code: Select all

1071: start new, suppress = 0, path='true'
1071: start new, suppress = 0, path='freechess.org'
1071: new instance, argc=7 argv='-ncp'
yet

Code: Select all

-ncp -ics -icshost freechess.org icsup true 
only gets the one instance launching freechess.org

Code: Select all

1160: new instance, argc=7 argv='-ncp'
User avatar
hgm
Posts: 27701
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: MacPorts and XQuartz

Post by hgm »

This is all extremely weird OS X behavior. There really seems no way to reliably guess how many of these spurious OpenFile commands the system will send you. So I guess the solution is to just ignore them for some time, say 1 sec, in the hope that none of the spurious ones will arive later than that, and that anything arriving later is due to genuine file clicking.

The following code should do this:

Code: Select all

#ifdef OSXAPP
static char clickedFile[MSG_SIZ];
TimeMark started;

static gboolean
StartNewXBoard(GtkosxApplication *app, gchar *path, gpointer user_data)
{ // handler of OSX OpenFile signal, which sends us the filename of clicked file or first argument
  TimeMark now;
  GetTimeMark(&now);
  if&#40;1000*now.sec + now.ms - 1000*started.sec - started.ms < 1000&#41; &#123; // received during first second
    strncpy&#40;clickedFile, path, MSG_SIZ&#41;; // remember file name, but otherwise ignore
  &#125; else &#123;       // we are running something presumably useful
    char buf&#91;MSG_SIZ&#93;;
    snprintf&#40;buf, MSG_SIZ, "open -n -a \"xboard\" --args \"%s\"", path&#41;;
    system&#40;buf&#41;; // start new instance on this file
  &#125;
  return TRUE;
&#125;

GtkosxApplication *theApp;
#endif

int
main &#40;int argc, char **argv&#41;
&#123;
    int i, clockFontPxlSize, coordFontPxlSize, fontPxlSize;
    int boardWidth, w, h; //, boardHeight;
    char *p;
    int forceMono = False;

    srandom&#40;time&#40;0&#41;); // &#91;HGM&#93; book&#58; make random truly random

    setbuf&#40;stdout, NULL&#41;;
    setbuf&#40;stderr, NULL&#41;;
    debugFP = stderr;

    if&#40;argc > 1 && (!strcmp&#40;argv&#91;1&#93;, "-v" ) || !strcmp&#40;argv&#91;1&#93;, "--version" ))) &#123;
        printf&#40;"%s version %s\n\n  configure options&#58; %s\n", PACKAGE_NAME, PACKAGE_VERSION, CONFIGURE_OPTIONS&#41;;
	exit&#40;0&#41;;
    &#125;

    if&#40;argc > 1 && !strcmp&#40;argv&#91;1&#93;, "--help" )) &#123;
	PrintOptions&#40;);
	exit&#40;0&#41;;
    &#125;

    /* set up GTK */
    gtk_init (&argc, &argv&#41;;
#ifdef OSXAPP
    &#123;   // prepare to catch OX OpenFile signal, which will tell us the clicked file
	char *path = gtkosx_application_get_bundle_path&#40;);
#ifdef ENABLE_NLS
	char *res_path = gtkosx_application_get_resource_path&#40;);
        GetTimeMark&#40;&started&#41;; // remember start time
	snprintf&#40;localeDir, MSG_SIZ, "%s/share/locale", res_path&#41;; // redefine locale dir for OSX bundle
#endif
	theApp = g_object_new&#40;GTKOSX_TYPE_APPLICATION, NULL&#41;;
	snprintf&#40;masterSettings, MSG_SIZ, "%s/Contents/Resources/etc/xboard.conf", path&#41;;
	snprintf&#40;dataDir, MSG_SIZ, "%s/Contents/Resources/share/xboard", path&#41;;
	snprintf&#40;svgDir, MSG_SIZ, "%s/themes/default", dataDir&#41;;
	g_signal_connect&#40;theApp, "NSApplicationOpenFile", G_CALLBACK&#40;StartNewXBoard&#41;, NULL&#41;;
	g_signal_connect&#40;theApp, "NSApplicationWillTerminate", G_CALLBACK&#40;ExitEvent&#41;, NULL&#41;;
	// we must call application ready before we can get the signal,
	// and supply a &#40;dummy&#41; menu bar before that, to avoid problems with dual apples in it
	gtkosx_application_set_menu_bar&#40;theApp, GTK_MENU_SHELL&#40;gtk_menu_bar_new&#40;)));
	gtkosx_application_ready&#40;theApp&#41;;
	if&#40;argc == 1&#41; &#123;                  // called without args&#58; OSX open-file signal might follow
	    static char *fakeArgv&#91;3&#93; = &#123;NULL, clickedFile, NULL&#125;;
	    usleep&#40;10000&#41;;               // wait 10 msec &#40;and hope this is long enough&#41;.
	    while&#40;gtk_events_pending&#40;))
		gtk_main_iteration&#40;);    // process all events that came in upto now
	    if&#40;clickedFile&#91;0&#93;) &#123;         // we were sent an open-file signal with filename!
	      fakeArgv&#91;0&#93; = argv&#91;0&#93;;
	      argc = 2; argv = fakeArgv; // fake that we were called as "xboard filename"
	    &#125;
	&#125;
    &#125;
#endif
JoshPettus
Posts: 730
Joined: Fri Oct 19, 2012 2:23 am

Re: MacPorts and XQuartz

Post by JoshPettus »

I don't get it.. I copied both #ifdef osxapp sections, but after compiling, it didn't work. It looks like it should, the extra xboards start up right along side the first one as fast as the computer can open them. I thought maybe my vmmachine was slow, so I upped it to 3 seconds, still no luck.
User avatar
hgm
Posts: 27701
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: MacPorts and XQuartz

Post by hgm »

So we should resort to printing again to see what is going on. You can replace

Code: Select all

  TimeMark now;
  GetTimeMark&#40;&now&#41;;
  if&#40;1000*now.sec + now.ms - 1000*started.sec - started.ms < 1000&#41; &#123; // received during first second
by

Code: Select all

  TimeMark now;
  int elapsedTime;
  GetTimeMark&#40;&now&#41;;
  elapsedTime = 1000*now.sec + now.ms - 1000*started.sec - started.ms;
&#123;FILE *f=fopen&#40;"FILENAME", "a"); fprintf&#40;f, "%d&#58; start new '%s', time=%d\n", getpid&#40;), path, elapsedTime&#41;; fclose&#40;f&#41;; &#125;
  if&#40;elapsedTime < 1000&#41; &#123; // received during first second
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: MacPorts and XQuartz

Post by sje »

sje wrote:MacPorts: port Unix applications to Mac OS/X
Seven versions for Mac OS/X 10.4 Tiger to OS/X 10.10 Yosemite
https://www.macports.org/
https://distfiles.macports.org/MacPorts ... semite.pkg

XQuartz: X Windows server for recent versions of Mac OS/X
Single version supports OS/X 10.6 Snow Leopard to OS/X 10.10 Yosemite.
http://xquartz.macosforge.org/
http://xquartz.macosforge.org/downloads ... -2.7.7.dmg

I installed both MacPorts and XQuartz on a year 2010 iMac (3.2 GHz Core i3 dual core, 16 GiB + OS/X 10.10 Yosemite), and then installed xboard via MacPorts. Everything went smoothly.

Before using XQuartz for the first time, it's recommended to log out or reboot first so that certain XQuartz secondary information can worm its way through the system.

I note that starting XQuartz implicitly by first starting xboard can be a bit clunky as XQuartz needs some time to itself to pull its brains together. Better is to start XQuartz explicitly first and maybe wait a few seconds before invoking xboard.

Perhaps the above could be included in the xboard documentation for Mac users, and particularly for Mac users who want to connect to an ICS via xboard with their own engine.

On my 2006 Mac Pro running OS/X 10.7 Lion, I use Apple's X11.app instead of XQuartz, but I might change this as the X11.app is no longer supported.
Update:

I installed XQuartz, MacPorts, and the MacPorts' version of xboard all on an older (2010) dual core iMac. In one terminal window, in my FICS directory I have xboard+timeseal+Symbolic connected to FICS; in a second window, in my ICC directory I have xboard+timestamp+Symbolic connected to ICC. It all works. The two xboard instances share the same configuration file while each having their own net of command line parameters and their own display window.
JoshPettus
Posts: 730
Joined: Fri Oct 19, 2012 2:23 am

Re: MacPorts and XQuartz

Post by JoshPettus »

Strange for

-ncp -icsUp true -ics -icshost freechess.org

it actually worked this time, only 1 instance.

Here were the file results

Code: Select all

1482&#58; start new 'true', time=-1550446610
1482&#58; start new 'freechess.org', time=-1550446608
perhaps I entered in something wrong the last time?
User avatar
hgm
Posts: 27701
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: MacPorts and XQuartz

Post by hgm »

Something must go very wrong in calculating the time. It was supposed to give msec since program start. The negative values make no sense, but because they are negative this time, they are considered smaller than 1000, and cause the OpenFile signals to be ignored. Last time they probably were positive random values.

OK, I see the problem, and it is entirely my fault: the GetTimeMark(&started); ended up between #ifdef ENABLE_NLS switches, because I just put it directly behind the declarations without noticing those switches. And probably you have internationalization switched off. So it never reads the initial time, and the elapsed time becomes garbage.

Moving the GetTimeMark(&started); to just behind the following #endif should do it.