Viewimage.c
- include <sys/types.h>
- include <sys/stat.h>
- include <unistd.h>
- include <stdlib.h>
- include <string.h>
- include <stdio.h>
- include <errno.h>
- include <fcntl.h>
- include "SDL.h"
- include "SDL_image.h"
- define VIEWIMAGE_SPLASH_PNG "splashimage.png"
- define FIFO_NAME "viewimage-progress"
- define FIFO_MODE 0777
- define FIFO_BUFFER_SIZE 300
- define GUI_MAXICONS 2
- define VIEWIMAGE_USE_FIFO false
SDL_Event event;
char *progress[GUI_MAXICONS] = { "harddisk-head.png", "foo.png" };
// load a png file into an SDL surface
// return a pointer to the surface object
SDL_Surface * LoadImage (char *filename) {
SDL_Surface * new_image;
// load the image new_image = IMG_Load( filename ); if(!new_image) { // image failed to load printf ("LoadImage: %s\n", IMG_GetError()); SDL_Quit(); return NULL; }
return new_image;
}
void show_progress( int p ) { // blank the progress display area
// display the number of pictograms based on p }
// take the message text and take action void parse_message( char *msg ) { printf("Message recieved: %s", msg);
if(strcmp(msg, "partition\n") == 0) { printf("Partitioning complete\n"); show_progress(1); } }
int main(int argc, char **argv) {
int i, done = 0; int screen_width = 1024; int screen_height = 768; int bpp = 24;
char s[FIFO_BUFFER_SIZE]; int num, fd;
SDL_Surface * image, * screen; SDL_Surface *progress_icon[GUI_MAXICONS];
for(i = 0; i<GUI_MAXICONS; i++) {
progress_icon[i] = LoadImage(progress[i]);
}
// set up FIFO
if(!mknod(FIFO_NAME, S_IFIFO | FIFO_MODE, 0))
perror("Couldn't open FIFO");
// startup SDL
if(SDL_Init(SDL_INIT_VIDEO)==-1) { printf("SDL_Init: %s\n", SDL_GetError()); return 1; }
// open the screen for displaying the image
screen = SDL_SetVideoMode ( screen_width, screen_height, bpp, SDL_HWPALETTE | SDL_ANYFORMAT );
// report errors if this failed for some reason if (!screen) { printf("SDL_SetVideoMode: %s\n", SDL_GetError()); SDL_Quit(); return 1; } // hide mouse pointer SDL_ShowCursor(SDL_DISABLE);
// load image image = LoadImage(VIEWIMAGE_SPLASH_PNG); if(!image) return 1;
// fill the screen SDL_FillRect (screen, NULL, SDL_MapRGB (screen->format, 0, 0, 0));
// blit it out SDL_BlitSurface (image, 0, screen, 0); SDL_Flip (screen);
num = 0;
// open the fifo non-blocking fd = open(FIFO_NAME, O_RDONLY | O_NONBLOCK); if(!fd) { perror("fifo open() falied"); exit(0); }
while(!done) {
while(SDL_PollEvent(&event)) { /* Loop until there are no events left on the queue */ switch(event.type) { /* Process the appropiate event type */ case SDL_QUIT: done = 1; break; case SDL_KEYDOWN: /* Handle a KEYDOWN event */ if(event.key.keysym.sym == SDLK_F1) printf("You pressed F1\n"); else if(event.key.keysym.sym == SDLK_F2) done = 1; break; } }
// read from pipe non-blocking num = read(fd, s, FIFO_BUFFER_SIZE);
// if data was read terminate and parse if(num > 0) { s[num] = '\0'; parse_message(s); }
} // close pipe close(fd);
// free the loaded image SDL_FreeSurface ( image );
// shutdown SDL SDL_Quit();
return 0; }