File: .#volume.c.1.10

package info (click to toggle)
gkrellm-volume 2.1.13-1.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 304 kB
  • ctags: 241
  • sloc: ansic: 1,929; makefile: 68
file content (1067 lines) | stat: -rw-r--r-- 33,359 bytes parent folder | download | duplicates (2)
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
#include <stdio.h>
#include <math.h>

#if !defined(WIN32)
  #include <gkrellm2/gkrellm.h>
#endif

#include "volume.h"
#include "mixer.h"

#define VOLUME_STYLE style_id
static gint style_id;
static GkrellmMonitor *monitor;
static GtkWidget *pluginbox;
static Mixer *Mixerz;
static int global_flags = 0;
static int config_global_flags = 0;


/* functions for the bookkeeping of open mixers and sliders */
/* retuns the added mixer or and existing one with the same id */
static Mixer *add_mixer_by_id(char *id) {
  Mixer *result,**m;
  mixer_t *mixer;

  if (Mixerz == NULL) m = &Mixerz;
  else {
    result = Mixerz;
    do {
        if (!strcmp(id,result->id)) return result;
    } while (result->next != NULL && (result = result->next));
    m = &(result->next);
  }

  if ((mixer = mixer_open(id)) == NULL) return NULL;

  result = malloc(sizeof(Mixer));
  result->id = strdup(id);
  result->mixer = mixer;
  result->next = NULL;
  result->Sliderz = NULL;
  /* add The Mixer to the end */
  *m = result;
  return result;
}

static void remove_mixer(Mixer *m) {
  Slider *s,*s1;
  Mixer *tmp;

  for (s = m->Sliderz ; s != NULL ;s = s1) {
    gkrellm_panel_destroy(s->panel);
    if (s->bal) gkrellm_panel_destroy(s->bal->panel);
    s1 = s->next;
    free(s->bal);
    free(s);
  }

  mixer_close(m->mixer);
  free(m->id);
  if (Mixerz == m) Mixerz = m->next;
  else {
    for (tmp = Mixerz; tmp->next != m; tmp = tmp->next);
    /* tmp->next == m */
    tmp->next = m->next;
  }
}

static void remove_all_mixers() {
  while(Mixerz != NULL) remove_mixer(Mixerz);
}

static Slider *add_slider(Mixer *m, int dev) {
  Slider *result,*s;
  if (dev < 0 || dev >= mixer_get_nr_devices(m->mixer)) return NULL;
  result = malloc(sizeof(Slider));
  result->mixer = m->mixer;
  result->parent = m;
  result->dev = dev;
  result->flags = 0;
  result->next = NULL;
  result->krell = NULL;
  result->panel = NULL;
  result->balance = 0;
  result->pleft = result->pright = -1;
  result->bal = NULL;
  if (m->Sliderz == NULL) m->Sliderz = result;
  else {
    for (s = m->Sliderz ; s->next != NULL; s = s->next);
    s->next = result; 
  }
  return result;
}

/*---*/

static gint
bvolume_expose_event(GtkWidget *widget, GdkEventExpose *event,gpointer slide) {
  gdk_draw_pixmap(widget->window,
                  widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
                  ((Bslider *)slide)->panel->pixmap,
                  event->area.x, event->area.y,
                  event->area.x, event->area.y,
                  event->area.width, event->area.height);
  return TRUE;
}

static gint
volume_expose_event(GtkWidget *widget, GdkEventExpose *event,gpointer slide) {
  gdk_draw_pixmap(widget->window,
                  widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
                  ((Slider *)slide)->panel->pixmap,
                  event->area.x, event->area.y,
                  event->area.x, event->area.y,
                  event->area.width, event->area.height);
  return TRUE;
}

static gint
volume_get_volume(Slider *s) {
  gint left,right;
  mixer_get_device_volume(s->mixer,s->dev,&left,&right);
  return left > right ?  left : right;
}

static void 
volume_show_volume(Slider *s) {
  gkrellm_update_krell(s->panel,s->krell,volume_get_volume(s));
  gkrellm_draw_panel_layers(s->panel);
  gkrellm_config_modified();
}


static void
volume_set_volume(Slider *s,gint volume) {
  gint left,right;

  if (GET_FLAG(s->flags,MUTED)) return;
  volume = volume < 0 ? 0 : volume;
  if (volume < 0) volume = 0;
  else if (volume > mixer_get_device_fullscale(s->mixer,s->dev)) 
      volume = mixer_get_device_fullscale(s->mixer,s->dev);

  if (s->balance == 0 && !GET_FLAG(s->flags,BALANCE)) left = right = volume;
  else if (s->balance > 0) {
    right = volume;
    left = ((100 - s->balance)* volume) / 100;
  } else { /* balance < 0 */
    left = volume;
    right = ((100 + s->balance)* volume) / 100;
  }
  mixer_set_device_volume(s->mixer,s->dev,left,right);
  s->pleft = left; s->pright = right;
  volume_show_volume(s);
}

