Modified in acordance to coding guidelines.
[glsdk/dual-decode.git] / src / main.c
1 /*
2  *  Copyright (c) 2012-2013, 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                 g_printerr("BUG: file: " __FILE__ " line: %d" "\n", __LINE__);          
75                 return ERR_BUG;
76         }
78         *uri = (gchar *) g_malloc (sizeof(gchar) * PATH_MAX_LEN);          
79         realPath = realpath (file, realPath);
80         if (NULL == realPath) {
81                 g_printerr ("File %s not found\n", file);
82                 ret = ERR_FILENOTFOUND;
83                 goto destroy;   
84         }
85         g_snprintf (*uri,PATH_MAX_LEN,"file://%s",realPath);
87         goto last;      
89 destroy:
90         g_free(*uri);
91         *uri = NULL;
92 last:
93         free (realPath);
94         return ret;                                                                                                                                
95 }                                                                                                                 
97 gint processOpts()
98 {
99         gint fileCount          = 0;
100         gchar *uri                      = NULL;
101         gint ret                        = ERR_SUCCESS;
102         if (version) {
103                 g_print ("VERSION %s\n", version_text);
104                 exit (ERR_SUCCESS);
105         }
107         if (NULL == files){
108                 return ERR_SUCCESS;
109         }
110         while (NULL != files[fileCount]){
111                 if (fileCount < NUM_MAX_FILES){
112                         uri = NULL;
113                         ret = setAbsolutePathname (files[fileCount], &uri);
114                         if (ERR_SUCCESS != ret){
115                                 return ret;
116                         }
117                         decodeMode++;
118                         filename[fileCount] = uri;
119                 }else{
120                         g_printerr("Ignoring file : %s\n",files[fileCount]);
121                 }
122                 fileCount++;
123         }
124     return ret;
127 gint processArgs (int *argc, char **argv[])
129         GOptionEntry entries[] =                                                                                                                
130         {                                                                                                                                                          
131                 { "version", 'v', 0, G_OPTION_ARG_NONE, &version,                                                        
132                 "Version of the application", NULL },                                                              
133                 { "sink", 's', 0, G_OPTION_ARG_STRING, &sink,                                                            
134                 "Use S for video sink", "S" },                                                                            
135                 { G_OPTION_REMAINING,0, 0, G_OPTION_ARG_FILENAME_ARRAY, &files, 
136                         "Files", "filenames"},
137                 { NULL }                                                                                                                                          
138         };       
139         GError *error = NULL;                                                                                                      
140         GOptionContext *context;   
141  
142         /*Create a GOption context*/                                                                                            
143         context = g_option_context_new ("- Dual Decode application");                      
144         g_option_context_add_main_entries (context, entries, NULL);                              
145         g_option_context_add_group (context, gst_init_get_option_group ());     
146                                                                                                                                                                 
147         /* Parse the command line arguments and options*/                                                  
148         if (!g_option_context_parse (context, argc, argv, &error)) {                       
149                 g_printerr ("option parsing failed: %s\n", error->message);                               
150                 g_error_free(error);
151                 return ERR_OPTIONPARSE;                                                                                                                          
152         }                                                                                                                          
153         return processOpts ();
156           
158 int main(int argc, char *argv[])
160         gint ret = ERR_SUCCESS;
162         ret = processArgs (&argc, &argv);
163         if(ERR_SUCCESS != ret){
164                 return ret;
165         }
167         if(FALSE == DualDecode_initGUI(&argc,&argv)){
168                 /*replace with a debug*/
169                 g_printerr("X is not found. Exiting\n");
170                 return ERR_XNOTFOUND;
171         }
172         DualDecode_startApplication();