aboutsummaryrefslogtreecommitdiffstats
path: root/svgablink.c
blob: 185a2ca6f3fda43103a6d3b5939d4aa0d24449a2 (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
/* 
 * Copyright 1996-2002 - Karl R. Hakimian and David Fries
 *
 * This file is part of datalink.
 *
 * Datalink 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.
 *
 * Datalink 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with datalink (see COPYING); if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

/*
 * Linux version of Timex/Microsoft SDK for Timex datalink watches making use
 * of the svga library to select a graphic mode and draw on it.
 */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/fcntl.h>
#include <signal.h>
#include <sched.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include "datalink.h"

static void maxPriority();

int main(int argc, char **argv)
{
	int oldvt;
	unsigned char **data;
	char buf[256];
	int fd;
	unsigned char size;
	int n;
	int p = 0;
	int type = DATALINK_IRONMAN;

/* Drop down to user privileges until we need root privs. */
	seteuid(getuid());

	while (argc > 1 && *argv[1] == '-')
	{

		if (strcmp(argv[1], "-ironman") == 0
		    || strcmp(argv[1], "-modelironman") == 0)
			type = DATALINK_IRONMAN;
		else
		    if (strcmp(argv[1], "-150") == 0
			|| strcmp(argv[1], "-model150") == 0)
			type = DATALINK_150;
		else
		    if (strcmp(argv[1], "-150s") == 0
			|| strcmp(argv[1], "-model150s") == 0)
			type = DATALINK_150S;
		else
		    if (strcmp(argv[1], "-70") == 0
			|| strcmp(argv[1], "-model70") == 0)
			type = DATALINK_70;
		else
		{
			fprintf(stderr, "Unknown option %s.\n", argv[1]);
			fprintf(stderr, "Usage: %s [ -ironman | -modelironman | -150 | -model150 | -150s | \n"
				"\t-model150s | -70 | -model70 ] datafile\n"
				"Version $Id: svgablink.c,v 1.12 2002/07/10 04:27:08 david Exp $\n", argv[0]);
			exit(-1);
		}

		argc--;
		argv++;
	}

	if (argc != 2)
	{
		fprintf(stderr, "Usage: %s data_file\n", argv[0]);
		exit(-1);
	}

	if ((data = (unsigned char **) calloc(sizeof(unsigned char *),
		1024)) == NULL)
	{
		fprintf(stderr, "Could not allocate data array.\n");
		exit(-1);
	}

/* Open data file */
	if ((fd = open(argv[1], O_RDONLY)) < 0)
	{
		fprintf(stderr, "Could not open %s for reading.\n",
			argv[1]);
		exit(-1);
	}

/* Read in packets. */
	while ((n = read(fd, &size, 1)) == 1)
	{
		size--;

		if ((n = read(fd, buf, size)) != size)
			break;

		size++;

		if ((data[p] = (char *) malloc(size)) == NULL)
		{
			fprintf(stderr,
				"Could not allocate data buffer.\n");
			exit(-1);
		}

		*data[p] = size;
		memcpy(&data[p++][1], buf, size - 1);
	};

	if (n != 0)
	{
		fprintf(stderr, "File error reading from %s\n", argv[1]);
		fprintf(stderr, "%d, %d, %d\n", n, size, p);
		exit(-1);
	}

/* We need to detach from the terminal and our parent may need to wait for us
   time to fork a child. */

	switch (fork())
	{
	case 0:
		break;
	case -1:
		perror("fork");
		exit(-1);
	default:
		exit(0);
	}

/* The rest of this needs to be as root, open_vt, will raise privileges */

	if ((oldvt = open_vt()) == -1)
		exit(-1);

	maxPriority();
	send_data(type, data, p);
	close_vt(oldvt);
	return 0;
}


static void maxPriority()
{
	struct sched_param scp;

	memset(&scp, '\0', sizeof(scp));
	scp.sched_priority = sched_get_priority_max(SCHED_RR);
	if (sched_setscheduler(0, SCHED_RR, &scp) < 0)
		fprintf(stderr, "WARNING: Cannot set RR-scheduler.\n");
	if (mlockall(MCL_CURRENT | MCL_FUTURE) < 0)
		fprintf(stderr, "WARNING: Cannot disable paging.\n");

}