Intercepting home button?

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
User avatar
pailes
Posts: 16
Joined: Fri Feb 16, 2007 4:31 am
Contact:

Intercepting home button?

Post by pailes »

Hey there,

I just got into PSP development, and I'd like to thank for all people working on the PSPSDK and especially TyRaNiD for PSPLink, it's working like a bliss even on Mac OSX and it makes development very easy. Keep on going, guys!

Now to my question:
In my application I'd like to intercept the home button, so the user can be asked if he wants to save changes to the current document, but I'm not sure what's the best way to do it. I searched the forums for a while and couldn't find any solution so I'm asking now, sorry if I've been missing something.

Or is this trivial?

Regards,
Pailes
jas0nuk
Posts: 137
Joined: Thu Apr 27, 2006 8:00 am

Post by jas0nuk »

Dirty solution... don't use callbacks. This will prevent the "Do you want to exit?" screen from appearing.

When the Home button is pressed (PSP_CTRL_HOME), just pop up a screen doing whatever you want, and to exit, use sceKernelExitGame();
User avatar
pailes
Posts: 16
Joined: Fri Feb 16, 2007 4:31 am
Contact:

Post by pailes »

I'm using sceCtrlReadBufferPositive() but it returns 0 in the button member when I press home. Do I need to enable something to make this work with the home button?
jas0nuk
Posts: 137
Joined: Thu Apr 27, 2006 8:00 am

Post by jas0nuk »

This is the code I use in my projects when I want a "Press HOME to exit" prompt, I don't know what could be wrong in your code:

Code: Select all

SceCtrlData pad_data;
sceCtrlSetSamplingCycle(0);
sceCtrlSetSamplingMode(1);
printf("Press HOME to exit\n");
while(1) {
sceCtrlReadBufferPositive(&pad_data, 1);
if (pad_data.Buttons & PSP_CTRL_HOME)
	sceKernelExitGame();
}
User avatar
pailes
Posts: 16
Joined: Fri Feb 16, 2007 4:31 am
Contact:

Post by pailes »

Strangely, I use the same code, it doesn't work. I do not install the exit callback, that's all I do. Is it too much to ask to post a complete example code that I can compile and examine?
jas0nuk
Posts: 137
Joined: Thu Apr 27, 2006 8:00 am

Post by jas0nuk »

main.c:

Code: Select all

#include <pspkernel.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <pspsdk.h>
#include <pspctrl.h>

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

PSP_MODULE_INFO&#40;"test", 0x1000, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;0&#41;;

#define printf pspDebugScreenPrintf

int main&#40;void&#41; &#123;
	// init stuff
	pspDebugScreenInit&#40;&#41;;
	pspDebugScreenClear&#40;&#41;;

	// pad stuff
	SceCtrlData pad_data;
	sceCtrlSetSamplingCycle&#40;0&#41;;
	sceCtrlSetSamplingMode&#40;1&#41;;

	printf&#40;"Press X to start\n\n"&#41;;
	while&#40;1&#41; &#123;
		sceCtrlReadBufferPositive&#40;&pad_data, 1&#41;;
		if &#40;pad_data.Buttons & PSP_CTRL_CROSS&#41; &#123;
			break;
		&#125;
	&#125;
	
	printf&#40;"blah\n"&#41;;

	printf&#40;"\nPress HOME to exit\n"&#41;;
	while&#40;1&#41; &#123;
	sceCtrlReadBufferPositive&#40;&pad_data, 1&#41;;
	if &#40;pad_data.Buttons & PSP_CTRL_HOME&#41;
		sceKernelExitGame&#40;&#41;;
	&#125;

	return 0;
&#125;
makefile:

Code: Select all

TARGET = out
OBJS = main.c

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = test app

INCDIR = 
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $&#40;CFLAGS&#41; -fno-exceptions -fno-rtti
ASFLAGS = $&#40;CFLAGS&#41;

LIBDIR =
LDFLAGS = 
LIBS =

PSPSDK=$&#40;shell psp-config --pspsdk-path&#41;
include $&#40;PSPSDK&#41;/lib/build.mak
Hope this helps.[/code]
User avatar
pailes
Posts: 16
Joined: Fri Feb 16, 2007 4:31 am
Contact:

Post by pailes »

Thanks for the code, it solved my problem, although I don't understand the reason yet why...

In my code I had the following macros:

Code: Select all

PSP_MODULE_INFO&#40;"test", 0, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER | THREAD_ATTR_VFPU&#41;;
which seemed to cause the problem, because when I changed it to

Code: Select all

PSP_MODULE_INFO&#40;"test", 0x1000, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;0&#41;;
it worked ok. Maybe somebody can enlighten me?
jas0nuk
Posts: 137
Joined: Thu Apr 27, 2006 8:00 am

Post by jas0nuk »

This changes it to kernel mode. Maybe kernel mode is needed to detect PSP_CTRL_HOME ?
moonlight
Posts: 567
Joined: Wed Oct 26, 2005 7:46 pm

Post by moonlight »

jas0nuk wrote:This changes it to kernel mode. Maybe kernel mode is needed to detect PSP_CTRL_HOME ?
Yes. Some keys cannot be read by user mode.

That's why there is a vshCtrlReadBufferPositive in vshbridge, to let vsh modules read the same keys as a kernel one.
User avatar
pailes
Posts: 16
Joined: Fri Feb 16, 2007 4:31 am
Contact:

Post by pailes »

Will it make trouble when I compile my program to run in kernel mode?
jas0nuk
Posts: 137
Joined: Thu Apr 27, 2006 8:00 am

Post by jas0nuk »

No, it's often easier to work in kernel mode.
User avatar
pailes
Posts: 16
Joined: Fri Feb 16, 2007 4:31 am
Contact:

Post by pailes »

Cool, thanks for the superfast replies :)
Post Reply