static void volume_show_balance(Slider *s) {
  gchar *buf;
  if (s->bal == NULL) return;
  if (s->balance == 0) buf = g_strdup("Centered");
  else buf = g_strdup_printf("%3d%% %s",abs(s->balance),
                   s->balance > 0 ? "Right" : "Left");

  gkrellm_draw_decal_text(s->bal->panel,s->bal->decal,buf,-1);
  gkrellm_update_krell(s->bal->panel,s->bal->krell,s->balance + 100 );
  gkrellm_draw_panel_layers(s->bal->panel);
  g_free(buf);
}

static void
volume_set_balance(Slider *s,gint amount) {
  if (amount < -100) amount = -100;
  else if (amount > 100) amount = 100;
  if (abs(amount) <= 3) amount = 0;
  s->balance = amount;
  volume_set_volume(s,volume_get_volume(s));
  volume_show_balance(s);
}

static void
volume_mute_mixer(Mixer *m) {
  Slider *s;
  for (s = m->Sliderz ; s != NULL ; s = s->next) {
      mixer_set_device_volume(s->mixer,s->dev,0,0);
      volume_show_volume(s);
      SET_FLAG(s->flags,MUTED);
  }
}

static void 
volume_unmute_mixer(Mixer *m) {
  Slider *s;
  for (s = m->Sliderz ; s != NULL ; s = s->next) {
      DEL_FLAG(s->flags,MUTED);
      mixer_set_device_volume(s->mixer,s->dev,s->pleft,s->pright);
      volume_show_volume(s);
  }
}

static void
volume_toggle_mute(Slider *s) {
  Mixer *m;
  if (GET_FLAG(s->flags,MUTED)) {
    if (GET_FLAG(global_flags,MUTEALL)) {
      for (m = Mixerz ; m != NULL; m = m->next) volume_unmute_mixer(m);
    } else volume_unmute_mixer(s->parent);
  } else {
    if (GET_FLAG(global_flags,MUTEALL)) {
      for (m = Mixerz ; m != NULL; m = m->next) volume_mute_mixer(m);
    } else volume_mute_mixer(s->parent);
  }
}

static gint 
bvolume_cb_scroll(GtkWidget *widget, GdkEventScroll *event,Bslider *s) {
  int amount;
  amount = event->direction == GDK_SCROLL_UP ? 5 : -5;
  volume_set_balance(s->slider,s->slider->balance + amount);
  return TRUE;
}

static gint 
volume_cb_scroll(GtkWidget *widget, GdkEventScroll *event,Slider *s) {
  gint  amount;
  amount = volume_get_volume(s);
  amount += event->direction == GDK_SCROLL_UP ? 5 : -5;
  volume_set_volume(s,amount);
  return TRUE;
}

static void
bvolume_button_press(GtkWidget *widget, GdkEventButton *ev,Bslider *s) {
  long location;
  if (ev->button == 1) {
    SET_FLAG(s->flags,IS_PRESSED);
    location = ev->x >= 0 ? ev->x : 0;
    location = (location * 200)
              / gkrellm_chart_width();
    volume_set_balance(s->slider,location - 100);
  }
}

static void
volume_button_press(GtkWidget *widget,GdkEventButton *ev,Slider *s) {
  long location;
  if (ev->button == 1) {
    SET_FLAG(s->flags,IS_PRESSED);
    location = ev->x >= 0 ? ev->x : 0;
    location = (location * mixer_get_device_fullscale(s->mixer,s->dev))
              / gkrellm_chart_width();
    volume_set_volume(s,location); 
  }
}

static void
bvolume_button_release(GtkWidget *widget,GdkEventButton *ev,Bslider *s) {
  if (ev->button == 1) DEL_FLAG(s->flags,IS_PRESSED);
  if (ev->button == 2) volume_toggle_mute(s->slider);
}

static void
volume_button_release(GtkWidget *widget,GdkEventButton *ev,Slider *s) {
  if (ev->button == 1) DEL_FLAG(s->flags,IS_PRESSED);
  if (ev->button == 2) volume_toggle_mute(s);
}


static void
bvolume_motion(GtkWidget *widget,GdkEventMotion *ev,Bslider *s) {
  gdouble location;
  if (!(GET_FLAG(s->flags,IS_PRESSED))) return;
  if (!(ev->state & GDK_BUTTON1_MASK)) {
    /* just to be sure */
    DEL_FLAG(s->flags,IS_PRESSED); return ;
  }
  location = ev->x >= 0 ? ev->x : 0;
  location = (location * 200) / gkrellm_chart_width();
  if (location > 200) location =  200 ;
  volume_set_balance(s->slider,location - 100);
}

static void
volume_motion(GtkWidget *widget,GdkEventMotion *ev,Slider *s) {
  gdouble location;
  if (!GET_FLAG(s->flags,IS_PRESSED)) return;
  if (!(ev->state & GDK_BUTTON1_MASK)) {
    /* just to be sure */
    DEL_FLAG(s->flags,IS_PRESSED); return ;
  }
  location = ev->x >= 0 ? ev->x : 0;
  location = (location * mixer_get_device_fullscale(s->mixer,s->dev))
              / gkrellm_chart_width();
  volume_set_volume(s,location);
}

