Merge branch 'master' of gitorious.tif.ti.com:glsdk/dual_decode
[glsdk/dual-decode.git] / src / main.c
1 /*
2  *  Copyright (c) 2010-2011, Texas Instruments Incorporated
3  *
4  *  Redistribution and use in source and binary forms, with or without
5  *  modification, are permitted provided that the following conditions
6  *  are met:
7  *
8  *  *  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  *
11  *  *  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *
15  *  *  Neither the name of Texas Instruments Incorporated nor the names of
16  *     its contributors may be used to endorse or promote products derived
17  *     from this software without specific prior written permission.
18  *
19  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  *  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  *  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  *  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  *  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29  *  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  *  Contact information for paper mail:
32  *  Texas Instruments
33  *  Post Office Box 655303
34  *  Dallas, Texas 75265
35  *  Contact information:
36  *  http://www-k.ext.ti.com/sc/technical-support/product-information-centers.htm
37  *  ?DCMP=TIHomeTracking&HQS=Other+OT+home_d_contact
38  *  ============================================================================
39  *
40  */
42 /**
43  * @file                main.c
44  *
45  * @brief               Main Application calling the GUI.
46  */
48 #include <gtk/gtk.h>
49 #include <gst/gst.h>
50 #include <string.h>
51 #include <linux/limits.h>
53 #include <common.h>
54 #include <gui.h>
56 gint decodeMode                 = DECODE_MODE_NONE; 
57 gchar *filename[]               = {NULL, NULL};
58 gboolean version                = FALSE;
59 gchar *sink                             = "xvimagesink";
60 gchar **files                   = NULL;
62 const gchar *version_text = "0.2"; 
64 gint setAbsolutePathname (gchar *file, gchar **uri)                                                                      
65 {               
66         gchar *realPath = NULL;
67         gint ret                = ERR_SUCCESS;
69         if(NULL == uri){
70                 return ERR_INVALIDPARAM;
71         }       
73         if(NULL == file){
74                 /*A BUG Here*/
75                 g_printerr("BUG: file: " __FILE__ " line: %d" "\n", __LINE__);          
76                 return ERR_BUG;
77         }
79         *uri = (gchar *) g_malloc (sizeof(gchar) * PATH_MAX_LEN);          
80         realPath = realpath (file, realPath);
81         if (NULL == realPath) {
82                 /*TODO: a debug trace here*/
83                 g_printerr ("File %s not found\n", file);
84                 ret = ERR_FILENOTFOUND;
85                 goto destroy;   
86         }
87         g_snprintf(*uri,PATH_MAX_LEN,"file://%s",realPath);
89         goto last;      
91 destroy:
92         g_free(*uri);
93         *uri=NULL;
94 last:
95         free (realPath);
96         return ret;                                                                                                                                
97 }                                                                                                                 
99 gint processOpts()
101         gint fileCount          = 0;
102         gchar *uri                      = NULL;
103         gint ret                        = ERR_SUCCESS;
104         if (version) {
105                 g_print ("VERSION %s", version_text);
106                 exit (ERR_SUCCESS);
107         }
109         if(NULL == files){
110                 return ERR_SUCCESS;
111         }
112         while(NULL != files[fileCount]){
113                 if(fileCount < NUM_MAX_FILES){
114                         uri = NULL;
115                         ret = setAbsolutePathname(files[fileCount],&uri);
116                         if(ERR_SUCCESS != ret){
117                                 return ret;
118                         }
119                         decodeMode++;
120                         filename[fileCount] = uri;
121                 }else{
122                         /*TODO: A debug trace here*/
123                         g_printerr("Ignoring file : %s\n",files[fileCount]);
124                 }
125                 fileCount++;
126         }
127     return ret;
130 gint processArgs (int *argc, char **argv[])
132         GOptionEntry entries[] =                                                                                                                
133         {                                                                                                                                                          
134                 { "version", 'v', 0, G_OPTION_ARG_NONE, &version,                                                        
135                 "Version of the application", NULL },                                                              
136                 { "sink", 's', 0, G_OPTION_ARG_STRING, &sink,                                                            
137                 "Use S for video sink", "S" },                                                                            
138                 { G_OPTION_REMAINING,0, 0, G_OPTION_ARG_FILENAME_ARRAY, &files, 
139                         "Files", "filenames"},
140                 { NULL }                                                                                                                                          
141         };       
142         GError *error = NULL;                                                                                                      
143         GOptionContext *context;   
144  
145         /*Create a GOption context*/                                                                                            
146         context = g_option_context_new ("- Dual Decode application");                      
147         g_option_context_add_main_entries (context, entries, NULL);                              
148         g_option_context_add_group (context, gst_init_get_option_group ());     
149                                                                                                                                                                 
150         /* Parse the command line arguments and options*/                                                  
151         if (!g_option_context_parse (context, argc, argv, &error)) {                       
152                 /*TODO: a debug trace here*/
153                 g_printerr ("option parsing failed: %s\n", error->message);                               
154                 g_error_free(error);
155                 return ERR_OPTIONPARSE;                                                                                                                          
156         }                                                                                                                          
157         return processOpts ();
160           
162 int main(int argc, char *argv[])
164         gint ret = ERR_SUCCESS;
166         ret = processArgs(&argc, &argv);
167         if(ERR_SUCCESS != ret){
168                 return ret;
169         }
171         if(FALSE == DualDecode_initGUI(&argc,&argv)){
172                 /*replace with a debug*/
173                 g_printerr("X is not found. Exiting\n");
174                 return ERR_XNOTFOUND;
175         }
176         DualDecode_startApplication();