Screen/Linux: Fix X11 display locking more.

There was a hidden locking error when running on the FOSS
(Mesa/Gallium) graphics drivers under X11: Each scheduled
doublebuffer-swap will invalidate the drawables system framebuffer.
First access to the fb by OpenGL will therefore trigger a roundtrip to
the X-Server for revalidation. This uses our shared x-display
connection handle for transport and therefore needs lock protection in
multi-threaded ops like frame-sequential stereo of async flips. The
NVidia binary blob uses a different mechanism for revalidation which
bypasses the X-Server, so this problem didn't show up during
pre-release testing.

We can't lock protect any glXXX call just in case, that would be a
performance disaster. Therefore we perform a dummy pixel write +
glFlush() immediately after scheduling a bufferswap and we do this
under display lock protection. This write after bufferswap will trigger
the X11 roundtrip for fb revalidation under the lock. After that
validation we should be safe from such surprises until the next
bufferswap gets scheduled.

Hopefully the last locking bug from our locking rewrite, this is
painful.

-> Screen mex files for 32/64-Bit Matlab/Octave on Linux rebuilt.
-> Tested in VM, but this is mostly pointless.
-> Tests on both NVidia proprietary and nouveau FOSS pending.
Screen/Linux: Fix deadlock in Screen('Resolutions', screenid);

Did deadlock when called that way, due to display locking bug.
Screen/Linux: Next attempt at proper X11 Locking :-(

Approach from previous commit didn't work, because the
rountrip to the X-Server (aka DRI2GetBuffersWithFormat) is
blocking if the bufferswap is still pending. Therefore this
caused massive performance degradation on FOSS drivers.

Now we are more careful to try to avoid blocking with
display lock held:

a) If OML_sync_control is supported, use
   PsychOSGetSwapCompletionTimestamp() to wait in a non-blocking,
   polling way for bufferswap completion, and only then perform
   the dummy-pixelwrite + glFlush() to the framebuffer. This
   guarantees the actual write won't block with lock held.
   PsychOSGetSwapCompletionTimestamp() can now do the waiting
   for swap completion without actual final timestamping and
   INTEL_swap_events processing if the time variable is set to
   NULL to signal we're not interested in the actual (ust,msc).

   If OML_sync_control isn't available or functional, we still
   do the dummy-write, because we have to - Crashing is worse
   than degraded performance!

b) Make sure this fixup/workaround is only executed if multi-threading
   is active, ie., if a async flip is pending or our homegrown frame-
   sequential stero mode is active. Skip this stuff in purely single-
   threaded operation (which is the common case) or on drivers which
   don't need it, e.g., currently NVidia binary blob, but possibly
   also the Catalyst blob in the future (unkown).

-> Screen for 64-Bit Octave on Linux rebuilt and tested with NVidia
   binary blob, to confirm there is no degradation for that setup
   which doesn't actually need the workaround.

-> Test on nouveau for active workaround pending.

The pain, when will it end?
Screen/Linux: Always poll-wait in PsychOSGetSwapCompletionTimestamp()

Not always of course, but also poll-wait if there is only 1 onscreen
window and all other requirements are fulfilled. We need to avoid a
blocking wait for swapbuffer completion, as we hold the display lock
during such a wait, so we'd block not only the (non-existent in the
single-window case) other graphics threads, but also the main thread
when executing X11 calls, e.g., GetMouse() queries.

This wasn't an issue before, but now that we lock the display lock, it
could cause some delays on the main thread even in the single window
case.
Rebuild Screen mex files for 32/64-Bit Octave and Matlab on Linux.

Finally incorporate all fixes for X11 display locking.
This one is probably as good as it gets.
DaqDeviceIndex: Implement new detection heuristic for OSX + single device.

Always choose daq as index of device with maximum index in the single
device case.
Finds proper daq, but DaqAInScan still doesn't work on OSX 10.7. But
maybe it helps somebody with OSX 10.8+
PsychHID/Linux: Fix memory leak in libusb on init failure.

