annotate libmenu/menu_txt.c @ 9380:edfe34c5405d

linux->osdep
author arpi
date Sun, 09 Feb 2003 20:18:23 +0000
parents b31caec933e9
children 931bdbc37ee0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
1
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
2 #include "../config.h"
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
3
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
4 #include <stdlib.h>
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
5 #include <stdio.h>
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
6 #include <string.h>
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
7
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
8 #include "img_format.h"
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
9 #include "mp_image.h"
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
10
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
11 #include "../m_struct.h"
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
12 #include "../m_option.h"
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
13 #include "menu.h"
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
14
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
15 #include "../libvo/font_load.h"
9380
edfe34c5405d linux->osdep
arpi
parents: 8197
diff changeset
16 #include "../osdep/keycodes.h"
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
17
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
18 struct menu_priv_s {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
19 char** lines;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
20 int num_lines;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
21 int cur_line;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
22 int disp_lines;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
23 int minb;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
24 int hspace;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
25 char* file;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
26 };
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
27
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
28 static struct menu_priv_s cfg_dflt = {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
29 NULL,
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
30 0,
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
31 0,
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
32 0,
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
33 0,
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
34 3,
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
35 NULL
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
36 };
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
37
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
38 #define ST_OFF(m) M_ST_OFF(struct menu_priv_s,m)
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
39
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
40 static m_option_t cfg_fields[] = {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
41 { "minbor", ST_OFF(minb), CONF_TYPE_INT, M_OPT_MIN, 0, 0, NULL },
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
42 { "hspace", ST_OFF(hspace), CONF_TYPE_INT, M_OPT_MIN, 0, 0, NULL },
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
43 { "file", ST_OFF(file), CONF_TYPE_STRING, 0, 0, 0, NULL },
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
44 { NULL, NULL, NULL, 0,0,0,NULL }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
45 };
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
46
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
47 #define mpriv (menu->priv)
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
48
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
49 static void read_cmd(menu_t* menu,int cmd) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
50 switch(cmd) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
51 case MENU_CMD_UP:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
52 mpriv->cur_line -= mpriv->disp_lines / 2;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
53 if(mpriv->cur_line < 0)
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
54 mpriv->cur_line = 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
55 break;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
56 case MENU_CMD_DOWN:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
57 case MENU_CMD_OK:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
58 mpriv->cur_line += mpriv->disp_lines / 2;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
59 if(mpriv->cur_line >= mpriv->num_lines)
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
60 mpriv->cur_line = mpriv->num_lines - 1;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
61 break;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
62 case MENU_CMD_CANCEL:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
63 menu->show = 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
64 menu->cl = 1;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
65 break;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
66 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
67 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
68
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
69 static void read_key(menu_t* menu,int c) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
70 switch (c) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
71 case KEY_HOME:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
72 mpriv->cur_line = 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
73 break;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
74 case KEY_END:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
75 mpriv->cur_line = mpriv->num_lines - 1;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
76 break;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
77 case KEY_PAGE_UP:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
78 mpriv->cur_line = mpriv->cur_line > mpriv->disp_lines ?
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
79 mpriv->cur_line - mpriv->disp_lines : 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
80 break;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
81 case KEY_PAGE_DOWN:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
82 mpriv->cur_line = mpriv->cur_line + mpriv->disp_lines > mpriv->num_lines - 1 ? mpriv->num_lines - 1 : mpriv->cur_line + mpriv->disp_lines;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
83 break;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
84 default:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
85 menu_dflt_read_key(menu,c);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
86 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
87 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
88
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
89
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
90 static void draw(menu_t* menu,mp_image_t* mpi) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
91 int x = mpriv->minb;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
92 int y = mpriv->minb;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
93 //int th = 2*mpriv->hspace + vo_font->height;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
94 int i,end;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
95
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
96 if(x < 0) x = 8;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
97 if(y < 0) y = 8;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
98
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
99 mpriv->disp_lines = (mpi->h + mpriv->hspace - 2*mpriv->minb) / ( vo_font->height + mpriv->hspace);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
100 if(mpriv->num_lines - mpriv->cur_line < mpriv->disp_lines) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
101 i = mpriv->num_lines - 1 - mpriv->disp_lines;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
102 if(i < 0) i = 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
103 end = mpriv->num_lines - 1;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
104 } else {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
105 i = mpriv->cur_line;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
106 end = i + mpriv->disp_lines;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
107 if(end >= mpriv->num_lines) end = mpriv->num_lines - 1;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
108 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
109
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
110 for( ; i < end ; i++) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
111 menu_draw_text(mpi,mpriv->lines[i],x,y);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
112 y += vo_font->height + mpriv->hspace;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
113 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
114
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
115 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
116
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
117 #define BUF_SIZE 1024
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
118
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
119 static int open(menu_t* menu, char* args) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
120 FILE* fd;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
121 char buf[BUF_SIZE];
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
122 char *l;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
123 int s;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
124 int pos = 0, r = 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
125 args = NULL; // Warning kill
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
126
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
127 menu->draw = draw;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
128 menu->read_cmd = read_cmd;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
129 menu->read_key = read_key;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
130
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
131 if(!mpriv->file) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
132 printf("Menu txt need a txt file name (param file)\n");
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
133 return 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
134 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
135
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
136 fd = fopen(mpriv->file,"r");
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
137 if(!fd) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
138 printf("Menu txt can't open: %s\n",mpriv->file);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
139 return 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
140 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
141
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
142 while(1) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
143 r = fread(buf+pos,1,BUF_SIZE-pos-1,fd);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
144 if(r <= 0) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
145 if(pos > 0) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
146 mpriv->lines = realloc(mpriv->lines,(mpriv->num_lines + 1)*sizeof(char*));
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
147 mpriv->lines[mpriv->num_lines] = strdup(buf);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
148 mpriv->num_lines++;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
149 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
150 fclose(fd);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
151 break;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
152 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
153 pos += r;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
154 buf[pos] = '\0';
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
155
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
156 while((l = strchr(buf,'\n')) != NULL) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
157 s = l-buf;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
158 mpriv->lines = realloc(mpriv->lines,(mpriv->num_lines + 1)*sizeof(char*));
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
159 mpriv->lines[mpriv->num_lines] = malloc(s+1);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
160 memcpy(mpriv->lines[mpriv->num_lines],buf,s);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
161 mpriv->lines[mpriv->num_lines][s] = '\0';
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
162 pos -= s + 1;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
163 if(pos > 0)
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
164 memmove(buf,l+1,pos);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
165 buf[pos] = '\0';
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
166 mpriv->num_lines++;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
167 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
168 if(pos >= BUF_SIZE-1) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
169 printf("Warning too long line, splitting it\n");
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
170 mpriv->lines = realloc(mpriv->lines,(mpriv->num_lines + 1)*sizeof(char*));
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
171 mpriv->lines[mpriv->num_lines] = strdup(buf);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
172 mpriv->num_lines++;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
173 pos = 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
174 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
175 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
176
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
177 printf("Parsed %d lines\n",mpriv->num_lines);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
178
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
179 return 1;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
180 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
181
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
182 const menu_info_t menu_info_txt = {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
183 "Text file viewer",
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
184 "txt",
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
185 "Albeu",
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
186 "",
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
187 {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
188 "txt_cfg",
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
189 sizeof(struct menu_priv_s),
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
190 &cfg_dflt,
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
191 cfg_fields
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
192 },
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
193 open,
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
194 };