/* text.c - Text manipulation functions * Copyright (c) 1995-1997 Stefan Jokisch * * This file is part of Frotz. * * Frotz 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. * * Frotz 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 "frotz.h" enum string_type { LOW_STRING, ABBREVIATION, HIGH_STRING, EMBEDDED_STRING, VOCABULARY }; extern zword object_name (zword); static zchar decoded[10]; static zword encoded[3]; /* * According to Matteo De Luigi , * 0xab and 0xbb were in each other's proper positions. * Sat Apr 21, 2001 */ static zchar zscii_to_latin1[] = { 0xe4, 0xf6, 0xfc, 0xc4, 0xd6, 0xdc, 0xdf, 0xbb, 0xab, 0xeb, 0xef, 0xff, 0xcb, 0xcf, 0xe1, 0xe9, 0xed, 0xf3, 0xfa, 0xfd, 0xc1, 0xc9, 0xcd, 0xd3, 0xda, 0xdd, 0xe0, 0xe8, 0xec, 0xf2, 0xf9, 0xc0, 0xc8, 0xcc, 0xd2, 0xd9, 0xe2, 0xea, 0xee, 0xf4, 0xfb, 0xc2, 0xca, 0xce, 0xd4, 0xdb, 0xe5, 0xc5, 0xf8, 0xd8, 0xe3, 0xf1, 0xf5, 0xc3, 0xd1, 0xd5, 0xe6, 0xc6, 0xe7, 0xc7, 0xfe, 0xf0, 0xde, 0xd0, 0xa3, 0x00, 0x00, 0xa1, 0xbf }; /* * translate_from_zscii * * Map a ZSCII character onto the ISO Latin-1 alphabet. * */ zchar translate_from_zscii (zbyte c) { if (c == 0xfc) return ZC_MENU_CLICK; if (c == 0xfd) return ZC_DOUBLE_CLICK; if (c == 0xfe) return ZC_SINGLE_CLICK; if (c >= 0x9b && story_id != BEYOND_ZORK) { if (hx_unicode_table != 0) { /* game has its own Unicode table */ zbyte N; LOW_BYTE (hx_unicode_table, N) if (c - 0x9b < N) { zword addr = hx_unicode_table + 1 + 2 * (c - 0x9b); zword unicode; LOW_WORD (addr, unicode) return (unicode < 0x100) ? (zchar) unicode : '?'; } else return '?'; } else /* game uses standard set */ if (c <= 0xdf) { if (c == 0xdc || c == 0xdd) /* Oe and oe ligatures */ return '?'; /* are not ISO-Latin 1 */ return zscii_to_latin1[c - 0x9b]; } else return '?'; } return c; }/* translate_from_zscii */ /* * translate_to_zscii * * Map an ISO Latin-1 character onto the ZSCII alphabet. * */ zbyte translate_to_zscii (zchar c) { int i; if (c == ZC_SINGLE_CLICK) return 0xfe; if (c == ZC_DOUBLE_CLICK) return 0xfd; if (c == ZC_MENU_CLICK) return 0xfc; if (c >= ZC_LATIN1_MIN) { if (hx_unicode_table != 0) { /* game has its own Unicode table */ zbyte N; int i; LOW_BYTE (hx_unicode_table, N) for (i = 0x9b; i < 0x9b + N; i++) { zword addr = hx_unicode_table + 1 + 2 * (i - 0x9b); zword unicode; LOW_WORD (addr, unicode) if (c == unicode) return (zbyte) i; } return '?'; } else { /* game uses standard set */ for (i = 0x9b; i <= 0xdf; i++) if (c == zscii_to_latin1[i - 0x9b]) return (zbyte) i; return '?'; } } if (c == 0) /* Safety thing from David Kinder */ c = '?'; /* regarding his Unicode patches */ /* Sept 15, 2002 */ return c; }/* translate_to_zscii */ /* * alphabet * * Return a character from one of the three character sets. * */ static zchar alphabet (int set, int index) { if (h_alphabet != 0) { /* game uses its own alphabet */ zbyte c; zword addr = h_alphabet + 26 * set + index; LOW_BYTE (addr, c) return translate_from_zscii (c); } else /* game uses default alphabet */ if (set == 0) return 'a' + index; else if (set == 1) return 'A' + index; else if (h_version == V1) return " 0123456789.,!?_#'\"/\\<-:()"[index]; else return " ^0123456789.,!?_#'\"/\\-:()"[index]; }/* alphabet */ /* * load_string * * Copy a ZSCII string from the memory to the global "decoded" string. * */ static void load_string (zword addr, zword length) { int resolution = (h_version <= V3) ? 2 : 3; int i = 0; while (i < 3 * resolution) if (i < length) { zbyte c; LOW_BYTE (addr, c) addr++; decoded[i++] = translate_from_zscii (c); } else decoded[i++] = 0; }/* load_string */ /* * encode_text * * Encode the Unicode text in the global "decoded" string then write * the result to the global "encoded" array. (This is used to look up * words in the dictionary.) Up to V3 the vocabulary resolution is * two, since V4 it is three words. Because each word contains three * Z-characters, that makes six or nine Z-characters respectively. * Longer words are chopped to the proper size, shorter words are are * padded out with 5's. For word completion we pad with 0s and 31s, * the minimum and maximum Z-characters. * */ static void encode_text (int padding) { static zchar again[] = { 'a', 'g', 'a', 'i', 'n', 0 }; static zchar examine[] = { 'e', 'x', 'a', 'm', 'i', 'n', 'e', 0 }; static zchar wait[] = { 'w', 'a', 'i', 't', 0 }; zbyte zchars[12]; const zchar *ptr = decoded; zchar c; int resolution = (h_version <= V3) ? 2 : 3; int i = 0; /* Expand abbreviations that some old Infocom games lack */ if (f_setup.expand_abbreviations) if (padding == 0x05 && decoded[1] == 0) switch (decoded[0]) { case 'g': ptr = again; break; case 'x': ptr = examine; break; case 'z': ptr = wait; break; } /* Translate string to a sequence of Z-characters */ while (i < 3 * resolution) if ((c = *ptr++) != 0) { int index, set; zbyte c2; /* Search character in the alphabet */ for (set = 0; set < 3; set++) for (index = 0; index < 26; index++) if (c == alphabet (set, index)) goto letter_found; /* Character not found, store its ZSCII value */ c2 = translate_to_zscii (c); zchars[i++] = 5; zchars[i++] = 6; zchars[i++] = c2 >> 5; zchars[i++] = c2 & 0x1f; continue; letter_found: /* Character found, store its index */ if (set != 0) zchars[i++] = ((h_version <= V2) ? 1 : 3) + set; zchars[i++] = index + 6; } else zchars[i++] = padding; /* Three Z-characters make a 16bit word */ for (i = 0; i < resolution; i++) encoded[i] = (zchars[3 * i + 0] << 10) | (zchars[3 * i + 1] << 5) | (zchars[3 * i + 2]); encoded[resolution - 1] |= 0x8000; }/* encode_text */ /* * z_check_unicode, test if a unicode character can be read and printed. * * zargs[0] = Unicode * */ void z_check_unicode (void) { zword c = zargs[0]; if (c >= 0x20 && c <= 0x7e) store (3); else if (c == 0xa0) store (1); else if (c >= 0xa1 && c <= 0xff) store (3); else store (0); }/* z_check_unicode */ /* * z_encode_text, encode a ZSCII string for use in a dictionary. * * zargs[0] = address of text buffer * zargs[1] = length of ASCII string * zargs[2] = offset of ASCII string within the text buffer * zargs[3] = address to store encoded text in * * This is a V5+ opcode and therefore the dictionary resolution must be * three 16bit words. * */ void z_encode_text (void) { int i; load_string ((zword) (zargs[0] + zargs[2]), zargs[1]); encode_text (0x05); for (i = 0; i < 3; i++) storew ((zword) (zargs[3] + 2 * i), encoded[i]); }/* z_encode_text */ /* * decode_text * * Convert encoded text to Unicode. The encoded text consists of 16bit * words. Every word holds 3 Z-characters (5 bits each) plus a spare * bit to mark the last word. The Z-characters translate to ZSCII by * looking at the current current character set. Some select another * character set, others refer to abbreviations. * * There are several different string types: * * LOW_STRING - from the lower 64KB (byte address) * ABBREVIATION - from the abbreviations table (word address) * HIGH_STRING - from the end of the memory map (packed address) * EMBEDDED_STRING - from the instruction stream (at PC) * VOCABULARY - from the dictionary (byte address) * * The last type is only used for word completion. * */ #define outchar(c) if (st==VOCABULARY) *ptr++=c; else print_char(c) static void decode_text (enum string_type st, zword addr) { zchar *ptr; long byte_addr; zchar c2; zword code; zbyte c, prev_c = 0; int shift_state = 0; int shift_lock = 0; int status = 0; ptr = NULL; /* makes compilers shut up */ byte_addr = 0; /* Calculate the byte address if necessary */ if (st == ABBREVIATION) byte_addr = (long) addr << 1; else if (st == HIGH_STRING) { if (h_version <= V3) byte_addr = (long) addr << 1; else if (h_version <= V5) byte_addr = (long) addr << 2; else if (h_version <= V7) byte_addr = ((long) addr << 2) + ((long) h_strings_offset << 3); else /* h_version == V8 */ byte_addr = (long) addr << 3; if (byte_addr >= story_size) runtime_error (ERR_ILL_PRINT_ADDR); } /* Loop until a 16bit word has the highest bit set */ if (st == VOCABULARY) ptr = decoded; do { int i; /* Fetch the next 16bit word */ if (st == LOW_STRING || st == VOCABULARY) { LOW_WORD (addr, code) addr += 2; } else if (st == HIGH_STRING || st == ABBREVIATION) { HIGH_WORD (byte_addr, code) byte_addr += 2; } else CODE_WORD (code) /* Read its three Z-characters */ for (i = 10; i >= 0; i -= 5) { zword abbr_addr; zword ptr_addr; c = (code >> i) & 0x1f; switch (status) { case 0: /* normal operation */ if (shift_state == 2 && c == 6) status = 2; else if (h_version == V1 && c == 1) new_line (); else if (h_version >= V2 && shift_state == 2 && c == 7) new_line (); else if (c >= 6) outchar (alphabet (shift_state, c - 6)); else if (c == 0) outchar (' '); else if (h_version >= V2 && c == 1) status = 1; else if (h_version >= V3 && c <= 3) status = 1; else { shift_state = (shift_lock + (c & 1) + 1) % 3; if (h_version <= V2 && c >= 4) shift_lock = shift_state; break; } shift_state = shift_lock; break; case 1: /* abbreviation */ ptr_addr = h_abbreviations + 64 * (prev_c - 1) + 2 * c; LOW_WORD (ptr_addr, abbr_addr) decode_text (ABBREVIATION, abbr_addr); status = 0; break; case 2: /* ZSCII character - first part */ status = 3; break; case 3: /* ZSCII character - second part */ c2 = translate_from_zscii ((prev_c << 5) | c); outchar (c2); status = 0; break; } prev_c = c; } } while (!(code & 0x8000)); if (st == VOCABULARY) *ptr = 0; }/* decode_text */ #undef outchar /* * z_new_line, print a new line. * * no zargs used * */ void z_new_line (void) { new_line (); }/* z_new_line */ /* * z_print, print a string embedded in the instruction stream. * * no zargs used * */ void z_print (void) { decode_text (EMBEDDED_STRING, 0); }/* z_print */ /* * z_print_addr, print a string from the lower 64KB. * * zargs[0] = address of string to print * */ void z_print_addr (void) { decode_text (LOW_STRING, zargs[0]); }/* z_print_addr */ /* * z_print_char print a single ZSCII character. * * zargs[0] = ZSCII character to be printed * */ void z_print_char (void) { print_char (translate_from_zscii (zargs[0])); }/* z_print_char */ /* * z_print_form, print a formatted table. * * zargs[0] = address of formatted table to be printed * */ void z_print_form (void) { zword count; zword addr = zargs[0]; bool first = TRUE; for (;;) { LOW_WORD (addr, count) addr += 2; if (count == 0) break; if (!first) new_line (); while (count--) { zbyte c; LOW_BYTE (addr, c) addr++; print_char (translate_from_zscii (c)); } first = FALSE; } }/* z_print_form */ /* * print_num * * Print a signed 16bit number. * */ void print_num (zword value) { int i; /* Print sign */ if ((short) value < 0) { print_char ('-'); value = - (short) value; } /* Print absolute value */ for (i = 10000; i != 0; i /= 10) if (value >= i || i == 1) print_char ('0' + (value / i) % 10); }/* print_num */ /* * z_print_num, print a signed number. * * zargs[0] = number to print * */ void z_print_num (void) { print_num (zargs[0]); }/* z_print_num */ /* * print_object * * Print an object description. * */ void print_object (zword object) { zword addr = object_name (object); zword code = 0x94a5; zbyte length; LOW_BYTE (addr, length) addr++; if (length != 0) LOW_WORD (addr, code) if (code == 0x94a5) { /* encoded text 0x94a5 == empty string */ print_string ("object#"); /* supply a generic name */ print_num (object); /* for anonymous objects */ } else decode_text (LOW_STRING, addr); }/* print_object */ /* * z_print_obj, print an object description. * * zargs[0] = number of object to be printed * */ void z_print_obj (void) { print_object (zargs[0]); }/* z_print_obj */ /* * z_print_paddr, print the string at the given packed address. * * zargs[0] = packed address of string to be printed * */ void z_print_paddr (void) { decode_text (HIGH_STRING, zargs[0]); }/* z_print_paddr */ /* * z_print_ret, print the string at PC, print newline then return true. * * no zargs used * */ void z_print_ret (void) { decode_text (EMBEDDED_STRING, 0); new_line (); ret (1); }/* z_print_ret */ /* * print_string * * Print a string of ASCII characters. * */ void print_string (const char *s) { char c; while ((c = *s++) != 0) if (c == '\n') new_line (); else print_char (c); }/* print_string */ /* * z_print_unicode * * zargs[0] = Unicode * */ void z_print_unicode (void) { print_char ((zargs[0] <= 0xff) ? zargs[0] : '?'); }/* z_print_unicode */ /* * lookup_text * * Scan a dictionary searching for the given word. The first argument * can be * * 0x00 - find the first word which is >= the given one * 0x05 - find the word which exactly matches the given one * 0x1f - find the last word which is <= the given one * * The return value is 0 if the search fails. * */ static zword lookup_text (int padding, zword dct) { zword entry_addr; zword entry_count; zword entry; zword addr; zbyte entry_len; zbyte sep_count; int resolution = (h_version <= V3) ? 2 : 3; int entry_number; int lower, upper; int i; bool sorted; encode_text (padding); LOW_BYTE (dct, sep_count) /* skip word separators */ dct += 1 + sep_count; LOW_BYTE (dct, entry_len) /* get length of entries */ dct += 1; LOW_WORD (dct, entry_count) /* get number of entries */ dct += 2; if ((short) entry_count < 0) { /* bad luck, entries aren't sorted */ entry_count = - (short) entry_count; sorted = FALSE; } else sorted = TRUE; /* entries are sorted */ lower = 0; upper = entry_count - 1; while (lower <= upper) { if (sorted) /* binary search */ entry_number = (lower + upper) / 2; else /* linear search */ entry_number = lower; entry_addr = dct + entry_number * entry_len; /* Compare word to dictionary entry */ addr = entry_addr; for (i = 0; i < resolution; i++) { LOW_WORD (addr, entry) if (encoded[i] != entry) goto continuing; addr += 2; } return entry_addr; /* exact match found, return now */ continuing: if (sorted) /* binary search */ if (encoded[i] > entry) lower = entry_number + 1; else upper = entry_number - 1; else lower++; /* linear search */ } /* No exact match has been found */ if (padding == 0x05) return 0; entry_number = (padding == 0x00) ? lower : upper; if (entry_number == -1 || entry_number == entry_count) return 0; return dct + entry_number * entry_len; }/* lookup_text */ /* * tokenise_text * * Translate a single word to a token and append it to the token * buffer. Every token consists of the address of the dictionary * entry, the length of the word and the offset of the word from * the start of the text buffer. Unknown words cause empty slots * if the flag is set (such that the text can be scanned several * times with different dictionaries); otherwise they are zero. * */ static void tokenise_text (zword text, zword length, zword from, zword parse, zword dct, bool flag) { zword addr; zbyte token_max, token_count; LOW_BYTE (parse, token_max) parse++; LOW_BYTE (parse, token_count) if (token_count < token_max) { /* sufficient space left for token? */ storeb (parse++, token_count + 1); load_string ((zword) (text + from), length); addr = lookup_text (0x05, dct); if (addr != 0 || !flag) { parse += 4 * token_count; storew ((zword) (parse + 0), addr); storeb ((zword) (parse + 2), length); storeb ((zword) (parse + 3), from); } } }/* tokenise_text */ /* * tokenise_line * * Split an input line into words and translate the words to tokens. * */ void tokenise_line (zword text, zword token, zword dct, bool flag) { zword addr1; zword addr2; zbyte length; zbyte c; length = 0; /* makes compilers shut up */ /* Use standard dictionary if the given dictionary is zero */ if (dct == 0) dct = h_dictionary; /* Remove all tokens before inserting new ones */ storeb ((zword) (token + 1), 0); /* Move the first pointer across the text buffer searching for the beginning of a word. If this succeeds, store the position in a second pointer. Move the first pointer searching for the end of the word. When it is found, "tokenise" the word. Continue until the end of the buffer is reached. */ addr1 = text; addr2 = 0; if (h_version >= V5) { addr1++; LOW_BYTE (addr1, length) } do { zword sep_addr; zbyte sep_count; zbyte separator; /* Fetch next ZSCII character */ addr1++; if (h_version >= V5 && addr1 == text + 2 + length) c = 0; else LOW_BYTE (addr1, c) /* Check for separator */ sep_addr = dct; LOW_BYTE (sep_addr, sep_count) sep_addr++; do { LOW_BYTE (sep_addr, separator) sep_addr++; } while (c != separator && --sep_count != 0); /* This could be the start or the end of a word */ if (sep_count == 0 && c != ' ' && c != 0) { if (addr2 == 0) addr2 = addr1; } else if (addr2 != 0) { tokenise_text ( text, (zword) (addr1 - addr2), (zword) (addr2 - text), token, dct, flag ); addr2 = 0; } /* Translate separator (which is a word in its own right) */ if (sep_count != 0) tokenise_text ( text, (zword) (1), (zword) (addr1 - text), token, dct, flag ); } while (c != 0); }/* tokenise_line */ /* * z_tokenise, make a lexical analysis of a ZSCII string. * * zargs[0] = address of string to analyze * zargs[1] = address of token buffer * zargs[2] = address of dictionary (optional) * zargs[3] = set when unknown words cause empty slots (optional) * */ void z_tokenise (void) { /* Supply default arguments */ if (zargc < 3) zargs[2] = 0; if (zargc < 4) zargs[3] = 0; /* Call tokenise_line to do the real work */ tokenise_line (zargs[0], zargs[1], zargs[2], zargs[3] != 0); }/* z_tokenise */ /* * completion * * Scan the vocabulary to complete the last word on the input line * (similar to "tcsh" under Unix). The return value is * * 2 ==> completion is impossible * 1 ==> completion is ambiguous * 0 ==> completion is successful * * The function also returns a string in its second argument. In case * of 2, the string is empty; in case of 1, the string is the longest * extension of the last word on the input line that is common to all * possible completions (for instance, if the last word on the input * is "fo" and its only possible completions are "follow" and "folly" * then the string is "ll"); in case of 0, the string is an extension * to the last word that results in the only possible completion. * */ int completion (const zchar *buffer, zchar *result) { zword minaddr; zword maxaddr; zchar *ptr; zchar c; int len; int i; *result = 0; /* Copy last word to "decoded" string */ len = 0; while ((c = *buffer++) != 0) if (c != ' ') { if (len < 9) decoded[len++] = c; } else len = 0; decoded[len] = 0; /* Search the dictionary for first and last possible extensions */ minaddr = lookup_text (0x00, h_dictionary); maxaddr = lookup_text (0x1f, h_dictionary); if (minaddr == 0 || maxaddr == 0 || minaddr > maxaddr) return 2; /* Copy first extension to "result" string */ decode_text (VOCABULARY, minaddr); ptr = result; for (i = len; (c = decoded[i]) != 0; i++) *ptr++ = c; *ptr = 0; /* Merge second extension with "result" string */ decode_text (VOCABULARY, maxaddr); for (i = len, ptr = result; (c = decoded[i]) != 0; i++, ptr++) if (*ptr != c) break; *ptr = 0; /* Search was ambiguous or successful */ return (minaddr == maxaddr) ? 0 : 1; }/* completion */ ' href='#n549'>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
#include "plugin.h"
#include "lib/helper.h"
#include "lib/configfile.h"

