# HG changeset patch
# User John Donoghue <john.donoghue@ieee.org>
# Date 1468789410 14400
#      Sun Jul 17 17:03:30 2016 -0400
# Node ID 2c1a4f939c3940245ad42b39672664c9ae0f8968
# Parent  ad1790bb8f71467c8c1ac5829ef56d0086a958ae
Add basic opengl function

* scripts/plot/util/opengl.m: new file.

* scripts/plot/util/module.mk: add opengl.m

diff -r ad1790bb8f71 -r 2c1a4f939c39 scripts/plot/util/module.mk
--- a/scripts/plot/util/module.mk	Fri Jul 15 16:32:13 2016 -0700
+++ b/scripts/plot/util/module.mk	Sun Jul 17 17:03:30 2016 -0400
@@ -66,6 +66,7 @@
   scripts/plot/util/__next_line_color__.m \
   scripts/plot/util/__next_line_style__.m \
   scripts/plot/util/pan.m \
+  scripts/plot/util/opengl.m \
   scripts/plot/util/__plt_get_axis_arg__.m \
   scripts/plot/util/__pltopt__.m \
   scripts/plot/util/printd.m \
diff -r ad1790bb8f71 -r 2c1a4f939c39 scripts/plot/util/opengl.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/plot/util/opengl.m	Sun Jul 17 17:03:30 2016 -0400
@@ -0,0 +1,134 @@
+## Copyright (C) 2016 John Donoghue <john.donoghue@ieee.org>
+## 
+## This program is free software; you can redistribute it and/or modify it
+## under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 3 of the License, or
+## (at your option) any later version.
+## 
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU General Public License for more details.
+## 
+## You should have received a copy of the GNU General Public License
+## along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*- 
+## @deftypefn {Function File} opengl info
+## @deftypefnx {Function File} {@var{retval} =} opengl ("data")
+##
+## Get the opengl driver information.
+##
+## @code{opengl info} will print information on the opengl subsystem.
+##
+## @code{@var{retval} = opengl ("data")} will return the information in a 
+## structure.
+## 
+## Fields in the structure are:
+## @table @asis
+## @item version
+## OpenGL Driver version string
+## @item vendor
+## OpenGL Driver vendor string
+## @item renderer
+## OpenGL renderer string
+## @item extensions
+## List of enabled extensions for the opengl driver.
+## @end table
+##
+## @example
+## glinfo = opengl("data");
+## @end example
+## 
+## @end deftypefn
+
+function [retval] = opengl (cmd)
+    
+  # currently we only handle a single argument
+  if nargin > 1
+    error ("opengl: expects only one argument");
+  endif
+    
+  if nargin == 0
+    cmd = "info";
+  else
+    if ! ischar(cmd)
+      error ("opengl: expected string command");
+    endif  
+  endif
+    
+  if strcmpi(cmd, "info")
+    
+    [gl, msg] = gl_info ();
+      
+    if isempty(msg)
+      
+      printf ("version  = %s\n", gl.version);
+      printf ("vendor   = %s\n", gl.vendor);
+      printf ("renderer = %s\n", gl.renderer);
+      printf ("# of extensions = %d\n", numel (gl.extensions));   
+    else
+      warning (msg);
+    endif
+  elseif strcmpi (cmd, "data")
+    [retval, msg]  = gl_info ();
+    
+    if !isempty (msg)
+      warning (msg);     
+    endif
+  else
+    error ("opengl: unknown cmd - should be 'info' or 'data'"); 
+  endif
+    
+endfunction
+
+function info = fig_gl_info (h)
+  info = [];
+  if ishandle (h) && strcmp (get (h, "renderer"), "opengl")
+    vers = get(h, "__gl_version__");
+    vend = get(h, "__gl_vendor__");
+    rend = get(h, "__gl_renderer__");
+    exts = get(h, "__gl_extensions__");
+          
+    if ! isempty(vend)
+      info.version = vers;           
+      info.vendor = vend;
+      info.renderer = rend;
+      info.extensions = strsplit (exts);
+             
+    endif
+  endif     
+endfunction 
+
+function [info, msg] = gl_info ()
+  info = [];
+  msg = "";
+    
+  # if we have any open figures, take a look for any opengl info
+  figs = findall (0, "type", "figure");
+    
+  for i = 1:numel (figs)
+    if isempty (info)
+      info = fig_gl_info (figs(i));
+    endif
+  endfor
+    
+  # if no info yet, try open a figure brifly to get the info
+  if isempty (info)
+    h = figure ("position", [0,0,1,1], "toolbar", "none", "menubar", "none");
+    waitfor (h, "timeout", 1);
+    info = fig_gl_info (h);
+    close (h);
+  endif
+     
+  if isempty (info)
+    msg = "opengl: can not obtain opengl information";
+  endif
+     
+endfunction
+ 
+
+%!xtest 
+%! a = opengl("data");
+%! assert(!isempty(a)) 
+%! assert(isfield(a, 'version'))
