C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
Initializing View I/O

Populate the C-Menu View Struct and Connect Input. More...

Files

file  init_view.c
 Initialize C-Menu View Screen IO and Input.

Functions

int init_view_boxwin (Init *init)
 Initialize the C-Menu View in box window mode.
int init_view_full_screen (Init *init)
 Initialize C-Menu View in full screen mode.
void view_calc_boxwin_dimensions (Init *init)
 Calculate the dimensions and position of the box window for C-Menu View.
void view_calc_full_screen_dimensions (Init *init)
 Calculate the dimensions for full screen mode.
int view_init_input (Init *init, char *file_name)
 Initialize the input for the C-Menu View.

Detailed Description

Populate the C-Menu View Struct and Connect Input.

Function Documentation

◆ init_view_boxwin()

int init_view_boxwin ( Init * init)

Initialize the C-Menu View in box window mode.

Parameters
initPointer to the Init structure containing view settings.
Returns
0 on success, -1 on failure.

sets up the view structure for box window mode, adjusts dimensions based on screen size, and creates a new pad for the view. It also configures various parameters such as scroll lines, command line position, and tab size.

Definition at line 147 of file init_view.c.

147 {
148 View *view = init->view;
149 view->f_full_screen = false;
150 // -------------------> 1. BOX / WIN <-------------------
152 view->sfc = ui_box_surface_new(NULL, 0, view->lines, view->cols, view->begy, view->begx, NULL);
153 ui_surface_addwin(view->sfc, WIN, BOX, view->lines, view->cols, 1, 1);
154 // -------------------> 2. LNNO <-------------------
155 ui_surface_addwin(view->sfc, LNNO, WIN, view->lines - 1, view->ln_win_cols, 0, 0);
156 ui_bkgd(view->sfc, LNNO, &cell_ln);
157 ui_bkgdset(view->sfc, LNNO, &cell_ln);
158 ui_scrollok(view->sfc, LNNO, true);
159 ui_setscrreg(view->sfc, LNNO, 0, view->scroll_lines);
160 // -------------------> 3. CMDLN <-------------------
161 ui_surface_addwin(view->sfc, CMDLN, WIN, 1, view->cols, view->lines - 1, 0);
162 ui_bkgd(view->sfc, CMDLN, &cell_nt);
163 ui_keypad(view->sfc, CMDLN, true);
164 ui_idlok(view->sfc, CMDLN, false);
165 ui_idcok(view->sfc, CMDLN, false);
166 ui_scrollok(view->sfc, CMDLN, false);
167 // -------------------> 4. PAD <-------------------
168 ui_surface_addpad(view->sfc, PAD, WIN, view->lines - 1, PAD_COLS - 1);
169 // ------------------------------------------------
170 return (0);
171}
int ui_bkgdset(UiSurface *s, uint w, const UiCell *cell)
Definition ui_ncurses.c:760
int ui_setscrreg(UiSurface *s, uint w, uint top, uint bottom)
Definition ui_ncurses.c:662
int ui_scrollok(UiSurface *s, uint w, bool enable)
Definition ui_ncurses.c:626
int ui_bkgd(UiSurface *s, uint w, const UiCell *cell)
Definition ui_ncurses.c:753
int ui_idlok(UiSurface *s, uint w, bool enable)
Definition ui_ncurses.c:644
UiSurface * ui_box_surface_new(UiSurface *parent, uint p, uint lines, uint cols, uint y, uint x, char *title)
Definition ui_ncurses.c:453
int ui_keypad(UiSurface *s, uint w, bool enable)
Definition ui_ncurses.c:635
int ui_idcok(UiSurface *s, uint w, bool enable)
Definition ui_ncurses.c:653
int ui_surface_addpad(UiSurface *s, uint w, uint view_win, uint lines, uint cols)
Definition ui_ncurses.c:509
int ui_surface_addwin(UiSurface *s, uint w, uint p, uint lines, uint cols, uint y, uint x)
Definition ui_ncurses.c:523
#define PAD_COLS
Definition view.h:41
View * view
Definition mem.c:38
UiCell cell_nt
Definition dwin.c:94
UiCell cell_ln
Definition dwin.c:102
void view_calc_boxwin_dimensions(Init *)
Calculate the dimensions and position of the box window for C-Menu View.
Definition init_view.c:195
View * view
Definition common.h:189
Definition view.h:61