#include "mpegplayer.h"
#include "mpeg_settings.h"

struct mpeg_settings settings;

#define THUMB_DELAY (75*HZ/100)

/* button definitions */
#if (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
    (CONFIG_KEYPAD == IRIVER_H300_PAD)
#define MPEG_START_TIME_SELECT      BUTTON_ON
#define MPEG_START_TIME_RIGHT       BUTTON_RIGHT
#define MPEG_START_TIME_LEFT        BUTTON_LEFT
#define MPEG_START_TIME_UP          BUTTON_UP
#define MPEG_START_TIME_DOWN        BUTTON_DOWN
#define MPEG_START_TIME_EXIT        BUTTON_OFF

#elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
#define MPEG_START_TIME_SELECT      BUTTON_PLAY
#define MPEG_START_TIME_RIGHT       BUTTON_RIGHT
#define MPEG_START_TIME_LEFT        BUTTON_LEFT
#define MPEG_START_TIME_UP          BUTTON_UP
#define MPEG_START_TIME_DOWN        BUTTON_DOWN
#define MPEG_START_TIME_EXIT        BUTTON_POWER

#elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
      (CONFIG_KEYPAD == IPOD_3G_PAD) || \
      (CONFIG_KEYPAD == IPOD_1G2G_PAD)
#define MPEG_START_TIME_SELECT      BUTTON_SELECT
#define MPEG_START_TIME_RIGHT       BUTTON_RIGHT
#define MPEG_START_TIME_LEFT        BUTTON_LEFT
#define MPEG_START_TIME_UP          BUTTON_SCROLL_FWD
#define MPEG_START_TIME_DOWN        BUTTON_SCROLL_BACK
#define MPEG_START_TIME_EXIT        BUTTON_MENU

