1 /* Hey EMACS -*- linux-c -*- */
2 /* $Id$ */
3 
4 /*  TiLP - Tilp Is a Linking Program
5  *  Copyright (C) 1999-2006  Romain Lievin
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software Foundation,
19  *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <signal.h>
30 #include <locale.h>
31 
32 #ifdef __WIN32__
33 # include <windows.h>
34 #endif
35 
36 #include "tilp_core.h"
37 
38 /* Global variables */
39 CableHandle* cable_handle = NULL;
40 CalcHandle*  calc_handle  = NULL;
41 
42 TilpOptions options    = { 0, /* cable_model */
43                            0, /* cable_port */
44                            0, /* cable_timeout */
45                            0, /* cable_delay */
46                            0, /* calc_model */
47                            0, /* pane_x_size */
48                            0, /* pane_y_size */
49                            0, /* wnd_x_size1 */
50                            0, /* wnd_y_size1 */
51                            0, /* wnd_x_size2 */
52                            0, /* wnd_y_size2 */
53                            0, /* local_sort */
54                            0, /* local_sort_order */
55                            0, /* remote_sort */
56                            0, /* remote_sort_order */
57                            0, /* filesel_type */
58                            0, /* fs_type */
59                            0, /* full_gui */
60                            0, /* auto_detect */
61                            0, /* local_path */
62                            0, /* show_all */
63                            0, /* overwrite */
64                            0, /* recv_as_group */
65                            0, /* backup_as_tigroup */
66                            NULL, /* working_dir */
67                            0, /* screen_format */
68                            0, /* screen_scaling */
69                            0, /* screen_clipping */
70                            0, /* screen_blurry */
71                            NULL, /* remote_font_name */
72                            NULL, /* local_font_name */
73                            0 /* usb_avail */
74     };
75 TilpLocal   local  = { NULL, /* dirlist */
76                        NULL, /* cwdir */
77                        NULL, /* selection0 */
78                        NULL, /* selection1 */
79                        NULL, /* selection2 */
80                        NULL, /* selection3 */
81                        NULL, /* selection4 */
82                        NULL, /* selection5 */
83                        NULL, /* file_selection */
84                        0 /* copy_cut */ };
85 TilpRemote  remote = { NULL, /* var_tree */
86                        NULL, /* app_tree */
87                        {     /* memory */
88                          0,  /* n_folders */
89                          0,  /* n_vars */
90                          0,  /* n_apps */
91                          0,  /* ram_used */
92                          0,  /* flash_used */
93                          0,  /* ram_free */
94                          0   /* flash_free */
95                        },
96                        NULL, /* selection1 */
97                        NULL  /* selection2 */ };
98 int working_mode = MODE_INI;
99 
100 /* Ctrl+C signal handler */
signal_handler(int sig_no)101 static void signal_handler(int sig_no)
102 {
103 	tilp_info(_("Signal SIGINT (Ctrl+C) caught...\n"));
104 	tilp_exit();
105 	tilp_info(_("Done.\n"));
106 
107 	exit(0);
108 }
109 
110 /*
111   This function must be the first function to call in your function 'main'.
112   It initializes the TiLP core engine.
113 */
tilp_init(int * argc,char *** argv)114 int tilp_init(int *argc, char ***argv)
115 {
116 	/* Display program version */
117 	tilp_cmdline_version();
118 
119 	/* Check the version of libraries and init framework */
120 	if (strcmp(ticonv_version_get(), TILP_REQUIRES_LIBCONV_VERSION) < 0)
121 	{
122 		tilp_critical(_("libticonv library version is %s but %s mini required.\n"), ticonv_version_get(), TILP_REQUIRES_LIBCONV_VERSION);
123 		return 1;
124 	}
125 
126 	if (strcmp(tifiles_version_get(), TILP_REQUIRES_LIBFILES_VERSION) < 0)
127 	{
128 		tilp_critical(_("libtifiles library version is %s but %s mini required.\n"), tifiles_version_get(), TILP_REQUIRES_LIBFILES_VERSION);
129 		return 1;
130 	}
131 
132 	if (strcmp(ticables_version_get(), TILP_REQUIRES_LIBCABLES_VERSION) < 0)
133 	{
134 		tilp_critical(_("libticables library version is %s but %s mini required.\n"), ticables_version_get(), TILP_REQUIRES_LIBCABLES_VERSION);
135 		return 1;
136 	}
137 
138 	if (strcmp(ticalcs_version_get(), TILP_REQUIRES_LIBCALCS_VERSION) < 0)
139 	{
140 		tilp_critical(_("libticalcs library version is %s but %s mini required.\n"), ticalcs_version_get(), TILP_REQUIRES_LIBCALCS_VERSION);
141 		return 1;
142 	}
143 
144 	/* Initialize platform independant paths */
145 	tilp_paths_init();
146 
147 	/* Init i18n support */
148 #ifdef ENABLE_NLS
149 	tilp_info("setlocale: %s", setlocale(LC_ALL, ""));
150   	tilp_info("bindtextdomain: %s", bindtextdomain(PACKAGE, inst_paths.locale_dir));
151   	bind_textdomain_codeset(PACKAGE, "UTF-8"/*"ISO-8859-15"*/);
152   	tilp_info("textdomain: %s", textdomain(PACKAGE));
153 #endif
154 
155 	/* Initialize callbacks with default functions */
156 	tilp_gif_set_default();
157 
158 	/* Initialize/reload config */
159 	tilp_config_default();
160 	tilp_config_read();
161 
162 	/* Scan and modify command line and change to working folder*/
163 	tilp_cmdline_scan(argc, argv);
164 	tilp_file_chdir(options.working_dir);
165 
166 	/* Catch 'Ctrl-C' */
167 	signal(SIGINT, signal_handler);
168 
169 	ticables_library_init();
170 	tifiles_library_init();
171 	ticalcs_library_init();
172 
173 	/* Check for USB support */
174 	options.usb_avail = ticables_is_usb_enabled();
175 
176 	/* Set cable & calc */
177 	if(options.auto_detect && options.usb_avail)
178 	{
179 		int ret;
180 		CableModel cable;
181 		CalcModel calc;
182 		CablePort port;
183 
184 		ret = tilp_device_probe_usb(&cable, &port, &calc);
185 		if(!ret)
186 		{
187 			options.cable_model = cable;
188 			options.cable_port = port;
189 			options.calc_model = calc;
190 		}
191 	}
192 
193 	tilp_device_open();
194 
195 	/*
196 	   If we are in command line mode, does the required operation
197 	   and exit else fallback on a graphic interface.
198 	 */
199 	if((working_mode & MODE_CMD) && !(working_mode & MODE_GUI))
200 	{
201 		int ret;
202 
203 		ret = tilp_cmdline_send();
204 		exit(ret);
205 	}
206 	else if(working_mode == MODE_INI)
207 		exit(0);
208 
209 	return 0;
210 }
211 
tilp_exit(void)212 int tilp_exit(void)
213 {
214 	tilp_device_close();
215 
216 	ticables_library_exit();
217 	tifiles_library_exit();
218 	ticalcs_library_exit();
219 
220 	return 0;
221 }
222