1 /* Hey EMACS -*- linux-c -*- */
2 /* $Id: refresh.c 2349 2007-02-09 11:21:47Z kevinkofler $ */
3
4 /* TiEmu - Tiemu Is an EMUlator
5 *
6 * Copyright (c) 2000-2001, Thomas Corvazier, Romain Lievin
7 * Copyright (c) 2001-2003, Romain Lievin
8 * Copyright (c) 2003, Julien Blache
9 * Copyright (c) 2004, Romain Li�vin
10 * Copyright (c) 2005, Romain Li�vin
11 * Copyright (c) 2007, Kevin Kofler
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
24 */
25
26 #include <stdio.h>
27 #include <gtk/gtk.h>
28
29 #include "intl.h"
30 #include "dboxes.h"
31 #include "tilibs.h"
32 #include "refresh.h"
33 #include "pbars.h"
34
35 extern CalcUpdate calc_update;
36
gt_start(void)37 static void gt_start(void)
38 {
39 calc_update.cnt1 = calc_update.max1 = 0;
40 calc_update.cnt2 = calc_update.max2 = 0;
41 calc_update.cnt3 = calc_update.max3 = 0;
42 }
43
gt_stop(void)44 static void gt_stop(void)
45 {
46 calc_update.cnt1 = calc_update.max1 = 0;
47 calc_update.cnt2 = calc_update.max2 = 0;
48 calc_update.cnt3 = calc_update.max3 = 0;
49 }
50
51 static void filter_shift(void);
52 static gfloat filter_compute(gfloat input);
53
refresh_pbar1(void)54 static void refresh_pbar1(void)
55 {
56 gchar buffer[32];
57 gfloat rate, avg;
58
59 if (p_win.pbar1 != NULL)
60 {
61 if(calc_update.cnt1 > calc_update.max1)
62 calc_update.cnt1 = calc_update.max1;
63
64 if(calc_update.max1 != 0)
65 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(p_win.pbar1),
66 (gdouble)calc_update.cnt1 / calc_update.max1);
67
68 if(p_win.label_rate != NULL)
69 {
70 rate = calc_update.rate;
71 filter_shift();
72 avg = filter_compute(rate);
73
74 g_snprintf(buffer, 32, "Rate: %1.1f Kbytes/s", avg);
75 gtk_label_set_text(GTK_LABEL(p_win.label_rate), buffer);
76 }
77 GTK_REFRESH();
78 }
79 }
80
refresh_pbar2(void)81 static void refresh_pbar2(void)
82 {
83 if (p_win.pbar2 != NULL)
84 {
85 if(calc_update.cnt2 > calc_update.max2)
86 calc_update.cnt2 = calc_update.max2;
87
88 if(calc_update.max2 != 0)
89 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(p_win.pbar2),
90 (gdouble)calc_update.cnt2 / calc_update.max2);
91
92 GTK_REFRESH();
93 }
94 }
95
refresh_pbar3(void)96 static void refresh_pbar3(void)
97 {
98 if(p_win.label_part != NULL)
99 {
100 gchar *str;
101
102 str = g_strdup_printf("%i/%i: ", calc_update.cnt3, calc_update.max3);
103 gtk_label_set_text(GTK_LABEL(p_win.label_part), str);
104
105 GTK_REFRESH();
106 }
107 }
108
gt_pbar(void)109 static void gt_pbar(void)
110 {
111 refresh_pbar1();
112 refresh_pbar2();
113 refresh_pbar3();
114 }
115
gt_label(void)116 static void gt_label(void)
117 {
118 if (p_win.label == NULL)
119 return;
120
121 gtk_label_set_text(GTK_LABEL(p_win.label), calc_update.text);
122
123 GTK_REFRESH();
124 }
125
gt_refresh(void)126 static void gt_refresh(void)
127 {
128 GTK_REFRESH();
129 }
130
131 CalcUpdate calc_update =
132 {
133 "", 0, 0.0,
134 0, 0, 0, 0, 0, 0,
135 0, 0,
136 gt_start,
137 gt_stop,
138 gt_refresh,
139 gt_pbar,
140 gt_label,
141 };
142
tiemu_update_set_gtk(void)143 void tiemu_update_set_gtk(void)
144 {
145 extern CalcHandle *calc_handle; // defined in dbus.c
146
147 ticalcs_update_set(calc_handle, &calc_update);
148 }
149
150 ///// misc
151
152 static gfloat filter[8] = { 0 };
153
filter_shift(void)154 static void filter_shift(void)
155 {
156 int i;
157
158 for(i=7; i>0; i--)
159 filter[i] = filter[i-1];
160 }
161
filter_compute(gfloat input)162 static gfloat filter_compute(gfloat input)
163 {
164 int i;
165 gfloat avg, min, max;
166
167 avg = min = max = 0.0;
168
169 filter[0] = input;
170 for(i=0; i<7; i++) {
171 if(filter[i] < min) min = filter[i];
172 if(filter[i] > max) max = filter[i];
173
174 avg += filter[i];
175 }
176
177 avg -= min;
178 avg -= max;
179
180 return (avg / 6);
181 }
182
183
184
185
186
187