1 /* Hey EMACS -*- linux-c -*- */
2 /* $Id$ */
3 
4 /*  TiLP - Tilp Is a Linking Program
5  *  Copyright (C) 1999-2008  Romain Lievin
6  *  Copyright (C) 2007  Kevin Kofler
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software Foundation,
20  *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif                          /*  */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <signal.h>             // for managing some signals
31 
32 #include <gtk/gtk.h>
33 #include <glib.h>
34 #include <glib/gstdio.h>
35 
36 #ifdef __WIN32__
37 # include <windows.h>
38 #endif                          /*  */
39 
40 #if WITH_KDE
41 #include "kde.h"
42 #endif
43 
44 #include "support.h"
45 #include "gstruct.h"
46 #include "splash.h"
47 #include "tilp.h"
48 #include "toolbar.h"
49 #include "labels.h"
50 #include "clist.h"
51 #include "tilp_core.h"
52 #include "gtk_update.h"
53 
my_blackhole_log_handler(const gchar * log_domain,GLogLevelFlags log_level,const gchar * message,gpointer user_data)54 static void my_blackhole_log_handler (const gchar *log_domain,
55                             GLogLevelFlags log_level,
56                             const gchar *message,
57                             gpointer user_data)
58 {
59 	// Do nothing.
60 }
61 
62 #ifdef __WIN32__
default_log_handler(const gchar * log_domain,GLogLevelFlags log_level,const gchar * message,gpointer user_data)63 static void default_log_handler(const gchar *log_domain,
64                                 GLogLevelFlags log_level,
65                                 const gchar *message,
66                                 gpointer user_data)
67 {
68 	FILE *log_file = user_data;
69 
70 	if (log_domain)
71 		fprintf(log_file, "%s-", log_domain);
72 
73 	switch (log_level & G_LOG_LEVEL_MASK) {
74 	case G_LOG_LEVEL_ERROR:
75 		fprintf(log_file, "ERROR: ");
76 		break;
77 	case G_LOG_LEVEL_CRITICAL:
78 		fprintf(log_file, "CRITICAL: ");
79 		break;
80 	case G_LOG_LEVEL_WARNING:
81 		fprintf(log_file, "WARNING: ");
82 		break;
83 	case G_LOG_LEVEL_MESSAGE:
84 		fprintf(log_file, "MESSAGE: ");
85 		break;
86 	case G_LOG_LEVEL_INFO:
87 		fprintf(log_file, "INFO: ");
88 		break;
89 	case G_LOG_LEVEL_DEBUG:
90 		fprintf(log_file, "DEBUG: ");
91 		break;
92 	default:
93 		fprintf(log_file, "???: ");
94 	}
95 	fprintf(log_file, "%s\n", message);
96 }
97 #endif
98 
99 #define LOG_FILE ".tilp.log"
100 
main(int argc,char * argv[])101 int main(int argc, char *argv[])
102 {
103 	GdkPixbuf *icon;
104 
105 	/* Redirect standard output to a file - printing to the Windows terminal slows operation down way too much */
106 #ifdef __WIN32__
107 	gchar *tmp;
108 	FILE *log_file;
109 
110 	tmp = g_strconcat(g_get_home_dir(), G_DIR_SEPARATOR_S, LOG_FILE, NULL);
111 	log_file = g_fopen(tmp, "wt");
112 	g_free(tmp);
113 
114 	if (log_file != NULL)
115 		g_log_set_default_handler(&default_log_handler, log_file);
116 #endif
117 
118 	// Force GLib 2.32+ to print info and debug messages like older versions did, unless this variable is already set.
119 	// No effect on earlier GLib versions.
120 	g_setenv("G_MESSAGES_DEBUG", "all", /* overwrite = */ FALSE);
121 
122 	/* Init the tilp core */
123 	tilp_init(&argc, &argv);
124 
125 	/* Init GTK+ */
126 	gtk_init(&argc, &argv);
127 	add_pixmap_directory(inst_paths.pixmap_dir);
128 	add_pixmap_directory(inst_paths.icon_dir);
129 	splash_screen_start();
130 
131 	/*
132 		Get rid of glib, gdk, gtk warnings when compiled in Release mode
133 	*/
134 #if !defined(_DEBUG)
135 	g_log_set_handler ("GLib",
136 		G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG,
137 		my_blackhole_log_handler, NULL);
138 	g_log_set_handler ("Gdk",
139 		G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG,
140 		my_blackhole_log_handler, NULL);
141 	g_log_set_handler ("Gtk",
142 		G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG,
143 		my_blackhole_log_handler, NULL);
144 
145 	g_log_set_handler ("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
146 		my_blackhole_log_handler, NULL);
147 #endif
148 
149 	/* Init the GUI-independent functions */
150 	tilp_gif_set_gtk();
151 	tilp_update_set_gtk();
152 
153 	/* Create the main window */
154 	if(!(working_mode & MODE_CMD))
155 	{
156 #if WITH_KDE
157 		splash_screen_set_label(_("Initializing KDE..."));
158 		sp_kde_init(argc, argv, "tilp", _("TiLP"), VERSION, _("Tilp Is a Linking Program"), "Copyright (c) 1999-2008 Romain Lievin", "http://lpg.ticalc.org/prj_tilp/", "tilp-users@lists.sf.net");
159 		atexit(sp_kde_finish);
160 		// Not needed unless we want to add a DCOP interface.
161 		// g_timeout_add(26, sp_kde_process_qt_events, NULL);
162 #endif
163 
164 		splash_screen_set_label(_("Loading GUI..."));
165 		main_wnd = display_tilp_dbox();
166 	}
167 
168 	/* Do a local directory list */
169 	g_free(local.cwdir);
170 	local.cwdir = g_get_current_dir();
171 
172 	/* Update the local view (not visible yet) */
173 	if(!(working_mode & MODE_CMD))
174 	{
175 		clist_refresh();
176 		labels_refresh();
177 	}
178 
179 	/* Inits are done ! */
180 	working_mode &= ~MODE_INI;
181 	tilp_err(0);		// pop errors (display console mode)
182 
183 	/* In cmdline, does not display the entire window, only the pbar */
184 	if(!(working_mode & MODE_CMD))
185 	{
186 		gtk_widget_show(main_wnd);
187 		toolbar_refresh_buttons();
188 		icon = create_pixbuf("tilp.xpm");
189 		gtk_window_set_icon(GTK_WINDOW(main_wnd), icon);
190 		gdk_window_set_icon_name(main_wnd->window, _("TiLP2"));
191 	}
192 
193 	/*
194 	   If variables have been passed on the command line in GUI mode then
195 	   send them
196 	 */
197 	if((working_mode & MODE_GUI) && (working_mode & MODE_CMD))
198 	{
199 		splash_screen_set_label(_("Command line..."));
200 
201 		if(local.selection0 || local.selection2 || local.selection5)
202 			on_tilp_send("");
203 		else if(local.selection4)
204 			tilp_cmdline_send();
205 	}
206 
207 	/* GTK main loop */
208 	splash_screen_stop();
209 	if(!(working_mode & MODE_CMD))
210 		gtk_main();
211 	tilp_exit();
212 
213 	return 0;
214 }
215 
216 
217 /*
218    If TiLP is compiled in console mode (_CONSOLE),
219    then we use the 'main' entry point.
220    If TiLP is compiled as a windowed application (_WINDOWS),
221    then we use the 'WinMain' entry point.
222 */
223 #if defined(__WIN32__) && defined(_WINDOWS)	// && !defined(_CONSOLE)
WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)224 int APIENTRY WinMain(HINSTANCE hInstance,
225 		     HINSTANCE hPrevInstance,
226 		     LPSTR lpCmdLine, int nCmdShow)
227 {
228 	HANDLE hMutex;
229 
230 	/* Check whether a TiLP session is already running */
231 	hMutex = CreateMutex(NULL, TRUE, "TiLP");
232 	if (GetLastError() == ERROR_ALREADY_EXISTS) {
233 		MessageBox(NULL, _("An TiLP session is already running. Check the task list."), _("Error"), MB_OK);
234 	}
235 
236 	return main(__argc, __argv, NULL);
237 }
238 
239 
240 #endif				/*  */
241