How to write your own hijack app

Posted by: jane

How to write your own hijack app - 07/10/2002 11:09

This is something that should go in the Hijack FAQ when it is properly commented here...

How do I write a program for the hijack menu?
(My examples are mainly taken from empacman)

This program should write a "hello world" on the screen.

After getting (making?) an ARM-cross-compiler (see Mark Lord's hijack page for how) you need the hijack.h and empeg.h files. The only way to get these is to download from MArk's page the linux-tree and all the patches and patch the tree, pick out these *.h files and delete the rest of the three.

Compile the program with

gcc vfdlib.c helloworld.c -o helloworld.

Transfer helloworld to the empeg, chmod +x and then run it before the player application is run, using one of the pre-init methods.
When you now acess the hijack-menu, you see another menu item, and when you select that, you get "Hello World" on screen for a while (60sec? 60min?)

Needs a little "niceness" of the code, I cut'n pasted from my files.

/*Includes needed: (maybe not all are needed?)*/
#include <stdlib.h>
#include <sys/ioctl.h>
#include "hijack.h"
#include <time.h>
#include <sys/time.h>
#include <linux/errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include "vfdlib.h"
/* hijack.h and vfdlib.h must be aquired from their sources*/
int main(){
/*Variables needed:*/
unsigned char screenbuf[EMPEG_SCREEN_BYTES] = {0,};
hijack_geom_t geom= {Xmin,Xmax,Ymin,Ymax}; /* the size of your "window" on screen when the player is running */

int g_fd, rc; /* a file descriptor and a return code */

/* you need to fork...*/
if(fork() != 0) {
return 0;
}
else {

int pid = getpid();

printf("myprog running as pid %d\n", pid);

/* open the display */
g_fd = open("/dev/display", O_RDWR);
while (1) {
/* Bind to emepg menu and stay there */
char *menulabels[] = {"helloworld", NULL};
rc = ioctl(g_fd, EMPEG_HIJACK_WAITMENU, menulabels);
if (rc < 0) {
perror("failed EMPEG_HIJACK_WAITMENU");
exit(1);
}
/* tell Hijack about our screen requirements */
rc = ioctl(g_fd, EMPEG_HIJACK_SETGEOM, &geom);
/* we need a font */
vfdlib_registerFont("/empeg/lib/fonts/small.bf", 0);
/* documentation of the vfdlib functions are in the library code :-*/
vfdlib_fastclear(screenbuf);
vfdlib_registerFont("/empeg/lib/fonts/small.bf", 0);
vfdlib_drawText(screenbuf, "Hello World!", 0, vfdlib_getTextHeight(0), 1, -1);

/* Now push the screenbuf to our hijack-allocated space */
rc = ioctl(g_fd, EMPEG_HIJACK_DISPWRITE, screenbuf);

/* Display the message a while */
sleep(60);
/* clear up memory */
vfdlib_unregisterAllFonts();

}



Posted by: jaharkes

Re: How to write your own hijack app - 07/10/2002 11:29

Another thing that you could add is a chdir("/"); which avoids having the program's cwd prevent the filesystem where the app was started from remounting. Not sure whether this completely fixes the problems people see during synchronization, but it should be at least a step in the right direction.

Another thing, you could look at is gpsapp. I wrapped several of the hijack ioctls in such a way that it gives an empeg application framework. When you compile natively on a UNIX system, it even gives you a small X11 window. Very useful for debugging and during development because you don't have to run gdb on the empeg all that often, and it is trivial to test for memory problems with programs like valgrind.
Posted by: tms13

Re: How to write your own hijack app - 08/10/2002 02:51

In reply to:

/*Includes needed: (maybe not all are needed?)*/
#include <stdlib.h>
#include <sys/ioctl.h>
#include "hijack.h"
#include <time.h>
#include <sys/time.h>
#include <linux/errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include "vfdlib.h"


Well, you only need to include <stdlib.h> once...

In the absence of dependencies, it's probably best to include standard headers first, then sys/, linux/, and local (quotes rather than angles) headers in that order.