<commit_before>#include "global.h"
#include "textlist.h"
#include "timer.h"
#include "espeak.h"
#include "scorelist.h"

#define FALSE 0
#define TRUE 1

#define FPS 15

int quit = FALSE;

SDL_Surface *screen;
SDL_Surface *header_message;
SDL_Surface *footer_message;
SDL_Surface *input_text;

char input_str[256];
wchar_t input_str_w[256];
int input_str_len = 0;

int screen_width = 800;
int screen_height = 600;
int screen_bpp = 32;

char window_header_str[256] = "TextIn";
char window_footer_str[256] = "<NAME>, 2014";

char font_file[128] = "resources/beyourself.ttf";
TTF_Font *font = NULL;
SDL_Color font_color = { 255, 255, 255 };
int font_size = 24;
TTF_Font *input_font = NULL;
int input_font_size = 64;

SDL_Event event;

void init_input() {
  input_font = TTF_OpenFont(font_file, input_font_size);

  strncpy(input_str, "", strlen(input_str));
  wcsncpy(input_str_w, L"", wcslen(input_str_w));
  input_text = NULL;
  SDL_EnableUNICODE(SDL_ENABLE);
}

int init_messages() {
  footer_message = TTF_RenderText_Solid(font, window_footer_str, font_color);
  if (footer_message == NULL) {
    return FALSE;
  }
  header_message = TTF_RenderText_Solid(font, window_header_str, font_color);
  if (header_message == NULL) {
    return FALSE;
  }
  return TRUE;
}

int init() {
  if(SDL_Init(SDL_INIT_EVERYTHING) == -1) {
    return FALSE;
  }

  screen = SDL_SetVideoMode(
    screen_width, screen_height, screen_bpp, SDL_SWSURFACE | SDL_RESIZABLE
  );
  if (screen == NULL) {
    return FALSE;
  }

  if (TTF_Init() == -1) {
    return FALSE;
  }

  font = TTF_OpenFont(font_file, font_size);
  if (font == NULL) {
    return FALSE;
  }

  char wt_temp[256] = "";
  strcat(wt_temp, window_header_str);
  strcat(wt_temp, " - ");
  strcat(wt_temp, window_footer_str);
  SDL_WM_SetCaption(wt_temp, NULL);

  init_input();

  textlist_init();
  textlist_set_random_pos();

  scorelist_load();

  espeak_init();

  timer_start();

  return init_messages();
}

void input_clean_up() {
  TTF_CloseFont(input_font);
  SDL_FreeSurface(input_text);
  SDL_EnableUNICODE(SDL_DISABLE);
}

void clean_up() {
  espeak_clean_up();

  input_clean_up();

  SDL_FreeSurface(footer_message);

  TTF_CloseFont(font);
  TTF_Quit();

  SDL_Quit();
}

void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination) {
  SDL_Rect offset;
  offset.x = x;
  offset.y = y;

  SDL_BlitSurface(source, NULL, destination, &offset);
}

void input_show_centered() {
  if (input_text != NULL) {
    apply_surface(
      (screen_width - input_text->w) / 2, (screen_height - input_text->h) / 2,
      input_text, screen
    );
  }
}

void handle_input() {
  if (event.type == SDL_KEYDOWN) {
    int input_change = FALSE;

    if (input_str_len < 256) {
      if (event.key.keysym.unicode == (Uint16)' ' ||
          (event.key.keysym.unicode >= (Uint16)'a' &&
           event.key.keysym.unicode <= (Uint16)'z') ||
          event.key.keysym.unicode == 223 || // ß
          event.key.keysym.unicode == 252 || // ü
          event.key.keysym.unicode == 228 || // ä
          event.key.keysym.unicode == 246) { // ö

        sprintf(input_str, "%s%c", input_str, event.key.keysym.unicode);
        wchar_t tmp[256];
        swprintf(tmp, 256, L"%lc", event.key.keysym.unicode);
        wcscat(input_str_w, tmp);

        input_str_len++;
        input_change = TRUE;
      }
    }
    if (event.key.keysym.sym == SDLK_BACKSPACE && input_str_len > 0) {
      input_str_len--;
      input_str[strlen(input_str)-1] = '\0';
      input_str_w[wcslen(input_str_w)-1] = '\0';
      input_change = TRUE;
    }
    if (input_change == TRUE) {
      SDL_FreeSurface(input_text);
      input_text = TTF_RenderText_Solid(input_font, input_str, font_color);
    }
  }
}

