X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=src%2Fclient%2Fwincontrol.c;h=eb9270b411130e5f4ce38471de247a817060857c;hb=8bf4e35ebe159f9cf051c8dbfc0909d7fe5d3399;hp=275fc9c96b0aefffc9056de1a3a6601b55515a26;hpb=e8e8f91cff96eebc1b440df18d9c3ef4ced1ca60;p=plomrogue diff --git a/src/client/wincontrol.c b/src/client/wincontrol.c index 275fc9c..eb9270b 100644 --- a/src/client/wincontrol.c +++ b/src/client/wincontrol.c @@ -1,458 +1,415 @@ -/* src/client/wincontrol.c */ +/* src/client/wincontrol.c + * + * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3 + * or any later version. For details on its copyright, license, and warranties, + * see the file NOTICE in the root directory of the PlomRogue source package. + */ #include "wincontrol.h" -#include /* global errno */ +#include /* getmaxx(), getmaxy(), wresize() */ #include /* NULL */ -#include /* uint8_t, uint16_t, uint32_t */ -#include /* FILE, sprintf() */ -#include /* free(), atoi() */ -#include /* strlen(), strchr(), memcpy() */ -#include "../common/readwrite.h" /* try_fgets(), try_fwrite(), try_fgetc(), - * try_fputc() - */ +#include /* uint8_t, uint32_t, UINT16_MAX */ +#include /* sprintf() */ +#include /* free() */ +#include /* memcpy(), memset(), strchr(), strlen() */ #include "../common/rexit.h" /* exit_err(), exit_trouble() */ #include "../common/try_malloc.h" /* try_malloc() */ -#include "../common/yx_uint16.h" /* struct yx_uint16 */ -#include "draw_wins.h" /* draw_win_map(), draw_win_info(), draw_win_log(), - * draw_win_available_keybindings(), - * draw_win_inventory(), draw_win_keybindings_global(), - * draw_win_keybindings_winconf_geometry(), - * draw_win_keybindings_winconf_keybindings(), - * draw_winconf_geometry(), draw_winconf_keybindings() - */ -#include "keybindings.h" /* struct KeyBinding, free_keybindings() */ -#include "windows.h" /* struct Win, resize_active_win(), reset_pad_offset(), - * append_win(), suspend_win(), init_win(), free_win() - */ +#include "windows.h" /* Win,yx_uint16, get_win_by_id(),get_win_pos_in_order() */ #include "world.h" /* global world */ -/* Wrapper around init_win() called with values from Winconf of "id". */ -static void init_win_from_winconf(char id); +/* Get Win before window identified by "c" or NULL if there is none. */ +static struct Win * get_win_before(char c); -/* Free data pointed to inside individual WinConf struct of "id". */ -static void free_winconf_data(char id); +/* Make .v_screen just wide enough to contain all visible windows. */ +static void refit_v_screen(); -/* Write geometry of a window to its WinConf, as positive or negative values - * (dependent on state ofWinConf->height_type / WinConf->width_type). +/* Update geometry (sizes, positions) of window "w" and its successors in the + * window chain. Use place_win() for the positioning algorithm. */ -static void set_winconf_geometry(char id); +static void update_wins(struct Win * w); +static void place_win(struct Win * w); -/* Get WinConf by "id". */ -static struct WinConf * get_winconf_by_id(char id); +/* Write "win"'s size back to .target_(height/width) as per .target_*_type. */ +static void set_win_target_size(struct Win * win); -/* Get (Win->draw) function identified by "c"; NULL if c not mapped to one. */ -static void * get_drawfunc_by_char(char c); +/* Append/suspend window "w" to/from chain of visible windows. Appended windows + * will become active. Suspended active windows will move the active window + * selection to their successor in the window chain or, failing that, their + * predecessor, or, failing that, to 0 (no window active). + */ +static void append_win(struct Win * w); +static void suspend_win(struct Win * w); -static void free_winconf_data(char id) +extern struct Win * get_win_before(char c) { - struct WinConf * wc = get_winconf_by_id(id); - free(wc->title); - free_keybindings(wc->kb.kbs); - free_win(wc->win); + uint8_t i = get_win_pos_in_order(c); + if (i > 0) + { + return get_win_by_id(world.winDB.order[i - 1]); + } + return NULL; } -static void set_winconf_geometry(char id) +static void refit_v_screen() { - struct WinConf * wcp = get_winconf_by_id(id); - if (0 == wcp->height_type) - { - wcp->height = wcp->win->framesize.y; - } - else if (1 == wcp->height_type) + /* Determine rightmost window column. */ + uint32_t lastwcol = 0; + struct Win * wp = get_win_by_id(world.winDB.order[0]); + while (wp != 0) { - wcp->height = wcp->win->framesize.y - world.wmeta.padsize.y + 1; - } - if (0 == wcp->width_type) - { - wcp->width = wcp->win->framesize.x; + if ((uint32_t) wp->start.x + (uint32_t) wp->frame_size.x > lastwcol + 1) + { + lastwcol = (uint32_t) wp->start.x + (uint32_t) wp->frame_size.x - 1; + } + wp = get_win_after(wp->id); } - else if (1 == wcp->width_type) + + /* Only resize .v_screen if the rightmost window column has changed. */ + char * err_s = "refit_v_screen() grows virtual screen beyond legal sizes."; + char * err_m = "refit_v_screen() triggers memory alloc error in wresize()."; + if ((uint32_t) getmaxx(world.winDB.v_screen) + 1 != lastwcol) { - wcp->width = wcp->win->framesize.x - world.wmeta.padsize.x; + uint8_t t = (lastwcol + 2 > UINT16_MAX); + exit_err(t, err_s); + t = wresize(world.winDB.v_screen, getmaxy(world.winDB.v_screen), + lastwcol + 2); + exit_err(t, err_m); } } -static void * get_drawfunc_by_char(char c) +static void update_wins(struct Win * w) { - - if ('c' == c) - { - return draw_win_inventory; - } - else if ('i' == c) - { - return draw_win_info; - } - else if ('l' == c) - { - return draw_win_log; - } - else if ('k' == c) - { - return draw_win_available_keybindings; - } - else if ('m' == c) + place_win(w); + refit_v_screen(); + struct Win * next = get_win_after(w->id); + if (next) { - return draw_win_map; + update_wins(next); } - else if ('0' == c) - { - return draw_win_keybindings_global; - } - else if ('1' == c) - { - return draw_win_keybindings_winconf_geometry; - } - else if ('2' == c) - { - return draw_win_keybindings_winconf_keybindings; - } - return NULL; } -extern void init_win_from_winconf(char id) +static void place_win(struct Win * w) { - char * err = "get_drawfunc_by_char() returns NULL to init_win_from_file()."; - struct WinConf * winconf = get_winconf_by_id(id); - void * f = get_drawfunc_by_char(winconf->draw); - exit_err(NULL == f, err); - init_win(&winconf->win, winconf->title, winconf->height, winconf->width, f); -} - + uint8_t sep = 1; /* Width of inter-window borders and title bars. */ - -static struct WinConf * get_winconf_by_id(char id) -{ - uint8_t i = 0; - while (1) + /* If w is first window, it goes into the top left corner. */ + w->start.x = 0; + w->start.y = 0 + sep; + struct Win * w_prev = get_win_before(w->id); + if (w_prev) { - if (id == world.winconf_db.winconfs[i].id) + + /* If not, get w's next predecessor starting a new stack on the screen + * top, fit w's top left corner to that predecessor's top right corner. + */ + struct Win * w_top = w_prev; + for (; w_top->start.y != 0 + sep; w_top = get_win_before(w_top->id)); + w->start.x = w_top->start.x + w_top->frame_size.x + sep; + + /* If enough space is found below w's predecessor, fit w's top left + * corner to that predecessor's bottom left corner. + */ + uint16_t next_free_y = w_prev->start.y + w_prev->frame_size.y + sep; + if ( w->frame_size.x <= w_prev->frame_size.x + && w->frame_size.y <= world.winDB.v_screen_size.y - next_free_y) { - return &world.winconf_db.winconfs[i]; + w->start.x = w_prev->start.x; + w->start.y = next_free_y; + return; } - i++; - } -} - - -extern struct WinConf * get_winconf_by_win(struct Win * win) -{ - uint8_t i = 0; - while (1) - { - if (win == world.winconf_db.winconfs[i].win) + /* If that fails, try to fit w's top left corner to the top right corner + * of its next predecessor w_test 1) below w_top (w's next predecessor + * starting a new stack on the screen top) 2) and the most rightward + * lower neighbor of a window w_high, itself throning over enough free + * space for w to fit below it, rightwards of its lower neighbor w_test. + */ + struct Win * w_test = w_prev; + struct Win * w_high; + while (w_test != w_top) { - return &world.winconf_db.winconfs[i]; + for (w_high = get_win_before(w_test->id); /* Walk down chain */ + w_test->start.y <= w_high->start.y; /* until w_high starts */ + w_high = get_win_before(w_high->id)); /* higher than w_test. */ + next_free_y = w_high->start.y + w_high->frame_size.y + sep; + uint16_t first_free_x = w_test->start.x + w_test->frame_size.x +sep; + uint16_t last_free_x = w_high->start.x + w_high->frame_size.x; + if ( w->frame_size.y <= world.winDB.v_screen_size.y - next_free_y + && w->frame_size.x <= last_free_x - first_free_x) + { + w->start.x = first_free_x; + w->start.y = next_free_y; + break; + } + w_test = w_high; } - i++; } } -extern struct Win * get_win_by_id(char id) -{ - struct WinConf * wc = get_winconf_by_id(id); - return wc->win; -} - - - -extern uint8_t read_winconf_from_file(char * line, uint32_t linemax, FILE * file) +static void set_win_target_size(struct Win * w) { - char * f_name = "read_winconf_from_file()"; - int test = try_fgetc(file, f_name); - if (EOF == test) + if (0 == w->target_height_type) { - return 0; + w->target_height = w->frame_size.y; } - struct WinConf winconf; - winconf.id = (char) test; - winconf.draw = winconf.id; - try_fgetc(file, f_name); - try_fgets(line, linemax + 1, file, f_name); - winconf.title = try_malloc(strlen(line), f_name); - memcpy(winconf.title, line, strlen(line) - 1); /* Eliminate newline char */ - winconf.title[strlen(line) - 1] = '\0'; /* char at end of string. */ - try_fgets(line, linemax + 1, file, f_name); - winconf.height = atoi(line); - winconf.height_type = (0 >= winconf.height); - try_fgets(line, linemax + 1, file, f_name); - winconf.width = atoi(line); - winconf.width_type = (0 >= winconf.width); - read_keybindings_from_file(line, linemax, file, &winconf.kb); - winconf.win = NULL; - winconf.view = 0; - winconf.center.y = 0; - winconf.center.x = 0; - if (world.winconf_db.ids) + else if (1 == w->target_height_type) { - uint8_t old_ids_size = strlen(world.winconf_db.ids); - char * new_ids = try_malloc(old_ids_size + 1 + 1, f_name); - sprintf(new_ids, "%s%c", world.winconf_db.ids, winconf.id); - free(world.winconf_db.ids); - world.winconf_db.ids = new_ids; - uint16_t old_winconfs_size = old_ids_size * sizeof(struct WinConf); - uint16_t new_winconfs_size = old_winconfs_size + sizeof(struct WinConf); - struct WinConf * new_winconfs = try_malloc(new_winconfs_size, f_name); - memcpy(new_winconfs, world.winconf_db.winconfs, old_winconfs_size); - new_winconfs[old_ids_size] = winconf; - free(world.winconf_db.winconfs); - world.winconf_db.winconfs = new_winconfs; + w->target_height = w->frame_size.y - world.winDB.v_screen_size.y +1; } - else + if (0 == w->target_width_type) { - world.winconf_db.ids = try_malloc(2, f_name); - sprintf(world.winconf_db.ids, "%c", winconf.id); - world.winconf_db.winconfs = try_malloc(sizeof(struct WinConf), f_name); - *world.winconf_db.winconfs = winconf; + w->target_width = w->frame_size.x; } - return 1; -} - - - -extern void write_winconf_of_id_to_file(FILE * file, char c, char * delim) -{ - char * f_name = "write_winconf_of_id_to_file()"; - struct WinConf * wc = get_winconf_by_id(c); - uint8_t size = strlen(wc->title) + 2; - if (size < 7) /* Ensure that at least 5 + 2 char fit into line so that */ - { /* the digit representation of any uint16_t may be stored. */ - size = 7; + else if (1 == w->target_width_type) + { + w->target_width = w->frame_size.x - world.winDB.v_screen_size.x; } - char line[size]; - sprintf(line, "%c\n", wc->draw); - try_fwrite(line, sizeof(char), strlen(line), file, f_name); - sprintf(line, "%s\n", wc->title); - try_fwrite(line, sizeof(char), strlen(line), file, f_name); - sprintf(line, "%d\n", wc->height); - try_fwrite(line, sizeof(char), strlen(line), file, f_name); - sprintf(line, "%d\n", wc->width); - try_fwrite(line, sizeof(char), strlen(line), file, f_name); - write_keybindings_to_file(file, &wc->kb, delim); } -extern void read_order_wins_visible_active(char * line, uint32_t linemax, FILE * file) +static void append_win(struct Win * w) { - char * f_name = "read_order_wins_visible_active()"; - char win_order[linemax + 1]; - try_fgets(win_order, linemax + 1, file, f_name); - world.winconf_db.order = try_malloc(linemax, f_name); - win_order[strlen(win_order) - 1] = '\0'; - sprintf(world.winconf_db.order, "%s", win_order); - int char_or_eof = try_fgetc(file, f_name); - char * err_eof = "fgetc() unexpectedly hitting EOF"; - exit_trouble(EOF == char_or_eof, f_name, err_eof); - world.winconf_db.active = (uint8_t) char_or_eof; - exit_trouble(EOF == try_fgetc(file, f_name), f_name, err_eof); - try_fgets(line, linemax + 1, file, f_name); + uint8_t old_size = strlen(world.winDB.order) + 1; + char * new_order = try_malloc(old_size + 1, __func__); + memcpy(new_order, world.winDB.order, old_size - 1); + new_order[old_size - 1] = w->id; + new_order[old_size] = '\0'; + free(world.winDB.order); + world.winDB.order = new_order; + world.winDB.active = w->id; + update_wins(w); } -extern void write_order_wins_visible_active(FILE * file, char * delim) +static void suspend_win(struct Win * w) { - char * f_name = "write_order_wins_visible_active()"; - char line[strlen(world.winconf_db.ids) + 2]; - struct Win * w_p = world.wmeta.chain_start; - char active = ' '; - uint8_t i; - for (; NULL != w_p; w_p = w_p->next, i++) + uint8_t new_size = strlen(world.winDB.order); + char * new_order = try_malloc(new_size, __func__); + uint8_t i = get_win_pos_in_order(w->id); + char next_char = world.winDB.order[i + 1]; + world.winDB.order[i] = '\0'; + char * second_part = &world.winDB.order[i + 1]; + int test = sprintf(new_order, "%s%s", world.winDB.order, second_part); + exit_trouble(test < 0, __func__, "sprintf"); + free(world.winDB.order); + world.winDB.order = new_order; + world.winDB.active = world.winDB.order[i]; + if (!world.winDB.order[i] && 0 < i) { - struct WinConf * wc_p = get_winconf_by_win(w_p); - line[i] = wc_p->id; - if (w_p == world.wmeta.active) - { - active = wc_p->id; - } + world.winDB.active = world.winDB.order[i - 1]; } - line[i] = '\n'; - line[i + 1] = '\0'; - try_fwrite(line, sizeof(char), strlen(line), file, f_name); - try_fputc(active, file, f_name); - try_fputc('\n', file, f_name); - try_fwrite(delim, strlen(delim), 1, file, f_name); -} - - - -extern void free_winconfs() -{ - char id; - while (0 != (id = get_next_winconf_id())) + if (world.winDB.order[i]) { - free_winconf_data(id); + update_wins(get_win_by_id(next_char)); /* Already calls */ + return; /* refit_v_screen(), so leave. */ } - free(world.winconf_db.ids); - world.winconf_db.ids = NULL; - free(world.winconf_db.winconfs); - world.winconf_db.winconfs = NULL; - free(world.winconf_db.order); - world.winconf_db.order = NULL; + refit_v_screen(); } -extern void init_wins() +extern void toggle_window(char id) { - char id; - while (0 != (id = get_next_winconf_id())) + struct Win * win = get_win_by_id(id); + if (!strchr(world.winDB.order, id)) { - init_win_from_winconf(id); + append_win(win); + return; } + suspend_win(win); } -extern void sorted_win_toggle_and_activate() +extern void toggle_winconfig() { - uint8_t i = 0; - for (; i < strlen(world.winconf_db.order); i++) + if (!world.winDB.active) { - if (NULL == strchr(world.winconf_db.ids, world.winconf_db.order[i])) - { - continue; - } - toggle_window(world.winconf_db.order[i]); - if (world.winconf_db.active == (uint8_t) world.winconf_db.order[i]) - { - world.wmeta.active = get_win_by_id(world.winconf_db.order[i]); - } + return; + } + struct Win * w = get_win_by_id(world.winDB.active); + if (0 == w->view) + { + w->view = 1; + w->target_center = w->center; + memset(&w->center, 0, sizeof(struct yx_uint16)); + return; } + else if (1 == w->view) + { + w->view = 2; + w->center.x = 0; + return; + } + w->view = 0; + w->center = w->target_center; } -extern char get_next_winconf_id() + +extern void toggle_win_size_type(char axis) { - static uint8_t i = 0; - char c = world.winconf_db.ids[i]; - if (0 == c) + struct Win * w = get_win_by_id(world.winDB.active); + if ('y' == axis) { - i = 0; - return c; + w->target_height_type = (0 == w->target_height_type); + set_win_target_size(w); + return; } - i++; - return c; + w->target_width_type = ( 0 == w->target_width_type + && w->frame_size.x <= world.winDB.v_screen_size.x); + set_win_target_size(w); } -extern void toggle_window(char id) +extern void toggle_linebreak_type() { - struct Win * win = get_win_by_id(id); - if (0 == win->prev && world.wmeta.chain_start != win) /* Win struct is */ - { /* outside chain? */ - append_win(win); + struct Win * w = get_win_by_id(world.winDB.active); + if (0 == w->linebreak) + { + w->linebreak = 1; } - else + else if (1 == w->linebreak) { - suspend_win(win); + w->linebreak = 2; + } + else if (2 == w->linebreak) + { + w->linebreak = 0; } } -extern void toggle_winconfig() +extern void resize_active_win(char change) { - struct Win * win = world.wmeta.active; - struct WinConf * wcp = get_winconf_by_win(win); - if (0 == wcp->view) + if (world.winDB.active) { - wcp->view = 1; - win->draw = draw_winconf_geometry; - wcp->center = win->center; - win->center.y = 0; - win->center.x = 0; - } - else if (1 == wcp->view) - { - wcp->view = 2; - win->draw = draw_winconf_keybindings; - win->center.x = 0; - } - else - { - wcp->view = 0; - win->draw = get_drawfunc_by_char(wcp->draw); - win->center = wcp->center; + struct Win * w = get_win_by_id(world.winDB.active); + if (change == '-' && w->frame_size.y > 1) + { + w->frame_size.y--; + } + else if (change == '_' && w->frame_size.x > 1) + { + w->frame_size.x--; + } + else if ( change == '+' + && w->frame_size.y < world.winDB.v_screen_size.y - 1) + { + w->frame_size.y++; + } + else if (change == '*' && w->frame_size.y < UINT16_MAX) + { + w->frame_size.x++; + } + if ( 1 == w->target_width_type + && w->frame_size.x > world.winDB.v_screen_size.x) + { + w->target_width_type = 0; + } + set_win_target_size(w); + update_wins(w); } } -extern void toggle_win_size_type(char axis) +extern void shift_active_win(char dir) { - struct Win * win = world.wmeta.active; - struct WinConf * wcp = get_winconf_by_win(win); - if ('y' == axis) + uint8_t len_order = strlen(world.winDB.order); + if (1 < len_order) { - wcp->height_type = (0 == wcp->height_type); - set_winconf_geometry(wcp->id); - return; + char * tmp = try_malloc(len_order + 1, __func__); + tmp[len_order] = '\0'; + uint8_t pos = get_win_pos_in_order(world.winDB.active); + if ('f' == dir) + { + if (pos == len_order - 1) + { + memcpy(tmp + 1, world.winDB.order, len_order - 1); + tmp[0] = world.winDB.active; + memcpy(world.winDB.order, tmp, len_order + 1); + } + else + { + world.winDB.order[pos] = world.winDB.order[pos + 1]; + world.winDB.order[pos + 1] = world.winDB.active; + } + } + else + { + if (pos == 0) + { + memcpy(tmp, world.winDB.order + 1, len_order - 1); + tmp[len_order - 1] = world.winDB.active; + memcpy(world.winDB.order, tmp, len_order + 1); + } + else + { + world.winDB.order[pos] = world.winDB.order[pos - 1]; + world.winDB.order[pos - 1] = world.winDB.active; + } + } + free(tmp); + update_wins(get_win_by_id(world.winDB.order[0])); } - wcp->width_type = ( 0 == wcp->width_type - && win->framesize.x <= world.wmeta.padsize.x); - set_winconf_geometry(wcp->id); } -extern void scroll_pad(char dir) +extern void scroll_v_screen(char dir) { - if ('+' == dir) + if ( '+' == dir + && world.winDB.v_screen_offset + world.winDB.v_screen_size.x + 1 + < getmaxx(world.winDB.v_screen)) { - reset_pad_offset(world.wmeta.pad_offset + 1); + world.winDB.v_screen_offset++; } - else if ('-' == dir) + else if ( '-' == dir + && world.winDB.v_screen_offset > 0) { - reset_pad_offset(world.wmeta.pad_offset - 1); + world.winDB.v_screen_offset--; } } -extern void growshrink_active_window(char change) +extern void cycle_active_win(char dir) { - if (0 != world.wmeta.active) + uint8_t len_order = strlen(world.winDB.order); + if (1 < len_order) { - struct yx_uint16 size = world.wmeta.active->framesize; - if (change == '-') - { - size.y--; - } - else if (change == '+') - { - size.y++; - } - else if (change == '_') - { - size.x--; - } - else if (change == '*') + uint8_t pos = get_win_pos_in_order(world.winDB.active); + if ('f' == dir) { - size.x++; + world.winDB.active = world.winDB.order[pos + 1]; + if ('\0' == world.winDB.active) + { + world.winDB.active = world.winDB.order[0]; + } + return; } - resize_active_win(size); - struct WinConf * wcp = get_winconf_by_win(world.wmeta.active); - if ( 1 == wcp->width_type - && world.wmeta.active->framesize.x > world.wmeta.padsize.x) + if (pos > 0) { - wcp->width_type = 0; + world.winDB.active = world.winDB.order[pos - 1]; + return; } - set_winconf_geometry(wcp->id); + world.winDB.active = world.winDB.order[len_order - 1]; } }