<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Is Mac OS X set_window_title() one of the cases that doesn&#39;t work?</title>
		<link>http://www.allegro.cc/forums/view/587424</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 07 Sep 2006 01:02:03 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Mac OS X set_window_title() doesn&#39;t appear to set the window title.  Is that a known issue?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Runesabre)</author>
		<pubDate>Tue, 05 Sep 2006 02:16:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>This code works OK for me, can you test and/or post code that doesn&#39;t work? (attached)
</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#include &lt;allegro.h&gt;</span>

<span class="k1">int</span> main<span class="k2">(</span><span class="k2">)</span>
<span class="k2">{</span>
  <a href="http://www.allegro.cc/manual/allegro_init" target="_blank"><span class="a">allegro_init</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
  <a href="http://www.allegro.cc/manual/install_keyboard" target="_blank"><span class="a">install_keyboard</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
  <a href="http://www.allegro.cc/manual/set_gfx_mode" target="_blank"><span class="a">set_gfx_mode</span></a><span class="k2">(</span>GFX_AUTODETECT_WINDOWED, <span class="n">640</span>, <span class="n">480</span>, <span class="n">0</span>,<span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
  <a href="http://www.allegro.cc/manual/set_window_title" target="_blank"><span class="a">set_window_title</span></a><span class="k2">(</span><span class="s">"Matthew Leverton Ate My Hamster"</span><span class="k2">)</span><span class="k2">;</span>
  <a href="http://www.allegro.cc/manual/readkey" target="_blank"><span class="a">readkey</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
<a href="http://www.allegro.cc/manual/END_OF_MAIN" target="_blank"><span class="a">END_OF_MAIN</span></a><span class="k2">(</span><span class="k2">)</span>
</pre></div></div><p>

Pete
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Peter Hull)</author>
		<pubDate>Tue, 05 Sep 2006 23:39:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I was calling set_window_title() before calling set_gfx_mode().  Moving the set_window_title() call after set_gfx_mode() works.</p><p>Thanks Peter!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Runesabre)</author>
		<pubDate>Wed, 06 Sep 2006 02:06:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Your poor hamster. <img src="http://www.allegro.cc/forums/smileys/cry.gif" alt=":&#39;(" /></p><p>Marcello
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Marcello)</author>
		<pubDate>Wed, 06 Sep 2006 02:14:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I was calling set_window_title() before calling set_gfx_mode(). Moving the set_window_title() call after set_gfx_mode() works.
</p></div></div><p>
The documentation doesn&#39;t specify an order for calling these in; should this be documented?<br />The documentation does say that &quot;...  this routine alters the window title for your Allegro program,&quot; weakly suggesting that the window should exist at that point, but it&#39;s not explicit.<br />How do other platforms behave?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Wed, 06 Sep 2006 02:59:48 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>When I had set_window_title() called before set_gfx_mode(), Windows and Linux windows showed the proper window title while Mac did not.</p><p>Personally, and if it&#39;s even possible, I prefer not to have an order requirement.  Otherwise, a documentation note would be nice.</p><p>Not a big deal either way, I&#39;m just glad it works thanks to Peter&#39;s help.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Runesabre)</author>
		<pubDate>Wed, 06 Sep 2006 07:16:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ok, I&#39;ll see if I can look into it before releasing 4.2.1<br />I think I&#39;m going to take the stance that since no such requirement is mentioned in the docs and neither the Windows nor the X11 port require it, this is a bug in the MacOS X port. It shouldn&#39;t be that hard to fix either.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Wed, 06 Sep 2006 11:40:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It&#39;s already been thought of, apart from there&#39;s a bug. It works like this:<br /><tt>install_allegro</tt> sets the default title (I hope no-one is expecting it to work if called before <tt>install_allegro</tt>?)</p><p>The implementation of <tt>set_window_title</tt> copies the supplied title into the variable <tt>osx_window_title</tt>, and does the OS call to actually set the title if the window exists.
