Get backspace working. Color escape codes sort of working but rendering is broken
This commit is contained in:
		
							parent
							
								
									2a9e327fdf
								
							
						
					
					
						commit
						bfb4e2a994
					
				
							
								
								
									
										223
									
								
								bt.c
									
									
									
									
									
								
							
							
						
						
									
										223
									
								
								bt.c
									
									
									
									
									
								
							| @ -32,6 +32,7 @@ typedef struct { | ||||
|     float ypos; | ||||
|     int capacity; | ||||
|     int length; | ||||
|     int cursor; | ||||
|     char *buf; | ||||
| } Scrollback; | ||||
| 
 | ||||
| @ -112,13 +113,24 @@ int read_pty(FileDescriptors *fds, Scrollback *sb) { | ||||
|             printf("Reallocated scrollback buffer\n"); | ||||
|         } | ||||
|         if (readbuf[nread - 1] == '\n') { | ||||
|             printf("are you firing when you shouldnt?\n"); | ||||
|             // printf("are you firing when you shouldnt?\n");
 | ||||
|             nread--; | ||||
|         } | ||||
|         // if (nread > 2 && readbuf[0] == '^' && readbuf[1] == '[') {
 | ||||
|             // memcpy(sb->buf + sb->length, readbuf, nread);
 | ||||
|             // Don't do anything for not
 | ||||
|             // return nread;
 | ||||
|         // }
 | ||||
