File: hardware.c

package info (click to toggle)
mudita24 1.0.3%2Bsvn13-6
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 604 kB
  • ctags: 626
  • sloc: ansic: 8,577; makefile: 5
file content (844 lines) | stat: -rw-r--r-- 28,986 bytes parent folder | download | duplicates (3)
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
/*****************************************************************************
   hardware.c - Hardware Settings
   Copyright (C) 2000 by Jaroslav Kysela <perex@perex.cz>
   
   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
******************************************************************************/

#include "envy24control.h"

static snd_ctl_elem_value_t *internal_clock;
static snd_ctl_elem_value_t *internal_clock_default;
static snd_ctl_elem_value_t *word_clock_sync;
static snd_ctl_elem_value_t *rate_locking;
static snd_ctl_elem_value_t *rate_reset;
static snd_ctl_elem_value_t *volume_rate;
static snd_ctl_elem_value_t *spdif_input;
static snd_ctl_elem_value_t *spdif_output;
static snd_ctl_elem_value_t *analog_input_select;
static snd_ctl_elem_value_t *breakbox_led;
static snd_ctl_elem_value_t *spdif_on_off;
static snd_ctl_elem_value_t *phono_input;
static snd_ctl_elem_value_t *iec958_in_status; /* NPM: add feature to display "Delta IEC958 Input Status" */

static inline int is_update_needed(void);

#define toggle_set(widget, state) \
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), state);

static int is_active(GtkWidget *widget)
{
	return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)) ? 1 : 0;
}

void master_clock_update(void)
{
	int err, rate, need_default_update;
	
	if ((err = snd_ctl_elem_read(ctl, internal_clock)) < 0)
		g_print("Unable to read Internal Clock state: %s\n", snd_strerror(err));
	if ((err = snd_ctl_elem_read(ctl, internal_clock_default)) < 0)
		g_print("Unable to read Internal Clock Default state: %s\n", snd_strerror(err));
	if (card_eeprom.subvendor == ICE1712_SUBDEVICE_DELTA1010 ||
	    card_eeprom.subvendor == ICE1712_SUBDEVICE_DELTA1010LT) {
		if ((err = snd_ctl_elem_read(ctl, word_clock_sync)) < 0)
			g_print("Unable to read word clock sync selection: %s\n", snd_strerror(err));
	}
	if (snd_ctl_elem_value_get_enumerated(internal_clock, 0) == 13) {
		if (snd_ctl_elem_value_get_boolean(word_clock_sync, 0)) {
			toggle_set(hw_master_clock_word_radio, TRUE);
		} else {
			toggle_set(hw_master_clock_spdif_radio, TRUE);
		}
	} else {
//		toggle_set(hw_master_clock_xtal_radio, TRUE);
		need_default_update = !is_update_needed() ? 1 : 0;
		if (need_default_update) {
			rate = snd_ctl_elem_value_get_enumerated(internal_clock_default, 0);
		} else {
			rate = snd_ctl_elem_value_get_enumerated(internal_clock, 0);
		}
		switch (rate) {
		case 5: toggle_set(hw_master_clock_xtal_22050, TRUE); break;
		case 7: toggle_set(hw_master_clock_xtal_32000, TRUE); break;
		case 8: toggle_set(hw_master_clock_xtal_44100, TRUE); break;
		case 9: toggle_set(hw_master_clock_xtal_48000, TRUE); break;
		case 11: toggle_set(hw_master_clock_xtal_88200, TRUE); break;
		case 12: toggle_set(hw_master_clock_xtal_96000, TRUE); break;
		default:
			    g_print("Error in rate: %d\n", rate);
			    break;
		}
	}
	internal_clock_status_timeout_callback(NULL);
	master_clock_status_timeout_callback(NULL);
}

static void master_clock_word_select(int on)
{
	int err;

	if (card_eeprom.subvendor != ICE1712_SUBDEVICE_DELTA1010 &&
	    card_eeprom.subvendor != ICE1712_SUBDEVICE_DELTA1010LT)
		return;
	snd_ctl_elem_value_set_boolean(word_clock_sync, 0, on ? 1 : 0);
	if ((err = snd_ctl_elem_write(ctl, word_clock_sync)) < 0)
		g_print("Unable to write word clock sync selection: %s\n", snd_strerror(err));
}

static void internal_clock_set(int xrate)
{
	int err;

	master_clock_word_select(0);
	snd_ctl_elem_value_set_enumerated(internal_clock, 0, xrate);
	if ((err = snd_ctl_elem_write(ctl, internal_clock)) < 0)
		g_print("Unable to write internal clock rate: %s\n", snd_strerror(err));
}