</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="c">/* osx_sys_set_window_title:</span></td></tr><tr><td class="number">2</td><td><span class="c"> *  Sets the title for both the application menu and the window if present.</span></td></tr><tr><td class="number">3</td><td><span class="c"> */</span></td></tr><tr><td class="number">4</td><td><span class="k1">static</span> <span class="k1">void</span> osx_sys_set_window_title<span class="k2">(</span>AL_CONST <span class="k1">char</span> <span class="k3">*</span>title<span class="k2">)</span></td></tr><tr><td class="number">5</td><td><span class="k2">{</span></td></tr><tr><td class="number">6</td><td>   <span class="k1">char</span> tmp<span class="k2">[</span>ALLEGRO_MESSAGE_SIZE<span class="k2">]</span><span class="k2">;</span></td></tr><tr><td class="number">7</td><td>   </td></tr><tr><td class="number">8</td><td>   _al_sane_strncpy<span class="k2">(</span>osx_window_title, title, ALLEGRO_MESSAGE_SIZE<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">9</td><td>   <a href="http://www.allegro.cc/manual/do_uconvert" target="_blank"><span class="a">do_uconvert</span></a><span class="k2">(</span>title, U_CURRENT, tmp, U_UTF8, ALLEGRO_MESSAGE_SIZE<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">10</td><td>&#160;</td></tr><tr><td class="number">11</td><td>   NSString <span class="k3">*</span>ns_title <span class="k3">=</span> <span class="k2">[</span>NSString stringWithUTF8String: tmp<span class="k2">]</span><span class="k2">;</span></td></tr><tr><td class="number">12</td><td>   </td></tr><tr><td class="number">13</td><td>   <span class="k1">if</span> <span class="k2">(</span>osx_window<span class="k2">)</span></td></tr><tr><td class="number">14</td><td>      <span class="k2">[</span>osx_window setTitle: ns_title<span class="k2">]</span><span class="k2">;</span></td></tr><tr><td class="number">15</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>

In set_gfx_mode, when the window is created, the call <br /><span class="source-code"><a href="http://www.allegro.cc/manual/set_window_title" target="_blank"><span class="a">set_window_title</span></a><span class="k2">(</span>osx_window_title<span class="k2">)</span></span><br /> is made. This should set the title to whatever has been set previously (which is the default if <tt>set_window_title</tt> has never been called by the user). This is the desired behaviour. Unfortunately, <tt>_al_sane_strcpy</tt> fails if source == destination, which it is, in this case. It leaves <tt>osx_window_title</tt> as an empty string, which is probably what you are seeing.</p><p>Still following me?</p><p>So I propose an explicit check for this case,
</p><div class="source-code snippet"><div class="inner"><pre>Index: src<span class="k3">/</span>macosx<span class="k3">/</span>system.m
<span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span><span class="k3">=</span>
<span class="k3">-</span><span class="k3">-</span><span class="k3">-</span> src<span class="k3">/</span>macosx<span class="k3">/</span>system.m <span class="k2">(</span>revision <span class="n">7527</span><span class="k2">)</span>
<span class="k3">+</span><span class="k3">+</span><span class="k3">+</span> src<span class="k3">/</span>macosx<span class="k3">/</span>system.m <span class="k2">(</span>working copy<span class="k2">)</span>
@@ <span class="k3">-</span><span class="n">563</span>,<span class="n">7</span> <span class="k3">+</span><span class="n">563</span>,<span class="n">8</span> @@
 <span class="k2">{</span>
    <span class="k1">char</span> tmp<span class="k2">[</span>ALLEGRO_MESSAGE_SIZE<span class="k2">]</span><span class="k2">;</span>
    
<span class="k3">-</span>   _al_sane_strncpy<span class="k2">(</span>osx_window_title, title, ALLEGRO_MESSAGE_SIZE<span class="k2">)</span><span class="k2">;</span>
<span class="k3">+</span>   <span class="k1">if</span> <span class="k2">(</span>osx_window_title <span class="k3">!</span><span class="k3">=</span> title<span class="k2">)</span>
<span class="k3">+</span>       _al_sane_strncpy<span class="k2">(</span>osx_window_title, title, ALLEGRO_MESSAGE_SIZE<span class="k2">)</span><span class="k2">;</span>
    <a href="http://www.allegro.cc/manual/do_uconvert" target="_blank"><span class="a">do_uconvert</span></a><span class="k2">(</span>title, U_CURRENT, tmp, U_UTF8, ALLEGRO_MESSAGE_SIZE<span class="k2">)</span><span class="k2">;</span>
 
    NSString <span class="k3">*</span>ns_title <span class="k3">=</span> <span class="k2">[</span>NSString stringWithUTF8String: tmp<span class="k2">]</span><span class="k2">;</span>
</pre></div></div><p>

This is IMO the lowest impact solution, though probably not the neatest one.<br />(diff attached)</p><p>Pete</p><p>ps. AFAIK, the behaviour of strncpy is supposed to be undefined if source and destination overlap (<a href="http://www.opengroup.org/onlinepubs/009695399/functions/strncpy.html">link</a>) so the behaviour of _al_sane_strncpy seems quite reasonable in this case.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Peter Hull)</author>
		<pubDate>Wed, 06 Sep 2006 23:25:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
This is IMO the lowest impact solution, though probably not the neatest one.<br />(diff attached)
</p></div></div><p>
A single line comment describing what&#39;s going on might be nice, though I would probably change _al_sane_strncpy to leave the case source==destination alone.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
ps. AFAIK, the behaviour of strncpy is supposed to be undefined if source and destination overlap (link [opengroup.org]) so the behaviour of _al_sane_strncpy seems quite reasonable in this case.
</p></div></div><p>
My manpage actually says something a bit stronger than that:
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
The strings may not overlap
</p></div></div><p>
Still, it&#39;s called _al_<b>sane</b>_strncpy for a reason, right? <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Thu, 07 Sep 2006 01:02:03 +0000</pubDate>
	</item>
</rss>
