summaryrefslogtreecommitdiff
path: root/src/options.c
blob: 54419b4fb8297b8ac4037b7796af660056483f7b (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
/*
 * This file is part of uxlaunch
 *
 * (C) Copyright 2009 Intel Corporation
 * Authors:
 *     Auke Kok <auke@linux.intel.com>
 *     Arjan van de Ven <arjan@linux.intel.com>
 *
 * 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; version 2
 * of the License.
 */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <getopt.h>
#include <dirent.h>
#include <pwd.h>

#include "uxlaunch.h"

#include "../config.h"

/* builtin defaults */
int tty = 1;
#ifdef ENABLE_CHOOSER
char chooser[256] = "";
#endif
char session[256] = "default";
char username[256] = "meego";
char dpinum[256] = "auto";
char addn_xopts[256] = "";

int verbose = 0;
int x_session_only = 0;

static struct option opts[] = {
#ifdef ENABLE_CHOOSER
	{ "chooser",  1, NULL, 'c' },
#endif
	{ "user",     1, NULL, 'u' },
	{ "tty",      1, NULL, 't' },
	{ "session",  1, NULL, 's' },
	{ "xsession", 0, NULL, 'x' },
	{ "help",     0, NULL, 'h' },
	{ "verbose",  0, NULL, 'v' },
	{ 0, 0, NULL, 0}
};


void usage(const char *name)
{
	printf("Usage: %s [OPTION...] [-- [session cmd] [session args]]\n", name);
#ifdef ENABLE_CHOOSER
	printf("  -c, --chooser   Start specified UX chooser\n");
#endif
	printf("  -u, --user      Start session as specific username\n");
	printf("  -t, --tty       Start session on alternative tty number\n");
	printf("  -s, --session   Start a non-default session\n");
	printf("  -x, --xsession  Start X apps inside an existing X session\n");
	printf("  -v, --verbose   Display lots of output to the console\n");
	printf("  -h, --help      Display this help message\n");
}

static void get_dmi_dpi(void)
{
	/* see if we have a dmi-based override dpi value for
	 *  this board */
	FILE *f;
	char boardname[PATH_MAX];

	d_in();

	f = fopen("/etc/boardname", "r");
	if (!f) {
		lprintf("Unable to open /etc/boardname");
		return;
	}
	if (fscanf(f, "%s", boardname) <= 0) {
		lprintf("Unable to read /etc/boardname");
		fclose(f);
		return;
	}
	fclose(f);
	dprintf("boardname=%s", boardname);

	f = fopen("/usr/share/uxlaunch/dmi-dpi", "r");
	if (!f) {
		lprintf("No DMI-DPI table present (/usr/share/uxlaunch/dmi-dpi)");
		return;
	}
	while (!feof(f)) {
		char b[PATH_MAX];
		char dpi[PATH_MAX];
		if (fscanf(f, "%s %s", b, dpi) < 0) {
			fclose(f);
			return;
		}
		dprintf("dmi-dpi entry: boardname=%s dpi=%s", b, dpi);
		if (!strcmp(boardname, b)) {
			strncpy(dpinum, dpi, sizeof(dpinum)-1);
			lprintf("Using dpi=%s based on dmi-dpi table", dpinum);
			fclose(f);
			return;
		}
	}
	fclose(f);

	d_out();
}

void get_options(int argc, char **argv)
{
	int i = 0;
	int c;
	FILE *f;
	DIR *dir;
	struct dirent *entry;

	d_in();
	/*
	 * default fallbacks are listed above in the declations
	 * each step below overrides them in order
	 */

	/* try and find a user in /home/ */
	dir = opendir("/home");
	while (dir) {
		char buf[80];
		char *u;
		struct passwd *p;

		entry = readdir(dir);
		if (!entry)
			break;
		if (entry->d_name[0] == '.')
			continue;
		if (strcmp(entry->d_name, "lost+found")==0)
			continue;
		if (entry->d_type != DT_DIR)
			continue;

		u = strdup(entry->d_name);
		dprintf("Found potential user folder %s", u);
		/* check if this is actually a valid user */
		p = getpwnam(u);
		if (!p)
			continue; // FIXME: small leak
		/* and make sure this is actually the guys homedir */
		snprintf(buf, 80, "/home/%s", u);
		if (strcmp(p->pw_dir, buf))
			continue; // FIXME: small leak
		strncpy(username, u, 256);
		free(u);
	}
	if (dir)
		closedir(dir);

	/*
	 * DPI setting order:
	 * - auto by default
	 * - dmi lookup table if it exists
	 * - config file overrides as normal
	 */
	get_dmi_dpi();

	/* parse config file */
	f = fopen("/etc/sysconfig/uxlaunch", "r");
	if (f) {
		char buf[256];
		char *key;
		char *val;

		while (fgets(buf, 80, f) != NULL) {
			char *c;

			c = strchr(buf, '\n');
			if (c) *c = 0; /* remove trailing \n */

			dprintf("config file: %s", buf);

			if (buf[0] == '#')
				continue; /* comment line */

			key = strtok(buf, "=");
			if (!key)
				continue;
			val = strtok(NULL, "=");
			if (!val)
				continue;

			// todo: filter leading/trailing whitespace

#ifdef ENABLE_CHOOSER
			if (!strcmp(key, "chooser"))
				strncpy(chooser, val, 256);
#endif
			if (!strcmp(key, "user"))
				strncpy(username, val, 256);
			if (!strcmp(key, "tty"))
				tty = atoi(val);
			if (!strcmp(key, "session"))
				strncpy(session, val, 256);
			if (!strcmp(key, "dpi"))
				strncpy(dpinum, val, 256);
			if (!strcmp(key, "xopts")) {
			        strncpy(addn_xopts, val, 256);
			}
 		}
		fclose(f);
	}

	/* parse cmdline - overrides */
	while (1) {
		c = getopt_long(argc, argv,
#ifdef ENABLE_CHOOSER
				"c:u:t:s:hvx",
#else
				"u:t:s:hvx",
#endif
				opts, &i);
		if (c == -1)
			break;

		switch (c) {
#ifdef ENABLE_CHOOSER
		case 'c':
			strncpy(chooser, optarg, 256);
			break;
#endif
		case 'u':
			strncpy(username, optarg, 256);
			break;
		case 't':
			tty = atoi(optarg);
			break;
		case 's':
			strncpy(session, optarg, 256);
			break;
		case 'h':
			usage(argv[0]);
			exit (EXIT_SUCCESS);
			break;
		case 'v':
			verbose = 1;
			break;
		case 'x':
			x_session_only = 1;
			if (getenv ("USER"))
				strncpy (username, getenv ("USER"), 256);
			break;
		default:
			break;
		}
	}

	/* Get session command from startup line */
	while (i < argc) {
		if (!strcmp(argv[i++], "--")) {
			session[0] = '\0';
			while (i < argc) {
				strncat(session, argv[i++], 256 - strlen(session));
				if (i != argc)
					strncat(session, " ", 256 - strlen(session));
			}
		}
	}

	lprintf("uxlaunch v%s started%s.", VERSION, x_session_only ? " for x session only" : "" );
	lprintf("user \"%s\", tty #%d, session \"%s\"", username, tty, session);

	pass = getpwnam(username);
	if (!pass) {
		lprintf("Error: can't find user \"%s\"", username);
		exit(EXIT_FAILURE);
	}
	d_out();
}