aboutsummaryrefslogtreecommitdiff
path: root/src/ui/skin.cpp
blob: da91458e4f347d59196f1f64a64827d3ea4fc377 (plain) (blame)
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
/***************************************************************************
 *   Copyright (C) 2007-2009 by Ilya Kotov                                 *
 *   forkotov02@hotmail.ru                                                 *
 *                                                                         *
 *   Based on Promoe, an XMMS2 Client                                      *
 *   Copyright (C) 2005-2006 by XMMS2 Team                                 *
 *                                                                         *
 *   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 <QCoreApplication>
#include <QDir>
#include <QSettings>
#include <QPainter>
#include <QPolygon>
#include <QImage>
#include <QBuffer>
#include <QAction>
#include <qmmp/qmmp.h>
#include "actionmanager.h"
#include "skin.h"
#include "cursorimage.h"

Skin *Skin::m_instance = 0;

Skin *Skin::instance()
{
    if (!m_instance)
        m_instance = new Skin();
    return m_instance;
}

QPixmap Skin::getPixmap (const QString& name, QDir dir)
{
    dir.setFilter (QDir::Files | QDir::Hidden | QDir::NoSymLinks);
    QFileInfoList f = dir.entryInfoList();
    for (int j = 0; j < f.size(); ++j)
    {
        QFileInfo fileInfo = f.at (j);
        QString fn = fileInfo.fileName().toLower();
        if (fn.section (".",0,0) == name)
        {
            return QPixmap (fileInfo.filePath());
        }
    }
    return QPixmap();
}

Skin::Skin (QObject *parent) : QObject (parent)
{
    m_instance = this;
    QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
    QString path = settings.value("skin_path","").toString();
    if (path.isEmpty() || !QDir(path).exists ())
        path = ":/default";
    m_double_size = settings.value("General/double_size", false).toBool();
    ACTION(ActionManager::WM_DOUBLE_SIZE)->setChecked(m_double_size);
    setSkin (QDir::cleanPath(path));
    /* skin directory */
    QDir skinDir(QDir::homePath()+"/.qmmp");
    skinDir.mkdir ("skins");
}

Skin::~Skin()
{}

void Skin::setSkin (const QString& path)
{
    QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
    m_use_cursors = settings.value("General/skin_cursors", false).toBool();
    m_double_size = ACTION(ActionManager::WM_DOUBLE_SIZE)->isChecked();
    settings.setValue("skin_path",path);
    qDebug ("Skin: using %s",qPrintable(path));
    m_skin_dir = QDir (path);
    //clear old values
    m_pledit_txt.clear();
    buttons.clear();
    titlebar.clear();
    m_numbers.clear();
    m_pl_parts.clear();
    m_eq_parts.clear();
    m_eq_bar.clear();
    m_eq_spline.clear();
    m_vis_colors.clear();
    cursors.clear();
    //load skin parts
    loadPLEdit();
    loadMain();
    loadButtons();
    loadShufRep();
    loadTitleBar();
    loadPosBar();
    loadNumbers();
    loadPlayList();
    loadEq_ex();
    loadEqMain();
    loadVisColor();
    loadLetters();
    loadMonoSter();
    loadVolume();
    loadBalance();
    loadRegion();
    loadCursors();
    if(m_double_size)
    {
        uint key;
        foreach(key, buttons.keys())
            buttons[key] = scalePixmap(buttons[key]);
        foreach(key, titlebar.keys())
            titlebar[key] = scalePixmap(titlebar[key]);
        foreach(key, m_pl_parts.keys())
            m_pl_parts[key] = scalePixmap(m_pl_parts[key]);
        foreach(key, m_eq_parts.keys())
            m_eq_parts[key] = scalePixmap(m_eq_parts[key]);
        foreach(key, m_ms_parts.keys())
            m_ms_parts[key] = scalePixmap(m_ms_parts[key]);
        foreach(key, m_parts.keys())
            m_parts[key] = scalePixmap(m_parts[key]);
        foreach(QChar c, m_letters.keys())
            m_letters[c] = scalePixmap(m_letters[c]);
        m_main = scalePixmap(m_main);
        posbar = scalePixmap(posbar);
        int i;
        for(i = 0; i < m_numbers.size(); ++i)
            m_numbers[i] = scalePixmap(m_numbers[i]);

        for(i = 0; i < m_eq_bar.size(); ++i)
            m_eq_bar[i] = scalePixmap(m_eq_bar[i]);

        for(i = 0; i < m_eq_spline.size(); ++i)
            m_eq_spline[i] = scalePixmap(m_eq_spline[i]);

        for(i = 0; i < m_volume.size(); ++i)
            m_volume[i] = scalePixmap(m_volume[i]);
        for(i = 0; i < m_balance.size(); ++i)
            m_balance[i] = scalePixmap(m_balance[i]);
    }
    emit skinChanged();
}

void Skin::reloadSkin()
{
    setSkin (m_skin_dir.absolutePath ());
}

void Skin::loadMain()
{
    QPixmap *pixmap = getPixmap ("main");
    if (!pixmap)
        pixmap = getDummyPixmap("main");

    m_main = pixmap->copy (0,0,275,116);
    delete pixmap;
}