void internal_clock_toggled(GtkWidget *togglebutton, gpointer data)
{
	char *what = (char *) data;

	if (!is_active(togglebutton))
		return;
	if (!strcmp(what, "22050")) {
		internal_clock_set(5);
	} else if (!strcmp(what, "32000")) {
		internal_clock_set(7);
	} else if (!strcmp(what, "44100")) {
		internal_clock_set(8);
	} else if (!strcmp(what, "48000")) {
		internal_clock_set(9);
	} else if (!strcmp(what, "88200")) {
		internal_clock_set(11);
	} else if (!strcmp(what, "96000")) {
		internal_clock_set(12);
	} else if (!strcmp(what, "SPDIF")) {
		internal_clock_set(13);
	} else if (!strcmp(what, "WordClock")) {
		internal_clock_set(13); // 13 should be considered '!internal'
		master_clock_word_select(1);
	} else {
		g_print("internal_clock_toggled: %s ???\n", what);
	}
}

static int is_rate_locked(void)
{
	int err;
	
	if ((err = snd_ctl_elem_read(ctl, rate_locking)) < 0)
		g_print("Unable to read rate locking state: %s\n", snd_strerror(err));
	return snd_ctl_elem_value_get_boolean(rate_locking, 0) ? 1 : 0;
}

static int is_rate_reset(void)
{
	int err;
	
	if ((err = snd_ctl_elem_read(ctl, rate_reset)) < 0)
		g_print("Unable to read rate reset state: %s\n", snd_strerror(err));
	return snd_ctl_elem_value_get_boolean(rate_reset, 0) ? 1 : 0;
}

static inline int is_update_needed(void)
{
	return (is_rate_locked() || !is_rate_reset());
}

gint master_clock_status_timeout_callback(gpointer data)
{
	snd_ctl_elem_value_t *sw;
	int err;
	
	if (card_eeprom.subvendor != ICE1712_SUBDEVICE_DELTA1010 && card_eeprom.subvendor != ICE1712_SUBDEVICE_DELTA1010LT)
		return FALSE;
	snd_ctl_elem_value_alloca(&sw);
	snd_ctl_elem_value_set_interface(sw, SND_CTL_ELEM_IFACE_MIXER);
	snd_ctl_elem_value_set_name(sw, "Word Clock Status");
	if ((err = snd_ctl_elem_read(ctl, sw)) < 0)
		g_print("Unable to determine word clock status: %s\n", snd_strerror(err));
	gtk_label_set_text(GTK_LABEL(hw_master_clock_status_label),
			   snd_ctl_elem_value_get_boolean(sw, 0) ? "No signal" : "Locked");
	return TRUE;
}

gint internal_clock_status_timeout_callback(gpointer data)
{
	int err, rate, need_update;
	char *label;
	
	if ((err = snd_ctl_elem_read(ctl, internal_clock)) < 0)
		g_print("Unable to read Internal Clock state: %s\n", snd_strerror(err));
	if ((err = snd_ctl_elem_read(ctl, internal_clock_default)) < 0)
		g_print("Unable to read Internal Clock Default state: %s\n", snd_strerror(err));
	if (card_eeprom.subvendor == ICE1712_SUBDEVICE_DELTA1010 ||
	    card_eeprom.subvendor == ICE1712_SUBDEVICE_DELTA1010LT) {
		if ((err = snd_ctl_elem_read(ctl, word_clock_sync)) < 0)
			g_print("Unable to read word clock sync selection: %s\n", snd_strerror(err));
	}
	need_update = is_update_needed() ? 1 : 0;
	if (snd_ctl_elem_value_get_enumerated(internal_clock, 0) == 13) {
		if (snd_ctl_elem_value_get_boolean(word_clock_sync, 0)) {
			label = "Word Clock";
		} else {
			label = "S/PDIF";
		}
	} else {
//		toggle_set(hw_master_clock_xtal_radio, TRUE);
		rate = snd_ctl_elem_value_get_enumerated(internal_clock, 0);
//		g_print("Rate: %d need_update: %d\n", rate, need_update); // for debug
		switch (rate) {
		case 0: label = "8000"; break;
		case 1: label = "9600"; break;
		case 2: label = "11025"; break;
		case 3: label = "12000"; break;
		case 4: label = "16000"; break;
		case 5: label = "22050";
			if (need_update)
			toggle_set(hw_master_clock_xtal_22050, TRUE); break;
		case 6: label = "24000"; break;
		case 7: label = "32000";
			if (need_update)
			toggle_set(hw_master_clock_xtal_32000, TRUE); break;
		case 8: label = "44100";
			if (need_update)
			toggle_set(hw_master_clock_xtal_44100, TRUE); break;
		case 9: label = "48000";
			if (need_update)
			toggle_set(hw_master_clock_xtal_48000, TRUE); break;
		case 10: label = "64000"; break;
		case 11: label = "88200";
			if (need_update)
			toggle_set(hw_master_clock_xtal_88200, TRUE); break;
		case 12: label = "96000";
			if (need_update)
			toggle_set(hw_master_clock_xtal_96000, TRUE); break;
		default:
			    label = "ERROR";
			    g_print("Error in rate: %d\n", rate);
			    break;
		}
		if (!need_update) {	//default clock need update
			rate = snd_ctl_elem_value_get_enumerated(internal_clock_default, 0);
			switch (rate) {
			case 5: toggle_set(hw_master_clock_xtal_22050, TRUE); break;
			case 7: toggle_set(hw_master_clock_xtal_32000, TRUE); break;
			case 8: toggle_set(hw_master_clock_xtal_44100, TRUE); break;
			case 9: toggle_set(hw_master_clock_xtal_48000, TRUE); break;
			case 11: toggle_set(hw_master_clock_xtal_88200, TRUE); break;
			case 12: toggle_set(hw_master_clock_xtal_96000, TRUE); break;
			default:
				g_print("Error in rate: %d\n", rate);
				break;
			}
		}
	}
	gtk_label_set_text(GTK_LABEL(hw_master_clock_actual_rate_label), label);
	return TRUE;
}