#elif CONFIG_KEYPAD == GIGABEAT_PAD
#define MPEG_START_TIME_SELECT      BUTTON_SELECT
#define MPEG_START_TIME_LEFT        BUTTON_LEFT
#define MPEG_START_TIME_RIGHT       BUTTON_RIGHT
#define MPEG_START_TIME_UP          BUTTON_UP
#define MPEG_START_TIME_DOWN        BUTTON_DOWN
#define MPEG_START_TIME_SCROLL_DOWN BUTTON_VOL_DOWN
#define MPEG_START_TIME_SCROLL_UP   BUTTON_VOL_UP
#define MPEG_START_TIME_EXIT        BUTTON_POWER

#define MPEG_START_TIME_RC_SELECT      (BUTTON_RC_PLAY | BUTTON_REL)
#define MPEG_START_TIME_RC_LEFT        BUTTON_RC_REW
#define MPEG_START_TIME_RC_RIGHT       BUTTON_RC_FF
#define MPEG_START_TIME_RC_UP          BUTTON_RC_VOL_UP
#define MPEG_START_TIME_RC_DOWN        BUTTON_RC_VOL_DOWN
#define MPEG_START_TIME_RC_EXIT        (BUTTON_RC_PLAY | BUTTON_REPEAT)

#elif CONFIG_KEYPAD == GIGABEAT_S_PAD
#define MPEG_START_TIME_SELECT      BUTTON_SELECT
#define MPEG_START_TIME_LEFT        BUTTON_LEFT
#define MPEG_START_TIME_RIGHT       BUTTON_RIGHT
#define MPEG_START_TIME_UP          BUTTON_UP
#define MPEG_START_TIME_DOWN        BUTTON_DOWN
#define MPEG_START_TIME_SCROLL_DOWN BUTTON_VOL_DOWN
#define MPEG_START_TIME_SCROLL_UP   BUTTON_VOL_UP
#define MPEG_START_TIME_EXIT        BUTTON_POWER

