summaryrefslogtreecommitdiff
path: root/panel/main.c
blob: 23ab066fd153418c8fb964deaf23bd2e48f125a8 (plain)
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
/*  xfce4
 *
 *  Copyright (c) 2002 Jasper Huijsmans <huysmans@users.sourceforge.net>
 *  Copyright (c) 2003 Benedikt Meurer <benedikt.meurer@unix-ag.uni-siegen.de>
 *
 *  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.
*/

/*  main.c 
 *  ------
 *  Contains 'main' function, quit and restart functions
 *  and session management.
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
#ifdef HAVE_STDDEF_H
#include <stddef.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <libxfce4util/i18n.h>
#include <libxfcegui4/session-client.h>
#include <libxfcegui4/netk-util.h>

#include "xfce.h"
#include "settings.h"
#include "mcs_client.h"
#include "controls_dialog.h"
#include "item_dialog.h"
#include "popup.h"

/* signal handling */
typedef enum
{
    NOSIGNAL,
    RESTART,
    QUIT,
    NUM_SIGNALS,
}
SignalState;

static SignalState sigstate = NOSIGNAL;

/*  session management
 *  ------------------
*/
static SessionClient *client_session;
static gboolean session_managed = FALSE;

static void
save_panel (void)
{
#if 0
    gboolean hidden = settings.autohide;

    if (panel.toplevel)
    {
	if (hidden)
	{
	    panel_set_autohide (FALSE);

	    while (gtk_events_pending ())
		gtk_main_iteration ();
	}

	gtk_window_get_position (GTK_WINDOW (panel.toplevel),
				 &panel.position.x, &panel.position.y);

	gtk_widget_hide (panel.toplevel);
    }

    if (hidden && panel.toplevel)
	panel_set_autohide (TRUE);
#endif
    write_panel_config ();
}

static void
save_yourself (gpointer data, int save_style, gboolean shutdown,
	       int interact_style, gboolean fast)
{
    save_panel ();
}

static void
die (gpointer client_data)
{
    quit (TRUE);
}

/*  Exported interface
 *  ------------------
*/
void
quit (gboolean force)
{
    if (!force)
    {
	if (session_managed)
	{
	    logout_session (client_session);
	    return;
	}
	else if (!confirm
		 (_("Are you sure you want to exit?"), GTK_STOCK_QUIT, NULL))
	{
	    return;
	}
    }

    mcs_stop_watch ();

    save_panel ();

#ifdef HAVE_LIBSTARTUP_NOTIFICATION
    free_startup_timeout ();
#endif

    panel_cleanup ();

    if (gtk_main_level ())
	gtk_main_quit ();

    exit (0);
}

void
restart (void)
{
/*    int x, y;
    
    x = panel.position.x;
    y = panel.position.y;
*/
    hide_current_popup_menu();

    save_panel ();

    if (panel.toplevel)
	gtk_widget_hide (panel.toplevel);

#ifdef HAVE_LIBSTARTUP_NOTIFICATION
    free_startup_timeout ();
#endif

    panel_cleanup ();
    gtk_widget_destroy (panel.main_frame);

    create_panel ();
/*
    panel.position.x = x;
    panel.position.y = y;
    panel_set_position ();*/
}

/*  Main program
 *  ------------
*/
static gboolean
check_signal_state (void)
{
    static gboolean restarting = FALSE;

    if (sigstate != NOSIGNAL)
    {
	/* close open dialogs */
	destroy_controls_dialog();
	destroy_menu_dialog();
	
	if (sigstate == RESTART && !restarting)
	{
	    restarting = TRUE;
	    sigstate = NOSIGNAL;
	    restart ();
	    restarting = FALSE;
	}
	else if (sigstate == QUIT)
	{
	    quit (TRUE);
	}
    }

    /* keep running */
    return TRUE;
}

static void
sighandler (int sig)
{
    /* Don't do any reall stuff here.
     * Only set a signal state flag. There's a timeout in the main loop
     * that tests the flag.
     * This will prevent problems with gtk main loop threads and stuff
     */
    switch (sig)
    {
	case SIGUSR1:
	    sigstate = RESTART;
	    break;

	case SIGINT:
	    /* hack: prevent the panel from saving config on ^C */
	    disable_user_config = TRUE;
	    /* fall through */

	default:
	    sigstate = QUIT;
    }
}

int
main (int argc, char **argv)
{
#ifdef HAVE_SIGACTION
    struct sigaction act;
#endif
    gboolean net_wm_support;
    int i;

    net_wm_support = FALSE;

#if 0
#ifdef ENABLE_NLS
    bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
    bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
#endif
    textdomain (GETTEXT_PACKAGE);
#endif
#else
    xfce_textdomain(GETTEXT_PACKAGE, LOCALEDIR, "UTF-8");
#endif

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

    if (argc == 2 &&
	(strequal (argv[1], "-v") || strequal (argv[1], "--version") ||
	 strequal (argv[1], "-h") || strequal (argv[1], "--help")))
    {
	g_print (_("%s, version %s\n"
		   "Part of the XFce Desktop Environment\n"
		   "http://www.xfce.org\n"), PACKAGE, VERSION);
	return 0;
    }

    for (i = 0; i < 5; i++)
    {
	if ((net_wm_support = check_net_wm_support ()) == TRUE)
	    break;

	g_usleep (2000 * 1000);
    }

    if (!net_wm_support)
    {
	xfce_err (_("Your window manager does not seem to support "
		    "the new window manager hints as defined on "
		    "http://www.freedesktop.org. \n"
		    "Some XFce features may not work as intended."));
    }

    client_session = client_session_new (argc, argv, NULL /* data */ ,
					 SESSION_RESTART_IF_RUNNING, 40);

    client_session->save_yourself = save_yourself;
    client_session->die = die;

    if (!(session_managed = session_init (client_session)))
	g_message ("%s: Running without session manager", PACKAGE);

#ifdef HAVE_SIGACTION
    act.sa_handler = sighandler;
    sigemptyset (&act.sa_mask);
#ifdef SA_RESTART
    act.sa_flags = SA_RESTART;
#else
    act.sa_flags = 0;
#endif
    sigaction (SIGUSR1, &act, NULL);
    sigaction (SIGINT, &act, NULL);
    sigaction (SIGTERM, &act, NULL);
#else
    signal (SIGUSR1, sighandler);
    signal (SIGINT, sighandler);
    signal (SIGTERM, sighandler);
#endif

    /* icon framework: names and id's */
    icons_init ();

    create_panel ();

    /* signal state */
    g_timeout_add (500, (GSourceFunc) check_signal_state, NULL);

    gtk_main ();

    return 0;
}