void Skin::loadCursors()
{
    if(!m_use_cursors)
    {
        for(int i = CUR_NORMAL; i <= CUR_WSWINBUT; ++i)
            cursors[i] = QCursor(Qt::ArrowCursor);
        cursors[CUR_PSIZE] = QCursor(Qt::SizeFDiagCursor);
        return;
    }
    cursors[CUR_NORMAL] = createCursor(getPath("normal"));
    cursors[CUR_CLOSE] = createCursor(getPath("close"));
    cursors[CUR_MAINMENU] = createCursor(getPath("mainmenu"));
    cursors[CUR_MIN] = createCursor(getPath("min"));
    cursors[CUR_POSBAR] = createCursor(getPath("posbar.cur"));
    cursors[CUR_SONGNAME] = createCursor(getPath("songname"));
    cursors[CUR_TITLEBAR] = createCursor(getPath("titlebar.cur"));
    cursors[CUR_VOLBAL] = createCursor(getPath("volbal"));
    cursors[CUR_WINBUT] = createCursor(getPath("winbut"));

    cursors[CUR_WSNORMAL] = createCursor(getPath("wsnormal"));
    cursors[CUR_WSPOSBAR] = createCursor(getPath("wsposbar"));

    cursors[CUR_EQCLOSE] = createCursor(getPath("eqclose"));
    cursors[CUR_EQNORMAL] = createCursor(getPath("eqnormal"));
    cursors[CUR_EQSLID] = createCursor(getPath("eqslid"));
    cursors[CUR_EQTITLE] = createCursor(getPath("eqtitle"));

    cursors[CUR_PCLOSE] = createCursor(getPath("pclose"));
    cursors[CUR_PNORMAL] = createCursor(getPath("pnormal"));
    cursors[CUR_PSIZE] = createCursor(getPath("psize"));
    if(cursors[CUR_PSIZE].shape() == Qt::ArrowCursor)
        cursors[CUR_PSIZE] = QCursor(Qt::SizeFDiagCursor);
    cursors[CUR_PTBAR] = createCursor(getPath("ptbar"));
    cursors[CUR_PVSCROLL] = createCursor(getPath("pvscroll"));
    cursors[CUR_PWINBUT] = createCursor(getPath("pwinbut"));

    cursors[CUR_PWSNORM] = createCursor(getPath("pwsnorm"));
    cursors[CUR_PWSSIZE] = createCursor(getPath("pwssize"));

    cursors[CUR_VOLBAR] = createCursor(getPath("volbar"));
    cursors[CUR_WSCLOSE] = createCursor(getPath("wsclose"));
    cursors[CUR_WSMIN] = createCursor(getPath("wsmin"));
    cursors[CUR_WSWINBUT] = createCursor(getPath("wswinbut"));
}

void Skin::loadButtons()
{

    QPixmap *pixmap = getPixmap ("cbuttons");

    if (!pixmap)
        pixmap = getDummyPixmap("cbuttons");

    buttons[BT_PREVIOUS_N] = pixmap->copy (0, 0,23,18);
    buttons[BT_PREVIOUS_P] = pixmap->copy (0,18,23,18);

    buttons[BT_PLAY_N] = pixmap->copy (23, 0,23,18);
    buttons[BT_PLAY_P] = pixmap->copy (23,18,23,18);

    buttons[BT_PAUSE_N] = pixmap->copy (46, 0,23,18);
    buttons[BT_PAUSE_P] = pixmap->copy (46,18,23,18);

    buttons[BT_STOP_N] = pixmap->copy (69, 0,23,18);
    buttons[BT_STOP_P] = pixmap->copy (69,18,23,18);

    buttons[BT_NEXT_N] = pixmap->copy (92, 0,22,18);
    buttons[BT_NEXT_P] = pixmap->copy (92,18,22,18);

    buttons[BT_EJECT_N] = pixmap->copy (114, 0,22,16);
    buttons[BT_EJECT_P] = pixmap->copy (114,16,22,16);
    delete pixmap;
}

void Skin::loadTitleBar()
{
    QPixmap *pixmap = getPixmap ("titlebar");

    if (!pixmap)
        pixmap = getDummyPixmap("titlebar");

    buttons[BT_MENU_N] = pixmap->copy (0,0,9,9);
    buttons[BT_MENU_P] = pixmap->copy (0,9,9,9);
    buttons[BT_MINIMIZE_N] = pixmap->copy (9,0,9,9);
    buttons[BT_MINIMIZE_P] = pixmap->copy (9,9,9,9);
    buttons[BT_CLOSE_N] = pixmap->copy (18,0,9,9);
    buttons[BT_CLOSE_P] = pixmap->copy (18,9,9,9);
    buttons[BT_SHADE1_N] = pixmap->copy (0,18,9,9);
    buttons[BT_SHADE1_P] = pixmap->copy (9,18,9,9);
    buttons[BT_SHADE2_N] = pixmap->copy (0,27,9,9);
    buttons[BT_SHADE2_P] = pixmap->copy (9,27,9,9);
    titlebar[TITLEBAR_A] = pixmap->copy (27, 0,275,14);
    titlebar[TITLEBAR_I] = pixmap->copy (27,15,275,14);
    titlebar[TITLEBAR_SHADED_A] = pixmap->copy (27,29,275,14);
    titlebar[TITLEBAR_SHADED_I] = pixmap->copy (27,42,275,14);
    delete pixmap;
}

void Skin::loadPosBar()
{
    QPixmap *pixmap = getPixmap ("posbar");

    if (!pixmap)
        pixmap = getDummyPixmap("posbar");

    if (pixmap->width() > 249)
    {
        buttons[BT_POSBAR_N] = pixmap->copy (248,0,29, pixmap->height());
        buttons[BT_POSBAR_P] = pixmap->copy (278,0,29, pixmap->height());
    }
    else
    {
        QPixmap dummy(29, pixmap->height());
        dummy.fill(Qt::transparent);
        buttons[BT_POSBAR_N] = dummy;
        buttons[BT_POSBAR_P] = dummy;
    }
    posbar = pixmap->copy (0,0,248,pixmap->height());
    delete pixmap;
}

void Skin::loadNumbers()
{
    QPixmap *pixmap = getPixmap ("nums_ex");
    if (!pixmap)
        pixmap = getPixmap ("numbers");
    if (!pixmap)
        pixmap = getDummyPixmap("numbers");

    for (uint i = 0; i < 10; i++)
        m_numbers << pixmap->copy (i*9, 0, 9, pixmap->height());

    if (pixmap->width() > 107)
        m_numbers << pixmap->copy(99, 0, 9, pixmap->height());
    else
    {
        // We didn't find "-" symbol. So we have to extract it from "2".
        // Winamp uses this method too.
        QPixmap pix;
        if(pixmap->width() > 98)
            pix = pixmap->copy(90,0,9,pixmap->height());
        else
        {
            pix = QPixmap(9, pixmap->height());
            pix.fill(Qt::transparent);
        }
        QPixmap minus = pixmap->copy(18,pixmap->height()/2,9,1);
        QPainter paint(&pix);
        paint.drawPixmap(0,pixmap->height()/2, minus);
        m_numbers << pix;
    }
    delete pixmap;
}