gint rate_locking_status_timeout_callback(gpointer data)
{
    int state;

    if (is_active(hw_rate_locking_check) != (state = is_rate_locked())) {
	toggle_set(hw_rate_locking_check, state ? TRUE : FALSE);
    }
    return TRUE;
}

gint rate_reset_status_timeout_callback(gpointer data)
{
    int state;

    if (is_active(hw_rate_reset_check) != (state = is_rate_reset())) {
	toggle_set(hw_rate_reset_check, state ? TRUE : FALSE);
    }
    return TRUE;
}

/* NPM: add feature to display "Delta IEC958 Input Status" */
static int iec958_input_status_enabled = TRUE;
gint iec958_input_status_timeout_callback(gpointer data)
{
  if (gtk_widget_get_visible(hw_iec958_input_status_label) && iec958_input_status_enabled) {
    int err;
    if ((err = snd_ctl_elem_read(ctl, iec958_in_status)) < 0) {
      char temp_text[1024];
      sprintf(temp_text,
	      "<span size=\"small\">Unable to read IEC958 Input Status:\n     %s</span>",
	      snd_strerror(err));
      gtk_label_set_markup(GTK_LABEL(hw_iec958_input_status_label),
			   temp_text);
      iec958_input_status_enabled = FALSE; /* NPM: to prevent constant retries on HW that doesn't support this feature, if it fails the first time it tries, assume it won't succeed later */
    }
    else if (snd_ctl_elem_value_get_boolean(iec958_in_status, 0))
      gtk_label_set_markup(GTK_LABEL(hw_iec958_input_status_label),
			   "<span size=\"medium\">Input Active</span>");
    else
      gtk_label_set_markup(GTK_LABEL(hw_iec958_input_status_label),
			   "<span size=\"medium\">No Signal Detected</span>");
  }
  return TRUE;
}

void rate_locking_update(void)
{
	int err;
	
	if ((err = snd_ctl_elem_read(ctl, rate_locking)) < 0)
		g_print("Unable to read rate locking state: %s\n", snd_strerror(err));
	if (snd_ctl_elem_value_get_boolean(rate_locking, 0))
			toggle_set(hw_rate_locking_check, TRUE);
}

void rate_reset_update(void)
{
	int err;
	
	if ((err = snd_ctl_elem_read(ctl, rate_reset)) < 0)
		g_print("Unable to read rate reset state: %s\n", snd_strerror(err));
	if (snd_ctl_elem_value_get_boolean(rate_reset, 0))
			toggle_set(hw_rate_reset_check, TRUE);
}

static void rate_locking_set(int on)
{
	int err;

	snd_ctl_elem_value_set_boolean(rate_locking, 0, on ? 1 : 0);
	if ((err = snd_ctl_elem_write(ctl, rate_locking)) < 0)
		g_print("Unable to write rate locking state: %s\n", snd_strerror(err));
}

static void rate_reset_set(int on)
{
	int err;

	snd_ctl_elem_value_set_boolean(rate_reset, 0, on ? 1 : 0);
	if ((err = snd_ctl_elem_write(ctl, rate_reset)) < 0)
		g_print("Unable to write rate reset state: %s\n", snd_strerror(err));
}

