<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Bug?: Task Switching Routine Called Twice</title>
		<link>http://www.allegro.cc/forums/view/590937</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Wed, 11 Apr 2007 00:24:54 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><b>Allegro Version:</b> 4.2.1<br /><b>Operating System:</b> Windows 98 SE</p><p>I have no idea what&#39;s causing this, even after looking through the Allegro source, or if it&#39;s limited to Windows 98 or not, but under the following circumstances, any routines I&#39;ve registered with set_display_switch_callback() get processed <i>twice!</i></p><ul><li><p>Must be windowed. (Won&#39;t happen fullscreen.)
</p></li><li><p>Only happens with SWITCH_IN, not with SWITCH_OUT.
</p></li><li><p>Only happens when restoring a minimized application. (It&#39;s not enough that it&#39;s in the background, it must be minimized.)</p></li></ul><p>I spent an hour looking through the Allegro source and haven&#39;t the foggiest idea what&#39;s causing this. I spent even longer than that debugging my code to see if perhaps it was something I was doing but my code checks out too, especially considering the specific circumstances outlined. If any one of those situations is not true, the problem doesn&#39;t happen.</p><p>I managed to make a work-around in my code that prevents this from being an issue, but still, it&#39;s particularly weird that it would happen at all. I&#39;m really not sure if this is a bug or a glitch with my system or what... it&#39;s <i>too</i> strange...</p><p>--- Kris Asick (Gemini)<br />--- <a href="http://www.pixelships.com">http://www.pixelships.com</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kris Asick)</author>
		<pubDate>Tue, 10 Apr 2007 06:29:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It would seem to me that its windows sending some message to allegro about the un minimize, then the &quot;to front&quot; actions. But that&#39;s just a wild guess.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Tue, 10 Apr 2007 09:51:38 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I confirm that this happens on a minimalistic example under Vista. Two &quot;in&quot; calls are triggered at the time focus is restored after a minimize.</p><p>It happens with SWITCH_BACKGROUND. Does not happen with SWITCH_PAUSE. (Only two modes supported in a windowed environment.)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matthew Leverton)</author>
		<pubDate>Tue, 10 Apr 2007 10:32:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I don&#39;t know if this is relevent here, but you should be aware that set_gfx_mode() induces a SWITCH_IN event, which is fine. So if you restore a graphic mode via set_gfx_mode() in a display switch callback routine, you&#39;ll have another SWITCH_IN callback.</p><p>EDIT: The above is not the case here.<br />The thing is that WM_ACTIVATE event is sent twice when deminimizing a window. Fortunatly, there is a way to distinguish the two events. The following patch ignores the first event:
</p><div class="source-code snippet"><div class="inner"><pre>Index: src<span class="k3">/</span>win<span class="k3">/</span>wwnd.c
<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>win<span class="k3">/</span>wwnd.c      <span class="k2">(</span>revision <span class="n">7814</span><span class="k2">)</span>
<span class="k3">+</span><span class="k3">+</span><span class="k3">+</span> src<span class="k3">/</span>win<span class="k3">/</span>wwnd.c      <span class="k2">(</span>working copy<span class="k2">)</span>
@@ <span class="k3">-</span><span class="n">261</span>,<span class="n">6</span> <span class="k3">+</span><span class="n">261</span>,<span class="n">10</span> @@
             _win_switch_out<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
          <span class="k2">}</span>
          <span class="k1">else</span> <span class="k2">{</span>
<span class="k3">+</span>           <span class="c">/* Ignore the WM_ACTIVATE event if the window is minimized. */</span>
<span class="k3">+</span>           <span class="k1">if</span> <span class="k2">(</span>HIWORD<span class="k2">(</span>wparam<span class="k2">)</span><span class="k2">)</span>
<span class="k3">+</span>              <span class="k1">break</span><span class="k2">;</span>
<span class="k3">+</span>
             <span class="k1">if</span> <span class="k2">(</span>gfx_driver <span class="k3">&amp;</span><span class="k3">&amp;</span> <span class="k3">!</span>gfx_driver-&gt;windowed<span class="k2">)</span> <span class="k2">{</span>
                <span class="c">/* 1.2s delay to let Windows complete the switch in fullscreen mode */</span>
                SetTimer<span class="k2">(</span>allegro_wnd, SWITCH_TIMER, <span class="n">1200</span>, NULL<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

I tested it on exswitch.exe example and it works fine.<br />Obviously, programs using workaround for this bug will become broken.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Milan Mimica)</author>
		<pubDate>Tue, 10 Apr 2007 14:47:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Does returning 0 instead of breaking do anything? MSDN says that you should return 0 if you handle the event. Maybe that would get rid of the double event? I&#39;d try it, but MinGW is broken under Vista and I cannot easily compile Allegro.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Obviously, programs using workaround for this bug will become broken.
</p></div></div><p>
Not necessarily:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">int</span> foo <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
<span class="k1">void</span> switch_in<span class="k2">(</span><span class="k2">)</span>
<span class="k2">{</span>
  <span class="k1">if</span> <span class="k2">(</span>foo<span class="k2">)</span>
  <span class="k2">{</span>
    foo <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
  <span class="k2">}</span>
<span class="k2">}</span>

<span class="k1">void</span> switch_out<span class="k2">(</span><span class="k2">)</span>
<span class="k2">{</span>
  foo <span class="k3">=</span> <span class="n">1</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

But this is a bug and should be fixed regardless if it breaks hacks. People should report bugs, not work around them in user code.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matthew Leverton)</author>
		<pubDate>Tue, 10 Apr 2007 22:11:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Does returning 0 instead of breaking do anything?
</p></div></div><p>
No.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Milan Mimica)</author>
		<pubDate>Wed, 11 Apr 2007 00:19:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Then I&#39;d say your patch should be applied.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matthew Leverton)</author>
		<pubDate>Wed, 11 Apr 2007 00:24:54 +0000</pubDate>
	</item>
</rss>