static void create_bslider(Slider *slide,int first_create) {
  GkrellmStyle *panel_style = gkrellm_meter_style(VOLUME_STYLE);
  GkrellmMargin *m = gkrellm_get_style_margins(panel_style);

  GkrellmStyle *slider_style = gkrellm_krell_slider_style();
  GkrellmTextstyle *ts = gkrellm_meter_textstyle(VOLUME_STYLE);
  GkrellmPiximage *krell_image;
  Bslider *result;

  if (first_create) {
    result = malloc(sizeof(Bslider));
    result->panel = gkrellm_panel_new0();
    slide->bal = result;
    result->slider = slide;
  } else result = slide->bal;

  krell_image = gkrellm_krell_slider_piximage();
  gkrellm_set_style_slider_values_default(slider_style,
      panel_style->krell_yoff,
      m->left, m->right);

  result->krell = gkrellm_create_krell(result->panel,krell_image,slider_style);

  /* [-100..+100] */
  gkrellm_set_krell_full_scale(result->krell,200, 1);
  gkrellm_monotonic_krell_values(result->krell, FALSE);

  result->decal = gkrellm_create_decal_text(result->panel,"Centered",
      ts,panel_style,-1,-1,-1);
  gkrellm_draw_decal_text(result->panel,result->decal,"Centered",-1);

  gkrellm_panel_configure(result->panel, NULL, panel_style);
  gkrellm_panel_create(pluginbox, monitor, result->panel);


  if (first_create) {
    g_signal_connect(GTK_OBJECT(result->panel->drawing_area), "expose_event",
                         G_CALLBACK(bvolume_expose_event),result);
    g_signal_connect(G_OBJECT(result->panel->drawing_area), "scroll_event", 
                         G_CALLBACK(bvolume_cb_scroll), result);

    g_signal_connect(G_OBJECT(result->panel->drawing_area),"button_press_event",
                        G_CALLBACK(bvolume_button_press),result);
    g_signal_connect(GTK_OBJECT(result->panel->drawing_area),
        "button_release_event", G_CALLBACK(bvolume_button_release),result);
    g_signal_connect(GTK_OBJECT(result->panel->drawing_area),
        "motion_notify_event", G_CALLBACK(bvolume_motion),result);
  }

  volume_show_balance(slide);
}

static void create_slider(Slider *s,int first_create) {
  GkrellmStyle *style = gkrellm_meter_style(VOLUME_STYLE);
  GkrellmStyle *slider_style = gkrellm_krell_slider_style();
  GkrellmPiximage *krell_image;

  if (first_create) s->panel = gkrellm_panel_new0();
  gkrellm_panel_configure(s->panel,
                          mixer_get_device_name(s->mixer,s->dev),
                          style);
  gkrellm_panel_create(pluginbox, monitor, s->panel);

  krell_image = gkrellm_krell_slider_piximage();
  s->krell = gkrellm_create_krell(s->panel,krell_image,slider_style);

  gkrellm_set_krell_full_scale(s->krell, 
                               mixer_get_device_fullscale(s->mixer,s->dev), 1);
  gkrellm_monotonic_krell_values(s->krell, FALSE);

  if (first_create) {
     g_signal_connect(GTK_OBJECT(s->panel->drawing_area), "expose_event",
                         G_CALLBACK(volume_expose_event),s);
     g_signal_connect(G_OBJECT(s->panel->drawing_area),
                         "scroll_event", G_CALLBACK(volume_cb_scroll), s);
     g_signal_connect(G_OBJECT(s->panel->drawing_area), "button_press_event", 
                      G_CALLBACK(volume_button_press),s);
     g_signal_connect(GTK_OBJECT(s->panel->drawing_area),"button_release_event",
                      G_CALLBACK(volume_button_release),s);
     g_signal_connect(GTK_OBJECT(s->panel->drawing_area),"motion_notify_event",
                      G_CALLBACK(volume_motion),s);
  }

  volume_show_volume(s);
  if (GET_FLAG(s->flags,BALANCE)) create_bslider(s,first_create);
}

static void create_volume_plugin(GtkWidget *vbox,gint first_create) {
  Mixer *m;
  Slider *s;

  pluginbox = vbox;
  for (m = Mixerz ; m != NULL; m = m->next)
    for (s = m->Sliderz; s != NULL ; s = s->next) {
    create_slider(s,first_create);
  }
}

static void update_volume_plugin(void) {
  Slider *s;
  Mixer *m;
  for (m = Mixerz; m != NULL; m = m->next) 
    for (s = m->Sliderz ; s != NULL; s = s->next) {
      int left,right;
      mixer_get_device_volume(s->mixer,s->dev,&left,&right);
      /* calculate the balance and show volume if needed */
      if (s->pleft!=left || s->pright!=right) { 
        if (GET_FLAG(s->flags,BALANCE)) {
          if (left < right) {
            s->balance = 100 - (gint) rint(((gdouble)left/right) * 100);
          } else if (left > right) {
            s->balance = (gint) rint(((gdouble)right/left) * 100) - 100;
          } else if (left == right && left != 0) s->balance = 0;
          volume_show_balance(s);
        }
       if (!GET_FLAG(s->flags,MUTED)) { s->pleft = left; s->pright = right; }
       volume_show_volume(s);
     }
   }
}