void rate_locking_toggled(GtkWidget *togglebutton, gpointer data)
{
	char *what = (char *) data;

	if (!is_active(togglebutton)) {
		rate_locking_set(0);
		return;
	}
	if (!strcmp(what, "locked")) {
		rate_locking_set(1);
		internal_clock_status_timeout_callback(NULL);
	} else {
		g_print("rate_locking_toggled: %s ???\n", what);
	}
}

void rate_reset_toggled(GtkWidget *togglebutton, gpointer data)
{
	char *what = (char *) data;

	if (!is_active(togglebutton)) {
		rate_reset_set(0);
		internal_clock_status_timeout_callback(NULL);
		return;
	}
	if (!strcmp(what, "reset")) {
		rate_reset_set(1);
	} else {
		g_print("rate_reset_toggled: %s ???\n", what);
	}
}

void volume_change_rate_update(void)
{
	int err;
	
	if ((err = snd_ctl_elem_read(ctl, volume_rate)) < 0)
		g_print("Unable to read volume change rate: %s\n", snd_strerror(err));
	gtk_adjustment_set_value(GTK_ADJUSTMENT(hw_volume_change_adj),
				 snd_ctl_elem_value_get_integer(volume_rate, 0));
}

void volume_change_rate_adj(GtkAdjustment *adj, gpointer data)
{
	int err;
	
	snd_ctl_elem_value_set_integer(volume_rate, 0, gtk_adjustment_get_value(adj));
	if ((err = snd_ctl_elem_write(ctl, volume_rate)) < 0)
		g_print("Unable to write volume change rate: %s\n", snd_strerror(err));
}

void spdif_output_update(void)
{
	int err;
	snd_aes_iec958_t iec958;
	
	if ((err = snd_ctl_elem_read(ctl, spdif_output)) < 0) {
		if (err == -ENOENT)
			return;
		g_print("Unable to read Delta S/PDIF output state: %s\n", snd_strerror(err));
	}
	snd_ctl_elem_value_get_iec958(spdif_output, &iec958);
	if (!(iec958.status[0] & IEC958_AES0_PROFESSIONAL)) {		/* consumer */
		toggle_set(hw_spdif_consumer_radio, TRUE);
		if (iec958.status[0] & IEC958_AES0_CON_NOT_COPYRIGHT) {
			toggle_set(hw_consumer_copyright_off_radio, TRUE);
		} else {
			toggle_set(hw_consumer_copyright_on_radio, TRUE);
		}
		if ((iec958.status[0] & IEC958_AES0_CON_EMPHASIS) != IEC958_AES0_CON_EMPHASIS_5015) {
			toggle_set(hw_consumer_emphasis_none_radio, TRUE);
		} else {
			toggle_set(hw_consumer_emphasis_5015_radio, TRUE);
		}
		switch (iec958.status[1] & IEC958_AES1_CON_CATEGORY) {
		case IEC958_AES1_CON_MAGNETIC_ID: toggle_set(hw_consumer_category_dat_radio, TRUE); break;
		case IEC958_AES1_CON_DIGDIGCONV_ID: toggle_set(hw_consumer_category_pcm_radio, TRUE); break;
		case IEC958_AES1_CON_GENERAL: toggle_set(hw_consumer_category_general_radio, TRUE); break;
		case IEC958_AES1_CON_LASEROPT_ID:
		default: toggle_set(hw_consumer_category_cd_radio, TRUE); break;
		}
		if (iec958.status[1] & IEC958_AES1_CON_ORIGINAL) {
			toggle_set(hw_consumer_copy_original_radio, TRUE);
		} else {
			toggle_set(hw_consumer_copy_1st_radio, TRUE);
		}
	} else {
		toggle_set(hw_spdif_professional_radio, TRUE);
		if (!(iec958.status[0] & IEC958_AES0_NONAUDIO)) {
			toggle_set(hw_spdif_profi_audio_radio, TRUE);
		} else {
			toggle_set(hw_spdif_profi_nonaudio_radio, TRUE);
		}
		switch (iec958.status[0] & IEC958_AES0_PRO_EMPHASIS) {
		case IEC958_AES0_PRO_EMPHASIS_CCITT: toggle_set(hw_profi_emphasis_ccitt_radio, TRUE); break;
		case IEC958_AES0_PRO_EMPHASIS_NONE: toggle_set(hw_profi_emphasis_none_radio, TRUE); break;
		case IEC958_AES0_PRO_EMPHASIS_5015: toggle_set(hw_profi_emphasis_5015_radio, TRUE); break;
		case IEC958_AES0_PRO_EMPHASIS_NOTID:
		default: toggle_set(hw_profi_emphasis_notid_radio, TRUE); break;
		}
		if ((iec958.status[1] & IEC958_AES1_PRO_MODE) == IEC958_AES1_PRO_MODE_STEREOPHONIC) {
			toggle_set(hw_profi_stream_stereo_radio, TRUE);
		} else {
			toggle_set(hw_profi_stream_notid_radio, TRUE);
		}
	}
}

