Skip to content

Commit 31b3409

Browse files
committed
add terminal resize
1 parent 4cb4c7f commit 31b3409

1 file changed

Lines changed: 41 additions & 41 deletions

File tree

lib/libterminal/terminal.c

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static void term_read_cb(struct shl_pty *pty, char *u8, size_t len,
3636
* output) */
3737
static void term_write_cb(struct tsm_vte *vtelocal, const char *u8, size_t len,
3838
void *data) {
39-
//printf("term_write_cb %s\n", u8);
39+
// printf("term_write_cb %s\n", u8);
4040
terminal *term = (terminal *)data;
4141
int r;
4242
r = shl_pty_write(term->pty, u8, len);
@@ -82,21 +82,22 @@ static int draw_cb(struct tsm_screen *screen, uint32_t id, const uint32_t *ch,
8282
}
8383
// color = glfonsRGBA(fr,fg,fb,255);
8484
// fonsSetColor(data, color);
85-
85+
8686
sth_draw_text(term->font->stash, term->font->id, term->font->size, term->x,
87-
term->y, tsm_screen_get_width(term->console) , -1, ch, fr, fg, fb, 1.0, &dx, &dy);
87+
term->y, tsm_screen_get_width(term->console), -1, ch, fr, fg,
88+
fb, 1.0, &dx, &dy);
8889
// printf("%f %f\n",term->x,term->y);
89-
90+
9091
if (posx >= (screen->size_x - 1)) {
9192
term->x = term->starx;
92-
term->y = dy + term->ascender;
93-
}else{
94-
term->x = dx;
93+
term->y = dy + term->lineh;
94+
} else {
95+
term->x = dx;
9596
}
9697

9798
if (posy > (screen->size_y - 1)) {
9899
term->x = term->starx;
99-
term->y = term->stary;
100+
term->y = term->stary;
100101
}
101102

102103
return 0;
@@ -133,69 +134,68 @@ void init_pty(terminal *term) {
133134
}
134135
}
135136

136-
void terminal_set_mvp(terminal* term,mvp_t *mvp,float width ,float height){
137-
term->mvp=mvp;
137+
void terminal_resize(terminal *term, float width, float height) {
138+
if(width<0||height<0) return;
139+
float w = measure_text(term->font, term->font->size, "W", -1);
140+
float h = graphic_get_font_height(term->font, term->font->size);
141+
tsm_screen_resize(term->console, (width* term->scale / w) - 1, (height* term->scale / (h+term->lineh )+2 ));
142+
shl_pty_resize(term->pty, tsm_screen_get_width(term->console),
143+
tsm_screen_get_height(term->console));
144+
}
145+
146+
void terminal_set_mvp(terminal *term, mvp_t *mvp, float width, float height) {
147+
*term->mvp = *mvp;
138148
mat4_set_identity(&term->mvp->projection);
139149
mat4_set_identity(&term->mvp->model);
140150
mat4_set_identity(&term->mvp->view);
141151
mat4_set_orthographic(&term->mvp->projection, 0, width * term->scale,
142152
height * term->scale, 0, 1, -1);
143-
144-
}
145-
146-
void terminal_resize(terminal* term,int width,int height){
147-
float w=measure_text(term->font,term->font->size,"W",-1);
148-
tsm_screen_resize(term->console, (width / w)-1 , (height / term->lineh)-1);
149-
shl_pty_resize(term->pty, tsm_screen_get_width(term->console), tsm_screen_get_height(term->console));
150153
}
151154

152-
terminal *terminal_create(font_t *font ,float size,float width,float height) {
155+
terminal *terminal_create(font_t *font, float size, float width, float height) {
153156
terminal *term = malloc(sizeof(terminal));
154-
if(font!=NULL){
155-
term->font=font;
156-
}else{
157-
term->font = new_font("./Roboto-Regular.ttf", size);
157+
if (font != NULL) {
158+
term->font = font;
159+
} else {
160+
term->font = new_font("./Roboto-Regular.ttf", size);
158161
}
159162
term->lineh = 10.0;
160163
term->linew = 20.0;
161164
term->ascender = 10.0;
162-
term->scale=2;
165+
term->scale = 2;
163166
term->x = 0;
164167
term->y = 0;
165-
term->mvp=NULL;
166-
168+
term->mvp = malloc(sizeof(mvp_t));
169+
term->width=width;
170+
term->height=height;
167171
tsm_screen_new(&term->console, log_tsm, 0);
168172
tsm_vte_new(&term->vte, term->console, term_write_cb, term, log_tsm, 0);
169-
// tsm_screen_resize(console, (width / (bounds[2]-bounds[0]) - 1), (height /
170-
// lineh)-1);
173+
init_pty(term);
171174

175+
terminal_resize(term, (int)width , (int)height );
172176
printf("console width: %d\n", tsm_screen_get_width(term->console));
173177
printf("console height: %d\n", tsm_screen_get_height(term->console));
174178
tsm_vte_get_def_attr(term->vte, &term->attr);
175179

176180
// char* test="\033[31m red \033[0m \n\033[32m green \033[0m\n\033[33m yellow
177181
// \033[0m"; tsm_vte_input(term->vte, test, strlen(test));
178-
179-
init_pty(term);
180-
181182
return term;
182183
}
183184

184-
185-
void terminal_draw(terminal* term,float x,float y){
185+
void terminal_draw(terminal *term, float x, float y) {
186186
term->x = term->starx;
187187
term->y = term->stary;
188-
term->starx=x*term->scale;
189-
term->stary=y*term->scale;
190-
if(term->mvp==NULL) return;
188+
term->starx = x * term->scale;
189+
term->stary = y * term->scale;
190+
if (term->mvp == NULL) return;
191191
graphic_set_mvp(term->mvp);
192192
sth_begin_draw(term->font->stash);
193193
tsm_screen_draw(term->console, draw_cb, term);
194-
graphic_render_end_string();
194+
sth_end_draw(term->font->stash);
195195
}
196-
void terminal_render(terminal *term,float x ,float y) {
196+
void terminal_render(terminal *term, float x, float y) {
197197
shl_pty_dispatch(term->pty);
198-
terminal_draw(term,x,y);
198+
terminal_draw(term, x, y);
199199
}
200200

201201
void terminal_key_event(terminal *term, int key, int scancode, int action,
@@ -217,9 +217,9 @@ void terminal_key_event(terminal *term, int key, int scancode, int action,
217217
if (scancode == XKB_KEY_Up || scancode == XKB_KEY_Down ||
218218
scancode == XKB_KEY_Left || scancode == XKB_KEY_Right ||
219219
scancode == XKB_KEY_Return || scancode == XKB_KEY_Delete) {
220-
printf("mode %d key=%d scancode=%0x\n", mod,key, scancode);
220+
printf("mode %d key=%d scancode=%0x\n", mod, key, scancode);
221221
tsm_vte_handle_keyboard(term->vte, scancode, key, mod, key);
222-
// terminal_draw(term);
222+
// terminal_draw(term);
223223
}
224224
}
225225
// tsm_vte_get_def_attr(term->vte, &term->attr);
@@ -228,7 +228,7 @@ void terminal_char_event(terminal *term, int ch, int mods) {
228228
char buf[20] = {0};
229229
sprintf(buf, "%c", ch);
230230
// tsm_vte_input(term->vte, buf, strlen(buf));
231-
//printf("terminal_char_event %c\n",buf);
231+
// printf("terminal_char_event %c\n",buf);
232232
tsm_vte_handle_keyboard(term->vte, 0, ch, 0, ch);
233233
}
234234

0 commit comments

Comments
 (0)