static void
save_volume_plugin_config(FILE *f) {
  Mixer *m;
  Slider *s;
  if (GET_FLAG(global_flags,MUTEALL)) fprintf(f,"%s MUTEALL\n",CONFIG_KEYWORD);

  for (m = Mixerz ; m != NULL ; m = m->next) { 
    fprintf(f,"%s ADDMIXER %s\n",CONFIG_KEYWORD,m->id);

    for(s = m->Sliderz; s != NULL; s = s->next) {
      fprintf(f,"%s ADDDEV %d\n",CONFIG_KEYWORD,s->dev);

      if (strcmp(mixer_get_device_name(s->mixer,s->dev),
                 mixer_get_device_real_name(s->mixer,s->dev))) {
        fprintf(f,"%s SETDEVNAME %s\n",CONFIG_KEYWORD,
            mixer_get_device_name(s->mixer,s->dev));
      }
      if (GET_FLAG(s->flags,BALANCE))
        fprintf(f,"%s SHOWBALANCE\n",CONFIG_KEYWORD);

      if (GET_FLAG(s->flags,SAVE_VOLUME)) {
        int left,right;
        mixer_get_device_volume(s->mixer,s->dev,&left,&right);
        fprintf(f,"%s SETVOLUME %d %d\n",CONFIG_KEYWORD,left,right);
      }
    }
  }
}

static void 
load_volume_plugin_config(gchar *command) {
  static Mixer *m = NULL;
  static Slider *s = NULL;
  gchar *arg;
  /* gkrellm doesn't care if we fsck the string it gives us */
  for (arg = command; !isspace(*arg); arg++);
  *arg = '\0'; arg++;

  if (!strcmp("MUTEALL",command)) SET_FLAG(global_flags,MUTEALL);
  else if (!strcmp("ADDMIXER",command)) {
    m = add_mixer_by_id(arg);
  } else if (!strcmp("ADDDEV",command)) {
    if (m != NULL) s = add_slider(m,atoi(arg));
  } else if (!strcmp("SETDEVNAME",command)) {
    if (s != NULL) mixer_set_device_name(s->mixer,s->dev,arg);
  } else if (!strcmp("SHOWBALANCE",command)) {
    if (s != NULL) SET_FLAG(s->flags,BALANCE);
  } else if (!strcmp("SETVOLUME",command)) {
    if (s != NULL) {
      char *next;
      int left,right;
      left = strtol(arg,&next,10);
      right = strtol(next,NULL,10);
      mixer_set_device_volume(s->mixer,s->dev,left,right);
      SET_FLAG(s->flags,SAVE_VOLUME);
    }
  } 
}

/* configuration code */
enum {
  ID_COLUMN = 0, 
  NAME_COLUMN,
  C_MODEL_COLUMN,
  C_NB_COLUMN,
  N_COLUMNS
};

enum {
  C_ENABLED_COLUMN = 0,
  C_VOLUME_COLUMN,
  C_BALANCE_COLUMN,
  C_NAME_COLUMN,
  C_SNAME_COLUMN,
  C_DEVNR_COLUMN,
  C_N_COLUMNS
};

GtkListStore *model;
GtkWidget *config_notebook;
/* Used to only update the sliderz if needed. So there isn't any unnecessary
 * flickering */
gboolean mixer_config_changed = FALSE;

static void 
toggle_item(gchar *path_str,gpointer data,gint column) {
  GtkTreePath *path = gtk_tree_path_new_from_string(path_str);
  GtkTreeIter iter;
  gboolean item;

  gtk_tree_model_get_iter(GTK_TREE_MODEL(data),&iter,path);
  gtk_tree_model_get(GTK_TREE_MODEL(data),&iter,column,&item,-1);

  gtk_list_store_set(GTK_LIST_STORE(data),&iter,column,!item,-1);
  mixer_config_changed = TRUE;

  gtk_tree_path_free(path);
}

static void 
toggle_enabled(GtkCellRendererToggle *cell,gchar *path_str,gpointer data) {
  toggle_item(path_str,data,C_ENABLED_COLUMN);
}

static void 
toggle_volume(GtkCellRendererToggle *cell,gchar *path_str,gpointer data) {
  toggle_item(path_str,data,C_VOLUME_COLUMN);
}

static void
toggle_balance(GtkCellRendererToggle *cell,gchar *path_str,gpointer data) {
  toggle_item(path_str,data,C_BALANCE_COLUMN);
}

static void
device_name_edited(GtkCellRendererText *text,
                    gchar *pathstr,gchar *value,gpointer user_data) {
  GtkTreePath *path = gtk_tree_path_new_from_string(pathstr);
  GtkTreeIter iter;

  gtk_tree_model_get_iter(GTK_TREE_MODEL(user_data),&iter,path);
  gtk_list_store_set(GTK_LIST_STORE(user_data),&iter,C_SNAME_COLUMN,value,-1);
  mixer_config_changed = TRUE;
}

