summaryrefslogtreecommitdiffstats
path: root/kmix/mixer_oss4.cpp
blob: 8543915b9d28ede97ddafb4f38ef98151705b009 (plain)
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
/*
 *              KMix -- KDE's full featured mini mixer
 *
 *              Copyright (C) 1996-2000 Christian Esken
 *                        esken@kde.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

//OSS4 mixer backend for KMix by Yoper Team released under GPL v2 or later


#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <tqregexp.h>
#include <kdebug.h>

// Since we're guaranteed an OSS setup here, let's make life easier
#if !defined(__NetBSD__) && !defined(__OpenBSD__)
#include <sys/soundcard.h>
#else
#include <soundcard.h>
#endif

#include "mixer_oss4.h"
#include <klocale.h>

Mixer_Backend* OSS4_getMixer(int device)
{
	Mixer_Backend *l_mixer;
	l_mixer = new Mixer_OSS4(device);
	return l_mixer;
}

Mixer_OSS4::Mixer_OSS4(int device) : Mixer_Backend(device)
{
	if ( device == -1 ) m_devnum = 0;
	m_numExtensions = 0;
}

Mixer_OSS4::~Mixer_OSS4()
{
	close();
}

bool Mixer_OSS4::setRecsrcHW(int ctrlnum, bool on)
{
	return true;
}

//dummy implementation only
bool Mixer_OSS4::isRecsrcHW(int ctrlnum)
{
	return false;
}

//classifies mixexts according to their name, last classification wins
MixDevice::ChannelType Mixer_OSS4::classifyAndRename(TQString &name, int flags)
{
	MixDevice::ChannelType cType = MixDevice::UNKNOWN;
	TQStringList classes = TQStringList::split ( TQRegExp ( "[-,.]" ), name );


	if ( flags & MIXF_PCMVOL  ||
	     flags & MIXF_MONVOL  ||
	     flags & MIXF_MAINVOL )
	{
		cType = MixDevice::VOLUME;
	}

	for ( TQStringList::Iterator it = classes.begin(); it != classes.end(); ++it )
	{
		if ( *it == "line" )
		{
			*it = "Line";
			cType = MixDevice::EXTERNAL;

		} else
		if ( *it == "mic" )
		{
			*it = "Microphone";
			cType = MixDevice::MICROPHONE;
		} else
		if ( *it == "vol" )
		{
			*it = "Volume";
			cType = MixDevice::VOLUME;
		} else
		if ( *it == "surr" )
		{
			*it = "Surround";
			cType = MixDevice::SURROUND;
		} else
		if ( *it == "bass" )
		{
			*it = "Bass";
			cType = MixDevice::BASS;
		} else
		if ( *it == "treble" )
		{
			*it = "Treble";
			cType = MixDevice::TREBLE;
		} else
		if ( (*it).startsWith ( "pcm" ) )
		{
			(*it).tqreplace ( "pcm","PCM" );
			cType = MixDevice::AUDIO;
		} else
		if ( *it == "src" )
		{
			*it = "Source";
		} else
		if ( *it == "rec" )
		{
			*it = "Recording";
		} else
		if ( *it == "cd" )
		{
			*it = (*it).upper();
			cType = MixDevice::CD;
		}
		if ( (*it).startsWith("vmix") )
		{
			(*it).tqreplace("vmix","Virtual Mixer");
			cType = MixDevice::VOLUME;
		} else
		if ( (*it).endsWith("vol") )
		{
			TQChar &ref = (*it).ref(0);
			ref = ref.upper();
			cType = MixDevice::VOLUME;
		}
		else
		{
			TQChar &ref = (*it).ref(0);
			ref = ref.upper();
		}
	}
	name = classes.join( " ");
	return cType;
}

int Mixer_OSS4::open()
{
	if ( (m_fd= ::open("/dev/mixer", O_RDWR)) < 0 )
	{
		if ( errno == EACCES )
			return Mixer::ERR_PERM;
		else
			return Mixer::ERR_OPEN;
	}

	if (wrapIoctl( ioctl (m_fd, OSS_GETVERSION, &m_ossVersion) ) < 0)
	{
		return Mixer::ERR_READ;
	}
	if (m_ossVersion < 0x040000)
	{
		return Mixer::ERR_NOTSUPP;
	}


	wrapIoctl( ioctl (m_fd, SNDCTL_MIX_NRMIX, &m_numMixers) );

	if ( m_mixDevices.isEmpty() )
	{
		if ( m_devnum >= 0 && m_devnum < m_numMixers )
		{
			m_numExtensions = m_devnum;
			bool masterChosen = false;
			oss_mixext ext;
			ext.dev = m_devnum;

			if ( wrapIoctl( ioctl (m_fd, SNDCTL_MIX_NREXT, &m_numExtensions) ) < 0 )
			{
				//TODO: more specific error handling here
				if ( errno == ENODEV ) return Mixer::ERR_NODEV;
				return Mixer::ERR_READ;
			}

			if( m_numExtensions == 0 )
			{
				return Mixer::ERR_NODEV;
			}

			ext.ctrl = 0;

			//read MIXT_DEVROOT, return Mixer::NODEV on error
			if ( wrapIoctl ( ioctl( m_fd, SNDCTL_MIX_EXTINFO, &ext) ) < 0 )
			{
				return Mixer::ERR_NODEV;
			}

			oss_mixext_root *root = (oss_mixext_root *) ext.data;
			m_mixerName = root->name;

			for ( int i = 1; i < m_numExtensions; i++ )
			{
				bool isCapture = false;

				ext.dev = m_devnum;
				ext.ctrl = i;
	
				//wrapIoctl handles reinitialization, cancel loading on EIDRM
				if ( wrapIoctl( ioctl( m_fd, SNDCTL_MIX_EXTINFO, &ext) ) == EIDRM )
				{
					return 0;
				}

				TQString name = ext.extname;

				//skip vmix volume controls and mute controls
				if ( (name.tqfind("vmix") > -1 && name.tqfind( "pcm") > -1) ||
				     name.tqfind("mute") > -1
#ifdef MIXT_MUTE
				|| (ext.type == MIXT_MUTE)
#endif
				)
				{
					continue;
				}

				//fix for old legacy names, according to Hannu's suggestions
				if ( name.tqcontains('_') )
				{
					name = name.section('_',1,1).lower();
				}

				if ( ext.flags & MIXF_RECVOL || ext.flags & MIXF_MONVOL || name.tqfind(".in") > -1  )
				{
					isCapture = true;
				}

				Volume::ChannelMask chMask = Volume::MNONE;

				MixDevice::ChannelType cType = classifyAndRename(name, ext.flags);

				if ( ext.type == MIXT_STEREOSLIDER16 ||
				        ext.type == MIXT_STEREOSLIDER   ||
				        ext.type == MIXT_MONOSLIDER16   ||
				        ext.type == MIXT_MONOSLIDER     ||
				        ext.type == MIXT_SLIDER
				   )
				{
					if ( ext.type == MIXT_STEREOSLIDER16 ||
					        ext.type == MIXT_STEREOSLIDER
					   )
					{
						if ( isCapture )
						{
							chMask = Volume::ChannelMask(Volume::MLEFT|Volume::MRIGHT);
						}
						else
						{
							chMask = Volume::ChannelMask(Volume::MLEFT|Volume::MRIGHT );
						}
					}
					else
					{
						if ( isCapture )
						{
							chMask = Volume::MLEFT;
						}
						else
						{
							chMask = Volume::MLEFT;
						}
					}

					Volume vol (chMask, ext.maxvalue, ext.minvalue, isCapture);

					MixDevice* md =	new MixDevice(i, vol, isCapture, true,
						                      name, cType, MixDevice::SLIDER);
					
					m_mixDevices.append(md);
					
					if ( !masterChosen && ext.flags & MIXF_MAINVOL )
					{
						m_recommendedMaster = md;
						masterChosen = true;
					}
				}
				else if ( ext.type == MIXT_ONOFF )
				{
					Volume vol;
					vol.setMuted(true);
					MixDevice* md = new MixDevice(i, vol, false, true, name, MixDevice::VOLUME, MixDevice::SWITCH);
					m_mixDevices.append(md);
				}
				else if ( ext.type == MIXT_ENUM )
				{
					oss_mixer_enuminfo ei;
					ei.dev = m_devnum;
					ei.ctrl = i;

					if ( wrapIoctl( ioctl (m_fd, SNDCTL_MIX_ENUMINFO, &ei) ) != -1 )
					{
						Volume vol(Volume::MLEFT, ext.maxvalue, ext.minvalue, false);

						MixDevice* md = new MixDevice (i, vol, false, false,
						                               name, MixDevice::UNKNOWN,
						                               MixDevice::ENUM);

						TQPtrList<TQString> &enumValuesRef = md->enumValues();
						TQString thisElement;

						for ( int i = 0; i < ei.nvalues; i++ )
						{
							thisElement = &ei.strings[ ei.strindex[i] ];

							if ( thisElement.isEmpty() )
							{
								thisElement = TQString::number(i);
							}
							enumValuesRef.append( new TQString(thisElement) );
						}
						m_mixDevices.append(md);
					}
				}

			}
		}
		else
		{
			return -1;
		}
	}
	m_isOpen = true;
	return 0;
}

int Mixer_OSS4::close()
{
	m_isOpen = false;
	int l_i_ret = ::close(m_fd);
	m_mixDevices.clear();
	return l_i_ret;
}

TQString Mixer_OSS4::errorText(int mixer_error)
{
	TQString l_s_errmsg;

	switch( mixer_error )
	{
		case Mixer::ERR_PERM:
			l_s_errmsg = i18n("kmix: You do not have permission to access the mixer device.\n" \
			                   "Login as root and do a 'chmod a+rw /dev/mixer*' to allow the access.");
			break;
		case Mixer::ERR_OPEN:
			l_s_errmsg = i18n("kmix: Mixer cannot be found.\n" \
			                  "Please check that the soundcard is installed and the\n" \
			                  "soundcard driver is loaded.\n" \
			                  "On Linux you might need to use 'insmod' to load the driver.\n" \
			                  "Use 'soundon' when using OSS4 from 4front.");
			break;
		case Mixer::ERR_NOTSUPP:
			l_s_errmsg = i18n("kmix expected an OSSv4 mixer module,\n" \
			                   "but instead found an older version.");
			break;
		default:
			l_s_errmsg = Mixer_Backend::errorText(mixer_error);
	}
	return l_s_errmsg;
}

int Mixer_OSS4::readVolumeFromHW(int ctrlnum, Volume &vol)
{

	oss_mixext extinfo;
	oss_mixer_value mv;

	extinfo.dev = m_devnum;
	extinfo.ctrl = ctrlnum;

	if ( wrapIoctl( ioctl(m_fd, SNDCTL_MIX_EXTINFO, &extinfo) ) < 0 )
	{
		//TODO: more specific error handling
		return Mixer::ERR_READ;
	}

	mv.dev = extinfo.dev;
	mv.ctrl = extinfo.ctrl;
	mv.timestamp = extinfo.timestamp;

	if ( wrapIoctl ( ioctl (m_fd, SNDCTL_MIX_READ, &mv) ) < 0 )
	{
		/* Oops, can't read mixer */
		return Mixer::ERR_READ;
	}
	else
	{
		if ( vol.isMuted() && extinfo.type != MIXT_ONOFF )
		{
			return 0;
		}

		if ( vol.isCapture() )
		{
			switch ( extinfo.type )
			{
				case MIXT_ONOFF:
					vol.setMuted(mv.value != extinfo.maxvalue);
					break;

				case MIXT_MONOSLIDER:
					vol.setVolume(Volume::LEFT, mv.value & 0xff);
					break;

				case MIXT_STEREOSLIDER:
					vol.setVolume(Volume::LEFT, mv.value & 0xff);
					vol.setVolume(Volume::RIGHT, ( mv.value >> 8 ) & 0xff);
					break;

				case MIXT_SLIDER:
					vol.setVolume(Volume::LEFT, mv.value);
					break;

				case MIXT_MONOSLIDER16:
					vol.setVolume(Volume::LEFT, mv.value & 0xffff);
					break;

				case MIXT_STEREOSLIDER16:
					vol.setVolume(Volume::LEFT, mv.value & 0xffff);
					vol.setVolume(Volume::RIGHT, ( mv.value >> 16 ) & 0xffff);
					break;
			}
		}
		else
		{
			switch( extinfo.type )
			{
				case MIXT_ONOFF:
					vol.setMuted(mv.value != extinfo.maxvalue);
					break;
				case MIXT_MONOSLIDER:
					vol.setVolume(Volume::LEFT, mv.value & 0xff);
					break;

				case MIXT_STEREOSLIDER:
					vol.setVolume(Volume::LEFT, mv.value & 0xff);
					vol.setVolume(Volume::RIGHT, ( mv.value >> 8 ) & 0xff);
					break;

				case MIXT_SLIDER:
					vol.setVolume(Volume::LEFT, mv.value);
					break;

				case MIXT_MONOSLIDER16:
					vol.setVolume(Volume::LEFT, mv.value & 0xffff);
					break;

				case MIXT_STEREOSLIDER16:
					vol.setVolume(Volume::LEFT, mv.value & 0xffff);
					vol.setVolume(Volume::RIGHT, ( mv.value >> 16 ) & 0xffff);
					break;
			}
		}
	}
	return 0;
}

