Irreco for N900 (Maemo 5) update. Push for 0.8.* changes.
[irreco.git] / irreco / src / core / irreco.c
blobceb4c2e7fb5603380932a295056a2b609f0ab7c1
1 /*
2 * irreco - Ir Remote Control
3 * Copyright (C) 2007 Arto Karppinen (arto.karppinen@iki.fi)
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "irreco.h"
21 #include "irreco_data.h"
22 #include "irreco_config.h"
23 #include "irreco_window_user.h"
24 #include <gdk-pixbuf/gdk-pixbuf.h>
25 #include <libosso.h>
26 #include <irreco_webdb.h>
27 #include <irreco_retry_loop.h>
29 /**
30 * @mainpage
32 * @par About Irreco
33 * Irreco is a remote control application for Nokia Internet Tablets. With
34 * Irreco and the help of external IR transceiver you can control IR compatible
35 * devices like televisions, DVD players and AV Receivers. Irreco also has a
36 * remote editor which you can use to place buttons to the screen and build the
37 * kind of remote you want to use.
39 * @par Irreco elsewhere
40 * @li Homepage: http://irreco.garage.maemo.org/
41 * @li Garage project: https://garage.maemo.org/projects/irreco/
42 * @li Mailing list: https://garage.maemo.org/mailman/listinfo/irreco-everything
43 * @li Internet Tablet Talk Thread:
44 * http://www.internettablettalk.com/forums/showthread.php?t=15919
45 * @li Maemo Downloads page: http://maemo.org/downloads/product/OS2008/irreco/
47 * @par Documentation
48 * @li Irreco Class documentation can be found in
49 * @htmlonly<a class="el" href="modules.html">Modules</a>@endhtmlonly page.
51 * @par How to implement irreco backend
52 * @li First take a look at @ref IrrecoBackendApi.
53 * @li Then make a copy of @ref PageIrrecoBackendTemplate.
54 * @li And write some code :)
57 /**
58 * @addtogroup Irreco Internal Irreco Classes
63 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
64 /* Prototypes */
65 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
66 void irreco_parse_args(int * argc, char **argv[]);
67 void irreco_process_args(IrrecoData *irreco_data);
68 void irreco_link_bg_image_dir();
72 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
73 /* Main. */
74 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
77 * Popup help message if user doesnt have any remotes.
79 gboolean irreco_startup_help_check(IrrecoData *irreco_data)
81 IRRECO_ENTER
83 if (irreco_string_table_is_empty(irreco_data->irreco_layout_array)) {
85 irreco_info_dlg(irreco_window_manager_get_gtk_window(
86 irreco_data->window_manager),
87 _(IRRECO_STARTUP_NO_REMOTE));
89 if (!irreco_show_remote_download_dlg(irreco_data,
90 irreco_window_manager_get_gtk_window(
91 irreco_data->window_manager))) {
92 irreco_info_dlg(irreco_window_manager_get_gtk_window(
93 irreco_data->window_manager),
94 _(IRRECO_NO_REMOTE_HELP));
97 IRRECO_RETURN_BOOL(FALSE);
100 int main(int argc, char *argv[])
102 IrrecoData *irreco_data;
103 osso_context_t* osso_context = NULL;
104 IRRECO_ENTER
106 /* Setup gettext. */
107 setlocale(LC_ALL, "");
108 IRRECO_DEBUG("bindtextdomain: \"%s\"\n",
109 bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR));
110 IRRECO_DEBUG("bind_textdomain_codeset: \"%s\"\n",
111 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8"));
112 IRRECO_DEBUG("textdomain: \"%s\"\n", textdomain(GETTEXT_PACKAGE));
114 /* Create program main data structure & parse arguments. */
115 irreco_data = irreco_data_new();
116 irreco_parse_args(&argc, &argv);
117 IRRECO_DEBUG("LOCALEDIR: %s\n", LOCALEDIR);
118 IRRECO_DEBUG("GETTEXT_PACKAGE: %s\n", GETTEXT_PACKAGE);
120 irreco_process_args(irreco_data);
122 /* Init Web Database*/
123 irreco_webdb_init();
125 /* Initialize the GTK. */
126 g_thread_init(NULL);
127 /*gdk_threads_init();*/
128 gtk_init(&argc, &argv);
130 /* Initialize libosso and maemo dbus thingy. */
131 osso_context = osso_initialize(PACKAGE_DBUS_NAME, PACKAGE_VERSION,
132 TRUE, NULL);
133 if (osso_context == NULL) {
134 IRRECO_PRINTF("Failed to init LibOSSO\n");
135 return EXIT_FAILURE;
138 /* Init UI. */
139 g_set_application_name(IRRECO_APP_NAME_SHORT);
140 irreco_window_manager_show(irreco_data->window_manager,
141 IRRECO_WINDOW_USER);
143 /* Load plugins. */
145 IrrecoBackendManager* manager = irreco_data->irreco_backend_manager;
146 irreco_backend_manager_load_lib_dir(manager, IRRECO_BACKEND_DIR);
147 irreco_config_read_backends(manager);
148 irreco_backend_manager_print(manager);
149 irreco_backend_manager_read_from_confs(manager);
150 irreco_backend_manager_get_devcmd_lists(manager);
153 /* Read layouts. */
154 irreco_data->theme_manager = irreco_theme_manager_new(irreco_data);
155 irreco_config_read_layouts_from_files(irreco_data);
157 /* Create link to background image dir. */
158 irreco_link_bg_image_dir();
160 /* Begin the main application. */
161 irreco_window_manager_set_layout(irreco_data->window_manager,
162 irreco_config_read_active_layout(
163 irreco_data));
164 g_idle_add(G_SOURCEFUNC(irreco_startup_help_check), irreco_data);
165 gtk_main();
167 /* Save active layout. */
168 if (irreco_data->window_manager->current_layout != NULL) {
169 irreco_config_save_active_layout(
170 irreco_data->window_manager->current_layout);
173 /* Exit */
174 irreco_data_free(irreco_data);
175 osso_deinitialize(osso_context);
176 irreco_webdb_finalize();
177 IRRECO_RETURN_INT(0);
182 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
183 /* Command line argument parsing. */
184 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
186 static gchar** style_dir_array = NULL;
187 static gchar** style_file_array = NULL;
188 static GOptionEntry irreco_option_entries[] = {
189 { "button-style-dir", 'B', 0, G_OPTION_ARG_FILENAME_ARRAY,
190 &style_dir_array, "Read button styles files from directory", "DIR" },
191 /*{ "button-style-file", 'b', 0, G_OPTION_ARG_FILENAME_ARRAY,
192 &style_file_array, "Read button style from file.", "FILE" },*/
193 { "debug", 'd', 0, G_OPTION_ARG_INT, &IRRECO_DEBUG_LEVEL_VAR,
194 "Debugging ouput level", "N" },
195 { NULL }
198 void irreco_parse_args(int * argc, char **argv[])
200 gchar* help_title;
201 GError *error = NULL;
202 GOptionContext *context;
203 IRRECO_ENTER
205 /* Format help title. */
206 help_title = g_strjoin("", "- ", IRRECO_APP_NAME_LONG,
207 " v", VERSION, NULL);
208 context = g_option_context_new(help_title);
209 g_free(help_title);
211 /* Parse arguments. */
212 g_option_context_add_main_entries(context, irreco_option_entries,
213 GETTEXT_PACKAGE);
214 g_option_context_add_group(context, gtk_get_option_group(TRUE));
215 g_option_context_parse(context, argc, argv, &error);
216 if (irreco_gerror_check_print(&error)) {
217 exit(1);
219 g_option_context_free(context);
220 IRRECO_RETURN
223 void irreco_process_args(IrrecoData *irreco_data)
225 guint array_pos;
226 gboolean file_error = FALSE;
227 IRRECO_ENTER
229 /* Check file filetypes. */
230 if (style_dir_array != NULL) {
231 for (array_pos = g_strv_length(style_dir_array); array_pos--;) {
232 if (!irreco_is_dir(style_dir_array[array_pos])) {
233 IRRECO_ERROR("\"%s\" is not a directory.\n",
234 style_dir_array[array_pos]);
235 file_error = TRUE;
239 if (style_file_array != NULL) {
240 for (array_pos = g_strv_length(style_file_array); array_pos--;) {
241 if (!irreco_is_file(style_file_array[array_pos])) {
242 IRRECO_ERROR("\"%s\" is not a file.\n",
243 style_file_array[array_pos]);
244 file_error = TRUE;
248 if (file_error) {
249 exit(1);
252 /* Read button configurations. */
253 if (style_dir_array != NULL) {
254 for (array_pos = g_strv_length(style_dir_array); array_pos--;) {
255 IRRECO_PRINTF("Reading button styles from \"%s\".\n",
256 style_dir_array[array_pos]);
257 /*irreco_config_read_themes_from_dir(
258 irreco_data, style_dir_array[array_pos]);*/
259 irreco_theme_manager_read_themes_from_dir(
260 irreco_data->theme_manager,
261 style_dir_array[array_pos]);
263 g_strfreev(style_dir_array);
264 style_dir_array = NULL;
267 IRRECO_RETURN
271 * Default Irreco background images are installed to somewhere under /usr/lib.
272 * That location is not normally accessible with the Maemo file selection
273 * dialog. So in order to work around that, we create a symlink into
274 * $HOME/MyDocs/.images/ which points to that directory.
276 void irreco_link_bg_image_dir()
278 const gchar *oldpwd;
279 const gchar link_name[] = "Irreco default backgrounds";
280 IRRECO_ENTER
282 oldpwd = getenv("PWD");
283 if (chdir(getenv("HOME")) != 0 || chdir("MyDocs/.images/") != 0) {
284 IRRECO_ERROR("Could not chdir into "
285 "\"$HOME/MyDocs/.images/\".\n");
286 IRRECO_RETURN
289 if (irreco_file_exists(link_name)) {
290 IRRECO_PRINTF("File \"%s\" already exists.\n", link_name);
291 } else if(symlink(IRRECO_BG_IMAGE_DIR, link_name) == 0) {
292 IRRECO_PRINTF("Created link from \"%s\" as \"%s\".\n",
293 link_name, IRRECO_BG_IMAGE_DIR);
294 } else {
295 IRRECO_ERROR("Could not make a link from \"%s\" to \"%s\".\n",
296 link_name, IRRECO_BG_IMAGE_DIR);
299 chdir(oldpwd);
300 IRRECO_RETURN