void ext_model::update( double time ) { view::update(time); double const fms_calc_step = cfg().model_params.msys_step; if(pilot_impl_) { double dt = last_time_ ? time - *last_time_ : 0.; #if 0 if (!cg::eq_zero(dt)) { geo_base_3 prev_pos = pilot_impl_->state().dyn_state.pos; if (dt > 0) { size_t steps = cg::floor(dt / fms_calc_step + fms_calc_step * .1); fms::pilot_simulation_ptr pilot_sim(pilot_impl_); for (size_t i = 0; i < steps; ++i) { pilot_sim->update(fms_calc_step); } } point_3 offset = prev_pos(pilot_impl_->state().dyn_state.pos); double course = pilot_impl_->state().dyn_state.course; double roll = pilot_impl_->roll(); cpr orien(course, polar_point_3(offset).pitch, roll); set_state(state_t(pilot_impl_->state(), orien.pitch, orien.roll, state_.version + 1), false); if (pilot_impl_->instrument() && pilot_impl_->instrument_ended()) { fms::plan_t plan; if (get_plan().size() > 1) { auto tmp_plan = fms::plan_t(get_plan().begin()+1, get_plan().end()); std::swap(plan, tmp_plan); } else if (pilot_impl_->instrument()->kind() == fms::INSTRUMENT_APPROACH && pilot_impl_->state().dyn_state.cfg != fms::CFG_GD) { plan.push_back(fms::create_direction_instrument(boost::none, get_instruments_env())) ; } set_plan(plan); } } #endif } last_time_ = time; }
sigar_rma_get_average(sigar_rma_stat_t * rma, int rate, sigar_int64_t cur_time_sec, int *result) { float avg = 0; int pos; int count; sigar_rma_sample_t *sample; *result = 0; if (rma == NULL) { *result = -1; return 0.0; } /* Start at our current position and work backwards. */ pos = prev_pos(rma, rma->current_pos); count = 0; while (pos != rma->current_pos) { sample = &rma->samples[pos]; if (sample->stime == 0 || (cur_time_sec - sample->stime > rate)) { break; } avg += sample->value; count++; pos = prev_pos(rma, pos); } if (count == 0) { *result = -1; return 0.0; } return (avg / count); }
bool deallocate(U* t_ptr, size_t size) { size_t size_bytes = size*sizeof(U); char *ptr = (char*)t_ptr; if (_pos != size_t(ptr-_mem)+size_bytes) throw std::runtime_error("attempting to free memory which is not at the top of the stack"); if (ptr_size(ptr) != size) throw std::runtime_error("attempting to free memory of wrong size"); _pos = prev_pos(ptr); return _pos > 0; }
U* allocate(size_t size, size_t align = alignof(U)) { align = std::min(align, alignof(size_t)); size_t size_bytes = size*sizeof(U); char* ptr = align_to(_mem+_pos+2*sizeof(size_t), align); if (ptr+size_bytes > _mem+_size) return nullptr; prev_pos(ptr) = _pos; ptr_size(ptr) = size; _pos = size_t(ptr-_mem)+size_bytes; return (U*)ptr; }
static void input_move_to_eol(void) { const int width_to_end = get_string_width(input_buffer + pos, len - pos, encoding); if (x + width_to_end < ne_columns) { x += width_to_end; pos = len; return; } x = start_x; pos = offset = len; int i = ne_columns - 1 - start_x; for(;;) { const int prev = prev_pos(input_buffer, offset, encoding); const int width = get_char_width(&input_buffer[prev], encoding); if (i - width < 0) break; offset = prev; i -= width; x += width; } input_refresh(); }
void MouseMoveCommand::execute() { if (!this->gamepad_) { return; } sf::Vector2f pos(this->gamepad_->cursor_position()); sf::Vector2f prev_pos(this->gamepad_->prev_cursor_position()); this->target_.target(pos); this->target_.execute(); SceneObjectList curr_list = this->target_.get(); this->target_.target(prev_pos); this->target_.execute(); SceneObjectList prev_list = this->target_.get(); SceneObjectList::const_iterator it; for (it = curr_list.begin(); it != curr_list.end(); ++it) { if (std::find(prev_list.begin(), prev_list.end(), *it) != prev_list.end()) { // intersection of curr and prev lists should execute mouse move command (*it)->on_mouse_move(); } else { // difference of curr - prev should get execute mouse in command (*it)->on_mouse_in(); } } for (it = prev_list.begin(); it != prev_list.end(); ++it) { if (std::find(curr_list.begin(), curr_list.end(), *it) == curr_list.end()) { // difference of prev - curr should get execute mouse out command (*it)->on_mouse_out(); } } }
static void input_autocomplete(void) { int dx = 0, prefix_pos = pos; char *p; /* find a usable prefix */ if (prefix_pos && prefix_pos <= len) { prefix_pos = prev_pos(input_buffer, prefix_pos, encoding); dx--; while (prefix_pos && ne_isword(get_char(&input_buffer[prefix_pos], encoding), encoding)) { dx--; prefix_pos = prev_pos(input_buffer, prefix_pos, encoding); } if (! ne_isword(get_char(&input_buffer[prefix_pos], encoding), encoding)) { dx++; prefix_pos = next_pos(input_buffer, prefix_pos, encoding); } p = malloc(pos - prefix_pos + 1); if (!p) { alert(); /* OUT_OF_MEMORY */ return; } strncpy(p, &input_buffer[prefix_pos], pos - prefix_pos); } else p = malloc(1); /* no prefix left of the cursor; we'll give an empty one. */ p[pos - prefix_pos] = 0; int ac_err; if (p = autocomplete(p, NULL, true, &ac_err)) { encoding_type ac_encoding = detect_encoding(p, strlen(p)); if (ac_encoding != ENC_ASCII && encoding != ENC_ASCII && ac_encoding != encoding) { free(p); alert(); } else { encoding = ac_encoding; if (prefix_pos < pos) { memmove(&input_buffer[prefix_pos], &input_buffer[pos], len - pos + 1); len -= pos - prefix_pos; pos = prefix_pos; } int ac_len = strlen(p); if (ac_len + len >= MAX_INPUT_LINE_LEN) ac_len = MAX_INPUT_LINE_LEN - len; memmove(&input_buffer[pos + ac_len], &input_buffer[pos], len - pos + 1); memmove(&input_buffer[pos], p, ac_len); len += ac_len; while (ac_len > 0) { const int cw = get_char_width(&input_buffer[pos],encoding); pos = next_pos(input_buffer, pos, encoding); ac_len -= cw; dx++; } free(p); x += dx; if (x >= ne_columns) { dx = x - ne_columns + 1; while (dx--) { offset = next_pos(input_buffer, offset, encoding); } x = ne_columns - 1; } } } if (ac_err == AUTOCOMPLETE_COMPLETED || ac_err == AUTOCOMPLETE_CANCELLED) { do_action(cur_buffer, REFRESH_A, 0, NULL); refresh_window(cur_buffer); set_attr(0); print_prompt(NULL); } input_refresh(); }
int shift(buffer * const b, char *p, char *msg, int msg_size) { const bool use_tabs = b->opt.tabs && b->opt.shift_tabs; const int64_t init_line = b->cur_line, init_pos = b->cur_pos, init_y = b->cur_y; line_desc *ld = NULL, *start_line_desc = NULL; int64_t shift_size = 1; char dir = '>'; int shift_mag = b->opt.tab_size, rc = 0; /* Parse parm p; looks like [<|>] ### [s|t], but we allow them in any order, once, with optional white space. */ if (p) { int dir_b = 0, size_b = 0, st_b = 0; while (*p) { if (isasciispace(*p)) p++; else if (!dir_b && (dir_b = (*p == '<' || *p == '>'))) dir = *p++; else if (!size_b && (size_b = isdigit((unsigned char)*p))) { errno = 0; shift_size = strtoll(p, &p, 10); if (errno) return INVALID_SHIFT_SPECIFIED; } else if (!st_b && (st_b = (*p == 's' || *p == 'S'))) { shift_mag = 1; p++; } else if (!st_b && (st_b = (*p == 't' || *p == 'T'))) p++; else return INVALID_SHIFT_SPECIFIED; } } shift_size *= max(1, shift_mag); if (shift_size == 0) return INVALID_SHIFT_SPECIFIED; int64_t first_line = b->cur_line, last_line = b->cur_line, left_col = 0; if (b->marking) { if (b->mark_is_vertical) left_col = min(calc_width(b->cur_line_desc, b->block_start_pos, b->opt.tab_size, b->encoding), calc_width(b->cur_line_desc, b->cur_pos, b->opt.tab_size, b->encoding)); first_line = min(b->block_start_line, b->cur_line); last_line = max(b->block_start_line, b->cur_line); } /* If we're shifting left (dir=='<'), verify that we have sufficient white space to remove on all the relevant lines before making any changes, i. */ if (dir == '<') { shift_size = -shift_size; /* signed shift_size now also indicates direction. */ for (int64_t line = first_line; !rc && line <= last_line; line++) { int64_t pos; goto_line(b, line); pos = calc_pos(b->cur_line_desc, left_col, b->opt.tab_size, b->encoding); while (pos < b->cur_line_desc->line_len && left_col - calc_width(b->cur_line_desc, pos, b->opt.tab_size, b->encoding) > shift_size) { if (isasciispace(b->cur_line_desc->line[pos])) pos = next_pos(b->cur_line_desc->line, pos, b->encoding); else { rc = INSUFFICIENT_WHITESPACE; break; } } } } if (!rc) { start_undo_chain(b); for (int64_t line = first_line; line <= last_line; line++) { int64_t pos, c_pos, c_col_orig, offset; b->attr_len = -1; goto_line(b, line); b->cur_pos = -1; ld = b->cur_line_desc; if (line == first_line) start_line_desc = ld; pos = calc_pos(ld, left_col, b->opt.tab_size, b->encoding); /* If left_col is in the middle of a tab, pos will be on that tab. */ /* whitespace adjustment strategy: 1. Starting from left_col, advance to the right to the first non-blank character C. 2. Note C's col. The desired new column is this value +/- shift_size. 3. Move left looking for the first tab or non-whitespace or the left_col, whichever comes first. Whitespace changes all take place at that transition point. 4. While C's col is wrong if C's col is too far to the right, if we're on a space, delete it; else if there's a tab to our left, delete it; else we should not have started, because it's not possible! if C's col is too far to the left, if its needs to be beyond the next tab stop, insert a tab and move right; else insert a space. */ /* 1. */ while (pos < ld->line_len && isasciispace(ld->line[pos])) pos = next_pos(ld->line, pos, b->encoding); if (pos >= ld->line_len) continue; /* We ran off the end of the line. */ /* line[pos] should be the first non-blank character. */ /* 2. */ c_pos = pos; c_col_orig = calc_width(ld, c_pos, b->opt.tab_size, b->encoding); /* 3. */ while (pos && ld->line[pos-1] == ' ') pos = prev_pos(ld->line, pos, b->encoding); /* If pos is non-zero, it should be on a blank, with only blanks between here and c_pos. */ /* 4. */ /* offset = how_far_we_have_moved - how_far_we_want_to_move. */ while (!stop && (offset = calc_width(ld, c_pos, b->opt.tab_size, b->encoding)-c_col_orig - shift_size)) { if (offset > 0) { /* still too far right; remove whitespace */ if (ld->line[pos] == ' ') { delete_stream(b, ld, b->cur_line, pos, 1); c_pos--; } else if (pos) { /* should be a tab just to our left */ pos = prev_pos(ld->line, pos, b->encoding); /* now we're on the tab */ if (ld->line[pos] == '\t') { delete_stream(b, ld, b->cur_line, pos, 1); c_pos--; } else break; /* Should have been a tab. This should never happen! Give up on this line and go mangle the next one. */ } else break; /* This should never happen; give up on this line and go mangle the next one. */ } else if (offset < 0) { /* too far left; insert whitespace */ char c = ' '; if (use_tabs && (b->opt.tab_size - calc_width(ld, pos, b->opt.tab_size, b->encoding) % b->opt.tab_size) <= -offset ) c = '\t'; if (insert_one_char(b, ld, b->cur_line, pos, c)) { break; } pos++; c_pos++; } } } end_undo_chain(b); if (b->syn) { b->attr_len = -1; need_attr_update = true; update_syntax_states(b, -1, start_line_desc, (line_desc *)ld->ld_node.next); } update_window_lines(b, b->top_line_desc, 0, ne_lines - 2, false); } /* put the screen back where way we found it. */ goto_line_pos(b, init_line, init_pos); delay_update(); const int64_t avshift = b->cur_y - init_y; if (avshift) { snprintf(msg, msg_size, "%c%" PRId64, avshift > 0 ? 'T' :'B', avshift > 0 ? avshift : -avshift); adjust_view(b, msg); } return rc; }
static void trim_trailing_space(buffer * const b, line_desc *ld, const int64_t line, const encoding_type encoding) { if (!ld->line) return; int64_t pos = ld->line_len; while (pos > 0 && isasciispace(ld->line[pos - 1])) pos = prev_pos(ld->line, pos, encoding); if (pos >= 0 && pos < ld->line_len) delete_stream(b, ld, line, pos, ld->line_len - pos); }
int find_matching_bracket(buffer *b, const int64_t min_line, int64_t max_line, int64_t *match_line, int64_t *match_pos, int *c, line_desc ** match_ld) { static unsigned char bracket_table[NUM_BRACKETS][2] = { { '(', ')' }, { '[', ']' }, { '{', '}' }, { '<', '>' }, { '`', '\'' } }; line_desc *ld = b->cur_line_desc; if (b->cur_pos >= ld->line_len) return NOT_ON_A_BRACKET; int i, j, dir; for(i = 0; i < NUM_BRACKETS; i++) { for(j = 0; j < 2; j++) if (ld->line[b->cur_pos] == bracket_table[i][j]) break; if (j < 2) break; } if (i == NUM_BRACKETS && j == 2) return NOT_ON_A_BRACKET; if (j) dir = -1; else dir = 1; int n = 0; int64_t pos = b->cur_pos, y = b->cur_line; while(ld->ld_node.next && ld->ld_node.prev && y >= min_line && y <= max_line) { if (pos >= 0) { char * const line = ld->line; while(pos >= 0 && pos < ld->line_len) { if (line[pos] == bracket_table[i][j]) n++; else if (line[pos] == bracket_table[i][1 - j]) n--; if (n == 0) { *match_line = y; *match_pos = pos; if (c) *c = line[pos]; if (match_ld) *match_ld = ld; return OK; } if (dir > 0) pos = next_pos(line, pos, b->encoding); else pos = prev_pos(line, pos, b->encoding); } } pos = -1; if (dir == 1) { ld = (line_desc *)ld->ld_node.next; if (ld->ld_node.next && ld->line) pos = 0; y++; } else { ld = (line_desc *)ld->ld_node.prev; if (ld->ld_node.prev && ld->line) pos = ld->line_len - 1; y--; } } return CANT_FIND_BRACKET; }