#define MPEG_START_TIME_RC_SELECT      (BUTTON_RC_PLAY | BUTTON_REL)
#define MPEG_START_TIME_RC_LEFT        BUTTON_RC_REW
#define MPEG_START_TIME_RC_RIGHT       BUTTON_RC_FF
#define MPEG_START_TIME_RC_UP          BUTTON_RC_VOL_UP
#define MPEG_START_TIME_RC_DOWN        BUTTON_RC_VOL_DOWN
#define MPEG_START_TIME_RC_EXIT        (BUTTON_RC_PLAY | BUTTON_REPEAT)

#elif CONFIG_KEYPAD == IRIVER_H10_PAD
#define MPEG_START_TIME_SELECT      BUTTON_PLAY
#define MPEG_START_TIME_LEFT        BUTTON_LEFT
#define MPEG_START_TIME_RIGHT       BUTTON_RIGHT
#define MPEG_START_TIME_UP          BUTTON_SCROLL_UP
#define MPEG_START_TIME_DOWN        BUTTON_SCROLL_DOWN
#define MPEG_START_TIME_EXIT        BUTTON_POWER

#elif (CONFIG_KEYPAD == SANSA_E200_PAD)
#define MPEG_START_TIME_SELECT      BUTTON_SELECT
#define MPEG_START_TIME_SCROLL_UP   BUTTON_SCROLL_BACK
#define MPEG_START_TIME_SCROLL_DOWN BUTTON_SCROLL_FWD
#define MPEG_START_TIME_LEFT        BUTTON_LEFT
#define MPEG_START_TIME_RIGHT       BUTTON_RIGHT
#define MPEG_START_TIME_UP          BUTTON_UP
#define MPEG_START_TIME_DOWN        BUTTON_DOWN
#define MPEG_START_TIME_EXIT        BUTTON_POWER

#elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
#define MPEG_START_TIME_SELECT      BUTTON_SELECT
#define MPEG_START_TIME_SCROLL_UP   BUTTON_SCROLL_BACK
#define MPEG_START_TIME_SCROLL_DOWN BUTTON_SCROLL_FWD
#define MPEG_START_TIME_LEFT        BUTTON_LEFT
#define MPEG_START_TIME_RIGHT       BUTTON_RIGHT
#define MPEG_START_TIME_UP          BUTTON_UP
#define MPEG_START_TIME_DOWN        BUTTON_DOWN
#define MPEG_START_TIME_EXIT        (BUTTON_HOME|BUTTON_REPEAT)

#elif (CONFIG_KEYPAD == SANSA_C200_PAD) || \
(CONFIG_KEYPAD == SANSA_CLIP_PAD) || \
(CONFIG_KEYPAD == SANSA_M200_PAD)
#define MPEG_START_TIME_SELECT      BUTTON_SELECT
#define MPEG_START_TIME_SCROLL_UP   BUTTON_VOL_UP
#define MPEG_START_TIME_SCROLL_DOWN BUTTON_VOL_DOWN
#define MPEG_START_TIME_LEFT        BUTTON_LEFT
#define MPEG_START_TIME_RIGHT       BUTTON_RIGHT
#define MPEG_START_TIME_UP          BUTTON_UP
#define MPEG_START_TIME_DOWN        BUTTON_DOWN
#define MPEG_START_TIME_EXIT        BUTTON_POWER

