Exemplo n.º 1
0
void* display_func(void) {

    int i, j, error;
    char message_buffer[LENGHT_MESSAGE], message_buffer_2[LENGHT_MESSAGE];
    char filename[LENGHT_MESSAGE];
    char confirm_file[LENGHT_MESSAGE];
    FILE *fp;

    while (state == 0) {

        if (recieve_data(LENGHT_MESSAGE, message_buffer) == 0)
            draw_new(global_display, "system>> recieve error");

        //draw_new(global_display, message_buffer);
        if (message_buffer[0] == '0') {
            split_str(1, strlen(message_buffer), message_buffer, message_buffer_2);
            draw_new(global_display, message_buffer_2);
        }
        else if (message_buffer[0] == '1') {
            //do nothing
        }
        else if (message_buffer[0] == '2') {
            split_str(27, strlen(message_buffer) - 2, message_buffer, message_buffer_2);
            strcpy(username, message_buffer_2);
            split_str(1, strlen(message_buffer), message_buffer, message_buffer_2);
            draw_new(global_display, message_buffer_2);
        }
        else if (message_buffer[0] == '3') {
            split_str(1, strlen(message_buffer), message_buffer, message_buffer_2);
            draw_new(global_display, message_buffer_2);
            split_str(31, strlen(message_buffer) - 1, message_buffer, filename);

            sprintf(message_buffer_2, "downloaded/%s", filename);
            fp = fopen(message_buffer_2, "w+");
            error = 1;
            do {
                recieve_file(message_buffer);
                fprintf(fp, "%c", message_buffer[1]);
            }while(message_buffer[0] != '5');
            fclose(fp);

            draw_new(global_display, "system>> Downloaded file success.");

        }

        //Reset value in message_buffer for check while loop's condition
        strcpy(message_buffer, "");
        strcpy(message_buffer_2, "");

    }

    return 0;

}
Exemplo n.º 2
0
static void tglgrid_dialog(t_tg *tg, t_symbol *s, int argc, t_atom *argv) {
  UNUSED(s);
  if (!tg)
    error("tglgrid: tried to set properties on a non-existant grid");
  else if (argc != 6)
    error("tglgrid: invalid number of arguments passed to tglgrid_dialog.  (Expected 6, got %d)",
          argc);
  else if (argv[0].a_type != A_FLOAT ||
           argv[1].a_type != A_FLOAT ||
           argv[2].a_type != A_FLOAT ||
           argv[3].a_type != A_FLOAT ||
           argv[4].a_type != A_SYMBOL ||
           argv[5].a_type != A_SYMBOL)
    error("tglgrid: invalid parameter types passed to tglgrid_dialog");
  else {
    t_int newcols = (t_int)argv[0].a_w.w_float;
    t_int newrows = (t_int)argv[1].a_w.w_float;
    tg->cell_size = (t_int)argv[2].a_w.w_float;
    tg->spacing = (t_int)argv[3].a_w.w_float;
    snprintf(tg->tglfill,8,argv[4].a_w.w_symbol->s_name);
    snprintf(tg->untglfill,8,argv[5].a_w.w_symbol->s_name);

    // need to erase before we change # of rows/cols
    // so we erase the old size
    draw_erase(tg,tg->glist);
    resize_to(tg,newcols,newrows);
    draw_new(tg,tg->glist);
  }
}
Exemplo n.º 3
0
static void tglgrid_vis(t_gobj *z, t_glist *glist, int vis) {
  t_tg *tg = (t_tg*)z;
  if (vis)
    draw_new(tg, glist);
  else
    draw_erase(tg, glist);
}
Exemplo n.º 4
0
void tg_size(t_tg* tg, t_floatarg cf, t_floatarg rf) {
  t_int c = (t_int)cf;
  t_int r = (t_int)rf;

  if (c != tg->cols || r != tg->rows) {
    draw_erase(tg,tg->glist);
    resize_to(tg,c,r);
    draw_new(tg,tg->glist);
  }
}
Exemplo n.º 5
0
struct draw *draw_from_bmp (const char *file)
{
  FILE *fp;
  DWORD *pix;
  struct draw *draw;
  
  void *fdata;
  size_t size;
  int color_size;
  int i, j;
  DWORD color;
  
  BITMAPFILEHEADER *header;
  BITMAPINFOHEADER *info;
  RGBQUAD          *colors;
  
  if ((fp = fopen (file, "rb")) == NULL)
  {
    ERROR ("Couldn't open file \"%s\": %s\n", file, strerror (errno));
    
    return NULL;
  }
  
  fseek (fp, 0, SEEK_END);
  size = ftell (fp);
  fseek (fp, 0, SEEK_SET);
  
