Exemple #1
0
enum CXChildVisitResult visit_program(CXCursor cursor, CXCursor parent, CXClientData data){
  json_t* program = (json_t*)data;
  json_t* js = NULL;
  enum CXChildVisitResult ret = CXChildVisit_Continue;
  switch (clang_getCursorKind(cursor)) {
  case CXCursor_FunctionDecl:
  case CXCursor_VarDecl:
    js = json_object();
    json_object_set_new(js, "name", render_string(clang_getCursorSpelling(cursor)));
    json_object_set_new(js, "display_name", render_string(clang_getCursorDisplayName(cursor)));
    json_object_set_new(js, "type", render_type(clang_getCursorType(cursor)));
    if (clang_getCursorKind(cursor) == CXCursor_FunctionDecl) {
      json_object_set_new(js, "argument_names", json_array());
      json_object_set_new(js, "kind", json_string("function"));
    } else {
      json_object_set_new(js, "kind", json_string("variable"));
    }
    switch (clang_getCursorLinkage(cursor)) {
    case CXLinkage_Internal:
      json_object_set_new(js, "linkage", json_string("static"));
      break;
    case CXLinkage_UniqueExternal:
      json_object_set_new(js, "linkage", json_string("anonymous"));
      break;
    case CXLinkage_External:
      break;
    default:
      json_object_set_new(js, "linkage", json_string("unknown"));
    }
    clang_visitChildren(cursor, visit_object, (CXClientData)js);
    
    break;
    
  case CXCursor_StructDecl:
  case CXCursor_UnionDecl:
    js = json_object();
    json_object_set_new(js, "kind", json_string(clang_getCursorKind(cursor) == CXCursor_UnionDecl ? 
                                                "union" : "struct"));
    json_object_set_new(js, "name", render_string(clang_getCursorSpelling(cursor)));
    json_object_set_new(js, "fields", json_array());
    clang_visitChildren(cursor, visit_structure, (CXClientData)js);
    ret = CXChildVisit_Recurse;
    break;

  case CXCursor_EnumDecl:
    js = json_object();
    json_object_set_new(js, "kind", json_string("enum"));
    json_object_set_new(js, "name", render_string(clang_getCursorSpelling(cursor)));
    json_object_set_new(js, "values", json_array());
    clang_visitChildren(cursor, visit_enum, (CXClientData)js);
    break;

  case CXCursor_TypedefDecl:
    js = json_object();
    json_object_set_new(js, "kind", json_string("typedef"));
    json_object_set_new(js, "name", render_string(clang_getCursorSpelling(cursor)));
    json_object_set_new(js, "type", render_type(clang_getTypedefDeclUnderlyingType(cursor)));
    ret = CXChildVisit_Recurse;
    break;


  case CXCursor_FieldDecl:
    break;

  case CXCursor_MacroDefinition:
    js = json_object();
    json_object_set_new(js, "kind", json_string("macro"));
    {
      CXToken* tokens;
      unsigned ntokens, i;
      CXString name = clang_getCursorSpelling(cursor);
      char value_buf[1024] = "";
      json_object_set_new(js, "name", render_string(clang_getCursorSpelling(cursor)));
      clang_tokenize(TU, clang_getCursorExtent(cursor), &tokens, &ntokens);
      for (i=0; i<ntokens; i++) {
        CXString str = clang_getTokenSpelling(TU, tokens[i]);
        CXTokenKind tkind = clang_getTokenKind(tokens[i]);
        if (i == 0 && !strcmp(clang_getCString(name), clang_getCString(str))) {
          // macro name
        } else if (i == ntokens - 1 && 
                   tkind == CXToken_Punctuation && !strcmp("#", clang_getCString(str))) {
          // weird clang terminator thingy
        } else if (tkind == CXToken_Comment) {
          // comment
        } else {
          if (strlen(value_buf) > 0) {
            strncat(value_buf, " ", sizeof(value_buf) - strlen(value_buf) - 1);
          }
          strncat(value_buf, clang_getCString(str), sizeof(value_buf) - strlen(value_buf) - 1);
        }
        clang_disposeString(str);
      }
      clang_disposeTokens(TU, tokens, ntokens);
      if (strlen(value_buf) > 0) {
        long int intval;
        double dblval;
        char* endptr_int;
        char* endptr_dbl;
        intval = strtol(value_buf, &endptr_int, 0);
        dblval = strtod(value_buf, &endptr_dbl);
        if (endptr_int[0] == 0 ||
            (endptr_int[1] == 0 && strchr("UuLl", endptr_int[0]) != NULL)) {
          json_object_set_new(js, "value", json_integer(intval));
        } else if (endptr_dbl[0] == 0 || 
                   (endptr_dbl[1] == 0 && strchr("fF", endptr_dbl[0]) != NULL)) {
          json_object_set_new(js, "value", json_real(dblval));
        } else {
          json_object_set_new(js, "value", json_string(value_buf));
        }
      }
    }
    break;

    /*
  case CXCursor_PreprocessingDirective:
  case CXCursor_MacroExpansion:
    js = json_object();
    json_object_set_new(js, "kind", json_string("macro"));
    json_object_set_new(js, "name", render_string(clang_getCursorSpelling(cursor)));
    putstring(clang_getCursorSpelling(cursor));
    putstring(clang_getCursorDisplayName(cursor));
    printf("%d\n", clang_getEnumConstantDeclValue(cursor));
    
    ret = CXChildVisit_Recurse;
    */

  default:
    js = json_object();
    json_object_set_new(js, "name", render_string(clang_getCursorSpelling(cursor)));
    json_object_set_new(js, "type", render_type(clang_getCursorType(cursor)));
    json_object_set_new(js, "kind", json_string("wtf"));
    json_object_set_new(js, "wtf", render_string(clang_getCursorKindSpelling(clang_getCursorKind(cursor))));
  }

