1 /* $Id: mod_chrono.c,v 1.8 2007/02/28 12:47:35 tamentis Exp $
2  *
3  * Copyright (c) 2007 Bertrand Janin <tamentis@neopulsar.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/ioctl.h>
30 #include <sys/time.h>
31 
32 #include <string.h>
33 #include <signal.h>
34 #include <unistd.h>
35 #include <curses.h>
36 #include <stdlib.h>
37 #include <time.h>
38 
39 #include "tbclock.h"
40 
41 extern struct tbclock_data tbc;
42 
43 /* returns the number of tenth of seconds since start of program (bigbang) */
44 unsigned long
chrono_nds()45 chrono_nds()
46 {
47 	struct timeval tp;
48 	struct timezone tzp;
49 	unsigned long dsec = 0;
50 
51 	gettimeofday(&tp, &tzp);
52 
53 	dsec = (tp.tv_sec - tbc.bigbang) * 10 + (tp.tv_usec / 100000);
54 
55 	return (dsec);
56 }
57 
58 
59 /* mod_chrono */
60 void
mod_chrono()61 mod_chrono()
62 {
63 	unsigned long start, elapsed, paused = 0;
64 	unsigned int hour, min, sec, dsec;
65 	int pause = 0;
66 	signed char c;
67 
68 	tbc.format.res_y = 4;
69 
70 	if (tbc.options.vertical)
71 		tbc.format.res_x = 7;
72 	else
73 		tbc.format.res_x = 6;
74 
75 	tbc_configure();
76 
77 	start = chrono_nds();
78 
79 	for (;;) {
80 		c = getch();
81 
82 		if (c == KB_SPACE) {
83 			if (pause == 0) {
84 				pause = 1;
85 				paused = chrono_nds();
86 			} else {
87 				pause = 0;
88 				bkgdset(COLOR_PAIR(TEXT_DEFAULT));
89 				mvprintw(2, tbc.format.width / 2 - 6,
90 						"             ");
91 				start = start + chrono_nds() - paused;
92 			}
93 		} else if (c == KB_BACKSPACE) {
94 			if (pause) {
95 				tbc_clear();
96 				refresh();
97 				paused = start;
98 			} else {
99 				start = chrono_nds();
100 			}
101 		} else if (c == KB_H) {
102 			tbc_next_help_value();
103 			tbc_configure();
104 		} else if (c == KB_A) {
105 			if (tbc.options.vertical) {
106 				tbc.options.vertical = 0;
107 				tbc.format.res_x = 6;
108 			} else {
109 				tbc.options.vertical = 1;
110 				tbc.format.res_x = 7;
111 			}
112 			tbc_configure();
113 		} else if (c > 0)
114 			return;
115 
116 		if (!pause) {
117 			elapsed = chrono_nds() - start;
118 
119 			hour = elapsed / 36000;
120 			min  = (elapsed % 36000) / 600;
121 			sec  = (elapsed % 36000) % 600 / 10;
122 			dsec = (elapsed % 36000) % 600 % 10;
123 
124 			tbc_draw_time(4, hour, min, sec, dsec);
125 		} else {
126 			bkgdset(COLOR_PAIR(BACK_YELLOW));
127 			mvprintw(2, tbc.format.width / 2 - 6,
128 					"--- PAUSE ---");
129 		}
130 
131 		refresh();
132 
133 		usleep(10000);
134 	}
135 }
136 
137 
138