int Mixer_OSS4::writeVolumeToHW(int ctrlnum, Volume &vol)
{
	int volume = 0;

	oss_mixext extinfo;
	oss_mixer_value mv;

	extinfo.dev = m_devnum;
	extinfo.ctrl = ctrlnum;

	if ( wrapIoctl( ioctl(m_fd, SNDCTL_MIX_EXTINFO, &extinfo) ) < 0 )
	{
		//TODO: more specific error handling
		kdDebug ( 67100 ) << "failed to read info for control " << ctrlnum << endl;
		return Mixer::ERR_READ;
	}

	if ( vol.isMuted() && extinfo.type != MIXT_ONOFF )
	{
		volume = 0;
	}
	else
	{
		switch ( extinfo.type )
		{
			case MIXT_ONOFF:
				volume = (vol.isMuted()) ? (extinfo.minvalue) : (extinfo.maxvalue);
				break;
			case MIXT_MONOSLIDER:
				volume = vol[Volume::LEFT];
				break;

			case MIXT_STEREOSLIDER:
				volume = vol[Volume::LEFT] | ( vol[Volume::RIGHT] << 8 );
				break;

			case MIXT_SLIDER:
				volume = vol[Volume::LEFT];
				break;

			case MIXT_MONOSLIDER16:
				volume = vol[Volume::LEFT];
				break;

			case MIXT_STEREOSLIDER16:
				volume = vol[Volume::LEFT] | ( vol[Volume::RIGHT] << 16 );
				break;
			default:
				return -1;
		}
	}

	mv.dev = extinfo.dev;
	mv.ctrl = extinfo.ctrl;
	mv.timestamp = extinfo.timestamp;
	mv.value = volume;

	if ( wrapIoctl ( ioctl (m_fd, SNDCTL_MIX_WRITE, &mv) ) < 0 )
	{
		kdDebug ( 67100 ) << "error writing: " << endl;
		return Mixer::ERR_WRITE;
	}
	return 0;
}

