1 /* vifm
2  * Copyright (C) 2001 Ken Steen.
3  * Copyright (C) 2011 xaizek.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19 
20 #ifndef VIFM__STATUS_H__
21 #define VIFM__STATUS_H__
22 
23 #include <stdint.h> /* uint64_t */
24 #include <stdio.h> /* FILE */
25 #include <time.h> /* time_t */
26 
27 #include "compat/fs_limits.h"
28 #include "ui/color_scheme.h"
29 #include "utils/hist.h"
30 #include "filetype.h"
31 
32 /* Special value foe dcache fields meaning that it wasn't set. */
33 #define DCACHE_UNKNOWN ((uint64_t)-1)
34 
35 struct config_t;
36 struct dir_entry_t;
37 
38 /* Orientation of a split. */
39 typedef enum
40 {
41 	HSPLIT, /* Top and bottom panes. */
42 	VSPLIT, /* Left and right panes. */
43 }
44 SPLIT;
45 
46 typedef enum
47 {
48 	SOURCING_NONE,
49 	SOURCING_PROCESSING,
50 	SOURCING_FINISHING,
51 }
52 SourcingState;
53 
54 /* Type of execution environment. */
55 typedef enum
56 {
57 	EET_LINUX_NATIVE,    /* Linux native console. */
58 	EET_EMULATOR,        /* Terminal emulator with no DISPLAY defined. */
59 	EET_EMULATOR_WITH_X, /* Terminal emulator within accessible X. */
60 }
61 ExecEnvType;
62 
63 /* List of terminal multiplexers. */
64 typedef enum
65 {
66 	TM_NONE,   /* Plain console. */
67 	TM_SCREEN, /* GNU screen. */
68 	TM_TMUX,   /* tmux. */
69 }
70 TermMultiplexer;
71 
72 /* Types of updates that can be performed by update_screen(). */
73 typedef enum
74 {
75 	UT_NONE,   /* No update needed. */
76 	UT_REDRAW, /* Screen redraw requested. */
77 	UT_FULL,   /* File lists reload (when visible) followed by screen redraw
78 	              requested. */
79 }
80 UpdateType;
81 
82 /* Possible states of terminal with regard to its size. */
83 typedef enum
84 {
85 	TS_NORMAL,         /* OK to draw UI. */
86 	TS_TOO_SMALL,      /* Too small terminal. */
87 	TS_BACK_TO_NORMAL, /* Was too small some moments ago, need to restore UI. */
88 }
89 TermState;
90 
91 typedef enum
92 {
93 	/* Shell that is aware of command escaping and backslashes in paths. */
94 	ST_NORMAL,
95 	/* Dumb cmd.exe shell on Windows. */
96 	ST_CMD,
97 	/* PowerShell on Windows. (Any less dumb?  I wish...) */
98 	ST_PS,
99 }
100 ShellType;
101 
102 /* Type of output variables of dcache_get_of(), which represent state of cache
103  * entries. */
104 typedef struct
105 {
106 	uint64_t value; /* Cached value or DCACHE_UNKNOWN. */
107 	int is_valid;   /* Whether value is up-to-date. */
108 }
109 dcache_result_t;
110 
111 /* Current preview (quickview) parameters. */
112 typedef struct
113 {
114 	char *cleanup_cmd;              /* Cleanup command. */
115 	struct modview_info_t *explore; /* State of explored quick view. */
116 	unsigned int on : 1;            /* Whether preview mode is active.  Use
117 	                                   stats_set_quickview() to change the
118 	                                   value. */
119 	ViewerKind kind : 2;            /* Kind of the preview. */
120 	unsigned int clearing : 1;      /* Whether in process of clearing preview. */
121 }
122 preview_t;
123 
124 typedef struct
125 {
126 	int last_char;
127 	int save_msg; /* zero - don't save, 2 - save after resize, other - save */
128 	int use_register;
129 	int use_input_bar;     /* Whether input bar should be updated. */
130 	int curr_register;
131 	int register_saved;
132 	int number_of_windows;
133 	int drop_new_dir_hist; /* Skip recording of new directory history. */
134 	int load_stage; /* -1 - test, 0 - no TUI, 1 - part of TUI, 2 - TUI, 3 - all */
135 
136 	preview_t preview; /* State of preview (quickview). */
137 
138 	/* Describes terminal state with regard to its dimensions. */
139 	TermState term_state;
140 
141 	int last_search_backward;
142 
143 	/* Flag which controls use of historical cursor positions.  On by default, but
144 	 * gets temporarily disabled on certain operations possibly depending on value
145 	 * of options. */
146 	int ch_pos;
147 
148 	int confirmed;
149 
150 	/* Whether to skip complete UI redraw after returning from a shellout. */
151 	int skip_shellout_redraw;
152 
153 	/* Color scheme to use for :commands that manipulate colors.  Almost always
154 	 * refers to cfg.cs, except for when local color scheme is loaded. */
155 	col_scheme_t *cs;
156 	/* Name of the color scheme loaded from vifminfo. */
157 	char color_scheme[NAME_MAX + 1];
158 
159 	int msg_head, msg_tail;
160 	char *msgs[51];
161 	int save_msg_in_list;
162 	int allow_sb_msg_truncation; /* Whether truncation can be performed. */
163 
164 	int scroll_bind_off;
165 	SPLIT split;
166 	/* Splitter position relative to viewport, negative values mean "centred".
167 	 * Handling it as a special case prevents drifting from center on resizes due
168 	 * to rounding.  Should be updated using stats_set_splitter_*(). */
169 	int splitter_pos;
170 	/* Splitter position as a ratio relative to terminal's width or height.
171 	 * Should be updated using stats_set_splitter_*().*/
172 	double splitter_ratio;
173 
174 	SourcingState sourcing_state;
175 
176 	/* Set while executing :restart command to prevent excess screen updates. */
177 	int restart_in_progress;
178 
179 	ExecEnvType exec_env_type; /* Specifies execution environment type. */
180 
181 	/* Shows which of supported terminal multiplexers is currently in use, if
182 	 * any. */
183 	TermMultiplexer term_multiplexer;
184 
185 	/* Stores last command-line mode command that was executed or an empty line
186 	 * (e.g. right after startup or :restart command). */
187 	char *last_cmdline_command;
188 
189 	int initial_lines;   /* Initial terminal height in lines. */
190 	int initial_columns; /* Initial terminal width in characters. */
191 
192 	const char *ellipsis; /* String representation of ellipsis. */
193 
194 	ShellType shell_type; /* Specifies type of shell. */
195 
196 	const char *fuse_umount_cmd; /* Command to use for fuse unmounting. */
197 
198 	FILE *original_stdout; /* Saved original standard output. */
199 
200 	char *chosen_files_out; /* Destination for writing chosen files. */
201 	char *chosen_dir_out;   /* Destination for writing chosen directory. */
202 	char *output_delimiter; /* Delimiter for writing out list of paths. */
203 
204 	char *on_choose; /* Command to execute on picking files. */
205 
206 	const void *preview_hint; /* Hint on which view is used for preview. */
207 
208 	int global_local_settings; /* Set local settings globally. */
209 
210 	int history_size;   /* Number of elements in histories. */
211 	hist_t cmd_hist;    /* History of command-line commands. */
212 	hist_t search_hist; /* History of search patterns. */
213 	hist_t prompt_hist; /* History of prompt input. */
214 	hist_t filter_hist; /* History of local filter patterns. */
215 
216 	struct ipc_t *ipc; /* Handle for IPC mechanism. */
217 
218 #ifdef HAVE_LIBGTK
219 	int gtk_available; /* for mimetype detection */
220 #endif
221 }
222 status_t;
223 
224 extern status_t curr_stats;
225 
226 /* Initializes curr_stats from the configuration.  Returns non-zero on error,
227  * otherwise zero is returned. */
228 int stats_init(struct config_t *config);
229 
230 /* Resets some part of runtime status information to its initial values.
231  * Returns non-zero on error. */
232 int stats_reset(const struct config_t *config);
233 
234 /* Updates curr_stats to reflect whether terminal multiplexers support is
235  * enabled. */
236 void stats_set_use_multiplexer(int use_term_multiplexer);
237 
238 /* Updates curr_stats.shell_type field according to passed shell command. */
239 void stats_update_shell_type(const char shell_cmd[]);
240 
241 /* Updates curr_stats.term_state field according to specified terminal
242  * dimensions.  Returns new state. */
243 TermState stats_update_term_state(int screen_x, int screen_y);
244 
245 /* Sets output location (curr_stats.chosen_files_out) for list of chosen
246  * files. */
247 void stats_set_chosen_files_out(const char output[]);
248 
249 /* Sets output location (curr_stats.chosen_dir_out) for last visited
250  * directory. */
251 void stats_set_chosen_dir_out(const char output[]);
252 
253 /* Sets delimiter (curr_stats.output_delimiter) for separating multiple paths in
254  * output. */
255 void stats_set_output_delimiter(const char delimiter[]);
256 
257 /* Sets command to run on file selection right before exiting
258  * exit (curr_stats.on_choose). */
259 void stats_set_on_choose(const char command[]);
260 
261 /* Checks whether custom actions on file choosing is set.  Returns non-zero if
262  * so, otherwise zero is returned. */
263 int stats_file_choose_action_set(void);
264 
265 /* Records status bar message. */
266 void stats_save_msg(const char msg[]);
267 
268 /* Updates curr_stats.preview.on field and performs necessary updates in other
269  * parts of the application. */
270 void stats_set_quickview(int on);
271 
272 /* Updates position of the splitter and schedules a redraw if it has changed. */
273 void stats_set_splitter_pos(int position);
274 
275 /* Updates position of the splitter based on a ratio and schedules a redraw if
276  * it has changed.  The parameter equal to -1.0 sets ratio based on current
277  * position of the splitter. */
278 void stats_set_splitter_ratio(double ratio);
279 
280 /* Scheduled updates. */
281 
282 /* Sets internal flag to schedule postponed redraw operation of the UI. */
283 void stats_redraw_later(void);
284 
285 /* Sets internal flag to schedule postponed refresh operation of the UI. */
286 void stats_refresh_later(void);
287 
288 /* Queries state of scheduled updates while resetting them at the same time.
289  * Returns the state. */
290 UpdateType stats_update_fetch(void);
291 
292 /* Checks whether redraw is scheduled without resetting it.  Returns non-zero if
293  * so, otherwise zero is returned. */
294 int stats_redraw_planned(void);
295 
296 /* UI silencing. */
297 
298 /* Non-zero argument makes UI more silent, zero argument makes it less silent.
299  * After calls with non-zero and zero arguments balance out, UI gets updated. */
300 void stats_silence_ui(int more);
301 
302 /* Checks whether UI is currently silenced. */
303 int stats_silenced_ui(void);
304 
305 /* Disables ui silencing of any level (after multiple invocations of
306  * `stats_silence_ui(1)`). */
307 void stats_unsilence_ui(void);
308 
309 /* Managing histories. */
310 
311 /* Changes size of histories stored in status_t.  Zero or negative value
312  * disables them. */
313 void hists_resize(int new_size);
314 
315 /* Saves command to command history. */
316 void hists_commands_save(const char command[]);
317 
318 /* Saves pattern to search history. */
319 void hists_search_save(const char pattern[]);
320 
321 /* Saves input to prompt history. */
322 void hists_prompt_save(const char input[]);
323 
324 /* Saves input to local filter history. */
325 void hists_filter_save(const char pattern[]);
326 
327 /* Gets the most recently used search pattern.  Returns the pattern or empty
328  * string if search history is empty. */
329 const char * hists_search_last(void);
330 
331 /* Caching of information about directories. */
332 
333 /* Retrieves information about the path at specified state checking whether it's
334  * outdated.  size and/or nitems can be NULL.  On unknown or outdated values
335  * variables are set to DCACHE_UNKNOWN. */
336 void dcache_get_at(const char path[], time_t mtime, uint64_t inode,
337 		uint64_t *size, uint64_t *nitems);
338 
339 /* Retrieves information about the entry checking whether it's outdated.  size
340  * and/or nitems can be NULL. */
341 void dcache_get_of(const struct dir_entry_t *entry, dcache_result_t *size,
342 		dcache_result_t *nitems);
343 
344 /* Updates cached sizes of parents by specified amount. */
345 void dcache_update_parent_sizes(const char path[], uint64_t by);
346 
347 /* Updates information about the path.  Returns zero on success, otherwise
348  * non-zero is returned. */
349 int dcache_set_at(const char path[], uint64_t inode, uint64_t size,
350 		uint64_t nitems);
351 
352 #endif /* VIFM__STATUS_H__ */
353 
354 /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */
355 /* vim: set cinoptions+=t0 filetype=c : */
356