static GtkWidget *
create_device_notebook(GtkListStore *store, char *name) {
  GtkWidget *treeview;
  GtkCellRenderer *renderer;
  GtkWidget *scrolledwindow;
  GtkWidget *label;
  GtkWidget *vbox = gtk_vbox_new(FALSE,3); 
  
  vbox = gtk_vbox_new(FALSE,0);
  gtk_container_set_border_width(GTK_CONTAINER(vbox), 0);
  label = gtk_label_new(name);
  gtk_notebook_insert_page(GTK_NOTEBOOK(config_notebook), vbox, label,1);
  vbox = gkrellm_gtk_framed_vbox(vbox,NULL,2,TRUE,0,2);

  treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
  gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(treeview),TRUE);
  gtk_tree_selection_set_mode(
      gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview)),
      GTK_SELECTION_SINGLE);

  /* causes it to be destroyed when the treeview is destroyed */
  g_object_unref(G_OBJECT(store));

  renderer = gtk_cell_renderer_toggle_new();
  g_signal_connect(G_OBJECT(renderer),"toggled",
      G_CALLBACK(toggle_enabled),store);

  gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeview), -1, 
      "Enabled",renderer,
      "active",C_ENABLED_COLUMN,
      NULL);

  renderer = gtk_cell_renderer_toggle_new();
  g_signal_connect(G_OBJECT(renderer),"toggled",
      G_CALLBACK(toggle_volume),store);
  gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeview), -1, 
      "Save volume",renderer,
      "active",C_VOLUME_COLUMN,
      "activatable",C_ENABLED_COLUMN,
      NULL);

#if !defined(WIN32)
  renderer = gtk_cell_renderer_toggle_new();
  g_signal_connect(G_OBJECT(renderer),"toggled",
      G_CALLBACK(toggle_balance),store);
  gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeview), -1, 
      "Balance",renderer,
      "active",C_BALANCE_COLUMN,
      "activatable",C_ENABLED_COLUMN,
      NULL);
#endif

  renderer = gtk_cell_renderer_text_new();
  gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeview), -1, 
      "Name",renderer,
      "text", C_NAME_COLUMN,
      NULL);

  renderer = gtk_cell_renderer_text_new();
  gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeview),
      -1, "Shown Name",renderer,
      "text",C_SNAME_COLUMN,
      "editable",C_ENABLED_COLUMN,
      NULL);
  g_signal_connect(G_OBJECT(renderer),"edited",
      G_CALLBACK(device_name_edited),store);

  scrolledwindow = gtk_scrolled_window_new(NULL,NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow),
                     GTK_POLICY_AUTOMATIC,
                     GTK_POLICY_ALWAYS);


  gtk_box_pack_start(GTK_BOX(vbox),scrolledwindow,TRUE,TRUE,3);
  gtk_container_add(GTK_CONTAINER(scrolledwindow),treeview);

  gtk_widget_show_all(vbox);
  return vbox;
}

static void add_mixer_to_model(char *id, mixer_t *mixer, Slider *s) {
  GtkTreeIter iter;
  GtkListStore *child_model;
  gboolean enabled,save_volume,balance;
  GtkWidget *notebook;
  int i;

  child_model = gtk_list_store_new(C_N_COLUMNS,
                                  G_TYPE_BOOLEAN, /* enabled or not */
                                  G_TYPE_BOOLEAN, /* save volume or not */
                                  G_TYPE_BOOLEAN, /* show balance or not */
                                  G_TYPE_STRING,  /* real name */
                                  G_TYPE_STRING,  /* set name */
                                  G_TYPE_INT      /* device number */
      );

   for(i = 0; i < mixer_get_nr_devices(mixer); i++) {
     if (s != NULL && s->dev == i) { 
       enabled = TRUE;
       save_volume = GET_FLAG(s->flags,SAVE_VOLUME);
       balance = GET_FLAG(s->flags,BALANCE);
         s = s->next;
      } else {
        enabled = save_volume = balance = FALSE;
      }

      gtk_list_store_append(child_model,&iter);
      gtk_list_store_set(child_model,&iter,
        C_ENABLED_COLUMN,enabled,
        C_VOLUME_COLUMN,save_volume,
        C_BALANCE_COLUMN,balance,
        C_NAME_COLUMN,mixer_get_device_real_name(mixer,i),
        C_SNAME_COLUMN,mixer_get_device_name(mixer,i),
        C_DEVNR_COLUMN,i,
        -1);
  }

  notebook = create_device_notebook(child_model,mixer_get_name(mixer));

  gtk_list_store_append(model,&iter);
  gtk_list_store_set(model,&iter,
                       ID_COLUMN,id,
                       NAME_COLUMN,mixer_get_name(mixer),
                       C_MODEL_COLUMN,child_model,
                       C_NB_COLUMN,notebook,
                       -1);
  return;
}