As found by Yaroslav.
Fix printf bug in Screen and PsychKinectCore

printf() Error reporting string had a %i defined, but no actual
variable whose value to print.
-> These are on non-critical error paths, so no rebuild for mex files.
Can wait until a future release.
Found by Yaroslav.
Fix memory leak on error in FTGL drawtext plugin.

In the unlikely case of an error, we leaked a few bytes of unicode text.
-> Not critical: Recompile can wait for a future release.
Found by Yaroslav.
PsychCV/OpenEyesTracker: Fix memory leak on error condition in svd.

Ok, this is code we don't even build atm, but doesn't hurt to keep it
free from memleak on error.
-> No recompile needed.
Reenable PsychCV for 64-Bit Octave on Linux.

This provides access to the ARToolkit tracking functions on
64-Bit GNU/Octave on Linux. As ARToolkit is GPLv2 licensed,
it - and therefore our PsychCV.mex file linked against it -
can only be used under GNU/Octave which is GPL licensed as
well. We only enable this on 64-Bit Octave for Linux, as a
little bonus for Linux users, if at all.

This just for testing. May get disabled before next beta release
again.

-> PsychCV for 64-Bit Octave on Linux built and tested.
-> ARToolkitDemo made functional again with GStreamer video capture
   engine as default.
OpenEyes: Tiny fixes -- However, this stays disabled for now.
Screen: Fix small bug in PsychNormalizeTextureOrientation().

Didn't restore the framebuffer binding to zero, as it should,
because PsychSetDrawingTarget(NULL); as used there would be
a no-op mostly for the purpose of resetting the binding to zero.

Fixed. Fixes a "invalid operation" error on nouveau. However, this
was not the culprit of morphing errors in MorphDemo. These remain
a mystery.
PrintPhotoReceptors, FillInPhotoreceptors: Bug fixes and restore Octave compatibility.

fields() doesn't exist in Octave 3.6.x, but fieldnames() does and does
the same thing.
Use proper short-circuit boolean operators.
Replace obsolete isstr() by ischar().
Fix loop variable bugs.
Linux XLib locking: The horror continues. Fix PsychHID XInitThreads()

Remaining use of XInitThreads in PsychHID can trigger crashes in
Screen() shutdown, due to mixup of non-threading and threading
x-connections in Screen vs. PsychHID. Only happened on Yaroslavs setup.

-> Make use of XInitThreads() in both Screen and PsychHID conditional
on a defined environment variable PSYCH_XINITTHREADS. By default it is
not used, as we have our own locking for Screen() and don't need it in
PsychHID due to it using dedicated x-connections for its threads (main
thread vs. kbqueue thread).
-> The environment variable is for debugging only, or in the faint hope
that it might fix crashes with the FOSS mesa intel/nouveau..
gfx-drivers caused by DRI2's racy way of sending drawable invalidate
events to clients. We'll see if it helps in those cases or not. Our own
locking definitely doesn't work reliably there on the Intel driver.
Rebuild Screen and PsychHID for Linux with latest XLib threading fixes.

All 32/64-Bit Octave/Matlab for Intel cpus.
No rebuild for 32-Bit ARM, the 3 people that use it will have to suffer
a bit longer.
Eyelink: Improve error handling in Eyelink('Initialize').

Provide meaningful error message with abort if open_eyelink_system()
fails, instead of silent failure and confusing status return codes.
-> Minor problem, no need to rebuild Eyelink mex file. Will be folded
into release at next real rebuild.
New demo TurnTableDemo - Free your inner DJ!

A demo which uses PsychPortAudio's functions to simulate a good'ol turn
table for some recreational scratching and other auditory fun
activities.

Work in progress - This is a backup commit of a basic working state.
TurnTableDemo: Number zero does not map well - exclude it.
TurnTableDemo: Refine key mappings.

Right ALT doesn't work well on some setups, so use
Right SHIFT instead to stop looping sounds and drop
the old mute function of that right SHIFT key.

Minor cleanups.