int main(int argc, char* args[]) {
  setlocale(LC_ALL, "de_DE.UTF-8");

  if (init() == FALSE)
    return 1;

  Uint32 frameStart = 0;
  espeak_set_run(TRUE);
  while (quit == FALSE) {
    timer_update();
    frameStart = SDL_GetTicks();

    while (SDL_PollEvent(&event)) {
      if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_RETURN) {

        int skip_input = (strcmp(input_str, "") == 0) ? TRUE : FALSE;
        if (textlist_current_compare(input_str_w) == 0 || skip_input == TRUE) {

          textlist_remove_current(skip_input);
          textlist_set_random_pos();
          init_input();
          espeak_set_run(TRUE);

        } else {
          init_input();
          espeak_set_run(TRUE);
        }
      } else {
        handle_input();
      }
      if (event.type == SDL_VIDEORESIZE) {
        screen_width = event.resize.w;
        screen_height = event.resize.h;
        screen = SDL_SetVideoMode(
          screen_width, screen_height, screen_bpp, SDL_SWSURFACE | SDL_RESIZABLE
        );<commit_msg>max inputs logic<commit_after>#include "global.h"
#include "textlist.h"
#include "timer.h"
#include "espeak.h"
#include "scorelist.h"

#define FALSE 0
#define TRUE 1

#define FPS 15

int quit = FALSE;
int max_inputs = 10;
int inputs_count = 0;

SDL_Surface *screen;
SDL_Surface *header_message;
SDL_Surface *footer_message;
SDL_Surface *input_text;

char input_str[256];
wchar_t input_str_w[256];
int input_str_len = 0;

int screen_width = 800;
int screen_height = 600;
int screen_bpp = 32;

char window_header_str[256] = "TextIn";
char window_footer_str[256] = "<NAME>, 2014";

char font_file[128] = "resources/beyourself.ttf";
TTF_Font *font = NULL;
SDL_Color font_color = { 255, 255, 255 };
int font_size = 24;
TTF_Font *input_font = NULL;
int input_font_size = 64;

SDL_Event event;

void input_init() {
  input_font = TTF_OpenFont(font_file, input_font_size);

  strncpy(input_str, "", strlen(input_str));
  wcsncpy(input_str_w, L"", wcslen(input_str_w));
  input_text = NULL;
  SDL_EnableUNICODE(SDL_ENABLE);
}

int init_messages() {
  footer_message = TTF_RenderText_Solid(font, window_footer_str, font_color);
  if (footer_message == NULL) {
    return FALSE;
  }
  header_message = TTF_RenderText_Solid(font, window_header_str, font_color);
  if (header_message == NULL) {
    return FALSE;
  }
  return TRUE;
}

int init() {
  if(SDL_Init(SDL_INIT_EVERYTHING) == -1) {
    return FALSE;
  }

  screen = SDL_SetVideoMode(
    screen_width, screen_height, screen_bpp, SDL_SWSURFACE | SDL_RESIZABLE
  );
  if (screen == NULL) {
    return FALSE;
  }

  if (TTF_Init() == -1) {
    return FALSE;
  }

  font = TTF_OpenFont(font_file, font_size);
  if (font == NULL) {
    return FALSE;
  }

  char wt_temp[256] = "";
  strcat(wt_temp, window_header_str);
  strcat(wt_temp, " - ");
  strcat(wt_temp, window_footer_str);
  SDL_WM_SetCaption(wt_temp, NULL);

  input_init();

  textlist_init();
  textlist_set_random_pos();

  scorelist_load();

  espeak_init();

  timer_start();

  return init_messages();
}

void input_clean_up() {
  TTF_CloseFont(input_font);
  SDL_FreeSurface(input_text);
  SDL_EnableUNICODE(SDL_DISABLE);
}

void clean_up() {
  espeak_clean_up();

  input_clean_up();

  SDL_FreeSurface(footer_message);

  TTF_CloseFont(font);
  TTF_Quit();

  SDL_Quit();
}

void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination) {
  SDL_Rect offset;
  offset.x = x;
  offset.y = y;

  SDL_BlitSurface(source, NULL, destination, &offset);
}

void input_show_centered() {
  if (input_text != NULL) {
    apply_surface(
      (screen_width - input_text->w) / 2, (screen_height - input_text->h) / 2,
      input_text, screen
    );
  }
}

void handle_input() {
  if (event.type == SDL_KEYDOWN) {
    int input_change = FALSE;

    if (input_str_len < 256) {
      if (event.key.keysym.unicode == (Uint16)' ' ||
          (event.key.keysym.unicode >= (Uint16)'a' &&
           event.key.keysym.unicode <= (Uint16)'z') ||
          event.key.keysym.unicode == 223 || // ß
          event.key.keysym.unicode == 252 || // ü
          event.key.keysym.unicode == 228 || // ä
          event.key.keysym.unicode == 246) { // ö

        sprintf(input_str, "%s%c", input_str, event.key.keysym.unicode);
        wchar_t tmp[256];
        swprintf(tmp, 256, L"%lc", event.key.keysym.unicode);
        wcscat(input_str_w, tmp);

        input_str_len++;
        input_change = TRUE;
      }
    }
    if (event.key.keysym.sym == SDLK_BACKSPACE && input_str_len > 0) {
      input_str_len--;
      input_str[strlen(input_str)-1] = '\0';
      input_str_w[wcslen(input_str_w)-1] = '\0';
      input_change = TRUE;
    }
    if (input_change == TRUE) {
      SDL_FreeSurface(input_text);
      input_text = TTF_RenderText_Solid(input_font, input_str, font_color);
    }
  }
}

int main(int argc, char* args[]) {
  setlocale(LC_ALL, "de_DE.UTF-8");

  if (init() == FALSE)
    return 1;

  Uint32 frameStart = 0;
  espeak_set_run(TRUE);
  while (quit == FALSE) {
    timer_update();
    frameStart = SDL_GetTicks();

    while (SDL_PollEvent(&event)) {
      if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_RETURN) {

        int skip_input = (strcmp(input_str, "") == 0) ? TRUE : FALSE;
        if (textlist_current_compare(input_str_w) == 0 || skip_input == TRUE) {
          inputs_count++;
          espeak_lock();
          textlist_remove_current(skip_input);
          espeak_unlock();

          if (inputs_count == max_inputs) {
            wprintf(L"max inputs count reached\n");
            // todo enter name logic
            //if (input_continue() == FALSE) {
              //quit = TRUE;
            //} else {
              textlist_init();
              textlist_set_random_pos();
              espeak_set_run(TRUE);
              timer_start();
              inputs_count = 0;
            //}
          }
          if (quit == FALSE) {
            espeak_lock();
            quit = (textlist_set_random_pos() == TRUE) ? FALSE : TRUE;
            espeak_unlock();
            input_init();
            espeak_set_run(TRUE);
          }
        } else {
          input_init();
          espeak_set_run(TRUE);
        }
      } else {
        handle_input();
      }
      if (event.type == SDL_VIDEORESIZE) {
        screen_width = event.resize.w;
        screen_height = event.resize.h;
        screen = SDL_SetVideoMode(
          screen_width, screen_height, screen_bpp, SDL_SWSURFACE | SDL_RESIZABLE
        );
{'fraction_non_alphanumeric': 0.13421900947543225, 'fraction_numerical': 0.01816938556217642, 'mean_word_length': 2.9929797191887677, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6665466', 'n_tokens_mistral': 4012, 'n_tokens_neox': 3699, 'n_words': 707}