References View::begx, View::begy, BOX, cell_ln, cell_nt, CMDLN, View::cols, View::f_full_screen, View::lines, View::ln_win_cols, LNNO, PAD, View::scroll_lines, View::sfc, ui_bkgd(), ui_bkgdset(), ui_box_surface_new(), ui_idcok(), ui_idlok(), ui_keypad(), ui_scrollok(), ui_setscrreg(), ui_surface_addpad(), ui_surface_addwin(), Init::view, view_calc_boxwin_dimensions(), and WIN.

Referenced by main(), new_pick_view(), popup_view(), and view_boxwin_resize().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_view_full_screen()

int init_view_full_screen ( Init * init)

Initialize C-Menu View in full screen mode.

Parameters
initPointer to the Init structure containing view settings.
Returns
0 on success, -1 on failure.

This function sets up the view structure for full screen mode and creates a new pad for the view.

The function creates the following windows:
1. view->cmdln_win: Status or Command Line
2. view->lnno_win: Line Number Window
3. view->pad: Main Content Pad

Definition at line 47 of file init_view.c.

47 {
48 View *view = init->view;
49 view->f_full_screen = true;
50 // -------------------> 1. WIN <-------------------
52 view->sfc = ui_surface_new(WIN, NULL, 0, view->lines, view->cols, 0, 0);
53 if (!view->sfc) {
54 ssnprintf(em0, MAXLEN - 1, "newwin(LINES, COLS, 0, 0) failed in init_view_full_screen");
55 Perror(em0);
56 return -1;
57 }
58 ui_bkgd(view->sfc, WIN, &cell_nt);
59 // -------------------> 2. LNNO <-------------------
60 ui_surface_addwin(view->sfc, LNNO, WIN, view->scroll_lines, view->ln_win_cols, 0, 0);
61 if (view->sfc->lnno == nullptr) {
62 ssnprintf(em0, MAXLEN - 1, "ui_sfc_addwin(LNNO, LINES - 1, COLS, 0, 0) failed in init_view_full_screen");
63 Perror(em0);
64 return -1;
65 }
66 ui_bkgd(view->sfc, LNNO, &cell_ln);
67 ui_render();
68 ui_scrollok(view->sfc, LNNO, true);
69 ui_setscrreg(view->sfc, LNNO, 0, view->scroll_lines);
70 // -------------------> 3. CMDLN <-------------------
71 ui_surface_addwin(view->sfc, CMDLN, WIN, 1, view->cols, view->scroll_lines, 0);
72 if (view->sfc->cmdln == nullptr) {
73 ssnprintf(em0, MAXLEN - 1, "ui_sfc_addwin(CMDLN, 1, COLS, LINES - 1, 0) failed in init_view_full_screen");
74 Perror(em0);
75 return -1;
76 }
77 ui_bkgd(view->sfc, CMDLN, &cell_nt);
78 ui_keypad(view->sfc, CMDLN, true);
79 ui_idlok(view->sfc, CMDLN, false);
80 ui_idcok(view->sfc, CMDLN, false);
81 ui_scrollok(view->sfc, CMDLN, false);
82 // -------------------> 4. PAD <-------------------
83
84 ui_surface_addpad(view->sfc, PAD, WIN, view->lines - 1, PAD_COLS - 1);
85
86 // ------------------------------------------------
87 return 0;
88}
void ui_render()
Definition ui_ncurses.c:800
UiSurface * ui_surface_new(uint w, UiSurface *parent, uint p, uint lines, uint cols, uint y, uint x)
Definition ui_ncurses.c:414
#define MAXLEN
Definition curskeys.c:15
char em0[MAXLEN]
Definition dwin.c:142
int Perror(char *emsg_str)
Display a simple error message window or print to stderr.
Definition dwin.c:841
size_t ssnprintf(char *, size_t, const char *,...)
ssnprintf was designed to be a safer alternative to snprintf.
Definition futil.c:413
void view_calc_full_screen_dimensions(Init *)
Calculate the dimensions for full screen mode.
Definition init_view.c:113