void Skin::loadPlayList()
{
    QPixmap *pixmap = getPixmap ("pledit");

    if (!pixmap)
        pixmap = getDummyPixmap("pledit");

    m_pl_parts[PL_CORNER_UL_A] = pixmap->copy (0,0,25,20);
    m_pl_parts[PL_CORNER_UL_I] = pixmap->copy (0,21,25,20);

    m_pl_parts[PL_CORNER_UR_A] = pixmap->copy (153,0,25,20);
    m_pl_parts[PL_CORNER_UR_I] = pixmap->copy (153,21,25,20);

    m_pl_parts[PL_TITLEBAR_A] = pixmap->copy (26,0,100,20);
    m_pl_parts[PL_TITLEBAR_I] = pixmap->copy (26,21,100,20);

    m_pl_parts[PL_TFILL1_A] = pixmap->copy (127,0,25,20);
    m_pl_parts[PL_TFILL1_I] = pixmap->copy (127,21,25,20);

    //m_pl_parts[PL_TFILL2_A] = pixmap->copy();//FIXME: �����
    //m_pl_parts[PL_TFILL2_I] = pixmap->copy();

    m_pl_parts[PL_LFILL] = pixmap->copy (0,42,12,29);
    m_pl_parts[PL_RFILL] = pixmap->copy (31,42,20,29); //???

    m_pl_parts[PL_LSBAR] = pixmap->copy (0,72,125,38);
    m_pl_parts[PL_RSBAR] = pixmap->copy (126,72,150,38);
    m_pl_parts[PL_SFILL1] = pixmap->copy (179,0,25,38);
    m_pl_parts[PL_SFILL2] = pixmap->copy (250,21,75,38);
    m_pl_parts[PL_TITLEBAR_SHADED1_A] = pixmap->copy (99,42,50,14);
    m_pl_parts[PL_TITLEBAR_SHADED1_I] = pixmap->copy (99,57,50,14);
    m_pl_parts[PL_TITLEBAR_SHADED2] = pixmap->copy (72,42,25,14);
    m_pl_parts[PL_TFILL_SHADED] = pixmap->copy (72,57,25,14);

    m_pl_parts[PL_CONTROL] = pixmap->copy(129,94,60,8);

    buttons[PL_BT_ADD] = pixmap->copy (11,80,25,18);
    buttons[PL_BT_SUB] = pixmap->copy (40,80,25,18);
    buttons[PL_BT_SEL] = pixmap->copy (70,80,25,18);
    buttons[PL_BT_SORT] = pixmap->copy (99,80,25,18);
    buttons[PL_BT_LST] = pixmap->copy(229, 80, 25, 18);
    buttons[PL_BT_SCROLL_N] = pixmap->copy (52,53,8,18);
    buttons[PL_BT_SCROLL_P] = pixmap->copy (61,53,8,18);

    buttons[PL_BT_CLOSE_N] = pixmap->copy (167,3,9,9);
    buttons[PL_BT_CLOSE_P] = pixmap->copy (52,42,9,9);
    buttons[PL_BT_SHADE1_N] = pixmap->copy (158,3,9,9);
    buttons[PL_BT_SHADE1_P] = pixmap->copy (62,42,9,9);
    buttons[PL_BT_SHADE2_N] = pixmap->copy (129,45,9,9);
    buttons[PL_BT_SHADE2_P] = pixmap->copy (150,42,9,9);

}

QPixmap *Skin::getPixmap (const QString& name)
{
    m_skin_dir.setFilter (QDir::Files | QDir::Hidden | QDir::NoSymLinks);
    QFileInfoList f = m_skin_dir.entryInfoList();
    for (int j = 0; j < f.size(); ++j)
    {
        QFileInfo fileInfo = f.at (j);
        QString fn = fileInfo.fileName().toLower();
        if (fn.section (".",0,0) == name)
        {
            return new QPixmap (fileInfo.filePath());
        }
    }
    return 0;
}

QString Skin::getPath (const QString& name)
{
    m_skin_dir.setFilter (QDir::Files | QDir::Hidden | QDir::NoSymLinks);
    QFileInfoList f = m_skin_dir.entryInfoList();
    bool nameHasExt = name.contains('.');
    for (int j = 0; j < f.size(); ++j)
    {
        QFileInfo fileInfo = f.at (j);
        QString fn = fileInfo.fileName().toLower();
        if (!nameHasExt && fn.section (".",0,0) == name)
        {
            return fileInfo.filePath();
        } else if (nameHasExt && fn == name)
        {
            return fileInfo.filePath();
        }
    }
    return QString();
}


void Skin::loadPLEdit()
{
    QString path = findFile("pledit.txt", m_skin_dir);
    if (path.isEmpty())
        path = findFile("pledit.txt", ":/default");
    if (path.isEmpty())
        qFatal("Skin: invalid default skin");

    QFile file(path);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        qFatal("Skin: unable to open %s", qPrintable(path));

    while (!file.atEnd ())
    {
        QByteArray line = file.readLine ();
        QList<QByteArray> l = line.split ('=');
        if (l.count () == 2)
        {
            m_pledit_txt[l[0].toLower () ] = l[1].trimmed();
        }
        else if (line.length() == 0)
        {
            break;
        }
    }
    if (!m_pledit_txt.keys().contains("mbbg"))
        m_pledit_txt["mbbg"] = m_pledit_txt["normalbg"];
    if (!m_pledit_txt.keys().contains("mbfg"))
        m_pledit_txt["mbfg"] = m_pledit_txt["normal"];
}

