1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020
|
/*
* Copyright (C) 2024 Mark Hills <mark@xwax.org>
*
* This file is part of "xwax".
*
* "xwax" is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License, version 3 as
* published by the Free Software Foundation.
*
* "xwax" 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, see <https://www.gnu.org/licenses/>.
*
*/
#define _GNU_SOURCE /* strdupa() */
#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 "debug.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)
/* Types of redraw event */
#define REDRAW_BACKGROUND 0x1
#define REDRAW_DECKS 0x2
#define REDRAW_STATUS 0x4
#define REDRAW_LIBRARY 0x8
/* Macro functions */
#define MIN(x,y) ((x)<(y)?(x):(y))
#define LOCK(sf) if (SDL_MUSTLOCK(sf)) SDL_LockSurface(sf)
#define UNLOCK(sf) if (SDL_MUSTLOCK(sf)) SDL_UnlockSurface(sf)
/* 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 meter_scale = DEFAULT_METER_SCALE;
static float scale = DEFAULT_SCALE;
static iconv_t utf;
static pthread_t ph;
SDL_Window *window;
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[9], char deci[4], 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[9], deci[4];
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;
split(*rect, from_left(CLOCKS_WIDTH, SPACER), &clocks, &right);
/* If there is no timecoder to display information on, or not enough
* available space, just draw clocks which span the overall space */
if (!pl->timecode_control || right.w < 0) {
draw_deck_clocks(surface, rect, pl, track);
return;
}
draw_deck_clocks(surface, &clocks, pl, track);
split(right, from_right(SPINNER_SIZE, SPACER), &left, &spinner);
if (left.w < 0)
return;
split(spinner, from_bottom(SPINNER_SIZE, 0), NULL, &spinner);
draw_spinner(surface, &spinner, pl);
split(left, from_right(SCOPE_SIZE, SPACER), &clocks, &scope);
if (clocks.w < 0)
return;
split(scope, from_bottom(SCOPE_SIZE, 0), NULL, &scope);
draw_scope(surface, &scope, pl->timecoder);
}
/*
* Draw the textual description of playback status, which includes
* information on the timecode
*/
static void draw_deck_status(SDL_Surface *surface,
const struct rect *rect,
const struct deck *deck)
{
char buf[128], *c;
int tc;
const struct player *pl = &deck->player;
c = buf;
c += sprintf(c, "%s: ", pl->timecoder->def->name);
tc = timecoder_get_position(pl->timecoder, NULL);
if (pl->timecode_control && tc != -1) {
c += sprintf(c, "%7d ", tc);
} else {
c += sprintf(c, " ");
}
sprintf(c, "pitch:%+0.2f (sync %0.2f %+.5fs = %+0.2f) %s%s",
pl->pitch,
pl->sync_pitch,
pl->last_difference,
pl->pitch * pl->sync_pitch,
pl->recalibrate ? "RCAL " : "",
deck_is_locked(deck) ? "LOCK " : "");
draw_text(surface, rect, buf, detail_font, detail_col, background_col);
}
/*
* Draw a single deck
*/
static void draw_deck(SDL_Surface *surface, const struct rect *rect,
struct deck *deck, int meter_scale)
{
int position;
struct rect track, top, meters, status, rest, lower;
struct player *pl;
struct track *t;
pl = &deck->player;
t = pl->track;
position = player_get_elapsed(pl) * t->rate;
split(*rect, from_top(FONT_SPACE + BIG_FONT_SPACE, 0), &track, &rest);
if (rest.h < 160)
rest = *rect;
else
draw_record(surface, &track, deck->record);
split(rest, from_top(CLOCK_FONT_SIZE * 2, SPACER), &top, &lower);
if (lower.h < 64)
lower = rest;
else
draw_deck_top(surface, &top, pl, t);
split(lower, from_bottom(FONT_SPACE, SPACER), &meters, &status);
if (meters.h < 64)
meters = lower;
else
draw_deck_status(surface, &status, deck);
draw_meters(surface, &meters, t, position, meter_scale);
}
/*
* Draw all the decks in the system left to right
*/
static void draw_decks(SDL_Surface *surface, const struct rect *rect,
struct deck deck[], size_t ndecks, int meter_scale)
{
int d;
struct rect left, right;
right = *rect;
for (d = 0; d < ndecks; d++) {
split(right, columns(d, ndecks, BORDER), &left, &right);
draw_deck(surface, &left, &deck[d], meter_scale);
}
}
/*
* Draw the status bar
*/
static void draw_status(SDL_Surface *sf, const struct rect *rect)
{
SDL_Color fg, bg;
switch (status_level()) {
case STATUS_ALERT:
case STATUS_WARN:
fg = text_col;
bg = dim(alert_col, 2);
break;
default:
fg = detail_col;
bg = background_col;
}
draw_text_in_locale(sf, rect, status(), detail_font, fg, bg);
}
/*
* Draw the search field which the user types into
*/
static void draw_search(SDL_Surface *surface, const struct rect *rect,
struct selector *sel)
{
int s;
const char *buf;
char cm[32];
SDL_Rect cursor;
struct rect rtext;
split(*rect, from_left(SCROLLBAR_SIZE, SPACER), NULL, &rtext);
if (sel->search[0] != '\0')
buf = sel->search;
else
buf = NULL;
s = draw_text(surface, &rtext, buf, font, text_col, background_col);
cursor.x = rtext.x + s;
cursor.y = rtext.y;
cursor.w = CURSOR_WIDTH * rect->scale; /* FIXME: use proper UI funcs */
cursor.h = rtext.h;
SDL_FillRect(surface, &cursor, palette(surface, &cursor_col));
if (sel->view_index->entries > 1)
sprintf(cm, "%zd matches", sel->view_index->entries);
else if (sel->view_index->entries > 0)
sprintf(cm, "1 match");
else
sprintf(cm, "no matches");
rtext.x += s + CURSOR_WIDTH + SPACER;
rtext.w -= s + CURSOR_WIDTH + SPACER;
draw_text(surface, &rtext, cm, em_font, detail_col, background_col);
}
/*
* Draw a vertical scroll bar representing our view on a list of the
* given number of entries
*/
static void draw_scroll_bar(SDL_Surface *surface, const struct rect *rect,
const struct listbox *scroll)
{
SDL_Rect box;
SDL_Color bg;
bg = dim(selected_col, 1);
box.x = rect->x;
box.y = rect->y;
box.w = rect->w;
box.h = rect->h;
SDL_FillRect(surface, &box, palette(surface, &bg));
if (scroll->entries > 0) {
box.x = rect->x;
box.y = rect->y + rect->h * scroll->offset / scroll->entries;
box.w = rect->w;
box.h = rect->h * MIN(scroll->lines, scroll->entries) / scroll->entries;
SDL_FillRect(surface, &box, palette(surface, &selected_col));
}
}
/*
* A callback function for drawing a row. Included here for
* readability where it is used.
*/
typedef void (*draw_row_t)(const void *context,
SDL_Surface *surface, const struct rect rect,
unsigned int entry, bool selected);
/*
* Draw a listbox, using the given function to draw each row
*/
static void draw_listbox(const struct listbox *lb, SDL_Surface *surface,
const struct rect rect,
const void *context, draw_row_t draw)
{
struct rect left, remain;
unsigned int row;
split(rect, from_left(SCROLLBAR_SIZE, SPACER), &left, &remain);
draw_scroll_bar(surface, &left, lb);
row = 0;
for (row = 0;; row++) {
int entry;
bool selected;
struct rect line;
entry = listbox_map(lb, row);
if (entry == -1)
break;
if (entry == listbox_current(lb))
selected = true;
else
selected = false;
split(remain, from_top(FONT_SPACE, 0), &line, &remain);
draw(context, surface, line, entry, selected);
}
draw_rect(surface, &remain, background_col);
}
static void draw_crate_row(const void *context,
SDL_Surface *surface, const struct rect rect,
unsigned int entry, bool selected)
{
const struct selector *selector = context;
const struct crate *crate;
struct rect left, right;
SDL_Color col;
crate = selector->library->crate[entry];
if (crate->is_fixed)
col = detail_col;
else
col = text_col;
if (!selected) {
draw_text_in_locale(surface, &rect, crate->name,
font, col, background_col);
return;
}
split(rect, from_right(SORT_WIDTH, 0), &left, &right);
switch (selector->sort) {
case SORT_ARTIST:
draw_token(surface, &right, "ART", text_col, artist_col, selected_col);
break;
case SORT_BPM:
draw_token(surface, &right, "BPM", text_col, bpm_col, selected_col);
break;
case SORT_PLAYLIST:
draw_token(surface, &right, "PLS", text_col, selected_col, selected_col);
break;
default:
abort();
}
if (crate->is_busy) {
split(left, from_right(25, 0), &left, &right);
draw_token(surface, &right, "BUSY", text_col,
dim(alert_col, 2), selected_col);
}
draw_text_in_locale(surface, &left, crate->name, font, col, selected_col);
}
/*
* Draw a crate index, with scrollbar and current selection
*/
static void draw_crates(SDL_Surface *surface, const struct rect rect,
const struct selector *x)
{
draw_listbox(&x->crates, surface, rect, x, draw_crate_row);
}
static void draw_record_row(const void *context,
SDL_Surface *surface, const struct rect rect,
unsigned int entry, bool selected)
{
int width;
struct record *record;
const struct index *index = context;
struct rect left, right;
SDL_Color col;
if (selected)
col = selected_col;
else
col = background_col;
width = rect.w / 2;
if (width > RESULTS_ARTIST_WIDTH)
width = RESULTS_ARTIST_WIDTH;
record = index->record[entry];
split(rect, from_left(BPM_WIDTH, 0), &left, &right);
draw_bpm_field(surface, &left, record->bpm, col);
split(right, from_left(SPACER, 0), &left, &right);
draw_rect(surface, &left, col);
split(right, from_left(width, 0), &left, &right);
draw_text_in_locale(surface, &left, record->artist, font, text_col, col);
split(right, from_left(SPACER, 0), &left, &right);
draw_rect(surface, &left, col);
draw_text_in_locale(surface, &right, record->title, font, text_col, col);
}
/*
* Display a record library index, with scrollbar and current
* selection
*/
static void draw_index(SDL_Surface *surface, const struct rect rect,
const struct selector *x)
{
draw_listbox(&x->records, surface, rect, x->view_index, draw_record_row);
}
/*
* Display the music library, which consists of the query, and search
* results
*/
static void draw_library(SDL_Surface *surface, const struct rect *rect,
struct selector *sel)
{
struct rect rsearch, rlists, rcrates, rrecords;
unsigned int rows;
split(*rect, from_top(SEARCH_HEIGHT, SPACER), &rsearch, &rlists);
rows = count_rows(rlists, FONT_SPACE);
if (rows == 0) {
/* Hide the selector: draw nothing, and make it a 'virtual'
* one row selector. This is enough to use it from the search
* field and status only */
draw_search(surface, rect, sel);
selector_set_lines(sel, 1);
return;
}
draw_search(surface, &rsearch, sel);
selector_set_lines(sel, rows);
split(rlists, columns(0, 4, SPACER), &rcrates, &rrecords);
if (rcrates.w > LIBRARY_MIN_WIDTH) {
draw_index(surface, rrecords, sel);
draw_crates(surface, rcrates, sel);
} else {
draw_index(surface, *rect, sel);
}
}
static SDL_Rect to_sdl_rect(struct rect ours)
{
return (SDL_Rect) {
.x = ours.x,
.y = ours.y,
.w = ours.w,
.h = ours.h,
};
}
/*
* Draw the interface, using a bitmask to optimise which areas
*/
static void draw(SDL_Surface *surface, unsigned int redraw)
{
SDL_Rect areas[3], *damaged = areas;
struct rect whole, rworkspace, rplayers, rlibrary, rstatus, rtmp;
/* Split the display into the various areas. If an area is too
* small, abandon any actions to happen in that area. */
whole = rect(0, 0, surface->w, surface->h, scale);
rworkspace = shrink(rect(0, 0, surface->w, surface->h, scale), BORDER);
split(rworkspace, from_bottom(STATUS_HEIGHT, SPACER), &rtmp, &rstatus);
if (rtmp.h < 128 || rtmp.w < 0) {
rtmp = rworkspace;
redraw &= ~REDRAW_STATUS;
}
split(rtmp, from_top(PLAYER_HEIGHT, SPACER), &rplayers, &rlibrary);
if (rlibrary.h < LIBRARY_MIN_HEIGHT || rlibrary.w < LIBRARY_MIN_WIDTH) {
rplayers = rtmp;
redraw &= ~REDRAW_LIBRARY;
}
if (rplayers.h < 0 || rplayers.w < 0)
redraw &= ~REDRAW_DECKS;
if (!redraw)
return;
LOCK(surface);
if (redraw & REDRAW_BACKGROUND)
draw_rect(surface, &whole, background_col);
if (redraw & REDRAW_LIBRARY) {
draw_library(surface, &rlibrary, &selector);
*damaged++ = to_sdl_rect(rlibrary);
}
if (redraw & REDRAW_STATUS) {
draw_status(surface, &rstatus);
*damaged++ = to_sdl_rect(rstatus);
}
if (redraw & REDRAW_DECKS) {
draw_decks(surface, &rplayers, deck, ndeck, meter_scale);
*damaged++ = to_sdl_rect(rplayers);
}
UNLOCK(surface);
/* These calls cannot be checked for errors, because
* errors happen when the window has been resized */
if (redraw & REDRAW_BACKGROUND)
(void)SDL_UpdateWindowSurface(window);
else
(void)SDL_UpdateWindowSurfaceRects(window, areas, damaged - areas);
}
/*
* Text "input" from SDL is a unicode string
*/
static void handle_text(const char *input)
{
char k = input[0];
switch (k) {
case '.':
case ' ':
case 'a' ... 'z':
case 'A' ... 'Z':
case '0' ... '9':
selector_search_refine(&selector, k);
}
}
/*
* Handle a single key event
*
* Return: true if the selector needs to be redrawn, otherwise false
*/
static bool handle_key(SDL_Keycode key, Uint16 mod)
{
struct selector *sel = &selector;
if (key == SDLK_BACKSPACE) {
selector_search_expand(sel);
return true;
} else if (key == SDLK_HOME) {
selector_top(sel);
return true;
} else if (key == SDLK_END) {
selector_bottom(sel);
return true;
} else if (key == SDLK_UP) {
selector_up(sel);
return true;
} else if (key == SDLK_DOWN) {
selector_down(sel);
return true;
} else if (key == SDLK_PAGEUP) {
selector_page_up(sel);
return true;
} else if (key == SDLK_PAGEDOWN) {
selector_page_down(sel);
return true;
} else if (key == SDLK_LEFT) {
selector_prev(sel);
return true;
} else if (key == SDLK_RIGHT) {
selector_next(sel);
return true;
} else if (key == SDLK_TAB) {
if (mod & KMOD_CTRL) {
if (mod & KMOD_SHIFT)
selector_rescan(sel);
else
selector_toggle_order(sel);
} else {
selector_toggle(sel);
}
return true;
} else if ((key == SDLK_EQUALS) || (key == SDLK_PLUS)) {
meter_scale--;
if (meter_scale < 0)
meter_scale = 0;
fprintf(stderr, "Meter scale decreased to %d\n", meter_scale);
} else if (key == SDLK_MINUS) {
meter_scale++;
if (meter_scale > MAX_METER_SCALE)
meter_scale = MAX_METER_SCALE;
fprintf(stderr, "Meter scale increased to %d\n", meter_scale);
} else if (key >= SDLK_F1 && key <= SDLK_F12) {
size_t d;
/* Handle the function key press in groups of four --
* F1-F4 (deck 0), F5-F8 (deck 1) etc. */
d = (key - SDLK_F1) / 4;
if (d < ndeck) {
int func;
struct deck *de;
struct player *pl;
struct record *re;
struct timecoder *tc;
func = (key - SDLK_F1) % 4;
de = &deck[d];
pl = &de->player;
tc = &de->timecoder;
/* Some undocumented and 'special' functions exist
* here for the developer */
if (mod & KMOD_SHIFT && !(mod & KMOD_CTRL)) {
if (func < ndeck)
deck_clone(de, &deck[func]);
} else switch(func) {
case FUNC_LOAD:
re = selector_current(sel);
if (re != NULL)
deck_load(de, re);
break;
case FUNC_RECUE:
deck_recue(de);
break;
case FUNC_TIMECODE:
if (mod & KMOD_CTRL) {
if (mod & KMOD_SHIFT)
player_set_internal_playback(pl);
else
timecoder_cycle_definition(tc);
} else {
(void)player_toggle_timecode_control(pl);
}
break;
}
}
}
return false;
}
/*
* Action on size change event on the main window
*/
static SDL_Surface* set_size(void)
{
SDL_Surface *surface;
surface = SDL_GetWindowSurface(window);
if (surface == NULL) {
fprintf(stderr, "%s\n", SDL_GetError());
return NULL;
}
fprintf(stderr, "New interface size is %dx%d.\n",
surface->w, surface->h);
return surface;
}
static void push_event(int t)
{
SDL_Event e;
if (!SDL_PeepEvents(&e, 1, SDL_PEEKEVENT, t, t)) {
e.type = t;
if (SDL_PushEvent(&e) == -1)
abort();
}
}
/*
* Timer which posts a screen redraw event
*/
static Uint32 ticker(Uint32 interval, void *p)
{
push_event(EVENT_TICKER);
return interval;
}
/*
* Callback to tell the interface that status has changed
*/
static void defer_status_redraw(struct observer *o, void *x)
{
push_event(EVENT_STATUS);
}
static void defer_selector_redraw(struct observer *o, void *x)
{
push_event(EVENT_SELECTOR);
}
static void sync_status_from_selector(void)
{
const char *text = "No search results found";
struct record *record;
record = selector_current(&selector);
if (record)
text = record->pathname;
status_set(STATUS_VERBOSE, text);
}
/*
* Handle one SDL event
*
* Update the provided variables.
*
* Return: false if asked to exit the main loop, otherwise true
*/
static bool handle_sdl_event(SDL_Event *event,
unsigned int *redraw, SDL_Surface **surface)
{
switch(event->type) {
case SDL_QUIT: /* user request to quit application; eg. window close */
if (rig_quit() == -1)
return -1;
break;
case SDL_WINDOWEVENT:
switch (event->window.event) {
case SDL_WINDOWEVENT_RESIZED:
*surface = set_size();
if (!surface)
return false;
/* fall-through */
case SDL_WINDOWEVENT_EXPOSED:
*redraw = (unsigned)-1;
break;
}
break;
case EVENT_TICKER:
*redraw |= REDRAW_DECKS;
break;
case EVENT_QUIT: /* internal request to finish this thread */
return false;
case EVENT_STATUS:
*redraw |= REDRAW_STATUS;
break;
case EVENT_SELECTOR:
*redraw |= REDRAW_LIBRARY;
break;
case SDL_TEXTINPUT:
handle_text(event->text.text);
sync_status_from_selector();
break;
case SDL_KEYDOWN:
if (handle_key(event->key.keysym.sym, event->key.keysym.mod))
sync_status_from_selector();
}
return true;
}
/*
* The SDL interface thread
*/
static int interface_main(void)
{
SDL_TimerID timer;
SDL_Surface *surface;
surface = set_size();
if (!surface)
return -1;
/* The final action is to add the timer which triggers refresh */
timer = SDL_AddTimer(REFRESH, ticker, NULL);
rig_lock();
for (;;) {
unsigned int redraw = 0;
SDL_Event event;
rig_unlock();
if (SDL_WaitEvent(&event) < 0)
break;
rig_lock();
do {
if (!handle_sdl_event(&event, &redraw, &surface))
goto finish;
} while (SDL_PollEvent(&event) > 0);
draw(surface, redraw);
}
finish:
rig_unlock();
SDL_RemoveTimer(timer);
return 0;
}
static void* launch(void *p)
{
interface_main();
return NULL;
}
/*
* Parse the given geometry string into the given variables
*
* Geometry string includes size, position and scale. The format is
* "[<n>x<n>][+<n>+<n>][/<f>]". Some examples:
*
* 960x720
* +10+10
* 960x720+10+10
* /1.6
* 1920x1200@1.6
*
* Return: -1 if string could not be actioned, otherwise 0
*/
static int parse_geometry(const char *s,
int *x, int *y,
int *width, int *height,
float *scale)
{
int n, len;
char buf[128];
/* The %n in format strings is not a token, see scanf(3) man page */
n = sscanf(s, "%[0-9]x%d%n", buf, height, &len);
switch (n) {
case EOF:
return 0;
case 0:
break;
case 2:
/* we used a format to prevent parsing the '+' in the next block */
*width = atoi(buf);
s += len;
break;
default:
return -1;
}
n = sscanf(s, "+%d+%d%n", x, y, &len);
switch (n) {
case EOF:
return 0;
case 0:
break;
case 2:
s += len;
break;
default:
return -1;
}
n = sscanf(s, "/%f%n", scale, &len);
switch (n) {
case EOF:
return 0;
case 0:
break;
case 1:
if (*scale <= 0.0)
return -1;
s += len;
break;
default:
return -1;
}
if (*s != '\0')
return -1;
return 0;
}
/*
* Cleanup resources associated with this user interface
*/
static void cleanup()
{
size_t n;
for (n = 0; n < ndeck; n++)
timecoder_monitor_clear(&deck[n].timecoder);
clear_spinner();
ignore(&on_status);
ignore(&on_selector);
selector_clear(&selector);
clear_fonts();
if (iconv_close(utf) == -1)
abort();
TTF_Quit();
SDL_Quit();
}
/*
* Start the SDL interface
*/
int interface_start(struct library *lib, const char *geo, bool decor)
{
int x = SDL_WINDOWPOS_UNDEFINED,
y = SDL_WINDOWPOS_UNDEFINED,
width = DEFAULT_WIDTH,
height = DEFAULT_HEIGHT;
size_t n;
Uint32 window_flags = SDL_WINDOW_RESIZABLE;
if (parse_geometry(geo, &x, &y, &width, &height, &scale) == -1) {
fprintf(stderr, "Window geometry ('%s') is not valid.\n", geo);
return -1;
}
if (!decor)
window_flags |= SDL_WINDOW_BORDERLESS;
/*
* Start allocating resources
*
* Many exit paths here; get the ones most likely to fail (user error)
* out of the way first.
*/
/*
* Fonts
*/
fprintf(stderr, "Initialising fonts...\n");
if (TTF_Init() == -1) {
fprintf(stderr, "%s\n", TTF_GetError());
return -1;
}
if (load_fonts() == -1) {
goto fail_fonts;
}
/*
* SDL
*/
fprintf(stderr, "Initialising SDL...\n");
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
fprintf(stderr, "%s\n", SDL_GetError());
goto fail_fonts;
}
window = SDL_CreateWindow(banner, x, y, width, height, window_flags);
if (!window) {
fprintf(stderr, "%s\n", SDL_GetError());
goto fail_sdl;
}
/*
* Character translations; internally UTF8 is used
*/
utf = iconv_open("UTF8", "");
if (utf == (iconv_t)-1) {
perror("iconv_open");
goto fail_sdl;
}
/*
* Timecode monitors
*/
if (init_spinner(zoom(SPINNER_SIZE)) == -1)
goto fail_sdl;
for (n = 0; n < ndeck; n++) {
if (timecoder_monitor_init(&deck[n].timecoder, zoom(SCOPE_SIZE)) == -1)
not_implemented();
}
selector_init(&selector, lib);
watch(&on_status, &status_changed, defer_status_redraw);
watch(&on_selector, &selector.changed, defer_selector_redraw);
status_set(STATUS_VERBOSE, banner);
fprintf(stderr, "Launching interface thread...\n");
if (pthread_create(&ph, NULL, launch, NULL)) {
perror("pthread_create");
cleanup();
return -1;
}
return 0;
fail_sdl:
SDL_Quit();
fail_fonts:
TTF_Quit();
return -1;
}
/*
* Synchronise with the SDL interface and exit
*/
void interface_stop(void)
{
push_event(EVENT_QUIT);
if (pthread_join(ph, NULL) != 0)
abort();
cleanup();
}
|