static void spdif_output_write(void)
{
	int err;

	if ((err = snd_ctl_elem_write(ctl, spdif_output)) < 0)
		g_print("Unable to write Delta S/PDIF Output Defaults: %s\n", snd_strerror(err));
}

void profi_data_toggled(GtkWidget *togglebutton, gpointer data)
{
	char *str = (char *)data;
	snd_aes_iec958_t iec958;

	snd_ctl_elem_value_get_iec958(spdif_output, &iec958);
	if (!is_active(togglebutton))
		return;
	if (!(iec958.status[0] & IEC958_AES0_PROFESSIONAL))
		return;
	if (!strcmp(str, "Audio")) {
		iec958.status[0] &= ~IEC958_AES0_NONAUDIO;
	} else if (!strcmp(str, "Non-audio")) {
		iec958.status[0] |= IEC958_AES0_NONAUDIO;
	}
	snd_ctl_elem_value_set_iec958(spdif_output, &iec958);
	spdif_output_write();
}

void profi_stream_toggled(GtkWidget *togglebutton, gpointer data)
{
	char *str = (char *)data;
	snd_aes_iec958_t iec958;

	if (!is_active(togglebutton))
		return;
	snd_ctl_elem_value_get_iec958(spdif_output, &iec958);
	if (!(iec958.status[0] & IEC958_AES0_PROFESSIONAL))
		return;
	iec958.status[1] &= ~IEC958_AES1_PRO_MODE;
	if (!strcmp(str, "NOTID")) {
		iec958.status[0] |= IEC958_AES1_PRO_MODE_STEREOPHONIC;
	} else if (!strcmp(str, "Stereo")) {
		iec958.status[0] |= IEC958_AES1_PRO_MODE_NOTID;
	}
	snd_ctl_elem_value_set_iec958(spdif_output, &iec958);
	spdif_output_write();
}

void profi_emphasis_toggled(GtkWidget *togglebutton, gpointer data)
{
	char *str = (char *)data;
	snd_aes_iec958_t iec958;

	snd_ctl_elem_value_get_iec958(spdif_output, &iec958);
	if (!is_active(togglebutton))
		return;
	if (!(iec958.status[0] & IEC958_AES0_PROFESSIONAL))
		return;
	iec958.status[0] &= ~IEC958_AES0_PRO_EMPHASIS;
	if (!strcmp(str, "CCITT")) {
		iec958.status[0] |= IEC958_AES0_PRO_EMPHASIS_CCITT;
	} else if (!strcmp(str, "No")) {
		iec958.status[0] |= IEC958_AES0_PRO_EMPHASIS_NONE;
	} else if (!strcmp(str, "5015")) {
		iec958.status[0] |= IEC958_AES0_PRO_EMPHASIS_5015;
	} else if (!strcmp(str, "NOTID")) {
		iec958.status[0] |= IEC958_AES0_PRO_EMPHASIS_NOTID;
	}
	snd_ctl_elem_value_set_iec958(spdif_output, &iec958);
	spdif_output_write();
}

void consumer_copyright_toggled(GtkWidget *togglebutton, gpointer data)
{
	char *str = (char *)data;
	snd_aes_iec958_t iec958;

	snd_ctl_elem_value_get_iec958(spdif_output, &iec958);	
	if (!is_active(togglebutton))
		return;
	if (iec958.status[0] & IEC958_AES0_PROFESSIONAL)
		return;
	if (!strcmp(str, "Copyright")) {
		iec958.status[0] &= ~IEC958_AES0_CON_NOT_COPYRIGHT;
	} else if (!strcmp(str, "Permitted")) {
		iec958.status[1] |= IEC958_AES0_CON_NOT_COPYRIGHT;
	}
	snd_ctl_elem_value_set_iec958(spdif_output, &iec958);
	spdif_output_write();
}

void consumer_copy_toggled(GtkWidget *togglebutton, gpointer data)
{
	char *str = (char *)data;
	snd_aes_iec958_t iec958;

	snd_ctl_elem_value_get_iec958(spdif_output, &iec958);	
	if (!is_active(togglebutton))
		return;
	if (iec958.status[0] & IEC958_AES0_PROFESSIONAL)
		return;
	if (!strcmp(str, "1st")) {
		iec958.status[0] |= IEC958_AES1_CON_ORIGINAL;
	} else if (!strcmp(str, "Original")) {
		iec958.status[1] &= ~IEC958_AES1_CON_ORIGINAL;
	}
	snd_ctl_elem_value_set_iec958(spdif_output, &iec958);
	spdif_output_write();
}