void Mixer_OSS4::setEnumIdHW(int ctrlnum, unsigned int idx)
{
	oss_mixext extinfo;
	oss_mixer_value mv;

	extinfo.dev = m_devnum;
	extinfo.ctrl = ctrlnum;

	if ( wrapIoctl ( ioctl(m_fd, SNDCTL_MIX_EXTINFO, &extinfo) ) < 0 )
	{
		//TODO: more specific error handling
		kdDebug ( 67100 ) << "failed to read info for control " << ctrlnum << endl;
		return;
	}

	if ( extinfo.type != MIXT_ENUM )
	{
		return;
	}


	//according to oss docs maxVal < minVal could be true - strange...
	unsigned int maxVal = (unsigned int) extinfo.maxvalue;
	unsigned int minVal = (unsigned int) extinfo.minvalue;

	if ( maxVal < minVal )
	{
		int temp;
		temp = maxVal;
		maxVal = minVal;
		minVal = temp;
	}

	if ( idx > maxVal || idx < minVal )
		idx = minVal;

	mv.dev = extinfo.dev;
	mv.ctrl = extinfo.ctrl;
	mv.timestamp = extinfo.timestamp;
	mv.value = idx;

	if ( wrapIoctl ( ioctl (m_fd, SNDCTL_MIX_WRITE, &mv) ) < 0 )
	{
		/* Oops, can't write to mixer */
		kdDebug ( 67100 ) << "error writing: " << endl;
	}
}

