diff --git a/caca/driver_gl.c b/caca/driver_gl.c index e596bd3..43c3af0 100644 --- a/caca/driver_gl.c +++ b/caca/driver_gl.c @@ -163,116 +163,116 @@ static int gl_init_graphics(caca_display_t *dp) glEnable(GL_TEXTURE_2D); for(i = 32; i < 128; i++) - { - glGenTextures(1, (GLuint*)&dp->drv.p->id[i - 32]); - glBindTexture(GL_TEXTURE_2D, dp->drv.p->id[i - 32]); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, - 16, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, empty_texture); - } + { + glGenTextures(1, (GLuint*)&dp->drv.p->id[i - 32]); + glBindTexture(GL_TEXTURE_2D, dp->drv.p->id[i - 32]); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, + 16, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, empty_texture); + } for(i = 0; i < 8; i++) - { - glGenTextures(1, (GLuint*)&dp->drv.p->id_uni[i]); - glBindTexture(GL_TEXTURE_2D, dp->drv.p->id_uni[i]); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, - 16, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, empty_texture); - } + { + glGenTextures(1, (GLuint*)&dp->drv.p->id_uni[i]); + glBindTexture(GL_TEXTURE_2D, dp->drv.p->id_uni[i]); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, + 16, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, empty_texture); + } for(i = 32; i < 128; i++) - { - glDisable(GL_TEXTURE_2D); - glClear(GL_COLOR_BUFFER_BIT); + { + glDisable(GL_TEXTURE_2D); + glClear(GL_COLOR_BUFFER_BIT); - glColor3f(1, 1, 1); - glRasterPos2f(0, 15); - glutBitmapCharacter(GLUT_BITMAP_9_BY_15, i); + glColor3f(1, 1, 1); + glRasterPos2f(0, 15); + glutBitmapCharacter(GLUT_BITMAP_9_BY_15, i); - glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, dp->drv.p->id[i - 32]); - glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, - 0, dp->drv.p->height - 16, 16, 16, 0); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, dp->drv.p->id[i - 32]); + glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, + 0, dp->drv.p->height - 16, 16, 16, 0); #ifdef HAVE_GLUTCHECKLOOP - glutCheckLoop(); + glutCheckLoop(); #else - glutMainLoopEvent(); + glutMainLoopEvent(); #endif - glutSwapBuffers(); + glutSwapBuffers(); - } + } /* CP437 hack */ for(i = 0; i < 8; i++) - { - glDisable(GL_TEXTURE_2D); - glClear(GL_COLOR_BUFFER_BIT); - glColor3f(1, 1, 1); - glTranslatef(0.5,0.5,0); - - if(i==0) /* 0x00002580*/ - { - glBegin(GL_QUADS); - glVertex2f(0,0); glVertex2f(9,0); glVertex2f(9,7); glVertex2f(0,7); - glEnd(); - } - else if(i==1) /* 0x00002584*/ - { - glBegin(GL_QUADS); - glVertex2f(0,7); glVertex2f(9,7); glVertex2f(9,15); glVertex2f(0,15); - glEnd(); - } - else if(i==2) /* 0x00002588*/ - { - glBegin(GL_QUADS); - glVertex2f(0,0); glVertex2f(9,0); glVertex2f(9,15); glVertex2f(0,15); - glEnd(); - } - else if(i==3) /* 0x0000258c*/ - { - glBegin(GL_QUADS); - glVertex2f(0,0); glVertex2f(4,0); glVertex2f(4,15); glVertex2f(0,15); - glEnd(); - } - else if(i==4) /* 0x00002590*/ - { - glBegin(GL_QUADS); - glVertex2f(4,0); glVertex2f(9,0); glVertex2f(9,15); glVertex2f(4,15); - glEnd(); - } - else if(i>=5) /* 0x00002591*/ - { - int a, j, k = i-5; - for(j = dp->drv.p->font_height; j--; ) - for(a = dp->drv.p->font_width; a--; ) + { + glDisable(GL_TEXTURE_2D); + glClear(GL_COLOR_BUFFER_BIT); + glColor3f(1, 1, 1); + glTranslatef(0.5,0.5,0); + + if(i==0) /* 0x00002580*/ + { + glBegin(GL_QUADS); + glVertex2f(0,0); glVertex2f(9,0); glVertex2f(9,7); glVertex2f(0,7); + glEnd(); + } + else if(i==1) /* 0x00002584*/ + { + glBegin(GL_QUADS); + glVertex2f(0,7); glVertex2f(9,7); glVertex2f(9,15); glVertex2f(0,15); + glEnd(); + } + else if(i==2) /* 0x00002588*/ + { + glBegin(GL_QUADS); + glVertex2f(0,0); glVertex2f(9,0); glVertex2f(9,15); glVertex2f(0,15); + glEnd(); + } + else if(i==3) /* 0x0000258c*/ + { + glBegin(GL_QUADS); + glVertex2f(0,0); glVertex2f(4,0); glVertex2f(4,15); glVertex2f(0,15); + glEnd(); + } + else if(i==4) /* 0x00002590*/ { - if(((a + 2 * (j & 1)) & 3) > k) - continue; + glBegin(GL_QUADS); + glVertex2f(4,0); glVertex2f(9,0); glVertex2f(9,15); glVertex2f(4,15); + glEnd(); + } + else if(i>=5) /* 0x00002591*/ + { + int a, j, k = i-5; + for(j = dp->drv.p->font_height; j--; ) + for(a = dp->drv.p->font_width; a--; ) + { + if(((a + 2 * (j & 1)) & 3) > k) + continue; - glBegin(GL_POINTS); - glVertex2f(a, j); - glEnd(); - } - } + glBegin(GL_POINTS); + glVertex2f(a, j); + glEnd(); + } + } - glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, dp->drv.p->id_uni[i]); - glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, - 0, dp->drv.p->height - 16, 16, 16, 0); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, dp->drv.p->id_uni[i]); + glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, + 0, dp->drv.p->height - 16, 16, 16, 0); #ifdef HAVE_GLUTCHECKLOOP - glutCheckLoop(); + glutCheckLoop(); #else - glutMainLoopEvent(); + glutMainLoopEvent(); #endif - glutSwapBuffers(); - glutPostRedisplay(); + glutSwapBuffers(); + glutPostRedisplay(); - } + } @@ -310,27 +310,27 @@ static void gl_display(caca_display_t *dp) line = 0; for(y = 0; y < dp->drv.p->height; y += dp->drv.p->font_height) - { - uint32_t *attr = dp->cv->attr + line * dp->cv->width; - - for(x = 0; x < dp->drv.p->width; x += dp->drv.p->font_width) { - uint16_t bg = _cucul_argb32_to_rgb12bg(*attr++); - glDisable(GL_TEXTURE_2D); - glColor3b(((bg & 0xf00) >> 8) * 8, - ((bg & 0x0f0) >> 4) * 8, - (bg & 0x00f) * 8); - glBegin(GL_QUADS); - glVertex2f(x, y); - glVertex2f(x + dp->drv.p->font_width, y); - glVertex2f(x + dp->drv.p->font_width, - y + dp->drv.p->font_height); - glVertex2f(x, y + dp->drv.p->font_height); - glEnd(); - } + uint32_t *attr = dp->cv->attr + line * dp->cv->width; - line++; - } + for(x = 0; x < dp->drv.p->width; x += dp->drv.p->font_width) + { + uint16_t bg = _cucul_argb32_to_rgb12bg(*attr++); + glDisable(GL_TEXTURE_2D); + glColor3b(((bg & 0xf00) >> 8) * 8, + ((bg & 0x0f0) >> 4) * 8, + (bg & 0x00f) * 8); + glBegin(GL_QUADS); + glVertex2f(x, y); + glVertex2f(x + dp->drv.p->font_width, y); + glVertex2f(x + dp->drv.p->font_width, + y + dp->drv.p->font_height); + glVertex2f(x, y + dp->drv.p->font_height); + glEnd(); + } + + line++; + } /* 2nd pass, avoids changing render state too much */ glEnable(GL_BLEND); @@ -339,70 +339,70 @@ static void gl_display(caca_display_t *dp) line = 0; for(y = 0; y < dp->drv.p->height; y += dp->drv.p->font_height) - { - uint32_t *attr = dp->cv->attr + line * dp->cv->width; - uint32_t *chars = dp->cv->chars + line * dp->cv->width; - - for(x = 0; x < dp->drv.p->width; x += dp->drv.p->font_width) { - uint32_t cv = *chars++; - - if(cv > 0x00000020 && cv < 0x00000080) - { - uint16_t fg = _cucul_argb32_to_rgb12fg(*attr); - glBindTexture(GL_TEXTURE_2D, dp->drv.p->id[cv - 32]); - glColor3b(((fg & 0xf00) >> 8) * 8, - ((fg & 0x0f0) >> 4) * 8, - (fg & 0x00f) * 8); - glBegin(GL_QUADS); - glTexCoord2f(0, dp->drv.p->sh); - glVertex2f(x, y); - glTexCoord2f(dp->drv.p->sw, dp->drv.p->sh); - glVertex2f(x + dp->drv.p->font_width, y); - glTexCoord2f(dp->drv.p->sw, 0); - glVertex2f(x + dp->drv.p->font_width, - y + dp->drv.p->font_height); - glTexCoord2f(0, 0); - glVertex2f(x, y + dp->drv.p->font_height); - glEnd(); - } - else if(cv!=' ') - { - switch(cv) - { - case 0x00002580: glBindTexture(GL_TEXTURE_2D, dp->drv.p->id_uni[0]); break; - case 0x00002584: glBindTexture(GL_TEXTURE_2D, dp->drv.p->id_uni[1]); break; - case 0x00002588: glBindTexture(GL_TEXTURE_2D, dp->drv.p->id_uni[2]); break; - case 0x0000258c: glBindTexture(GL_TEXTURE_2D, dp->drv.p->id_uni[3]); break; - case 0x00002590: glBindTexture(GL_TEXTURE_2D, dp->drv.p->id_uni[4]); break; - case 0x00002591: glBindTexture(GL_TEXTURE_2D, dp->drv.p->id_uni[5]); break; - case 0x00002592: glBindTexture(GL_TEXTURE_2D, dp->drv.p->id_uni[6]); break; - case 0x00002593: glBindTexture(GL_TEXTURE_2D, dp->drv.p->id_uni[7]); break; - default: glBindTexture(GL_TEXTURE_2D, dp->drv.p->id['?' - 32]); break; - } - - uint16_t fg = _cucul_argb32_to_rgb12fg(*attr); - glColor3b(((fg & 0xf00) >> 8) * 8, - ((fg & 0x0f0) >> 4) * 8, - (fg & 0x00f) * 8); - glBegin(GL_QUADS); - glTexCoord2f(0, dp->drv.p->sh); - glVertex2f(x, y); - glTexCoord2f(dp->drv.p->sw, dp->drv.p->sh); - glVertex2f(x + dp->drv.p->font_width, y); - glTexCoord2f(dp->drv.p->sw, 0); - glVertex2f(x + dp->drv.p->font_width, - y + dp->drv.p->font_height); - glTexCoord2f(0, 0); - glVertex2f(x, y + dp->drv.p->font_height); - glEnd(); - - } + uint32_t *attr = dp->cv->attr + line * dp->cv->width; + uint32_t *chars = dp->cv->chars + line * dp->cv->width; - attr++; + for(x = 0; x < dp->drv.p->width; x += dp->drv.p->font_width) + { + uint32_t cv = *chars++; + + if(cv > 0x00000020 && cv < 0x00000080) + { + uint16_t fg = _cucul_argb32_to_rgb12fg(*attr); + glBindTexture(GL_TEXTURE_2D, dp->drv.p->id[cv - 32]); + glColor3b(((fg & 0xf00) >> 8) * 8, + ((fg & 0x0f0) >> 4) * 8, + (fg & 0x00f) * 8); + glBegin(GL_QUADS); + glTexCoord2f(0, dp->drv.p->sh); + glVertex2f(x, y); + glTexCoord2f(dp->drv.p->sw, dp->drv.p->sh); + glVertex2f(x + dp->drv.p->font_width, y); + glTexCoord2f(dp->drv.p->sw, 0); + glVertex2f(x + dp->drv.p->font_width, + y + dp->drv.p->font_height); + glTexCoord2f(0, 0); + glVertex2f(x, y + dp->drv.p->font_height); + glEnd(); + } + else if(cv!=' ') + { + switch(cv) + { + case 0x00002580: glBindTexture(GL_TEXTURE_2D, dp->drv.p->id_uni[0]); break; + case 0x00002584: glBindTexture(GL_TEXTURE_2D, dp->drv.p->id_uni[1]); break; + case 0x00002588: glBindTexture(GL_TEXTURE_2D, dp->drv.p->id_uni[2]); break; + case 0x0000258c: glBindTexture(GL_TEXTURE_2D, dp->drv.p->id_uni[3]); break; + case 0x00002590: glBindTexture(GL_TEXTURE_2D, dp->drv.p->id_uni[4]); break; + case 0x00002591: glBindTexture(GL_TEXTURE_2D, dp->drv.p->id_uni[5]); break; + case 0x00002592: glBindTexture(GL_TEXTURE_2D, dp->drv.p->id_uni[6]); break; + case 0x00002593: glBindTexture(GL_TEXTURE_2D, dp->drv.p->id_uni[7]); break; + default: glBindTexture(GL_TEXTURE_2D, dp->drv.p->id['?' - 32]); break; + } + + uint16_t fg = _cucul_argb32_to_rgb12fg(*attr); + glColor3b(((fg & 0xf00) >> 8) * 8, + ((fg & 0x0f0) >> 4) * 8, + (fg & 0x00f) * 8); + glBegin(GL_QUADS); + glTexCoord2f(0, dp->drv.p->sh); + glVertex2f(x, y); + glTexCoord2f(dp->drv.p->sw, dp->drv.p->sh); + glVertex2f(x + dp->drv.p->font_width, y); + glTexCoord2f(dp->drv.p->sw, 0); + glVertex2f(x + dp->drv.p->font_width, + y + dp->drv.p->font_height); + glTexCoord2f(0, 0); + glVertex2f(x, y + dp->drv.p->font_height); + glEnd(); + + } + + attr++; + } + line++; } - line++; - } glDisable(GL_BLEND); glDisable(GL_TEXTURE_2D); @@ -439,88 +439,88 @@ static int gl_get_event(caca_display_t *dp, caca_event_t *ev) #ifdef HAVE_GLUTCLOSEFUNC if(dp->drv.p->close) - { - dp->drv.p->close = 0; - ev->type = CACA_EVENT_QUIT; - return 1; - } + { + dp->drv.p->close = 0; + ev->type = CACA_EVENT_QUIT; + return 1; + } #endif if(dp->resize.resized) - { - ev->type = CACA_EVENT_RESIZE; - ev->data.resize.w = dp->cv->width; - ev->data.resize.h = dp->cv->height; - return 1; - } + { + ev->type = CACA_EVENT_RESIZE; + ev->data.resize.w = dp->cv->width; + ev->data.resize.h = dp->cv->height; + return 1; + } if(dp->drv.p->mouse_changed) - { - ev->type = CACA_EVENT_MOUSE_MOTION; - ev->data.mouse.x = dp->mouse.x; - ev->data.mouse.y = dp->mouse.y; - dp->drv.p->mouse_changed = 0; - - if(dp->drv.p->mouse_clicked) { - _push_event(dp, ev); - ev->type = CACA_EVENT_MOUSE_PRESS; - ev->data.mouse.button = dp->drv.p->mouse_button; - dp->drv.p->mouse_clicked = 0; - } + ev->type = CACA_EVENT_MOUSE_MOTION; + ev->data.mouse.x = dp->mouse.x; + ev->data.mouse.y = dp->mouse.y; + dp->drv.p->mouse_changed = 0; - return 1; - } + if(dp->drv.p->mouse_clicked) + { + _push_event(dp, ev); + ev->type = CACA_EVENT_MOUSE_PRESS; + ev->data.mouse.button = dp->drv.p->mouse_button; + dp->drv.p->mouse_clicked = 0; + } + + return 1; + } if(dp->drv.p->key != 0) - { - ev->type = CACA_EVENT_KEY_PRESS; - ev->data.key.ch = dp->drv.p->key; - ev->data.key.ucs4 = (uint32_t)dp->drv.p->key; - ev->data.key.utf8[0] = dp->drv.p->key; - ev->data.key.utf8[1] = '\0'; - dp->drv.p->key = 0; - return 1; - } + { + ev->type = CACA_EVENT_KEY_PRESS; + ev->data.key.ch = dp->drv.p->key; + ev->data.key.ucs4 = (uint32_t)dp->drv.p->key; + ev->data.key.utf8[0] = dp->drv.p->key; + ev->data.key.utf8[1] = '\0'; + dp->drv.p->key = 0; + return 1; + } if(dp->drv.p->special_key != 0) - { - switch(dp->drv.p->special_key) { - case GLUT_KEY_F1 : ev->data.key.ch = CACA_KEY_F1; break; - case GLUT_KEY_F2 : ev->data.key.ch = CACA_KEY_F2; break; - case GLUT_KEY_F3 : ev->data.key.ch = CACA_KEY_F3; break; - case GLUT_KEY_F4 : ev->data.key.ch = CACA_KEY_F4; break; - case GLUT_KEY_F5 : ev->data.key.ch = CACA_KEY_F5; break; - case GLUT_KEY_F6 : ev->data.key.ch = CACA_KEY_F6; break; - case GLUT_KEY_F7 : ev->data.key.ch = CACA_KEY_F7; break; - case GLUT_KEY_F8 : ev->data.key.ch = CACA_KEY_F8; break; - case GLUT_KEY_F9 : ev->data.key.ch = CACA_KEY_F9; break; - case GLUT_KEY_F10: ev->data.key.ch = CACA_KEY_F10; break; - case GLUT_KEY_F11: ev->data.key.ch = CACA_KEY_F11; break; - case GLUT_KEY_F12: ev->data.key.ch = CACA_KEY_F12; break; - case GLUT_KEY_LEFT : ev->data.key.ch = CACA_KEY_LEFT; break; - case GLUT_KEY_RIGHT: ev->data.key.ch = CACA_KEY_RIGHT; break; - case GLUT_KEY_UP : ev->data.key.ch = CACA_KEY_UP; break; - case GLUT_KEY_DOWN : ev->data.key.ch = CACA_KEY_DOWN; break; - case GLUT_KEY_PAGE_UP : ev->data.key.ch = CACA_KEY_PAGEUP; break; - case GLUT_KEY_PAGE_DOWN : ev->data.key.ch = CACA_KEY_PAGEDOWN; - break; - case GLUT_KEY_HOME : ev->data.key.ch = CACA_KEY_HOME; break; - case GLUT_KEY_END : ev->data.key.ch = CACA_KEY_END; break; - case GLUT_KEY_INSERT : ev->data.key.ch = CACA_KEY_INSERT; break; - - default: ev->type = CACA_EVENT_NONE; return 0; + switch(dp->drv.p->special_key) + { + case GLUT_KEY_F1 : ev->data.key.ch = CACA_KEY_F1; break; + case GLUT_KEY_F2 : ev->data.key.ch = CACA_KEY_F2; break; + case GLUT_KEY_F3 : ev->data.key.ch = CACA_KEY_F3; break; + case GLUT_KEY_F4 : ev->data.key.ch = CACA_KEY_F4; break; + case GLUT_KEY_F5 : ev->data.key.ch = CACA_KEY_F5; break; + case GLUT_KEY_F6 : ev->data.key.ch = CACA_KEY_F6; break; + case GLUT_KEY_F7 : ev->data.key.ch = CACA_KEY_F7; break; + case GLUT_KEY_F8 : ev->data.key.ch = CACA_KEY_F8; break; + case GLUT_KEY_F9 : ev->data.key.ch = CACA_KEY_F9; break; + case GLUT_KEY_F10: ev->data.key.ch = CACA_KEY_F10; break; + case GLUT_KEY_F11: ev->data.key.ch = CACA_KEY_F11; break; + case GLUT_KEY_F12: ev->data.key.ch = CACA_KEY_F12; break; + case GLUT_KEY_LEFT : ev->data.key.ch = CACA_KEY_LEFT; break; + case GLUT_KEY_RIGHT: ev->data.key.ch = CACA_KEY_RIGHT; break; + case GLUT_KEY_UP : ev->data.key.ch = CACA_KEY_UP; break; + case GLUT_KEY_DOWN : ev->data.key.ch = CACA_KEY_DOWN; break; + case GLUT_KEY_PAGE_UP : ev->data.key.ch = CACA_KEY_PAGEUP; break; + case GLUT_KEY_PAGE_DOWN : ev->data.key.ch = CACA_KEY_PAGEDOWN; + break; + case GLUT_KEY_HOME : ev->data.key.ch = CACA_KEY_HOME; break; + case GLUT_KEY_END : ev->data.key.ch = CACA_KEY_END; break; + case GLUT_KEY_INSERT : ev->data.key.ch = CACA_KEY_INSERT; break; + + default: ev->type = CACA_EVENT_NONE; return 0; + } + + ev->type = CACA_EVENT_KEY_PRESS; + ev->data.key.ucs4 = 0; + ev->data.key.utf8[0] = '\0'; + + dp->drv.p->special_key = 0; + return 1; } - ev->type = CACA_EVENT_KEY_PRESS; - ev->data.key.ucs4 = 0; - ev->data.key.utf8[0] = '\0'; - - dp->drv.p->special_key = 0; - return 1; - } - ev->type = CACA_EVENT_NONE; return 0; } @@ -557,15 +557,15 @@ static void gl_handle_reshape(int w, int h) caca_display_t *dp = gl_d; if(dp->drv.p->bit) /* Do not handle reshaping at the first time */ - { - dp->drv.p->new_width = w; - dp->drv.p->new_height = h; + { + dp->drv.p->new_width = w; + dp->drv.p->new_height = h; - dp->resize.w = w / dp->drv.p->font_width; - dp->resize.h = (h / dp->drv.p->font_height) + 1; + dp->resize.w = w / dp->drv.p->font_width; + dp->resize.h = (h / dp->drv.p->font_height) + 1; - dp->resize.resized = 1; - } + dp->resize.resized = 1; + } else dp->drv.p->bit = 1; } diff --git a/cucul/import.c b/cucul/import.c index ae92b33..9c43790 100644 --- a/cucul/import.c +++ b/cucul/import.c @@ -75,24 +75,24 @@ cucul_canvas_t * cucul_import_canvas(cucul_buffer_t *buffer, char const *format) /* Autodetection */ if(!strcasecmp("", format)) - { - unsigned int i=0; - /* if 4 first letters are CACA */ - if(buffer->size >= 4 && - buf[0] == 'C' && buf[1] == 'A' && buf[2] == 'C' && buf[3] != 'A') - return import_caca(buffer->data, buffer->size); - - /* If we find ESC[ argv, we guess it's an ANSI file */ - while(i < buffer->size - 1) { - if((buf[i] == 0x1b) && (buf[i+1] == '[')) - return import_ansi(buffer->data, buffer->size); - i++; - } + unsigned int i=0; + /* if 4 first letters are CACA */ + if(buffer->size >= 4 && + buf[0] == 'C' && buf[1] == 'A' && buf[2] == 'C' && buf[3] != 'A') + return import_caca(buffer->data, buffer->size); + + /* If we find ESC[ argv, we guess it's an ANSI file */ + while(i < buffer->size - 1) + { + if((buf[i] == 0x1b) && (buf[i+1] == '[')) + return import_ansi(buffer->data, buffer->size); + i++; + } - /* Otherwise, import it as text */ - return import_text(buffer->data, buffer->size); - } + /* Otherwise, import it as text */ + return import_text(buffer->data, buffer->size); + } return NULL; } @@ -108,13 +108,13 @@ cucul_canvas_t * cucul_import_canvas(cucul_buffer_t *buffer, char const *format) char const * const * cucul_get_import_list(void) { static char const * const list[] = - { - "", "autodetect", - "text", "plain text", - "caca", "native libcaca format", - "ansi", "ANSI coloured text", - NULL, NULL - }; + { + "", "autodetect", + "text", "plain text", + "caca", "native libcaca format", + "ansi", "ANSI coloured text", + NULL, NULL + }; return list; } @@ -139,9 +139,9 @@ static cucul_canvas_t *import_caca(void const *data, unsigned int size) return NULL; width = ((uint32_t)buf[8] << 24) | ((uint32_t)buf[9] << 16) - | ((uint32_t)buf[10] << 8) | (uint32_t)buf[11]; + | ((uint32_t)buf[10] << 8) | (uint32_t)buf[11]; height = ((uint32_t)buf[12] << 24) | ((uint32_t)buf[13] << 16) - | ((uint32_t)buf[14] << 8) | (uint32_t)buf[15]; + | ((uint32_t)buf[14] << 8) | (uint32_t)buf[15]; if(!width || !height) return NULL; @@ -155,16 +155,16 @@ static cucul_canvas_t *import_caca(void const *data, unsigned int size) return NULL; for(n = height * width; n--; ) - { - cv->chars[n] = ((uint32_t)buf[16 + 0 + 8 * n] << 24) - | ((uint32_t)buf[16 + 1 + 8 * n] << 16) - | ((uint32_t)buf[16 + 2 + 8 * n] << 8) - | (uint32_t)buf[16 + 3 + 8 * n]; - cv->attr[n] = ((uint32_t)buf[16 + 4 + 8 * n] << 24) - | ((uint32_t)buf[16 + 5 + 8 * n] << 16) - | ((uint32_t)buf[16 + 6 + 8 * n] << 8) - | (uint32_t)buf[16 + 7 + 8 * n]; - } + { + cv->chars[n] = ((uint32_t)buf[16 + 0 + 8 * n] << 24) + | ((uint32_t)buf[16 + 1 + 8 * n] << 16) + | ((uint32_t)buf[16 + 2 + 8 * n] << 8) + | (uint32_t)buf[16 + 3 + 8 * n]; + cv->attr[n] = ((uint32_t)buf[16 + 4 + 8 * n] << 24) + | ((uint32_t)buf[16 + 5 + 8 * n] << 16) + | ((uint32_t)buf[16 + 6 + 8 * n] << 8) + | (uint32_t)buf[16 + 7 + 8 * n]; + } return cv; } @@ -179,33 +179,33 @@ static cucul_canvas_t *import_text(void const *data, unsigned int size) cucul_set_color(cv, CUCUL_COLOR_DEFAULT, CUCUL_COLOR_TRANSPARENT); for(i = 0; i < size; i++) - { - unsigned char ch = *text++; + { + unsigned char ch = *text++; - if(ch == '\r') - continue; + if(ch == '\r') + continue; - if(ch == '\n') - { - x = 0; - y++; - continue; - } + if(ch == '\n') + { + x = 0; + y++; + continue; + } - if(x >= width || y >= height) - { - if(x >= width) - width = x + 1; + if(x >= width || y >= height) + { + if(x >= width) + width = x + 1; - if(y >= height) - height = y + 1; + if(y >= height) + height = y + 1; - cucul_set_canvas_size(cv, width, height); - } + cucul_set_canvas_size(cv, width, height); + } - cucul_putchar(cv, x, y, ch); - x++; - } + cucul_putchar(cv, x, y, ch); + x++; + } return cv; } @@ -223,155 +223,155 @@ static cucul_canvas_t *import_ansi(void const *data, unsigned int size) ansi_parse_grcm(cv, &grcm, 1, &dummy); for(i = 0; i < size; i += skip) - { - skip = 1; - - /* Wrap long lines */ - if((unsigned int)x >= width) { - x = 0; - y++; - } - - if(buffer[i] == '\x1a' && size - i >= 8 - && !memcmp(buffer + i + 1, "SAUCE00", 7)) - break; /* End before SAUCE data */ + skip = 1; - if(buffer[i] == '\r') - continue; /* DOS sucks */ - - if(buffer[i] == '\n') - { - x = 0; - y++; - continue; - } - - /* Interpret escape commands, as per Standard ECMA-48 "Control - * Functions for Coded Character Sets", 5.4. Control sequences. */ - if(buffer[i] == '\x1b' && buffer[i + 1] == '[') - { - unsigned int argc = 0, argv[101]; - unsigned int param, inter, final; - - /* Compute offsets to parameter bytes, intermediate bytes and - * to the final byte. Only the final byte is mandatory, there - * can be zero of the others. - * 0 param=2 inter final final+1 - * +-----+------------------+---------------------+-----------------+ - * | CSI | parameter bytes | intermediate bytes | final byte | - * | | 0x30 - 0x3f | 0x20 - 0x2f | 0x40 - 0x7e | - * | ^[[ | 0123456789:;<=>? | SPC !"#$%&'()*+,-./ | azAZ@[\]^_`{|}~ | - * +-----+------------------+---------------------+-----------------+ - */ - param = 2; - - for(inter = param; i + inter < size; inter++) - if(buffer[i + inter] < 0x30 || buffer[i + inter] > 0x3f) - break; - - for(final = inter; i + final < size; final++) - if(buffer[i + final] < 0x20 || buffer[i + final] > 0x2f) - break; + /* Wrap long lines */ + if((unsigned int)x >= width) + { + x = 0; + y++; + } - if(buffer[i + final] < 0x40 || buffer[i + final] > 0x7e) - break; /* Invalid Final Byte */ + if(buffer[i] == '\x1a' && size - i >= 8 + && !memcmp(buffer + i + 1, "SAUCE00", 7)) + break; /* End before SAUCE data */ - skip += final; + if(buffer[i] == '\r') + continue; /* DOS sucks */ - /* Sanity checks */ - if(param < inter && buffer[i + param] >= 0x3c) - { - fprintf(stderr, "private sequence \"^[[%.*s\"\n", - final - param + 1, buffer + i + param); - continue; /* Private sequence, skip it entirely */ - } + if(buffer[i] == '\n') + { + x = 0; + y++; + continue; + } - if(final - param > 100) - continue; /* Suspiciously long sequence, skip it */ + /* Interpret escape commands, as per Standard ECMA-48 "Control + * Functions for Coded Character Sets", 5.4. Control sequences. */ + if(buffer[i] == '\x1b' && buffer[i + 1] == '[') + { + unsigned int argc = 0, argv[101]; + unsigned int param, inter, final; + + /* Compute offsets to parameter bytes, intermediate bytes and + * to the final byte. Only the final byte is mandatory, there + * can be zero of the others. + * 0 param=2 inter final final+1 + * +-----+------------------+---------------------+-----------------+ + * | CSI | parameter bytes | intermediate bytes | final byte | + * | | 0x30 - 0x3f | 0x20 - 0x2f | 0x40 - 0x7e | + * | ^[[ | 0123456789:;<=>? | SPC !"#$%&'()*+,-./ | azAZ@[\]^_`{|}~ | + * +-----+------------------+---------------------+-----------------+ + */ + param = 2; + + for(inter = param; i + inter < size; inter++) + if(buffer[i + inter] < 0x30 || buffer[i + inter] > 0x3f) + break; + + for(final = inter; i + final < size; final++) + if(buffer[i + final] < 0x20 || buffer[i + final] > 0x2f) + break; + + if(buffer[i + final] < 0x40 || buffer[i + final] > 0x7e) + break; /* Invalid Final Byte */ + + skip += final; + + /* Sanity checks */ + if(param < inter && buffer[i + param] >= 0x3c) + { + fprintf(stderr, "private sequence \"^[[%.*s\"\n", + final - param + 1, buffer + i + param); + continue; /* Private sequence, skip it entirely */ + } + + if(final - param > 100) + continue; /* Suspiciously long sequence, skip it */ + + /* Parse parameter bytes as per ECMA-48 5.4.2: Parameter string + * format */ + if(param < inter) + { + argv[0] = 0; + for(j = param; j < inter; j++) + { + if(buffer[i + j] == ';') + argv[++argc] = 0; + else if(buffer[i + j] >= '0' && buffer[i + j] <= '9') + argv[argc] = 10 * argv[argc] + (buffer[i + j] - '0'); + } + argc++; + } + + /* Interpret final byte. The code representations are given in + * ECMA-48 5.4: Control sequences, and the code definitions are + * given in ECMA-48 8.3: Definition of control functions. */ + switch(buffer[i + final]) + { + case 'f': /* CUP - Cursor Position */ + case 'H': /* HVP - Character And Line Position */ + x = (argc > 1) ? argv[1] - 1 : 0; + y = (argc > 0) ? argv[0] - 1 : 0; + break; + case 'A': /* CUU - Cursor Up */ + y -= argc ? argv[0] : 1; + if(y < 0) + y = 0; + break; + case 'B': /* CUD - Cursor Down */ + y += argc ? argv[0] : 1; + break; + case 'C': /* CUF - Cursor Right */ + x += argc ? argv[0] : 1; + break; + case 'D': /* CUB - Cursor Left */ + x -= argc ? argv[0] : 1; + if(x < 0) + x = 0; + break; + case 's': /* Private (save cursor position) */ + save_x = x; + save_y = y; + break; + case 'u': /* Private (reload cursor positin) */ + x = save_x; + y = save_y; + break; + case 'J': /* ED - Erase In Page */ + if(argv[0] == 2) + x = y = 0; + break; + case 'K': /* EL - Erase In Line */ + for(j = x; j < width; j++) + _cucul_putchar32(cv, j, y, (uint32_t)' '); + x = width; + break; + case 'm': /* SGR - Select Graphic Rendition */ + ansi_parse_grcm(cv, &grcm, argc, argv); + break; + default: + fprintf(stderr, "unknown command %c\n", buffer[i + final]); + break; + } + + continue; + } - /* Parse parameter bytes as per ECMA-48 5.4.2: Parameter string - * format */ - if(param < inter) - { - argv[0] = 0; - for(j = param; j < inter; j++) + /* We're going to paste a character. First make sure the canvas + * is big enough. */ + if((unsigned int)y >= height) { - if(buffer[i + j] == ';') - argv[++argc] = 0; - else if(buffer[i + j] >= '0' && buffer[i + j] <= '9') - argv[argc] = 10 * argv[argc] + (buffer[i + j] - '0'); + height = y + 1; + cucul_set_canvas_size(cv, width, height); } - argc++; - } - - /* Interpret final byte. The code representations are given in - * ECMA-48 5.4: Control sequences, and the code definitions are - * given in ECMA-48 8.3: Definition of control functions. */ - switch(buffer[i + final]) - { - case 'f': /* CUP - Cursor Position */ - case 'H': /* HVP - Character And Line Position */ - x = (argc > 1) ? argv[1] - 1 : 0; - y = (argc > 0) ? argv[0] - 1 : 0; - break; - case 'A': /* CUU - Cursor Up */ - y -= argc ? argv[0] : 1; - if(y < 0) - y = 0; - break; - case 'B': /* CUD - Cursor Down */ - y += argc ? argv[0] : 1; - break; - case 'C': /* CUF - Cursor Right */ - x += argc ? argv[0] : 1; - break; - case 'D': /* CUB - Cursor Left */ - x -= argc ? argv[0] : 1; - if(x < 0) - x = 0; - break; - case 's': /* Private (save cursor position) */ - save_x = x; - save_y = y; - break; - case 'u': /* Private (reload cursor positin) */ - x = save_x; - y = save_y; - break; - case 'J': /* ED - Erase In Page */ - if(argv[0] == 2) - x = y = 0; - break; - case 'K': /* EL - Erase In Line */ - for(j = x; j < width; j++) - _cucul_putchar32(cv, j, y, (uint32_t)' '); - x = width; - break; - case 'm': /* SGR - Select Graphic Rendition */ - ansi_parse_grcm(cv, &grcm, argc, argv); - break; - default: - fprintf(stderr, "unknown command %c\n", buffer[i + final]); - break; - } - - continue; - } - /* We're going to paste a character. First make sure the canvas - * is big enough. */ - if((unsigned int)y >= height) - { - height = y + 1; - cucul_set_canvas_size(cv, width, height); + /* Now paste our character */ + _cucul_putchar32(cv, x, y, _cucul_cp437_to_utf32(buffer[i])); + x++; } - /* Now paste our character */ - _cucul_putchar32(cv, x, y, _cucul_cp437_to_utf32(buffer[i])); - x++; - } - return cv; } @@ -381,82 +381,82 @@ static void ansi_parse_grcm(cucul_canvas_t *cv, struct ansi_grcm *g, unsigned int argc, unsigned int const *argv) { static uint8_t const ansi2cucul[] = - { - CUCUL_COLOR_BLACK, CUCUL_COLOR_RED, - CUCUL_COLOR_GREEN, CUCUL_COLOR_BROWN, - CUCUL_COLOR_BLUE, CUCUL_COLOR_MAGENTA, - CUCUL_COLOR_CYAN, CUCUL_COLOR_LIGHTGRAY - }; + { + CUCUL_COLOR_BLACK, CUCUL_COLOR_RED, + CUCUL_COLOR_GREEN, CUCUL_COLOR_BROWN, + CUCUL_COLOR_BLUE, CUCUL_COLOR_MAGENTA, + CUCUL_COLOR_CYAN, CUCUL_COLOR_LIGHTGRAY + }; unsigned int j; uint8_t myfg, mybg; for(j = 0; j < argc; j++) - { - /* Defined in ECMA-48 8.3.117: SGR - SELECT GRAPHIC RENDITION */ - if(argv[j] >= 30 && argv[j] <= 37) - g->fg = ansi2cucul[argv[j] - 30]; - else if(argv[j] >= 40 && argv[j] <= 47) - g->bg = ansi2cucul[argv[j] - 40]; - else if(argv[j] >= 90 && argv[j] <= 97) - g->fg = ansi2cucul[argv[j] - 90] + 8; - else if(argv[j] >= 100 && argv[j] <= 107) - g->bg = ansi2cucul[argv[j] - 100] + 8; - else switch(argv[j]) { - case 0: /* default rendition */ - g->fg = CUCUL_COLOR_DEFAULT; - g->bg = CUCUL_COLOR_DEFAULT; - g->bold = g->negative = g->concealed = 0; - break; - case 1: /* bold or increased intensity */ - g->bold = 1; - break; - case 4: /* singly underlined */ - break; - case 5: /* slowly blinking (less then 150 per minute) */ - break; - case 7: /* negative image */ - g->negative = 1; - break; - case 8: /* concealed characters */ - g->concealed = 1; - break; - case 22: /* normal colour or normal intensity (neither bold nor faint) */ - g->bold = 0; - break; - case 28: /* revealed characters */ - g->concealed = 0; - break; - case 39: /* default display colour (implementation-defined) */ - g->fg = CUCUL_COLOR_DEFAULT; - break; - case 49: /* default background colour (implementation-defined) */ - g->bg = CUCUL_COLOR_DEFAULT; - break; - default: - fprintf(stderr, "unknown sgr %i\n", argv[j]); - break; + /* Defined in ECMA-48 8.3.117: SGR - SELECT GRAPHIC RENDITION */ + if(argv[j] >= 30 && argv[j] <= 37) + g->fg = ansi2cucul[argv[j] - 30]; + else if(argv[j] >= 40 && argv[j] <= 47) + g->bg = ansi2cucul[argv[j] - 40]; + else if(argv[j] >= 90 && argv[j] <= 97) + g->fg = ansi2cucul[argv[j] - 90] + 8; + else if(argv[j] >= 100 && argv[j] <= 107) + g->bg = ansi2cucul[argv[j] - 100] + 8; + else switch(argv[j]) + { + case 0: /* default rendition */ + g->fg = CUCUL_COLOR_DEFAULT; + g->bg = CUCUL_COLOR_DEFAULT; + g->bold = g->negative = g->concealed = 0; + break; + case 1: /* bold or increased intensity */ + g->bold = 1; + break; + case 4: /* singly underlined */ + break; + case 5: /* slowly blinking (less then 150 per minute) */ + break; + case 7: /* negative image */ + g->negative = 1; + break; + case 8: /* concealed characters */ + g->concealed = 1; + break; + case 22: /* normal colour or normal intensity (neither bold nor faint) */ + g->bold = 0; + break; + case 28: /* revealed characters */ + g->concealed = 0; + break; + case 39: /* default display colour (implementation-defined) */ + g->fg = CUCUL_COLOR_DEFAULT; + break; + case 49: /* default background colour (implementation-defined) */ + g->bg = CUCUL_COLOR_DEFAULT; + break; + default: + fprintf(stderr, "unknown sgr %i\n", argv[j]); + break; + } } - } if(g->concealed) - { - myfg = mybg = CUCUL_COLOR_TRANSPARENT; - } + { + myfg = mybg = CUCUL_COLOR_TRANSPARENT; + } else - { - myfg = g->negative ? g->bg : g->fg; - mybg = g->negative ? g->fg : g->bg; - - if(g->bold) { - if(myfg < 8) - myfg += 8; - else if(myfg == CUCUL_COLOR_DEFAULT) - myfg = CUCUL_COLOR_WHITE; + myfg = g->negative ? g->bg : g->fg; + mybg = g->negative ? g->fg : g->bg; + + if(g->bold) + { + if(myfg < 8) + myfg += 8; + else if(myfg == CUCUL_COLOR_DEFAULT) + myfg = CUCUL_COLOR_WHITE; + } } - } cucul_set_color(cv, myfg, mybg); }