static gboolean findid(GtkTreeModel *m,GtkTreePath *path,
                                            GtkTreeIter *iter,gpointer data) {
  char *item;
  char **arg = (char **) data;
  gtk_tree_model_get(m,iter,ID_COLUMN,&item,-1);
  if (!strcmp(item,*arg)) {
    *arg = NULL;
    return TRUE;
  }
  return FALSE;
}

static void add_mixerid_to_model(char *id,gboolean gui) {
  char **arg = &id;
  char *name;
  mixer_t *mixer;

  gtk_tree_model_foreach(GTK_TREE_MODEL(model),findid,arg);
  if (id == NULL) { 
    if (gui) gkrellm_message_window("Error","Id already in list",NULL); 
    return;
  }
  if ((mixer = mixer_open(id)) == NULL) {
    if (gui) {
      name = 
        g_strdup_printf("Couldn't open %s or %s isn't a mixer device",id,id);
      gkrellm_message_window("Error",name,NULL);
      g_free(name);
    }
    return;
  }
  add_mixer_to_model(id,mixer, NULL);
  mixer_close(mixer);
  return;
}

static void up_clicked(GtkWidget *widget,gpointer user_data) {
  GtkTreeIter selected,up,new;
  GtkTreeView *view = GTK_TREE_VIEW(user_data);
  GtkTreeSelection *selection;
  GtkTreeModel *model;
  GtkTreePath *path;
  gchar *name,*id;
  gpointer *child_model,*nb;

  selection = gtk_tree_view_get_selection(view);

  if (!gtk_tree_selection_get_selected(selection,&model,&selected)) return;
  gtk_tree_model_get(model,&selected,
       ID_COLUMN,&id,
       NAME_COLUMN,&name,
       C_MODEL_COLUMN,&child_model,
       C_NB_COLUMN,&nb,
        -1);

  path = gtk_tree_model_get_path(model,&selected);
  if (!gtk_tree_path_prev(path)) return;
  if (!gtk_tree_model_get_iter(model,&up,path)) return;
  gtk_list_store_remove(GTK_LIST_STORE(model),&selected);
  gtk_list_store_insert_before(GTK_LIST_STORE(model),&new,&up);

  gtk_list_store_set(GTK_LIST_STORE(model),&new,
       ID_COLUMN,id,
       NAME_COLUMN,name,
       C_MODEL_COLUMN,child_model,
       C_NB_COLUMN,nb,
       -1);
  mixer_config_changed = TRUE;

}

static void down_clicked(GtkWidget *widget,gpointer user_data) {
  GtkTreeIter selected,down,new;
  GtkTreeView *view = GTK_TREE_VIEW(user_data);
  GtkTreeSelection *selection;
  GtkTreeModel *model;
  GtkTreePath *path;
  gchar *name,*id;
  gpointer *child_model,*nb;

  selection = gtk_tree_view_get_selection(view);

  if (!gtk_tree_selection_get_selected(selection,&model,&selected)) return;
  gtk_tree_model_get(model,&selected,
       ID_COLUMN,&id,
       NAME_COLUMN,&name,
       C_MODEL_COLUMN,&child_model,
       C_NB_COLUMN,&nb,
        -1);

  path = gtk_tree_model_get_path(model,&selected);
  gtk_tree_path_next(path);
  if (!gtk_tree_model_get_iter(model,&down,path)) return;
  gtk_list_store_insert_after(GTK_LIST_STORE(model),&new,&down);

  gtk_list_store_set(GTK_LIST_STORE(model),&new,
       ID_COLUMN,id,
       NAME_COLUMN,name,
       C_MODEL_COLUMN,child_model,
       C_NB_COLUMN,nb,
       -1);
  mixer_config_changed = TRUE;
  gtk_list_store_remove(GTK_LIST_STORE(model),&selected);
}

#ifndef WIN32
static void file_choosen(GtkWidget *w,gpointer selector) {
  char *id;
  id = (char *) gtk_file_selection_get_filename(GTK_FILE_SELECTION(selector));
  add_mixerid_to_model(id,TRUE);
}

static void select_file(GtkWidget *widget,gpointer user_data) {
  GtkWidget *selector;
  selector = gtk_file_selection_new("Please select a mixer device");
  gtk_file_selection_set_filename(GTK_FILE_SELECTION(selector),"/dev/");

  g_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(selector)->ok_button),
      "clicked",
      G_CALLBACK(file_choosen),
      selector);

  g_signal_connect_swapped(GTK_OBJECT(GTK_FILE_SELECTION(selector)->ok_button),
      "clicked",
      G_CALLBACK (gtk_widget_destroy), (gpointer) selector); 

  g_signal_connect_swapped(
      GTK_OBJECT(GTK_FILE_SELECTION(selector)->cancel_button),
      "clicked", G_CALLBACK (gtk_widget_destroy), (gpointer) selector); 
  gtk_widget_show(selector);
}
#endif