#elif CONFIG_KEYPAD == MROBE500_PAD
#define MPEG_START_TIME_SELECT      BUTTON_RC_HEART
#define MPEG_START_TIME_SCROLL_UP   BUTTON_RC_VOL_UP
#define MPEG_START_TIME_SCROLL_DOWN BUTTON_RC_VOL_DOWN
#define MPEG_START_TIME_LEFT        BUTTON_LEFT
#define MPEG_START_TIME_RIGHT       BUTTON_RIGHT
#define MPEG_START_TIME_UP          BUTTON_RC_PLAY
#define MPEG_START_TIME_DOWN        BUTTON_RC_DOWN
#define MPEG_START_TIME_EXIT        BUTTON_POWER

#elif CONFIG_KEYPAD == MROBE100_PAD
#define MPEG_START_TIME_SELECT      BUTTON_SELECT
#define MPEG_START_TIME_LEFT        BUTTON_LEFT
#define MPEG_START_TIME_RIGHT       BUTTON_RIGHT
#define MPEG_START_TIME_UP          BUTTON_UP
#define MPEG_START_TIME_DOWN        BUTTON_DOWN
#define MPEG_START_TIME_SCROLL_DOWN BUTTON_MENU
#define MPEG_START_TIME_SCROLL_UP   BUTTON_PLAY
#define MPEG_START_TIME_EXIT        BUTTON_POWER

#elif CONFIG_KEYPAD == IAUDIO_M3_PAD
#define MPEG_START_TIME_SELECT      BUTTON_RC_PLAY
#define MPEG_START_TIME_LEFT        BUTTON_RC_REW
#define MPEG_START_TIME_RIGHT       BUTTON_RC_FF
#define MPEG_START_TIME_UP          BUTTON_RC_VOL_UP
#define MPEG_START_TIME_DOWN        BUTTON_RC_VOL_DOWN
#define MPEG_START_TIME_EXIT        BUTTON_RC_REC

#elif CONFIG_KEYPAD == COWOND2_PAD
#define MPEG_START_TIME_EXIT        BUTTON_POWER

#elif CONFIG_KEYPAD == IAUDIO67_PAD
#define MPEG_START_TIME_SELECT      BUTTON_MENU
#define MPEG_START_TIME_LEFT        BUTTON_LEFT
#define MPEG_START_TIME_RIGHT       BUTTON_RIGHT
#define MPEG_START_TIME_UP          BUTTON_STOP
#define MPEG_START_TIME_DOWN        BUTTON_PLAY
#define MPEG_START_TIME_EXIT        BUTTON_POWER

#elif CONFIG_KEYPAD == CREATIVEZVM_PAD
#define MPEG_START_TIME_SELECT      BUTTON_SELECT
#define MPEG_START_TIME_SCROLL_UP   BUTTON_PLAY
#define MPEG_START_TIME_SCROLL_DOWN BUTTON_MENU
#define MPEG_START_TIME_LEFT        BUTTON_LEFT
#define MPEG_START_TIME_RIGHT       BUTTON_RIGHT
#define MPEG_START_TIME_UP          BUTTON_UP
#define MPEG_START_TIME_DOWN        BUTTON_DOWN
#define MPEG_START_TIME_EXIT        BUTTON_BACK

#elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
#define MPEG_START_TIME_SELECT      BUTTON_SELECT
#define MPEG_START_TIME_LEFT        BUTTON_LEFT
#define MPEG_START_TIME_RIGHT       BUTTON_RIGHT
#define MPEG_START_TIME_UP          BUTTON_UP
#define MPEG_START_TIME_DOWN        BUTTON_DOWN
#define MPEG_START_TIME_SCROLL_DOWN BUTTON_VOL_DOWN
#define MPEG_START_TIME_SCROLL_UP   BUTTON_VOL_UP
#define MPEG_START_TIME_EXIT        BUTTON_POWER

#elif CONFIG_KEYPAD == ONDAVX747_PAD
#define MPEG_START_TIME_EXIT        BUTTON_POWER

#else
#error No keymap defined!
#endif

#ifdef HAVE_TOUCHSCREEN
#ifndef MPEG_START_TIME_SELECT
#define MPEG_START_TIME_SELECT      BUTTON_CENTER
#endif
#ifndef MPEG_START_TIME_SCROLL_UP
#define MPEG_START_TIME_SCROLL_UP   BUTTON_TOPRIGHT
#endif
#ifndef MPEG_START_TIME_SCROLL_DOWN
#define MPEG_START_TIME_SCROLL_DOWN BUTTON_TOPLEFT
#endif
#ifndef MPEG_START_TIME_LEFT
#define MPEG_START_TIME_LEFT        BUTTON_MIDLEFT
#endif
#ifndef MPEG_START_TIME_RIGHT
#define MPEG_START_TIME_RIGHT       BUTTON_MIDRIGHT
#endif
#ifndef MPEG_START_TIME_UP
#define MPEG_START_TIME_UP          BUTTON_TOPMIDDLE
#endif
#ifndef MPEG_START_TIME_DOWN
#define MPEG_START_TIME_DOWN        BUTTON_BOTTOMMIDDLE
#endif
#ifndef MPEG_START_TIME_EXIT
#define MPEG_START_TIME_EXIT        BUTTON_TOPLEFT
#endif
#endif