void Skin::loadEqMain()
{
    QPixmap *pixmap = getPixmap ("eqmain");

    if (!pixmap)
        pixmap = getDummyPixmap("eqmain");

    m_eq_parts[ EQ_MAIN ] = pixmap->copy (0,0,275,116);
    m_eq_parts[ EQ_TITLEBAR_A ] = pixmap->copy (0,134,275,14);
    m_eq_parts[ EQ_TITLEBAR_I ] = pixmap->copy (0,149,275,14);

    if (pixmap->height() > 295)
        m_eq_parts[ EQ_GRAPH ] = pixmap->copy (0,294,113,19);
    else
        m_eq_parts[ EQ_GRAPH ] = QPixmap();

    for (int i = 0; i < 14; ++i)
    {
        m_eq_bar << pixmap->copy (13 + i*15,164,14,63);
    }
    for (int i = 0; i < 14; ++i)
    {
        m_eq_bar << pixmap->copy (13 + i*15,229,14,63);
    }
    buttons[ EQ_BT_BAR_N ] = pixmap->copy (0,164,11,11);
    buttons[ EQ_BT_BAR_P ] = pixmap->copy (0,164+12,11,11);

    buttons[ EQ_BT_ON_N ] = pixmap->copy (69,119,28,12);
    buttons[ EQ_BT_ON_P ] = pixmap->copy (128,119,28,12);
    buttons[ EQ_BT_OFF_N ] = pixmap->copy (10, 119,28,12);
    buttons[ EQ_BT_OFF_P ] = pixmap->copy (187,119,28,12);

    buttons[ EQ_BT_PRESETS_N ] = pixmap->copy (224,164,44,12);
    buttons[ EQ_BT_PRESETS_P ] = pixmap->copy (224,176,44,12);

    buttons[ EQ_BT_AUTO_1_N ] = pixmap->copy (94,119,33,12);
    buttons[ EQ_BT_AUTO_1_P ] = pixmap->copy (153,119,33,12);
    buttons[ EQ_BT_AUTO_0_N ] = pixmap->copy (35, 119,33,12);
    buttons[ EQ_BT_AUTO_0_P ] = pixmap->copy (212,119,33,12);

    buttons[ EQ_BT_CLOSE_N ] = pixmap->copy (0,116,9,9);
    buttons[ EQ_BT_CLOSE_P ] = pixmap->copy (0,125,9,9);
    buttons[ EQ_BT_SHADE1_N ] = pixmap->copy (254,137,9,9);

    for (int i = 0; i < 19; ++i)
    {
        m_eq_spline << pixmap->copy (115, 294+i, 1, 1);
    }
    delete pixmap;
}

void Skin::loadEq_ex()
{
    QPixmap *pixmap = getPixmap ("eq_ex");

    if (!pixmap)
        pixmap = getDummyPixmap("eq_ex");

    buttons[ EQ_BT_SHADE1_P ] = pixmap->copy (1,38,9,9);
    buttons[ EQ_BT_SHADE2_N ] = pixmap->copy (254,3,9,9);
    buttons[ EQ_BT_SHADE2_P ] = pixmap->copy (1,47,9,9);
    m_eq_parts[ EQ_TITLEBAR_SHADED_A ] = pixmap->copy(0,0,275,14);
    m_eq_parts[ EQ_TITLEBAR_SHADED_I ] = pixmap->copy(0,15,275,14);
    m_eq_parts[ EQ_VOLUME1 ] = pixmap->copy(1,30,3,8);
    m_eq_parts[ EQ_VOLUME2 ] = pixmap->copy(4,30,3,8);
    m_eq_parts[ EQ_VOLUME3 ] = pixmap->copy(7,30,3,8);
    m_eq_parts[ EQ_BALANCE1 ] = pixmap->copy(11,30,3,8);
    m_eq_parts[ EQ_BALANCE2 ] = pixmap->copy(14,30,3,8);
    m_eq_parts[ EQ_BALANCE3 ] = pixmap->copy(17,30,3,8);

    delete pixmap;
}

void Skin::loadVisColor()
{
    QString path = findFile("viscolor.txt", m_skin_dir);
    if (path.isEmpty())
        path = findFile("viscolor.txt", ":/default");
    if (path.isEmpty())
        qFatal("Skin: invalid default skin");

    QFile file(path);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        qFatal("Skin: unable to open %s", qPrintable(path));

    int j = 0;
    while (!file.atEnd () && j<24)
    {
        j++;
        QByteArray line = file.readLine ();
        QString tmp = QString::fromAscii (line);
        tmp = tmp.trimmed ();
        int i = tmp.indexOf ("//");
        if (i>0)
            tmp.truncate (tmp.indexOf ("//"));
        QStringList list = tmp.split (",");
        if (list.count () >= 3)
        {
            //colors
            int r = list.at (0).toInt();
            int g = list.at (1).toInt();
            int b = list.at (2).toInt();
            m_vis_colors << QColor (r,g,b);
        }
        else if (line.length() == 0)
        {
            break;
        }
    }
    if (m_vis_colors.size() < 24)
    {
        qWarning ("Skin: cannot parse viscolor.txt");
        while (m_vis_colors.size() < 24)
            m_vis_colors << QColor (0,0,0);
    }
}

void Skin::loadShufRep()
{
    QPixmap *pixmap = getPixmap ("shufrep");

    if (!pixmap)
        pixmap = getDummyPixmap("shufrep");

    buttons[ BT_EQ_ON_N ] = pixmap->copy (0,73,23,12);
    buttons[ BT_EQ_ON_P ] = pixmap->copy (46,73,23,12);
    buttons[ BT_EQ_OFF_N ] = pixmap->copy (0,61,23,12);
    buttons[ BT_EQ_OFF_P ] = pixmap->copy (46,61,23,12);

    buttons[ BT_PL_ON_N ] = pixmap->copy (23,73,23,12);
    buttons[ BT_PL_ON_P ] = pixmap->copy (69,73,23,12);
    buttons[ BT_PL_OFF_N ] = pixmap->copy (23,61,23,12);
    buttons[ BT_PL_OFF_P ] = pixmap->copy (69,61,23,12);

    //buttons[ BT_PL_CLOSE_N ] = pixmap->copy ();
    //buttons[ BT_PL_CLOSE_P ] = pixmap->copy ();

    buttons[REPEAT_ON_N] = pixmap->copy (0,30, 28, 15);
    buttons[REPEAT_ON_P] = pixmap->copy (0,45, 28, 15);

    buttons[REPEAT_OFF_N] = pixmap->copy (0, 0,28,15);
    buttons[REPEAT_OFF_P] = pixmap->copy (0,15,28,15);

    buttons[SHUFFLE_ON_N] = pixmap->copy (28,30,46,15);
    buttons[SHUFFLE_ON_P] = pixmap->copy (28,45,46,15);

    buttons[SHUFFLE_OFF_N] = pixmap->copy (28, 0,46,15);
    buttons[SHUFFLE_OFF_P] = pixmap->copy (28,15,46,15);

    delete pixmap;
}