References cell_ln, cell_nt, CMDLN, View::cols, em0, View::f_full_screen, View::lines, View::ln_win_cols, LNNO, PAD, Perror(), View::scroll_lines, View::sfc, ssnprintf(), ui_bkgd(), ui_idcok(), ui_idlok(), ui_keypad(), ui_render(), ui_scrollok(), ui_setscrreg(), ui_surface_addpad(), ui_surface_addwin(), ui_surface_new(), Init::view, view_calc_full_screen_dimensions(), and WIN.

Referenced by main(), and view_full_screen_resize().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ view_calc_boxwin_dimensions()

void view_calc_boxwin_dimensions ( Init * init)

Calculate the dimensions and position of the box window for C-Menu View.

This function calculates the dimensions and position of the box window for the C-Menu View based on the screen size and any specified parameters in the Init structure. It ensures that the box window fits within the screen dimensions and adjusts its size and position accordingly.

Parameters
initPointer to the Init structure containing view settings.

Use view->lines and view->cols if set, otherwise calculate based on screen size with some padding. Ensure the view fits within the screen dimensions.

Definition at line 195 of file init_view.c.

195 {
196 View *view = init->view;
197
201 uint scr_lines, scr_cols;
202 ui_get_screen_size(&scr_lines, &scr_cols);
203#ifdef DEBUG_RESIZE
204 ssnprintf(em0, MAXLEN - 1,
205 "%s:%d=%d calc lines=%d, cols=%d, begy=%d, begx=%d",
206 __FILE__, __LINE__, 526, view->lines, view->cols, view->begy, view->begx);
208#endif
209 if (view->lines == 0 && view->cols == 0 && view->begy == 0 && view->begx == 0) {
210 view->lines = scr_lines - 3;
211 view->cols = scr_cols - 2;
212 view->begy = 0;
213 view->begx = 0;
214 }
215 uint lines = max(scr_lines / 10, 8);
216 view->lines = max(view->lines, lines);
217 if (view->lines > scr_lines - 3)
218 view->lines = scr_lines - 3;
219 if (view->lines + view->begy > scr_lines - 3)
220 view->begy = scr_lines - (view->lines + 3);
221
222 uint cols = max(scr_cols / 3, 40);
223 view->cols = max(view->cols, cols);
224 if (view->cols > scr_cols - 2)
225 view->cols = scr_cols - 2;
226 if (view->cols + view->begx > scr_cols - 2)
227 view->begx = scr_cols - (view->cols + 2);
228
229 view->h_shift = view->cols / 3;
230 view->ln_win_lines = view->lines - 1;
231 if (view->f_ln)
232 view->ln_win_cols = 8;
233 else
234 view->ln_win_cols = 0;
235 view->scroll_lines = view->lines - 1;
236 view->cmd_line = 0;
237 view->pminrow = 0;
238 view->pmincol = 0;
239 view->sminrow = view->begy + 1;
240 view->smincol = view->begx + view->ln_win_cols + 1;
241 view->smaxrow = view->begy + view->lines;
242 view->smaxcol = view->begx + view->cols;
243 view->ln_no = view->page_top_ln_no + view->scroll_lines;
244 view->page_bot_ln_no = view->ln_no;
245#ifdef DEBUG_RESIZE
246 ssnprintf(em0, MAXLEN - 1,
247 "%s:%d calc BOX lines=%d, cols=%d, begy=%d, begx=%d",
248 __FILE__, __LINE__, view->lines + 2, view->cols + 2, view->begy, view->begx);
250 ssnprintf(em0, MAXLEN - 1,
251 "%s:%d calc WIN lines=%d, cols=%d, begy=%d, begx=%d",
252 __FILE__, __LINE__, view->lines, view->cols, 1, 1);
254 ssnprintf(em0, MAXLEN - 1,
255 "%s:%d calc CMDLN lines=%d, cols=%d, begy=%d, begx=%d",
256 __FILE__, __LINE__, 1, view->cols, view->lines - 1, 1);
258 ssnprintf(em0, MAXLEN - 1,
259 "%s:%d calc LNNO lines=%d, cols=%d, begy=%d, begx=%d",
260 __FILE__, __LINE__, view->lines - 1, view->ln_win_cols, 0, 0);
262 ssnprintf(em0, MAXLEN - 1,
263 "%s:%d calc PAD lines=%d, cols=%d, begy=%d, begx=%d",
264 __FILE__, __LINE__, view->lines - 1, view->cols - view->ln_win_cols, 0, view->ln_win_cols);
266
267#endif
268}
#define max(a, b)
max macro evaluates two expressions, returning greatest result.
Definition cm.h:84
void ui_get_screen_size(uint *lines, uint *cols)
Definition ui_ncurses.c:692
void write_cmenu_log(char *)
Write message to C-Menu log file without timestamp.
Definition futil.c:1684

