| 1 |
/* play.c - command to play a tune */ |
| 2 |
/* |
| 3 |
* GRUB -- GRand Unified Bootloader |
| 4 |
* Copyright (C) 2005,2007,2009 Free Software Foundation, Inc. |
| 5 |
* |
| 6 |
* GRUB is free software: you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation, either version 3 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* GRUB is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with GRUB. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* Lots of this file is borrowed from GNU/Hurd generic-speaker driver. */ |
| 21 |
|
| 22 |
#include <grub/dl.h> |
| 23 |
#include <grub/file.h> |
| 24 |
#include <grub/disk.h> |
| 25 |
#include <grub/term.h> |
| 26 |
#include <grub/misc.h> |
| 27 |
#include <grub/cpu/io.h> |
| 28 |
#include <grub/command.h> |
| 29 |
#include <grub/i18n.h> |
| 30 |
#include <grub/time.h> |
| 31 |
#include <grub/speaker.h> |
| 32 |
|
| 33 |
GRUB_MOD_LICENSE ("GPLv3+"); |
| 34 |
|
| 35 |
#define BASE_TEMPO (60 * 1000) |
| 36 |
|
| 37 |
|
| 38 |
#define T_REST ((grub_uint16_t) 0) |
| 39 |
#define T_FINE ((grub_uint16_t) -1) |
| 40 |
|
| 41 |
struct note |
| 42 |
{ |
| 43 |
grub_uint16_t pitch; |
| 44 |
grub_uint16_t duration; |
| 45 |
}; |
| 46 |
|
| 47 |
/* Returns whether playing should continue. */ |
| 48 |
static int |
| 49 |
play (unsigned tempo, struct note *note) |
| 50 |
{ |
| 51 |
grub_uint64_t to; |
| 52 |
|
| 53 |
if (note->pitch == T_FINE || grub_getkey_noblock () != GRUB_TERM_NO_KEY) |
| 54 |
return 1; |
| 55 |
|
| 56 |
grub_dprintf ("play", "pitch = %d, duration = %d\n", note->pitch, |
| 57 |
note->duration); |
| 58 |
|
| 59 |
switch (note->pitch) |
| 60 |
{ |
| 61 |
case T_REST: |
| 62 |
grub_speaker_beep_off (); |
| 63 |
break; |
| 64 |
|
| 65 |
default: |
| 66 |
grub_speaker_beep_on (note->pitch); |
| 67 |
break; |
| 68 |
} |
| 69 |
|
| 70 |
to = grub_get_time_ms () + BASE_TEMPO * note->duration / tempo; |
| 71 |
while ((grub_get_time_ms () <= to) |
| 72 |
&& (grub_getkey_noblock () == GRUB_TERM_NO_KEY)); |
| 73 |
|
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
static grub_err_t |
| 78 |
grub_cmd_play (grub_command_t cmd __attribute__ ((unused)), |
| 79 |
int argc, char **args) |
| 80 |
{ |
| 81 |
|
| 82 |
if (argc < 1) |
| 83 |
return grub_error (GRUB_ERR_BAD_ARGUMENT, |
| 84 |
/* TRANSLATORS: It's musical notes, not the notes |
| 85 |
you take. Play command expects arguments which can |
| 86 |
be either a filename or tempo+notes. |
| 87 |
This error happens if none is specified. */ |
| 88 |
N_("filename or tempo and notes expected")); |
| 89 |
|
| 90 |
if (argc == 1) |
| 91 |
{ |
| 92 |
struct note buf; |
| 93 |
grub_uint32_t tempo; |
| 94 |
grub_file_t file; |
| 95 |
|
| 96 |
file = grub_file_open (args[0], GRUB_FILE_TYPE_AUDIO); |
| 97 |
|
| 98 |
if (! file) |
| 99 |
return grub_errno; |
| 100 |
|
| 101 |
if (grub_file_read (file, &tempo, sizeof (tempo)) != sizeof (tempo)) |
| 102 |
{ |
| 103 |
grub_file_close (file); |
| 104 |
if (!grub_errno) |
| 105 |
grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"), |
| 106 |
args[0]); |
| 107 |
return grub_errno; |
| 108 |
} |
| 109 |
|
| 110 |
if (!tempo) |
| 111 |
{ |
| 112 |
grub_file_close (file); |
| 113 |
grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Invalid tempo in %s"), |
| 114 |
args[0]); |
| 115 |
return grub_errno; |
| 116 |
} |
| 117 |
|
| 118 |
tempo = grub_le_to_cpu32 (tempo); |
| 119 |
grub_dprintf ("play","tempo = %d\n", tempo); |
| 120 |
|
| 121 |
while (grub_file_read (file, &buf, |
| 122 |
sizeof (struct note)) == sizeof (struct note)) |
| 123 |
{ |
| 124 |
buf.pitch = grub_le_to_cpu16 (buf.pitch); |
| 125 |
buf.duration = grub_le_to_cpu16 (buf.duration); |
| 126 |
|
| 127 |
if (play (tempo, &buf)) |
| 128 |
break; |
| 129 |
} |
| 130 |
|
| 131 |
grub_file_close (file); |
| 132 |
} |
| 133 |
else |
| 134 |
{ |
| 135 |
const char *end; |
| 136 |
unsigned tempo; |
| 137 |
struct note note; |
| 138 |
int i; |
| 139 |
|
| 140 |
tempo = grub_strtoul (args[0], &end, 0); |
| 141 |
|
| 142 |
if (!tempo) |
| 143 |
{ |
| 144 |
grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Invalid tempo in %s"), |
| 145 |
args[0]); |
| 146 |
return grub_errno; |
| 147 |
} |
| 148 |
|
| 149 |
if (*end) |
| 150 |
/* Was not a number either, assume it was supposed to be a file name. */ |
| 151 |
return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file `%s' not found"), args[0]); |
| 152 |
|
| 153 |
grub_dprintf ("play","tempo = %d\n", tempo); |
| 154 |
|
| 155 |
for (i = 1; i + 1 < argc; i += 2) |
| 156 |
{ |
| 157 |
note.pitch = grub_strtoul (args[i], &end, 0); |
| 158 |
if (grub_errno) |
| 159 |
break; |
| 160 |
if (*end) |
| 161 |
{ |
| 162 |
grub_error (GRUB_ERR_BAD_NUMBER, N_("unrecognized number")); |
| 163 |
break; |
| 164 |
} |
| 165 |
|
| 166 |
note.duration = grub_strtoul (args[i + 1], &end, 0); |
| 167 |
if (grub_errno) |
| 168 |
break; |
| 169 |
if (*end) |
| 170 |
{ |
| 171 |
grub_error (GRUB_ERR_BAD_NUMBER, N_("unrecognized number")); |
| 172 |
break; |
| 173 |
} |
| 174 |
|
| 175 |
if (play (tempo, ¬e)) |
| 176 |
break; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
grub_speaker_beep_off (); |
| 181 |
|
| 182 |
return 0; |
| 183 |
} |
| 184 |
|
| 185 |
static grub_command_t cmd; |
| 186 |
|
| 187 |
GRUB_MOD_INIT(play) |
| 188 |
{ |
| 189 |
cmd = grub_register_command ("play", grub_cmd_play, |
| 190 |
N_("FILE | TEMPO [PITCH1 DURATION1] [PITCH2 DURATION2] ... "), |
| 191 |
N_("Play a tune.")); |
| 192 |
} |
| 193 |
|
| 194 |
GRUB_MOD_FINI(play) |
| 195 |
{ |
| 196 |
grub_unregister_command (cmd); |
| 197 |
} |