void Skin::loadLetters(void)
{
    QPixmap *img = getPixmap("text");

    if (!img)
        img = getDummyPixmap("text");

    QList<QList<QPixmap> > (letters);
    for (int i = 0; i < 3; i++)
    {
        QList<QPixmap> (l);
        for (int j = 0; j < 31; j++)
        {
            l.append (img->copy (j*5, i*6, 5, 6));
        }
        letters.append (l);
    }

    delete img;


    /* alphabet */
    for (uint i = 97; i < 123; i++)
    {
        m_letters.insert(i, letters[0][i-97]);
    }

    /* digits */
    for (uint i = 0; i <= 9; i++)
    {
        m_letters.insert (i+48, letters[1][i]);
    }

    /* special characters */
    m_letters.insert('"',  letters[0][27]);
    m_letters.insert('@',  letters[0][28]);
    m_letters.insert(':',  letters[1][12]);
    m_letters.insert('(',  letters[1][13]);
    m_letters.insert(')',  letters[1][14]);
    m_letters.insert('-',  letters[1][15]);
    m_letters.insert('\'', letters[1][16]);
    m_letters.insert('`',  letters[1][16]);
    m_letters.insert('!',  letters[1][17]);
    m_letters.insert('_',  letters[1][18]);
    m_letters.insert('+',  letters[1][19]);
    m_letters.insert('\\', letters[1][20]);
    m_letters.insert('/',  letters[1][21]);
    m_letters.insert('[',  letters[1][22]);
    m_letters.insert(']',  letters[1][23]);
    m_letters.insert('^',  letters[1][24]);
    m_letters.insert('&',  letters[1][25]);
    m_letters.insert('%',  letters[1][26]);
    m_letters.insert('.',  letters[1][27]);
    m_letters.insert(',',  letters[1][27]);
    m_letters.insert('=',  letters[1][28]);
    m_letters.insert('$',  letters[1][29]);
    m_letters.insert('#',  letters[1][30]);

    m_letters.insert(229, letters[2][0]);
    m_letters.insert(246, letters[2][1]);
    m_letters.insert(228, letters[2][2]);
    m_letters.insert('?', letters[2][3]);
    m_letters.insert('*', letters[2][4]);
    m_letters.insert(' ', letters[2][5]);

    /* text background */
    //m_items->insert (TEXTBG, letters[2][6]);
}

void Skin::loadMonoSter()
{
    QPixmap *pixmap = getPixmap("monoster");

    if (!pixmap)
        pixmap = getDummyPixmap("monoster");

    m_ms_parts.clear();
    m_ms_parts[ MONO_A ] = pixmap->copy (29,0,27,12);
    m_ms_parts[ MONO_I ] = pixmap->copy (29,12,27,12);
    m_ms_parts[ STEREO_A ] = pixmap->copy (0,0,27,12);
    m_ms_parts[ STEREO_I ] = pixmap->copy (0,12,27,12);

    delete pixmap;

    m_parts.clear();
    QPainter paint;
    pixmap = getPixmap("playpaus");

    if (!pixmap)
        pixmap = getDummyPixmap("playpaus");

    QPixmap part(11, 9);
    paint.begin(&part);
    paint.drawPixmap (0, 0, 3, 9, *pixmap, 36, 0, 3, 9);
    paint.drawPixmap (3, 0, 8, 9, *pixmap,  1, 0, 8, 9);
    paint.end();
    m_parts [PLAY] = part.copy();

    part = QPixmap(11, 9);
    paint.begin(&part);
    paint.drawPixmap (0, 0, 2, 9, *pixmap, 27, 0, 2, 9);
    paint.drawPixmap (2, 0, 9, 9, *pixmap,  9, 0, 9, 9);
    paint.end();
    m_parts [PAUSE] = part.copy();

    part = QPixmap(11, 9);
    paint.begin(&part);
    paint.drawPixmap (0, 0, 2, 9, *pixmap, 27, 0, 2, 9);
    paint.drawPixmap (2, 0, 9, 9, *pixmap, 18, 0, 9, 9);
    paint.end();
    m_parts [STOP]  = part.copy();

    delete pixmap;
}

void Skin::loadVolume()
{
    QPixmap *pixmap = getPixmap("volume");

    if (!pixmap)
        pixmap = getDummyPixmap("volume");

    m_volume.clear();
    for (int i = 0; i < 28; ++i)
        m_volume.append(pixmap->copy (0,i*15, pixmap->width(),13));
    if (pixmap->height() > 425)
    {
        buttons [BT_VOL_N] = pixmap->copy (15,422,14, pixmap->height() - 422);
        buttons [BT_VOL_P] = pixmap->copy (0, 422,14, pixmap->height() - 422);
    }
    else
    {
        buttons [BT_VOL_N] = QPixmap();
        buttons [BT_VOL_P] = QPixmap();
    }
    delete pixmap;
}

void Skin::loadBalance()
{
    QPixmap *pixmap = getPixmap ("balance");
    if (!pixmap)
        pixmap = getPixmap ("volume");

    if (!pixmap)
        pixmap = getDummyPixmap("balance");

    m_balance.clear();
    for (int i = 0; i < 28; ++i)
        m_balance.append(pixmap->copy (9,i*15,38,13));
    if (pixmap->height() > 427)
    {
        buttons [BT_BAL_N] = pixmap->copy (15, 422,14,pixmap->height()-422);
        buttons [BT_BAL_P] = pixmap->copy (0,422,14,pixmap->height()-422);
    }
    else
    {
        buttons [BT_BAL_N] = QPixmap();
        buttons [BT_BAL_P] = QPixmap();
    }
    delete pixmap;
}

void Skin::loadRegion()
{
    m_regions.clear();
    QString path = findFile("region.txt", m_skin_dir);

    if (path.isNull ())
    {
        qDebug ("Skin: cannot find region.txt. Transparency disabled");
        return;
    }
    m_regions[NORMAL] = createRegion(path, "Normal");
    m_regions[EQUALIZER] = createRegion(path, "Equalizer");
    m_regions[WINDOW_SHADE] = createRegion(path, "WindowShade");
    m_regions[EQUALIZER_WS] = createRegion(path, "EqualizerWS");
}

