annotate libmenu/menu_txt.c @ 18077:cede662f40a5

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