Mercurial > mplayer.hg
annotate libmenu/menu_txt.c @ 36660:7fd255e0db1b
stream.c: remove pointless NULL check.
Since it currently is a fixed-size array it can never be NULL.
But even if this was changed, this check has no real value:
it should still only be possible in case of an obvious code bug
during development and the crash it would cause would be easy enough
to debug.
author | reimar |
---|---|
date | Sun, 26 Jan 2014 18:59:15 +0000 |
parents | b28f3ff37ae7 |
children |
rev | line source |
---|---|
28113 | 1 /* |
2 * This file is part of MPlayer. | |
3 * | |
4 * MPlayer is free software; you can redistribute it and/or modify | |
5 * it under the terms of the GNU General Public License as published by | |
6 * the Free Software Foundation; either version 2 of the License, or | |
7 * (at your option) any later version. | |
8 * | |
9 * MPlayer is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License along | |
15 * with MPlayer; if not, write to the Free Software Foundation, Inc., | |
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
17 */ | |
8197 | 18 |
16862 | 19 #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
|
20 #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
|
21 #include "help_mp.h" |
8197 | 22 |
23 #include <stdlib.h> | |
24 #include <stdio.h> | |
25 #include <string.h> | |
26 | |
36582
b28f3ff37ae7
Use av_unused for unused arguments instead of various hacks.
reimar
parents:
36151
diff
changeset
|
27 #include "libavutil/attributes.h" |
b28f3ff37ae7
Use av_unused for unused arguments instead of various hacks.
reimar
parents:
36151
diff
changeset
|
28 |
19431
ac69ba536915
Explicitly include libmpcodecs/img_format.h and libvo/fastmemcpy.h.
diego
parents:
17994
diff
changeset
|
29 #include "libmpcodecs/img_format.h" |
ac69ba536915
Explicitly include libmpcodecs/img_format.h and libvo/fastmemcpy.h.
diego
parents:
17994
diff
changeset
|
30 #include "libmpcodecs/mp_image.h" |
8197 | 31 |
16862 | 32 #include "m_struct.h" |
33 #include "m_option.h" | |
8197 | 34 #include "menu.h" |
35 | |
32466
9e627a1793b1
Move font_load.[ch], font_load_ft.c and osd_font.h from libvo to sub.
cigaes
parents:
30957
diff
changeset
|
36 #include "sub/font_load.h" |
16862 | 37 #include "osdep/keycodes.h" |
8197 | 38 |
39 struct menu_priv_s { | |
40 char** lines; | |
41 int num_lines; | |
42 int cur_line; | |
43 int disp_lines; | |
44 int minb; | |
45 int hspace; | |
46 char* file; | |
47 }; | |
48 | |
49 static struct menu_priv_s cfg_dflt = { | |
50 NULL, | |
51 0, | |
52 0, | |
53 0, | |
54 0, | |
55 3, | |
56 NULL | |
57 }; | |
58 | |
59 #define ST_OFF(m) M_ST_OFF(struct menu_priv_s,m) | |
60 | |
30957 | 61 static const m_option_t cfg_fields[] = { |
8197 | 62 { "minbor", ST_OFF(minb), CONF_TYPE_INT, M_OPT_MIN, 0, 0, NULL }, |
63 { "hspace", ST_OFF(hspace), CONF_TYPE_INT, M_OPT_MIN, 0, 0, NULL }, | |
64 { "file", ST_OFF(file), CONF_TYPE_STRING, 0, 0, 0, NULL }, | |
65 { NULL, NULL, NULL, 0,0,0,NULL } | |
66 }; | |
67 | |
68 #define mpriv (menu->priv) | |
69 | |
70 static void read_cmd(menu_t* menu,int cmd) { | |
71 switch(cmd) { | |
72 case MENU_CMD_UP: | |
73 mpriv->cur_line -= mpriv->disp_lines / 2; | |
74 if(mpriv->cur_line < 0) | |
75 mpriv->cur_line = 0; | |
76 break; | |
77 case MENU_CMD_DOWN: | |
78 case MENU_CMD_OK: | |
79 mpriv->cur_line += mpriv->disp_lines / 2; | |
80 if(mpriv->cur_line >= mpriv->num_lines) | |
81 mpriv->cur_line = mpriv->num_lines - 1; | |
82 break; | |
23367 | 83 case MENU_CMD_LEFT: |
8197 | 84 case MENU_CMD_CANCEL: |
85 menu->show = 0; | |
86 menu->cl = 1; | |
87 break; | |
25263
96d0992c7920
Remove hardcoded key->cmd bindings in libmenu, support custom key bindings
ulion
parents:
23367
diff
changeset
|
88 case MENU_CMD_HOME: |
8197 | 89 mpriv->cur_line = 0; |
90 break; | |
25263
96d0992c7920
Remove hardcoded key->cmd bindings in libmenu, support custom key bindings
ulion
parents:
23367
diff
changeset
|
91 case MENU_CMD_END: |
8197 | 92 mpriv->cur_line = mpriv->num_lines - 1; |
93 break; | |
25263
96d0992c7920
Remove hardcoded key->cmd bindings in libmenu, support custom key bindings
ulion
parents:
23367
diff
changeset
|
94 case MENU_CMD_PAGE_UP: |
8197 | 95 mpriv->cur_line = mpriv->cur_line > mpriv->disp_lines ? |
96 mpriv->cur_line - mpriv->disp_lines : 0; | |
97 break; | |
25263
96d0992c7920
Remove hardcoded key->cmd bindings in libmenu, support custom key bindings
ulion
parents:
23367
diff
changeset
|
98 case MENU_CMD_PAGE_DOWN: |
8197 | 99 mpriv->cur_line = mpriv->cur_line + mpriv->disp_lines > mpriv->num_lines - 1 ? mpriv->num_lines - 1 : mpriv->cur_line + mpriv->disp_lines; |
100 break; | |
101 } | |
102 } | |
103 | |
104 | |
105 static void draw(menu_t* menu,mp_image_t* mpi) { | |
106 int x = mpriv->minb; | |
107 int y = mpriv->minb; | |
108 //int th = 2*mpriv->hspace + vo_font->height; | |
109 int i,end; | |
110 | |
111 if(x < 0) x = 8; | |
112 if(y < 0) y = 8; | |
113 | |
114 mpriv->disp_lines = (mpi->h + mpriv->hspace - 2*mpriv->minb) / ( vo_font->height + mpriv->hspace); | |
115 if(mpriv->num_lines - mpriv->cur_line < mpriv->disp_lines) { | |
116 i = mpriv->num_lines - 1 - mpriv->disp_lines; | |
117 if(i < 0) i = 0; | |
118 end = mpriv->num_lines - 1; | |
119 } else { | |
120 i = mpriv->cur_line; | |
121 end = i + mpriv->disp_lines; | |
122 if(end >= mpriv->num_lines) end = mpriv->num_lines - 1; | |
123 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28113
diff
changeset
|
124 |
8197 | 125 for( ; i < end ; i++) { |
126 menu_draw_text(mpi,mpriv->lines[i],x,y); | |
127 y += vo_font->height + mpriv->hspace; | |
128 } | |
129 | |
130 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28113
diff
changeset
|
131 |
8197 | 132 #define BUF_SIZE 1024 |
133 | |
36582
b28f3ff37ae7
Use av_unused for unused arguments instead of various hacks.
reimar
parents:
36151
diff
changeset
|
134 static int open_txt(menu_t* menu, char* av_unused args) { |
8197 | 135 FILE* fd; |
136 char buf[BUF_SIZE]; | |
137 char *l; | |
138 int s; | |
139 int pos = 0, r = 0; | |
140 | |
141 menu->draw = draw; | |
142 menu->read_cmd = read_cmd; | |
143 | |
144 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
|
145 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_MenuTxtNeedATxtFileName); |
8197 | 146 return 0; |
147 } | |
148 | |
149 fd = fopen(mpriv->file,"r"); | |
150 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
|
151 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_MenuTxtCantOpen,mpriv->file); |
8197 | 152 return 0; |
153 } | |
154 | |
155 while(1) { | |
156 r = fread(buf+pos,1,BUF_SIZE-pos-1,fd); | |
36151 | 157 if (r > 0) pos += r; |
158 buf[pos] = '\0'; | |
8197 | 159 if(r <= 0) { |
160 if(pos > 0) { | |
161 mpriv->lines = realloc(mpriv->lines,(mpriv->num_lines + 1)*sizeof(char*)); | |
162 mpriv->lines[mpriv->num_lines] = strdup(buf); | |
163 mpriv->num_lines++; | |
164 } | |
165 fclose(fd); | |
166 break; | |
167 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28113
diff
changeset
|
168 |
8197 | 169 while((l = strchr(buf,'\n')) != NULL) { |
170 s = l-buf; | |
171 mpriv->lines = realloc(mpriv->lines,(mpriv->num_lines + 1)*sizeof(char*)); | |
172 mpriv->lines[mpriv->num_lines] = malloc(s+1); | |
173 memcpy(mpriv->lines[mpriv->num_lines],buf,s); | |
174 mpriv->lines[mpriv->num_lines][s] = '\0'; | |
175 pos -= s + 1; | |
176 if(pos > 0) | |
177 memmove(buf,l+1,pos); | |
178 buf[pos] = '\0'; | |
179 mpriv->num_lines++; | |
180 } | |
181 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
|
182 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_WarningTooLongLineSplitting); |
8197 | 183 mpriv->lines = realloc(mpriv->lines,(mpriv->num_lines + 1)*sizeof(char*)); |
184 mpriv->lines[mpriv->num_lines] = strdup(buf); | |
185 mpriv->num_lines++; | |
186 pos = 0; | |
187 } | |
188 } | |
189 | |
17994
6927fabaef92
Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents:
16862
diff
changeset
|
190 mp_msg(MSGT_GLOBAL,MSGL_INFO,MSGTR_LIBMENU_ParsedLines,mpriv->num_lines); |
8197 | 191 |
192 return 1; | |
193 } | |
194 | |
195 const menu_info_t menu_info_txt = { | |
196 "Text file viewer", | |
197 "txt", | |
198 "Albeu", | |
199 "", | |
200 { | |
201 "txt_cfg", | |
202 sizeof(struct menu_priv_s), | |
203 &cfg_dflt, | |
204 cfg_fields | |
205 }, | |
23366
b344b6520518
rename some menu open functions, to avoid confusion with libc native open()
ben
parents:
19431
diff
changeset
|
206 open_txt, |
8197 | 207 }; |