unsigned int Mixer_OSS4::enumIdHW(int ctrlnum)
{
	oss_mixext extinfo;
	oss_mixer_value mv;

	extinfo.dev = m_devnum;
	extinfo.ctrl = ctrlnum;

	if ( wrapIoctl ( ioctl(m_fd, SNDCTL_MIX_EXTINFO, &extinfo) ) < 0 )
	{
		//TODO: more specific error handling
		//TODO: check whether those return values are actually possible
		return Mixer::ERR_READ;
	}

	if ( extinfo.type != MIXT_ENUM )
	{
		return Mixer::ERR_READ;
	}

	mv.dev = extinfo.dev;
	mv.ctrl = extinfo.ctrl;
	mv.timestamp = extinfo.timestamp;

	if ( wrapIoctl ( ioctl (m_fd, SNDCTL_MIX_READ, &mv) ) < 0 )
	{
		/* Oops, can't read mixer */
		return Mixer::ERR_READ;
	}
	return mv.value;
}

int Mixer_OSS4::wrapIoctl(int ioctlRet)
{
	switch( ioctlRet )
	{
		case EIO:
		{
			kdDebug ( 67100 ) << "A hardware level error occured" << endl;
			break;
		}
		case EINVAL:
		{
			kdDebug ( 67100 ) << "Operation caused an EINVAL. You may have found a bug in kmix OSS4 module or in OSS4 itself" << endl;
			break;
		}
		case ENXIO:
		{
			kdDebug ( 67100 ) << "Operation index out of bounds or requested device does not exist - you likely found a bug in the kmix OSS4 module" << endl;
			break;
		}
		case EPERM:
		case EACCES:
		{
			kdDebug ( 67100 ) << errorText ( Mixer::ERR_PERM ) << endl;
			break;
		}
		case ENODEV:
		{
			kdDebug ( 67100 ) << "kmix received an ENODEV errors - are the OSS4 drivers loaded?" << endl;
			break;
		}
		case EPIPE:
		case EIDRM:
		{
			reinitialize();
		}

	}
	return ioctlRet;
}


TQString OSS4_getDriverName()
{
	return "OSS4";
}