Screen/Linux: mexLock() Screen() into memory if running on Mesa OpenGL

Turns out Mesa, at least as of 10.1, as shipped in Ubuntu 14.04-LTS,
has a bug which causes it to crash on a "clear Screen" -> Reload Screen
cycle.

On first call in a session to glXGetProcAddress('glfoo') to get the
entry point to gl function glfoo(), if the stub for this function
needs to get dynamically generated, Mesa will do so, but it will
setup a persistent pointer to the name string 'glfoo' passed to the
glXGetProcAddress() function. It will use the callers memory space
to cache the name string, instead of creating its own private copy!

For us this means that if Screen() calls glewInit() to setup gl
entry point bindings, Mesa will have pointers into the inside of
Screen! If the user 'clear Screen' unloads the Screen.mex file,
then reload it - usually to a different address in the address space
due to relocations or maybe address space layout randomization -
Mesa (specifically libglapi.so) will have dangling pointers to
unallocated memory. Next time somebody (e.g., reloaded Screen())
calls glXGetProcAddress(), libglapi will deref these invalid
pointers and crash!

As various bits of Octave and Matlab also use OpenGL and therefore
hold references to libOpenGL / libglapi, both libraries won't get
unloaded / reloaded when our Screen.mex file gets unloaded/reloaded,
therefore the dangling pointers won't get reinitialized properly.

The only real fix for this crasher is to fix Mesa with a trivial
one-liner bugfix. However, until that is done, we need a workaround.

Our workaround is to mexLock() Screen() into memory as soon as
"Mesa" is detected as underlying OpenGL library, so we can't get
clear'ed out of memory until session termination.

This can be prevented via setenv('PSYCH_DONT_LOCK_SCREEN','1'); and
won't happen at all on non-Mesa OpenGL implementations.

-> 'clear Screen' / 'clear mex' / 'clear all' won't clear Screen,
   with all the annoying inconvenience this causes to some users and
   Mk specifically during development. Better than crashing tough.

-> Hashtag: SoftwareSucks.
Screen/Linux: Add gcc pragma to suppress XKeyCodetoKeysym deprecation warning.
Screen/GStreamer: Change handling of pipeline bus messages.

Turns out our old method of receiving/handling messages on the
GStreamer GstPipeline message bus doesn't work on Octave 3.8 under
Linux if Octave's QT based GUI is active :(

The Qt toolkit on Linux - and thereby Octave on Linux GUI - uses
glib's event loops internally for some GUI event processing. We
also used glib's default event loop to process/dispatch bus
messages and this caused cross talk between Octave GUI and our
GStreamer backend - Octave GUI ate our events, so we never received
them. This caused failure to loop videos during playback because
we didn't get the EOS messages needed to trigger a rewind. It also
made error handling useless, because we couldn't get the error and
warning message events.

Now all our GStreamer backends (video playback, video capture/recording,
movie writing) implement a new strategy in their PsychGSProcessContext()
methods. Direct polling of the bus and direct dequeueing/dispatch without
GLib's event loops. Old code is still there as disabled alternative.

-> Works now fine under Linux with Octave GUI/CLI and Matlab GUI/CLI.
-> Needs retesting on OSX and Windows. If this doesn't work i will sob
   uncontrollably. How painful can this get??

-> Screen for 64-Bit Octave/Matlab on Linux rebuilt and tested.
-> Test on other platforms pending.

HashTag: CanThisReleaseCycleSuckEvenMore?
Screen/Linux: Move deprecated warning suppress pragma out of function.

Old gcc of Debian 6 doesn’t like the pragma inside the function, so
don’t do it.
Screen/Linux: Make output of Screen('Computer') more consistent with OSX.

computer.kern struct format should match OSX output closely.
computer.system format should match other OS’es closely.
PsychSupported() output format should match other OS’es closely.

-> Update PsychtoolboxRegistration.m to make use of the new
Screen(‘Computer’) fields and get rid of some Unix system() command
line calls.
Rebuild some sample of Screen mex files for compile testing and GStreamer testing.

Compile is ok. New GStreamer bus message handling works on all
operating systems and scripting environments. Pheew!
moalcore/moglcore: Add include of uchar.h to fix compile failure on R2012a

Add explicit #include <uchar.h> before include for mex.h into
mogltypes.h and moaltypes.h. Apparently latest versions of gcc
and clang do not include this file automatically, which causes
build failure for moglcore and moalcore under Matlab R2012a on
recent Ubuntu 14.04 and OSX 10.9.

Adding the defines fixes this. Similar fix may be needed for
some mex files on OSX which don't use Psychtoolbox core
infrastructure.
Screen: Disable RTLD_GLOBAL export of Screen's symbols on Linux.

Disable the dlopen() self-reloading() of Screen. It had the
purpose of making Screen's symbols globally available to other
shared libraries. However, the reason for doing this is long
gone (ListenChar magic sauce for Octave + GUI) as it proved
too fragile.

Now the code just causes trouble because mex files like
moglcore.mex would use Screen's symbols (e.g., for GLEW stuff)
and keep Screen.mex loaded into the process when it should
unload on 'clear Screen'. We disable this useless code, but
leave it in place to document the approach for future more
targeted use.

-> This prepares the latest workaround in the next commit
   for an annoying Mesa bug.

-> Remove some orphaned prototype from ScreenCloseAll().
Screen: Try harder to workaround Mesa OpenGL bug.