static struct configdata config[] =
{
    {TYPE_INT, 0, 2, { .int_p = &settings.showfps }, "Show FPS", NULL},
    {TYPE_INT, 0, 2, { .int_p = &settings.limitfps }, "Limit FPS", NULL},
    {TYPE_INT, 0, 2, { .int_p = &settings.skipframes }, "Skip frames", NULL},
    {TYPE_INT, 0, INT_MAX, { .int_p = &settings.resume_count }, "Resume count",
     NULL},
    {TYPE_INT, 0, MPEG_RESUME_NUM_OPTIONS,
     { .int_p = &settings.resume_options }, "Resume options", NULL},
#if defined(TOSHIBA_GIGABEAT_F) || defined(SANSA_E200) || defined(SANSA_C200) || defined(SANSA_FUZE) || defined(SANSA_E200V2)
    {TYPE_INT, 0, INT_MAX, { .int_p = &settings.displayoptions },
     "Display options", NULL},
#endif
    {TYPE_INT, 0, 2, { .int_p = &settings.tone_controls }, "Tone controls",
     NULL},
    {TYPE_INT, 0, 2, { .int_p = &settings.channel_modes }, "Channel modes",
     NULL},
    {TYPE_INT, 0, 2, { .int_p = &settings.crossfeed }, "Crossfeed", NULL},
    {TYPE_INT, 0, 2, { .int_p = &settings.equalizer }, "Equalizer", NULL},
    {TYPE_INT, 0, 2, { .int_p = &settings.dithering }, "Dithering", NULL},
#ifdef HAVE_BACKLIGHT_BRIGHTNESS
    {TYPE_INT, -1, INT_MAX, { .int_p = &settings.backlight_brightness },
     "Backlight brightness", NULL},
#endif
};

static const struct opt_items noyes[2] = {
    { "No", -1 },
    { "Yes", -1 },
};

static const struct opt_items enabledisable[2] = {
    { "Disable", -1 },
    { "Enable", -1 },
};

static const struct opt_items globaloff[2] = {
    { "Force off", -1 },
    { "Use sound setting", -1 },
};

#ifdef HAVE_BACKLIGHT_BRIGHTNESS
#define BACKLIGHT_OPTION_DEFAULT "Use setting"
#endif

static long mpeg_menu_sysevent_id;

void mpeg_menu_sysevent_clear(void)
{
    mpeg_menu_sysevent_id = 0;
}

int mpeg_menu_sysevent_callback(int btn, const struct menu_item_ex *menu)
{
    switch (btn)
    {
    case SYS_USB_CONNECTED:
    case SYS_POWEROFF:
        mpeg_menu_sysevent_id = btn;
        return ACTION_STD_CANCEL;
    }

    return btn;
    (void)menu;
}

long mpeg_menu_sysevent(void)
{
    return mpeg_menu_sysevent_id;
}

void mpeg_menu_sysevent_handle(void)
{
    long id = mpeg_menu_sysevent();
    if (id != 0)
        rb->default_event_handler(id);
}

static bool mpeg_set_option(const char* string,
                            void* variable,
                            enum optiontype type,
                            const struct opt_items* options,
                            int numoptions,
                            void (*function)(int))
{
    mpeg_menu_sysevent_clear();

    /* This eats SYS_POWEROFF - :\ */
    bool usb = rb->set_option(string, variable, type, options, numoptions,
                              function);

    if (usb)
        mpeg_menu_sysevent_id = ACTION_STD_CANCEL;

    return usb;
}

#ifdef HAVE_BACKLIGHT_BRIGHTNESS /* Only used for this atm */
static bool mpeg_set_int(const char *string, const char *unit,
                         int voice_unit, const int *variable,
                         void (*function)(int), int step,
                         int min,
                         int max,
                         void (*formatter)(char*, size_t, int, const char*))
{
    mpeg_menu_sysevent_clear();

    bool usb = rb->set_int(string, unit, voice_unit, variable, function,
                           step, min, max, formatter);

    if (usb)
        mpeg_menu_sysevent_id = ACTION_STD_CANCEL;

    return usb;
}
#endif /* HAVE_BACKLIGHT_BRIGHTNESS */

#ifdef HAVE_BACKLIGHT_BRIGHTNESS
void mpeg_backlight_update_brightness(int value)
{
    if (value >= 0)
    {
        value += MIN_BRIGHTNESS_SETTING;
        backlight_brightness_set(value);
    }
    else
    {
        backlight_brightness_use_setting();
    }
}

static void backlight_brightness_function(int value)
{
    mpeg_backlight_update_brightness(value);
}

static void backlight_brightness_formatter(char *buf, size_t length,
                                           int value, const char *input)
{
    if (value < 0)
        rb->strncpy(buf, BACKLIGHT_OPTION_DEFAULT, length);
    else
        rb->snprintf(buf, length, "%d", value + MIN_BRIGHTNESS_SETTING);

    (void)input;
}
#endif /* HAVE_BACKLIGHT_BRIGHTNESS */

/* Sync a particular audio setting to global or mpegplayer forced off */
static void sync_audio_setting(int setting, bool global)
{
    int val0, val1;

    switch (setting)
    {
    case MPEG_AUDIO_TONE_CONTROLS:
        if (global || settings.tone_controls)
        {
            val0 = rb->global_settings->bass;
            val1 = rb->global_settings->treble;
        }
        else
        {
            val0 = rb->sound_default(SOUND_BASS);
            val1 = rb->sound_default(SOUND_TREBLE);
        }
        rb->sound_set(SOUND_BASS, val0);
        rb->sound_set(SOUND_TREBLE, val1);
        break;

    case MPEG_AUDIO_CHANNEL_MODES:
        val0 = (global || settings.channel_modes) ?
                rb->global_settings->channel_config :
                SOUND_CHAN_STEREO;
        rb->sound_set(SOUND_CHANNELS, val0);
        break;

    case MPEG_AUDIO_CROSSFEED:
        rb->dsp_set_crossfeed((global || settings.crossfeed) ?
                              rb->global_settings->crossfeed : false);
        break;

    case MPEG_AUDIO_EQUALIZER:
        rb->dsp_set_eq((global || settings.equalizer) ?
                       rb->global_settings->eq_enabled : false);
        break;

    case MPEG_AUDIO_DITHERING:
        rb->dsp_dither_enable((global || settings.dithering) ?
                              rb->global_settings->dithering_enabled : false);
       break;
    }
}

