PSP Programming/General/Common Callback

Common Callback
Before we begin programming for the PSP, we will start by creating a folder named “common” in our programming directory. This folder will contain several files which will often be used in our programs. We'll put these files here so that we don't constantly have to write the same code over and over again.

callback.h
Now let's create a new file with the name "callback.h". This header file will declare some of the stuff needed so that we can get our program to run on the PSP.

The 'ifndef' is used to make sure that this file only gets imported once, otherwise an error will occur. Then it should be pretty self-explanatory; we will have two functions which we will use: "isRunning" to check if the user has requested to quit the program, and "setupCallbacks" which will setup all the necessary things for your program to run on the PSP.

callback.c
Now that we have the header definitions we can also create the source file: name it "callback.c". We'll start by including the file "pspkernel.h" which gives us access to several kernel methods.

Next we create a boolean. Executing the method "isRunning" will tell us whether a request to exit the application was created by the user. We will use this function in our program so that we can clean up any leftover memory, and exit gracefully.

Now this is where the hard part comes in... the good news, is that you don't need to completely understand it! Essentially what it does is create a new thread, which will create an exit callback. The callback will then make "exitRequest" equal to true if the user presses the "home" and "exit" button on the PSP. See for yourself:

Don't worry if you don't completely understand it, that's why we created a separate file for it. It's not going to change, so all we have to do is include it.

Now you can go into the next section, which will show you how to create a simple "Hello World!" program.