Пример #1
0
/* 线程入口*/
void thread1_entry(void* parameter)
{
    int i,j = 1;
    char *ptr[10]; /* 用于放置10个分配内存块的指针*/
    /* 对指针清零*/
    for (i = 0; i <10; i ++) 
        ptr[i] = RT_NULL;
    while(j--)
    {
        for (i = 0; i <10; i++)
        {
        /* 每次分配16字节的内存空间*/
            ptr[i] = rt_malloc(10);
            /* 如果分配成功*/
            if (ptr[i] != RT_NULL)
            {
                rt_kprintf("get memory: 0x%x\n", ptr[i]);
                 rt_realloc(ptr[i],16);
                /* 如果分配成功*/
                if (ptr[i] != RT_NULL)
                {
                    rt_kprintf("memory realloc success!\n");
                    /* 释放内存块*/
                    rt_free(ptr[i]);
                    rt_kprintf("memory free success!\n");
                    ptr[i] = RT_NULL;
                }
            }
        }
    }
}
Пример #2
0
void *mem_Realloc(void *pOld, uint_t nSize)
#endif
{
	void *p;

	if (pOld == NULL)
#if DEBUG_MEMORY_ENABLE
		return _mem_Malloc(nSize, fn, line);
#else
		return mem_Malloc(nSize);
#endif
	if (nSize == 0) {
#if DEBUG_MEMORY_ENABLE
		_mem_Free(pOld, fn, line);
#else
		mem_Free(pOld);
#endif
		return NULL;
	}
	mem_Lock();
#if DEBUG_MEMORY_ENABLE
	p = _rt_realloc(pOld, nSize);
#else
	p = rt_realloc(pOld, nSize);
#endif
	mem_Unlock();
#if DEBUG_MEMORY_ENABLE
	mem_DebugRealloc(p, pOld, fn, line);
#endif
	return p;
}
Пример #3
0
void play_list_append(char* fn)
{
	int media;
	char *ptr;

    play_list_size ++;
	if (play_list == RT_NULL)
		play_list = (struct play_item*) rt_malloc (play_list_size * sizeof(struct play_item));
	else
    	play_list = (struct play_item*) rt_realloc(play_list, play_list_size * sizeof(struct play_item));

	media = media_type(fn);
	if (media == MEDIA_MP3)
	{
		struct tag_info info;

		memset(&info, 0, sizeof(info));
		mp3_get_info(fn, &info);
		
		ptr = strrchr(fn, '/'); //WP MCU工作室
		rt_snprintf(play_list[play_list_size - 1].title, sizeof(play_list[play_list_size - 1].title),
				ptr+1);
		
		/*
		if (info.title[0] == '\0')
			rt_snprintf(play_list[play_list_size - 1].title, sizeof(play_list[play_list_size - 1].title),
				"<未知名音乐>");
		else
			strcpy(play_list[play_list_size - 1].title, info.title);
		*/
		play_list[play_list_size - 1].fn = rt_strdup(fn);
		play_list[play_list_size - 1].duration = info.duration;
	}
	else if (media == MEDIA_RADIO)
	{
		rt_snprintf(play_list[play_list_size - 1].title, sizeof(play_list[play_list_size - 1].title),
			"<未知名电台>");
		play_list[play_list_size - 1].fn = rt_strdup(fn);
		play_list[play_list_size - 1].duration = 0;
	}
	else if (media == MEDIA_WAV)
	{
		struct tag_info info;
		memset(&info, 0, sizeof(info));
		
		get_wav_info(fn, &info);
		ptr = strrchr(fn, '/'); //UP MCU工作室
		rt_snprintf(play_list[play_list_size - 1].title, sizeof(play_list[play_list_size - 1].title),
				ptr+1);
		play_list[play_list_size - 1].fn = rt_strdup(fn);
		play_list[play_list_size - 1].duration = info.duration;
	}
}
Пример #4
0
Файл: edit.c Проект: amsl/RTGUI
rt_bool_t rtgui_edit_readin_file(struct rtgui_edit *edit, const char *filename)
{
	int fd, num=0, read_bytes, size ,len=0;
	char *text ,ch;

	fd = open(filename, O_RDONLY, 0);
	if (fd < 0)
	{
		return RT_FALSE;
	}

	while(edit->max_rows > 0)
		rtgui_edit_delete_line(edit, edit->head);
	edit->max_rows = 0;

	size = edit->bzsize;
	text = rtgui_malloc(size);
	if(text == RT_NULL) return RT_FALSE;
	
	do {
		if ( (read_bytes = read(fd, &ch, 1)) > 0 )
		{
			if(num >= size - 1)
				text = rt_realloc(text, rtgui_edit_alloc_len(size, num));
			if(ch == 0x09) 
			{
				len = edit->tabsize - num%edit->tabsize;
				while(len--)
					*(text + num++) = ' ';
			}
			else
				*(text + num++) = ch;
			if(ch == 0x0A)
			{
				rtgui_edit_append_line(edit, text);
				num = 0;
			}
			
		}
		else if(num > 0)
		{	/* last line does not exist the end operator */
			*(text + num) = '\0';
			rtgui_edit_append_line(edit, text);
		}
	} while(read_bytes);
	
	close(fd);
	rtgui_free(text);
	rtgui_edit_ondraw(edit);

	return RT_TRUE;
}
Пример #5
0
/* append radio */
void play_list_append_radio(const char* url, const char* station)
{
    play_list_size ++;
	if (play_list == RT_NULL)
		play_list = (struct play_item*) rt_malloc (play_list_size * sizeof(struct play_item));
	else
    	play_list = (struct play_item*) rt_realloc(play_list, play_list_size * sizeof(struct play_item));

	strncpy(play_list[play_list_size - 1].title, station, 
		sizeof(play_list[play_list_size - 1].title));
	play_list[play_list_size - 1].fn = rt_strdup(url);
	play_list[play_list_size - 1].duration = 0;
}
Пример #6
0
// TODO the inliner doesn't want to inline these; is there any point to having them in the inline section?
void BoxedList::ensure(int space) {
    if (size + space > capacity) {
        if (capacity == 0) {
            const int INITIAL_CAPACITY = 8;
            int initial = std::max(INITIAL_CAPACITY, space);
            elts = new (initial) BoxedList::ElementArray();
            capacity = initial;
        } else {
            int new_capacity = std::max(capacity * 2, size + space);
            elts = (BoxedList::ElementArray*)rt_realloc(elts, new_capacity * sizeof(Box*) + sizeof(BoxedList::ElementArray));
            capacity = new_capacity;
        }
    }
    assert(capacity >= size + space);
}
Пример #7
0
void* rtgui_realloc(void* ptr, rt_size_t size)
{
	void* new_ptr;

#ifdef RTGUI_MEM_TRACE
	new_ptr = rtgui_malloc(size);
	if ((new_ptr != RT_NULL) && (ptr != RT_NULL))
	{
		rt_memcpy(new_ptr, ptr, size);
		rtgui_free(ptr);
	}
#else
	new_ptr = rt_realloc(ptr, size);
#endif

	return new_ptr;
}
Пример #8
0
Файл: edit.c Проект: amsl/RTGUI
rt_bool_t rtgui_edit_connect_line(struct rtgui_edit* edit, struct edit_line *line, struct edit_line *connect)
{
	rt_size_t len1,len2;
	
	RT_ASSERT(edit != RT_NULL);
	RT_ASSERT(line != RT_NULL);
	RT_ASSERT(connect != RT_NULL);

	len1 = rtgui_edit_line_strlen(line->text);
	len2 = rtgui_edit_line_strlen(connect->text);

	line->zsize = rtgui_edit_alloc_len(edit->bzsize, len1+len2+1);
	line->text = rt_realloc(line->text, line->zsize);
	rt_memcpy(line->text+len1, connect->text, len2);
	*(line->text+len1+len2) = '\0';

	line->len = rtgui_edit_line_strlen(line->text);
	return RT_TRUE;
}
Пример #9
0
void *rtgui_realloc(void *ptr, rt_size_t size)
{
    void *new_ptr;

#ifdef RTGUI_MEM_TRACE
    new_ptr = rtgui_malloc(size);
    if ((new_ptr != RT_NULL) && (ptr != RT_NULL))
    {
        rt_memcpy(new_ptr, ptr, size);
        rtgui_free(ptr);
    }
#else
    new_ptr = rt_realloc(ptr, size);
#endif

#ifdef DEBUG_MEMLEAK
    rt_kprintf("realloc %p to %p (%d) on %p %*.s\n",
               ptr, new_ptr, size, __builtin_return_address(0),
               RT_NAME_MAX, rt_thread_self()->name);
#endif

    return new_ptr;
}
Пример #10
0
Файл: edit.c Проект: amsl/RTGUI
static rt_bool_t rtgui_edit_onkey(pvoid wdt, rtgui_event_t* event)
{
	enum { EDIT_NONE, EDIT_ONDRAW, EDIT_UPDATE };
	struct rtgui_edit *edit = RTGUI_EDIT(wdt);
	struct rtgui_event_kbd *ekbd = (struct rtgui_event_kbd*)event;
	struct edit_line *line=RT_NULL;
	rt_bool_t update_type = EDIT_NONE;
	
	RT_ASSERT(edit != RT_NULL);
	RT_ASSERT(ekbd != RT_NULL);

	if (RTGUI_KBD_IS_UP(ekbd))
	{	/* reset function key */
		if(ekbd->key == RTGUIK_RCTRL || ekbd->key == RTGUIK_LCTRL)
			edit->flag &= ~RTGUI_EDIT_CTRL;
		else if(ekbd->key == RTGUIK_RALT || ekbd->key == RTGUIK_LALT)
			edit->flag &= ~RTGUI_EDIT_ALT;
		else if(ekbd->key == RTGUIK_RSHIFT || ekbd->key == RTGUIK_LSHIFT)
			edit->flag &= ~RTGUI_EDIT_SHIFT;
		else if(ekbd->key == RTGUIK_CAPSLOCK)
			edit->flag &= ~RTGUI_EDIT_CAPSLOCK;
		else if(ekbd->key == RTGUIK_NUMLOCK)
			edit->flag &= ~RTGUI_EDIT_NUMLOCK;
		return RT_TRUE;
	}
	
	line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->visual.y);
	if(line == RT_NULL) 
		return RT_FALSE;

	/* rt_kprintf("key=%04X ",ekbd->key); */
	if(ekbd->key == RTGUIK_RCTRL || ekbd->key == RTGUIK_LCTRL)
	{	/* use CTRL key */
		edit->flag |= RTGUI_EDIT_CTRL;
		return RT_FALSE;
	}
	else if(ekbd->key == RTGUIK_RALT || ekbd->key == RTGUIK_LALT)
	{	/* use ALT key */
		edit->flag |= RTGUI_EDIT_ALT;
		return RT_FALSE;
	}
	else if(ekbd->key == RTGUIK_RSHIFT || ekbd->key == RTGUIK_LSHIFT)
	{	/* use SHIFT key */
		edit->flag |= RTGUI_EDIT_SHIFT;
		return RT_FALSE;
	}
	else if(ekbd->key == RTGUIK_CAPSLOCK)
	{
		edit->flag |= RTGUI_EDIT_CAPSLOCK;
		return RT_FALSE;
	}
	else if(ekbd->key == RTGUIK_NUMLOCK)
	{
		edit->flag |= RTGUI_EDIT_NUMLOCK;
		return RT_FALSE;
	}
	else if(ekbd->key == RTGUIK_DELETE)
	{	/* delete latter character */
		int ofs = edit->upleft.x + edit->visual.x;
		if(ofs > line->len - 1 || (ofs==0 && line->len==0))
		{	/* will the next line marges into the current line */
			struct edit_line* next_line = line->next;
			if(next_line != RT_NULL)
			{
				struct edit_line *update_end_line;
				
				update_type = EDIT_UPDATE;
				edit->update.start = edit->visual;

				rtgui_edit_connect_line(edit, line, next_line);
				rtgui_edit_delete_line(edit, next_line);

				if(edit->max_rows-edit->upleft.y > edit->row_per_page)
				{
					update_end_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y+edit->row_per_page);
					if(update_end_line != RT_NULL)
					{
						edit->update.end.x = edit->col_per_page;
						edit->update.end.y = edit->upleft.y + edit->row_per_page;
					}
				}
				else
				{
					int update_end_index = rtgui_edit_get_index_by_line(edit, edit->tail);
					edit->update.end.x = edit->col_per_page;
					edit->update.end.y = update_end_index+1;
				}
			}
			line->len = rtgui_edit_line_strlen(line->text);
			goto _edit_exit;
		}
		else if(ofs == line->len - 1)
		{
			line->text[ofs] = '\0';
		}
		else
		{
			char *c;
			rt_uint32_t tmp_pos=1;
			identify_double_byte(edit, line, EDIT_IDENT_DIR_RIGHT, &tmp_pos);
			/* remove character */
			for(c = &line->text[ofs]; c[tmp_pos] != '\0'; c++)
				*c = c[tmp_pos];
			*c = '\0';
		}
		update_type = EDIT_UPDATE;
		edit->update.start = edit->visual;
		edit->update.end.x = line->len-edit->upleft.x;
		if (edit->update.end.x > edit->col_per_page)
			edit->update.end.x = edit->col_per_page;
		edit->update.end.y = edit->visual.y;
	}
	else if(ekbd->key == RTGUIK_BACKSPACE)
	{	
		if(edit->visual.x == 0)
		{   /* incorporated into prev line */
			struct rtgui_event_kbd event_kbd;
			struct edit_line* prev_line = line->prev;
			if(prev_line != RT_NULL)
			{
				struct edit_line *update_end_line;
				
				update_type = EDIT_UPDATE;
				edit->visual.x = prev_line->len;

				rtgui_edit_connect_line(edit, prev_line, line);
				kbd_event_set_key(&event_kbd, RTGUIK_UP);
				rtgui_edit_onkey(edit, (rtgui_event_t*)&event_kbd);
				rtgui_edit_delete_line(edit, line);

				edit->update.start = edit->visual; /* update.start.y is changed */
				if(edit->max_rows-edit->upleft.y > edit->row_per_page)
				{
					update_end_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y+edit->row_per_page);
					if(update_end_line != RT_NULL)
					{
						edit->update.end.x = edit->col_per_page;
						edit->update.end.y = edit->upleft.y + edit->row_per_page;
					}
				}
				else
				{
					int update_end_index = rtgui_edit_get_index_by_line(edit, edit->tail);
					edit->update.end.x = edit->col_per_page;
					edit->update.end.y = update_end_index+1;
				}
			}
			goto _edit_exit;
		}
		
		/* delete front character */
		if(edit->visual.x == line->len)
		{
			rt_uint32_t tmp_pos=1;
			identify_double_byte(edit, line, EDIT_IDENT_DIR_LEFT, &tmp_pos);
			line->text[edit->visual.x-tmp_pos] = '\0';
			edit->visual.x -= tmp_pos;
		}
		else if(edit->visual.x != 0)
		{	/* remove current character */
			char *c;
			rt_uint32_t tmp_pos=1;
			identify_double_byte(edit, line, EDIT_IDENT_DIR_LEFT, &tmp_pos);
			/* remove character */
			for(c = &line->text[edit->visual.x - tmp_pos]; c[tmp_pos] != '\0'; c++)
			{
				*c = c[tmp_pos];
			}
			*c = '\0';
			edit->visual.x -= tmp_pos;
		}
		/* adjusted line buffer length */
		if(rtgui_edit_alloc_len(edit->bzsize, line->len+2) < line->zsize)
		{	
			line->zsize = rtgui_edit_alloc_len(edit->bzsize, line->len+1);
			line->text = rt_realloc(line->text, line->zsize);
		}
		update_type = EDIT_UPDATE;
		edit->update.start = edit->visual; 
		edit->update.end.x = line->len;
		edit->update.end.y = edit->visual.y;
	}
	else if(ekbd->key == RTGUIK_UP)
	{	/* move to prev line */
		struct edit_line* prev_line;
		if(edit->visual.y > 0)
			edit->visual.y --;
		else
		{
			/* change first row */
			if(edit->upleft.y > 0)
			{
				edit->upleft.y --;
				if(edit->first_line->prev != RT_NULL)
					edit->first_line = edit->first_line->prev;
				update_type = EDIT_ONDRAW;
			}
		}
		
		/* The position of the recount X */
		prev_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y+edit->visual.y);
		if(prev_line == RT_NULL)
			return RT_FALSE;

		if(edit->upleft.x > 0)
		{
			if(prev_line->len <= edit->upleft.x)
			{
				if(prev_line->len <= edit->col_per_page)
				{
					edit->upleft.x = 0;
					edit->visual.x = prev_line->len;
				}
				else
				{
					edit->upleft.x = prev_line->len - (edit->col_per_page-1);
					edit->visual.x = edit->col_per_page-1;
				}
				update_type = EDIT_ONDRAW;
			}
			else if(prev_line->len - edit->upleft.x < edit->col_per_page)
			{
				if(edit->visual.x > prev_line->len - edit->upleft.x)
					edit->visual.x = prev_line->len - edit->upleft.x;
				else
				{
					rt_uint32_t tmp_pos=0;
					if(identify_double_byte(edit, line, EDIT_IDENT_DIR_LEFT, &tmp_pos))
						edit->visual.x -= (2-tmp_pos);
				}
			}
		}
		else if(edit->visual.x > prev_line->len)
			edit->visual.x = prev_line->len;
		else if(prev_line->len >= 2)
		{
			rt_uint32_t tmp_pos=0;
			if(identify_double_byte(edit, prev_line, EDIT_IDENT_DIR_LEFT, &tmp_pos))
				edit->visual.x -= (2-tmp_pos);
		}