References View::begx, View::begy, View::cmd_line, View::cols, View::f_ln, View::h_shift, View::lines, View::ln_no, View::ln_win_cols, View::ln_win_lines, View::page_bot_ln_no, View::page_top_ln_no, View::pmincol, View::pminrow, View::scroll_lines, View::smaxcol, View::smaxrow, View::smincol, View::sminrow, ui_get_screen_size(), and Init::view.

Referenced by init_view_boxwin().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ view_calc_full_screen_dimensions()

void view_calc_full_screen_dimensions ( Init * init)

Calculate the dimensions for full screen mode.

This function calculates the dimensions for the full screen mode of the C-Menu View. It retrieves the maximum dimensions of the standard screen and sets the view parameters accordingly. It also resizes the line number window and command line window based on the new dimensions.

Parameters
initPointer to the Init structure containing view settings.

Definition at line 113 of file init_view.c.

113 {
114 View *view = init->view;
115 ui_get_screen_size(&view->lines, &view->cols);
116 view->ln_win_lines = view->lines;
117 view->ln_win_cols = 8;
118 view->scroll_lines = view->lines - 1;
119 view->h_shift = view->cols / 3;
120#ifdef DEBUG_RESIZE
121 char file[MAXLEN];
122 ssnprintf(
123 em0, MAXLEN - 1,
124 "%s:%d view->lines=%d, view->cols=%d, view->maxrows=%d, view->maxcols=%d",
125 __FILE__, __LINE__,
126 view->lines, view->cols, view->smaxrow, view->smaxcol);
128#endif
129 view->cmd_line = 0;
130 view->pminrow = 0;
131 view->pmincol = 0;
132 view->sminrow = 0;
133 view->smincol = view->ln_win_cols;
134 view->smaxrow = view->lines - 1;
135 view->smaxcol = view->cols - 1;
136 view->ln_no = view->page_top_ln_no + view->scroll_lines;
137 view->page_bot_ln_no = view->ln_no;
138}

References View::cmd_line, View::cols, View::h_shift, View::lines, View::ln_no, View::ln_win_cols, View::ln_win_lines, View::page_bot_ln_no, View::page_top_ln_no, View::pmincol, View::pminrow, View::scroll_lines, View::smaxcol, View::smaxrow, View::smincol, View::sminrow, ui_get_screen_size(), and Init::view.

Referenced by init_view_full_screen(), and view_full_screen_resize().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ view_init_input()

int view_init_input ( Init * init,
char * file_name )

Initialize the input for the C-Menu View.

Parameters
initPointer to the Init structure containing view settings.
file_nameName of the input file or "-" for standard input.
Returns
0 on success, -1 on failure.

This function initializes the input for the C-Menu View. It handles both regular files and standard input, setting up pipes if necessary. It also memory-maps the input file for efficient access and sets up the view structure accordingly.

Prevent child process from writing to terminal

Definition at line 279 of file init_view.c.