static void create_volume_model(void) {
  Mixer *m;
  mixer_idz_t *idz,*t;

  model = gtk_list_store_new(N_COLUMNS,
                             G_TYPE_STRING,  /* id */
                             G_TYPE_STRING,  /* name */
                             G_TYPE_POINTER, /* pointer to the child store */
                             G_TYPE_POINTER  /* pointer to the child NB */
      );
  for (m = Mixerz; m != NULL; m = m->next) {
    add_mixer_to_model(m->id,m->mixer,m->Sliderz);
  }
  idz = mixer_get_id_list();
  for (t = idz; t != NULL; t = t->next) {
    add_mixerid_to_model(t->id,FALSE);
  }
  mixer_free_idz(idz);
}

static void create_volume_plugin_mixer_tabs(void) {
  GtkWidget *treeview;
  GtkWidget *widget,*hbox,*vbox;
  GtkWidget *scrolledwindow;
  GtkWidget *notebook;
  GtkCellRenderer *renderer;
  
  notebook = 
    gkrellm_gtk_framed_notebook_page(config_notebook,"Available mixers");

  vbox = gtk_vbox_new(FALSE,3);
  gtk_container_add(GTK_CONTAINER(notebook),vbox);

  create_volume_model();

  /* creating the treeview */

  treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
  gtk_tree_view_set_reorderable(GTK_TREE_VIEW(treeview),TRUE);
  gtk_tree_selection_set_mode(
      gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview)),
      GTK_SELECTION_SINGLE);

  /* causes it to be destroyed when the treeview is destroyed */
  g_object_unref(G_OBJECT(model));

  renderer = gtk_cell_renderer_toggle_new();
  g_signal_connect(G_OBJECT(renderer),"toggled",
      G_CALLBACK(toggle_enabled),model);

  renderer = gtk_cell_renderer_text_new();
  gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeview), -1, 
      "Id",renderer,
      "text", ID_COLUMN,
      NULL);

  renderer = gtk_cell_renderer_text_new();
  gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeview),
      -1, "Mixer Name",renderer,
      "text",NAME_COLUMN,
      NULL);

  scrolledwindow = gtk_scrolled_window_new(NULL,NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow),
                     GTK_POLICY_AUTOMATIC,
                     GTK_POLICY_ALWAYS);


  gtk_box_pack_start(GTK_BOX(vbox),scrolledwindow,TRUE,TRUE,3);
  gtk_container_add(GTK_CONTAINER(scrolledwindow),treeview);

  hbox = gtk_hbox_new(FALSE,3);

  widget = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
  g_signal_connect(G_OBJECT(widget),"clicked",
                   G_CALLBACK(up_clicked),treeview);
  gtk_box_pack_start(GTK_BOX(hbox),widget,FALSE,FALSE,3);

  widget = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
  g_signal_connect(G_OBJECT(widget),"clicked",
                   G_CALLBACK(down_clicked),treeview);
  gtk_box_pack_start(GTK_BOX(hbox),widget,FALSE,FALSE,3);


#ifndef WIN32
  widget = gtk_button_new_from_stock(GTK_STOCK_ADD);
  gtk_box_pack_end(GTK_BOX(hbox),widget,FALSE,FALSE,3);
  g_signal_connect(G_OBJECT(widget),"clicked",
                   G_CALLBACK(select_file),model);
#endif
  gtk_box_pack_start(GTK_BOX(vbox),hbox,FALSE,FALSE,3);
  gtk_widget_show_all(notebook);
}