void consumer_emphasis_toggled(GtkWidget *togglebutton, gpointer data)
{
	char *str = (char *)data;
	snd_aes_iec958_t iec958;

	snd_ctl_elem_value_get_iec958(spdif_output, &iec958);	
	if (!is_active(togglebutton))
		return;
	if (iec958.status[0] & IEC958_AES0_PROFESSIONAL)
		return;
	iec958.status[0] &= ~IEC958_AES0_CON_EMPHASIS;
	if (!strcmp(str, "No")) {
		iec958.status[0] |= IEC958_AES0_CON_EMPHASIS_NONE;
	} else if (!strcmp(str, "5015")) {
		iec958.status[1] |= ~IEC958_AES0_CON_EMPHASIS_5015;
	}
	snd_ctl_elem_value_set_iec958(spdif_output, &iec958);
	spdif_output_write();
}

void consumer_category_toggled(GtkWidget *togglebutton, gpointer data)
{
	char *str = (char *)data;
	snd_aes_iec958_t iec958;

	snd_ctl_elem_value_get_iec958(spdif_output, &iec958);	
	if (!is_active(togglebutton))
		return;
	if (iec958.status[0] & IEC958_AES0_PROFESSIONAL)
		return;
	iec958.status[0] &= ~IEC958_AES1_CON_CATEGORY;
	if (!strcmp(str, "DAT")) {
		iec958.status[0] |= IEC958_AES1_CON_DAT;
	} else if (!strcmp(str, "PCM")) {
		iec958.status[0] |= IEC958_AES1_CON_PCM_CODER;
	} else if (!strcmp(str, "CD")) {
		iec958.status[0] |= IEC958_AES1_CON_IEC908_CD;
	} else if (!strcmp(str, "General")) {
		iec958.status[0] |= IEC958_AES1_CON_GENERAL;
	}
	snd_ctl_elem_value_set_iec958(spdif_output, &iec958);
	spdif_output_write();
}

void spdif_output_toggled(GtkWidget *togglebutton, gpointer data)
{
	char *str = (char *)data;
	snd_aes_iec958_t iec958;
	int page;

	if (is_active(togglebutton)) {
		snd_ctl_elem_value_get_iec958(spdif_output, &iec958);
		if (!strcmp(str, "Professional")) {
			if (!(iec958.status[0] & IEC958_AES0_PROFESSIONAL)) {
				/* default setup: audio, no emphasis */
				memset(&iec958, 0, sizeof(iec958));
				iec958.status[0] = IEC958_AES0_PROFESSIONAL | IEC958_AES0_PRO_EMPHASIS_NONE | IEC958_AES0_PRO_FS_48000;
				iec958.status[1] = IEC958_AES1_PRO_MODE_STEREOPHONIC;
				snd_ctl_elem_value_set_iec958(spdif_output, &iec958);
			}
			page = 0;
		} else {
			if (iec958.status[0] & IEC958_AES0_PROFESSIONAL) {
				/* default setup: no emphasis, PCM encoder */
				memset(&iec958, 0, sizeof(iec958));
				iec958.status[0] = IEC958_AES0_CON_EMPHASIS_NONE;
				iec958.status[1] = IEC958_AES1_CON_PCM_CODER | IEC958_AES1_CON_ORIGINAL;
				iec958.status[3] = IEC958_AES3_CON_FS_48000;
				snd_ctl_elem_value_set_iec958(spdif_output, &iec958);
			}
			page = 1;
		}
		spdif_output_write();
		gtk_notebook_set_current_page(GTK_NOTEBOOK(hw_spdif_output_notebook), page);
		spdif_output_update();
	}
}

void spdif_input_update(void)
{
	int err;
	int digoptical = FALSE;
	int diginternal = FALSE;

	if ((card_eeprom.subvendor != ICE1712_SUBDEVICE_DELTADIO2496) &&
	    ! card_is_dmx6fire)
		return;
	if ((err = snd_ctl_elem_read(ctl, spdif_input)) < 0)
		g_print("Unable to read S/PDIF input switch: %s\n", snd_strerror(err));
	if (snd_ctl_elem_value_get_boolean(spdif_input, 0))
		digoptical = TRUE;
	if (card_is_dmx6fire) {
        	if ((err = snd_ctl_elem_read(ctl, spdif_on_off)) < 0)
			g_print("Unable to read S/PDIF on/off switch: %s\n", snd_strerror(err));
	      	if (!(snd_ctl_elem_value_get_boolean(spdif_on_off, 0)))
			diginternal = TRUE;
	}
	if (digoptical) {
		toggle_set(hw_spdif_input_optical_radio, TRUE);
	} else {
		toggle_set(hw_spdif_input_coaxial_radio, TRUE);
	}
	if (diginternal)
		toggle_set(hw_spdif_switch_off_radio, TRUE);
 }

