~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to src/led_meter.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-25 15:47:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080225154712-qvr11ekcea4c9ry8
Tags: 1.1.6-0.1ubuntu1
* Merge from debian-multimedia (LP: #120003), Ubuntu Changes:
 - For ffmpeg-related build-deps, remove cvs from package names.
 - Standards-Version 3.7.3
 - Maintainer Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
/* GTK - The GIMP Toolkit
3
 
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4
 
 *
5
 
 * This library is free software; you can redistribute it and/or
6
 
 * modify it under the terms of the GNU Library General Public
7
 
 * License as published by the Free Software Foundation; either
8
 
 * version 2 of the License, or (at your option) any later version.
9
 
 *
10
 
 * This library is distributed in the hope that it will be useful,
 
1
/**
 
2
 * \file led_meter.c
 
3
 *
 
4
 * This file contains a composite widget drawing a led display used for
 
5
 * monitoring frame drop.
 
6
 */
 
7
/*
 
8
 * Copyright (C) 2003-07 Karl, Frankfurt
 
9
 *
 
10
 * This program is free software; you can redistribute it and/or modify
 
11
 * it under the terms of the GNU General Public License as published by
 
12
 * the Free Software Foundation; either version 2 of the License, or
 
13
 * (at your option) any later version.
 
14
 *
 
15
 * This program is distributed in the hope that it will be useful,
11
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 
 * Library General Public License for more details.
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
18
 * GNU General Public License for more details.
14
19
 *
15
 
 * You should have received a copy of the GNU Library General Public
16
 
 * License along with this library; if not, write to the
17
 
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
 
 * Boston, MA 02111-1307, USA.
 
20
 * You should have received a copy of the GNU General Public License
 
21
 * along with this program; if not, write to the Free Software
 
22
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
23
 */
 
24
 
20
25
#include <stdio.h>
21
26
#include <gtk/gtkeventbox.h>
22
27
#include <gtk/gtktable.h>
28
33
#include "gtk/gtksignal.h"
29
34
#include "led_meter.h"
30
35
 
31
 
enum {
32
 
  LED_METER_SIGNAL,
33
 
  LAST_SIGNAL
 
36
enum
 
37
{
 
38
    LED_METER_SIGNAL,
 
39
    LAST_SIGNAL
34
40
};
35
41
 
36
 
static void led_meter_class_init          (LedMeterClass *klass);
37
 
static void led_meter_init                (LedMeter      *ttt);
38
 
static gint on_motion_notify_event        (GtkWidget *widget, GdkEventMotion *event, gpointer* );
 
42
static void led_meter_class_init (LedMeterClass * klass);
 
43
static void led_meter_init (LedMeter * ttt);
39
44
 
40
45
static gint led_meter_signals[LAST_SIGNAL] = { 0 };
41
46
 
42
47
GType
43
48
led_meter_get_type ()
44
49
{
45
 
  static GType lm_type = 0;
46
 
 
47
 
  if (!lm_type)
48
 
    {
49
 
      static const GTypeInfo lm_info =
50
 
      {
51
 
        sizeof (LedMeterClass),
52
 
        NULL,
53
 
        NULL,
54
 
        (GClassInitFunc) led_meter_class_init,
55
 
        NULL,
56
 
        NULL,
57
 
        sizeof (LedMeter),
58
 
        0,
59
 
        (GInstanceInitFunc) led_meter_init,
60
 
      };
61
 
 
62
 
      lm_type = g_type_register_static (GTK_TYPE_VBOX, "LedMeter", &lm_info, 0);
63
 
    }
64
 
 
65
 
  return lm_type;
66
 
}
67
 
 
68
 
static void
69
 
led_meter_class_init (LedMeterClass *class)
70
 
{
71
 
  GtkObjectClass *object_class;
72
 
 
73
 
  object_class = (GtkObjectClass*) class;
74
 
  
75
 
  led_meter_signals[LED_METER_SIGNAL] = g_signal_new ("led_meter",
76
 
                                         G_TYPE_FROM_CLASS (object_class),
77
 
                                         G_SIGNAL_RUN_FIRST,
78
 
                                         0,
79
 
                                         NULL, 
80
 
                                         NULL,                
81
 
                                         g_cclosure_marshal_VOID__VOID,
82
 
                                         G_TYPE_NONE, 0, NULL);
83
 
 
84
 
 
85
 
  class->led_meter = NULL;
86
 
}
87
 
 
88
 
static void
89
 
led_meter_init (LedMeter *lm)
90
 
{
91
 
        GdkColor        g_col;
92
 
        GtkWidget       *table, *viewport1;
93
 
        int             i;
94
 
 
95
 
        // set colours [x][0] for inactive [x][1] for active
96
 
        lm->led_background[LM_LOW][0].red = 0;
97
 
        lm->led_background[LM_LOW][0].green = 0x3333;
98
 
        lm->led_background[LM_LOW][0].blue = 0;
99
 
        lm->led_background[LM_LOW][1].red = 0;
100
 
        lm->led_background[LM_LOW][1].green = 0xFFFF;
101
 
        lm->led_background[LM_LOW][1].blue = 0;
102
 
        lm->led_background[LM_MEDIUM][0].red = 0x3333;
103
 
        lm->led_background[LM_MEDIUM][0].green = 0x3333;
104
 
        lm->led_background[LM_MEDIUM][0].blue = 0;
105
 
        lm->led_background[LM_MEDIUM][1].red = 0xFFFF;
106
 
        lm->led_background[LM_MEDIUM][1].green = 0xFFFF;
107
 
        lm->led_background[LM_MEDIUM][1].blue = 0;
108
 
        lm->led_background[LM_HIGH][0].red = 0x3333;
109
 
        lm->led_background[LM_HIGH][0].green = 0;
110
 
        lm->led_background[LM_HIGH][0].blue = 0;
111
 
        lm->led_background[LM_HIGH][1].red = 0xFFFF;
112
 
        lm->led_background[LM_HIGH][1].green = 0;
113
 
        lm->led_background[LM_HIGH][1].blue = 0; 
114
 
        // set current max_da to 0 (no led active)
115
 
        lm->max_da = 0;
116
 
        lm->old_max_da = 0;
117
 
 
118
 
        gtk_container_set_border_width (GTK_CONTAINER(lm), 0);
119
 
 
120
 
        // add viewport to get shadow
121
 
        viewport1 = gtk_viewport_new (NULL, NULL);
122
 
        gtk_widget_show (viewport1);
123
 
        gtk_container_add (GTK_CONTAINER (lm), viewport1);
124
 
//    gtk_container_set_border_width (GTK_CONTAINER (viewport1), 8);
125
 
        
126
 
        // add eventbox to be able to set a background colour
127
 
        lm->ebox = gtk_event_box_new ();
128
 
        gtk_container_add (GTK_CONTAINER (viewport1), lm->ebox);
129
 
        
130
 
/*      g_signal_connect (G_OBJECT (lm->ebox), "motion_notify_event",
131
 
                    G_CALLBACK (on_motion_notify_event), (gpointer*) lm); */
132
 
 
133
 
        g_col.red = 0;
134
 
        g_col.green = 0;
135
 
        g_col.blue = 0;
136
 
        gtk_widget_modify_bg (lm->ebox, GTK_STATE_NORMAL, &g_col);
137
 
        
138
 
        // add a table for the da/led layout
139
 
        table = gtk_table_new (LM_NUM_DAS, 1, TRUE);
140
 
        gtk_container_add (GTK_CONTAINER (lm->ebox), table);
141
 
        gtk_widget_show (table);
142
 
 
143
 
        // add the das/leds
144
 
        for ( i=0; i < LM_NUM_DAS; i++ ) {
145
 
                lm->da[i] = gtk_drawing_area_new ();
146
 
 
147
 
                // set colours according to threshholds defined elsewhere
148
 
                if ( i < LM_LOW_THRESHOLD ) g_col = lm->led_background[LM_LOW][0];
149
 
                else if ( i < LM_MEDIUM_THRESHOLD ) g_col = lm->led_background[LM_MEDIUM][0];
150
 
                else g_col = lm->led_background[LM_HIGH][0]; 
151
 
                gtk_widget_modify_bg (lm->da[i], GTK_STATE_NORMAL, &g_col );
152
 
 
153
 
                gtk_widget_set_size_request (lm->da[i], 8, 1);
154
 
                gtk_table_attach (GTK_TABLE(table), lm->da[i], 0, 1, (LM_NUM_DAS-i-1), (LM_NUM_DAS-i), (GTK_EXPAND|GTK_SHRINK|GTK_FILL), (GTK_EXPAND|GTK_SHRINK|GTK_FILL), 0, 0);
155
 
                gtk_widget_show (lm->da[i]);
156
 
                gtk_table_set_row_spacing(GTK_TABLE(table), i, 1);
157
 
        }
158
 
 
159
 
        gtk_widget_show (lm->ebox);
160
 
}
161
 
 
162
 
GtkWidget*
 
50
    static GType lm_type = 0;
 
51
 
 
52
    if (!lm_type) {
 
53
        static const GTypeInfo lm_info = {
 
54
            sizeof (LedMeterClass),
 
55
            NULL,
 
56
            NULL,
 
57
            (GClassInitFunc) led_meter_class_init,
 
58
            NULL,
 
59
            NULL,
 
60
            sizeof (LedMeter),
 
61
            0,
 
62
            (GInstanceInitFunc) led_meter_init,
 
63
        };
 
64
 
 
65
        lm_type =
 
66
            g_type_register_static (GTK_TYPE_VBOX, "LedMeter", &lm_info, 0);
 
67
    }
 
68
 
 
69
    return lm_type;
 
70
}
 
71
 
 
72
static void
 
73
led_meter_class_init (LedMeterClass * class)
 
74
{
 
75
    GtkObjectClass *object_class;
 
76
 
 
77
    object_class = (GtkObjectClass *) class;
 
78
 
 
79
    led_meter_signals[LED_METER_SIGNAL] = g_signal_new ("led_meter",
 
80
                                                        G_TYPE_FROM_CLASS
 
81
                                                        (object_class),
 
82
                                                        G_SIGNAL_RUN_FIRST,
 
83
                                                        0, NULL, NULL,
 
84
                                                        g_cclosure_marshal_VOID__VOID,
 
85
                                                        G_TYPE_NONE, 0, NULL);
 
86
 
 
87
    class->led_meter = NULL;
 
88
}
 
89
 
 
90
static void
 
91
led_meter_init (LedMeter * lm)
 
92
{
 
93
    GdkColor g_col;
 
94
    GtkWidget *table, *viewport1;
 
95
    int i;
 
96
 
 
97
    // set colours [x][0] for inactive [x][1] for active
 
98
    lm->led_background[LM_LOW][0].red = 0;
 
99
    lm->led_background[LM_LOW][0].green = 0x3333;
 
100
    lm->led_background[LM_LOW][0].blue = 0;
 
101
    lm->led_background[LM_LOW][1].red = 0;
 
102
    lm->led_background[LM_LOW][1].green = 0xFFFF;
 
103
    lm->led_background[LM_LOW][1].blue = 0;
 
104
    lm->led_background[LM_MEDIUM][0].red = 0x3333;
 
105
    lm->led_background[LM_MEDIUM][0].green = 0x3333;
 
106
    lm->led_background[LM_MEDIUM][0].blue = 0;
 
107
    lm->led_background[LM_MEDIUM][1].red = 0xFFFF;
 
108
    lm->led_background[LM_MEDIUM][1].green = 0xFFFF;
 
109
    lm->led_background[LM_MEDIUM][1].blue = 0;
 
110
    lm->led_background[LM_HIGH][0].red = 0x3333;
 
111
    lm->led_background[LM_HIGH][0].green = 0;
 
112
    lm->led_background[LM_HIGH][0].blue = 0;
 
113
    lm->led_background[LM_HIGH][1].red = 0xFFFF;
 
114
    lm->led_background[LM_HIGH][1].green = 0;
 
115
    lm->led_background[LM_HIGH][1].blue = 0;
 
116
    // set current max_da to 0 (no led active)
 
117
    lm->max_da = 0;
 
118
    lm->old_max_da = 0;
 
119
 
 
120
    gtk_container_set_border_width (GTK_CONTAINER (lm), 0);
 
121
 
 
122
    // add viewport to get shadow
 
123
    viewport1 = gtk_viewport_new (NULL, NULL);
 
124
    gtk_widget_show (viewport1);
 
125
    gtk_container_add (GTK_CONTAINER (lm), viewport1);
 
126
    // gtk_container_set_border_width (GTK_CONTAINER (viewport1), 8);
 
127
 
 
128
    // add eventbox to be able to set a background colour
 
129
    lm->ebox = gtk_event_box_new ();
 
130
    gtk_container_add (GTK_CONTAINER (viewport1), lm->ebox);
 
131
 
 
132
    g_col.red = 0;
 
133
    g_col.green = 0;
 
134
    g_col.blue = 0;
 
135
    gtk_widget_modify_bg (lm->ebox, GTK_STATE_NORMAL, &g_col);
 
136
 
 
137
    // add a table for the da/led layout
 
138
    table = gtk_table_new (LM_NUM_DAS, 1, TRUE);
 
139
    gtk_container_add (GTK_CONTAINER (lm->ebox), table);
 
140
    gtk_widget_show (table);
 
141
 
 
142
    // add the das/leds
 
143
    for (i = 0; i < LM_NUM_DAS; i++) {
 
144
        lm->da[i] = gtk_drawing_area_new ();
 
145
 
 
146
        // set colours according to threshholds defined elsewhere
 
147
        if (i < LM_LOW_THRESHOLD)
 
148
            g_col = lm->led_background[LM_LOW][0];
 
149
        else if (i < LM_MEDIUM_THRESHOLD)
 
150
            g_col = lm->led_background[LM_MEDIUM][0];
 
151
        else
 
152
            g_col = lm->led_background[LM_HIGH][0];
 
153
        gtk_widget_modify_bg (lm->da[i], GTK_STATE_NORMAL, &g_col);
 
154
 
 
155
        gtk_widget_set_size_request (lm->da[i], 8, 1);
 
156
        gtk_table_attach (GTK_TABLE (table), lm->da[i], 0, 1,
 
157
                          (LM_NUM_DAS - i - 1), (LM_NUM_DAS - i),
 
158
                          (GTK_EXPAND | GTK_SHRINK | GTK_FILL),
 
159
                          (GTK_EXPAND | GTK_SHRINK | GTK_FILL), 0, 0);
 
160
        gtk_widget_show (lm->da[i]);
 
161
        gtk_table_set_row_spacing (GTK_TABLE (table), i, 1);
 
162
    }
 
163
 
 
164
    gtk_widget_show (lm->ebox);
 
165
}
 
166
 
 
167
/**
 
168
 * \brief creates a new led_meter widget
 
169
 *
 
170
 * @return new led_meter widget
 
171
 */
 
172
GtkWidget *
163
173
led_meter_new ()
164
174
{
165
 
  return GTK_WIDGET (g_object_new (led_meter_get_type (), NULL));
166
 
}
167
 
 
168
 
 
169
 
void led_meter_set_percent (LedMeter *lm, int percent) {
170
 
        int n;
171
 
        GdkColor g_col;
172
 
 
173
 
        if ( percent > 100 ) percent = 100;
174
 
        if ( percent < 0 ) percent = 0;
175
 
 
176
 
        lm->max_da = ( LM_NUM_DAS * percent / 100 ) - 1;
177
 
/*      printf("lm-pointer: %p - percent: %i \n", lm, percent);
178
 
        printf("set percent - max_da %i - old_max_da %i\n", lm->max_da, lm->old_max_da); */
179
 
        if ( lm->max_da == lm->old_max_da ) return;
180
 
//      printf("set percent ... new percent\n");
181
 
        if ( lm->max_da > lm->old_max_da ) {
182
 
//                      printf("n: ");
183
 
                for ( n = lm->old_max_da; n <= lm->max_da; n++ ) {
184
 
//                      printf("%i ", n);
185
 
                        if ( n < LM_LOW_THRESHOLD ) g_col = lm->led_background[LM_LOW][1];
186
 
                        else if ( n < LM_MEDIUM_THRESHOLD ) g_col = lm->led_background[LM_MEDIUM][1];
187
 
                        else g_col = lm->led_background[LM_HIGH][1]; 
188
 
//                      printf("\n");
189
 
                        gtk_widget_modify_bg (GTK_WIDGET(lm->da[n]), GTK_STATE_NORMAL, &g_col );
190
 
                }
191
 
        } else {
192
 
                for ( n = lm->old_max_da; n >= lm->max_da; n-- ) {
193
 
                        if ( n < LM_LOW_THRESHOLD ) g_col = lm->led_background[LM_LOW][0];
194
 
                        else if ( n < LM_MEDIUM_THRESHOLD ) g_col = lm->led_background[LM_MEDIUM][0];
195
 
                        else g_col = lm->led_background[LM_HIGH][0]; 
196
 
 
197
 
                        gtk_widget_modify_bg (GTK_WIDGET(lm->da[n]), GTK_STATE_NORMAL, &g_col );
198
 
                }
199
 
        }
200
 
        lm->old_max_da = lm->max_da;
201
 
 
202
 
}
203
 
 
204
 
 
205
 
void led_meter_set_tip (LedMeter *lm, char* tip) {
206
 
        static GtkTooltips *tooltips;
207
 
        
208
 
        tooltips = gtk_tooltips_new ();
209
 
 
210
 
        gtk_tooltips_set_tip (tooltips, lm->ebox, tip, NULL);
211
 
 
212
 
//        "Monitor current rate of dropped frames\nyellow = 10-60 %, red = 60+ %", 
213
 
}
214
 
 
215
 
 
216
 
/*
217
 
static gint on_motion_notify_event( GtkWidget *widget,
218
 
                                 GdkEventMotion *event, gpointer *t )
219
 
{
220
 
  static double a = 0;
221
 
  int x;
222
 
//  GdkModifierType state;
223
 
  LedMeter *lm;
224
 
 
225
 
  lm = (LedMeter*) t;
226
 
 
227
 
  x = (int) (sin(a) * 100);
228
 
  x = abs(x);
229
 
 
230
 
  led_meter_set_percent (lm, x);
231
 
        a += 10;
232
 
 
233
 
 
234
 
//  printf("a: %f - x: %i\n", a, x);
235
 
  return TRUE;
236
 
}
237
 
*/
 
175
    return GTK_WIDGET (g_object_new (led_meter_get_type (), NULL));
 
176
}
 
