Intial commit
[profile/ivi/w3m.git] / w3mimg / fb / fb_w3mimg.c
1 /* $Id: fb_w3mimg.c,v 1.14 2004/08/04 17:32:28 ukai Exp $ */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <ctype.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10
11 #include "w3mimg/fb/fb.h"
12 #include "w3mimg/fb/fb_img.h"
13 #include "w3mimg/w3mimg.h"
14
15 static int
16 w3mfb_init(w3mimg_op * self)
17 {
18     if (self == NULL)
19         return 0;
20     /* XXX */
21     return 1;
22 }
23
24 static int
25 w3mfb_finish(w3mimg_op * self)
26 {
27     if (self == NULL)
28         return 0;
29     return 1;
30 }
31
32 static int
33 w3mfb_active(w3mimg_op * self)
34 {
35     if (self == NULL)
36         return 0;
37     return 1;
38 }
39
40 static void
41 w3mfb_set_background(w3mimg_op * self, char *background)
42 {
43     if (self == NULL)
44         return;
45     if (background) {
46         int r, g, b;
47         if (sscanf(background, "#%02x%02x%02x", &r, &g, &b) == 3)
48             fb_image_set_bg(r, g, b);
49     }
50 }
51
52 static void
53 w3mfb_sync(w3mimg_op * self)
54 {
55     return;
56 }
57
58 static void
59 w3mfb_close(w3mimg_op * self)
60 {
61     fb_close();
62 }
63
64 static int
65 w3mfb_clear(w3mimg_op * self, int x, int y, int w, int h)
66 {
67     if (self == NULL)
68         return 0;
69     fb_image_clear(x, y, w, h);
70     return 1;
71 }
72
73 static int
74 w3mfb_load_image(w3mimg_op * self, W3MImage * img, char *fname, int w, int h)
75 {
76     FB_IMAGE **im;
77
78     if (self == NULL)
79         return 0;
80     im = fb_image_load(fname, w, h, self->max_anim);
81     if (!im)
82         return 0;
83     img->pixmap = im;
84     img->width = im[0]->width;
85     img->height = im[0]->height;
86     return 1;
87 }
88
89 static int
90 w3mfb_show_image(w3mimg_op * self, W3MImage * img, int sx, int sy,
91                  int sw, int sh, int x, int y)
92 {
93     int i;
94     FB_IMAGE **frame;
95 #define WAIT_CNT 4
96
97     if (self == NULL)
98         return 0;
99
100     if (img->pixmap == NULL)
101         return 0;
102
103     frame = (FB_IMAGE **) img->pixmap;
104     i = frame[0]->id;
105     fb_image_draw(frame[i],
106                   x + self->offset_x, y + self->offset_y,
107                   sx, sy, (sw ? sw : img->width), (sh ? sh : img->height));
108     if (frame[0]->num > 1) {
109         if (frame[1]->id > WAIT_CNT) {
110             frame[1]->id = 0;
111             if (i < frame[0]->num - 1)
112                 frame[0]->id = i + 1;
113             else
114                 frame[0]->id = 0;
115         }
116         frame[1]->id += 1;
117     }
118     return 1;
119 }
120
121 static void
122 w3mfb_free_image(w3mimg_op * self, W3MImage * img)
123 {
124     if (self == NULL)
125         return;
126     if (img && img->pixmap) {
127         fb_frame_free((FB_IMAGE **) img->pixmap);
128         img->pixmap = NULL;
129         img->width = 0;
130         img->height = 0;
131     }
132 }
133
134 static int
135 w3mfb_get_image_size(w3mimg_op * self, W3MImage * img,
136                      char *fname, int *w, int *h)
137 {
138     int i;
139
140     if (self == NULL)
141         return 0;
142     i = get_image_size(fname, w, h);
143     if (i)
144         return 0;
145     return 1;
146 }
147
148 #ifdef W3MIMGDISPLAY_SETUID
149 static int
150 check_tty_console(char *tty)
151 {
152     if (tty == NULL || *tty == '\0')
153         return 0;
154     if (strncmp(tty, "/dev/", 5) == 0)
155         tty += 5;
156     if (strncmp(tty, "tty", 3) == 0 && isdigit(*(tty + 3)))
157         return 1;
158     if (strncmp(tty, "vc/", 3) == 0 && isdigit(*(tty + 3)))
159         return 1;
160     return 0;
161 }
162 #else
163 #define check_tty_console(tty)  1
164 #endif
165
166 w3mimg_op *
167 w3mimg_fbopen()
168 {
169     w3mimg_op *wop = NULL;
170     wop = (w3mimg_op *) malloc(sizeof(w3mimg_op));
171     if (wop == NULL)
172         return NULL;
173     memset(wop, 0, sizeof(w3mimg_op));
174
175     if (!check_tty_console(getenv("W3M_TTY")) && strcmp("jfbterm", getenv("TERM")) != 0) {
176         fprintf(stderr, "w3mimgdisplay/fb: tty is not console\n");
177         goto error;
178     }
179
180     if (fb_open())
181         goto error;
182
183     wop->width = fb_width();
184     wop->height = fb_height();
185
186     wop->init = w3mfb_init;
187     wop->finish = w3mfb_finish;
188     wop->active = w3mfb_active;
189     wop->set_background = w3mfb_set_background;
190     wop->sync = w3mfb_sync;
191     wop->close = w3mfb_close;
192     wop->clear = w3mfb_clear;
193
194     wop->load_image = w3mfb_load_image;
195     wop->show_image = w3mfb_show_image;
196     wop->free_image = w3mfb_free_image;
197     wop->get_image_size = w3mfb_get_image_size;
198
199     /* XXX */
200     fb_image_init();
201
202     return wop;
203   error:
204     free(wop);
205     return NULL;
206 }