QRegion Skin::createRegion(const QString &path, const QString &key)
{
    QRegion region;
    QSettings settings(path, QSettings::IniFormat);
    QStringList numPoints = settings.value(key+"/NumPoints").toStringList();
    QStringList value = settings.value(key+"/PointList").toStringList();
    QStringList numbers;
    foreach(QString str, value)
    numbers << str.split(" ", QString::SkipEmptyParts);

    QList <QRegion> regions;

    QList<QString>::iterator n;
    n = numbers.begin();
    int r = m_double_size ? 2 : 1;
    for (int i = 0; i < numPoints.size(); ++i)
    {
        QList <int> lp;
        for (int j = 0; j < numPoints.at(i).toInt()*2; j++)
        {
            lp << n->toInt();
            n ++;
        }
        QVector<QPoint> points;

        for (int l = 0; l < lp.size(); l+=2)
        {
            points << QPoint(lp.at(l)*r, lp.at(l+1)*r);
        }
        region = region.united(QRegion(QPolygon(points)));
    }
    return region;
}

QPixmap * Skin::getDummyPixmap(const QString& name)
{
    QDir dir (":/default");
    dir.setFilter (QDir::Files | QDir::Hidden | QDir::NoSymLinks);
    QFileInfoList f = dir.entryInfoList();
    for (int j = 0; j < f.size(); ++j)
    {
        QFileInfo fileInfo = f.at (j);
        QString fn = fileInfo.fileName().toLower();
        if (fn.section (".",0,0) == name)
        {
            return new QPixmap (fileInfo.filePath());
        }
    }
    qFatal("Skin: default skin is corrupted");
    return 0;
}

QPixmap Skin::scalePixmap(const QPixmap &pix, int ratio)
{
    return pix.scaled(pix.width() * ratio, pix.height() * ratio,
                      Qt::KeepAspectRatio);
}

const QString Skin::findFile(const QString &name, QDir dir)
{
    dir.setFilter (QDir::Files | QDir::Hidden | QDir::NoSymLinks);
    QString path;
    QFileInfoList list = dir.entryInfoList();
    for (int i = 0; i < list.size(); ++i)
    {
        QFileInfo fileInfo = list.at (i);
        if (fileInfo.fileName().toLower() == name)
        {
            path = fileInfo.filePath ();
            break;
        }
    }
    return path;
}

