summaryrefslogtreecommitdiff
path: root/tools/gstreamer-complete.c
blob: 26ad350829c67045c4c7f6ccff0d67cdfab189c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <stdio.h>
#include <string.h>
#include <parser.h> // NOTE: this is xml-config's fault

// Include compatability defines: if libxml hasn't already defined these,
// we have an old version 1.x
#ifndef xmlChildrenNode
#define xmlChildrenNode childs
#define xmlRootNode root
#endif

#include <glib.h>

#include "config.h"

typedef struct {
  gchar *name;
  GSList *srcpads;
  GSList *sinkpads;
  GSList *srcpadtemplates;
  GSList *sinkpadtemplates;
  GSList *arguments;
} comp_element;

enum {
  ARG_INT,
  ARG_FILENAME,
  ARG_ENUM
};

typedef struct {
  gchar *name;
  int type;
  GSList *enums;
} comp_argument;

typedef struct {
  gint value;
  gchar *nick;
} enum_value;


void print_match_list (gchar *prefix, int len, GSList *wordlist) {
  GSList *words = wordlist;

  while (words) {
    if (!len || !strncmp((gchar *)(words->data), prefix, len))
      printf("%s\n",(gchar *)(words->data));
    words = g_slist_next (words);
  }
}

int match_element (comp_element *element, gchar *name) {
  return strcmp(element->name,name);
}

int main(int argc,char *argv[]) {
  xmlDocPtr doc;
  xmlNodePtr rootnode, elementnode, propnode, argnode;
  GList *element_list = NULL;
  comp_element *element;
  GSList *element_names = NULL;
  comp_argument *argument;
  enum_value *option;

  gchar *prev_word = argv[3];
  gchar *partial_word = argv[2];
  int partial_len = strlen(partial_word);
  GList *elements;
  GSList *pads;
  int num_pads;
  GSList *args;
  gchar *word;
  GSList *words = NULL;

  /***** Loading the completion information from the registry *****/

  doc = xmlParseFile (GST_CONFIG_DIR "/compreg.xml");
  rootnode = doc->xmlRootNode;

  elementnode = rootnode->xmlChildrenNode;
  while (elementnode) {
    if (!strcmp(elementnode->name, "element")) {
      element = g_new0(comp_element,1);
      propnode = elementnode->xmlChildrenNode;
      while (propnode) {

        if (!strcmp(propnode->name, "name")) {
          element->name = xmlNodeGetContent(propnode);
//fprintf(stderr,element->name);
        } else if (!strcmp(propnode->name, "srcpad")) {
          element->srcpads = g_slist_prepend(element->srcpads, xmlNodeGetContent(propnode));
//fprintf(stderr,".");
        } else if (!strcmp(propnode->name, "sinkpad")) {
          element->sinkpads = g_slist_prepend(element->sinkpads, xmlNodeGetContent(propnode));
        } else if (!strcmp(propnode->name, "srcpadtemplate")) {
          element->srcpadtemplates = g_slist_prepend(element->srcpadtemplates, xmlNodeGetContent(propnode));
//fprintf(stderr,".");
        } else if (!strcmp(propnode->name, "sinkpad")) {
          element->sinkpadtemplates = g_slist_prepend(element->sinkpadtemplates, xmlNodeGetContent(propnode));
        } else if (!strcmp(propnode->name, "argument")) {
          argument = g_new0(comp_argument,1);
          argument->name = xmlNodeGetContent(propnode);
          argument->type = ARG_INT;

          // walk through the values data
          argnode = propnode->xmlChildrenNode;
          while (argnode) {
            if (!strcmp(argnode->name, "filename")) {
              argument->type = ARG_FILENAME;
            } else if (!strcmp(argnode->name,"option")) {
              argument->type = ARG_ENUM;
              option = g_new0(enum_value,1);
              sscanf(xmlNodeGetContent(argnode),"%d",&option->value);
              argument->enums = g_slist_prepend (argument->enums, option);
            }
            argnode = argnode->next;
          }

          element->arguments = g_slist_prepend(element->arguments, argument);
        }

        propnode = propnode->next;
      }
      element_list = g_list_prepend(element_list, element);
      element_names = g_slist_prepend(element_names, element->name);
    }
    elementnode = elementnode->next;
  }



  /***** Completion *****/

  /* The bulk of the work is in deciding exactly which words are an option. */

  // if we're right at the beginning, with -launch in the first word
  if (strstr(prev_word,"-launch")) {
    // print out only elements with no sink pad or padtemplate
    elements = element_list;
    while (elements) {
      element = (comp_element *)(elements->data);
      if (!element->sinkpads && !element->sinkpadtemplates)
        words = g_slist_prepend (words, element->name);
      elements = g_list_next(elements);
    }
  }

  // if the previous word is a connection
  if (strchr(prev_word, '!')) {
    // print out oly elements with a sink pad or template
    elements = element_list;
    while (elements) {
      element = (comp_element *)(elements->data);
      if (element->sinkpads || element->sinkpadtemplates)
        words = g_slist_prepend (words, element->name);
      elements = g_list_next (elements);
    }
  }

  // if the partial word is an argument, and it's an enum
  if (strchr(prev_word,'=')) {
    fprintf(stderr,"it's an arg, but dunno what element yet\n");
  }

  // if the previous word is an element, we need to list both pads and arguments
  if ((elements = g_list_find_custom(element_list, prev_word, (GCompareFunc)match_element))) {
    element = elements->data;
    // zero the numpads list so we can count them
    num_pads = 0;

    // pads
    pads = element->srcpads;
    while (pads) {
      num_pads++;
      words = g_slist_prepend (words, g_strdup_printf("%s!",(gchar *)(pads->data)));
      pads = g_slist_next (pads);
    }

    // padtemplates
    pads = element->srcpadtemplates;
    while (pads) {
      num_pads++;
      word = g_strdup_printf("%s!",(gchar *)(pads->data));
      if (!g_slist_find_custom(words,word,(GCompareFunc)strcmp))
        words = g_slist_prepend (words, word);
      pads = g_slist_next (pads);
    }

    // if there is only one pad, add '!' to the list of completions
    if (num_pads == 1) {
      words = g_slist_prepend (words, "!");
    }

    // arguments
    args = element->arguments;
    while (args) {
      argument = (comp_argument *)(args->data);
      word = strstr(argument->name,"::")+2;
      words = g_slist_prepend (words, g_strdup_printf("%s=",word));
      words = g_slist_prepend (words, g_strdup_printf("%s=...",word));
      args = g_slist_next (args);
    }
  }


  /* The easy part is ouptuting the correct list of possibilities. */
  print_match_list (partial_word, partial_len, words);

  return 0;
}