update luadoc tools
[ardour.git] / tools / fmt-luadoc.php
1 #!/usr/bin/php
2 <?php
3 ## USAGE
4 #
5 ## generate doc/luadoc.json.gz (lua binding doc)
6 # ./waf configure --luadoc ....
7 # ./waf
8 # ./gtk2_ardour/arluadoc > doc/luadoc.json.gz
9 #
10 ## generate doc/ardourapi.json.gz (ardour header doxygen doc)
11 # cd ../../tools/doxy2json
12 # ./ardourdoc.sh
13 # cd -
14 #
15 ## format HTML (using this scripterl)
16 # php tools/fmt-luadoc.php > /tmp/luadoc.html
17 #
18
19 $options = getopt("m");
20 if (isset ($options['m'])) {
21         $HTMLOUTPUT = false; ## set to false to output ardour-manual
22 } else {
23         $HTMLOUTPUT = true; ## set to false to output ardour-manual
24 }
25
26 ################################################################################
27 ################################################################################
28
29 $json = gzdecode (file_get_contents (dirname (__FILE__).'/../doc/luadoc.json.gz'));
30 $doc = array ();
31 $ardourversion = '';
32 foreach (json_decode ($json, true) as $b) {
33         if (!isset ($b['type'])) {
34                 if (isset ($b['version'])) { $ardourversion = $b['version']; }
35                 continue;
36         }
37         # reserved lua words
38         $b ['lua'] = preg_replace ('/:_end/', ':end', $b ['lua']);
39         $b ['lua'] = preg_replace ('/:_type/', ':type', $b ['lua']);
40         $b ['ldec'] = preg_replace ('/ const/', '', preg_replace ('/ const&/', '', $b['decl']));
41         if (isset ($b['ret'])) {
42                 $b['ret'] = preg_replace ('/ const/', '', preg_replace ('/ const&/', '', $b['ret']));
43         }
44         $doc[] = $b;
45 }
46
47 if (count ($doc) == 0) {
48         fwrite (STDERR, "Failed to read luadoc.json\n");
49         exit (1);
50 }
51
52 ################################################################################
53 ## Global result variables
54 ################################################################################
55
56 $classlist = array ();
57 $constlist = array ();
58
59
60 ################################################################################
61 ## Pre-process the data, collect functions, parse arguments, cross reference
62 ################################################################################
63
64
65 ################################################################################
66 # some internal helper functions first
67
68 $funclist = array ();
69 $classes = array ();
70 $consts = array ();
71
72 function my_die ($msg) {
73         fwrite (STDERR, $msg."\n");
74         exit (1);
75 }
76
77 ##function ptr_strip ($ctype) {
78 #       # boost::shared_ptr<std::list<boost::shared_ptr<ARDOUR::Route>> > >
79 #       # -> std::list<ARDOUR::Route>
80 #       $ctype = preg_replace ('/boost::shared_ptr<([^>]*)[ ]*>/', '$1', $ctype);
81 #       return preg_replace ('/boost::shared_ptr<([^>]*)[ ]*>/', '$1', $ctype);
82 #}
83
84 function arg2lua ($argtype, $flags = 0) {
85         global $classes;
86         global $consts;
87
88         # LuaBridge abstracts C++ references
89         $flags |= preg_match ('/&$/', $argtype);
90         $arg = preg_replace ('/&$/', '', $argtype);
91         $arg = preg_replace ('/ $/', '', $arg);
92
93         # filter out basic types
94         $builtin = array ('float', 'double', 'bool', 'std::string', 'int', 'long', 'unsigned long', 'unsigned int', 'unsigned char', 'char', 'void', 'char*', 'unsigned char*', 'void*');
95         if (in_array ($arg, $builtin)) {
96                 return array ($arg => $flags);
97         }
98
99         # check Class declarations first
100         foreach (array_merge ($classes, $consts) as $b) {
101                 if ($b['ldec'] == $arg) {
102                         return array ($b['lua'] => $flags);
103                 }
104         }
105
106         # strip class pointers -- TODO Check C'tor for given class
107         $arg = preg_replace ('/[&*]*$/', '', $argtype);
108         foreach (array_merge ($classes, $consts) as $b) {
109                 if ($b['ldec'] == $arg) {
110                         return array ($b['lua'] => $flags);
111                 }
112         }
113         if ($flags & 2) {
114                 return array ($argtype => ($flags | 4));
115         } else {
116                 return array ('--MISSING (' . $argtype . ')--' => ($flags | 4));
117         }
118 }
119
120 function stripclass ($classname, $name) {
121         $classname .= ':';
122         if (strpos ($name, $classname) !== 0) {
123                 my_die ('invalid class prefix: ' .$classname. ' -- '. $name);
124         }
125         return substr ($name, strlen ($classname));
126 }
127
128 function datatype ($decl) {
129         # TODO handle spaces in type. Works because
130         # we don't yet have templated types (with_space <here >)
131         return substr ($decl, 0, strrpos ($decl, ' '));
132 }
133
134 function luafn2class ($lua) {
135         return substr ($lua, 0, strrpos ($lua, ':'));
136 }
137
138 function luafn2name ($lua) {
139         $fn = strrpos ($lua, ':');
140         if ($fn !== 0 && strlen($lua) > $fn + 1) {
141                 return substr ($lua, $fn + 1);
142         }
143         my_die ('invalid class prefix: '. $name);
144 }
145
146
147 function checkclass ($b) {
148         global $classlist;
149         if (!isset ($classlist[luafn2class ($b['lua'])])) {
150                 my_die ('MISSING CLASS FOR '. print_r ($b['lua'], true));
151         }
152 }
153
154 # parse functions argument list to lua-names
155 function decl2args ($decl) {
156         $start = strrpos ($decl, '(');
157         $end = strrpos ($decl, ')');
158         $args = substr ($decl, $start + 1, $end - $start - 1);
159         $arglist = preg_split ('/, */', $args);
160         $rv = array ();
161         foreach ($arglist as $a) {
162                 if (empty ($a)) { continue; }
163                 $rv[] = arg2lua ($a);
164         }
165         return $rv;
166 }
167
168 function canonical_ctor ($b) {
169         $rv = '';
170         if (preg_match('/[^(]*\(([^)*]*)\*\)(\(.*\))/', $b['decl'], $matches)) {
171                 $lc = luafn2class ($b['lua']);
172                 $cn = str_replace (':', '::', $lc);
173                 $fn = substr ($lc, 1 + strrpos ($lc, ':'));
174                 $rv = $cn . '::'. $fn . $matches[2];
175         }
176         return $rv;
177 }
178
179 function canonical_decl ($b) {
180         $rv = '';
181         $pfx = '';
182         # match clang's declatation format
183         if (preg_match('/[^(]*\(([^)*]*)\*\)\((.*)\)/', $b['decl'], $matches)) {
184                 if (strpos ($b['type'], 'Free Function') !== false) {
185                         $pfx = str_replace (':', '::', luafn2class ($b['lua'])) . '::';
186                 }
187                 $fn = substr ($b['lua'], 1 + strrpos ($b['lua'], ':'));
188                 $rv = $matches[1] . $fn . '(';
189                 $arglist = preg_split ('/, */', $matches[2]);
190                 $first = true;
191                 foreach ($arglist as $a) {
192                         if (!$first) { $rv .= ', '; }; $first = false;
193                         if (empty ($a)) { continue; }
194                         $a = preg_replace ('/([^>]) >/', '$1>', $a);
195                         $a = preg_replace ('/^Cairo::/', '', $a); // special case cairo enums
196                         $a = preg_replace ('/([^ ])&/', '$1 &', $a);
197                         $a = str_replace ('vector', 'std::vector', $a);
198                         $a = str_replace ('std::string', 'string', $a);
199                         $a = str_replace ('string const', 'const string', $a);
200                         $a = str_replace ('string', 'std::string', $a);
201                         $rv .= $a;
202                 }
203                 $rv .= ')';
204         }
205         return $pfx . $rv;
206 }
207
208 ################################################################################
209 # step 1: build class indices
210
211 foreach ($doc as $b) {
212         if (strpos ($b['type'], "[C] ") === 0) {
213                 $classes[] = $b;
214                 $classlist[$b['lua']] = $b;
215                 if (strpos ($b['type'], 'Pointer Class') === false) {
216                         $classdecl[$b['ldec']] = $b;
217                 }
218         }
219 }
220
221 foreach ($classes as $c) {
222         if (strpos ($c['type'], 'Pointer Class') !== false) { continue; }
223         if (isset ($c['parent'])) {
224                 if (isset ($classdecl[$c['parent']])) {
225                         $classlist[$c['lua']]['luaparent'][] = $classdecl[$c['parent']]['lua'];
226                 } else {
227                         my_die ('unknown parent class: ' . print_r ($c, true));
228                 }
229         }
230 }
231
232 # step 2: extract constants/enum
233 foreach ($doc as $b) {
234         switch ($b['type']) {
235         case "Constant/Enum":
236         case "Constant/Enum Member":
237                 if (strpos ($b['ldec'], '::') === false) {
238                         # for extern c enums, use the Lua Namespace
239                         $b['ldec'] = str_replace (':', '::', luafn2class ($b['lua']));
240                 }
241                 $ns = str_replace ('::', ':', $b['ldec']);
242                 $constlist[$ns][] = $b;
243                 # arg2lua lookup
244                 $b['lua'] = $ns;
245                 $consts[] = $b;
246                 break;
247         default:
248                 break;
249         }
250 }
251
252 # step 3: process functions
253 foreach ($doc as $b) {
254         switch ($b['type']) {
255         case "Constructor":
256         case "Weak/Shared Pointer Constructor":
257                 checkclass ($b);
258                 $classlist[luafn2class ($b['lua'])]['ctor'][] = array (
259                         'name' => luafn2class ($b['lua']),
260                         'args' => decl2args ($b['ldec']),
261                         'cand' => canonical_ctor ($b)
262                 );
263                 break;
264         case "Data Member":
265                 checkclass ($b);
266                 $classlist[luafn2class ($b['lua'])]['data'][] = array (
267                         'name' => $b['lua'],
268                         'ret'  => arg2lua (datatype ($b['ldec']))
269                 );
270                 break;
271         case "Static C Function":
272                 checkclass ($b);
273                 if (strpos ($b['lua'], 'ARDOUR:DataType:') === 0) {
274                         # special case ARDOUR:DataType convenience c'tor
275                         $args = array ();
276                         $ret = array (luafn2class ($b['lua']) => 0);
277                         $canon = 'ARDOUR::LuaAPI::datatype_ctor_'.strtolower (luafn2name ($b['lua'])).'(lua_State*)';
278                 } else {
279                         my_die ('unhandled Static C: ' . print_r($b, true));
280                 }
281                 $classlist[luafn2class ($b['lua'])]['func'][] = array (
282                         'bind' => $b,
283                         'name' => $b['lua'],
284                         'args' => $args,
285                         'ret'  => $ret,
286                         'ref'  => false,
287                         'ext'  => false,
288                         'cand' => $canon
289                 );
290                 break;
291         case "C Function":
292                 # we required C functions to be in a class namespace
293         case "Ext C Function":
294                 checkclass ($b);
295                 $args = array (array ('--lua--' => 0));
296                 $ret = array ('...' => 0);
297                 $ns = luafn2class ($b['lua']);
298                 $cls = $classlist[$ns];
299                 if (preg_match ('/.*<([^>]*)[ ]*>/', $cls['ldec'], $templ)) {
300                         # std::vector, std::list types
301                         switch (stripclass($ns, $b['lua'])) {
302                         case 'add':
303                                 #$args = array (array ('LuaTable {'.$templ[1].'}' => 0));
304                                 $args = array (arg2lua ($templ[1], 2));
305                                 $ret = array ('LuaTable' => 0);
306                                 break;
307                         case 'iter':
308                                 $args = array ();
309                                 $ret = array ('LuaIter' => 0);
310                                 break;
311                         case 'table':
312                                 $args = array ();
313                                 $ret = array ('LuaTable' => 0);
314                                 break;
315                         default:
316                                 break;
317                         }
318                 } else if (strpos ($cls['type'], ' Array') !== false) {
319                         # catches  C:FloatArray, C:IntArray
320                         $templ = preg_replace ('/[&*]*$/', '', $cls['ldec']);
321                         switch (stripclass($ns, $b['lua'])) {
322                         case 'array':
323                                 $args = array ();
324                                 $ret = array ('LuaMetaTable' => 0);
325                                 break;
326                         case 'get_table':
327                                 $args = array ();
328                                 $ret = array ('LuaTable' => 0);
329                                 break;
330                         case 'set_table':
331                                 $args = array (array ('LuaTable {'.$templ.'}' => 0));
332                                 $ret = array ('void' => 0);
333                                 break;
334                         default:
335                                 break;
336                         }
337                 }
338                 $classlist[luafn2class ($b['lua'])]['func'][] = array (
339                         'bind' => $b,
340                         'name' => $b['lua'],
341                         'args' => $args,
342                         'ret'  => $ret,
343                         'ref'  => true,
344                         'ext'  => true,
345                         'cand' => canonical_decl ($b)
346                 );
347                 break;
348         case "Free C Function":
349                 $funclist[luafn2class ($b['lua'])][] = array (
350                         'bind' => $b,
351                         'name' => $b['lua'],
352                         'args' => $args,
353                         'ret'  => $ret,
354                         'ref'  => false,
355                         'ext'  => true,
356                         'cand' => str_replace (':', '::', $b['lua']).'(lua_State*)'
357                 );
358                 break;
359         case "Free Function":
360         case "Free Function RefReturn":
361                 $funclist[luafn2class ($b['lua'])][] = array (
362                         'bind' => $b,
363                         'name' => $b['lua'],
364                         'args' => decl2args ($b['ldec']),
365                         'ret'  => arg2lua ($b['ret']),
366                         'ref'  => (strpos ($b['type'], "RefReturn") !== false),
367                         'cand' => canonical_decl ($b)
368                 );
369                 break;
370         case "Member Function":
371         case "Member Function RefReturn":
372         case "Member Pointer Function":
373         case "Weak/Shared Pointer Function":
374         case "Weak/Shared Pointer Function RefReturn":
375         case "Weak/Shared Null Check":
376         case "Static Member Function":
377                 checkclass ($b);
378                 $classlist[luafn2class ($b['lua'])]['func'][] = array (
379                         'bind' => $b,
380                         'name' => $b['lua'],
381                         'args' => decl2args ($b['ldec']),
382                         'ret'  => arg2lua ($b['ret']),
383                         'ref'  => (strpos ($b['type'], "RefReturn") !== false),
384                         'cand' => canonical_decl ($b)
385                 );
386                 break;
387         case "Weak/Shared Pointer Cast":
388                 checkclass ($b);
389                 $classlist[luafn2class ($b['lua'])]['cast'][] = array (
390                         'bind' => $b,
391                         'name' => $b['lua'],
392                         'args' => decl2args ($b['ldec']),
393                         'ret'  => arg2lua ($b['ret']),
394                         'ref'  => (strpos ($b['type'], "RefReturn") !== false),
395                         'cand' => canonical_decl ($b)
396                 );
397                 break;
398         case "Constant/Enum":
399         case "Constant/Enum Member":
400                 # already handled -> $consts
401                 break;
402         default:
403                 if (strpos ($b['type'], "[C] ") !== 0) {
404                         my_die ('unhandled type: ' . $b['type']);
405                 }
406                 break;
407         }
408 }
409
410
411 # step 4: collect/group/sort
412
413 # step 4a: unify weak/shared Ptr classes
414 foreach ($classlist as $ns => $cl) {
415         if (strpos ($cl['type'], ' Array') !== false) {
416                 $classlist[$ns]['arr'] = true;
417                 continue;
418         }
419         foreach ($classes as $c) {
420                 if ($c['lua'] == $ns) {
421                         if (strpos ($c['type'], 'Pointer Class') !== false) {
422                                 $classlist[$ns]['ptr'] = true;
423                                 $classlist[$ns]['cdecl'] = 'boost::shared_ptr< '.$c['decl']. ' >, boost::weak_ptr< '.$c['decl']. ' >';
424                                 break;
425                         } else {
426                                 $classlist[$ns]['cdecl'] = $c['decl'];
427                         }
428                 }
429         }
430 }
431
432 # step4b: sanity check
433 foreach ($classlist as $ns => $cl) {
434         if (isset ($classes[$ns]['parent']) && !isset ($classlist[$ns]['luaparent'])) {
435                 my_die ('missing parent class: ' . print_r ($cl, true));
436         }
437 }
438
439 # step 4c: merge free functions into classlist
440 foreach ($funclist as $ns => $fl) {
441         if (isset ($classlist[$ns])) {
442                 my_die ('Free Funcion in existing namespace: '.$ns.' '. print_r ($ns, true));
443         }
444         $classlist[$ns]['func'] = $fl;
445         $classlist[$ns]['free'] = true;
446 }
447
448 # step 4d: order to chaos
449 # no array_multisort() here, sub-types are sorted after merging parents
450 ksort ($classlist);
451
452
453 ################################################################################
454 ################################################################################
455 ################################################################################
456
457
458 #### -- split here --  ####
459
460 # from here on, only $classlist and $constlist arrays are relevant.
461 # we also pull in C++ header annotation from doxygen to $api
462
463
464 # read documentation from doxygen
465 $json = gzdecode (file_get_contents (dirname (__FILE__).'/../doc/ardourapi.json.gz'));
466 $api = array ();
467 foreach (json_decode ($json, true) as $a) {
468         if (!isset ($a['decl'])) { continue; }
469         if (empty ($a['decl'])) { continue; }
470         $canon = str_replace (' *', '*', $a['decl']);
471         $api[$canon] = $a;
472 }
473
474 # keep track of found/missing doc
475 $dox_found = 0;
476 $dox_miss = 0;
477
478 # retrive a value from $api
479 function doxydoc ($canonical_declaration) {
480         global $api;
481         global $dox_found;
482         global $dox_miss;
483         if (isset ($api[$canonical_declaration])) {
484                 $dox_found++;
485                 return $api[$canonical_declaration]['doc'];
486         }
487         // remove template namespace e.g.
488         //  "ARDOUR::Track::bounceable(boost::shared_ptr<ARDOUR::Processor>"
489         //  "ARDOUR::Track::bounceable(boost::shared_ptr<Processor>"
490         $cn = preg_replace ('/<[^>]*::([^>]*)>/', '<$1>', $canonical_declaration);
491         if (isset ($api[$cn])) {
492                 $dox_found++;
493                 return $api[$cn]['doc'];
494         }
495         #fwrite (STDERR, $canonical_declaration."\n"); # XXX DEBUG
496
497         $dox_miss++;
498         return '';
499 }
500
501 ################################################################################
502 # OUTPUT
503 ################################################################################
504
505
506 ################################################################################
507 # Helper functions
508 define ('NL', "\n");
509
510 # constructors, enums (constants) use a dot.  (e.g. "LuaOSC.Address" -> "LuaOSC.Address" )
511 function ctorname ($name) {
512         return htmlentities (str_replace (':', '.', $name));
513 }
514
515 # strip class prefix (e.g "Evoral:MidiEvent:channel"  -> "channel")
516 function shortname ($name) {
517         return htmlentities (substr ($name, strrpos ($name, ':') + 1));
518 }
519
520 # retrieve variable name from    array["VARNAME"] => FLAGS
521 function varname ($a) {
522         return array_keys ($a)[0];
523 }
524
525 # recusively collect class parents (derived classes)
526 function traverse_parent ($ns, &$inherited) {
527         global $classlist;
528         $rv = '';
529         if (isset ($classlist[$ns]['luaparent'])) {
530                 $parents = array_unique ($classlist[$ns]['luaparent']);
531                 asort ($parents);
532                 foreach ($parents as $p) {
533                         if (!empty ($rv)) { $rv .= ', '; }
534                         $rv .= typelink ($p);
535                         $inherited[$p] = $classlist[$p];
536                         traverse_parent ($p, $inherited);
537                 }
538         }
539         return $rv;
540 }
541
542 # create a cross-reference to a type (class or enum)
543 # *all* <a> links are generated here, currently anchors on a single page.
544 function typelink ($a, $short = false, $argcls = '', $linkcls = '', $suffix = '') {
545         global $classlist;
546         global $constlist;
547         if (isset($classlist[$a]['free'])) {
548                 return '<a class="'.$linkcls.'" href="#'.htmlentities ($a).'">'.($short ? shortname($a) : ctorname($a)).$suffix.'</a>';
549         } else if (in_array ($a, array_keys ($classlist))) {
550                 return '<a class="'.$linkcls.'" href="#'.htmlentities($a).'">'.($short ? shortname($a) : htmlentities($a)).$suffix.'</a>';
551         } else if (in_array ($a, array_keys ($constlist))) {
552                 return '<a class="'.$linkcls.'" href="#'.ctorname ($a).'">'.($short ? shortname($a) : ctorname($a)).$suffix.'</a>';
553         } else {
554                 return '<span class="'.$argcls.'">'.htmlentities($a).$suffix.'</span>';
555         }
556 }
557
558 # output format function arguments
559 function format_args ($args) {
560         $rv = '<span class="functionargs"> (';
561         $first = true;
562         foreach ($args as $a) {
563                 if (!$first) { $rv .= ', '; }; $first = false;
564                 $flags = $a[varname ($a)];
565                 if ($flags & 2) {
566                         $rv .= '<em>LuaTable</em> {'.typelink (varname ($a), true, 'em').'}';
567                 }
568                 elseif ($flags & 1) {
569                         $rv .= typelink (varname ($a), true, 'em', '', '&amp;');
570                 }
571                 else {
572                         $rv .= typelink (varname ($a), true, 'em');
573                 }
574         }
575         $rv .= ')</span>';
576         return $rv;
577 }
578
579 # format doxygen documentation for class-definition
580 function format_doxyclass ($cl) {
581         $rv = '';
582         if (isset ($cl['decl'])) {
583                 $doc = doxydoc ($cl['decl']);
584                 if (!empty ($doc)) {
585                         $rv.= '<div class="classdox">'.$doc.'</div>'.NL;
586                 }
587         }
588         return $rv;
589 }
590
591 # format doxygen documentation for class-members
592 function format_doxydoc ($f) {
593         $rv = '';
594         if (isset ($f['cand'])) {
595                 $doc = doxydoc ($f['cand']);
596                 if (!empty ($doc)) {
597                         $rv.= '<tr><td></td><td class="doc" colspan="2"><div class="dox">'.$doc;
598                         $rv.= '</div></td></tr>'.NL;
599                 } else if (0) { # debug
600                         $rv.= '<tr><td></td><td class="doc" colspan="2"><p>'.htmlentities($f['cand']).'</p>';
601                         $rv.= '</td></tr>'.NL;
602                 }
603         }
604         return $rv;
605 }
606
607 # usort() callback for class-members
608 function name_sort_cb ($a, $b) {
609         return strcmp ($a['name'], $b['name']);
610 }
611
612 # main output function for every class
613 function format_class_members ($ns, $cl, &$dups) {
614         $rv = '';
615         # print contructor - if any
616         if (isset ($cl['ctor'])) {
617                 usort ($cl['ctor'], 'name_sort_cb');
618                 $rv.= ' <tr><th colspan="3">Constructor</th></tr>'.NL;
619                 foreach ($cl['ctor'] as $f) {
620                         $rv.= ' <tr><td class="def">&Copf;</td><td class="decl">';
621                         $rv.= '<span class="functionname">'.ctorname ($f['name']).'</span>';
622                         $rv.= format_args ($f['args']);
623                         $rv.= '</td><td class="fill"></td></tr>'.NL;
624                         # doxygen documentation (may be empty)
625                         $rv.= format_doxydoc($f);
626                 }
627         }
628
629         # strip duplicates (inherited or derived methods)
630         # e.g  AudioTrack -> Track -> Route -> SessionObject -> Stateful
631         # all 5 have "isnil()"
632         $nondups = array ();
633         if (isset ($cl['func'])) {
634                 foreach ($cl['func'] as $f) {
635                         if (in_array (stripclass ($ns, $f['name']), $dups)) { continue; }
636                         $nondups[] = $f;
637                 }
638         }
639
640         # print methods - if any
641         if (count ($nondups) > 0) {
642                 usort ($nondups, 'name_sort_cb');
643                 $rv.= ' <tr><th colspan="3">Methods</th></tr>'.NL;
644                 foreach ($nondups as $f) {
645                         $dups[] = stripclass ($ns, $f['name']);
646                         # return value/type
647                         $rv.= ' <tr><td class="def">';
648                         if ($f['ref'] && isset ($f['ext'])) {
649                                 # external C functions
650                                 $rv.= '<em>'.varname ($f['ret']).'</em>';
651                         } elseif ($f['ref'] && varname ($f['ret']) == 'void') {
652                                 # void functions with reference args
653                                 $rv.= '<em>LuaTable</em>(...)';
654                         } elseif ($f['ref']) {
655                                 # functions with reference args and return value
656                                 $rv.= '<em>LuaTable</em>('.typelink (varname ($f['ret']), true, 'em').', ...)';
657                         } else {
658                                 # normal class members
659                                 $rv.= typelink (varname ($f['ret']), true, 'em');
660                         }
661                         # function declaration and arguments
662                         $rv.= '</td><td class="decl">';
663                         $rv.= '<span class="functionname"><abbr title="'.htmlentities($f['bind']['decl']).'">'.stripclass ($ns, $f['name']).'</abbr></span>';
664                         $rv.= format_args ($f['args']);
665                         $rv.= '</td><td class="fill"></td></tr>'.NL;
666                         # doxygen documentation (may be empty)
667                         $rv.= format_doxydoc($f);
668                 }
669         }
670         # print cast - if any
671         if (isset ($cl['cast'])) {
672                 usort ($cl['cast'], 'name_sort_cb');
673                 $rv.= ' <tr><th colspan="3">Cast</th></tr>'.NL;
674                 foreach ($cl['cast'] as $f) {
675                         $rv.= ' <tr><td class="def">';
676                         $rv.= typelink (varname ($f['ret']), true, 'em');
677                         # function declaration and arguments
678                         $rv.= '</td><td class="decl">';
679                         $rv.= '<span class="functionname"><abbr title="'.htmlentities($f['bind']['decl']).'">'.stripclass ($ns, $f['name']).'</abbr></span>';
680                         $rv.= format_args ($f['args']);
681                         $rv.= '</td><td class="fill"></td></tr>'.NL;
682                         # doxygen documentation (may be empty)
683                         $rv.= format_doxydoc($f);
684                 }
685         }
686
687         # print data members - if any
688         if (isset ($cl['data'])) {
689                 usort ($cl['data'], 'name_sort_cb');
690                 $rv.= ' <tr><th colspan="3">Data Members</th></tr>'.NL;
691                 foreach ($cl['data'] as $f) {
692                         $rv.= ' <tr><td class="def">'.typelink (array_keys ($f['ret'])[0], false, 'em').'</td><td class="decl">';
693                         $rv.= '<span class="functionname">'.stripclass ($ns, $f['name']).'</span>';
694                         $rv.= '</td><td class="fill"></td></tr>'.NL;
695                 }
696         }
697         return $rv;
698 }
699
700
701 ################################################################################
702 # Start Output
703
704 if ($HTMLOUTPUT) {
705
706 ?><!DOCTYPE html>
707 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
708 <head>
709 <title>Ardour Lua Bindings</title>
710 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
711 <style type="text/css">
712 div.header         { text-align:center; }
713 div.header h2      { margin:0; }
714 div.header p       { margin:.25em; text-align:center; }
715 div.luafooter      { text-align:center; font-size:80%; color: #888; margin: 2em 0; }
716 #luaref            { max-width:60em; margin: 1em auto; }
717
718 #luaref h2                 { margin:2em 0 0 0; padding:0em; border-bottom: 1px solid black; }
719 #luaref h3.cls             { margin:2em 0 0 0; padding: 0 0 0 1em; border: 1px dashed #6666ee; }
720 #luaref h3.cls abbr        { text-decoration:none; cursor:default; }
721 #luaref h4.cls             { margin:1em 0 0 0; }
722 #luaref h3.class           { background-color: #aaee66; }
723 #luaref h3.enum            { background-color: #aaaaaa; }
724 #luaref h3.pointerclass    { background-color: #eeaa66; }
725 #luaref h3.array           { background-color: #66aaee; }
726 #luaref h3.opaque          { background-color: #6666aa; }
727 #luaref p                  { text-align: justify; }
728 #luaref p.cdecl            { text-align: right; float:right; font-size:90%; margin:0; padding: 0 0 0 1em; }
729 #luaref ul.classindex      { columns: 2; -webkit-columns: 2; -moz-columns: 2; }
730 #luaref div.clear          { clear:both; }
731 #luaref p.classinfo        { margin: .25em 0; }
732 #luaref div.code           { width:80%; margin:.5em auto; }
733 #luaref div.code div       { width:45%; }
734 #luaref div.code pre       { line-height: 1.2em; margin: .25em 0; }
735 #luaref div.code samp      { color: green; font-weight: bold; background-color: #eee; }
736 #luaref div.classdox       { padding: .1em 1em; }
737 #luaref div.classdox p     { margin: .5em 0 .5em .6em; }
738 #luaref div.classdox p     { margin: .5em 0 .5em .6em; }
739 #luaref div.classdox       { padding: .1em 1em; }
740 #luaref div.classdox p     { margin: .5em 0 .5em .6em; }
741 #luaref table.classmembers { width: 100%; }
742 #luaref table.classmembers th      { text-align:left; border-bottom:1px solid black; padding-top:1em; }
743 #luaref table.classmembers td.def  { text-align:right; padding-right:.5em;  white-space: nowrap; }
744 #luaref table.classmembers td.decl { text-align:left; padding-left:.5em; white-space: nowrap; }
745 #luaref table.classmembers td.doc  { text-align:left; padding-left:.6em; line-height: 1.2em; font-size:80%; }
746 #luaref table.classmembers td.doc div.dox {background-color:#eee; padding: .1em 1em; }
747 #luaref table.classmembers td.doc p { margin: .5em 0; }
748 #luaref table.classmembers td.doc p.para-brief { font-size:120%; }
749 #luaref table.classmembers td.doc p.para-returns { font-size:120%; }
750 #luaref table.classmembers td.doc dl { font-size:120%; line-height: 1.3em; }
751 #luaref table.classmembers td.doc dt { font-style: italic; }
752 #luaref table.classmembers td.fill { width: 99%; }
753 #luaref table.classmembers span.em { font-style: italic; }
754 #luaref span.functionname abbr     { text-decoration:none; cursor:default; }
755 </style>
756 </head>
757 <body>
758 <div class="header">
759 <h2>Ardour Lua Bindings</h2>
760 <p>
761 <a href="#h_classes">Class Documentation</a>
762 &nbsp;|&nbsp;
763 <a href="#h_enum">Enum/Constants</a>
764 &nbsp;|&nbsp;
765 <a href="#h_index">Index</a>
766 </p>
767 </div>
768
769 <!-- #### SNIP #### !-->
770
771 <?php
772
773 } else {
774
775 ?>
776 ---
777 layout: default
778 style: luadoc
779 title: Class Reference
780 ---
781
782 <p class="warning">
783 This documention is far from complete may be inaccurate and subject to change.
784 </p>
785
786 <?php
787 }
788 ?>
789
790 <div id="luaref">
791
792 <?php
793
794 ################################################################################
795 # some general documentation -- should really go elsehere
796
797 ?>
798
799 <h2 id="h_intro">Overview</h2>
800 <p>
801 The top-level entry point are <?=typelink('ARDOUR:Session')?> and <?=typelink('ArdourUI:Editor')?>.
802 Most other Classes are used indirectly starting with a Session function. e.g. Session:get_routes().
803 </p>
804 <p>
805 A few classes are dedicated to certain script types, e.g. Lua DSP processors have exclusive access to
806 <?=typelink('ARDOUR:DSP')?> and <?=typelink('ARDOUR:ChanMapping')?>. Action Hooks Scripts to
807 <?=typelink('LuaSignal:Set')?> etc.
808 </p>
809 <p>
810 Detailed documentation (parameter names, method description) is not yet available. Please stay tuned.
811 </p>
812 <h3>Short introduction to Ardour classes</h3>
813 <p>
814 Ardour's structure is object oriented. The main object is the Session. A Session contains Audio Tracks, Midi Tracks and Busses.
815 Audio and Midi tracks are derived from a more general "Track" Object,  which in turn is derived from a "Route" (aka Bus).
816 (We say "An Audio Track <em>is-a</em> Track <em>is-a</em> Route").
817 Tracks contain specifics. For Example a track <em>has-a</em> diskstream (for file i/o).
818 </p>
819 <p>
820 Operations are performed on objects. One gets a reference to an object and then calls a method.
821 e.g <code>obj = Session:route_by_name("Audio")   obj:set_name("Guitar")</code>.
822 </p>
823 <p>
824 Lua automatically follows C++ class inheritance. e.g one can directly call all SessionObject and Route methods on Track object. However lua does not automatically promote objects. A Route object which just happens to be a Track needs to be explicily cast to a Track. Methods for casts are provided with each class. Note that the cast may fail and return a <em>nil</em> reference.
825 </p>
826 <p>
827 Likewise multiple inheritance is a <a href="http://www.lua.org/pil/16.3.html">non-trivial issue</a> in lua. To avoid performance penalties involved with lookups, explicit casts are required in this case. One example is <?=typelink('ARDOUR:SessionObject')?> which is-a StatefulDestructible which inhertis from both Stateful and Destructible.
828 </p>
829 <p>
830 Object lifetimes are managed by the Session. Most Objects cannot be directly created, but one asks the Session to create or destroy them. This is mainly due to realtime constrains:
831 you cannot simply remove a track that is currently processing audio. There are various <em>factory</em> methods for object creation or removal.
832 </p>
833 <h3>Pass by Reference</h3>
834 <p>
835 Since lua functions are closures, C++ methods that pass arguments by reference cannot be used as-is.
836 All parameters passed to a C++ method which uses references are returned as Lua Table.
837 If the C++ method also returns a value it is prefixed. Two parameters are returned: the value and a Lua Table holding the parameters.
838 </p>
839
840 <div class="code">
841         <div style="float:left;">C++
842
843 <pre><code class="cxx">void set_ref (int&amp; var, long&amp; val)
844 {
845         printf ("%d %ld\n", var, val);
846         var = 5;
847         val = 7;
848 }
849 </code></pre>
850
851         </div>
852         <div style="float:right;">Lua
853
854 <pre><code class="lua">local var = 0;
855 ref = set_ref (var, 2);
856 -- output from C++ printf()
857 </code><samp class="lua">0 2</samp><code>
858 -- var is still 0 here
859 print (ref[1], ref[2])
860 </code><samp class="lua">5 7</samp></pre>
861
862         </div>
863 </div>
864 <div class="clear"></div>
865 <div class="code">
866         <div style="float:left;">
867
868 <pre><code class="cxx">int set_ref2 (int &amp;var, std::string unused)
869 {
870         var = 5;
871         return 3;
872 }
873 </code></pre>
874
875         </div>
876         <div style="float:right;">
877 <pre><code class="lua">rv, ref = set_ref2 (0, "hello");
878 print (rv, ref[1], ref[2])
879 </code><samp class="lua">3 5 hello</samp></pre>
880         </div>
881 </div>
882 <div class="clear"></div>
883
884 <h3>Pointer Classes</h3>
885 <p>
886 Libardour makes extensive use of reference counted <code>boost::shared_ptr</code> to manage lifetimes.
887 The Lua bindings provide a complete abstration of this. There are no pointers in lua.
888 For example a <?=typelink('ARDOUR:Route')?> is a pointer in C++, but lua functions operate on it like it was a class instance.
889 </p>
890 <p>
891 <code>shared_ptr</code> are reference counted. Once assigned to a lua variable, the C++ object will be kept and remains valid.
892 It is good practice to assign references to lua <code>local</code> variables or reset the variable to <code>nil</code> to drop the ref.
893 </p>
894 <p>
895 All pointer classes have a <code>isnil ()</code> method. This is for two cases:
896 Construction may fail. e.g. <code><?=typelink('ARDOUR:LuaAPI')?>.newplugin()</code>
897 may not be able to find the given plugin and hence cannot create an object.
898 </p>
899 <p>
900 The second case if for <code>boost::weak_ptr</code>. As opposed to <code>boost::shared_ptr</code> weak-pointers are not reference counted.
901 The object may vanish at any time.
902 If lua code calls a method on a nil object, the interpreter will raise an exception and the script will not continue.
903 This is not unlike <code>a = nil a:test()</code> which results in en error "<em>attempt to index a nil value</em>".
904 </p>
905 <p>
906 From the lua side of things there is no distinction between weak and shared pointers. They behave identically.
907 Below they're inidicated in orange and have an arrow to indicate the pointer type.
908 Pointer Classes cannot be created in lua scripts. It always requires a call to C++ to create the Object and obtain a reference to it.
909 </p>
910
911
912 <?php
913
914 #################################
915 # Main output function -- Classes
916
917 echo '<h2 id="h_classes">Class Documentation</h2>'.NL;
918 foreach ($classlist as $ns => $cl) {
919         $dups = array ();
920         $tbl =  format_class_members ($ns, $cl, $dups);
921
922         # format class title - depending on type
923         if (empty ($tbl)) {
924                 # classes with no members (no ctor, no methods, no data)
925                 echo '<h3 id="'.htmlentities ($ns).'" class="cls opaque"><abbr title="Opaque Object">&empty;</abbr>&nbsp;'.htmlentities ($ns).'</h3>'.NL;
926         }
927         else if (isset ($classlist[$ns]['free'])) {
928                 # free functions (no class)
929                 echo '<h3 id="'.htmlentities ($ns).'" class="cls freeclass"><abbr title="Namespace">&Nopf;</abbr>&nbsp;'.ctorname($ns).'</h3>'.NL;
930         }
931         else if (isset ($classlist[$ns]['arr'])) {
932                 # C Arrays
933                 echo '<h3 id="'.htmlentities ($ns).'" class="cls array"><abbr title="C Array">&ctdot;</abbr>&nbsp;'.htmlentities ($ns).'</h3>'.NL;
934         }
935         else if (isset ($classlist[$ns]['ptr'])) {
936                 # Pointer Classes
937                 echo '<h3 id="'.htmlentities ($ns).'" class="cls pointerclass"><abbr title="Pointer Class">&Rarr;</abbr>&nbsp;'. htmlentities ($ns).'</h3>'.NL;
938         }
939         else {
940                 # Normal Class
941                 echo '<h3 id="'.htmlentities ($ns).'" class="cls class"><abbr title="Class">&comp;</abbr>&nbsp;'.htmlentities ($ns).'</h3>'.NL;
942         }
943
944         # show original C++ declaration
945         if (isset ($cl['decl'])) {
946                 echo '<p class="cdecl"><em>C&#8225;</em>: '.htmlentities ($cl['cdecl']).'</p>'.NL;
947         }
948
949         # print class inheritance (direct parent *name* only)
950         $inherited = array ();
951         $isa = traverse_parent ($ns, $inherited);
952         if (!empty ($isa)) {
953                 echo ' <p class="classinfo">is-a: '.$isa.'</p>'.NL;
954         }
955         echo '<div class="clear"></div>'.NL;
956
957
958         # class documentation (if any)
959         echo format_doxyclass ($cl);
960
961         # member documentation
962         if (empty ($tbl)) {
963                 echo '<p class="classinfo">This class object is only used indirectly as return-value and function-parameter. It provides no methods by itself.</p>'.NL;
964         } else {
965                 echo '<table class="classmembers">'.NL;
966                 echo $tbl;
967                 echo ' </table>'.NL;
968         }
969
970         # traverse parent classes (all inherited members)
971         foreach ($inherited as $pns => $pcl) {
972                 $tbl = format_class_members ($pns, $pcl, $dups);
973                 if (!empty ($tbl)) {
974                         echo '<h4 class="cls">Inherited from '.$pns.'</h4>'.NL;
975                         echo '<table class="classmembers">'.NL;
976                         echo $tbl;
977                         echo '</table>'.NL;
978                 }
979         }
980 }
981
982 ####################
983 # Enum and Constants
984
985 echo '<h2 id="h_enum">Enum/Constants</h2>'.NL;
986 foreach ($constlist as $ns => $cs) {
987         echo '<h3 id="'.ctorname ($ns).'" class="cls enum"><abbr title="Enum">&isin;</abbr>&nbsp;'.ctorname ($ns).'</h3>'.NL;
988         echo '<ul class="enum">'.NL;
989         foreach ($cs as $c) {
990                 echo '<li class="const">'.ctorname ($c['lua']).'</li>'.NL;
991         }
992         echo '</ul>'.NL;
993 }
994
995 ######################
996 # Index of all classes
997
998 echo '<h2 id="h_index" >Class Index</h2>'.NL;
999 echo '<ul class="classindex">'.NL;
1000 foreach ($classlist as $ns => $cl) {
1001         echo '<li>'.typelink($ns).'</li>'.NL;
1002 }
1003 echo '</ul>'.NL;
1004
1005
1006 # see how far there is still to go...
1007 fwrite (STDERR, "Found $dox_found annotations. missing: $dox_miss\n");
1008 echo '<!-- '.$dox_found.' / '.$dox_miss.' !-->'.NL;
1009
1010 ?>
1011 </div>
1012 <div class="luafooter">Ardour <?=$ardourversion?> &nbsp;-&nbsp; <?=date('r')?></div>
1013 <?php
1014
1015 if ($HTMLOUTPUT) {
1016         echo '<!-- #### SNIP #### !-->'.NL;
1017         echo '</body>'.NL;
1018         echo '</html>'.NL;
1019 }