  if (size < sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER))
  {
    ERROR ("%s: broken header\n", file);
    fclose (fp);
    
    return NULL;
  }
  
  if ((fdata = mmap (NULL, size, PROT_READ, MAP_SHARED, fileno (fp), 0)) ==
    NULL)
  {
    ERROR ("%s: mmap failed: %s\n", file, strerror (errno));
    fclose (fp);
    
    return NULL;
  }
  
  header = fdata;
  info   = fdata + sizeof (BITMAPFILEHEADER);
    
  if (header->bfType != BM_MAGIC)
  {
    ERROR ("%s: not a BMP file\n", file);
    fclose (fp);
    munmap (fdata, size);
    
    return NULL;
  }
  
  if (header->bfSize > size)
  {
    ERROR ("%s: broken file\n", file);
    fclose (fp);
    munmap (fdata, size);
    
    return NULL;
  }
  
  if (header->bfOffBits >= size)
  {
    ERROR ("%s: incongruent field bfOffBits" 
           "(it says %d, size is %d bytes long)\n", 
           file, header->bfOffBits, (int) size);
    fclose (fp);
    munmap (fdata, size);
    
    return NULL;
  }
  
  if (info->biCompression != 0)
  {
    ERROR ("%s: the file is compressed\n", file);
    fclose (fp);
    munmap (fdata, size);
    
    return NULL;
  }
  
  colors = fdata + sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER);
  
  color_size = info->biBitCount >= 24 ? 0 : 
    (info->biClrUsed == 0 ? 1 << info->biBitCount : info->biClrUsed);
    
  if ((sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) + 
      color_size * sizeof (RGBQUAD)) > header->bfOffBits)
  {
    ERROR ("%s: color table overlaps picture data\n", file);
    ERROR ("0x%lx > 0x%x\n", (sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) + 
      color_size * sizeof (RGBQUAD)), header->bfOffBits);
    ERROR ("%d colors used\n", info->biClrUsed);
    
    fclose (fp);
    munmap (fdata, size);
    
    return NULL;
  }
  
  draw = draw_new (info->biWidth, info->biHeight);
  
  for (j = 0; j < info->biHeight; j++)
    for (i = 0; i < info->biWidth; i++)
    {
      pix = (DWORD *) (fdata + header->bfOffBits + 
        PIX_OFFSET (i, j, info->biWidth, info->biHeight, info->biBitCount));
      
      /*FIXME: check if pix is out of bounds */
      
      color = 0xffffff & *pix;
      if (color_size)
      {
        color = get_color (color, info->biBitCount,
          PIX_BIT_OFFSET (i, info->biBitCount));
          
        if (color >= color_size)
        {
          ERROR ("%s: unmapped color %x (%d colors)\n", file, color,
            color_size);
          fclose (fp);
          munmap (fdata, size);
          draw_free (draw);
          
          return NULL;  
        }
        
        color = RGB (colors[color].rgbRed,
                     colors[color].rgbGreen,
                     colors[color].rgbBlue);
      }
      
      draw_pset (draw, i, j, color);
    }
    
  fclose (fp);
  munmap (fdata, size);
  
  return draw;
}
Exemplo n.º 6
0
void* typing_func(void) {

    char message_buffer[LENGHT_MESSAGE];
    char message_buffer_2[LENGHT_MESSAGE];
    char confirm_file[LENGHT_MESSAGE];
    char filename[LENGHT_MESSAGE];
    char ch;
    int buffer_int;
    FILE *fp;

    while (state == 0) {

        //Reset string for get new message
        strcpy(message_buffer, "");
        strcpy(message_buffer_2, "");

        wscanw(global_typing, " %[^\n]s", message_buffer);
        while (strlen(message_buffer) > 200) {
            werase(global_typing);
            draw_new(global_display, "system>> Message cannot more than 200 characters.");
            wscanw(global_typing, " %[^\n]s", message_buffer);
        }

        //Draw_new line to display message
        strcpy(message_buffer_2, "you>> ");
        strcat(message_buffer_2, message_buffer);
        draw_new(global_display, message_buffer_2);


        //Check exit command
        if (strcmp(message_buffer, ":q!") == 0) {
            //set state to stop all function
            state = 1;
        }
        else if (message_buffer[0] == '/') {

            if (split_strcmp(0, 6, "/upload", 0, 6, message_buffer)){

                split_str(8, strlen(message_buffer), message_buffer, filename);
                sprintf(message_buffer, "3system>> Sending file to you: %s", filename);
                send_data(message_buffer);

                sleep(1);

                draw_new(global_display, "system>> Uploading...");

                fp = fopen(filename, "r");
                while( ( ch = fgetc(fp) ) != EOF ){

                    sprintf(message_buffer, "4%c", ch);

                    if(send_data(message_buffer) == 0)
                        draw_new(global_display, "system>> Send failed");

                }
                fclose(fp);

                sleep(1);

                strcpy(message_buffer, "5");
                send_data(message_buffer);
                draw_new(global_display, "system>> Done!");

            }
            else if (split_strcmp(0, 2, "/up", 0, 2, message_buffer)){

                split_str(4, strlen(message_buffer), message_buffer, message_buffer_2);
                buffer_int = atoi(message_buffer_2);
                draw_old_line(global_display, 1, buffer_int);

            }
            else if (split_strcmp(0, 4, "/down", 0, 4, message_buffer)){

                split_str(6, strlen(message_buffer), message_buffer, message_buffer_2);
                buffer_int = atoi(message_buffer_2);
                draw_old_line(global_display, 2, buffer_int);

            }
            else if (split_strcmp(0, 4, "/help", 0, 4, message_buffer)){

                draw_new(global_display, "system>> ### THIS IS HELP! ###");
                draw_new(global_display, "system>> \":q!\" to exit program.");
                draw_new(global_display, "system>> \"/talkto [nickname]\" to choose contact.");
                draw_new(global_display, "system>> \"/untalk\" to remove contact that we are talking.");
                draw_new(global_display, "system>> \"/upload [file]\" to upload file to client that you are talking.");
                draw_new(global_display, "system>> \"/watline\" to show number of latest line");
                draw_new(global_display, "system>> \"/up [amount of line]\" to scroll screen up n lines.");
                draw_new(global_display, "system>> \"/down [amount of line]\" to scroll screen down n lines.");
                draw_new(global_display, "system>> \"/find [word]\" to find number of line that word was display.");
                draw_new(global_display, "system>> \"/contact\" to show all user on server.");

            }
            else if (split_strcmp(0, 4, "/find", 0, 4, message_buffer)){

                split_str(6, strlen(message_buffer) - 1, message_buffer, message_buffer_2);
                search(message_buffer_2, global_display);

            }
            else if (split_strcmp(0, 7, "/watline", 0, 7, message_buffer)){

                //bottom_line come from buffer_screen.h
                sprintf(message_buffer, "system>> v This is lines number %d. v", bottom_line);
                draw_new(global_display, message_buffer);

            }
            else if (
                    split_strcmp(0, 6, "/talkto", 0, 6, message_buffer) ||
                    split_strcmp(0, 6, "/untalk", 0, 6, message_buffer) ||
                    split_strcmp(0, 7, "/contact", 0, 7, message_buffer)) {

                sprintf(message_buffer_2, "0%s", message_buffer);
                send_data(message_buffer_2);
            }
            else {

                draw_new(global_display, "system>> Command not found.");

            }
        }
        else {

            //Set protocal to send packet
            sprintf(message_buffer_2, "0%s", message_buffer);
            if(send_data(message_buffer_2) == 0)
                draw_new(global_display, "system>> Send failed");

        }

        werase(global_typing);

    }

    pthread_cancel(*global_display_thread);
    return 0;

}
Exemplo n.º 7
0
int main(int argc , char *argv[]) {

    int read_size, port;
    char ip_address[255];
    char message_buffer[LENGHT_MESSAGE], c;
    char message_buffer_2[LENGHT_MESSAGE];
    WINDOW *buffer_window[2];

    //Config ip address and port
    strcpy(ip_address, argv[1]);
    port = atoi(argv[2]);

    /////////////////////////////////////////////////
    //             START NCURSES CODE              //
    /////////////////////////////////////////////////

    //Set variable about ncurses
    int parent_x, parent_y;
    int typing_size = 4;

    initscr();
    start_color();
    init_pair(1, COLOR_BLACK, COLOR_GREEN);
    init_pair(2, COLOR_BLACK, COLOR_RED);

    // get our maximum window dimensions
    getmaxyx(stdscr, parent_y, parent_x);

    // set up initial windows
    WINDOW *display = newwin(parent_y - typing_size - 1, parent_x, 0, 0);
    WINDOW *split_line = newwin(1, parent_x, parent_y - typing_size - 1, 0);
    WINDOW *typing = newwin(typing_size, parent_x, parent_y - typing_size, 0);

    display_height = parent_y - typing_size - 1;

    global_typing = typing;
    global_display = display;

    scrollok(display, 1);

    //wbkgd(display, COLOR_PAIR(2));
    //wrefresh(display);

    wbkgd(split_line, COLOR_PAIR(1));
    wrefresh(split_line);

    //wbkgd(typing, COLOR_PAIR(2));
    //wrefresh(typing);

    initial_buffer_screen(display_height, typing_size);

    /////////////////////////////////////////////////
    //               END NCURSES CODE              //
    /////////////////////////////////////////////////

    draw_new(display, "--------------------------------------------------");
    draw_new(display, "             Welcome to terminal chat!            ");
    draw_new(display, "--------------------------------------------------\n");

    draw_new(display, "system>> Terminal-chat is started.");

    //Initial connection server - client
    initial_connection(ip_address, port);

    //prepare to pthread_create with WINDOW *buffer_window[2];
    pthread_t typing_thread, display_thread;
    global_typing_thread = &typing_thread;
    global_display_thread = &display_thread;
    pthread_create( &typing_thread, NULL, (void *)typing_func, NULL);
    pthread_create( &display_thread, NULL, (void *)display_func, NULL );

    pthread_join( typing_thread, NULL);
    pthread_join( display_thread, NULL);

    // while (state == 0) {
    //     //do nothing
    // }

    draw_new(display, "\n------------------------------");
    draw_new(display, "Good bye, see you again! owo)/\n");
    sleep(2);

    // clean up
    delwin(display);
    delwin(typing);
    endwin();

    return 0;
}