C reference: input, sound and the system
These are the calls in #include <macaw.h> for the keyboard and mouse, sound and music, files, the network, the system and saved passwords. They work like the Lua calls of the same names in Input and sound, Networking and Files and the system, and C explains how text, lists and your own functions pass between you and them. Keys are named by text, like "left", "space" or "z". None of the network calls waits: each does what it can straight away, so call it again from _update until it's done.
The keyboard and mouse
btn
int btn(const char *key)
Tells you whether a key is held down right now. Use it for movement and anything else that carries on while the key is held.
key: the key's name, like"left","space","a"or"f1". A name it doesn't know stops the program with an error.
Returns 1 while the key is held down, or 0. It's always 0 while your window isn't the one in front.
btnp
int btnp(const char *key)
Tells you whether a key was pressed this frame. It's true again every so often while the key is held, the way typing repeats, so use it for menus and jumps.
key: the key's name, as forbtn.
Returns 1 in the frame the key goes down or repeats, or 0.
mouse
void mouse(int *x, int *y, int *buttons)
Finds out where the pointer is and which mouse buttons are held down.
x: where to put the pointer's x in your window, in its own pixels: pass the address of anint, like&x, orNULL.y: where to put the pointer's y in your window, the same way. Both can be outside the window, even negative.buttons: where to put the buttons held: 1 for left, 2 for right and 4 for middle, added together. It's 0 over another window.
textinput
int textinput(char *buf, int size)
Gives you the characters typed this frame, for a text box or a name. Use btn and btnp for game controls instead.
buf: where the typed text goes.size: how many bytesbufholds. Text that doesn't fit is cut short.
Returns how many bytes were typed, even if buf was too small for them all. It's 0 in most frames.
keysp
int keysp(void)
Makes a list of every key that went down or repeated this frame. Read the names from it with keyspname.
It takes nothing.
Returns how many keys are in the list.
keyspname
int keyspname(int i, char *buf, int size)
Gives the name of one key in the list keysp made, so you can see what was pressed without testing every key.
i: which key in the list, from 0 to one less thankeysp()returned.buf: where the key's name goes, like"left"or"q".size: how many bytesbufholds. A name that doesn't fit is cut short.
Returns the name's whole length, or -1 if there's no key i in the list.
Sound
sfx
void sfx(const char *name)
Plays one of Macaw OS's ready-made sound effects. They play on voice 4 and sometimes 3 ("chime" uses all four), and cut off whatever you're playing there.
name: the effect:"blip","coin","jump","hit","lose","squawk","rawk","chime"or"shutter". Any other name stops the program with an error.
play
void play(int voice, const char *note, int ms)
Plays a musical note on one of the four sound-effect voices, as a square wave at half volume.
voice: which voice plays it, from 1 to 4. A new sound on a voice stops the one before.note: the note's name, like"C4"(middle C),"F#3"or"Bb5". A name it can't read stops the program with an error.ms: how long it lasts, in milliseconds.
tone
void tone(int voice, double hz, int ms)
Plays a plain beep at any pitch on one of the four sound-effect voices, as a square wave at half volume.
voice: which voice plays it, from 1 to 4. A new sound on a voice stops the one before. Other numbers play nothing.hz: the pitch in hertz: 440 is the A above middle C.ms: how long it lasts, in milliseconds.
note
double note(const char *name)
Works out the pitch of a named note, to give to tone.
name: the note's name, like"A4","C#5"or"Eb3", in capitals or small letters.
Returns the note's pitch in hertz (note("A4") is 440), or 0 if it can't read the name.
stop
void stop(void)
Stops whatever the four sound-effect voices are playing. A song on the music chip carries on.
It takes nothing.
music
void music(const char *path)
Plays a song made in Tracker from its start, or stops it. For songs made in the Music app, use playsong.
path: the song file's path. A path without/at the start is from your program's folder.NULLstops the music.
Songs and the music chip
playsong
int playsong(const char *path, int loop)
Plays a song file on the music chip, from the Music app or Tracker. The chip has voices of its own, so sound effects can play at the same time.
path: the song file's path, like"/home/music/island.song". A path without/at the start is from your program's folder.loop: 1 to let it loop until you stop it, or 0 to play it once.
Returns 1 if the song started, or 0 if it couldn't be read.
stopsong
void stopsong(void)
Stops the song that's playing.
It takes nothing.
loadsong
int loadsong(const char *path)
Reads a song file once, so you can play it many times with playloadedsong without reading the file again.
path: the song file's path. A path without/at the start is from your program's folder.
Returns a number for the song, or 0 if it couldn't be read.
playloadedsong
int playloadedsong(int song, int loop)
Plays a song you loaded with loadsong.
song: the numberloadsonggave you. A number that isn't a song stops the program with an error.loop: 1 to let it loop until you stop it, or 0 to play it once.
Returns 1 if the song started, or 0 if it couldn't.
freesong
void freesong(int song)
Lets go of a song you loaded, once you won't play it again. If it's playing, it carries on.
song: the song's number. A number that isn't a song is ignored.
music_volume
void music_volume(double v)
Sets how loud the music chip is, for songs and for music_note. There's one chip, so this changes the music of every program.
v: the volume, from 0 (silent) to 1 (full). Numbers outside that are kept to it.
music_note
void music_note(int channel, int instrument, double n, double volume)
Plays one note on the music chip straight away, with an instrument from the last song played. Use it for music that answers what the player does.
channel: which of the chip's 16 channels plays it, from 0 to 15. A new note on a channel takes over from the last.instrument: which of the song's instruments plays it, from 0 to 15. If no song has set it up, nothing plays.n: the note's number, asnotenumbergives it: 49 is C-4, middle C. Fractions fall between two notes.volume: how loud, from 0 to 1. Any number below 0 means 1.
music_release
void music_release(int channel)
Lets go of the note playing on a channel, so it fades away the way its instrument does.
channel: the channel, from 0 to 15.
notenumber
int notenumber(const char *name)
Turns a note's name, written the way Tracker writes it, into its number.
name: the note, like"C-4","F#3"or"A-5": a letter,-or#, then the octave."---"is no note and"==="is a note-off.
Returns the note's number ("C-4" is 49, and each semitone up is 1 more), 97 for "===", or 0 for "---" or a name it can't read.
notename
int notename(int n, char *buf, int size)
Turns a note's number back into its name, the way Tracker writes it.
n: the note's number: 49 is C-4. 0 gives"---"and 97 gives"===".buf: where the name goes, like"C-4".size: how many bytesbufholds. A name that doesn't fit is cut short.
Returns the name's whole length, which is 3 for the notes from 0 to 97.
Time
now
double now(void)
Tells you how long Macaw OS has been running. Take one reading from another to time something.
It takes nothing.
Returns the seconds since Macaw OS started, with fractions down to a millisecond.
Folders and files
fileexists
int fileexists(const char *path)
Checks whether there's a file or folder at a path.
path: the path to check. A path without/at the start is from your program's folder.
Returns 1 if there's a file or folder there, or 0.
isfolder
int isfolder(const char *path)
Checks whether a path is a folder.
path: the path to check. A path without/at the start is from your program's folder.
Returns 1 if it's a folder, or 0 if it's a file or isn't there.
makefolder
int makefolder(const char *path)
Makes a new folder. It makes one at a time, so the folder it goes in has to be there already.
path: the new folder's path. A path without/at the start is from your program's folder.
Returns 1 if it made the folder, or 0 if it couldn't, for example because it's already there.
listfolder
int listfolder(const char *path, int index, char *name, int size)
Gives the name of one thing in a folder. Call it with index 0, 1, 2 and so on until it returns 0, to go through them all.
path: the folder. A path without/at the start is from your program's folder.index: which thing, counting from 0. Folders come first, then files, each in order of name.name: where its name goes. A folder's name has no/on the end, so useisfolderto tell.size: how many bytesnameholds. A name that doesn't fit is cut short.
Returns 1 if there's a thing at index, or 0 once there's nothing left or the folder isn't there.
fileinfo
int fileinfo(const char *path, double *size, double *modified, int *isfolder)
Finds out how big a file is, when it last changed and whether it's a folder.
path: the file or folder. A path without/at the start is from your program's folder.size: where to put its size in bytes: pass the address of adouble, like&size, orNULL.modified: where to put when it last changed, in seconds since 1970: the address of adouble, orNULL.isfolder: where to put 1 if it's a folder or 0 if not: the address of anint, orNULL.
Returns 1 if it's there, or 0 if it isn't (and then nothing is written).
Markdown
markdown
int markdown(const char *text, char *html, int size, char *title, int tsize)
Turns Markdown into HTML, and gives you its first heading to use as a title.
text: the Markdown to turn into HTML.html: where the HTML goes.size: how many byteshtmlholds. HTML that doesn't fit is cut short.title: where the first heading's text goes (empty if there isn't one), orNULLif you don't need it.tsize: how many bytestitleholds, or 0 withNULL.
Returns the whole HTML's length. If it's size or more, html was cut short.
The clipboard
getclipboard
int getclipboard(char *buf, int size)
Gets the text on the clipboard, the one every Macaw OS program shares with the computer underneath.
buf: where the clipboard's text goes.size: how many bytesbufholds. Text that doesn't fit is cut short.
Returns the whole text's length, or 0 if the clipboard is empty.
setclipboard
void setclipboard(const char *s)
Puts text on the clipboard, for every Macaw OS program and the computer underneath.
s: the text to put there.
The desktop
openpath
int openpath(const char *path, const char **args, int n)
Opens a file or program as double-clicking it would: a program runs, and a document opens in its program.
path: what to open. A path without/at the start is from your program's folder.args: an array of text to give a program you open, which it gets asARGS, orNULLfor none.n: how many there are inargs, or 0 for none.
Returns 1 if it opened, or 0 if it couldn't.
getpref
int getpref(const char *key, char *buf, int size)
Reads one of the system preferences, the settings in System Preferences, as text.
key: the setting's name, like"volume"or"clock_24h".buf: where its value goes: a number like"0.8","true"or"false", or the setting's own text.size: how many bytesbufholds. A value that doesn't fit is cut short.
Returns the value's whole length, or -1 if there's no setting called key.
setpref
int setpref(const char *key, const char *value)
Changes one of the system preferences. The change is saved, so it stays after Macaw OS starts again.
key: the setting's name. Programs can't change the screen's (display_...): ask withdisplay_requestinstead.value: the new value as text: a number,"true"or"false"(or"1"or"0"), or text.NULLputs it back as it starts.
Returns 1 if it changed, or 0 if there's no such setting or the text doesn't suit it.
The network
net_online
int net_online(void)
Checks whether Macaw OS is dialled in to the Macaw network.
It takes nothing.
Returns 1 while it's online, or 0.
net_dial
void net_dial(void)
Asks Macaw OS to go online: Dial-Up opens and dials, unless it's online already. It doesn't wait, so check net_online in _update to see when the line is up.
It takes nothing.
net_connect
int net_connect(const char *host, int port)
Starts a connection to a computer on the Macaw network. It doesn't wait: check net_status to see when it's open.
host: the name to connect to, like"macaw.com","isp.macaw"for the ISP, or"name.macaw"for someone dialled in.port: the port to connect to on that computer, like 80 for web pages.
Returns a number for the connection, or 0 if Macaw OS isn't online. Then it also opens Dial-Up to dial.
net_send
int net_send(int s, const void *data, int n)
Sends bytes down a connection. Anything you send while it's still connecting goes once it's open.
s: the connection's number, fromnet_connectornet_accept.data: the bytes to send, such as a string or an array.n: how many bytes to send. For a string, that'sstrlen(data).
Returns how many bytes it took to send, or 0 if the connection has closed.
net_readline
int net_readline(int s, char *buf, int size)
Takes one whole line that has arrived on a connection. Use it for text that comes a line at a time, like a chat or a web server's answer.
s: the connection's number.buf: where the line goes, without its line ending (\nor\r\n).size: how many bytesbufholds. The rest of a line that doesn't fit is lost.
Returns the line's whole length, or -1 if no whole line has arrived yet. Once the other end closes, a last line without an ending comes too.
net_read
int net_read(int s, void *buf, int n)
Takes exactly n bytes from a connection, once that many have arrived. Use it when you know how much is coming.
s: the connection's number.buf: where the bytes go. It has to hold at leastnbytes.n: how many bytes to take.
Returns n, or -1 if fewer than n bytes have arrived so far (and then it takes none).
net_receive
int net_receive(int s, void *buf, int size)
Takes whatever has arrived on a connection so far, up to size bytes.
s: the connection's number.buf: where the bytes go. They don't end with a 0, so use the number it returns.size: the most bytes to take: how manybufholds. Any more wait for the next call.
Returns how many bytes it took, which is 0 if nothing has arrived.
net_status
int net_status(int s)
Tells you how a connection is getting on.
s: the connection's number.
Returns 0 while it's connecting, 1 when it's open (or closed with some still to read), 2 once it's closed, or 3 if it went wrong.
net_peer
int net_peer(int s, char *buf, int size)
Tells you who is at the other end of a connection.
s: the connection's number.buf: where their address goes: the host you connected to, or the address of someone who connected to you.size: how many bytesbufholds. An address that doesn't fit is cut short.
Returns the address's whole length.
net_close
void net_close(int s)
Closes a connection, or stops a listener. Everything closes by itself when your window does.
s: the number of the connection or listener. A number that isn't one is ignored.
net_listen
int net_listen(int port)
Starts listening on a port, so other people on the Macaw network can connect to you at yourname.macaw.
port: the port to listen on, like 7000. 0 picks a free one from 40000 to 60000, andnet_portsays which.
Returns a number for the listener, or 0 if it couldn't, because Macaw OS isn't online or the port is in use.
net_accept
int net_accept(int listener)
Takes the next person who has connected to your listener. It doesn't wait, so call it from _update.
listener: the numbernet_listengave you.
Returns a number for the new connection, or 0 if nobody is waiting.
net_port
int net_port(int listener)
Tells you which port a listener is on, which you need after net_listen(0).
listener: the numbernet_listengave you.
Returns the port's number.
The system
platform
int platform(char *buf, int size)
Tells you what Macaw OS is running on.
buf: where the name goes:"windows","web","emac"(a PowerPC Mac),"mac9"(Mac OS 9),"mac"or"unix".size: how many bytesbufholds. A name that doesn't fit is cut short.
Returns the name's whole length.
ismac
int ismac(void)
Checks whether Macaw OS is running on any kind of Mac.
It takes nothing.
Returns 1 on a Mac ("emac", "mac9" or "mac"), or 0.
modkey
int modkey(char *buf, int size)
Gives the name of the key people hold for shortcuts, so your help text can name the right one.
buf: where the name goes:"Cmd"on a Mac,"Ctrl"everywhere else.size: how many bytesbufholds. A name that doesn't fit is cut short.
Returns the name's whole length.
memory
void memory(double *mine, double *used, double *total)
Tells you how much memory your program has and how much the machine has.
mine: where to put how many bytes your program has asked for: the address of adouble, orNULL.used: where to put how many bytes every program together is using: adouble's address, orNULL.total: where to put how many bytes the machine has (512 MB): adouble's address, orNULL.
sys_version
int sys_version(char *buf, int size)
Tells you which version of Macaw OS this is.
buf: where the version goes, as text.size: how many bytesbufholds. Text that doesn't fit is cut short.
Returns the version's whole length.
sys_fps
double sys_fps(void)
Tells you how fast Macaw OS is drawing right now.
It takes nothing.
Returns the frame rate, in frames a second.
sys_display
void sys_display(int *w, int *h, int *colours)
Finds out the size of the whole screen and how many colours it shows.
w: where to put the screen's width in pixels: pass the address of anint, like&w, orNULL.h: where to put the screen's height in pixels, the same way.colours: where to put its colours: 8, 256 orCOLORS_THOUSANDS(32768).
sys_volume
double sys_volume(double v)
Reads the volume of all sound in Macaw OS, and can change it. A change is for the whole system, until someone sets it in System Preferences or Macaw OS starts again.
v: the new volume, from 0 to 1. Any number below 0 leaves it as it is, sosys_volume(-1)only reads it.
Returns the volume now, from 0 to 1.
sys_screenshot
int sys_screenshot(const char *path)
Saves a picture of the whole screen as a PNG file.
path: where to save it, like"/home/shot.png". A path without/at the start is from your program's folder.
Returns 1 if it saved the picture, or 0 if it couldn't.
sys_title
void sys_title(const char *s)
Sets the title of the window Macaw OS runs in on the computer underneath. It does nothing on Mac OS 9.
s: the new title.
sys_cursor
void sys_cursor(int on)
Shows or hides the computer's own pointer over Macaw OS. It starts hidden, because Macaw OS draws its own, and it stays as you leave it after your program quits.
on: 1 to show it, or 0 to hide it.
sys_clipboard
int sys_clipboard(char *buf, int size)
Gets the text on the computer's own clipboard. getclipboard is usually better: it also has what Macaw OS programs copy.
buf: where the clipboard's text goes.size: how many bytesbufholds. Text that doesn't fit is cut short.
Returns the text's whole length, or -1 if there's no text on it.
sys_setclipboard
void sys_setclipboard(const char *s)
Puts text on the computer's own clipboard. setclipboard is usually better, because every Macaw OS program can paste it too.
s: the text to put there.
Saved passwords (Nestchain)
nest_set
int nest_set(const char *service, const char *account, const char *password, const char *label)
Saves a password in your program's own keychain, which no other program can read. Saving another for the same service and account replaces it.
service: what the password is for, like"mygame.macaw".account: whose password it is, like a user name.NULLis the same as"".password: the password to save.label: a note to keep with it, shown in the list, orNULLto keep the note it had.
Returns 1 if it was saved, or 0 if it wasn't.
nest_get
int nest_get(const char *service, const char *account, char *buf, int size)
Reads back a password your program saved with nest_set.
service: what the password is for.account: whose password it is.NULLis the same as"".buf: where the password goes. Never print it or save it anywhere else.size: how many bytesbufholds. A password that doesn't fit is cut short.
Returns the password's whole length, or -1 if there isn't one saved.
nest_has
int nest_has(const char *service, const char *account)
Checks whether your program has a password saved, without reading it.
service: what the password is for.account: whose password it is.NULLis the same as"".
Returns 1 if there's one saved, or 0.
nest_remove
int nest_remove(const char *service, const char *account)
Forgets a password your program saved.
service: what the password is for.account: whose password it is.NULLis the same as"".
Returns 1 if it forgot one, or 0 if there wasn't one.
nest_list
int nest_list(const char *service)
Makes a list of what your program has saved, in order of service and then account. Read it with nest_listitem, nest_listlabel and nest_listchanged. It never holds the passwords.
service: only list the ones for this service, orNULLto list them all.
Returns how many are in the list.
nest_listitem
int nest_listitem(int i, char *service, int ssize, char *account, int asize)
Gives the service and account of one entry in the list nest_list made.
i: which entry, from 0.service: where its service goes.ssize: how many bytesserviceholds. Text that doesn't fit is cut short.account: where its account goes.asize: how many bytesaccountholds. Text that doesn't fit is cut short.
Returns 1 if there's an entry i in the list, or 0.
nest_listlabel
int nest_listlabel(int i, char *buf, int size)
Gives the note kept with one entry in the list nest_list made.
i: which entry, from 0.buf: where its note goes. It's empty if it has none.size: how many bytesbufholds. A note that doesn't fit is cut short.
Returns the note's whole length, or -1 if there's no entry i.
nest_listchanged
double nest_listchanged(int i)
Tells you when one entry in the list nest_list made was last saved.
i: which entry, from 0.
Returns when it was saved, in seconds since 1970, or 0 if there's no entry i.
nest_request
void nest_request(const char *service, const char *account, void (*fn)(const char *password, const char *why))
Asks the person for one of the system's own passwords, such as Dial-Up's. They choose Allow Once, Always Allow or Don't Allow, then fn gets the answer. If there's no such password, or they've chosen Always Allow before, fn is called straight away.
service: what the password is for.account: whose password it is.NULLis the same as"".fn: your function for the answer:password(NULLif you can't have it), andwhy,"denied"or"none"then. Copy the password to keep it.
nest_choose
void nest_choose(void (*fn)(const char *password, const char *service, const char *account))
Lets the person pick one of their saved passwords for your program. If they cancel, fn isn't called.
fn: your function, called with the password they picked and its service and account. Copy them to keep them.
nest_keep
int nest_keep(const char *password, const char *service, const char *account, void (*fn)(const char *service, const char *account))
Offers to save a password the person typed in the system's keychain: a box asks them what it's for. Your program can have it back later with nest_request, without them being asked.
password: the password to save.service: what it's for, to fill in the box with, orNULLto let the person say.account: whose it is, to fill in the box with, orNULLto let the person say.fn: your function, called with the service and account once it's saved, orNULL. It isn't called if they cancel.
Returns 1 if the box opened, or 0 if it didn't, for example because password is empty.
Handy maths for games
rnd
double rnd(double n)
Gives a random number, for dice, enemy moves and anything else that should change each time.
n: the top of the range. A number that isn't a real one (NaN or infinite) counts as 0.
Returns a random number from 0 up to, but not including, n, with fractions. flr(rnd(6)) + 1 rolls a dice.
mid
double mid(double a, double b, double c)
Gives the middle one of three numbers. With a low and a high limit either side, it keeps a number between them.
a: the first number.b: the second number.c: the third number.
Returns whichever of the three is between the other two.
clamp
double clamp(double v, double lo, double hi)
Keeps a number between two limits, like a player's position inside the screen.
v: the number to keep in range.lo: the lowest it may be.hi: the highest it may be.
Returns v, or lo if v is below it, or hi if v is above it.
sgn
int sgn(double n)
Tells you whether a number is below 0, for things like which way something faces.
n: the number to look at.
Returns -1 if n is below 0, or 1 otherwise. So sgn(0) is 1, not 0.
lerp
double lerp(double a, double b, double t)
Finds the number part of the way from one number to another, for smooth movement and fades.
a: where it starts, whentis 0.b: where it ends, whentis 1.t: how far along, from 0 to 1: 0.5 is halfway. Numbers outside that go pastaorb.
Returns a + (b - a) * t.
flr
double flr(double n)
Rounds a number down to a whole number, as floor does: flr(2.7) is 2 and flr(-2.5) is -3.
n: the number to round. A number that isn't a real one (NaN or infinite) gives 0.
Returns the biggest whole number that isn't more than n, as a double.