/* Sync all audio settings to global or mpegplayer forced off */
static void sync_audio_settings(bool global)
{
    static const int setting_index[] =
    {
        MPEG_AUDIO_TONE_CONTROLS,
        MPEG_AUDIO_CHANNEL_MODES,
        MPEG_AUDIO_CROSSFEED,
        MPEG_AUDIO_EQUALIZER,
        MPEG_AUDIO_DITHERING,
    };
    unsigned i;

    for (i = 0; i < ARRAYLEN(setting_index); i++)
    {
        sync_audio_setting(setting_index[i], global);
    }
}

#ifndef HAVE_LCD_COLOR
/* Cheapo splash implementation for the grey surface */
static void grey_splash(int ticks, const unsigned char *fmt, ...)
{
    unsigned char buffer[256];
    int x, y, w, h;
    int oldfg, oldmode;

    va_list ap;
    va_start(ap, fmt);

    rb->vsnprintf(buffer, sizeof (buffer), fmt, ap);

    va_end(ap);

    grey_getstringsize(buffer, &w, &h);

    oldfg = grey_get_foreground();
    oldmode = grey_get_drawmode();

    grey_set_drawmode(DRMODE_FG);
    grey_set_foreground(GREY_LIGHTGRAY);

    x = (LCD_WIDTH - w) / 2;
    y = (LCD_HEIGHT - h) / 2;

    grey_fillrect(x - 1, y - 1, w + 2, h + 2);

    grey_set_foreground(GREY_BLACK);

    grey_putsxy(x, y, buffer);
    grey_drawrect(x - 2, y - 2, w + 4, h + 4);

    grey_set_foreground(oldfg);
    grey_set_drawmode(oldmode);

    grey_update();

    if (ticks > 0)
        rb->sleep(ticks);
}
#endif /* !HAVE_LCD_COLOR */

static void show_loading(struct vo_rect *rc)
{
    int oldmode = lcd_(get_drawmode)();
    lcd_(set_drawmode)(DRMODE_SOLID | DRMODE_INVERSEVID);
    lcd_(fillrect)(rc->l-1, rc->t-1, rc->r - rc->l + 2, rc->b - rc->t + 2);
    lcd_(set_drawmode)(oldmode);
    lcd_(splash)(0, "Loading...");
}

static void draw_slider(uint32_t range, uint32_t pos, struct vo_rect *rc)
{
    #define SLIDER_WIDTH   (LCD_WIDTH-SLIDER_LMARGIN-SLIDER_RMARGIN)
    #define SLIDER_X       SLIDER_LMARGIN
    #define SLIDER_Y       (LCD_HEIGHT-SLIDER_HEIGHT-SLIDER_BMARGIN)
    #define SLIDER_HEIGHT  8
    #define SLIDER_TEXTMARGIN 1
    #define SLIDER_LMARGIN 1
    #define SLIDER_RMARGIN 1
    #define SLIDER_TMARGIN 1
    #define SLIDER_BMARGIN 1
    #define SCREEN_MARGIN  1

    struct hms hms;
    char str[32];
    int text_w, text_h, text_y;

    /* Put positition on left */
    ts_to_hms(pos, &hms);
    hms_format(str, sizeof(str), &hms);
    lcd_(getstringsize)(str, NULL, &text_h);
    text_y = SLIDER_Y - SLIDER_TEXTMARGIN - text_h;

    if (rc == NULL)
    {
        int oldmode = lcd_(get_drawmode)();
        lcd_(set_drawmode)(DRMODE_BG | DRMODE_INVERSEVID);
        lcd_(fillrect)(SLIDER_X, text_y, SLIDER_WIDTH,
                       LCD_HEIGHT - SLIDER_BMARGIN - text_y
                       - SLIDER_TMARGIN);
        lcd_(set_drawmode)(oldmode);

        lcd_(putsxy)(SLIDER_X, text_y, str);

        /* Put duration on right */
        ts_to_hms(range, &hms);
        hms_format(str, sizeof(str), &hms);
        lcd_(getstringsize)(str, &text_w, NULL);

        lcd_(putsxy)(SLIDER_X + SLIDER_WIDTH - text_w, text_y, str);

        /* Draw slider */
        lcd_(drawrect)(SLIDER_X, SLIDER_Y, SLIDER_WIDTH, SLIDER_HEIGHT);
        lcd_(fillrect)(SLIDER_X, SLIDER_Y,
                       muldiv_uint32(pos, SLIDER_WIDTH, range),
                       SLIDER_HEIGHT);

        /* Update screen */
        lcd_(update_rect)(SLIDER_X, text_y - SLIDER_TMARGIN, SLIDER_WIDTH,
                          LCD_HEIGHT - SLIDER_BMARGIN - text_y + SLIDER_TEXTMARGIN);
    }
    else
    {
        /* Just return slider rectangle */
        rc->l = SLIDER_X;
        rc->t = text_y - SLIDER_TMARGIN;
        rc->r = rc->l + SLIDER_WIDTH;
        rc->b = rc->t + LCD_HEIGHT - SLIDER_BMARGIN - text_y;
    }
}

static bool display_thumb_image(const struct vo_rect *rc)
{
    if (!stream_display_thumb(rc))
    {
        lcd_(splash)(0, "Frame not available");
        return false;
    }

    /* Draw a raised border around the frame */
    int oldcolor = lcd_(get_foreground)();
    lcd_(set_foreground)(DRAW_LIGHTGRAY);

    lcd_(hline)(rc->l-1, rc->r-1, rc->t-1);
    lcd_(vline)(rc->l-1, rc->t, rc->b-1);

    lcd_(set_foreground)(DRAW_DARKGRAY);

    lcd_(hline)(rc->l-1, rc->r, rc->b);