|         if (nread == 3 && readbuf[0] == '\b') { | ||||
|             memmove(sb->buf + sb->length - 1, sb->buf + sb->length, sb->length); | ||||
|             sb->length--; | ||||
|             return nread; | ||||
|         } | ||||
|         for (int i = 0; i < nread; i++) { | ||||
|             if (readbuf[i] == '\r') { | ||||
|                 readbuf[i] = '\n'; | ||||
|             } | ||||
|             // printf("%ld %d\n", nread, readbuf[i]);
 | ||||
|         } | ||||
|         memcpy(sb->buf + sb->length, readbuf, nread); | ||||
|         sb->length += nread; | ||||
| @ -127,43 +139,59 @@ int read_pty(FileDescriptors *fds, Scrollback *sb) { | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| void draw_line(char *buf, int x, int y, SDL_Color color, Context* ctx) { | ||||
|     SDL_Surface* surface = | ||||
|         TTF_RenderText_Blended(ctx->font, buf, color); | ||||
| 
 | ||||
|     if (surface == NULL) { | ||||
|         fprintf(stderr, "DONT PRINT THERES AN ERROR"); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     // now you can convert it into a texture
 | ||||
|     SDL_Texture* texture = SDL_CreateTextureFromSurface(ctx->renderer, surface); | ||||
| 
 | ||||
|     SDL_Rect rect; | ||||
|     rect.x = x; | ||||
|     rect.y = y; | ||||
| 
 | ||||
|     rect.w = surface->w; | ||||
|     rect.h = surface->h; | ||||
| 
 | ||||
|     // last_xpos = surface->w;
 | ||||
| 
 | ||||
|     SDL_RenderCopy(ctx->renderer, texture, NULL, &rect); | ||||
| 
 | ||||
|     SDL_DestroyTexture(texture); | ||||
|     SDL_FreeSurface(surface); | ||||
| } | ||||
| 
 | ||||
| // Not being used right now
 | ||||
| // This function is to handle a prompt in case that's something we have to do at some point
 | ||||
| void handle_prompt(Context* ctx, int last_xpos, int nrow) { | ||||
|     if (ctx->rline_len > 0) { | ||||
|         draw_line(ctx->readline, last_xpos, nrow * 20, WHITE, ctx); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| void render_scrollback(Context* ctx) { | ||||
|     SDL_RenderClear(ctx->renderer); | ||||
| 
 | ||||
|     // SDL_Color current_color = WHITE;
 | ||||
|     SDL_Color current_color = WHITE; | ||||
|     int col_max = 120; | ||||
|     char row_buf[col_max]; | ||||
|     int nrow = 0; | ||||
|     int ncol = 0; | ||||
|     int last_xpos = 0; | ||||
|     // int last_xpos = 0;
 | ||||
|     Scrollback sb = *ctx->sb; | ||||
|     for (int c = 0; c < sb.length; c++) { | ||||
|         if ((sb.buf[c] == '\n' && ncol > 0) || c == sb.length - 1) { | ||||
|             row_buf[ncol] = '\0'; | ||||
|             SDL_Surface* surface = | ||||
|                 TTF_RenderText_Blended(ctx->font, row_buf, WHITE); | ||||
| 
 | ||||
|             if (surface == NULL) { | ||||
|                 fprintf(stderr, "DONT PRINT THERES AN ERROR"); | ||||
|                 return; | ||||
|             if (c == sb.length - 1) { | ||||
|                 row_buf[ncol++] = sb.buf[c]; | ||||
|             } | ||||
| 
 | ||||
|             // now you can convert it into a texture
 | ||||
|             SDL_Texture* texture = SDL_CreateTextureFromSurface(ctx->renderer, surface); | ||||
| 
 | ||||
|             SDL_Rect rect; | ||||
|             rect.x = 0; | ||||
|             rect.y = nrow * 20; | ||||
| 
 | ||||
|             rect.w = surface->w; | ||||
|             rect.h = surface->h; | ||||
| 
 | ||||
|             last_xpos = surface->w; | ||||
| 
 | ||||
|             SDL_RenderCopy(ctx->renderer, texture, NULL, &rect); | ||||
| 
 | ||||
|             SDL_DestroyTexture(texture); | ||||
|             SDL_FreeSurface(surface); | ||||
|             row_buf[ncol] = '\0'; | ||||
|             draw_line(row_buf, 0, nrow * 20, current_color, ctx); | ||||
|             // last_xpos = surface->w;
 | ||||
| 
 | ||||
|             if (c < sb.length - 1) { | ||||
|                 nrow++; | ||||
| @ -174,56 +202,49 @@ void render_scrollback(Context* ctx) { | ||||
|         } else if (sb.buf[c] == '\x1b') { | ||||
|             int c2 = c + 1; | ||||
|             // Control Sequence Introducer
 | ||||
|             // int csi_args[16];
 | ||||
|             // char csi_code;
 | ||||
|             // int nargs = 0;
 | ||||
|             int csi_args[16]; | ||||
|             char csi_code; | ||||
|             int nargs = 0; | ||||
|             if (sb.buf[c2] == '[') { | ||||
|                 // char *esc_buf = sb.buf + c2 + 1;
 | ||||
|                 char *esc_buf = sb.buf + c2 + 1; | ||||
|                 while (true) { | ||||
|                     // char *substr;
 | ||||
|                     // // long num = strtol(esc_buf, &substr, 10);
 | ||||
|                     // if (esc_buf != substr) {
 | ||||
|                     //     // csi_args[nargs++] = num;
 | ||||
|                     //     esc_buf = substr;
 | ||||
|                     //     if (substr[0] == ';') {
 | ||||
|                     //         esc_buf++;
 | ||||
|                     //     }
 | ||||
|                     //     continue;
 | ||||
|                     // }
 | ||||
|                     // csi_code = substr[0];
 | ||||
|                     // SDL_Color new_color = WHITE;
 | ||||
|                     // switch (csi_code) {
 | ||||
|                     //     case 'm':
 | ||||
|                     //         for (int i = 0; i < nargs; i++) {
 | ||||
|                     //             if (csi_args[i] == 31) {
 | ||||
|                     //                 new_color = RED;
 | ||||
|                     //             } else if (csi_args[i] == 32) {
 | ||||
|                     //                 new_color = GREEN;
 | ||||
|                     //             } else if (csi_args[i] == 33) {
 | ||||
|                     //                 new_color = YELLOW;
 | ||||
|                     //             } else if (csi_args[i] == 34) {
 | ||||
|                     //                 new_color = BLUE;
 | ||||
|                     //             } else if (csi_args[i] == 35) {
 | ||||
|                     //                 new_color = MAGENTA;
 | ||||
|                     //             } else if (csi_args[i] == 36) {
 | ||||
|                     //                 new_color = SKYBLUE;
 | ||||
|                     //             } else if (csi_args[i] == 0) {
 | ||||
|                     //                 new_color = RAYWHITE;
 | ||||
|                     //             }
 | ||||
|                     //         }
 | ||||
|                     //         row_buf[ncol] = '\0';
 | ||||
|                     //         // TODO: This looks like the actual place where we
 | ||||
|                     //         // draw the text at a line
 | ||||
|                     //         // Vector2 pos = { row_posx, nrow * row_height + sb.ypos };
 | ||||
|                     //         // DrawTextEx(*fontDefault, row_buf, pos, fontsize, 0, current_color);
 | ||||
|                     //         // current_color = new_color;
 | ||||
|                     //         ncol = 0;
 | ||||
|                     //         c += (substr) - (sb.buf + c);
 | ||||
|                     //         // TODO: Get the text height
 | ||||
|                     //         // int width = MeasureTextEx(*fontDefault, row_buf, fontsize, 1).x;
 | ||||
|                     //         // row_posx += width + 0.85;
 | ||||
|                     //         break;
 | ||||
|                     // }
 | ||||
|                     char *substr; | ||||
|                     long num = strtol(esc_buf, &substr, 10); | ||||
|                     if (esc_buf != substr) { | ||||
|                         csi_args[nargs++] = num; | ||||
|                         esc_buf = substr; | ||||
|                         if (substr[0] == ';') { | ||||
|                             esc_buf++; | ||||
|                         } | ||||
|                         continue; | ||||
|                     } | ||||
|                     csi_code = substr[0]; | ||||
|                     SDL_Color new_color = current_color; | ||||
|                     switch (csi_code) { | ||||
|                         case 'm': | ||||
|                             for (int i = 0; i < nargs; i++) { | ||||
|                                 if (csi_args[i] == 31) { | ||||
|                                     new_color = RED; | ||||
|                                 } else if (csi_args[i] == 32) { | ||||
|                                     new_color = GREEN; | ||||
|                                 } else if (csi_args[i] == 33) { | ||||
|                                     new_color = YELLOW; | ||||
|                                 } else if (csi_args[i] == 34) { | ||||
|                                     new_color = BLUE; | ||||
|                                 } else if (csi_args[i] == 35) { | ||||
|                                     new_color = MAGENTA; | ||||
|                                 } else if (csi_args[i] == 36) { | ||||
|                                     new_color = SKYBLUE; | ||||
|                                 } else if (csi_args[i] == 0) { | ||||
|                                     // new_color = RAYWHITE;
 | ||||
|                                     // new_color = WHITE;
 | ||||
|                                 } | ||||
|                                 // printf("%d %d\n", i, csi_args[i]);
 | ||||
|                             } | ||||
|                             c += (substr) - (sb.buf + c); | ||||
|                             current_color = new_color; | ||||
|                             break; | ||||
|                     } | ||||
|                     break; | ||||
|                 } | ||||
|             } | ||||
| @ -233,33 +254,6 @@ void render_scrollback(Context* ctx) { | ||||
|     } | ||||
| 
 | ||||
|     // We drew the scrollback buffer, now draw the user's prompt
 | ||||
|     if (ctx->rline_len > 0) { | ||||
|         SDL_Surface* surface = | ||||
|             TTF_RenderText_Blended(ctx->font, ctx->readline, WHITE); | ||||
| 
 | ||||
|         if (surface == NULL) { | ||||
|             fprintf(stderr, "DONT PRINT THERES AN ERROR"); | ||||
|             return; | ||||
|         } | ||||
| 
 | ||||
|         // now you can convert it into a texture
 | ||||
|         SDL_Texture* texture = SDL_CreateTextureFromSurface(ctx->renderer, surface); | ||||
| 
 | ||||
|         SDL_Rect rect; | ||||
|         rect.x = last_xpos + 10; | ||||
|         rect.y = nrow * 20; | ||||
| 
 | ||||
|         rect.w = surface->w; | ||||
|         rect.h = surface->h; | ||||
| 
 | ||||
| 
 | ||||
|         SDL_RenderCopy(ctx->renderer, texture, NULL, &rect); | ||||
| 
 | ||||
|         SDL_DestroyTexture(texture); | ||||
|         SDL_FreeSurface(surface); | ||||
|          | ||||
|     } | ||||
| 
 | ||||
|     SDL_RenderPresent(ctx->renderer); | ||||
| } | ||||
| 
 | ||||
| @ -343,23 +337,26 @@ int main(void) { | ||||
|                     printf("%s\n", sb.buf); | ||||
|                     printf("============\n"); | ||||
|                 } else if ((key >= 32) && (key <= 125)) { | ||||
|                     ctx.readline[ctx.rline_len] = (char)key; | ||||
|                     ctx.rline_len++; | ||||
|                     ctx.readline[ctx.rline_len] = '\0'; | ||||
|                     // ctx.readline[ctx.rline_len] = (char)key;
 | ||||
|                     // ctx.rline_len++;
 | ||||
|                     // ctx.readline[ctx.rline_len] = '\0';
 | ||||
|                     // printf("%d\n", key);
 | ||||
|                     write(fds.parent, &key, 1); | ||||
|                 } | ||||
| 
 | ||||
|                 if (key == SDLK_RETURN) { | ||||
|                     ctx.readline[ctx.rline_len] = '\r'; | ||||
|                     ctx.rline_len++; | ||||
|                     write(fds.parent, ctx.readline, ctx.rline_len); | ||||
|                     ctx.rline_len = 0; | ||||
|                     char k = '\r'; | ||||
|                     write(fds.parent, &k, 1); | ||||
|                     new_char = true; | ||||
|                 } | ||||
|                 if (key == SDLK_BACKSPACE) { | ||||
|                     if (ctx.rline_len > 0) { | ||||
|                         ctx.rline_len--; | ||||
|                         ctx.readline[ctx.rline_len] = '\0'; | ||||
|                     } | ||||
|                     // if (ctx.rline_len > 0) {
 | ||||
|                     //     ctx.rline_len--;
 | ||||
|                     //     ctx.readline[ctx.rline_len] = '\0';
 | ||||
|                     // }
 | ||||
|                     char k = '\177'; | ||||
|                     write(fds.parent, &k, 1); | ||||
| 
 | ||||
|                     new_char = true; | ||||
|                 } | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user