void spdif_input_toggled(GtkWidget *togglebutton, gpointer data)
{
	int err;
	char *str = (char *)data;
	
	if (!is_active(togglebutton))
		return;
	if (!strcmp(str, "Off"))
               	snd_ctl_elem_value_set_boolean(spdif_on_off, 0, 0);
	else {
		snd_ctl_elem_value_set_boolean(spdif_on_off, 0, 1);
		if (!strcmp(str, "Optical"))
			snd_ctl_elem_value_set_boolean(spdif_input, 0, 1);
		else
			if (!strcmp(str, "Coaxial"))
				snd_ctl_elem_value_set_boolean(spdif_input, 0, 0);
	}
	if ((err = snd_ctl_elem_write(ctl, spdif_on_off)) < 0)
               g_print("Unable to write S/PDIF on/off switch: %s\n", snd_strerror(err));
	if ((err = snd_ctl_elem_write(ctl, spdif_input)) < 0)
		g_print("Unable to write S/PDIF input switch: %s\n", snd_strerror(err));
}

void analog_input_select_update(void)
{
	int err, input_interface;

	if (! card_is_dmx6fire)
		return;
	if ((err = snd_ctl_elem_read(ctl, analog_input_select)) < 0)
		g_print("Unable to read analog input switch: %s\n", snd_strerror(err));
	input_interface = snd_ctl_elem_value_get_enumerated(analog_input_select, 0);
	switch (input_interface) {
	case 0: toggle_set(input_interface_internal, TRUE); break;
	case 1: toggle_set(input_interface_front_input, TRUE); break;
	case 2: toggle_set(input_interface_rear_input, TRUE); break;
	case 3: toggle_set(input_interface_wavetable, TRUE); break;
	default:
		g_print("Error in analogue input: %d\n", input_interface);
		break;
	}
}

void analog_input_select_set(int value)
{
	int err;

        snd_ctl_elem_value_set_enumerated(analog_input_select, 0, value);
        if ((err = snd_ctl_elem_write(ctl, analog_input_select)) < 0)
                g_print("Unable to write analog input selection: %s\n", snd_strerror(err));
}

void analog_input_select_toggled(GtkWidget *togglebutton, gpointer data)
{
	char *what = (char *) data;
       int err;

        if (!is_active(togglebutton))
                return;
        if (!strcmp(what, "Internal")) {
                analog_input_select_set(0);
               snd_ctl_elem_value_set_boolean(breakbox_led, 0, 0);
        } else if (!strcmp(what, "Front Input")) {
                analog_input_select_set(1);
               snd_ctl_elem_value_set_boolean(breakbox_led, 0, 1);
        } else if (!strcmp(what, "Rear Input")) {
                analog_input_select_set(2);
               snd_ctl_elem_value_set_boolean(breakbox_led, 0, 0);
        } else if (!strcmp(what, "Wave Table")) {
                analog_input_select_set(3);
               snd_ctl_elem_value_set_boolean(breakbox_led, 0, 0);
        } else {
                g_print("analog_input_select_toggled: %s ???\n", what);
        }
       if ((err = snd_ctl_elem_write(ctl, breakbox_led)) < 0)
               g_print("Unable to write breakbox LED switch: %s\n", snd_strerror(err));
}

void phono_input_update(void)
{
        int err;

        if (! card_is_dmx6fire)
                return;
        if ((err = snd_ctl_elem_read(ctl, phono_input)) < 0)
                g_print("Unable to read phono input switch: %s\n", snd_strerror(err));
        if (snd_ctl_elem_value_get_boolean(phono_input, 0)) {
                toggle_set(hw_phono_input_on_radio, TRUE);
        } else {
                toggle_set(hw_phono_input_off_radio, TRUE);
        }
}

void phono_input_toggled(GtkWidget *togglebutton, gpointer data)
{
        int err;
        char *str = (char *) data;

        if (!is_active(togglebutton))
                return;
        if (!strcmp(str, "Phono"))
                snd_ctl_elem_value_set_boolean(phono_input, 0, 1);
        else
                snd_ctl_elem_value_set_boolean(phono_input, 0, 0);
        if ((err = snd_ctl_elem_write(ctl, phono_input)) < 0)
                g_print("Unable to write phono input switch: %s\n", snd_strerror(err));
}