  if (js) {
    json_t* str;
    if (!json_object_get(program, "")) json_object_set_new(program, "", json_array());
    str = render_string(clang_getCursorUSR(cursor));
    if (strlen(json_string_value(str)) == 0) {
      json_decref(str);
      json_array_append_new(json_object_get(program, ""), js);
    } else {
      json_object_set_with_key_new(program, render_string(clang_getCursorUSR(cursor)), js);
    }
  }

  return ret;
}
Exemple #2
0
json_t* render_type(CXType type) {
  int i;
  CXCursor decl = clang_getTypeDeclaration(type);
  json_t* js = json_object();

  enum CXTypeKind kind = type.kind;
  if (type.kind == CXType_Unexposed) {
    // workaround for libclang bug
    if (clang_getResultType(type).kind != CXType_Invalid) {
      kind = CXType_FunctionProto;
    }
  }


  if (clang_isVolatileQualifiedType(type)) {
    json_object_set_new(js, "volatile", json_true());
  }
  if (clang_isConstQualifiedType(type)) {
    json_object_set_new(js, "const", json_true());
  }
  if (clang_isRestrictQualifiedType(type)) {
    json_object_set_new(js, "restrict", json_true());
  }

  static const struct {
    enum CXTypeKind kind;
    const char* name;
  } PRIMITIVE_TYPES[] = {
    {CXType_Void, "void"},
    {CXType_Bool, "bool"},
    {CXType_Char_U, "char"},
    {CXType_Char_S, "char"},
    {CXType_UChar, "unsigned char"},
    {CXType_SChar, "signed char"},
    {CXType_WChar, "wchar_t"},
    {CXType_Char16, "char16_t"},
    {CXType_Char32, "char32_t"},
    {CXType_Short, "short"},
    {CXType_UShort, "unsigned short"},
    {CXType_Int, "int"},
    {CXType_UInt, "unsigned int"},
    {CXType_Long, "long"},
    {CXType_ULong, "unsigned long"},
    {CXType_LongLong, "unsigned long"},
    {CXType_ULongLong, "unsigned long long"},
    {CXType_Int128, "__int128"},
    {CXType_UInt128, "unsigned __int128"},
    {CXType_Float, "float"},
    {CXType_Double, "double"},
    {CXType_LongDouble, "long double"}
  };


  switch (kind) {
  case CXType_Pointer:
    json_object_set_new(js, "kind", json_string("pointer"));
    json_object_set_new(js, "pointee", render_type(clang_getPointeeType(type)));
    return js;

  case CXType_ConstantArray:
    json_object_set_new(js, "kind", json_string("array"));
    json_object_set_new(js, "element", render_type(clang_getArrayElementType(type)));
    json_object_set_new(js, "length", json_integer(clang_getArraySize(type)));
    return js;
    
  case CXType_FunctionNoProto:
  case CXType_FunctionProto:
    json_object_set_new(js, "kind", json_string("function"));
    json_object_set_new(js, "return", render_type(clang_getResultType(type)));
    switch (clang_getFunctionTypeCallingConv(type)) {
    case CXCallingConv_C:
      json_object_set_new(js, "calling_convention", json_string("cdecl"));
      break;
    case CXCallingConv_X86StdCall:
      json_object_set_new(js, "calling_convention", json_string("stdcall"));
      break;
    case CXCallingConv_X86FastCall:
      json_object_set_new(js, "calling_convention", json_string("fastcall"));
      break;
    case CXCallingConv_X86ThisCall:
      json_object_set_new(js, "calling_convention", json_string("thiscall"));
      break;
    case CXCallingConv_X86Pascal:
      json_object_set_new(js, "calling_convention", json_string("pascal"));
      break;
    case CXCallingConv_AAPCS:
      json_object_set_new(js, "calling_convention", json_string("aapcs"));
      break;
    case CXCallingConv_AAPCS_VFP:
      json_object_set_new(js, "calling_convention", json_string("aapcs-vfp"));
      break;
    default:
      break;
    }
    json_t* arguments = json_array();
    for (i=0; i<clang_getNumArgTypes(type); i++) {
      json_array_append_new(arguments, render_type(clang_getArgType(type, i)));
    }
    json_object_set_new(js, "arguments", arguments);
    return js;

  default:
    if (clang_getCursorKind(decl) != CXCursor_NoDeclFound) {
      json_object_set_new(js, "kind", json_string("ref"));
      json_object_set_new(js, "id", render_string(clang_getCursorUSR(decl)));
    } else {
      int found = 0, i;
      for (i=0; i<sizeof(PRIMITIVE_TYPES)/sizeof(PRIMITIVE_TYPES[0]); i++) {
        if (PRIMITIVE_TYPES[i].kind == kind) {
          json_object_set_new(js, "kind", json_string("primitive"));
          json_object_set_new(js, "primitive", json_string(PRIMITIVE_TYPES[i].name));
          found = 1;
          break;
        }
      }
      if (!found) {
        json_object_set_new(js, "kind", json_string("unknown"));
        json_object_set_new(js, "clang_kind", render_string(clang_getTypeKindSpelling(kind)));
      }
    }
    return js;
  }
}
Exemple #3
0
void
perfnames::draw_sequence (int seqnum)
{
    int yloc = m_names_y * (seqnum - m_sequence_offset);
    if (seqnum < m_sequence_max)                    /* less than "infinity" */
    {
        char snb[8];                                /* set-number buffer    */
        snprintf(snb, sizeof(snb), "%2d", seqnum / m_seqs_in_set);
        draw_rectangle(black(), 0, yloc, m_names_x, m_names_y);     /* + 1  */
        if (seqnum % m_seqs_in_set == 0)
            render_string(m_xy_offset, yloc + m_xy_offset, snb, font::WHITE);
        else
            draw_rectangle(white(), 1, yloc, m_setbox_w + 1, m_names_y);

        sequence * seq = perf().get_sequence(seqnum);
        Color fg = grey();
        font::Color col = font::BLACK;
        bool is_active = perf().is_active(seqnum);
        bool muted = false;
        bool empty_highlight = false;
        bool smf_0 = false;
        int chan = 0;
        if (is_active)
        {
            muted = seq->get_song_mute();          /* vs get_playing()      */
            empty_highlight = perf().highlight(*seq);
            smf_0 = perf().is_smf_0(*seq);
            chan = seq->is_smf_0() ? 0 : seq->get_midi_channel() + 1 ;
        }

#ifdef SEQ64_EDIT_SEQUENCE_HIGHLIGHT
        bool current_highlight = smf_0 || is_edit_sequence(seqnum);
#else
        bool current_highlight = smf_0;
#endif
        if (is_active)                              /* Note 2               */
        {
            if (muted)
                fg = black();

            if (empty_highlight)
            {
                if (muted)
                    col = font::YELLOW_ON_BLACK;
                else
                {
                    fg = yellow();
                    col = font::BLACK_ON_YELLOW;
                }
            }
            else if (current_highlight)
            {
                if (muted)
                    col = font::CYAN_ON_BLACK;
                else
                {
                    fg = dark_cyan();
                    col = font::BLACK_ON_CYAN;
                }
            }
            else
            {
                if (muted)
                    col = font::WHITE;
                else
                {
                    fg = white();
                    col = font::BLACK;
                }
            }
        }
        draw_rectangle                              /* Note 3               */
        (
            fg, m_setbox_w + 3, yloc + 1,
            m_names_x - 3 - m_setbox_w, m_names_y - 1
        );
        if (is_active)
        {
            char temp[32];
            m_sequence_active[seqnum] = true;
            snprintf(temp, sizeof temp, "%-14.14s   %2d", seq->get_name(), chan);
            render_string(5 + m_setbox_w, yloc + 2, temp, col);

            std::string label = perf().sequence_label(*seq);
            render_string(m_setbox_w + 5, yloc + 12, label, col);
            draw_rectangle(black(), m_namebox_w + 2, yloc, 10, m_names_y, muted);
            render_string(m_namebox_w + 5, yloc + 2, "M", col);
        }
    }
    else
    {
        /*
         * This "else" shouldn't legally be possible!
         */

        draw_rectangle(grey(), 0, yloc + 1, m_names_x, m_names_y);
    }
}
void
perftime::draw_background ()
{
    draw_rectangle(white_paint(), 0, 0, m_window_x, m_window_y);
    draw_line(black_paint(), 0, m_window_y - 1, m_window_x, m_window_y - 1);
    midipulse first_measure = m_tick_offset / m_measure_length;
    midipulse last_measure = first_measure +
        (m_window_x * m_perf_scale_x / m_measure_length) + 1;

#ifdef USE_STAZED_EXTRAS

    float bar_draw = m_measure_length / float(m_perf_scale_x);
    int bar_skip = 1;
    if (bar_draw < 24)
        bar_skip = 4;

    if (bar_draw < 12)
        bar_skip = 8;

    if (bar_draw < 6)
        bar_skip = 16;

    if (bar_draw < 3)
        bar_skip = 32;

    if (bar_draw < .75)
        bar_skip = 64;
#endif

    m_gc->set_foreground(grey());                   /* draw vertical lines  */

#ifdef USE_STAZED_EXTRAS
    for (midipulse i = first_measure; i < last_measure; i += bar_skip)
    {
        int x_pos = ((i * m_measure_length) - m_tick_offset) / m_perf_scale_x;
#else
    for (midipulse i = first_measure; i < last_measure; ++i)
    {
        int x_pos = tick_to_pixel(i * m_measure_length);
#endif

        char bar[8];
        snprintf(bar, sizeof(bar), "%ld", i + 1);       /* bar numbers      */
        draw_line(x_pos, 0, x_pos, m_window_y);         /* beat             */
        render_string(x_pos + 2, 0, bar, font::BLACK, true);
    }

    midipulse left = tick_to_pixel(perf().get_left_tick());
    midipulse right = tick_to_pixel(perf().get_right_tick());
    if (left >= 0 && left <= m_window_x)                /* draw L marker    */
    {
        draw_rectangle(black_paint(), left, m_window_y - 9, 7, 10);
        render_string(left + 1, 9, "L", font::WHITE, true);
    }
    if (right >= 0 && right <= m_window_x)              /* draw R marker    */
    {
        draw_rectangle(black_paint(), right - 6, m_window_y - 9, 7, 10);
        render_string(right - 6 + 1, 9, "R", font::WHITE, true);
    }
}

/**
 *  Implement the button-press event to set the L and R ticks.  Added
 *  functionality to try to set the start-tick if ctrl-left-click is pressed.
 *
 * \param p0
 *      The button event.
 *
 * \return
 *      Always returns true.
 */

bool
perftime::on_button_press_event (GdkEventButton * p0)
{
    midipulse tick = pixel_to_tick(long(p0->x));
    tick -= tick % m_snap;

    /**
     * Why is setting the start-tick disabled?  We re-enable it and see if it
     * works.  To our surprise, it works, but it sticks between stop/pause and
     * the next playback in the performance editor.  We added a feature where
     * stop sets the start-tick to the left tick (or the beginning tick).
     */

    if (SEQ64_CLICK_MIDDLE(p0->button))
    {
        perf().set_start_tick(tick);
    }
    else if (SEQ64_CLICK_LEFT(p0->button))
    {
        if (is_ctrl_key(p0))
            perf().set_start_tick(tick);
        else
            perf().set_left_tick(tick);
    }
    else if (SEQ64_CLICK_RIGHT(p0->button))
    {
        perf().set_right_tick(tick + m_snap);
    }
    enqueue_draw();
    return true;
}
Exemple #5
0
void render_botbar(WINDOW *w) {
    render_string(w, "^X Exit \t ^L Clear \t ^O Write \t ^H Chat");
}
Exemple #6
0
int main(int argc, char **argv) {
    to_send = (message *)malloc(sizeof(message)); // message struct to talk to server with
    message *received = (message *)malloc(sizeof(message));
    client *me = (client *)malloc(sizeof(client)); // ME!
    tbuf B = 0;
    char welcome_msg[100];

    signal(SIGINT, sigint_handler);

    // handle command line args
    int i;
    if (argc > 0) {
        for (i = 0 ; i < argc ; i++) {
            if (*argv[i] == '-') {
                switch(*(argv[i] + 1)) {
                    case 'n':
                        strncpy(NAME, argv[++i], sizeof(NAME));
                        break;

                    case 'h':
                        printf("%s\n", HELP);
                        exit(1);
                        break;

                    case 'l': // local use
                        CONN = 0;
                        me->room_id = 0;
                        break;

                    case 'j': // not creating a room, but joining
                        ROOM_NO = atoi(argv[++i]);
                        break;

                    case 't':
                        DEBUG = 1;
                        break;
                }
            }

            else if(i > 0) {
                strncpy(filename, argv[i], sizeof(filename));
                B = read_from_file(argv[i]);
                debug("reading done (%s)\n", argv[i]);
            }
        }
    }

    if (!NAME[0] && CONN) { // name is not set yet and connecting to network
        printf("%s\n", HELP);
        exit(1);
    }
    
    if (!B) {

        B = new_tbuf();
        printf("is this it?\n");

    }


    // build socket connection
    if (CONN) {
        if ((FROM_SERVER = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
            fprintf(stderr, "Fatal - Cannot create socket\n");
            close(1);
        }

        serv_addr.sin_family = AF_INET;
        serv_addr.sin_port = htons(CLIENT_PORT);
        serv_addr.sin_addr.s_addr = inet_addr(SERVER_ADDR);

        if (connect(FROM_SERVER, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
            fprintf(stderr, "Fatal - Connection to server failed\n");
            close(1);
        }

        // setup terminal args and handshake with server
        // 1. join server request
        strncpy(to_send->cmd, CONN_REQUEST, sizeof(CONN_REQUEST));
        strncpy(to_send->content, NAME, sizeof(NAME));
        debug("Name sent to server: %s\n", to_send->content);
        write(FROM_SERVER, to_send, sizeof(message));
        read(FROM_SERVER, &to_send->remote_client_id, sizeof(int));
        debug("Received from server: %d\n", to_send->remote_client_id);

        // let us assume that you are creating a room here
        
        if (ROOM_NO != -1) { // join a given room
            me->room = ROOM_NO;
            strncpy(to_send->cmd, "join", 5);
            to_send->to_distribute = 1;
            sprintf(to_send->content, "%d", ROOM_NO);
            debug("command sent: %s\n", to_send->cmd);
            send_to_server(to_send, sizeof(message));
            room_state *current_room = (room_state *)malloc(sizeof(room_state));
            read(FROM_SERVER, current_room, sizeof(room_state));
            B = chararr2tbuf(current_room->buf);
            memcpy(usernames, current_room->usernames, sizeof(usernames));
            read(FROM_SERVER, &me->room_id, sizeof(int));
            to_send->local_client_id = me->room_id;
            strncpy(usernames[me->room_id], NAME, sizeof(NAME));
            sprintf(welcome_msg, "<SERVER> You have just joined room %d", me->room);
        }
        else { // create a room
            strncpy(to_send->cmd, "new", 4);
            debug("command sent: %s\n", to_send->cmd);
            send_to_server(to_send, sizeof(message));
            read(FROM_SERVER, &ROOM_NO, sizeof(int));
            to_send->local_client_id = 0; // create a room = 0
            me->room = ROOM_NO;
            me->room_id = 0;
            strncpy(usernames[me->room_id], NAME, sizeof(NAME));
            debug("recv from server: in room %d\n", me->room);
            sprintf(welcome_msg, "<SERVER> You are now the owner of %d", me->room);
        }
        to_send->to_distribute = 1;
    }
    else {
        sprintf(welcome_msg, "You are currently running NetScribe locally");
    }


    //getchar();

    // setup GUI
    WINDOW *mainwin = initscr();
    cbreak();
    noecho();
    keypad(mainwin, true);
    int vis = curs_set(0);

    int ncols = getmaxx(mainwin);
    int nlines = getmaxy(mainwin);
    int begx = getbegx(mainwin);
    int begy = getbegy(mainwin);

    debug("%d, %d, %d, %d\n", ncols, nlines, begx, begy);

    WINDOW *canvas = subwin(mainwin,
                            nlines - 4, // save 2 lines for bottom status
                            ncols, // same as main
                            begy + 2, // save one line for title and one line for 'chat'
                            begx);

    WINDOW *topbar = subwin(mainwin, 1, ncols, begy, begx);
    WINDOW *chatbar = subwin(mainwin, 1, ncols, begy + 1, begx);
    WINDOW *botbar = subwin(mainwin, 1, ncols, nlines - 2, begx);
    WINDOW *inputbar = subwin(mainwin, 1, ncols, nlines - 1, begx);

    render_topbar(topbar);
    render_string(chatbar, welcome_msg);
    render_botbar(botbar);
    wrefresh(mainwin);

    debug("setup done\n");

    // setup the select program
    char c[3];
    fd_set readfds, current;

    FD_ZERO(&readfds);
    FD_ZERO(&current);

    FD_SET(STDIN_FILENO, &readfds);

    if (CONN) {
        FD_SET(FROM_SERVER, &readfds);
    }

    while (1) {
        if (!DEBUG) {
            render_tbuf(B, canvas); // FIXME OVERWRITING STUFF
        }

        current = readfds;

        debug("BEFORE SELECT\n");

        if (select(FROM_SERVER + 1, &current, NULL, NULL, NULL) < 0) {
            fprintf(stderr, "Error %d: %s", errno, strerror(errno));
        }

        debug("HELLO!\n");

        if (FD_ISSET(FROM_SERVER, &current)) { // receive msg from server
            read(FROM_SERVER, received, sizeof(message));
            debug("MSG FROM SERVER: %s\n", received->cmd);
            int c_user = received->local_client_id;

            if (strstr(received->cmd, "chat")) { // this is a chat command
                time_t t;
                struct tm *tinfo;
                time(&t);
                tinfo = localtime(&t);
                char time_s[20];
                if (!strftime(time_s, sizeof(time_s), "%H:%M:%S", tinfo)) {
                    fprintf(stderr, "Error %d: %s", errno, strerror(errno));
                    exit(1);
                }
                char to_put_up[100];
                sprintf(to_put_up, "[%s] %s: %s", time_s,
                        usernames[received->local_client_id],
                        received->content);

                render_string(chatbar, to_put_up);
            }

            if (strstr(received->cmd, "join")) { // if new user joins
                debug("THIS IS A JOIN REQUEST\n");
                strncpy(usernames[received->local_client_id], received->content, 16);
                time_t t;
                struct tm *tinfo;
                time(&t);
                tinfo = localtime(&t);
                char time_s[20];
                if (!strftime(time_s, sizeof(time_s), "%H:%M:%S", tinfo)) {
                    fprintf(stderr, "Error %d: %s", errno, strerror(errno));
                    exit(1);
                }
                char to_put_up[100];
                sprintf(to_put_up, "[%s] <SERVER>: %s has joined", time_s,
                        usernames[received->local_client_id]);

                render_string(chatbar, to_put_up);
            }

            if (strstr(received->cmd, "exit")) { // if user exits
                time_t t;
                struct tm *tinfo;
                time(&t);
                tinfo = localtime(&t);
                char time_s[20];
                if (!strftime(time_s, sizeof(time_s), "%H:%M:%S", tinfo)) {
                    fprintf(stderr, "Error %d: %s", errno, strerror(errno));
                    exit(1);
                }
                char to_put_up[100];
                sprintf(to_put_up, "[%s] <SERVER>: %s has exited", time_s,
                        usernames[received->local_client_id]);

                render_string(chatbar, to_put_up);
                memset(usernames[received->local_client_id], 0, sizeof(usernames[received->local_client_id]));
            }

            if (strstr(received->cmd, SERVER_EXIT)) { // server died :(
                SERVER_DIED = 1;
                break;
            }

            if (strstr(received->cmd, BUF_REQUEST)) { // you are the owner and the server asked you for the buffer
                debug("HELLO I GOTCHU\n");
                //assert(me->room_id == 0); // i should be the owner
                room_state *current_state = (room_state *)malloc(sizeof(room_state));
                strncpy(current_state->buf, tbuf2chararr(B), 20480);
                memcpy(current_state->usernames, usernames, sizeof(usernames));
                debug("conversion done\n");
                write(FROM_SERVER, current_state, sizeof(room_state)); // only except to the RO rule
                debug("Wrote to server\n");
            }

            if (strstr(received->cmd, "edit")) {
                memcpy(c, received->content, sizeof(c));

                if (c[0] == 27) {
                    switch (c[2]) {
                        case 'C': // move cursor right
                            backward_char(B, c_user);
                            break;

                        case 'D':
                            forward_char(B, c_user);
                            break;
                    }
                }

                else if (c[0] == 127) { // backspace
                    delete_char(B, c_user);
                }

                else if (0 < c[0] && c[0] < 127) { // other characters
                    insert_char(B, c[0], c_user);
                }
            }
        }

        if (FD_ISSET(STDIN_FILENO, &current)) { // reading from stdin
            read(STDIN_FILENO, &c, 1);

            if (c[0] == 24) { // ^X --> exit
                break; // exit out of the listening loop
            }

            switch (mode) {
                case EDIT_MODE:
                    if (c[0] == 12) { // ^L --> redraw
                        wclear(mainwin);
                        render_topbar(topbar);
                        render_string(chatbar, "");
                        render_tbuf(B, canvas);
                        wrefresh(mainwin);
                    }

                    else if (c[0] == 15) { // ^O --> saving mode
                        mode = SAVE_MODE;
                    }

                    else if (c[0] == 8) { // ^H --> chat
                        mode = CHAT_MODE;
                        werase(inputbar);
                        wmove(inputbar, 0, 1);
                        waddstr(inputbar, "Chat (press enter to send): ");
                        wrefresh(inputbar);
                        msg_i = 0;
                    }

                    else { // these will be sent
                        strncpy(to_send->cmd, "edit", 5);

                        if (c[0] == 27) {
                            read(STDIN_FILENO, &c[1], 2);
                            switch (c[2]) {
                                case 'C': // move cursor right
                                    backward_char(B, me->room_id);
                                    break;

                                case 'D':
                                    forward_char(B, me->room_id);
                                    break;
                            }
                            memcpy(to_send->content, c, 3);
                        }

                        else if (c[0] == 127) { // backspace
                            delete_char(B, me->room_id);
                            memcpy(to_send->content, c, 3);
                        }

                        else if (0 < c[0] && c[0] < 127) { // other characters
                            printf("%c\n", c[0]);
                            insert_char(B, c[0], me->room_id);
                            memcpy(to_send->content, c, 3);
                        }

                        send_to_server(to_send, sizeof(message));
                    }

                    break;

                case CHAT_MODE: // when user is inputing things for the chat
                    if (c[0] == 13) { // enter
                        msg[msg_i] = 0; // null terminator
                        strncpy(to_send->cmd, "chat", 5);
                        strncpy(to_send->content, msg, sizeof(to_send->content));
                        send_to_server(to_send, sizeof(message));
                        werase(inputbar);
                        wrefresh(inputbar);
                        mode = EDIT_MODE;
                    }

                    else if (0 < c[0] && c[0] < 127) { // other chars
                        msg[msg_i] = c[0];
                        msg_i++;
                        waddch(inputbar, c[0]);
                        wrefresh(inputbar);
                    }
                    break;

                case SAVE_MODE: // when user is typing in the to-save filename
                    break;
            }
        }

    }

    if (CONN && !SERVER_DIED) { // send off closing request
        strncpy(to_send->cmd, "exit", 5);
        send_to_server(to_send, sizeof(message));
    }

    curs_set(vis);
    endwin();
    if (SERVER_DIED) {
        printf("The server has shutdown or the owner of the room has quit\n");
    }
    else {
        printf("Exiting...\n");
    }
    return 0;
}
Exemple #7
0
/* renders the header for the text editor */
void render_topbar(WINDOW *w) {
    render_string(w, "NetScribe, the terminal based cross-network editor");
}
Exemple #8
0
int main(int argc, char **argv) {
#if HAVE_DVB_API_VERSION >= 3
  struct dvb_frontend_parameters feparams;
  int tune = 0;
#endif
  int lcd_fd;
  int fe_fd,dmx_fd;
  int lcd_mode;
  fd_set rfds;
  int result;
  screen_t screen;
  struct timeval tv;
  struct signal signal_quality,old_signal;
  struct dmx_sct_filter_params flt;
  unsigned char buf[1024];
  char network_name[31],old_name[31];
  int lcd;

#if HAVE_DVB_API_VERSION >= 3
   if (argc == 2) {
   	if (!strncmp(argv[1], "--tune", 6)) { 
		tune  = 1; 
	}
	else {
		printf("Usage: satfind [--tune]\n");
		return 0;
	} 
  }
#endif

  /* open dbox2-specific devices (LCD) */
  if((lcd_fd=open(LCD,O_RDWR))<0) {
    fprintf(stderr,"lcd open - Can't open LCD: %d\n",errno);
    lcd = 0;
  }
  else {
    lcd = 1;
  }

  /* open nokia-api specific devices (demux,tuner and sat-control) */
  if((dmx_fd=open(DMX,O_RDWR))<0) {
    perror("Can't open Demux");
    return 1;
  }
  
  if((fe_fd=open(FE,O_RDONLY))<0) {
    perror("[satfind.c] Can't open Tuner");
    return 1;
  }

  /* switch LCD to binary mode and clear it */
  if (lcd) { 
    lcd_mode=LCD_MODE_BIN;
    if ((ioctl(lcd_fd,LCD_IOCTL_ASC_MODE,&lcd_mode)<0) || (ioctl(lcd_fd,LCD_IOCTL_CLEAR)<0)) {
      fprintf(stderr,"lcd ioctl - error setting LCD-mode/clearing LCD: %d\n",errno);
      return 1;
    }
    memset(screen,0,sizeof(screen));
  }
  memset(&old_signal,0,sizeof(old_signal));
  
  /* initialize demux to get the NIT */
 
#if HAVE_DVB_API_VERSION >= 3
  memset(&flt, 0, sizeof(flt));
#else
  memset(&flt.filter.filter, 0, DMX_FILTER_SIZE);
  memset(&flt.filter.mask, 0, DMX_FILTER_SIZE);
#endif
  
  flt.pid=0x10;
  flt.filter.filter[0]=0x40;
  flt.filter.mask[0]=0xFF;
  flt.timeout=10000;
  flt.flags=DMX_IMMEDIATE_START;
  
  if (ioctl(dmx_fd, DMX_SET_FILTER, &flt)<0)  {
    perror("DMX_SET_FILTER");
    return -1;
  }
  
  /* main stuff here */
  if (lcd)
    prepare_main(screen);
  network_name[0]=0;
  old_name[0]=0;
  FD_ZERO(&rfds);
  FD_SET(dmx_fd,&rfds);
  tv.tv_sec=0;
  tv.tv_usec=10000;
 
#if HAVE_DVB_API_VERSION >= 3
  if (tune) {
  	/* TP 82 (ProSiebenSat.1 Media AG) on ASTRA 1H */
  	feparams.frequency = 1880000;
  	feparams.inversion = 0;
  	feparams.u.qpsk.symbol_rate = 27500000; 
  	feparams.u.qpsk.fec_inner = FEC_3_4; 
  }
#endif

  while(1) {
    if((result=select(dmx_fd+1,&rfds,NULL,NULL,&tv))>0) {
      if(FD_ISSET(dmx_fd,&rfds)) {
	/* got data */
	if((result=read(dmx_fd,buf,sizeof(buf)))>0)
	  get_network_name_from_nit(network_name,buf,result);
	/* zero or less read - we have to restart the DMX afaik*/
	else {
	  printf("result: %d\n",result);
	  ioctl(dmx_fd,DMX_STOP,0);
	  ioctl(dmx_fd,DMX_START,0);
	  network_name[0]=0;
	}
	/* new networkname != "" */
	if((memcmp(network_name,old_name,sizeof(network_name))!=0)&&(network_name[0]!=0)) {
	  int count;
	  for(count=strlen(network_name);count<=10;count++)
	    network_name[count]=0x20;
	  network_name[count]=0;
	  if (lcd)
	    render_string(screen,0,56,network_name);
	  memcpy(old_name,network_name,sizeof(old_name));
	}
      }
      else
	printf("that should never happen...\n");
    }

    FD_ZERO(&rfds);
    FD_SET(dmx_fd,&rfds);
    tv.tv_sec=0;
    tv.tv_usec=10000;

#if HAVE_DVB_API_VERSION >= 3
  if(tune) {
	/* TUNE and wait till a possibly LOCK is done */
  	ioctl(fe_fd,FE_SET_VOLTAGE,SEC_VOLTAGE_13);
	ioctl(fe_fd,FE_SET_FRONTEND, &feparams);
	ioctl(fe_fd,FE_SET_TONE, SEC_TONE_ON);
	usleep(250);
  }
#endif

    get_signal(&signal_quality,fe_fd);
    if (!signal_changed(&signal_quality, &old_signal))
	    continue;
    if (lcd) {
      draw_signal(&signal_quality,&old_signal,screen);
      draw_screen(screen,lcd_fd);
    }
    printf("%s %u %u %u [%c%c]\n",network_name,signal_quality.ber,signal_quality.snr,signal_quality.strength,signal_quality.status&FE_HAS_SIGNAL? 'S':' ',signal_quality.status&FE_HAS_LOCK? 'L':' ');
  }

  /* close devices */
  if (lcd)
    close(lcd_fd);
  close(dmx_fd);
  close(fe_fd);
  
  return 0;
}
Exemple #9
0
void prepare_main(screen_t screen) {
  render_string(screen,0,8,"ber          0");
  render_string(screen,0,26,"snr          0");
  render_string(screen,0,45,"sig          0");
}
Exemple #10
0
void draw_signal(struct signal *signal_data, struct signal *old_signal, screen_t screen) {
  char dec_buf[11];
  int val,count;
  
  if (signal_data->ber!=old_signal->ber) {
    draw_progressbar(screen,0,0,6,119,&max_values[0],signal_data->ber,4|1);//1656720 max?
    memset(dec_buf,0x20,10);
    dec_buf[10]=0;
    val=signal_data->ber;
    for(count=9;count>=0;count--) {
      dec_buf[count]=(val%10)+0x30;
      val/=10;
      if(val==0)
	break;
    }
    render_string(screen,32,8,dec_buf);
  }
  if (signal_data->snr!=old_signal->snr) {
    draw_progressbar(screen,0,18,6,119,&max_values[1],signal_data->snr,4|1);
    memset(dec_buf,0x20,10);
    dec_buf[10]=0;
    val=signal_data->snr;
    for(count=9;count>=0;count--) {
      dec_buf[count]=(val%10)+0x30;
      val/=10;
      if(val==0)
	break;
    }
    render_string(screen,32,26,dec_buf);
  }
  if (signal_data->strength!=old_signal->strength) {
    draw_progressbar(screen,0,37,6,119,&max_values[2],signal_data->strength,4|1);
    memset(dec_buf,0x20,10);
    dec_buf[10]=0;
    val=signal_data->strength;
    for(count=9;count>=0;count--) {
      dec_buf[count]=(val%10)+0x30;
      val/=10;
      if(val==0)
	break;
    }
    render_string(screen,32,45,dec_buf);
  }
  if ((signal_data->status&FE_HAS_SIGNAL) != (old_signal->status&FE_HAS_SIGNAL)) {
    if(signal_data->status&FE_HAS_SIGNAL) {
      /* we got signal */
      draw_bmp(screen,signal_icon,120-signal_icon[0],64-signal_icon[1]);
      render_string(screen,0,56,"???        ");
    }
    else {
      /* we lost signal */
      draw_rectangle(screen,120-signal_icon[0],64-signal_icon[1],120,64,4);
      render_string(screen,0,56,"NO SIGNAL  ");
    }
  }
  if ((signal_data->status&FE_HAS_LOCK) != (old_signal->status&FE_HAS_LOCK)) {
    if (signal_data->status&FE_HAS_LOCK)
      draw_bmp(screen,lock_icon,120-lock_icon[0]-8,64-lock_icon[1]);
    else
      draw_rectangle(screen,120-lock_icon[0]-8,64-lock_icon[1],120,64,4);
  }
  memcpy(old_signal,signal_data,sizeof(struct signal));
}
Exemple #11
0
static int
render_frame(LinkedList *list,
		int left,	/* left edge of frame */
		int top,	/* top edge of frame */
		int right,	/* right edge of frame */
		int bottom,	/* bottom edge of frame */
		int fwid,	/* frame width? */
		int fhgt,	/* frame height? */
		char fscroll,	/* direction of scrolling */
		int fspeed,	/* speed of scrolling... */
		long timer)	/* current timer tick */
{
	int fy = 0;		/* Scrolling offset for the frame... */

	debug(RPT_DEBUG, "%s(list=%p, left=%d, top=%d, "
			  "right=%d, bottom=%d, fwid=%d, fhgt=%d, "
			  "fscroll='%c', fspeed=%d, timer=%ld)",
			  __FUNCTION__, list, left, top, right, bottom,
			  fwid, fhgt, fscroll, fspeed, timer);

	/* return on no data or illegal height */
	if ((list == NULL) || (fhgt <= 0))
		return -1;

	if (fscroll == 'v') {		/* vertical scrolling */
		// only set offset !=0 when fspeed is != 0 and there is something to scroll
		if ((fspeed != 0) && (fhgt > bottom - top)) {
			int fy_max = fhgt - (bottom - top) + 1;

			fy = (fspeed > 0)
			     ? (timer / fspeed) % fy_max
			     : (-fspeed * timer) % fy_max;

			fy = max(fy, 0);	// safeguard against negative values

			debug(RPT_DEBUG, "%s: fy=%d", __FUNCTION__, fy);
		}
	}
	else if (fscroll == 'h') {	/* horizontal scrolling */
		/* TODO:  Frames don't scroll horizontally yet! */
	}

	/* reset widget list */
	LL_Rewind(list);

	/* loop over all widgets */
	do {
		Widget *w = (Widget *) LL_Get(list);

		if (w == NULL)
			return -1;

		/* TODO:  Make this cleaner and more flexible! */
		switch (w->type) {
		case WID_STRING:
			render_string(w, left, top - fy, right, bottom, fy);
			break;
		case WID_HBAR:
			render_hbar(w, left, top - fy, right, bottom, fy);
			break;
		case WID_VBAR:	  /* FIXME:  Vbars don't work in frames! */
			render_vbar(w, left, top, right, bottom);
			break;
		case WID_ICON:	  /* FIXME:  Icons don't work in frames! */
			drivers_icon(w->x, w->y, w->length);
			break;
		case WID_TITLE:	  /* FIXME:  Doesn't work quite right in frames... */
			render_title(w, left, top, right, bottom, timer);
			break;
		case WID_SCROLLER: /* FIXME: doesn't work in frames... */
			render_scroller(w, left, top, right, bottom, timer);
			break;
		case WID_FRAME:
			{
				/* FIXME: doesn't handle nested frames quite right!
				 * doesn't handle scrolling in nested frames at all...
				 */
				int new_left = left + w->left - 1;
				int new_top = top + w->top - 1;
				int new_right = min(left + w->right, right);
				int new_bottom = min(top + w->bottom, bottom);

				if ((new_left < right) && (new_top < bottom))	/* Render only if it's visible... */
					render_frame(w->frame_screen->widgetlist, new_left, new_top,
							new_right, new_bottom, w->width, w->height,
							w->length, w->speed, timer);
			}
			break;
		case WID_NUM:	  /* FIXME: doesn't work in frames... */
			/* NOTE: y=10 means COLON (:) */
			if ((w->x > 0) && (w->y >= 0) && (w->y <= 10)) {
				drivers_num(w->x + left, w->y);
			}
			break;
		case WID_NONE:
			/* FALLTHROUGH */
		default:
			break;
		}
	} while (LL_Next(list) == 0);

	return 0;
}