#ifdef RTGUI_EDIT_USING_SCROLL		
		/* update vscroll */
		if(edit->vscroll && !RTGUI_WIDGET_IS_HIDE(edit))
		{
			if(!RTGUI_WIDGET_IS_HIDE(edit->vscroll))
				rtgui_scrollbar_set_value(edit->vscroll,edit->upleft.y);
		}
#endif
	}
	else if(ekbd->key == RTGUIK_DOWN)
	{	
		struct edit_line *tail_line, *next_line;
		tail_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->visual.y);
		if(tail_line != RT_NULL)
		{	/* it is tail line */
			if(tail_line == edit->tail) return RT_FALSE;
		}
		/* move to next line */
		if(edit->visual.y < edit->row_per_page - 2)
		{
			edit->visual.y ++;
		}
		else if(edit->visual.y+edit->upleft.y < edit->max_rows-1)
		{
			/* change first row */
			edit->upleft.y++;
			if(edit->first_line->next != RT_NULL)
				edit->first_line = edit->first_line->next;
			update_type = EDIT_ONDRAW;
		}
		
		/* adjust next line end position */
		next_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y+edit->visual.y);
		if(next_line == RT_NULL)	
			return RT_FALSE;
		
		if(edit->upleft.x > 0)
		{
			if(next_line->len <= edit->upleft.x)
			{
				if(next_line->len <= edit->col_per_page)
				{
					edit->upleft.x = 0;
					edit->visual.x = next_line->len;
				}
				else
				{
					edit->upleft.x = next_line->len - (edit->col_per_page-1);
					edit->visual.x = edit->col_per_page-1;
				}
				update_type = EDIT_ONDRAW;
			}
			else if(next_line->len - edit->upleft.x < edit->col_per_page)
			{
				if(edit->visual.x > next_line->len - edit->upleft.x)
					edit->visual.x = next_line->len - edit->upleft.x;
				else
				{
					rt_uint32_t tmp_pos=0;
					if(identify_double_byte(edit, next_line, EDIT_IDENT_DIR_LEFT, &tmp_pos))
						edit->visual.x -= (2-tmp_pos);
				}
			}
		}
		else if(edit->visual.x > next_line->len)
			edit->visual.x = next_line->len;
		else if(next_line->len >= 2)
		{
			rt_uint32_t tmp_pos=0;
			if(identify_double_byte(edit, next_line, EDIT_IDENT_DIR_LEFT, &tmp_pos))
				edit->visual.x -= (2-tmp_pos);
		}