static void option_toggle(GtkWidget *widget,gpointer data) {
  if ( gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
    SET_FLAG(config_global_flags,GPOINTER_TO_INT(data));
  else
    DEL_FLAG(config_global_flags,GPOINTER_TO_INT(data));
}

static void
create_volume_plugin_config(GtkWidget *tab) {
  GtkWidget *label,*text,*page,*toggle;
  gchar *info_text[] = { 
   "<b>Gkrellm Volume Plugin\n\n",
   "This plugin allows you to control your mixers with gkrellm\n\n",
   "<b>User Interface:\n",
   "\tDragging the krells around or turning your mousewheel above a panel\n",
   "\twill change the volume of the device.\n",
   "\tMiddle mouse button will (un)mute the mixer\n\n",
   "<b> Configuration:\n",
   "\tThe available mixers tab shows the detected mixers. The order of the\n",
   "\tmixers is the same as they will be in the main window\n",
   "\n",
   "\tEach mixers gets it's own tab. You can adjust each device seperatly:\n",
   "\t * Enabled: will cause the device to show up in the main window.\n",
   "\t * Save volume: will cause the volume and balance to be saved on exit\n",
   "\t   and reset on startup.\n",
#if !defined(WIN32)
   "\t * Balance: Gives you a panel below the device to control it's balance\n",
#endif
   "\t * Name: The 'official' name of the device.\n",
   "\t * Shown name: can be edited and is the name shown in the main window.\n",
   "\n",
   "\t Options: Currently only one option, which should explain itself\n", 
    "\n"
  };

  gchar *plugin_about_text = g_strdup_printf(
     "Volumeplugin %d.%d.%d\n" \
     "GKrellM volume Plugin\n\n" \
     "Copyright (C) 2000 Sjoerd Simons\n" \
     "sjoerd@luon.net\n" \
     "http://gkrellm.luon.net \n\n" \
     "Released under the GNU Public Licence",
     VOLUME_MAJOR_VERSION,VOLUME_MINOR_VERSION,VOLUME_EXTRA_VERSION);

  config_global_flags= global_flags;

  config_notebook = gtk_notebook_new();
  gtk_notebook_set_scrollable(GTK_NOTEBOOK(config_notebook),TRUE);
  gtk_notebook_set_tab_pos(GTK_NOTEBOOK(config_notebook), GTK_POS_TOP);

  gtk_box_pack_start(GTK_BOX(tab), config_notebook, TRUE, TRUE, 0);

  /* mixer tabs */
  create_volume_plugin_mixer_tabs();
  /* global options tab */
  page = gkrellm_gtk_framed_notebook_page(config_notebook,"Options");
  toggle = gtk_check_button_new_with_label("Mute all mixers at the same time");
  g_signal_connect(GTK_OBJECT(toggle),"toggled",
                           G_CALLBACK(option_toggle),GINT_TO_POINTER(MUTEALL));

  gtk_box_pack_start(GTK_BOX(page),toggle,FALSE,FALSE,3);
  
  /* info tab */
  page = gkrellm_gtk_notebook_page(config_notebook,"Info");
  text = gkrellm_gtk_scrolled_text_view(page,NULL,
                                   GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
  gkrellm_gtk_text_view_append_strings(text,info_text,
      sizeof(info_text)/sizeof(gchar *));


  /* about tab */
  text = gtk_label_new(plugin_about_text); 
  label = gtk_label_new("About");
  gtk_notebook_append_page(GTK_NOTEBOOK(config_notebook),text,label);

  g_free(plugin_about_text);
  gtk_widget_show_all(config_notebook);
}

static gboolean add_configed_mixer_device(GtkTreeModel *m, GtkTreePath *path,
                                              GtkTreeIter *iter,gpointer data) {
  gboolean enabled;
  gboolean save_volume;
  gboolean balance;
  gint nr;
  Slider *s;
  gchar *rname,*name;
  gchar *id = (gchar *) data;
  Mixer *mixer;

  gtk_tree_model_get(m,iter,C_ENABLED_COLUMN,&enabled,-1);
  if (enabled) {
    mixer = add_mixer_by_id(id);

    gtk_tree_model_get(m,iter,
          C_DEVNR_COLUMN,&nr,
          C_VOLUME_COLUMN,&save_volume,
          C_BALANCE_COLUMN,&balance,
          C_NAME_COLUMN,&rname,
          C_SNAME_COLUMN,&name,
          -1);
    if (strcmp(name,rname)) mixer_set_device_name(mixer->mixer,nr,name);
    
    s = add_slider(mixer,nr);
    if (save_volume) SET_FLAG(s->flags,SAVE_VOLUME);
    else DEL_FLAG(s->flags,SAVE_VOLUME);
    if (balance) SET_FLAG(s->flags,BALANCE);
    else DEL_FLAG(s->flags,BALANCE);
    create_slider(s,1);
  }
  return FALSE;
}

static gboolean add_configed_mixer(GtkTreeModel *m,GtkTreePath *path,
                             GtkTreeIter *iter,gpointer data) {
  GtkListStore *store;
  gchar *id;

  gtk_tree_model_get(m,iter,ID_COLUMN,&id,C_MODEL_COLUMN,&store,-1);
  gtk_tree_model_foreach(GTK_TREE_MODEL(store),add_configed_mixer_device,id);

  return FALSE;
}

void apply_volume_plugin_config(void) {
  if (mixer_config_changed) {
    remove_all_mixers();
    gtk_tree_model_foreach(GTK_TREE_MODEL(model),add_configed_mixer,NULL);
    mixer_config_changed = FALSE;
  }
  global_flags = config_global_flags;
}

/* end of configuration code */

static GkrellmMonitor plugin_mon  = {
  "Volume Plugin",      /* Name, for config tab.    */
  0,          /* Id,  0 if a plugin       */
  create_volume_plugin,    /* The create function      */
  update_volume_plugin,      /* The update function      */
  create_volume_plugin_config, /* The config tab create function   */
  apply_volume_plugin_config,       /* Apply the config function        */
  save_volume_plugin_config, /* Save user config     */
  load_volume_plugin_config, /* Load user config     */
  CONFIG_KEYWORD,   /* config keyword     */

  NULL,     /* Undefined 2  */
  NULL,     /* Undefined 1  */
  NULL,     /* Undefined 0  */
  LOCATION,

  NULL,       /* Handle if a plugin, filled in by GKrellM     */
  NULL        /* path if a plugin, filled in by GKrellM       */
};

#if defined(WIN32)
    __declspec(dllexport) GkrellmMonitor *
        gkrellm_init_plugin(win32_plugin_callbacks* calls)
#else
    GkrellmMonitor * gkrellm_init_plugin(void)
#endif
{
  #if defined(WIN32)
    callbacks = calls;
  #endif
  style_id = gkrellm_add_meter_style(&plugin_mon,"volume");
  Mixerz = NULL;
  monitor = &plugin_mon;
  return monitor;
}