void hardware_init(void)
{
	if (snd_ctl_elem_value_malloc(&internal_clock) < 0 ||
	    snd_ctl_elem_value_malloc(&internal_clock_default) < 0 ||
	    snd_ctl_elem_value_malloc(&word_clock_sync) < 0 ||
	    snd_ctl_elem_value_malloc(&rate_locking) < 0 ||
	    snd_ctl_elem_value_malloc(&rate_reset) < 0 ||
	    snd_ctl_elem_value_malloc(&volume_rate) < 0 ||
	    snd_ctl_elem_value_malloc(&spdif_input) < 0 ||
	    snd_ctl_elem_value_malloc(&spdif_output) < 0 ||
	    snd_ctl_elem_value_malloc(&analog_input_select) < 0 ||
	    snd_ctl_elem_value_malloc(&breakbox_led) < 0 ||
	    snd_ctl_elem_value_malloc(&spdif_on_off) < 0 ||
	    snd_ctl_elem_value_malloc(&phono_input) < 0  ||
	    snd_ctl_elem_value_malloc(&iec958_in_status) < 0) { /* NPM: add feature to display "Delta IEC958 Input Status" */
		g_print("Cannot allocate memory\n");
		exit(1);
	}

	snd_ctl_elem_value_set_interface(internal_clock, SND_CTL_ELEM_IFACE_MIXER);
	snd_ctl_elem_value_set_name(internal_clock, "Multi Track Internal Clock");

	snd_ctl_elem_value_set_interface(internal_clock_default, SND_CTL_ELEM_IFACE_MIXER);
	snd_ctl_elem_value_set_name(internal_clock_default, "Multi Track Internal Clock Default");

	snd_ctl_elem_value_set_interface(word_clock_sync, SND_CTL_ELEM_IFACE_MIXER);
	snd_ctl_elem_value_set_name(word_clock_sync, "Word Clock Sync");

	snd_ctl_elem_value_set_interface(rate_locking, SND_CTL_ELEM_IFACE_MIXER);
	snd_ctl_elem_value_set_name(rate_locking, "Multi Track Rate Locking");

	snd_ctl_elem_value_set_interface(rate_reset, SND_CTL_ELEM_IFACE_MIXER);
	snd_ctl_elem_value_set_name(rate_reset, "Multi Track Rate Reset");

	snd_ctl_elem_value_set_interface(volume_rate, SND_CTL_ELEM_IFACE_MIXER);
	snd_ctl_elem_value_set_name(volume_rate, "Multi Track Volume Rate");

	if (card_is_dmx6fire) {
		snd_ctl_elem_value_set_interface(spdif_input, SND_CTL_ELEM_IFACE_MIXER);
		snd_ctl_elem_value_set_name(spdif_input, "Optical Digital Input Switch");
	} else {
		snd_ctl_elem_value_set_interface(spdif_input, SND_CTL_ELEM_IFACE_MIXER);
		snd_ctl_elem_value_set_name(spdif_input, "IEC958 Input Optical");
	}

	snd_ctl_elem_value_set_interface(spdif_output, SND_CTL_ELEM_IFACE_PCM);
	snd_ctl_elem_value_set_name(spdif_output, "IEC958 Playback Default");

	snd_ctl_elem_value_set_interface(analog_input_select, SND_CTL_ELEM_IFACE_MIXER);
	snd_ctl_elem_value_set_name(analog_input_select, "Analog Input Select");

	snd_ctl_elem_value_set_interface(breakbox_led, SND_CTL_ELEM_IFACE_MIXER);
	snd_ctl_elem_value_set_name(breakbox_led, "Breakbox LED");

	snd_ctl_elem_value_set_interface(spdif_on_off, SND_CTL_ELEM_IFACE_MIXER);
	snd_ctl_elem_value_set_name(spdif_on_off, "Front Digital Input Switch");

	snd_ctl_elem_value_set_interface(phono_input, SND_CTL_ELEM_IFACE_MIXER);
	snd_ctl_elem_value_set_name(phono_input, "Phono Analog Input Switch");

	snd_ctl_elem_value_set_interface(iec958_in_status, SND_CTL_ELEM_IFACE_MIXER); /* NPM: add feature to display "Delta IEC958 Input Status" */
	snd_ctl_elem_value_set_name(iec958_in_status, "Delta IEC958 Input Status"); /* NPM: add feature to display "Delta IEC958 Input Status" */
}

void hardware_postinit(void)
{
	master_clock_update();
	rate_locking_update();
	rate_reset_update();
	volume_change_rate_update();
	spdif_input_update();
	spdif_output_update();
	analog_input_select_update();
	phono_input_update();
	if (card_has_delta_iec958_input_status)
	  iec958_input_status_timeout_callback(NULL); /* NPM */
}