Same cause and solution principle as in commit

77b7b0fb2581473b5c1944b8f4b2161738da56e8

However, instead of locking Screen() into process memory, we
lock moglcore into process memory and make sure Mesa gets its
evil string pointers targeted to moglcore.

1. Perform OpenGL context creation/init/binding in Screen() as usual.
2. Detect buggy Mesa.
3. Call moglcore('LockModule') to trigger moglcore's glewInit() to
   get Mesa's pointers directed towards its library image and to
   mexLock() moglcore into memory, so moglcore is a safe haven for
   Mesa's pointers until the work session gets terminated normally.
4. Call Screen()'s glewInit(). Mesa won't redirect its pointers, as
   it already has happy pointers towards moglcore.

-> moglcore can'b be clear'ed, but that doesn't matter, given how
   rarely one rebuilds or reinitializes it. Or so i hope...

-> Screen is not locked and can be clear'ed as usual, which is
   convenient :)

-> Seems to work. Needed new moglcore('LockModule') subfunction to
   trigger a mexLock() inside moglcore, followed by a no-op otherwise.

-> Tested on Octave and Matlab on Linux with Mesa's Gallium nouveau
   driver under Ubuntu 14.04-LTS.
PsychHID: Update code comment wrt. CTRL+C handling on Octave 3.8

Update April 2014: As of Octave 3.8.1 on Linux, sending SIGINT
explicitely when running under Octave 3.8 with QT based GUI is no
longer needed for the Qt-GUI, and also not executed, because GNUTERM
is no longer set to 'qt', so all is good :)
moglcore/moalcore: Undo uchar.h include

Breaks build on old build-system. Needs more thought.

(cherry picked from commit 7e64a3384abbc7e02f419c8c40b5024347adc257)
Almost all mex files for all platforms rebuilt for 3.0.11 release.

(cherry picked from commit 374b5d4eed881c876454c94ced5d18e67c7dc2d3)

-> Get all the rebuilt mex-files from 3.0.11 to save rebuild time.

Conflicts:
	Psychtoolbox/PsychBasic/Octave3OSXFiles64/Screen.mex
	Psychtoolbox/PsychBasic/Screen.mexmaci64
Screen for 64-Bit Octave/Matlab on OSX rebuilt.

To sync it up to 3.0.11, feature and bug-fix wise.
Update parseptblog.php for v3.0.11 and Matlab R2014a/b support.

Account for Psychtoolbox-3.0.11.
Account for Matlab R2014a and R2014b.
Cleanup useless info.
LoadIdentityClut: Add new case for some AMD cards on Linux.

Mesa 8 reports, e.g., “AMD CYPRESS”, for HD-5000, a case we don’t yet
handle.
By chance, the fallback path chose the proper gfxhwtype = 0 anyway. But
get rid of warning clutter.

-> A better approach on Linux (possibly OSX) would be to use DCE
display engine generation as a matching criterion instead -> More
“constant”.
Screen/Linux: Bugfix: Enable 10 bpc framebuffers on AMD DCE4+ gpu's.

Some stray check for DCE version <= 1.0 was left, preventing this
to work on DCE-4 and later.
Screen on Linux/OSX: Fix AMD DCE-4 detection and mmio handling.

Two hilarous bugs fixed:

a) If DCE4+ was detected, the minimal allowable register access offset was
   set to a well meaning and wrong value. This caused register read/write
   access to regs below that wrong limit to silently fail, because the
   error reporting was non-existent! Hilarious debugging fun ensures!

   -> Set limit to zero to disable it. Too much potential for harm with
      too less benefit.

   -> Improve error reporting to reduce future fun.

b) AMD DCE engine generations are reported in decimal notation, but
   some of the DCE generation specific code matched against hexadecimal
   limits, e.g., 0x40 instead of 40 for detecting DCE-4. This caused
   code to no-op, or wrong code pathes to be taken for a given gpu.

   Affected functionality, which totally malfunctioned in DCE4+:

   Identity lut and color offset loading and checking for pixel
   identity passthrough setup on AMD, aka LoadIdentityClut().

   Dithering control on DCE4+.

   10 bit ARGB2101010 framebuffer setup on DCE4+

a) and b) combined explain following bugs, which are now fixed:

- Inability to configure low level pixelpassthrough on AMD DCE4+ on
  both Linux and OSX with AMD cards.

- Inability to enable 10 bpc framebuffers on DCE4+ on Linux and OSX.

- Inability to auto-detect screen->head mappings via auto-probing:
  This was considered unworkable and is still disabled and untested
  in this commit. However the likely reason it never worked is simply
  these bugs. Need to reenable and retest in future commits.

-> Source code updated, but no mex files included, so we can
   cherry-pick this easily into the 3.0.11 branch.

-> Will also need rebuild for PsychtoolboxKernelDriver on OSX.
Screen for 64-Bit Octave and Matlab on Linux test-rebuilt.

-> Confirms that identity passthrough and 10 bpc framebuffers now
   work on DCE-4.
PsychRTBox.m: Fix for firmware version 5.0

v5.0 firmware doesn't seem to send back acknowledge char 'e' anymore
for event 'e'nable. So don't expect an ack for 'e' on version 5.

Is this a complete fix? I don't know, because that f$#%# firmware
and underlying protocol gets changed over and over to make it a
bit worse with each iteration.