Programming Macaw OS

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.

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.

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.

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.

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.

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.

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.

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.

note

double note(const char *name)

Works out the pitch of a named note, to give to tone.

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.

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.

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.

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.

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.

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.

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.

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.

notenumber

int notenumber(const char *name)

Turns a note's name, written the way Tracker writes it, into its number.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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).

Returns the port's number.

The system

platform

int platform(char *buf, int size)

Tells you what Macaw OS is running on.

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.

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.

sys_version

int sys_version(char *buf, int size)

Tells you which version of Macaw OS this is.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

Returns the biggest whole number that isn't more than n, as a double.