#ifdef RTGUI_EDIT_USING_SCROLL		
		/* update vscroll */
		if(edit->vscroll && !RTGUI_WIDGET_IS_HIDE(edit))
		{
			if(!RTGUI_WIDGET_IS_HIDE(edit->vscroll))
				rtgui_scrollbar_set_value(edit->vscroll,edit->upleft.y);
		}
#endif
	}
	else if(ekbd->key == RTGUIK_LEFT)
	{	/* move to prev char */
		if(edit->visual.x > 0)
		{
			rt_uint32_t tmp_pos=1;
			identify_double_byte(edit, line, EDIT_IDENT_DIR_LEFT, &tmp_pos);
			edit->visual.x -= tmp_pos;
			if(edit->visual.x == -1)
			{
				edit->visual.x = 0;
				edit->upleft.x -= 1;
				update_type = EDIT_ONDRAW;
			}
		}
		else
		{
			if(edit->upleft.x > 0)
			{
				rt_uint32_t tmp_pos=1;
				identify_double_byte(edit, line, EDIT_IDENT_DIR_LEFT, &tmp_pos);
				edit->upleft.x -= tmp_pos;
				update_type = EDIT_ONDRAW;
			}
			else
			{	
				struct rtgui_event_kbd event_kbd;
				struct edit_line* first_line;
				first_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->visual.y);
				if(first_line != RT_NULL)
				{	/* it is head line */
					if(first_line == edit->head) return RT_FALSE;
				}
				/* move the caret to the prev line end */
				kbd_event_set_key(&event_kbd, RTGUIK_UP);
				rtgui_edit_onkey(edit, (rtgui_event_t*)&event_kbd);
				kbd_event_set_key(&event_kbd, RTGUIK_END);
				rtgui_edit_onkey(edit, (rtgui_event_t*)&event_kbd);
			}
		}
	}
	else if(ekbd->key == RTGUIK_RIGHT)
	{	/* move to next char */
		if(line->len >= edit->col_per_page)
		{
			if(edit->upleft.x+edit->col_per_page <= line->len)
			{
				if(edit->visual.x < edit->col_per_page-1)
				{
					rt_uint32_t tmp_pos=1;
					identify_double_byte(edit, line, EDIT_IDENT_DIR_RIGHT, &tmp_pos);
					edit->visual.x += tmp_pos;
				}
				else if(edit->visual.x == edit->col_per_page-1)
				{
					if(edit->upleft.x+edit->col_per_page < line->len)
						edit->upleft.x ++;
					else
						edit->upleft.x = line->len - edit->col_per_page + 1;
					update_type = EDIT_ONDRAW;
				}
			}
			else
			{
				struct rtgui_event_kbd event_kbd;
				/* move to next head */
				kbd_event_set_key(&event_kbd, RTGUIK_DOWN);
				rtgui_edit_onkey(edit, (rtgui_event_t*)&event_kbd);
				kbd_event_set_key(&event_kbd, RTGUIK_HOME);
				rtgui_edit_onkey(edit, (rtgui_event_t*)&event_kbd);
			}
		}
		else
		{
			if(edit->visual.x < line->len)
			{
				rt_uint32_t tmp_pos=1;
				identify_double_byte(edit, line, EDIT_IDENT_DIR_RIGHT, &tmp_pos);
				edit->visual.x += tmp_pos;
			}
			else
			{
				struct rtgui_event_kbd event_kbd;
				struct edit_line* tail_line;
				tail_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->visual.y);
				if(tail_line != RT_NULL)
				{	/* it is tail line */
					if(tail_line == edit->tail) return RT_FALSE;
				}
				/* move the caret to the next line head */
				kbd_event_set_key(&event_kbd, RTGUIK_DOWN);
				rtgui_edit_onkey(edit, (rtgui_event_t*)&event_kbd);
				kbd_event_set_key(&event_kbd, RTGUIK_HOME);
				rtgui_edit_onkey(edit, (rtgui_event_t*)&event_kbd);
			}
		}
	}
	else if(ekbd->key == RTGUIK_HOME)
	{	/* move cursor to line head */
		edit->visual.x = 0;
		if(edit->upleft.x > 0)
		{
			edit->upleft.x = 0;
			update_type = EDIT_ONDRAW;
		}
	}
	else if(ekbd->key == RTGUIK_END)
	{	/* move cursor to line tail */
		if(line->len >= edit->col_per_page)
		{
			edit->visual.x = edit->col_per_page - 1;
			edit->upleft.x = line->len - (edit->col_per_page-1);
			update_type = EDIT_ONDRAW;
		}
		else
			edit->visual.x = line->len;
	}
	else if(ekbd->key == RTGUIK_TAB)
	{
		int space_nums;
		struct rtgui_event_kbd event_kbd;

		/* using spaces to replace TAB */
		space_nums = edit->tabsize - (edit->upleft.x+edit->visual.x) % edit->tabsize;
		while(space_nums--)
		{
			kbd_event_set_key(&event_kbd, RTGUIK_SPACE);
			rtgui_edit_onkey(edit, (rtgui_event_t*)&event_kbd);
		}
	}
	else if(ekbd->key == RTGUIK_PAGEUP)
	{
		if(edit->max_rows <= edit->row_per_page)
			return RT_FALSE;
	}
	else if(ekbd->key == RTGUIK_PAGEDOWN)
	{
		if(edit->max_rows <= edit->row_per_page)
			return RT_FALSE;
	}
	else if(ekbd->key == RTGUIK_RETURN)
	{
		struct edit_line *update_end_line;
		struct rtgui_event_kbd event_kbd;
	
		/* insert a new line buffer */
		rtgui_edit_insert_line(edit, line, line->text + edit->upleft.x + edit->visual.x);
		line->text[edit->upleft.x + edit->visual.x] = '\0';
		line->len = rtgui_edit_line_strlen(line->text);
		
		/* adjust update line end position */
		if((edit->max_rows-edit->upleft.y) > edit->row_per_page)
		{
			update_type = EDIT_UPDATE;
			edit->update.start = edit->visual;
			update_end_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y+edit->row_per_page-1);
			if(update_end_line != RT_NULL)
			{
				edit->update.end.x = update_end_line->len;
				edit->update.end.y = edit->upleft.y + edit->row_per_page;
			}
		}
		else if((edit->max_rows-edit->upleft.y) < edit->row_per_page)
		{
			int update_end_index = rtgui_edit_get_index_by_line(edit, edit->tail);
			update_type = EDIT_UPDATE;
			edit->update.start = edit->visual;
			edit->update.end.x = edit->tail->len;
			edit->update.end.y = update_end_index;
		}
		
		/* move the caret to the next line head */
		kbd_event_set_key(&event_kbd, RTGUIK_DOWN);
		rtgui_edit_onkey(edit, (rtgui_event_t*)&event_kbd);
		kbd_event_set_key(&event_kbd, RTGUIK_HOME);
		rtgui_edit_onkey(edit, (rtgui_event_t*)&event_kbd);
	}
	else
	{
		if(isprint((unsigned char)ekbd->key))
		{	/* it's may print character */
			update_type = EDIT_UPDATE;
			edit->update.start = edit->visual;
			
			if(edit->flag & RTGUI_EDIT_SHIFT)
				ekbd->key = query_shift_code(ekbd->key);
			if(edit->flag & RTGUI_EDIT_CAPSLOCK)
				ekbd->key = query_caps_code(ekbd->key);

			if(line->len < line->zsize-1)
			{
				int ofs = edit->upleft.x + edit->visual.x;
				if(edit->visual.x >= edit->col_per_page-1)
				{
					edit->upleft.x ++;
					update_type = EDIT_ONDRAW;
				}

				if(ofs < line->len)
				{
					char* c;
					for(c = &line->text[line->len]; c != &line->text[ofs]; c--)
						*c = *(c-1);
				}
				line->text[ofs] = ekbd->key;
				if(edit->visual.x < edit->col_per_page-1)
					edit->visual.x ++;
				line->text[line->len+1] = '\0';
				line->len = rtgui_edit_line_strlen(line->text);
				edit->update.end.x = line->len;
				if(edit->update.end.x > edit->col_per_page)
					edit->update.end.x = edit->col_per_page;
				edit->update.end.y = edit->visual.y;
			}
			else
			{	/* adjust line buffer's zone size */
				line->zsize = rtgui_edit_alloc_len(edit->bzsize, line->len+1);
				line->text = rt_realloc(line->text, line->zsize);
				rtgui_edit_onkey(edit, event); /* reentry */
			}
		}
		else
		{
			/* Is small keyboard ? */
			if(edit->flag & RTGUI_EDIT_NUMLOCK)
			{
				if(is_small_keyboard(&ekbd->key))
					rtgui_edit_onkey(edit, event);
				/* small keyboard another value reserved */
			}
		}
	}
	line->len = rtgui_edit_line_strlen(line->text); 

