-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.c
1921 lines (1488 loc) · 44.7 KB
/
interface.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2018 Mark Hills <mark@xwax.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2, as published by the Free Software Foundation.
*
* 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 version 2 for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
#include <assert.h>
#include <errno.h>
#include <iconv.h>
#include <math.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <SDL.h>
#include <SDL_ttf.h>
#include "interface.h"
#include "layout.h"
#include "player.h"
#include "rig.h"
#include "selector.h"
#include "status.h"
#include "timecoder.h"
#include "xwax.h"
/* Screen refresh time in milliseconds */
#define REFRESH 10
/* Font definitions */
#define FONT "DejaVuSans.ttf"
#define FONT_SIZE 10
#define FONT_SPACE 15
#define EM_FONT "DejaVuSans-Oblique.ttf"
#define BIG_FONT "DejaVuSans-Bold.ttf"
#define BIG_FONT_SIZE 14
#define BIG_FONT_SPACE 19
#define CLOCK_FONT FONT
#define CLOCK_FONT_SIZE 32
#define DECI_FONT FONT
#define DECI_FONT_SIZE 20
#define DETAIL_FONT "DejaVuSansMono-Bold.ttf"
#define DETAIL_FONT_SIZE 9
#define DETAIL_FONT_SPACE 12
/* Screen size (pixels) */
#define DEFAULT_WIDTH 960
#define DEFAULT_HEIGHT 720
/* Relationship between pixels and screen units */
#define DEFAULT_SCALE 1.0
/* Dimensions in our own screen units */
#define BORDER 12
#define SPACER 8
#define HALF_SPACER 4
#define CURSOR_WIDTH 4
#define PLAYER_HEIGHT 213
#define OVERVIEW_HEIGHT 16
#define LIBRARY_MIN_WIDTH 64
#define LIBRARY_MIN_HEIGHT 64
#define DEFAULT_METER_SCALE 8
#define MAX_METER_SCALE 11
#define SEARCH_HEIGHT (FONT_SPACE)
#define STATUS_HEIGHT (DETAIL_FONT_SPACE)
#define BPM_WIDTH 32
#define SORT_WIDTH 21
#define RESULTS_ARTIST_WIDTH 200
#define TOKEN_SPACE 2
#define CLOCKS_WIDTH 160
#define SPINNER_SIZE (CLOCK_FONT_SIZE * 2 - 6)
#define SCOPE_SIZE (CLOCK_FONT_SIZE * 2 - 6)
#define SCROLLBAR_SIZE 10
#define METER_WARNING_TIME 20 /* time in seconds for "red waveform" warning */
/* Function key (F1-F12) definitions */
#define FUNC_LOAD 0
#define FUNC_RECUE 1
#define FUNC_TIMECODE 2
/* Types of SDL_USEREVENT */
#define EVENT_TICKER (SDL_USEREVENT)
#define EVENT_QUIT (SDL_USEREVENT + 1)
#define EVENT_STATUS (SDL_USEREVENT + 2)
#define EVENT_SELECTOR (SDL_USEREVENT + 3)
/* Macro functions */
#define MIN(x,y) ((x)<(y)?(x):(y))
#define SQ(x) ((x)*(x))
#define LOCK(sf) if (SDL_MUSTLOCK(sf)) SDL_LockSurface(sf)
#define UNLOCK(sf) if (SDL_MUSTLOCK(sf)) SDL_UnlockSurface(sf)
#define UPDATE(sf, rect) SDL_UpdateRect(sf, (rect)->x, (rect)->y, \
(rect)->w, (rect)->h)
/* List of directories to use as search path for fonts. */
static const char *font_dirs[] = {
"/usr/X11R6/lib/X11/fonts/TTF",
"/usr/share/fonts/truetype/ttf-dejavu/",
"/usr/share/fonts/ttf-dejavu",
"/usr/share/fonts/dejavu",
"/usr/share/fonts/TTF",
"/usr/share/fonts/truetype/dejavu",
"/usr/share/fonts/truetype/ttf-dejavu",
NULL
};
static TTF_Font *clock_font, *deci_font, *detail_font,
*font, *em_font, *big_font;
static SDL_Color background_col = {0, 0, 0, 255},
text_col = {224, 224, 224, 255},
alert_col = {192, 64, 0, 255},
ok_col = {32, 128, 3, 255},
elapsed_col = {0, 32, 255, 255},
cursor_col = {192, 0, 0, 255},
selected_col = {0, 48, 64, 255},
detail_col = {128, 128, 128, 255},
needle_col = {255, 255, 255, 255},
artist_col = {16, 64, 0, 255},
bpm_col = {64, 16, 0, 255};
static unsigned short *spinner_angle, spinner_size;
static int width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT,
meter_scale = DEFAULT_METER_SCALE;
static Uint32 video_flags = SDL_RESIZABLE;
static float scale = DEFAULT_SCALE;
static iconv_t utf;
static pthread_t ph;
static struct selector selector;
static struct observer on_status, on_selector;
/*
* Scale a dimension according to the current zoom level
*
* FIXME: This function is used where a rendering does not
* acknowledge the scale given in the local rectangle.
* These cases should be removed.
*/
static int zoom(int d)
{
return d * scale;
}
/*
* Convert the given time (in milliseconds) to displayable time
*/
static void time_to_clock(char *buf, char *deci, int t)
{
int minutes, seconds, frac;
bool neg;
if (t < 0) {
t = abs(t);
neg = true;
} else
neg = false;
minutes = (t / 60 / 1000) % (60*60);
seconds = (t / 1000) % 60;
frac = t % 1000;
if (neg)
*buf++ = '-';
sprintf(buf, "%02d:%02d.", minutes, seconds);
sprintf(deci, "%03d", frac);
}
/*
* Calculate a lookup which maps a position on screen to an angle,
* relative to the centre of the spinner
*/
static void calculate_angle_lut(unsigned short *lut, int size)
{
int r, c, nr, nc;
float theta, rat;
for (r = 0; r < size; r++) {
nr = r - size / 2;
for (c = 0; c < size; c++) {
nc = c - size / 2;
if (nr == 0)
theta = M_PI_2;
else if (nc == 0) {
theta = 0;
if (nr < 0)
theta = M_PI;
} else {
rat = (float)(nc) / -nr;
theta = atanf(rat);
if (rat < 0)
theta += M_PI;
}
if (nc <= 0)
theta += M_PI;
/* The angles stored in the lookup table range from 0 to
* 1023 (where 1024 is 360 degrees) */
lut[r * size + c]
= ((int)(theta * 1024 / (M_PI * 2)) + 1024) % 1024;
}
}
}
static int init_spinner(int size)
{
spinner_angle = malloc(size * size * (sizeof *spinner_angle));
if (spinner_angle == NULL) {
perror("malloc");
return -1;
}
calculate_angle_lut(spinner_angle, size);
spinner_size = size;
return 0;
}
static void clear_spinner(void)
{
free(spinner_angle);
}
/*
* Open a font, given the leafname
*
* This scans the available font directories for the file, to account
* for different software distributions.
*
* As this is an SDL (it is not an X11 app) we prefer to avoid the use
* of fontconfig to select fonts.
*/
static TTF_Font* open_font(const char *name, int size) {
int r, pt;
char buf[256];
const char **dir;
struct stat st;
TTF_Font *font;
pt = zoom(size);
dir = &font_dirs[0];
while (*dir) {
sprintf(buf, "%s/%s", *dir, name);
r = stat(buf, &st);
if (r != -1) { /* something exists at this path */
fprintf(stderr, "Loading font '%s', %dpt...\n", buf, pt);
font = TTF_OpenFont(buf, pt);
if (!font)
fprintf(stderr, "Font error: %s\n", TTF_GetError());
return font; /* or NULL */
}
if (errno != ENOENT) {
perror("stat");
return NULL;
}
dir++;
continue;
}
fprintf(stderr, "Font '%s' cannot be found in", name);
dir = &font_dirs[0];
while (*dir) {
fputc(' ', stderr);
fputs(*dir, stderr);
dir++;
}
fputc('.', stderr);
fputc('\n', stderr);
return NULL;
}
/*
* Load all fonts
*/
static int load_fonts(void)
{
clock_font = open_font(CLOCK_FONT, CLOCK_FONT_SIZE);
if (!clock_font)
return -1;
deci_font = open_font(DECI_FONT, DECI_FONT_SIZE);
if (!deci_font)
return -1;
font = open_font(FONT, FONT_SIZE);
if (!font)
return -1;
em_font = open_font(EM_FONT, FONT_SIZE);
if (!em_font)
return -1;
big_font = open_font(BIG_FONT, BIG_FONT_SIZE);
if (!big_font)
return -1;
detail_font = open_font(DETAIL_FONT, DETAIL_FONT_SIZE);
if (!detail_font)
return -1;
return 0;
}
/*
* Free resources associated with fonts
*/
static void clear_fonts(void)
{
TTF_CloseFont(clock_font);
TTF_CloseFont(deci_font);
TTF_CloseFont(font);
TTF_CloseFont(em_font);
TTF_CloseFont(big_font);
TTF_CloseFont(detail_font);
}
static Uint32 palette(SDL_Surface *sf, SDL_Color *col)
{
return SDL_MapRGB(sf->format, col->r, col->g, col->b);
}
/*
* Draw text
*
* Render the string "buf" text inside the given "rect". If "locale"
* is set then a conversion from the system locale is done.
*
* Return: width of text drawn
*/
static int do_draw_text(SDL_Surface *sf, const struct rect *rect,
const char *buf, TTF_Font *font,
SDL_Color fg, SDL_Color bg, bool locale)
{
SDL_Surface *rendered;
SDL_Rect dst, src, fill;
if (buf == NULL) {
src.w = 0;
src.h = 0;
} else if (buf[0] == '\0') { /* SDL_ttf fails for empty string */
src.w = 0;
src.h = 0;
} else {
if (!locale) {
rendered = TTF_RenderText_Shaded(font, buf, fg, bg);
} else {
char ubuf[256], /* fixed buffer is reasonable for rendering */
*in, *out;
size_t len, fill;
out = ubuf;
fill = sizeof(ubuf) - 1; /* always leave space for \0 */
if (iconv(utf, NULL, NULL, &out, &fill) == -1)
abort();
in = strdupa(buf);
len = strlen(in);
(void)iconv(utf, &in, &len, &out, &fill);
*out = '\0';
rendered = TTF_RenderUTF8_Shaded(font, ubuf, fg, bg);
}
src.x = 0;
src.y = 0;
src.w = MIN(rect->w, rendered->w);
src.h = MIN(rect->h, rendered->h);
dst.x = rect->x;
dst.y = rect->y;
SDL_BlitSurface(rendered, &src, sf, &dst);
SDL_FreeSurface(rendered);
}
/* Complete the remaining space with a blank rectangle */
if (src.w < rect->w) {
fill.x = rect->x + src.w;
fill.y = rect->y;
fill.w = rect->w - src.w;
fill.h = rect->h;
SDL_FillRect(sf, &fill, palette(sf, &bg));
}
if (src.h < rect->h) {
fill.x = rect->x;
fill.y = rect->y + src.h;
fill.w = src.w; /* the x-fill rectangle does the corner */
fill.h = rect->h - src.h;
SDL_FillRect(sf, &fill, palette(sf, &bg));
}
return src.w;
}
static int draw_text(SDL_Surface *sf, const struct rect *rect,
const char *buf, TTF_Font *font,
SDL_Color fg, SDL_Color bg)
{
return do_draw_text(sf, rect, buf, font, fg, bg, false);
}
static int draw_text_in_locale(SDL_Surface *sf, const struct rect *rect,
const char *buf, TTF_Font *font,
SDL_Color fg, SDL_Color bg)
{
return do_draw_text(sf, rect, buf, font, fg, bg, true);
}
/*
* Given a rectangle and font, calculate rendering bounds
* for another font so that the baseline matches.
*/
static void track_baseline(const struct rect *rect, const TTF_Font *a,
struct rect *aligned, const TTF_Font *b)
{
split(*rect, pixels(from_top(TTF_FontAscent(a) - TTF_FontAscent(b), 0)),
NULL, aligned);
}
/*
* Draw a coloured rectangle
*/
static void draw_rect(SDL_Surface *surface, const struct rect *rect,
SDL_Color col)
{
SDL_Rect b;
b.x = rect->x;
b.y = rect->y;
b.w = rect->w;
b.h = rect->h;
SDL_FillRect(surface, &b, palette(surface, &col));
}
/*
* Draw some text in a box
*/
static void draw_token(SDL_Surface *surface, const struct rect *rect,
const char *buf,
SDL_Color text_col, SDL_Color col, SDL_Color bg_col)
{
struct rect b;
draw_rect(surface, rect, bg_col);
b = shrink(*rect, TOKEN_SPACE);
draw_text(surface, &b, buf, detail_font, text_col, col);
}
/*
* Dim a colour for display
*/
static SDL_Color dim(const SDL_Color x, int n)
{
SDL_Color c;
c.r = x.r >> n;
c.g = x.g >> n;
c.b = x.b >> n;
return c;
}
/*
* Get a colour from RGB values
*/
static SDL_Color rgb(double r, double g, double b)
{
SDL_Color c;
c.r = r * 255;
c.g = g * 255;
c.b = b * 255;
return c;
}
/*
* Get a colour from HSV values
*
* Pre: h is in degrees, in the range 0.0 to 360.0
*/
static SDL_Color hsv(double h, double s, double v)
{
int i;
double f, p, q, t;
if (s == 0.0)
return rgb(v, v, v);
h /= 60;
i = floor(h);
f = h - i;
p = v * (1 - s);
q = v * (1 - s * f);
t = v * (1 - s * (1 - f));
switch (i) {
case 0:
return rgb(v, t, p);
case 1:
return rgb(q, v, p);
case 2:
return rgb(p, v, t);
case 3:
return rgb(p, q, v);
case 4:
return rgb(t, p, v);
case 5:
case 6:
return rgb(v, p, q);
default:
abort();
}
}
static bool show_bpm(double bpm)
{
return (bpm > 20.0 && bpm < 400.0);
}
/*
* Draw the beats-per-minute indicator
*/
static void draw_bpm(SDL_Surface *surface, const struct rect *rect, double bpm,
SDL_Color bg_col)
{
static const double min = 60.0, max = 240.0;
char buf[32];
double f, h;
sprintf(buf, "%5.1f", bpm);
/* Safety catch against bad BPM values, NaN, infinity etc. */
if (bpm < min || bpm > max) {
draw_token(surface, rect, buf, detail_col, bg_col, bg_col);
return;
}
/* Colour compatible BPMs the same; cycle 360 degrees
* every time the BPM doubles */
f = log2(bpm);
f -= floor(f);
h = f * 360.0; /* degrees */
draw_token(surface, rect, buf, text_col, hsv(h, 1.0, 0.3), bg_col);
}
/*
* Draw the BPM field, or a gap
*/
static void draw_bpm_field(SDL_Surface *surface, const struct rect *rect,
double bpm, SDL_Color bg_col)
{
if (show_bpm(bpm))
draw_bpm(surface, rect, bpm, bg_col);
else
draw_rect(surface, rect, bg_col);
}
/*
* Draw the record information in the deck
*/
static void draw_record(SDL_Surface *surface, const struct rect *rect,
const struct record *record)
{
struct rect artist, title, left, right;
split(*rect, from_top(BIG_FONT_SPACE, 0), &artist, &title);
draw_text_in_locale(surface, &artist, record->artist,
big_font, text_col, background_col);
/* Layout changes slightly if BPM is known */
if (show_bpm(record->bpm)) {
split(title, from_left(BPM_WIDTH, 0), &left, &right);
draw_bpm(surface, &left, record->bpm, background_col);
split(right, from_left(HALF_SPACER, 0), &left, &title);
draw_rect(surface, &left, background_col);
}
draw_text_in_locale(surface, &title, record->title,
font, text_col, background_col);
}
/*
* Draw a single time in milliseconds in hours:minutes.seconds format
*/
static void draw_clock(SDL_Surface *surface, const struct rect *rect, int t,
SDL_Color col)
{
char hms[8], deci[8];
short int v;
struct rect sr;
time_to_clock(hms, deci, t);
v = draw_text(surface, rect, hms, clock_font, col, background_col);
split(*rect, pixels(from_left(v, 0)), NULL, &sr);
track_baseline(&sr, clock_font, &sr, deci_font);
draw_text(surface, &sr, deci, deci_font, col, background_col);
}
/*
* Draw the visual monitor of the input audio to the timecoder
*/
static void draw_scope(SDL_Surface *surface, const struct rect *rect,
struct timecoder *tc)
{
int r, c, v, mid;
Uint8 *p;
mid = tc->mon_size / 2;
for (r = 0; r < tc->mon_size; r++) {
for (c = 0; c < tc->mon_size; c++) {
p = surface->pixels
+ (rect->y + r) * surface->pitch
+ (rect->x + c) * surface->format->BytesPerPixel;
v = tc->mon[r * tc->mon_size + c];
if ((r == mid || c == mid) && v < 64)
v = 64;
p[0] = v;
p[1] = p[0];
p[2] = p[1];
}
}
}
/*
* Draw the spinner
*
* The spinner shows the rotational position of the record, and
* matches the physical rotation of the vinyl record.
*/
static void draw_spinner(SDL_Surface *surface, const struct rect *rect,
struct player *pl)
{
int x, y, r, c, rangle, pangle;
double elapsed, remain, rps;
Uint8 *rp, *p;
SDL_Color col;
x = rect->x;
y = rect->y;
elapsed = player_get_elapsed(pl);
remain = player_get_remain(pl);
rps = timecoder_revs_per_sec(pl->timecoder);
rangle = (int)(player_get_position(pl) * 1024 * rps) % 1024;
if (elapsed < 0 || remain < 0)
col = alert_col;
else
col = ok_col;
for (r = 0; r < spinner_size; r++) {
/* Store a pointer to this row of the framebuffer */
rp = surface->pixels + (y + r) * surface->pitch;
for (c = 0; c < spinner_size; c++) {
/* Use the lookup table to provide the angle at each
* pixel */
pangle = spinner_angle[r * spinner_size + c];
/* Calculate the final pixel location and set it */
p = rp + (x + c) * surface->format->BytesPerPixel;
if ((rangle - pangle + 1024) % 1024 < 512) {
p[0] = col.b >> 2;
p[1] = col.g >> 2;
p[2] = col.r >> 2;
} else {
p[0] = col.b;
p[1] = col.g;
p[2] = col.r;
}
}
}
}
/*
* Draw the clocks which show time elapsed and time remaining
*/
static void draw_deck_clocks(SDL_Surface *surface, const struct rect *rect,
struct player *pl, struct track *track)
{
int elapse, remain;
struct rect upper, lower;
SDL_Color col;
split(*rect, from_top(CLOCK_FONT_SIZE, 0), &upper, &lower);
elapse = player_get_elapsed(pl) * 1000;
remain = player_get_remain(pl) * 1000;
if (elapse < 0)
col = alert_col;
else if (remain > 0)
col = ok_col;
else
col = text_col;
draw_clock(surface, &upper, elapse, col);
if (remain <= 0)
col = alert_col;
else
col = text_col;
if (track_is_importing(track))
col = dim(col, 2);
draw_clock(surface, &lower, -remain, col);
}
/*
* Draw the high-level overview meter which shows the whole length
* of the track
*/
static void draw_overview(SDL_Surface *surface, const struct rect *rect,
struct track *tr, int position)
{
int x, y, w, h, r, c, sp, fade, bytes_per_pixel, pitch, height,
current_position;
Uint8 *pixels, *p;
SDL_Color col;
x = rect->x;
y = rect->y;
w = rect->w;
h = rect->h;
pixels = surface->pixels;
bytes_per_pixel = surface->format->BytesPerPixel;
pitch = surface->pitch;
if (tr->length)
current_position = (long long)position * w / tr->length;
else
current_position = 0;
for (c = 0; c < w; c++) {
/* Collect the correct meter value for this column */
sp = (long long)tr->length * c / w;
if (sp < tr->length) /* account for rounding */
height = track_get_overview(tr, sp) * h / 256;
else
height = 0;
/* Choose a base colour to display in */
if (!tr->length) {
col = background_col;
fade = 0;
} else if (c == current_position) {
col = needle_col;
fade = 1;
} else if (position > tr->length - tr->rate * METER_WARNING_TIME) {
col = alert_col;
fade = 3;
} else {
col = elapsed_col;
fade = 3;
}
if (track_is_importing(tr))
col = dim(col, 1);
if (c < current_position)
col = dim(col, 1);
/* Store a pointer to this column of the framebuffer */
p = pixels + y * pitch + (x + c) * bytes_per_pixel;
r = h;
while (r > height) {
p[0] = col.b >> fade;
p[1] = col.g >> fade;
p[2] = col.r >> fade;
p += pitch;
r--;
}
while (r) {
p[0] = col.b;
p[1] = col.g;
p[2] = col.r;
p += pitch;
r--;
}
}
}
/*
* Draw the close-up meter, which can be zoomed to a level set by
* 'scale'
*/
static void draw_closeup(SDL_Surface *surface, const struct rect *rect,
struct track *tr, int position, int scale)
{
int x, y, w, h, c;
size_t bytes_per_pixel, pitch;
Uint8 *pixels;
x = rect->x;
y = rect->y;
w = rect->w;
h = rect->h;
pixels = surface->pixels;
bytes_per_pixel = surface->format->BytesPerPixel;
pitch = surface->pitch;
/* Draw in columns. This may seem like a performance hit,
* but oprofile shows it makes no difference */
for (c = 0; c < w; c++) {
int r, sp, height, fade;
Uint8 *p;
SDL_Color col;
/* Work out the meter height in pixels for this column */
sp = position - (position % (1 << scale))
+ ((c - w / 2) << scale);
if (sp < tr->length && sp > 0)
height = track_get_ppm(tr, sp) * h / 256;
else
height = 0;
/* Select the appropriate colour */
if (c == w / 2) {
col = needle_col;
fade = 1;
} else {
col = elapsed_col;
fade = 3;
}
/* Get a pointer to the top of the column, and increment
* it for each row */
p = pixels + y * pitch + (x + c) * bytes_per_pixel;
r = h;
while (r > height) {
p[0] = col.b >> fade;
p[1] = col.g >> fade;
p[2] = col.r >> fade;
p += pitch;
r--;
}
while (r) {
p[0] = col.b;
p[1] = col.g;
p[2] = col.r;
p += pitch;
r--;
}
}
}
/*
* Draw the audio meters for a deck
*/
static void draw_meters(SDL_Surface *surface, const struct rect *rect,
struct track *tr, int position, int scale)
{
struct rect overview, closeup;
split(*rect, from_top(OVERVIEW_HEIGHT, SPACER), &overview, &closeup);
if (closeup.h > OVERVIEW_HEIGHT)
draw_overview(surface, &overview, tr, position);
else
closeup = *rect;
draw_closeup(surface, &closeup, tr, position, scale);
}
/*
* Draw the current playback status -- clocks, spinner and scope
*/
static void draw_deck_top(SDL_Surface *surface, const struct rect *rect,
struct player *pl, struct track *track)
{
struct rect clocks, left, right, spinner, scope;