177
 
 
178
/**
 
179
 * \brief sets the percentage to use for the amplitude in the led meter
 
180
 *
 
181
 * @param lm pointer to the led meter widget to work on
 
182
 * @param percent percentag value (1-100)
 
183
 */
 
184
void
 
185
led_meter_set_percent (LedMeter * lm, int percent)
 
186
{
 
187
    int n;
 
188
    GdkColor g_col;
 
189
 
 
190
    if (percent > 100)
 
191
        percent = 100;
 
192
    if (percent < 0)
 
193
        percent = 0;
 
194
 
 
195
    lm->max_da = (LM_NUM_DAS * percent / 100) - 1;
 
196
 
 
197
    if (lm->max_da == lm->old_max_da)
 
198
        return;
 
199
    if (lm->max_da > lm->old_max_da) {
 
200
        for (n = lm->old_max_da; n <= lm->max_da; n++) {
 
201
            if (n < LM_LOW_THRESHOLD)
 
202
                g_col = lm->led_background[LM_LOW][1];
 
203
            else if (n < LM_MEDIUM_THRESHOLD)
 
204
                g_col = lm->led_background[LM_MEDIUM][1];
 
205
            else
 
206
                g_col = lm->led_background[LM_HIGH][1];
 
207
            gtk_widget_modify_bg (GTK_WIDGET (lm->da[n]), GTK_STATE_NORMAL,
 
208
                                  &g_col);
 
209
        }
 
210
    } else {
 
211
        for (n = lm->old_max_da; n >= lm->max_da; n--) {
 
212
            if (n < LM_LOW_THRESHOLD)
 
213
                g_col = lm->led_background[LM_LOW][0];
 
214
            else if (n < LM_MEDIUM_THRESHOLD)
 
215
                g_col = lm->led_background[LM_MEDIUM][0];
 
216
            else
 
217
                g_col = lm->led_background[LM_HIGH][0];
 
218
 
 
219
            gtk_widget_modify_bg (GTK_WIDGET (lm->da[n]), GTK_STATE_NORMAL,
 
220
                                  &g_col);
 
221
        }
 
222
    }
 
223
    lm->old_max_da = lm->max_da;
 
224
 
 
225
}
 
226
 
 
227
/**
 
228
 * \brief sets tooltip on an led meter widget
 
229
 *
 
230
 * @param lm pointer to led meter widget to work on
 
231
 * @param tip string to set as tooltip
 
232
 */
 
233
void
 
234
led_meter_set_tip (LedMeter * lm, char *tip)
 
235
{
 
236
    static GtkTooltips *tooltips;
 
237
 
 
238
    tooltips = gtk_tooltips_new ();
 
239
 
 
240
    gtk_tooltips_set_tip (tooltips, lm->ebox, tip, NULL);
 
241
}