File: main.c

package info (click to toggle)
cssed 0.4.0-2.1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 7,712 kB
  • ctags: 9,499
  • sloc: ansic: 64,325; cpp: 38,658; sh: 7,959; xml: 963; makefile: 321; python: 157
file content (391 lines) | stat: -rw-r--r-- 10,151 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*  cssed (c) Iago Rubio 2003, 2005 - A tiny CSS editor.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#ifdef WIN32
# include <windows.h>
#else
# include <unistd.h>
#endif

#include <sys/types.h>
#include <string.h>

#include <gtk/gtk.h>

#include "cssedwindow.h"
#include "configparser.h"
#include "interface.h"
#include "document.h"
#include "support.h"
#include "utils.h"
#include "debug.h"

#ifdef WITH_HELP_MENUS
# include <stdlib.h>
#endif


#ifdef WITH_IPC_QUEUE
#ifdef DARWIN
# undef WITH_IPC_QUEUE
#else
# include <sys/types.h>
# include <sys/ipc.h>
# include <sys/msg.h>
# include <pthread.h>
# include <signal.h>
# include <limits.h>

# define CSSED_IPC_MSG_QUEUE 5423623

struct ipcmsg
{
	long type;
	char file[PATH_MAX];
};

struct ipcproc_data
{
	CssedWindow *window;
	gint ident;
	gboolean run;
};

gpointer listen_queue_proc (gpointer * ident);
#endif
#endif

#ifdef WIN32
char *package_prefix;
char *package_datadir;
char *package_libdir;
#ifdef NDEBUG
static void nulloutput(const gchar* s){};
static void null_log(const gchar* dom, GLogLevelFlags l, const gchar* m, gpointer d){};
#endif
#endif