_edit_exit:
	if(edit->flag & RTGUI_EDIT_CARET)
	{
		if(edit->caret_timer != RT_NULL)
			rtgui_timer_stop(edit->caret_timer);

		edit->flag &= ~RTGUI_EDIT_CARET;
		rtgui_edit_draw_caret(edit);/* refresh it */
		if(edit->caret_timer != RT_NULL)
			rtgui_timer_start(edit->caret_timer);
	}

	/* re-draw edit widget */
	if(update_type == EDIT_ONDRAW)
		rtgui_edit_ondraw(edit);
	else if(update_type == EDIT_UPDATE)
		rtgui_edit_update(edit);

	if(RTGUI_WIDGET_IS_FOCUSED(edit))
	{
		rtgui_edit_init_caret(edit, edit->visual);
		edit->flag |= RTGUI_EDIT_CARET;
		rtgui_edit_draw_caret(edit);
	}
	return RT_TRUE;
}
Пример #11
0
void *realloc(void *ptr, size_t size)
{
	return rt_realloc(ptr, size);
}
Пример #12
0
/***************************************************************************//**
* @brief
*   Configure USART device
*
* @details
*
* @note
*
* @param[in] dev
*   Pointer to device descriptor
*
* @param[in] cmd
*   IIC control command
*
* @param[in] args
*   Arguments
*
* @return
*   Error code
******************************************************************************/
static rt_err_t rt_usart_control (
    rt_device_t     dev,
    rt_uint8_t      cmd,
    void            *args)
{
    RT_ASSERT(dev != RT_NULL);

    rt_err_t    err_code;
    struct efm32_usart_device_t *usart;

    usart = (struct efm32_usart_device_t *)(dev->user_data);

    /* Lock device */
    if (rt_hw_interrupt_check())
    {
        err_code = rt_sem_take(usart->lock, RT_WAITING_NO);
    }
    else
    {
        err_code = rt_sem_take(usart->lock, RT_WAITING_FOREVER);
    }
    if (err_code != RT_EOK)
    {
        return err_code;
    }

    switch (cmd)
    {
    case RT_DEVICE_CTRL_SUSPEND:
        /* Suspend device */
        dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
        USART_Enable(usart->usart_device, usartDisable);
        break;

    case RT_DEVICE_CTRL_RESUME:
        /* Resume device */
        dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
        USART_Enable(usart->usart_device, usartEnable);
        break;

    case RT_DEVICE_CTRL_USART_RBUFFER:
        /* Set RX buffer */
    {
        struct efm32_usart_int_mode_t *int_rx;
        rt_uint8_t size;

        int_rx = (struct efm32_usart_int_mode_t *)(usart->rx_mode);
        size = (rt_uint8_t)((rt_uint32_t)args & 0xFFUL);

        /* Free previous RX buffer */
        if (int_rx->data_ptr != RT_NULL)
        {
            if (size == 0)
            {   /* Free RX buffer */
                rt_free(int_rx->data_ptr);
                int_rx->data_ptr = RT_NULL;
            }
            else if (size != int_rx->data_size)
            {
                /* Re-allocate RX buffer */
                if ((int_rx->data_ptr = rt_realloc(int_rx->data_ptr, size)) \
                        == RT_NULL)
                {
                    usart_debug("USART%d err: no mem for RX BUF\n", usart->unit);
                    err_code = -RT_ENOMEM;
                    break;
                }
                // TODO: Is the following line necessary?
                //rt_memset(int_rx->data_ptr, 0, size);
            }
        }
        else
        {
            /* Allocate new RX buffer */
            if ((int_rx->data_ptr = rt_malloc(size)) == RT_NULL)
            {
                usart_debug("USART%d err: no mem for RX BUF\n", usart->unit);
                err_code = -RT_ENOMEM;
                break;
            }
        }
        int_rx->data_size = size;
        int_rx->read_index = 0;
        int_rx->save_index = 0;
    }
    break;

    }

    /* Unlock device */
    rt_sem_release(usart->lock);

    return err_code;
}