Debian Codegraph @

tools/extra_defs_gen/generate_plugin_gmmproc_file.cc

raw symbols SCIP
1 /*
2 * Copyright 2008 The gstreamermm Development Team
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <glibmm.h>
20 #include <gst/gst.h>
21 #include <algorithm>
22 #include <iostream>
23
24 namespace
25 {
26
27 static gchar* nmspace = 0;
28 static gchar* defsFile = 0;
29 static gchar* target = 0;
30
31 static std::string pluginName;
32 static std::string cTypeName;
33 static std::string cParentTypeName;
34 static std::string cppTypeName;
35 static std::string cppParentTypeName;
36 static std::string castMacro;
37 static std::string parentInclude;
38 static std::string parentNameSpace;
39
40 static GType type = 0;
41
42 // To add a base class that is already wrapped to the list of wrapped base
43 // classes, insert it alphabetically below.
44 static const char *const wrappedBaseClasses[] =
45 {
46 "GstAudioFilter",
47 "GstAudioSink",
48 "GstAudioSrc",
49 "GstAudioBaseSink",
50 "GstAudioBaseSrc",
51 "GstBaseSink",
52 "GstBaseSrc",
53 "GstBaseTransform",
54 "GstBin",
55 "GstCddaParanoiaSrc",
56 "GstElement",
57 "GstPipeline",
58 "GstPushSrc",
59 "GstVideoSink"
60 };
61
62 static bool gst_type_is_a_pointer(GType gtype)
63 {
64 return (g_type_is_a(gtype, G_TYPE_OBJECT) ||
65 g_type_is_a(gtype, G_TYPE_BOXED));
66 }
67
68 static std::string to_lowercase(const std::string& str)
69 {
70 std::string result (str.size(), '\0');
71 std::transform(str.begin(), str.end(), result.begin(), &Glib::Ascii::tolower);
72 return result;
73 }
74
75 static std::string get_cast_macro(const std::string& typeName)
76 {
77 std::string result;
78
79 std::string::const_iterator iter = typeName.end();
80
81 bool prev_is_upper = false;
82 bool prev_is_lower = true; // Going backwards so last char should be lower.
83 int underscore_char_count = 0; // Used to count characters between underscores.
84
85 for(--iter; iter != typeName.begin(); --iter, ++underscore_char_count)
86 {
87 if(Glib::Ascii::isupper(*iter))
88 {
89 result.insert(0, 1, *iter);
90 if(prev_is_lower && underscore_char_count > 1)
91 {
92 result.insert(0, 1, '_');
93 underscore_char_count = 0;
94 }
95 prev_is_upper = true;
96 prev_is_lower = false;
97 }
98 else
99 {
100 if(prev_is_upper && underscore_char_count > 1)
101 {
102 result.insert(0, 1, '_');
103 underscore_char_count = 0;
104 }
105 result.insert(0, 1, Glib::Ascii::toupper(*iter));
106 prev_is_upper = false;
107 prev_is_lower = true;
108 }
109 }
110
111 // Insert first character (not included in the for loop).
112 result.insert(0, 1, Glib::Ascii::toupper(*iter));
113
114 return result;
115 }
116
117 static bool is_wrapped_base_class(const std::string& cTypeName)
118 {
119 return std::binary_search(&wrappedBaseClasses[0],
120 &wrappedBaseClasses[G_N_ELEMENTS(wrappedBaseClasses)],
121 cTypeName);
122 }
123
124 static bool is_plugin(const std::string& cTypeName)
125 {
126 const std::string element = to_lowercase(cTypeName.substr(3));
127
128 if (GstElementFactory *const fact = gst_element_factory_find(element.c_str()))
129 {
130 g_object_unref(fact);
131 return true;
132 }
133 return false;
134 }
135
136 static std::string get_property_wrap_statements(std::string& includeMacroCalls,
137 std::string& enumWrapStatements,
138 std::string& enumGTypeFunctionDefinitions)
139 {
140 std::string wrapStatements;
141
142 //Get the list of properties:
143 GParamSpec** ppParamSpec = 0;
144 guint iCount = 0;
145 if(G_TYPE_IS_OBJECT(type))
146 {
147 GObjectClass* pGClass = G_OBJECT_CLASS(g_type_class_ref(type));
148 ppParamSpec = g_object_class_list_properties (pGClass, &iCount);
149 g_type_class_unref(pGClass);
150 }
151 else if(G_TYPE_IS_INTERFACE(type))
152 {
153 gpointer pGInterface = g_type_default_interface_ref(type);
154 if(pGInterface) //We check because this fails for G_TYPE_VOLUME, for some reason.
155 {
156 ppParamSpec = g_object_interface_list_properties(pGInterface, &iCount);
157 g_type_default_interface_unref(pGInterface);
158 }
159 }
160
161 //This extra check avoids an occasional crash, for instance for GVolume
162 if(!ppParamSpec)
163 iCount = 0;
164
165 for(guint i = 0; i < iCount; i++)
166 {
167 GParamSpec* pParamSpec = ppParamSpec[i];
168 if(pParamSpec)
169 {
170 GType propertyGType = pParamSpec->value_type;
171 GType ownerGType = pParamSpec->owner_type;
172
173 if(ownerGType == type)
174 {
175 //Name and type:
176 std::string propertyName = g_param_spec_get_name(pParamSpec);
177 std::string propertyCType = g_type_name(propertyGType);
178 if (gst_type_is_a_pointer(propertyGType))
179 propertyCType += '*';
180
181 if((G_TYPE_IS_ENUM(propertyGType) || G_TYPE_IS_FLAGS(propertyGType)))
182 {
183 std::string enumPrefix = propertyCType.substr(0, 3);
184 std::string propertyCppType = propertyCType.substr(3);
185
186 enumWrapStatements +=
187 "_WRAP_PLUGIN_ENUM(" + enumPrefix + ',' + propertyCppType + ')';
188 enumGTypeFunctionDefinitions +=
189 "_PLUGIN_ENUM_GET_TYPE_FUNC(" + propertyCType + ')';
190 }
191
192 wrapStatements += " _WRAP_PROPERTY(\"" + propertyName
193 + "\", _TRANSLATE(" + propertyCType + ", `return'))\n";
194
195 includeMacroCalls += "_TRANSLATION_INCLUDE(" + propertyCType + ")dnl\n";
196 }
197 }
198 }
199
200 g_free(ppParamSpec);
201 return wrapStatements;
202 }
203
204 static std::string get_method_name(const std::string& signalName)
205 {
206 std::string result = signalName;
207 std::replace(result.begin(), result.end(), '-', '_');
208 return result;
209 }
210
211 static std::string get_c_enum_definition_macro(GType enumGType,
212 std::string& enumCType)
213 {
214 std::string result;
215
216 if(G_TYPE_IS_ENUM(enumGType))
217 {
218 GEnumClass* enumClass = G_ENUM_CLASS(g_type_class_ref(enumGType));
219 if(enumClass)
220 {
221 result += "_C_ENUM_DEFINITION(" + enumCType + ',';
222 for(guint i = 0; i < enumClass->n_values; i++)
223 {
224 std::stringstream stream;
225 result += std::string(enumClass->values[i].value_nick) + ',';
226 stream << enumClass->values[i].value;
227 result += stream.str();
228 if(i < enumClass->n_values - 1)
229 result += ',';
230 }
231 result += ')';
232 }
233 g_type_class_unref(enumClass);
234 }
235 else
236 {
237 GFlagsClass* flagsClass = G_FLAGS_CLASS(g_type_class_ref(enumGType));
238 if(flagsClass)
239 {
240 result += "_C_ENUM_DEFINITION(" + enumCType + ',';
241 for(guint i = 0; i < flagsClass->n_values; i++)
242 {
243 std::stringstream stream;
244 result += std::string(flagsClass->values[i].value_nick) + ',';
245 stream << flagsClass->values[i].value;
246 result += stream.str();
247 if(i < flagsClass->n_values - 1)
248 result += ',';
249 }
250 result += ')';
251 }
252 g_type_class_unref(flagsClass);
253 }
254 return result;
255 }
256
257 static std::string get_signal_wrap_statements(std::string& includeMacroCalls,
258 std::string& cEnumDefinitions,
259 std::string& enumWrapStatements,
260 std::string& actionSignalsMethodDeclarations,
261 std::string& enumGTypeFunctionDefinitions,
262 std::string& actionSignalsMethodDefinitions)
263 {
264 std::string wrapStatements;
265
266 gpointer gclass_ref = 0;
267 gpointer ginterface_ref = 0;
268
269 if(G_TYPE_IS_OBJECT(type))
270 gclass_ref = g_type_class_ref(type); //Ensures that class_init() is called.
271 else if(G_TYPE_IS_INTERFACE(type))
272 ginterface_ref = g_type_default_interface_ref(type); //install signals.
273
274 //Get the list of signals:
275 guint iCount = 0;
276 guint* pIDs = g_signal_list_ids(type, &iCount);
277
278 //Loop through the list of signals:
279 if(pIDs)
280 {
281 for(guint i = 0; i < iCount; i++)
282 {
283 std::string convertMacros;
284 std::string wrapStatement;
285 std::string pluginEnumsTranslationMacros;
286
287 std::string actionSignalMethodDeclaration;
288 std::string actionSignalReturnDeclaration;
289 std::string actionSignalReturnStatement;
290 std::string actionSignalEmitSignalStatement;
291 std::string actionSignalMethodDefinition;
292
293 guint signal_id = pIDs[i];
294
295 //Name:
296 std::string signalName = g_signal_name(signal_id);
297 std::string signalMethodName = get_method_name(signalName);
298
299 //Other information about the signal:
300 GSignalQuery signalQuery = { 0, 0, 0, GSignalFlags(0), 0, 0, 0, };
301 g_signal_query(signal_id, &signalQuery);
302
303 //Return type:
304 GType returnGType = signalQuery.return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
305
306 std::string returnCType = g_type_name(returnGType);
307 if (gst_type_is_a_pointer(returnGType))
308 returnCType += '*';
309
310 std::string returnCTypeTranslation = "_TRANSLATE(" + returnCType +
311 ", `return')";
312
313 // Start building action signal method declaration, definition and
314 // emit statement if dealing with an action signal.
315 if(signalQuery.signal_flags & G_SIGNAL_ACTION)
316 {
317 // Start the method declaration with a Doxygen comment about its
318 // purpose.
319 actionSignalMethodDeclaration = " /** This is a convenience method "
320 "for the action signal\n * signal_" + signalMethodName + "().\n"
321 " */\n";
322
323 // Append the actual method declaration.
324 actionSignalMethodDeclaration += " " + returnCTypeTranslation + ' ' +
325 signalMethodName + '(';
326
327 // Start the method definition.
328 actionSignalMethodDefinition = returnCTypeTranslation + ' ' +
329 cppTypeName + "::" + signalMethodName + '(';
330
331 // Start the g_signal_emit() statement that will go in the method
332 // body.
333 actionSignalEmitSignalStatement =
334 " g_signal_emit_by_name(gobj(), \"" + signalName + "\"";
335
336 // Generate return declaration and return statement if not a void
337 // return.
338 if (returnCType != "void")
339 {
340 actionSignalReturnDeclaration = " " + returnCType + " result;\n";
341 actionSignalReturnStatement = (std::string)" return " +
342 "_CONVERT(``" + returnCType + "'', _LQ()" +
343 returnCTypeTranslation + "_RQ(), ``" + "result'');\n";
344 }
345
346 // Insert a newline above the action signal wrapping statements to
347 // separte it since it will have a Doxygen comment.
348 convertMacros += '\n';
349 }
350
351 // Check for an enum first and attempt to generate _WRAP_ENUM() and
352 // _TRANSLATION() macros if it is not wrapped (see plugingen_base.m4
353 // file for docs).
354 if(G_TYPE_IS_ENUM(returnGType) || G_TYPE_IS_FLAGS(returnGType))
355 {
356 std::string enumPrefix = returnCType.substr(0, 3);
357 std::string enumCppType = returnCType.substr(3);
358
359 enumWrapStatements += "_WRAP_PLUGIN_ENUM(" + enumPrefix + ','
360 + enumCppType + ')';
361
362 enumGTypeFunctionDefinitions +=
363 "_PLUGIN_ENUM_GET_TYPE_FUNC(" + returnCType + ')';
364
365 cEnumDefinitions += get_c_enum_definition_macro(returnGType,
366 returnCType);
367
368 // Define a translation for the plug-in enum in case the enum is
369 // used in an action signal method. These translations will be
370 // prepended to the action signals method definitions in the ccg file.
371 pluginEnumsTranslationMacros += "_TRANSLATION(`" + returnCType +
372 "', `" + enumPrefix + "::" + enumCppType + "', `" + enumPrefix +
373 "::" + enumCppType + "')";
374 }
375 else if(gst_type_is_a_pointer(returnGType))
376 {
377 // Boxed type returns for signals need special conversions because
378 // when unwrapping them, gobj_copy() should be used instead of just
379 // gobj() to guard against losing the original with a temporary
380 // wrapper.
381 if(g_type_is_a(returnGType, G_TYPE_BOXED))
382 {
383 G_GNUC_BEGIN_IGNORE_DEPRECATIONS // temporary disable deprecations because of G_TYPE_VALUE_ARRAY
384 // Unwrapping conversion:
385 std::string access_operator = (returnGType != G_TYPE_ARRAY &&
386 returnGType != G_TYPE_VALUE_ARRAY &&
387 returnGType != GST_TYPE_TAG_LIST &&
388 returnGType != GST_TYPE_STRUCTURE) ? "->" : ".";
389 G_GNUC_END_IGNORE_DEPRECATIONS
390 // For GValueArray and TagList, RefPtr is not generated, and we need to use '.' operator.
391 convertMacros += "#m4 _CONVERSION(_LQ()" + returnCTypeTranslation +
392 "_RQ(), ``" + returnCType + "'', ``($3)" + access_operator + "gobj_copy()'')\n";
393
394 // Also include a wrapping conversion:
395
396 if(returnGType == GST_TYPE_TAG_LIST)
397 {
398 // Dealing with a GstTagList* return which has a special
399 // Glib::wrap() because of the conflict with the Glib::wrap() for
400 // GstStructure* (GstTagList is infact a GstStructure).
401 convertMacros += "#m4 _CONVERSION(``" + returnCType + "'', _LQ()" +
402 returnCTypeTranslation + "_RQ(), ``Glib::wrap_taglist($3)'')\n";
403 }
404 else
405 {
406 // Dealing with a regular boxed type return.
407 convertMacros += "#m4 _CONVERSION(``" + returnCType + "'', "
408 "_LQ()" + returnCTypeTranslation + "_RQ(), ``Glib::wrap($3)'')\n";
409 }
410 }
411 else
412 {
413 // Dealing with a RefPtr<> return so include a wrapping conversion
414 // just so these conversions can be automatic for plug-ins and not
415 // needed in the global convert file. (An unwrapping conversion will
416 // already probably be included in the global convert file).
417 convertMacros += "#m4 _CONVERSION(``" + returnCType +
418 "'', _LQ()" + returnCTypeTranslation + "_RQ(), " +
419 "``Glib::wrap($3)'')\n";
420 }
421 }
422
423 includeMacroCalls += "_TRANSLATION_INCLUDE(" + returnCType + ")dnl\n";
424
425 // Start the wrap statement with a comment about action signals if
426 // dealing with an action signal.
427 if(signalQuery.signal_flags & G_SIGNAL_ACTION)
428 {
429 wrapStatement =
430 " /** This is an action signal which is designed to be used "
431 "as a method. To \n * do that, use its convenience method, " +
432 signalMethodName + "().\n */\n";
433 }
434
435 wrapStatement += " _WRAP_SIGNAL(" + returnCTypeTranslation + ' ' +
436 signalMethodName + '(';
437
438 //Loop through the list of parameters:
439 const GType* pParameters = signalQuery.param_types;
440 if(pParameters)
441 {
442 for(unsigned i = 0; i < signalQuery.n_params; i++)
443 {
444 GType paramGType = pParameters[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
445
446 //Parameter name:
447 //TODO: How can we get the real parameter name?
448 gchar* pchNum = g_strdup_printf("arg%u", i);
449 std::string paramName = std::string(pchNum);
450 g_free(pchNum);
451
452 std::string paramCType = g_type_name(paramGType);
453 if (gst_type_is_a_pointer(paramGType))
454 paramCType += '*';
455
456 std::string paramCTypeTranslation = "_TRANSLATE(" + paramCType +
457 ",`param')";
458
459 includeMacroCalls += "_TRANSLATION_INCLUDE(" + paramCType + ")dnl\n";
460
461 // Continue building action signal method declaration, definition
462 // and emit statement if it is an action signal.
463 if(signalQuery.signal_flags & G_SIGNAL_ACTION)
464 {
465 actionSignalMethodDeclaration += paramCTypeTranslation + ' ' +
466 paramName;
467
468 actionSignalMethodDefinition += paramCTypeTranslation + ' ' +
469 paramName;
470
471 actionSignalEmitSignalStatement += ", _CONVERT(_LQ()" +
472 paramCTypeTranslation + "_RQ(), ``" + paramCType + "'', ``" +
473 paramName + "'')";
474 }
475
476 // Include wrapping conversions for signal parameters. (Unwrapping
477 // conversions will already probably be defined in the global
478 // convert file). Also try to wrap plug-in specific enums:
479
480 if(G_TYPE_IS_ENUM(paramGType) || G_TYPE_IS_FLAGS(paramGType))
481 {
482 // Check for an enum first and attempt to generate _WRAP_ENUM() and
483 // _TRANSLATION() macros and other necessary code if it is not
484 // wrapped (see plugingen_base.m4 file for docs).
485 std::string enumPrefix = paramCType.substr(0, 3);
486 std::string enumCppType = paramCType.substr(3);
487
488 enumWrapStatements += "_WRAP_PLUGIN_ENUM(" + enumPrefix + ',' +
489 enumCppType + ')';
490
491 enumGTypeFunctionDefinitions +=
492 "_PLUGIN_ENUM_GET_TYPE_FUNC(" + paramCType + ')';
493
494 cEnumDefinitions +=
495 get_c_enum_definition_macro(paramGType, paramCType);
496
497 // Define a translation for the plug-in enum in case the enum is
498 // used in an action signal method. These translations will be
499 // prepended to the action signals method definitions in the
500 // ccg file.
501 pluginEnumsTranslationMacros += "_TRANSLATION(`" + paramCType +
502 "', `" + enumPrefix + "::" + enumCppType + "', `" + enumPrefix +
503 "::" + enumCppType + "')";
504 }
505 if(gst_type_is_a_pointer(paramGType))
506 {
507 if(paramGType == GST_TYPE_TAG_LIST)
508 {
509 // Dealing with a GstTagList* which has a special Glib::wrap()
510 // because of the conflict with the Glib::wrap() for
511 // GstStructure* (GstTagList is in fact a GstStructure).
512 convertMacros += "#m4 _CONVERSION(``" + paramCType + "'', "
513 "_LQ()" + paramCTypeTranslation + "_RQ(), "
514 "``Glib::wrap_taglist($3, true)'')\n";
515 }
516 else
517 {
518 // Dealing with reference counted parameter or a boxed type.
519 convertMacros += "#m4 _CONVERSION(``" + paramCType +
520 "'', _LQ()" + paramCTypeTranslation + "_RQ(), " +
521 "``Glib::wrap($3, true)'')\n";
522 }
523 }
524
525 wrapStatement += paramCTypeTranslation + ' ' + paramName;
526
527 if(i < signalQuery.n_params - 1)
528 {
529 wrapStatement += ", ";
530
531 // Append commas to action signal method declaration and
532 // definition if it is an action signal. The emit statement
533 // pre-pends the commas so adding commas to it is not done here.
534 if(signalQuery.signal_flags & G_SIGNAL_ACTION)
535 {
536 actionSignalMethodDeclaration += ", ";
537 actionSignalMethodDefinition += ", ";
538 }
539 }
540 }
541 }
542
543 wrapStatement += "), \"" + signalName + "\", no_default_handler)\n";
544
545 // Finish off the action signal declaration and definition if this
546 // is an action signal.
547 if(signalQuery.signal_flags & G_SIGNAL_ACTION)
548 {
549 // Close off the method declaration.
550 actionSignalMethodDeclaration += ");\n\n";
551
552 // Append a return location if the signal return is not void.
553 if(returnCType != "void")
554 actionSignalEmitSignalStatement += ", &result";
555
556 // Close off the emit statement.
557 actionSignalEmitSignalStatement += ", static_cast<void*>(0));\n";
558
559 // Generate the method definition.
560 actionSignalMethodDefinition += ")\n"
561 "{\n" +
562 actionSignalReturnDeclaration +
563 actionSignalEmitSignalStatement +
564 actionSignalReturnStatement +
565 "}\n\n";
566
567 // Append and extra end of line to separate the wrap statement from
568 // the rest since it has a Doxygen comment block (that's how the wrap
569 // statement was started if this is an action signal).
570 wrapStatement += '\n';
571 }
572
573 wrapStatements += convertMacros + wrapStatement;
574 actionSignalsMethodDeclarations += actionSignalMethodDeclaration;
575 actionSignalsMethodDefinitions += pluginEnumsTranslationMacros +
576 actionSignalMethodDefinition;
577 }
578 }
579
580 g_free(pIDs);
581
582 if(gclass_ref)
583 g_type_class_unref(gclass_ref);
584 else if(ginterface_ref)
585 g_type_default_interface_unref(ginterface_ref);
586
587 return wrapStatements;
588 }
589
590 static std::string get_interface_macros(std::string& includeMacroCalls,
591 std::string& cppExtends)
592 {
593 std::string interfaceMacros;
594
595 guint n_interfaces = 0;
596 GType* interfaces = g_type_interfaces(type, &n_interfaces);
597
598 GType parent_type = g_type_parent(type);
599
600 for(guint i = 0; i < n_interfaces; i++)
601 {
602 if(!g_type_is_a(parent_type, interfaces[i]))
603 {
604 std::string interfaceCType = std::string(g_type_name(interfaces[i])) + '*';
605
606 cppExtends += "public _TRANSLATE(`" + interfaceCType + "',`type')";
607
608 if(i < n_interfaces - 1)
609 cppExtends += ", ";
610
611 interfaceMacros += " _IMPLEMENTS_INTERFACE(_TRANSLATE(`" +
612 interfaceCType + "',`type'))\n";
613
614 includeMacroCalls += "_TRANSLATION_INCLUDE(" + interfaceCType + ")dnl\n";
615 }
616 }
617
618 g_free(interfaces);
619 return interfaceMacros;
620 }
621
622 static void generate_hg_file(const std::string& includeMacroCalls,
623 const std::string& cEnumDefinitions,
624 const std::string& enumWrapStatements,
625 const std::string& cppExtends,
626 const std::string& interfaceMacros,
627 const std::string& actionSignalsMethodDeclarations,
628 const std::string& propertyWrapStatements,
629 const std::string& signalWrapStatements)
630 {
631 std::cout << "// Generated by generate_plugin_gmmproc_file. Don't edit this file." << std::endl << std::endl;
632 std::cout << "include(plugingen_base.m4)dnl" << std::endl;
633 std::cout << "changecom()dnl" << std::endl;
634 std::cout << "#include <" << parentInclude << '/' <<
635 to_lowercase(cppParentTypeName) << ".h>" << std::endl;
636
637 std::cout << includeMacroCalls;
638
639 std::cout << "_DEFS(" << target << ',' << defsFile << ')' << std::endl <<
640 std::endl;
641
642 std::cout << "_CONFIGINCLUDE(gstreamermmconfig.h)" << std::endl;
643 std::cout << "_IS_DEPRECATED" << std::endl << std::endl;
644
645 if(!cEnumDefinitions.empty())
646 {
647 std::cout << "// Plug-in C enums used in signals:" << std::endl;
648 std::cout << cEnumDefinitions;
649 }
650
651 std::cout << "namespace " << nmspace << std::endl;
652 std::cout << '{' << std::endl << std::endl;
653
654 if(!enumWrapStatements.empty())
655 std::cout << enumWrapStatements;
656
657 std::cout << "/** A Wrapper for the " << pluginName << " plugin." << std::endl;
658 std::cout << " * Please note that not all plug-ins are available on all "
659 "systems so care\n * must be taken that they exist before they are used "
660 "otherwise there will\n * be errors and possibly a crash." << std::endl;
661 std::cout << " *" << std::endl;
662 std::cout << " * @ingroup " << nmspace << "Plugins" << std::endl;
663 std::cout << " * @deprecated Plugins API is deprecated, and should not be used in a new code." << std::endl;
664 std::cout << " */" << std::endl;
665 std::cout << "class " << cppTypeName << std::endl;
666 std::cout << ": public " << parentNameSpace << "::" << cppParentTypeName;
667
668 if(!cppExtends.empty())
669 std::cout << ", " << cppExtends;
670
671 std::cout << std::endl;
672
673 std::cout << '{' << std::endl;
674 std::cout << " _CLASS_GOBJECT(" << cppTypeName << ", " << cTypeName <<
675 ", " << castMacro << "_GSTREAMERMM, " << parentNameSpace << "::" <<
676 cppParentTypeName << ", " << cParentTypeName << ')' << std::endl;
677
678 if(!interfaceMacros.empty())
679 std::cout << interfaceMacros << std::endl;
680
681 std::cout << " _IS_GSTREAMERMM_PLUGIN" << std::endl;
682
683 std::cout << "protected:" << std::endl;
684 std::cout << " " << cppTypeName << "();" << std::endl;
685 std::cout << " explicit " << cppTypeName << "(const Glib::ustring& name);" << std::endl << std::endl;
686
687 std::cout << "public:" << std::endl;
688 std::cout << " /** Creates a new " << pluginName << " plugin with a unique name." << std::endl;
689 std::cout << " */" << std::endl;
690 std::cout << " _WRAP_CREATE()" << std::endl << std::endl;
691
692 std::cout << " /** Creates a new " << pluginName << " plugin with the given name." << std::endl;
693 std::cout << " */" << std::endl;
694 std::cout << " _WRAP_CREATE(const Glib::ustring& name)" << std::endl;
695
696 if(!actionSignalsMethodDeclarations.empty())
697 std::cout << std::endl << actionSignalsMethodDeclarations;
698
699 if(!propertyWrapStatements.empty())
700 std::cout << std::endl << propertyWrapStatements;
701
702 if(!signalWrapStatements.empty())
703 std::cout << std::endl << signalWrapStatements;
704
705 std::cout << "};" << std::endl;
706
707 std::cout << std::endl << "} // namespace " << nmspace << std::endl;
708 }
709
710 static void generate_ccg_file(const std::string& enumGTypeFunctionDefinitions,
711 const std::string& actionSignalsMethodDefinitions)
712 {
713 std::cout << "// Generated by generate_plugin_gmmproc_file. Don't edit this file." << std::endl << std::endl;
714 std::cout << "include(plugingen_base.m4)dnl" << std::endl;
715 std::cout << "changecom()dnl" << std::endl;
716 std::cout << "_PINCLUDE(" << parentInclude << "/private/" <<
717 to_lowercase(cppParentTypeName) << "_p.h)" << std::endl << std::endl;
718
719 std::string getTypeName = to_lowercase(castMacro) + "_gstreamermm_get_type";
720
721 std::cout << "extern \"C\"" << std::endl;
722 std::cout << '{' << std::endl << std::endl;
723
724 if(!enumGTypeFunctionDefinitions.empty())
725 std::cout << enumGTypeFunctionDefinitions;
726
727 std::cout << "GType " << getTypeName << "()" << std::endl;
728 std::cout << '{' << std::endl;
729 std::cout << " static GType type = 0;" << std::endl;
730 std::cout << " GstElementFactory* factory = 0;" << std::endl;
731 std::cout << " GstPluginFeature* feature = 0;" << std::endl << std::endl;
732
733 std::cout << " if(!type)" << std::endl;
734 std::cout << " {" << std::endl;
735 std::cout << " factory = gst_element_factory_find(\"" << pluginName << "\");" << std::endl;
736
737 std::cout << " // Make sure that the feature is actually loaded:" << std::endl;
738 std::cout << " if(factory)" << std::endl;
739 std::cout << " {" << std::endl;
740 std::cout << " feature =" << std::endl;
741 std::cout << " gst_plugin_feature_load(GST_PLUGIN_FEATURE(factory));" << std::endl << std::endl;
742
743 std::cout << " g_object_unref(factory);" << std::endl;
744 std::cout << " factory = GST_ELEMENT_FACTORY(feature);" << std::endl;
745 std::cout << " type = gst_element_factory_get_element_type(factory);" << std::endl;
746 std::cout << " g_object_unref(factory);" << std::endl;
747 std::cout << " }" << std::endl;
748 std::cout << " }" << std::endl << std::endl;
749
750 std::cout << " return type;" << std::endl;
751 std::cout << '}' << std::endl;
752
753 std::cout << std::endl << "} // extern \"C\"" << std::endl << std::endl;
754
755 std::cout << "namespace " << nmspace << std::endl;
756 std::cout << '{' << std::endl << std::endl;
757
758 std::cout << cppTypeName << "::" << cppTypeName << "()" << std::endl;
759 std::cout << ": _CONSTRUCT(\"name\", static_cast<char*>(0))" << std::endl;
760 std::cout << "{}" << std::endl << std::endl;
761
762 std::cout << cppTypeName << "::" << cppTypeName <<
763 "(const Glib::ustring& name)" << std::endl;
764 std::cout << ": _CONSTRUCT(\"name\", name.c_str())" << std::endl;
765 std::cout << "{}" << std::endl << std::endl;
766
767 if(!actionSignalsMethodDefinitions.empty())
768 std::cout << actionSignalsMethodDefinitions;
769
770 std::cout << '}' << std::endl;
771 }
772
773 } // anonymous namespace
774
775 int main(int argc, char** argv)
776 {
777 gboolean hgFile = false;
778 gboolean ccgFile = false;
779 gboolean confirmExistence = false;
780
781 static const GOptionEntry optionEntries[] =
782 {
783 {"hg", 'h', 0, G_OPTION_ARG_NONE, &hgFile, "Generate preliminary .hg file.", 0 },
784 {"ccg", 'c', 0, G_OPTION_ARG_NONE, &ccgFile, "Generate preliminary .ccg file.", 0 },
785 {"confirm-existence", 'e', 0, G_OPTION_ARG_NONE, &confirmExistence, "Returns success if the plugin exists.", 0 },
786 {"namespace", 'n', 0, G_OPTION_ARG_STRING, &nmspace, "The namespace of the plugin.", "namespace" },
787 {"main-defs", 'm', 0, G_OPTION_ARG_STRING, &defsFile, "The main defs file (no .defs extension).", "def" },
788 {"target", 't', 0, G_OPTION_ARG_STRING, &target, "The .h and .cc target directory.", "directory" },
789 { 0, '\0', 0, G_OPTION_ARG_NONE, 0, 0, 0 }
790 };
791
792 Glib::OptionContext optionContext("<plugin-name> [CppPluginClassName]");
793 optionContext.set_summary(
794 "Outputs a GStreamer plugin's gmmproc files to be processed by gmmproc "
795 "for\nwrapping in gstreamermm. Use the same syntax for the plugin name "
796 "as in\ngst-inspect and supply the desired C++ class name unless "
797 "the\n--confirm-existence option is used. The generated files are "
798 "preliminary\nfiles that need to be run through m4 including the macros "
799 "in the\nplugingen*.m4 files to get the final .hg or .ccg file."
800 );
801
802 g_option_context_add_main_entries(optionContext.gobj(), optionEntries, 0);
803 g_option_context_add_group(optionContext.gobj(), gst_init_get_option_group());
804
805 try
806 {
807 if(!optionContext.parse(argc, argv))
808 {
809 std::cout << "Error parsing options and initializing. Sorry." <<
810 std::endl;
811 return 1;
812 }
813 }
814 catch(Glib::OptionError& error)
815 {
816 std::cout << "Error parsing options and initializing GStreamer." <<
817 std::endl << "Run `" << argv[0] << " -?' for a list of options." <<
818 std::endl;
819 return 1;
820 }
821
822 if(confirmExistence)
823 {
824 if(argc != 2)
825 {
826 std::cout << "A plugin name must be supplied to confirm plugin "
827 "existence." << std::endl << "Run `" << argv[0] <<
828 " -?' for help." << std::endl;
829 return 1;
830 }
831 }
832 else if(argc != 3)
833 {
834 std::cout << "A plugin name and C++ class name must be supplied to "
835 "generate gmmproc files." << std::endl <<
836 "Run `" << argv[0] << " -?' for help." << std::endl;
837 return 1;
838 }
839
840 pluginName = argv[1];
841
842 if(!confirmExistence)
843 cppTypeName = argv[2];
844
845 GstElementFactory* factory = 0;
846
847 factory = gst_element_factory_find(pluginName.c_str());
848
849 // Make sure that the feature is actually loaded:
850 if(factory)
851 {
852 GstPluginFeature* loaded_feature =
853 gst_plugin_feature_load(GST_PLUGIN_FEATURE(factory));
854
855 g_object_unref(factory);
856 factory = GST_ELEMENT_FACTORY(loaded_feature);
857 type = gst_element_factory_get_element_type(factory);
858 }
859
860 if(type)
861 {
862 if(confirmExistence)
863 return 0;
864
865 if(!nmspace || !defsFile || !target)
866 {
867 std::cout << "A namespace, a default defs file and a target directory "
868 "must be supplied" << std::endl << "with the --namespace, --main-defs "
869 "and --target options (run with -? option for " << std::endl <<
870 "details)." << std::endl;
871 return 1;
872 }
873
874 cTypeName = g_type_name(type);
875 cParentTypeName = g_type_name(g_type_parent(type));
876
877 while(!cParentTypeName.empty() &&
878 !is_wrapped_base_class(cParentTypeName) && !is_plugin(cParentTypeName))
879 {
880 cParentTypeName =
881 g_type_name(g_type_parent(g_type_from_name(cParentTypeName.c_str())));
882 }
883
884 cppParentTypeName = cParentTypeName.substr(3);
885 castMacro = get_cast_macro(cTypeName);
886
887 // Check for gstreamermm base classes so that the Gst namespace and the
888 // gstreamermm include directory is always used with them.
889 if(
890 cppParentTypeName.compare("AudioFilter") == 0 ||
891 cppParentTypeName.compare("AudioSink") == 0 ||
892 cppParentTypeName.compare("AudioSrc") == 0 ||
893 cppParentTypeName.compare("AudioBaseSink") == 0 ||
894 cppParentTypeName.compare("AudioBaseSrc") == 0 ||
895 cppParentTypeName.compare("BaseSink") == 0 ||
896 cppParentTypeName.compare("BaseSrc") == 0 ||
897 cppParentTypeName.compare("BaseTransform") == 0 ||
898 cppParentTypeName.compare("Bin") == 0 ||
899 cppParentTypeName.compare("AudioCdSrc") == 0 ||
900 cppParentTypeName.compare("Element") == 0 ||
901 cppParentTypeName.compare("Object") == 0 ||
902 cppParentTypeName.compare("Pipeline") == 0 ||
903 cppParentTypeName.compare("PushSrc") == 0 ||
904 cppParentTypeName.compare("VideoSink") == 0
905 )
906 {
907 parentInclude = "gstreamermm";
908 parentNameSpace = "Gst";
909 }
910 else
911 {
912 parentInclude = target;
913 parentNameSpace = nmspace;
914 }
915
916 if(hgFile || ccgFile)
917 {
918 std::string includeMacroCalls;
919 std::string cEnumDefinitions;
920 std::string enumWrapStatements;
921 std::string actionSignalsMethodDeclarations;
922 std::string propertyWrapStatements;
923 std::string signalWrapStatements;
924 std::string enumGTypeFunctionDefinitions;
925 std::string actionSignalsMethodDefinitions;
926
927 propertyWrapStatements = get_property_wrap_statements(includeMacroCalls,
928 enumWrapStatements, enumGTypeFunctionDefinitions);
929
930 signalWrapStatements = get_signal_wrap_statements(includeMacroCalls,
931 cEnumDefinitions, enumWrapStatements, actionSignalsMethodDeclarations,
932 enumGTypeFunctionDefinitions, actionSignalsMethodDefinitions);
933
934 if(hgFile)
935 {
936 std::string interfaceMacros;
937 std::string cppExtends;
938
939 interfaceMacros = get_interface_macros(includeMacroCalls, cppExtends);
940
941 generate_hg_file(includeMacroCalls, cEnumDefinitions,
942 enumWrapStatements, cppExtends, interfaceMacros,
943 actionSignalsMethodDeclarations, propertyWrapStatements,
944 signalWrapStatements);
945 }
946 else
947 {
948 generate_ccg_file(enumGTypeFunctionDefinitions,
949 actionSignalsMethodDefinitions);
950 }
951 }
952 }
953 else
954 {
955 std::cerr << "Unrecognized Gstreamer element type." << std::endl;
956
957 if(confirmExistence)
958 return 1;
959
960 if(hgFile)
961 {
962 std::cout << "_DEFS(" << target << ',' << defsFile << ')' <<
963 std::endl << std::endl;
964
965 std::cout << "// The build system does not know of a gstreamer plugin named " <<
966 argv[1] << "." << std::endl;
967 std::cout << "// A wrapper class for it was not generated." <<
968 std::endl;
969 }
970 }
971
972 if(factory)
973 g_object_unref(factory);
974
975 return 0;
976 }