1
/* multihead.c -- Simple multihead support
2
$Id: multihead.c,v 1.1 2001/06/20 21:43:16 jsh Exp $
4
Copyright (C) 2001 John Harper <jsh@pixelslut.com>
6
This file is part of sawfish.
8
sawfish is free software; you can redistribute it and/or modify it
9
under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2, or (at your option)
13
sawfish is distributed in the hope that it will be useful, but
14
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.
18
You should have received a copy of the GNU General Public License
19
along with sawfish; see the file COPYING. If not, write to
20
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22
/* AIX requires this to be the first thing in the file. */
33
# ifndef alloca /* predefined by HP cc +Olibcalls */
49
argv_test_option (const char *option, int argc, char **argv,
50
int *positionp, char **value)
52
int optlen = strlen (option);
53
int position = (*positionp)++;
55
if (strncmp (option, argv[position], optlen) != 0)
60
/* Argument required */
62
if (argv[position][optlen] == '=')
63
*value = argv[position] + optlen + 1;
64
else if (argv[position][optlen] == 0 && position + 1 < argc)
66
*value = argv[position + 1];
77
argv_get_option (int argc, char **argv, const char *option, char **valuep)
82
if (argv_test_option (option, argc, argv, &i, valuep))
89
copy_argv (int argc, char **argv)
91
char **copy = malloc (sizeof (char *) * argc);
94
memcpy (copy, argv, sizeof (char *) * argc);
99
grow_argv (int *argcp, char ***argvp, int extra_slots)
103
char **copy = malloc (sizeof (char *) * (*argcp + extra_slots));
106
memcpy (copy, *argvp, sizeof (char *) * *argcp);
109
*argcp += extra_slots;
114
argv_remove_option (int *argcp, char ***argvp, char *option, bool has_arg)
123
if (argv_test_option (option, *argcp, *argvp, &i, has_arg ? &value : 0))
125
if (!has_arg || i - old_i == 1)
127
/* Option only takes one slot, i.e. --foo=bar form.
128
So just delete the single slot */
129
memmove (*argvp + old_i, *argvp + old_i + 1,
130
sizeof (char *) * (*argcp - (old_i + 1)));
135
memmove (*argvp + old_i, *argvp + old_i + 2,
136
sizeof (char *) * (*argcp - (old_i + 2)));
146
argv_set_option (int *argcp, char ***argvp, char *option, char *arg)
155
if (argv_test_option (option, *argcp, *argvp, &i, arg ? &value : 0))
161
/* Option only takes one slot, i.e. --foo=bar form.
162
So open up space for the second slot */
163
grow_argv (argcp, argvp, 1);
164
memmove (*argvp + i + 1, *argvp + i,
165
sizeof (char *) * (*argcp - (i + 1)));
166
(*argvp)[old_i] = option;
170
(*argvp)[old_i + 1] = arg;
177
/* No existing instance of this option. */
180
grow_argv (argcp, argvp, 2);
181
(*argvp)[*argcp - 2] = option;
182
(*argvp)[*argcp - 1] = arg;
186
grow_argv (argcp, argvp, 1);
187
(*argvp)[*argcp - 1] = option;
191
/* Spawn multiple copies, one per screen, if necessary */
193
multihead_init (int *argcp, char ***argvp)
196
char **argv = *argvp;
197
char *dpy_name, *dpy_copy, *tem;
199
int i, master_screen, total_screens;
201
if (!argv_get_option (argc, argv, "--multihead", NULL))
205
Warning: sawfish's --multihead option is known to have fundamental\n\
206
design flaws, which may lead to sawfish behaving strangely, or\n\
207
the files in your ~/.sawfish directory being corrupted. Use\n\
208
this option at your own risk!\n\n", stderr);
210
if (!argv_get_option (argc, argv, "--display", &dpy_name))
211
dpy_name = getenv ("DISPLAY");
213
dpy = XOpenDisplay (dpy_name);
217
argv = copy_argv (argc, argv);
219
dpy_name = XDisplayString (dpy);
220
dpy_copy = strdup (dpy_name);
221
tem = strrchr (dpy_copy, '.');
225
master_screen = DefaultScreen (dpy);
226
total_screens = ScreenCount (dpy);
229
argv_set_option (&argc, &argv, "--display", strdup (dpy_name));
231
/* This will remove it from all future children as well.. */
232
argv_remove_option (&argc, &argv, "--multihead", FALSE);
236
for (i = 0; i < total_screens; i++)
238
if (i != master_screen && fork () == 0)
240
dpy_name = malloc (strlen (dpy_copy) + 6);
241
sprintf (dpy_name, "%s%d", dpy_copy, i);
243
/* Give each instance the correct display name */
244
argv_set_option (&argc, &argv, "--display", dpy_name);