279 {
280 struct stat sb;
281 int idx = 0;
282 pid_t pid = -1;
283 int pipe_fd[2];
284 int s_argc = 0;
285 char *s_argv[MAXARGS];
286 char tmp_str[MAXLEN];
287 View *view = init->view;
288 view->f_in_pipe = false;
289 if (strcmp(file_name, "-") == 0) {
290 file_name = "/dev/stdin";
291 view->f_in_pipe = true;
292 }
293 if (view->provider_cmd[0] != '\0') {
294 s_argc = str_to_args(s_argv, view->provider_cmd, MAXARGS - 1);
295 if (pipe(pipe_fd) == -1) {
296 Perror("pipe(pipe_fd) failed in init_view");
297 return -1;
298 }
299 // endwin();
300 if ((pid = fork()) == -1) {
301 Perror("fork() failed in init_view");
302 return -1;
303 }
304 if (pid == 0) { // Child
306 int dev_null = open("/dev/null", O_WRONLY);
307 if (dev_null == -1) {
308 Perror("open(/dev/null) failed in init_pick child process");
309 exit(EXIT_FAILURE);
310 }
311 dup2(dev_null, STDERR_FILENO);
312 close(dev_null);
313 close(pipe_fd[P_READ]);
314 dup2(pipe_fd[P_WRITE], STDOUT_FILENO);
315 close(pipe_fd[P_WRITE]);
316 execvp(s_argv[0], s_argv);
317 strnz__cpy(tmp_str, "Can't exec view start cmd: ", MAXLEN - 1);
318 strnz__cat(tmp_str, s_argv[0], MAXLEN - 1);
319 Perror(tmp_str);
320 exit(EXIT_FAILURE);
321 }
322 // Back to parent
323 destroy_argv(s_argc, s_argv);
326 close(pipe_fd[P_WRITE]);
327 view->in_fd = dup(pipe_fd[P_READ]);
328 view->f_in_pipe = true;
329 } else {
330 if (view->f_in_pipe)
331 view->in_fd = dup(STDIN_FILENO);
332 else {
333 /*----------------------------------------------------------------------*/
334 /* Open the input file for reading and get its size. */
335 expand_tilde(file_name, MAXLEN - 1);
336 view->in_fd = open(file_name, O_RDONLY);
337 if (view->in_fd == -1) {
338 ssnprintf(em0, MAXLEN - 1, "%s, line: %d", __FILE__,
339 __LINE__ - 3);
340 ssnprintf(em1, MAXLEN - 1, "open %s", file_name);
341 strerror_r(errno, em2, MAXLEN);
342 display_error(em0, em1, em2, nullptr);
343 return -1;
344 }
345 if (fstat(view->in_fd, &sb) == -1) {
346 ssnprintf(em0, MAXLEN - 1, "%s, line: %d", __FILE__,
347 __LINE__ - 1);
348 ssnprintf(em1, MAXLEN - 1, "fstat %s", file_name);
349 strerror_r(errno, em2, MAXLEN);
350 display_error(em0, em1, em2, nullptr);
351 close(view->in_fd);
352 return -1;
353 }
354 view->file_size = sb.st_size;
355 if (view->file_size == 0) {
356 close(view->in_fd);
357 ssnprintf(em0, MAXLEN - 1, "%s, line: %d", __FILE__,
358 __LINE__ - 1);
359 ssnprintf(em1, MAXLEN - 1, "file %s is empty", file_name);
360 strerror_r(errno, em2, MAXLEN);
361 display_error(em0, em1, em2, nullptr);
362 return -1;
363 }
364 if (!S_ISREG(sb.st_mode))
365 view->f_in_pipe = true;
366 }
367 /*----------------------------------------------------------------------*/
368 }
369 if (view->f_in_pipe) {
370 errno = 0;
371 view->tmp_fd = memfd_create("view_input", MFD_CLOEXEC);
372 if (view->in_fd < 0 || errno != 0) {
373 ssnprintf(em0, MAXLEN - 1, "memfd_create failed\n");
374 ssnprintf(em1, MAXLEN - 1, "%s", strerror(errno));
375 ssnprintf(em2, MAXLEN - 1, "%s, line: %d, errno: %d", __FILE__,
376 __LINE__ - 4, errno);
377 display_error(em0, em1, em2, nullptr);
378 exit(EXIT_FAILURE);
379 }
380 char buf[VBUFSIZ];
381 ssize_t bytes_read = 0;
382 ssize_t bytes_written = 0;
383 while ((bytes_read = read(view->in_fd, buf, sizeof(buf))) > 0) {
384 if ((bytes_written = write(view->tmp_fd, buf, bytes_read)) != bytes_read) {
385 abend(-1, "unable to write view->tmp_fd");
386 exit(EXIT_FAILURE);
387 }
388 }
389 if (fstat(view->tmp_fd, &sb) == -1) {
390 ssnprintf(em0, MAXLEN - 1, "fstat(view->in_fd) failed\n");
391 ssnprintf(em1, MAXLEN - 1, "%s", strerror(errno));
392 ssnprintf(em2, MAXLEN - 1, "%s, line: %d, errno: %d", __FILE__,
393 __LINE__ - 4, errno);
394 display_error(em0, em1, em2, nullptr);
395 exit(EXIT_FAILURE);
396 }
397 view->file_size = sb.st_size;
398 if (view->file_size == 0) {
399 close(view->tmp_fd);
400 strnz__cpy(tmp_str, "no standard input", MAXLEN - 1);
401 abend(-1, tmp_str);
402 exit(EXIT_FAILURE);
403 }
404 waitpid(-1, nullptr, 0);
405 view->in_fd = view->tmp_fd;
406 }
407 view->buf =
408 mmap(nullptr, view->file_size, PROT_READ, MAP_PRIVATE, view->in_fd, 0);
409 if (view->buf == MAP_FAILED) {
410 ssnprintf(em0, MAXLEN - 1, "%s, line: %d", __FILE__, __LINE__ - 2);
411 ssnprintf(em1, MAXLEN - 1, "mmap %s", file_name);
412 strerror_r(errno, em2, MAXLEN);
413 display_error(em0, em1, em2, nullptr);
414 close(view->in_fd);
415 return -1;
416 }
417 close(view->in_fd);
418 stdio_fdnames(stdio_names_str, "init_view.c 422");
419 view->file_size = sb.st_size;
420 view->prev_file_pos = NULL_POSITION;
421 view->buf_curr_ptr = view->buf;
422 if (view->cmd_all[0] != '\0')
423 strnz__cpy(view->cmd, view->cmd_all, MAXLEN - 1);
424 for (idx = 0; idx < NMARKS; idx++)
425 view->mark_tbl[idx] = NULL_POSITION;
426 strnz__cpy(view->cur_file_str, file_name, MAXLEN - 1);
427 base_name(view->file_name, view->cur_file_str);
428 return 0;
429}
#define P_READ
Definition common.h:50
#define P_WRITE
Definition common.h:51
char stdio_names_str[4096]
Definition futil.c:127
#define MAXARGS
Definition cm.h:50
char * stdio_fdnames(char *, char *)
Definition futil.c:1082
void ui_restore_wins()
#define NMARKS
Definition view.h:35
#define VBUFSIZ
Definition view.h:39
#define NULL_POSITION
Definition view.h:38
int pipe_fd[2]
Definition pick_engine.c:46
char em1[MAXLEN]
Definition dwin.c:143
char em2[MAXLEN]
Definition dwin.c:144
void abend(int ec, char *s)
Abnormal program termination.
Definition dwin.c:1204
int display_error(char *msg0, char *msg1, char *msg2, char *msg3)
Display an error message window or print to stderr.
Definition dwin.c:778
size_t strnz__cpy(char *, const char *, size_t)
safer alternative to strncpy
Definition futil.c:537
int str_to_args(char **, char *, uint)
Converts a string into an array of argument strings.
Definition futil.c:433
bool expand_tilde(char *, uint)
Replaces "~/" in string with the user's home directory.
Definition futil.c:963
size_t strnz__cat(char *, const char *, size_t)
safer alternative to strncat
Definition futil.c:566
int destroy_argv(uint argc, char **argv)
Deallocates memory allocated for argument strings in argv.
Definition futil.c:487
bool base_name(char *, char *)
Returns the base name of a file specification.
Definition futil.c:1106
void sig_prog_mode()
Set up signal handlers for interrupt signals.
Definition sig.c:62

References abend(), base_name(), View::buf, View::buf_curr_ptr, View::cmd, View::cmd_all, View::cur_file_str, destroy_argv(), display_error(), em0, em1, em2, expand_tilde(), View::f_in_pipe, View::file_name, View::file_size, View::in_fd, View::mark_tbl, Perror(), View::prev_file_pos, View::provider_cmd, sig_prog_mode(), ssnprintf(), stdio_fdnames(), stdio_names_str, str_to_args(), strnz__cat(), strnz__cpy(), View::tmp_fd, ui_restore_wins(), and Init::view.

Referenced by new_view_file(), and view_file().

Here is the call graph for this function:
Here is the caller graph for this function: