File: led.c

package info (click to toggle)
ledcontrol 0.5.2-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 792 kB
  • ctags: 259
  • sloc: ansic: 3,264; sh: 848; makefile: 176; perl: 49
file content (389 lines) | stat: -rw-r--r-- 8,345 bytes parent folder | download | duplicates (5)
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389

/*
 * The actual low-level LED handling.
 */

#include "ledd.h"

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/kd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <glob.h>


/* GLOBAL CONSTANTS: */

/* This has to match the sequence of LED_X in ledd.h! */
const gint led_flags[LED_MAX]={LED_SCR,LED_CAP,LED_NUM};

/* This must contain all the flags or'ed together. */
const gint led_flags_all=LED_SCR|LED_CAP|LED_NUM;


/* Static functions */
static void led_blink(LedControl *led);
static void led_set_all(options *opts,gint andflag,gint orflag);
static void led_log_error(gchar *str,gchar *arg);

/*
 * Checks whole opts->leds table for stuff to do and does it.
 * If animation is on, then do that at end.
 * Returns always TRUE (continue looping).
 */
gboolean led_do_your_thing(options *opts) {
	gint andflag,orflag;
	LedName led;
	gint priority;

	andflag=led_flags_all;
	orflag=0;
	for (led=0; led<LED_MAX; led++) {
		for (priority=PRIORITY_MAX; priority>=0; priority--) {
			/* Why can't you use enums in a switch ?!?!? */
			if (opts->leds[led][priority]->type==LEDTYPE_NORMAL)
				continue;
			if (opts->leds[led][priority]->type==LEDTYPE_OFF)
				led_flag_set(&andflag,&orflag,led,LEDTYPE_OFF);
			if (opts->leds[led][priority]->type==LEDTYPE_ON)
				led_flag_set(&andflag,&orflag,led,LEDTYPE_ON);
			if (opts->leds[led][priority]->type==LEDTYPE_BLINK) {
				led_blink(opts->leds[led][priority]);
				if (opts->leds[led][priority]->mode)
					led_flag_set(&andflag,&orflag,
						     led,LEDTYPE_ON);
				else
					led_flag_set(&andflag,&orflag,
						     led,LEDTYPE_OFF);
			}
			break;
		}
	}

	/* If we are doing an animation... */
	if (opts->anim!=NULL) {
		/* Animation time!!! */
		led_do_anim(opts);
		/* These are justified in the file LOGICS: */
		andflag=andflag & opts->anim_and;
		orflag=(orflag & opts->anim_and) | opts->anim_or;
	}

	led_set_all(opts,andflag,orflag);
	return TRUE;
}


/*
 * AND's and OR's the flags into the current LED settings on tty *name.
 * Now optimized for the bare minimum of ioctl's. It re-opens the ttys every
 * 0.2 seconds (that shouldn't bother anyone during console-switch).
 * Returns flags actually set or -1 on error.
 */
gint led_set(File *f,gint and, gint or) {
	char current;
	char ref;
	char new;
	int i;

	if (f->timer==NULL || f->fd<0 || g_timer_elapsed(f->timer,NULL)>=0.2) {
		/* Re-open the tty */

		if (f->fd>=0)
			close(f->fd);

		f->fd=open(f->name,O_RDONLY);
		if (f->fd==-1) {
			led_log_error("cannot open tty %s (%s)",f->name);
			return -1;
		}


		/* Get text/graphics mode */
		if (ioctl(f->fd,KDGETMODE,&i)) {
			led_log_error("KDGETMODE on tty %s failed (%s)",
				      f->name);
			close(f->fd);
			f->fd=-1;
			return -1;
		}
		if (i==KD_GRAPHICS)
			f->type=TYPE_GRAPHICS;
		else
			f->type=TYPE_TEXT;

		/* Set the timer */
		if (f->timer==NULL) {
			f->timer=g_timer_new();
			g_timer_start(f->timer);
		}
		g_timer_reset(f->timer);
	}

	/* f->fd now holds the file descriptor,
	 * do your stuff and leave it open... */


	/* Get the tty status */
	if (f->type==TYPE_GRAPHICS) {
		/* In X we assume LEDs to be in "correct" state */
		if (ioctl(f->fd,KDGETLED,&current)) {
			led_log_error("KDGETLED on tty %s failed (%s)",
				      f->name);
			return -1;
		}
		ref=current;
	} else {
		if (ioctl(f->fd,KDGETLED,&current)) {
			led_log_error("KDGETLED on tty %s failed (%s)",
				      f->name);
			return -1;
		}
		if (ioctl(f->fd,KDGKBLED,&ref)) {
			led_log_error("KDGKBLED on tty %s failed (%s)",
				      f->name);
			return -1;
		}
	}

	new=(ref & and) | or;

	if (new!=current) {
		if (ioctl(f->fd,KDSETLED,new)) {
			led_log_error("KDSETLED on tty %s failed (%s)",
				      f->name);
			return -1;
		}
	}

	return new;
}



/*
 * Set LEDs on all tty's specified to the normal state. Does not
 * function in X.
 * Returns amount of tty's for which the normal setting failed.
 *
 * tty-change
 */
gint led_set_all_normal(options *opts) {
	GSList *list;
	gint ret=0;
	glob_t globbuf;
	File *f;
	int i;

	for (list=opts->fixttys; list; list=g_slist_next(list)) {
		if (list->data==NULL)
			continue;
		f=list->data;

		/* Do the globbing */
		if (glob(f->name,GLOB_ERR,NULL,&globbuf)) {
			g_warning("cannot determine files matching %s",
				  f->name);
			/* TODO: Should I globfree()? We're exiting now,
			 * so it doesn't really matter... */
			ret++;
			continue;
		}

		for (i=0; i<globbuf.gl_pathc; i++)
			if (led_set_normal(globbuf.gl_pathv[i]))
				ret++;
		globfree(&globbuf);
	}
	return ret;
}


/*
 * Set LEDs on tty defined in *name to the normal state. Does not
 * function in X.
 */
gint led_set_normal(gchar *name) {
	char c;
	int fd;

	fd=open(name,O_RDONLY);
	if (fd==-1) {
		/* Called only on exit. */
		g_warning("cannot open tty %s (%s)",name,STRERROR);
		return -1;
	}

	c=~0;     /* No, that's not a perl regexp. ;) */

	if (ioctl(fd,KDSETLED,c)) {
		g_warning("KDSETLED on tty %s failed (%s)",name,STRERROR);
		close(fd);
		return -1;
	}
	close(fd);
	return 0;
}



/*
 * Sets the led flags to a specified type.
 */
void led_flag_set(gint *andflag,gint *orflag,LedName led,LedType type) {
	/* Why can't enums be used in a switch ?!?!? */
	if (type==LEDTYPE_NORMAL) {   /* AND on, OR off */
		*andflag|=led_flags[led];
		*orflag&=(~led_flags[led]);
	}
	if (type==LEDTYPE_ON) {       /* AND on, OR on */
		*andflag|=led_flags[led];
		*orflag|=led_flags[led];
	}
	if (type==LEDTYPE_OFF) {      /* AND off, OR off */
		*andflag&=(~led_flags[led]);
		*orflag&=(~led_flags[led]);
	}
	return;
}



/*
 * Frees all possible stuff in led. Does NOT free led.
 */
void led_free(LedControl *led) {
	if (led->timer) {
		g_timer_destroy(led->timer);
		led->timer=NULL;
	}
	if (led->blink) {
		g_slist_free(led->blink);
		led->blink=NULL;
	}
	return;
}


/*
 * Blinks one led.
 */
static void led_blink(LedControl *led) {
	if (led->type!=LEDTYPE_BLINK)
		return;
	if (GPOINTER_TO_INT(led->blink->data) <=
	    (gint)(g_timer_elapsed(led->timer,NULL)*1000)) {
		led->mode=!led->mode;
		g_timer_reset(led->timer);
		/* Rotate the list */
		led->blink=gslist_rotate(led->blink);
	}
	return;
}



/*
 * Sets led status on all ttys according to andflag and orflag.
 */
static void led_set_all(options *opts,gint andflag,gint orflag) {
	GSList *list;

	for (list=opts->ttys; list; list=g_slist_next(list)) {
		File *f;
		if (list->data==NULL)
			continue;

		f=list->data;
		led_set(f,andflag,orflag); /* Logs error itself. */
	}
	return;
}

/*
 * Makes all changes to opts->anim_(and|or) -flags before next
 * wait command.
 */
void led_do_anim(options *opts) {
	GSList *list;
	AnimAction *act;
	gboolean met_loop=FALSE;


	/* The first one must always be defined and a timeout. */
	if (ANIM(opts->anim->data)->data >=
	    (gint)(g_timer_elapsed(opts->anim_timer,NULL)*1000)) 
		return;

	/* Move and / or rotate */
	if (opts->anim_loop)
		opts->anim=gslist_rotate(opts->anim);
	else
		opts->anim=gslist_next_free(opts->anim);
	g_timer_reset(opts->anim_timer);


	list=opts->anim;
	while (list) {
		if (list->data==NULL) {
			/* We don't need NULLs */
			list=gslist_next_free(list);
			continue;
		}
		act=ANIM(list->data);
		if (act->type==ANIM_WAIT)
			break;
		if (act->type==ANIM_LOOP) {
			/* Don't allow loop twice per round, that would
			 * mean infinite loop -> animation bad. */
			if (met_loop) {
				gslist_free_all(opts->anim);
				opts->anim=NULL;
				return;
			}

			met_loop=TRUE;
			opts->anim_loop=TRUE;
		} else {
			led_flag_set(&opts->anim_and,&opts->anim_or,
				     act->data,act->type);
		}
		if (opts->anim_loop)
			list=gslist_rotate(list);
		else
			list=gslist_next_free(list);
	}
	opts->anim=list;

	return;
}

/*
 * Log an error, but don't flood with them. str is assumed to be
 * a printf-like format string with two "%s", the first one of which is
 * given as arg and the second one is set to be the current error.
 * Logs at warning level.
 */
static void led_log_error(gchar *str,gchar *arg) {
	/* TODO: this is a really naive flood-stopper... */
	static GTimer *timer=NULL;

	if (timer==NULL) {
		timer=g_timer_new();
		g_timer_start(timer);
		g_warning(str,arg,STRERROR);
		return;
	}
	if (g_timer_elapsed(timer,NULL)>10) {
		g_timer_reset(timer);
		g_warning(str,arg,STRERROR);
		return;
	}
	/* Don't flood it. */
	return;
}