more file endings
[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 IDirectFBEventBuffer *k_buf=NULL;
38
39 char *title_txt="directfb photoshow";
40 char *exp_txt="an image presenter ...";
41 char *hp_txt="http://hackdaworld.dyndns.org";
42 char *msg1_txt="(press key to continue, space pauses, left key for last image, esc quits ...)";
43
44 void clear_primary(void) {
45  DFBCHECK(primary->SetColor(primary,0x00,0x00,0x00,0x00));
46  DFBCHECK(primary->FillRectangle(primary,0,0,screen_width,screen_height));
47 }
48
49 int main(int argc,char **argv) {
50  int i,str_width;
51  DFBInputEvent k_event;
52  DFBSurfaceDescription dsc;
53  DFBFontDescription font_dsc;
54  IDirectFBImageProvider *img_prov;
55
56  DFBCHECK(DirectFBInit (&argc, &argv));
57
58  DFBCHECK(DirectFBCreate(&dfb));
59  DFBCHECK(dfb->SetCooperativeLevel(dfb,DFSCL_FULLSCREEN));
60
61  dsc.flags=DSDESC_CAPS;
62  dsc.caps=DSCAPS_PRIMARY|DSCAPS_FLIPPING;
63  DFBCHECK(dfb->CreateSurface(dfb,&dsc,&primary));
64
65  DFBCHECK(primary->GetSize(primary,&screen_width,&screen_height));
66  fprintf(stdout,"dimensions: %dx%d\n",screen_width,screen_height);
67
68  DFBCHECK(dfb->GetInputDevice(dfb,DIDID_KEYBOARD,&keyboard));
69  DFBCHECK(keyboard->CreateEventBuffer(keyboard,&k_buf));
70
71  /* title */
72  font_dsc.flags=DFDESC_HEIGHT;
73  font_dsc.height=screen_height/20;
74  DFBCHECK(dfb->CreateFont(dfb,"/usr/share/DFBSee/decker.ttf",&font_dsc,&font));
75  DFBCHECK(primary->SetFont(primary,font));
76  DFBCHECK(font->GetStringWidth(font,title_txt,-1,&str_width));
77  clear_primary();
78  DFBCHECK(primary->SetColor(primary,0x80,0x80,0xff,0xff));
79  DFBCHECK(primary->DrawString(primary,title_txt,-1,(screen_width-str_width)/2,screen_height/4,DSTF_LEFT));
80  /* explanation,homepage,msg */
81  font_dsc.height=screen_height/40;
82  DFBCHECK(dfb->CreateFont(dfb,"/usr/share/DFBSee/decker.ttf",&font_dsc,&font));
83  DFBCHECK(primary->SetFont(primary,font));
84  DFBCHECK(font->GetStringWidth(font,exp_txt,-1,&str_width));
85  DFBCHECK(primary->DrawString(primary,exp_txt,-1,(screen_width-str_width)/2,screen_height/2,DSTF_LEFT));
86  DFBCHECK(font->GetStringWidth(font,hp_txt,-1,&str_width));
87  DFBCHECK(primary->DrawString(primary,hp_txt,-1,(screen_width-str_width)/2,(screen_height/2)+font_dsc.height,DSTF_LEFT));
88  DFBCHECK(font->GetStringWidth(font,msg1_txt,-1,&str_width));
89  DFBCHECK(primary->DrawString(primary,msg1_txt,-1,(screen_width-str_width)/2,(screen_height/2)+3*font_dsc.height,DSTF_LEFT));
90  DFBCHECK(primary->Flip(primary,NULL,DSFLIP_WAITFORSYNC));
91  DFBCHECK(k_buf->WaitForEvent(k_buf));
92
93  /* display pictures */
94  i=2;
95  while(i<argc) {
96   do {
97    printf("image: %s\n",argv[i]);
98    if((int)k_buf->GetEvent(k_buf,DFB_EVENT(&k_event))!=0) {
99     if(k_event.type==DIET_KEYPRESS) {
100      int a;
101      a=(int)k_event.key_id;
102      if(a==(int)DIKI_ESCAPE) {
103       i=argc;
104       break;
105      }
106      if(a==(int)DIKI_SPACE) {
107       char *pause_txt="paused (press button to continue)";
108       DFBCHECK(font->GetStringWidth(font,pause_txt,-1,&str_width));
109       DFBCHECK(primary->SetColor(primary,0x80,0x80,0xff,0xff));
110       DFBCHECK(primary->DrawString(primary,pause_txt,-1,(screen_width-str_width)/2,screen_height/2,DSTF_LEFT));
111       DFBCHECK(primary->Flip(primary,NULL,DSFLIP_WAITFORSYNC));
112       DFBCHECK(k_buf->Reset(k_buf));
113       DFBCHECK(k_buf->WaitForEvent(k_buf));
114       k_event.key_id=DIKI_A; /* no longer pause! */
115      }
116      if(a==(int)DIKI_LEFT) {
117       i=((i<=3)?2:i-2);
118       k_event.key_id=DIKI_A; /* no longer back key! */
119       break;
120      }
121     }
122    }
123    DFBCHECK(dfb->CreateImageProvider(dfb,argv[i],&img_prov));
124    DFBCHECK(img_prov->GetSurfaceDescription(img_prov,&dsc));
125    if((screen_width<dsc.width)||(screen_height<dsc.height)) {
126     float q_w=(float)(dsc.width)/screen_width;
127     float q_h=(float)(dsc.height)/screen_height;
128     if(q_w>=q_h) {
129      dsc.width=(int)(dsc.width/q_w);
130      dsc.height=(int)(dsc.height/q_w);
131     } else {
132      dsc.width=(int)(dsc.width/q_h);
133      dsc.height=(int)(dsc.height/q_h);
134     }
135    }
136    DFBCHECK(dfb->CreateSurface(dfb,&dsc,&logo ));
137    DFBCHECK(img_prov->RenderTo(img_prov,logo,NULL));
138    img_prov->Release(img_prov);
139    clear_primary();
140    DFBCHECK(primary->Blit(primary,logo,NULL,(screen_width-dsc.width)/2,(screen_height-dsc.height)/2));
141    DFBCHECK(primary->Flip(primary,NULL,DSFLIP_WAITFORSYNC));
142    DFBCHECK(k_buf->Reset(k_buf));
143    k_buf->WaitForEventWithTimeout(k_buf,atoi(argv[1]),0);
144    logo->Release(logo);
145    ++i;
146    if(i==argc) break;
147   } while(k_buf->GetEvent(k_buf,DFB_EVENT(&k_event))==DFB_OK);
148  }
149
150  font->Release(font);
151  k_buf->Release(k_buf);
152  keyboard->Release(keyboard);
153  logo->Release(logo);
154  primary->Release(primary);
155  dfb->Release(dfb);
156
157  return 23; /* ;) */
158 }