File: gtkpbinit.c

package info (click to toggle)
gtkpbbuttons 0.6.4-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,344 kB
  • ctags: 218
  • sloc: sh: 3,011; ansic: 1,642; makefile: 112
file content (200 lines) | stat: -rw-r--r-- 5,877 bytes parent folder | download
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
/* ----------------------------------------------------------------------------
 * gtkpbinit.c
 * funtions that prepare program workflow and initialization
 *
 * Copyright 2002 Matthias Grimm
 *
 * 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.
 * ----------------------------------------------------------------------------*/
#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <getopt.h>
#include <sys/stat.h>

#include <pbb.h>

#include "gtkpbinit.h"
#include "gtkinterface.h"
#include "audio.h"
#include "themes.h"
#include "gettext_macros.h"

extern volatile sig_atomic_t prgexit;

/* This function does the first initialization of the init-structure. Some values
   are used very early to decide if some cleanup is nessecary or not. */
void init_init(struct init *md)
{
	md->themename = DEFAULT_THEME;
	md->themedata = NULL;
	md->timeout = 0;

	md->dsp.active = 0;
	md->dsp.audiodev = DEFAULT_AUDIO;
	md->dsp.sample = NULL;   /* no sound to play */
}

int prg_init(struct init *md)
{
	int err;

	if ((md->themedata = theme_init(md->themename)) == NULL)
		return 1;

	if ((md->window = popup_create_window(md->themedata)) == NULL)
		return 1;

	if ((err = ipc_init (LIBMODE_CLIENT, 1)) != 0) {        /* setup IPC subsystem for clients */
		if (err == E_NOSERVER)
			print_error (_("ERROR: Server message port not found. Server isn't running.\n"));
		else if (err == E_REGISTER)
			print_error (_("ERROR: Client list full. Server doesn't accept more clients.\n"));
		else if (err == E_MSGPORT)
			print_error (_("ERROR: Can't create message port for client.\n"));
		return err;
	}

#ifdef HAVE_SOUND
	if (md->themedata->havesound) {
		if ((init_sound_thread(&md->dsp)) == -1)
			print_error(_("WARNING: Problems initializing sound subsystem. Beep disabled.\n"));
		else
			md->dsp.active = 1;
	}
#endif
	return 0;
}

/* --- Signal Handler --- */

void
signalhandler (int signum)
{
	prgexit = 1;
}

int
install_sighandler ()
{
	struct sigaction sa = { {signalhandler}, {{0}}, SA_RESTART, 0 };

	/* install the signal handlers */
	if (!sigaction (SIGINT, &sa, NULL))
		if (!sigaction (SIGTERM, &sa, NULL))
			return 0;
	print_error(_("ERROR: Can't install signal handler\n"));
	return -1;
}

int evaluate_args(struct init *md, int argc, char *argv[])
{
	struct option const long_options[] = {
		  {"help", no_argument, 0, ARG_HELP},
		  {"version", no_argument, 0, ARG_VERSION},
		  {"audio", required_argument, 0, ARG_AUDIODEV},
		  {"detach", optional_argument, 0, ARG_DETACH},
		  {"theme", required_argument, 0, ARG_THEME},
		  {"sm-client-id", optional_argument, 0, ARG_DUMMY},
		  {"sm-config-prefix", optional_argument, 0, ARG_DUMMY},
		  {NULL, 0, NULL, 0}
	};
	int c, err;
	char *optarg2, *prgname;

	if((prgname = strrchr(argv[0],'/')) == NULL)
		prgname = argv[0];
	else prgname++;		/* ignore first slash*/
	argv[0] = prgname;

	while ((c = getopt_long (argc, argv, ARG_ALL, long_options, (int *) 0)) != EOF) {
		switch (c) {
			case ARG_VERSION:
				printf(_("%s, version %s"), PACKAGE, VERSION);
				printf(", (c) 2002-2004 Matthias Grimm\n");
				return E_INFO;
			case ARG_DETACH:
				optarg2 = (optarg) ? optarg : argv[optind];
				if (optarg2 == NULL || optarg2[0] == '-')
					optarg2 = DEFAULT_PIDFILE;
				prepare_daemon (prgname, optarg2, PBBDF_FORCE);
				break;
			case ARG_AUDIODEV:
				if((err = check_path (md->dsp.audiodev = optarg, TYPE_CHARDEV)))
					return err;
				break;
			case ARG_THEME:
				if ((err = is_theme(optarg)) != 0)
					printf(_("ERROR: %s is not a theme for %s. Using default.\n"), optarg, PACKAGE);
				else
					md->themename = optarg;
				break;
			case ARG_DUMMY: /* dummy so that libgnomeui options don't */
				break;  /* cancel the program                     */
			case ARG_HELP:
			default:
				printf (_("%s - visualization client for pbbuttonsd, support for special laptop functions.\n"), prgname);
				printf (_("Usage: %s [OPTION]... \n"), prgname);
				printf (_("Options:\n"));
#ifdef ENABLE_GNOME
				printf (_("   -%c                     display this help text and exit\n"
					  "       --help             display GNOME help text and exit\n"),
					  ARG_HELP);
#else
				printf (_("   -%c, --help             display this help and exit\n"),
					  ARG_HELP);
#endif
				printf (_("   -%c, --version          display version information and exit\n"
					  "   -%c, --detach[=PIDFILE] start %s as background process and\n"
					  "                          optional use an alternative pid-file\n"
					  "                          (default: %s)\n"
					  "   -%c, --audio=DSP        use alternative audio device\n"
					  "                          (default: %s)\n"
					  "   -%c, --theme=THEME      set theme, that should be used.\n"
					  "                          (default %s)\n"),
					  ARG_VERSION, ARG_DETACH, prgname, DEFAULT_PIDFILE,
					  ARG_AUDIODEV, md->dsp.audiodev, ARG_THEME, DEFAULT_THEME);
#ifdef ENABLE_GNOME
				printf (_("   Compiled with GNOME session management support.\n"));
#endif
				return E_INFO;
		}
	}
	return 0;
}

int
check_path (char *source, int type)
{
	int rc = 0;

	rc = check_devorfile (source, type);
	switch (rc) {
	case E_NOEXIST:
		print_error(_("ERROR: Can't access %s: %s.\n"), source, strerror(errno));
		break;
	case E_NOFILE:
		print_error(_("ERROR: %s is not a file.\n"), source);
		break;
	case E_NOCHAR:
		print_error(_("ERROR: %s is not a character device.\n"), source);
		break;
	case E_NOBLK:
		print_error(_("ERROR: %s is not a block device.\n"), source);
		break;
	}
	return rc;
}