gboolean
apply_stored_pos_on_idle (gpointer window)
{
	// apply paned positions stored on the configuration	
	cssed_window_apply_stored_paned_positions(CSSED_WINDOW(window)); //PANEDBUG
	return FALSE;
}
int
main (int argc, char *argv[])
{
	GtkWidget *window;				// the main window
	CssedWindow *cssedwin;		// the main window object
	gint i;							// to parse arguments
	GList* plugins_to_load = NULL;	// list wilth all plugins to be loaded when the window is created.
	gchar* full_path;
	gchar* curdir;
#ifdef WIN32
    gchar *temp;
#endif
#ifdef WITH_IPC_QUEUE
	gint identifier;
	key_t clave;
	struct ipcmsg buff_msg;
	struct ipcproc_data pdata;
	gboolean isserv;
	GThread *listener = NULL;

	if (!g_thread_supported ())
	{
		g_thread_init (NULL);
		gdk_threads_init ();
	}
#endif

	cssedwin = cssed_window_new();

#ifdef WIN32
    package_prefix = g_win32_get_package_installation_directory(CSSED_REGISTRY_NAME, NULL);
    package_datadir = g_strdup_printf("%s%s", package_prefix, "\\share");
	package_libdir = g_strdup_printf("%s%s", package_prefix, "\\lib");
#endif


#ifdef ENABLE_NLS
#ifdef WIN32
    temp = g_strdup_printf("%s%s", package_prefix, "/lib/locale");
    bindtextdomain(GETTEXT_PACKAGE, temp);
    g_free(temp);
#else
    bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
#endif
    bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
    textdomain(GETTEXT_PACKAGE);
#endif

#ifdef WITH_IPC_QUEUE
	isserv = FALSE;
	clave = CSSED_IPC_MSG_QUEUE + getuid ();		// one queue for each user
	DBGMSG (__FILE__,__LINE__,"Will start ipc test %d\n", getpid ());
	// try to open the queue
	if ((identifier = msgget (clave, 0)) < 0)
	{
		DBGMSG (__FILE__,__LINE__,"Queue doesn't exists\n");
		if ((identifier =
			 msgget (clave, IPC_CREAT | 0666 | IPC_EXCL)) < 0)
		{
			DBGMSG (__FILE__,__LINE__,"Can't create queue %d\n",	 getpid ());
		}
		else
		{
			DBGMSG (__FILE__,__LINE__,"Queue created, this instance is the server %d\n", getpid ());
			isserv = TRUE;
			pdata.window = cssedwin;
			pdata.ident = identifier;
			pdata.run = TRUE;
			listener =	g_thread_create ((GThreadFunc) listen_queue_proc, &pdata, TRUE, NULL);
		}
	}
	else
	{
		DBGMSG (__FILE__,__LINE__,"Queue exists\n");
		//*
		if( argc > 1 ){ // avoid to open or listen if no args
			// the queue exists
			// check if dead (timeout)
			struct msqid_ds msg_stat;
			gint timediff;
			msgctl ( identifier, IPC_STAT, &msg_stat );
			if ( msg_stat.msg_qnum > 0 )
			{
				timediff = time (NULL) - msg_stat.msg_ctime;
				if ( timediff > 5 )
				{
					DBGMSG (__FILE__,__LINE__,"more than 5 seconds no reads diff=%d, deleting queue\n", timediff);
					msgctl (identifier, IPC_RMID, NULL);
					// must check here
					if ((identifier = msgget(clave, IPC_CREAT|0666|IPC_EXCL)) < 0)
					{
						DBGMSG (__FILE__,__LINE__,"Can't create queue %d\n", getpid ());
					}
					else
					{
						DBGMSG (__FILE__,__LINE__,"Queue created, this instance is the server %d\n", getpid ());
						isserv = TRUE;
						pdata.window = cssedwin;
						pdata.ident = identifier;
						pdata.run = TRUE;
						listener = g_thread_create ((GThreadFunc) listen_queue_proc, &pdata, TRUE, NULL);
					}
				}
			}
			else
			{	// no messages send message
				buff_msg.type = getpid ();
				memset(buff_msg.file, 0, PATH_MAX);
				if( g_path_is_absolute (argv[1]) ){
					memcpy(buff_msg.file , argv[1], PATH_MAX);
				}else{
					curdir = g_get_current_dir ();
					full_path = g_build_filename( curdir, argv[1], NULL );
					memmove(buff_msg.file , full_path, PATH_MAX);
					g_free(	full_path );
					g_free( curdir );
				}
				DBGMSG (__FILE__,__LINE__,"Queue exists %d\n", getpid ());

				if ((msgsnd (identifier, &buff_msg, PATH_MAX, 0)) < 0)
				{
					DBGMSG (__FILE__,__LINE__,
						"Can't send message, will start but not a server %d\n",
						getpid ()
						);
				}
				else
				{
					for (i = 2; i < argc; i++)
					{
						ssize_t msgretval;
						memset(buff_msg.file, 0, PATH_MAX);
						if( g_path_is_absolute (argv[i]) ){
							memcpy(buff_msg.file , argv[i], PATH_MAX);
						}else{
							curdir = g_get_current_dir ();
							full_path = g_build_filename( curdir, argv[i], NULL );
							memmove(buff_msg.file , full_path, PATH_MAX);
							g_free(full_path);
							g_free(curdir);
						}
						msgretval = msgsnd (identifier, &buff_msg, PATH_MAX, 0);
						if( msgretval != -1 ){
							DBGMSG (__FILE__,__LINE__,"Msg sent %s\n", argv[i]);
						}else{
							DBGMSG (__FILE__,__LINE__,"Cannot send msg %s\n", argv[i]);
						}
					}
					DBGMSG (__FILE__,__LINE__,"Msgs sent bail out %d\n", getpid ());
					return 0;
				}
			}
		}// end args
	}
#endif

	gtk_set_locale ();
	gtk_init (&argc, &argv);
	gdk_rgb_init ();

#ifdef WIN32
#ifdef NDEBUG
	// no output console
	g_set_print_handler( nulloutput );
	g_set_printerr_handler( nulloutput );
	g_log_set_handler("Gdk",
				G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
				null_log, NULL);
	g_log_set_handler("Gtk",
				G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
				null_log, NULL);				
	  g_log_set_handler("GLib",
				G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
				null_log, NULL);				
	  g_log_set_handler("GLib-GObject",
				G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
				null_log, NULL);				
	  g_log_set_handler("GModule",
				G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
				null_log, NULL);				
	  g_log_set_handler("GThread",
				G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
				null_log, NULL);
 
#endif
#endif

#ifdef WIN32
    temp = g_build_filename(package_datadir, PACKAGE, "pixmaps", NULL);
    add_pixmap_directory(temp);
    g_free(temp);
#else
    add_pixmap_directory(PACKAGE_DATA_DIR "/" PACKAGE "/pixmaps");
#endif

	init_default_config (cssedwin);
	// this will return a GList with the filenames of plugins to be loaded
	plugins_to_load = parse_configuration_file (cssedwin);
	// create the cssed "window" - not a good metaphor, may be create app should sound better.
	window = create_window (cssedwin);
	// load all files in plugins list
	if( plugins_to_load != NULL ){
		// this also frees resources
		load_all_plugins(cssedwin, plugins_to_load);
	}
	
	// apply the size settings stored on the configuration
	cssed_window_apply_stored_size_and_position (cssedwin);
	
	// on IDLE set the pos PANEDBUG
	g_idle_add  (apply_stored_pos_on_idle, (gpointer) cssedwin);
	
	// show window widget
	gtk_widget_show (window);

	/* args processing everything passed in taken as a file name */
	for ( i = 1; i < argc; i++ )
	{
		if( g_path_is_absolute (argv[i]) ){
			document_open_with_error_check ( cssedwin, argv[i] );
		}else{
			curdir = g_get_current_dir ();
			full_path = g_build_filename( curdir, argv[i], NULL );
			document_open_with_error_check ( cssedwin, full_path );
			g_free(full_path);
			g_free(curdir);
		}
	}

	
#ifndef WITH_IPC_QUEUE
	gtk_main ();
#else
	gdk_threads_enter();
	gtk_main();
	gdk_threads_leave();

	  if( isserv ){
		DBGMSG (__FILE__,__LINE__,"It's a server let's kill the queue\n");
		if( (msgctl( identifier, IPC_RMID, NULL )) < 0){
		  DBGMSG (__FILE__,__LINE__,"Can't delete queue\n");
		}else{
		  DBGMSG (__FILE__,__LINE__,"Queue deleted\n");
		}
		pdata.run = FALSE;
		g_thread_join(listener); // wait for the ipc reader thread to terminate
	  }
#endif

#ifdef WIN32
    g_free(package_prefix);
    g_free(package_datadir);
    g_free(package_libdir);
#endif

	return 0;
}

#ifdef WIN32
int APIENTRY WinMain(struct HINSTANCE__ *hInstance,
					 struct HINSTANCE__ *hPrevInstance,
					 LPSTR cmd,
					 int show)
	{
		return main( __argc, __argv );
	}
#endif

#ifdef WITH_IPC_QUEUE
gpointer
listen_queue_proc (gpointer * data)
{
	struct ipcmsg buff_msg;
	CssedWindow* cssedwin;
	int identifier;
	int size;
	struct ipcproc_data *pdata;

	pdata = (struct ipcproc_data*) data;
	identifier = pdata->ident;
	cssedwin = pdata->window;

	DBGMSG (__FILE__,__LINE__,"Checking queue %d\n", getpid ());

	while (pdata->run)
	{
		size = msgrcv(identifier, &buff_msg, PATH_MAX, 0, 0);
		while (size > 0)
		{
			gdk_threads_enter ();
			document_open_with_error_check (cssedwin, buff_msg.file);
			gdk_threads_leave ();
			size = msgrcv (identifier, &buff_msg, PATH_MAX, 0, 0);
		}
		sleep (2);
	}
	return 0;
}
#endif