const QString Skin::findFile(const QString &name, const QString &dir)
{
    return findFile(name, QDir(dir));
}
span class="hl opt">= nframes; return 0; } /****************************************************************** * JACK_srate */ int JACK_srate(nframes_t nframes, void *arg) { jack_driver_t *drv = (jack_driver_t *) arg; drv->jack_sample_rate = (long) nframes; if(drv->output_src) { soxr_delete(drv->output_src); drv->output_src = soxr_create(drv->client_sample_rate, drv->jack_sample_rate, drv->num_output_channels, 0, 0, 0, 0); } if(drv->input_src) { soxr_delete(drv->input_src); drv->input_src =soxr_create(drv->jack_sample_rate, drv->client_sample_rate, drv->num_input_channels, 0, 0, 0, 0); } TRACE("the sample rate is now %lu/sec\n", (long) nframes); return 0; } /****************************************************************** * JACK_shutdown * * if this is called then jack shut down... handle this appropriately */ void JACK_shutdown(void *arg) { jack_driver_t *drv = (jack_driver_t *) arg; TRACE("\n"); getDriver(drv->deviceID); drv->client = 0; /* reset client */ drv->jackd_died = TRUE; TRACE("jack shutdown, setting client to 0 and jackd_died to true, closing device\n"); #if JACK_CLOSE_HACK JACK_CloseDevice(drv, TRUE); #else JACK_CloseDevice(drv); #endif TRACE("trying to reconnect right now\n"); /* lets see if we can't reestablish the connection */ /*if(JACK_OpenDevice(drv) != ERR_SUCCESS) { ERR("unable to reconnect with jack\n"); }*/ releaseDriver(drv); } /****************************************************************** * JACK_Error * * Callback for jack errors */ static void JACK_Error(const char *desc) { ERR("%s\n", desc); } /****************************************************************** * JACK_OpenDevice * * RETURNS: ERR_SUCCESS upon success */ static int JACK_OpenDevice(jack_driver_t * drv) { const char **ports; char *our_client_name = 0; unsigned int i; int failed = 0; TRACE("creating jack client and setting up callbacks\n"); #if JACK_CLOSE_HACK /* see if this device is already open */ if(drv->client) { /* if this device is already in use then it is bad for us to be in here */ if(drv->in_use) return ERR_OPENING_JACK; TRACE("using existing client\n"); drv->in_use = TRUE; return ERR_SUCCESS; } #endif /* set up an error handler */ jack_set_error_function(JACK_Error); /* build the client name */ our_client_name = (char *) malloc(snprintf (our_client_name, 0, "%s_%d_%d%02d", client_name, getpid(), drv->deviceID, drv->clientCtr + 1) + 1); sprintf(our_client_name, "%s_%d_%d%02d", client_name, getpid(), drv->deviceID, drv->clientCtr++); /* try to become a client of the JACK server */ TRACE("client name '%s'\n", our_client_name); if((drv->client = jack_client_open(our_client_name, JackNullOption | JackNoStartServer, NULL)) == 0) { /* try once more */ TRACE("trying once more to jack_client_new"); if((drv->client = jack_client_open(our_client_name, JackNullOption | JackNoStartServer, NULL)) == 0) { ERR("jack server not running?\n"); free(our_client_name); return ERR_OPENING_JACK; } } free(our_client_name); TRACE("setting up jack callbacks\n"); /* JACK server to call `JACK_callback()' whenever there is work to be done. */ jack_set_process_callback(drv->client, JACK_callback, drv); /* setup a buffer size callback */ jack_set_buffer_size_callback(drv->client, JACK_bufsize, drv); /* tell the JACK server to call `srate()' whenever the sample rate of the system changes. */ jack_set_sample_rate_callback(drv->client, JACK_srate, drv); /* tell the JACK server to call `jack_shutdown()' if it ever shuts down, either entirely, or if it just decides to stop calling us. */ jack_on_shutdown(drv->client, JACK_shutdown, drv); /* display the current sample rate. once the client is activated (see below), you should rely on your own sample rate callback (see above) for this value. */ drv->jack_sample_rate = jack_get_sample_rate(drv->client); drv->output_sample_rate_ratio = (double) drv->jack_sample_rate / (double) drv->client_sample_rate; drv->input_sample_rate_ratio = (double) drv->client_sample_rate / (double) drv->jack_sample_rate; TRACE("client sample rate: %lu, jack sample rate: %lu, output ratio = %f, input ratio = %f\n", drv->client_sample_rate, drv->jack_sample_rate, drv->output_sample_rate_ratio, drv->input_sample_rate_ratio); drv->jack_buffer_size = jack_get_buffer_size(drv->client); /* create the output ports */ TRACE("creating output ports\n"); for(i = 0; i < drv->num_output_channels; i++) { char portname[32]; sprintf(portname, "out_%d", i); TRACE("port %d is named '%s'\n", i, portname); /* NOTE: Yes, this is supposed to be JackPortIsOutput since this is an output */ /* port FROM bio2jack */ drv->output_port[i] = jack_port_register(drv->client, portname, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0); } /* create the input ports */ TRACE("creating input ports\n"); for(i = 0; i < drv->num_input_channels; i++) { char portname[32]; sprintf(portname, "in_%d", i); TRACE("port %d is named '%s'\n", i, portname); /* NOTE: Yes, this is supposed to be JackPortIsInput since this is an input */ /* port TO bio2jack */ drv->input_port[i] = jack_port_register(drv->client, portname, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0); } #if JACK_CLOSE_HACK drv->in_use = TRUE; #endif /* tell the JACK server that we are ready to roll */ TRACE("calling jack_activate()\n"); if(jack_activate(drv->client)) { ERR("cannot activate client\n"); return ERR_OPENING_JACK; } /* if we have output channels and the port connection mode isn't CONNECT_NONE */ /* then we should connect up some ports */ if((drv->num_output_channels > 0) && (port_connection_mode != CONNECT_NONE)) { /* determine how we are to acquire output port names */ if((drv->jack_port_name_count == 0) || (drv->jack_port_name_count == 1)) { if(drv->jack_port_name_count == 0) { TRACE("jack_get_ports() passing in NULL/NULL\n"); ports = jack_get_ports(drv->client, NULL, JACK_DEFAULT_AUDIO_TYPE, drv->jack_output_port_flags); } else { TRACE("jack_get_ports() passing in port of '%s'\n", drv->jack_port_name[0]); ports = jack_get_ports(drv->client, drv->jack_port_name[0], NULL, drv->jack_output_port_flags); } /* display a trace of the output ports we found */ unsigned int num_ports = 0; if(ports) { for(i = 0; ports[i]; i++) { TRACE("ports[%d] = '%s'\n", i, ports[i]); num_ports++; } } /* ensure that we found enough ports */ if(!ports || (i < drv->num_output_channels)) { TRACE("ERR: jack_get_ports() failed to find ports with jack port flags of 0x%lX'\n", drv->jack_output_port_flags); #if JACK_CLOSE_HACK JACK_CloseDevice(drv, TRUE); #else JACK_CloseDevice(drv); #endif return ERR_PORT_NOT_FOUND; } /* connect a port for each output channel. Note: you can't do this before the client is activated (this may change in the future). */ for(i = 0; i < drv->num_output_channels; i++) { TRACE("jack_connect() to port %d('%p')\n", i, drv->output_port[i]); if(jack_connect(drv->client, jack_port_name(drv->output_port[i]), ports[i])) { ERR("cannot connect to output port %d('%s')\n", i, ports[i]); failed = 1; } } /* only if we are in CONNECT_ALL mode should we keep connecting ports up beyond */ /* the minimum number of ports required for each output channel coming into bio2jack */ if(port_connection_mode == CONNECT_ALL) { /* It's much cheaper and easier to let JACK do the processing required to connect 2 channels to 4 or 4 channels to 2 or any other combinations. This effectively eliminates the need for sample_move_d16_d16() */ if(drv->num_output_channels < num_ports) { for(i = drv->num_output_channels; ports[i]; i++) { int n = i % drv->num_output_channels; TRACE("jack_connect() to port %d('%p')\n", i, drv->output_port[n]); if(jack_connect(drv->client, jack_port_name(drv->output_port[n]), ports[i])) { // non fatal ERR("cannot connect to output port %d('%s')\n", n, ports[i]); } } } else if(drv->num_output_channels > num_ports) { for(i = num_ports; i < drv->num_output_channels; i++) { int n = i % num_ports; TRACE("jack_connect() to port %d('%p')\n", i, drv->output_port[n]); if(jack_connect(drv->client, jack_port_name(drv->output_port[i]), ports[n])) { // non fatal ERR("cannot connect to output port %d('%s')\n", i, ports[n]); } } } } free(ports); /* free the returned array of ports */ } else { for(i = 0; i < drv->jack_port_name_count; i++) { TRACE("jack_get_ports() portname %d of '%s\n", i, drv->jack_port_name[i]); ports = jack_get_ports(drv->client, drv->jack_port_name[i], NULL, drv->jack_output_port_flags); if(!ports) { ERR("jack_get_ports() failed to find ports with jack port flags of 0x%lX'\n", drv->jack_output_port_flags); return ERR_PORT_NOT_FOUND; } TRACE("ports[%d] = '%s'\n", 0, ports[0]); /* display a trace of the output port we found */ /* connect the port */ TRACE("jack_connect() to port %d('%p')\n", i, drv->output_port[i]); if(jack_connect(drv->client, jack_port_name(drv->output_port[i]), ports[0])) { ERR("cannot connect to output port %d('%s')\n", 0, ports[0]); failed = 1; } free(ports); /* free the returned array of ports */ } } } /* if( drv->num_output_channels > 0 ) */ if(drv->num_input_channels > 0) { /* determine how we are to acquire input port names */ if((drv->jack_port_name_count == 0) || (drv->jack_port_name_count == 1)) { if(drv->jack_port_name_count == 0) { TRACE("jack_get_ports() passing in NULL/NULL\n"); ports = jack_get_ports(drv->client, NULL, NULL, drv->jack_input_port_flags); } else { TRACE("jack_get_ports() passing in port of '%s'\n", drv->jack_port_name[0]); ports = jack_get_ports(drv->client, drv->jack_port_name[0], NULL, drv->jack_input_port_flags); } /* display a trace of the input ports we found */ unsigned int num_ports = 0; if(ports) { for(i = 0; ports[i]; i++) { TRACE("ports[%d] = '%s'\n", i, ports[i]); num_ports++; } } /* ensure that we found enough ports */ if(!ports || (i < drv->num_input_channels)) { TRACE("ERR: jack_get_ports() failed to find ports with jack port flags of 0x%lX'\n", drv->jack_input_port_flags); #if JACK_CLOSE_HACK JACK_CloseDevice(drv, TRUE); #else JACK_CloseDevice(drv); #endif return ERR_PORT_NOT_FOUND; } /* connect the ports. Note: you can't do this before the client is activated (this may change in the future). */ for(i = 0; i < drv->num_input_channels; i++) { TRACE("jack_connect() to port %d('%p')\n", i, drv->input_port[i]); if(jack_connect(drv->client, ports[i], jack_port_name(drv->input_port[i]))) { ERR("cannot connect to input port %d('%s')\n", i, ports[i]); failed = 1; } } /* It's much cheaper and easier to let JACK do the processing required to connect 2 channels to 4 or 4 channels to 2 or any other combinations. This effectively eliminates the need for sample_move_d16_d16() */ if(drv->num_input_channels < num_ports) { for(i = drv->num_input_channels; ports[i]; i++) { int n = i % drv->num_input_channels; TRACE("jack_connect() to port %d('%p')\n", i, drv->input_port[n]); if(jack_connect(drv->client, ports[i], jack_port_name(drv->input_port[n]))) { // non fatal ERR("cannot connect to input port %d('%s')\n", n, ports[i]); } } } else if(drv->num_input_channels > num_ports) { for(i = num_ports; i < drv->num_input_channels; i++) { int n = i % num_ports; TRACE("jack_connect() to port %d('%p')\n", i, drv->input_port[n]); if(jack_connect(drv->client, ports[n], jack_port_name(drv->input_port[i]))) { // non fatal ERR("cannot connect to input port %d('%s')\n", i, ports[n]); } } } free(ports); /* free the returned array of ports */ } else { for(i = 0; i < drv->jack_port_name_count; i++) { TRACE("jack_get_ports() portname %d of '%s\n", i, drv->jack_port_name[i]); ports = jack_get_ports(drv->client, drv->jack_port_name[i], NULL, drv->jack_input_port_flags); if(!ports) { ERR("jack_get_ports() failed to find ports with jack port flags of 0x%lX'\n", drv->jack_input_port_flags); return ERR_PORT_NOT_FOUND; } TRACE("ports[%d] = '%s'\n", 0, ports[0]); /* display a trace of the input port we found */ /* connect the port */ TRACE("jack_connect() to port %d('%p')\n", i, drv->input_port[i]); if(jack_connect(drv->client, jack_port_name(drv->input_port[i]), ports[0])) { ERR("cannot connect to input port %d('%s')\n", 0, ports[0]); failed = 1; } free(ports); /* free the returned array of ports */ } } } /* if( drv->num_input_channels > 0 ) */ /* if something failed we need to shut the client down and return 0 */ if(failed) { TRACE("failed, closing and returning error\n"); #if JACK_CLOSE_HACK JACK_CloseDevice(drv, TRUE); #else JACK_CloseDevice(drv); #endif return ERR_OPENING_JACK; } TRACE("success\n"); drv->jackd_died = FALSE; /* clear out this flag so we don't keep attempting to restart things */ drv->state = PLAYING; /* clients seem to behave much better with this on from the start, especially when recording */ return ERR_SUCCESS; /* return success */ } /****************************************************************** * JACK_CloseDevice * * Close the connection to the server cleanly. * If close_client is TRUE we close the client for this device instead of * just marking the device as in_use(JACK_CLOSE_HACK only) */ #if JACK_CLOSE_HACK static void JACK_CloseDevice(jack_driver_t * drv, bool close_client) #else static void JACK_CloseDevice(jack_driver_t * drv) #endif { unsigned int i; #if JACK_CLOSE_HACK if(close_client) { #endif TRACE("closing the jack client thread\n"); if(drv->client) { TRACE("after jack_deactivate()\n"); int errorCode = jack_client_close(drv->client); if(errorCode) ERR("jack_client_close() failed returning an error code of %d\n", errorCode); } /* reset client */ drv->client = 0; /* free up the port strings */ TRACE("freeing up %d port strings\n", drv->jack_port_name_count); if(drv->jack_port_name_count > 1) { for(i = 0; i < drv->jack_port_name_count; i++) free(drv->jack_port_name[i]); free(drv->jack_port_name); } JACK_CleanupDriver(drv); JACK_ResetFromDriver(drv); #if JACK_CLOSE_HACK } else { TRACE("setting in_use to FALSE\n"); drv->in_use = FALSE; if(!drv->client) { TRACE("critical error, closing a device that has no client\n"); } } #endif } /**************************************/ /* External interface functions below */ /**************************************/ /* Clear out any buffered data, stop playing, zero out some variables */ static void JACK_ResetFromDriver(jack_driver_t * drv) { TRACE("resetting drv->deviceID(%d)\n", drv->deviceID); /* NOTE: we use the RESET state so we don't need to worry about clearing out */ /* variables that the callback modifies while the callback is running */ /* we set the state to RESET and the callback clears the variables out for us */ drv->state = RESET; /* tell the callback that we are to reset, the callback will transition this to STOPPED */ } /* Clear out any buffered data, stop playing, zero out some variables */ void JACK_Reset(int deviceID) { jack_driver_t *drv = getDriver(deviceID); TRACE("resetting deviceID(%d)\n", deviceID); JACK_ResetFromDriver(drv); releaseDriver(drv); } /* * open the audio device for writing to * * deviceID is set to the opened device * if client is non-zero and in_use is FALSE then just set in_use to TRUE * * return value is zero upon success, non-zero upon failure * * if ERR_RATE_MISMATCH (*rate) will be updated with the jack servers rate */ int JACK_Open(int *deviceID, unsigned int bits_per_channel, unsigned long *rate, int channels) { /* we call through to JACK_OpenEx(), but default the input channels to 0 for better backwards compatibility with clients written before recording was available */ return JACK_OpenEx(deviceID, bits_per_channel, rate, 0, channels, NULL, 0, JackPortIsPhysical); } /* * see JACK_Open() for comments * NOTE: jack_port_name has three ways of being used: * - NULL - finds all ports with the given flags * - A single regex string used to retrieve all port names * - A series of port names, one for each output channel