corrected while loop
[my-code/dfb-photoshow.git] / dfb-photoshow.c
1 /*
2  * dfb-photoshow.c
3  *
4  * build: gcc -o dfb-photoshow -O3 -Wall -I/usr/X11/include \
5  *        -I/usr/include/directfb dfb-photoshow.c
6  * usage: ./showdir <seconds> <directory>
7  *
8  * author: 
9  * hackbard@hackdaworld.dyndns.org
10  * download: 
11  * cvs -d:pserver:anonymous@hackdaworld.dyndns.org:/my-code co dfb-photoshow
12  *
13  */
14
15 #include <stdio.h>
16 #include <unistd.h>
17 #include <stdlib.h>
18 #include <directfb.h>
19
20 IDirectFB *dfb = NULL;
21 IDirectFBSurface *primary = NULL;
22 int screen_width  = 0;
23 int screen_height = 0;
24
25 #define DFBCHECK(x...) \
26 { \
27  DFBResult err=x; \
28  if(err!=DFB_OK) { \
29   fprintf(stderr,"file: %s , line: %d !\n\t",__FILE__,__LINE__); \
30   DirectFBErrorFatal( #x, err ); \
31  } \
32 }
33
34 IDirectFBInputDevice *keyboard=NULL;
35 IDirectFBSurface *logo=NULL;
36 IDirectFBFont *font=NULL;
37 char *title_txt="directfb photoshow";
38 char *author_txt="hackbard@hackdaworld.dyndns.org";
39 char *hp_txt="http://hackdaworld.dyndns.org";
40 char *msg1_txt="(press return to continue ...)";
41
42 void clear_primary(void) {
43  DFBCHECK(primary->SetColor(primary,0x00,0x00,0x00,0x00));
44  DFBCHECK(primary->FillRectangle(primary,0,0,screen_width,screen_height));
45 }
46
47 int main(int argc,char **argv) {
48  int i,str_width;
49  DFBInputDeviceKeyState state_enter=DIKS_UP;
50  DFBInputDeviceKeyState state_escape=DIKS_UP;
51  DFBSurfaceDescription dsc;
52  DFBFontDescription font_dsc;
53  IDirectFBImageProvider *img_prov;
54
55  DFBCHECK(DirectFBInit (&argc, &argv));
56
57  DFBCHECK(DirectFBCreate(&dfb));
58  DFBCHECK(dfb->SetCooperativeLevel(dfb,DFSCL_FULLSCREEN));
59
60  dsc.flags=DSDESC_CAPS;
61  dsc.caps=DSCAPS_PRIMARY|DSCAPS_FLIPPING;
62  DFBCHECK(dfb->CreateSurface(dfb,&dsc,&primary));
63
64  DFBCHECK(primary->GetSize(primary,&screen_width,&screen_height));
65  fprintf(stdout,"dimensions: %dx%d\n",screen_width,screen_height);
66
67  DFBCHECK(dfb->GetInputDevice(dfb,DIDID_KEYBOARD,&keyboard));
68
69  /* title */
70  font_dsc.flags=DFDESC_HEIGHT;
71  font_dsc.height=screen_height/20;
72  DFBCHECK(dfb->CreateFont(dfb,"/usr/share/DFBSee/decker.ttf",&font_dsc,&font));
73  DFBCHECK(primary->SetFont(primary,font));
74  DFBCHECK(font->GetStringWidth(font,title_txt,-1,&str_width));
75  clear_primary();
76  DFBCHECK(primary->SetColor(primary,0x80,0x80,0xff,0xff));
77  DFBCHECK(primary->DrawString(primary,title_txt,-1,(screen_width-str_width)/2,screen_height/4,DSTF_LEFT));
78  /* author,homepage,msg */
79  font_dsc.height=screen_height/40;
80  DFBCHECK(dfb->CreateFont(dfb,"/usr/share/DFBSee/decker.ttf",&font_dsc,&font));
81  DFBCHECK(primary->SetFont(primary,font));
82  DFBCHECK(font->GetStringWidth(font,author_txt,-1,&str_width));
83  DFBCHECK(primary->DrawString(primary,author_txt,-1,(screen_width-str_width)/2,screen_height/2,DSTF_LEFT));
84  DFBCHECK(font->GetStringWidth(font,hp_txt,-1,&str_width));
85  DFBCHECK(primary->DrawString(primary,hp_txt,-1,(screen_width-str_width)/2,(screen_height/2)+font_dsc.height,DSTF_LEFT));
86  DFBCHECK(font->GetStringWidth(font,msg1_txt,-1,&str_width));
87  DFBCHECK(primary->DrawString(primary,msg1_txt,-1,(screen_width-str_width)/2,(screen_height/2)+3*font_dsc.height,DSTF_LEFT));
88  DFBCHECK(primary->Flip(primary,NULL,DSFLIP_WAITFORSYNC));
89  DFBCHECK(keyboard->GetKeyState(keyboard,DIKI_ESCAPE,&state_enter));
90  while(state_enter==DIKS_UP) {
91   DFBCHECK(keyboard->GetKeyState(keyboard,DIKI_ENTER,&state_enter));
92  }
93  font->Release(font);
94
95  /* display pictures */
96  i=2;
97  while(state_escape==DIKS_UP) { 
98   DFBCHECK(dfb->CreateImageProvider(dfb,argv[i],&img_prov));
99   DFBCHECK(img_prov->GetSurfaceDescription(img_prov,&dsc));
100   DFBCHECK(dfb->CreateSurface(dfb,&dsc,&logo ));
101   DFBCHECK(img_prov->RenderTo(img_prov,logo,NULL));
102   img_prov->Release(img_prov);
103   DFBCHECK (primary->SetColor(primary,0x00,0x00,0x00,0x00));
104   DFBCHECK(primary->FillRectangle(primary,0,0,screen_width,screen_height));
105   DFBCHECK(primary->SetColor(primary,0x80,0x80,0xff,0xff));
106   DFBCHECK(primary->DrawLine(primary,0,0,screen_width-1,screen_height-1));
107   if((screen_width>=dsc.width)&&(screen_height>=dsc.height)) {
108    DFBCHECK(primary->Blit(primary,logo,NULL,(screen_width-dsc.width)/2,(screen_height-dsc.height)/2));
109   }
110   else {
111    DFBCHECK(primary->StretchBlit(primary,logo,NULL,NULL));
112   }
113   DFBCHECK(primary->Flip(primary,NULL,DSFLIP_WAITFORSYNC));
114   sleep(atoi(argv[1]));
115   logo->Release(logo);
116   DFBCHECK(keyboard->GetKeyState(keyboard,DIKI_ESCAPE,&state_escape));
117   i++;
118   if(i==argc) state_escape=DIKS_DOWN;
119  }
120
121
122  keyboard->Release(keyboard);
123  logo->Release(logo);
124  primary->Release(primary);
125  dfb->Release(dfb);
126
127  return 23; /* ;) */
128 }
129