update radio_list_update by inkfish

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@510 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
wuyangyong 2010-03-18 08:34:34 +00:00
parent bfc473ebee
commit a4ce05206f
2 changed files with 749 additions and 553 deletions

View File

@ -23,7 +23,8 @@
#define RADIO_FN "/radio.pls"
const static char *time_bg_xpm[] = {
const static char *time_bg_xpm[] =
{
"48 20 7 1",
". c #007DC6",
"+ c #0079C6",
@ -52,7 +53,8 @@ const static char *time_bg_xpm[] = {
"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&",
"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&",
"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&",
"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"};
"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"
};
static struct rtgui_view* home_view;
static struct rtgui_list_view* function_view;
@ -96,8 +98,10 @@ static void info_timer_timeout(rtgui_timer_t* timer, void* parameter)
play_time++;
rt_snprintf(line, sizeof(line), "%3d:%02d", play_time / 60, play_time % 60);
rect.x1 = 172; rect.y1 = 39;
rect.x2 = 220; rect.y2 = 59;
rect.x1 = 172;
rect.y1 = 39;
rect.x2 = 220;
rect.y2 = 59;
rtgui_image_blit(time_bg_image, dc, &rect);
rtgui_dc_draw_text(dc, line, &rect);
}
@ -115,8 +119,10 @@ static void player_update_tag_info(struct rtgui_dc* dc)
saved = rtgui_dc_get_color(dc);
rtgui_dc_set_color(dc, black);
rect.x1 = 0; rect.y1 = 0;
rect.x2 = 240; rect.y2 = 65;
rect.x1 = 0;
rect.y1 = 0;
rect.x2 = 240;
rect.y2 = 65;
/* draw background */
background = rtgui_image_create_from_file("hdc", "/resource/bg.hdc", RT_FALSE);
if (background != RT_NULL)
@ -132,8 +138,10 @@ static void player_update_tag_info(struct rtgui_dc* dc)
}
/* draw playing information */
rect.x1 = 28; rect.y1 = 12;
rect.x2 = 220; rect.y2 = rect.y1 + 16;
rect.x1 = 28;
rect.y1 = 12;
rect.x2 = 220;
rect.y2 = rect.y1 + 16;
if (player_mode == PLAYER_STOP)
{
rt_snprintf(line, sizeof(line), "网络收音机");
@ -142,8 +150,10 @@ static void player_update_tag_info(struct rtgui_dc* dc)
else
rtgui_dc_draw_text(dc, tinfo.title, &rect);
rect.x1 = 28; rect.y1 = 39;
rect.x2 = 220; rect.y2 = 59;
rect.x1 = 28;
rect.y1 = 39;
rect.x2 = 220;
rect.y2 = 59;
if (player_mode == PLAYER_STOP)
{
rt_snprintf(line, sizeof(line), "radio.rt-thread.org");
@ -220,6 +230,8 @@ static void function_radio_list_update(void* parameter)
extern rt_mq_t update_radio_mq;
rt_thread_t update_radio_list_thread;
rtgui_view_t *view;
extern rtgui_view_t* update_radio_list_view_create(rtgui_workbench_t* workbench);
if(update_radio_mq == RT_NULL)
{
@ -237,6 +249,13 @@ static void function_radio_list_update(void* parameter)
update_radio_list_req();
}
}
view = update_radio_list_view_create(workbench);
if (view != RT_NULL)
{
rtgui_view_show(view, RT_FALSE);
}
return;
}
@ -302,7 +321,8 @@ static void function_filelist(void* parameter)
}
else play_list_append(line);
}
} while (length > 0);
}
while (length > 0);
close(fd);
@ -464,8 +484,10 @@ void player_play_file(const char* fn)
/* update play button */
button = rtgui_image_create_from_mem("hdc",
play_hdh, sizeof(play_hdh), RT_FALSE);
play_rect.x1 = 32; play_rect.y1 = 92;
play_rect.x2 = 61; play_rect.y2 = 114;
play_rect.x1 = 32;
play_rect.y1 = 92;
play_rect.x2 = 61;
play_rect.y2 = 114;
rtgui_image_blit(button, dc, &play_rect);
rtgui_image_destroy(button);
@ -501,8 +523,10 @@ void player_play_url(const char* url)
/* update play button */
button = rtgui_image_create_from_mem("hdc",
play_hdh, sizeof(play_hdh), RT_FALSE);
play_rect.x1 = 32; play_rect.y1 = 92;
play_rect.x2 = 61; play_rect.y2 = 114;
play_rect.x1 = 32;
play_rect.y1 = 92;
play_rect.x2 = 61;
play_rect.y2 = 114;
rtgui_image_blit(button, dc, &play_rect);
rtgui_image_destroy(button);
@ -544,8 +568,10 @@ static rt_bool_t home_view_event_handler(struct rtgui_widget* widget, struct rtg
play = rtgui_image_create_from_mem("hdc", stop_hdh, sizeof(stop_hdh), RT_FALSE);
else
play = rtgui_image_create_from_mem("hdc", play_hdh, sizeof(play_hdh), RT_FALSE);
play_rect.x1 = 32; play_rect.y1 = 92;
play_rect.x2 = 61; play_rect.y2 = 114;
play_rect.x1 = 32;
play_rect.y1 = 92;
play_rect.x2 = 61;
play_rect.y2 = 114;
rtgui_image_blit(play, dc, &play_rect);
rtgui_image_destroy(play);
}
@ -559,8 +585,10 @@ static rt_bool_t home_view_event_handler(struct rtgui_widget* widget, struct rtg
{
char line[32];
rect.x1 = 28; rect.y1 = 12;
rect.x2 = 220; rect.y2 = rect.y1 + 16;
rect.x1 = 28;
rect.y1 = 12;
rect.x2 = 220;
rect.y2 = rect.y1 + 16;
if (player_mode == PLAYER_STOP)
{
rt_snprintf(line, sizeof(line), "网络收音机");
@ -569,8 +597,10 @@ static rt_bool_t home_view_event_handler(struct rtgui_widget* widget, struct rtg
else
rtgui_dc_draw_text(dc, tinfo.title, &rect);
rect.x1 = 28; rect.y1 = 39;
rect.x2 = 170; rect.y2 = 59;
rect.x1 = 28;
rect.y1 = 39;
rect.x2 = 170;
rect.y2 = 59;
if (player_mode == PLAYER_STOP)
{
rect.x2 = 220;
@ -584,7 +614,8 @@ static rt_bool_t home_view_event_handler(struct rtgui_widget* widget, struct rtg
{
rt_uint32_t t = player_mode == PLAYER_STOP ? tinfo.duration : play_time;
rect.x1 = 172; rect.x2 = 220;
rect.x1 = 172;
rect.x2 = 220;
rt_snprintf(line, sizeof(line), "%3d:%02d", t / 60, t % 60);
rtgui_dc_draw_text(dc, line, &rect);
}
@ -601,20 +632,25 @@ static rt_bool_t home_view_event_handler(struct rtgui_widget* widget, struct rtg
rt_uint32_t index;
struct play_item* item;
rect.x1 = 20; rect.y1 = 150;
rect.x2 = 170; rect.y2 = 168;
rect.x1 = 20;
rect.y1 = 150;
rect.x2 = 170;
rect.y2 = 168;
for (index = 0; index < play_list_items() && index < 8; index ++)
{
item = play_list_item(index);
rtgui_dc_draw_text(dc, item->title, &rect);
rect.x1 = 172; rect.x2 = 220;
rect.x1 = 172;
rect.x2 = 220;
rt_snprintf(line, sizeof(line), "%3d:%02d", item->duration / 60, item->duration % 60);
rtgui_dc_draw_text(dc, line, &rect);
/* move to next item */
rect.x1 = 20; rect.x2 = 170;
rect.y1 += 18; rect.y2 += 18;
rect.x1 = 20;
rect.x2 = 170;
rect.y1 += 18;
rect.y2 += 18;
}
}
rtgui_dc_end_drawing(dc);
@ -710,8 +746,10 @@ static rt_bool_t home_view_event_handler(struct rtgui_widget* widget, struct rtg
/* update play button */
button = rtgui_image_create_from_mem("hdc", stop_hdh, sizeof(stop_hdh), RT_FALSE);
play_rect.x1 = 32; play_rect.y1 = 92;
play_rect.x2 = 61; play_rect.y2 = 114;
play_rect.x1 = 32;
play_rect.y1 = 92;
play_rect.x2 = 61;
play_rect.y2 = 114;
rtgui_image_blit(button, dc, &play_rect);
rtgui_image_destroy(button);
@ -747,8 +785,10 @@ static rt_bool_t home_view_event_handler(struct rtgui_widget* widget, struct rtg
/* update play button */
button = rtgui_image_create_from_mem("hdc",
stop_hdh, sizeof(stop_hdh), RT_FALSE);
play_rect.x1 = 32; play_rect.y1 = 92;
play_rect.x2 = 61; play_rect.y2 = 114;
play_rect.x1 = 32;
play_rect.y1 = 92;
play_rect.x2 = 61;
play_rect.y2 = 114;
rtgui_image_blit(button, dc, &play_rect);
rtgui_image_destroy(button);
@ -797,8 +837,10 @@ static rt_bool_t home_view_event_handler(struct rtgui_widget* widget, struct rtg
/* set container to window rect */
container_rect = rect;
rect.x1 = 0; rect.y1 = 0;
rect.x2 = 120; rect.y2 = 20;
rect.x1 = 0;
rect.y1 = 0;
rect.x2 = 120;
rect.y2 = 20;
label = rtgui_label_create("USB 联机中...");
rtgui_rect_moveto_align(&container_rect, &rect, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL);
rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);

View File

@ -1,3 +1,11 @@
#include <rtgui/rtgui.h>
#include <rtgui/rtgui_system.h>
#include <stdio.h>
#include <stdlib.h>
#include <rtgui/widgets/view.h>
#include <rtgui/widgets/workbench.h>
#include <stm32f10x.h>
#include <rtthread.h>
#include <dfs_posix.h>
@ -20,8 +28,16 @@ extern int http_resolve_address(struct sockaddr_in *server, const char * url, ch
const char _radio_list_update_get[] = "GET %s HTTP/1.0\r\nHost: %s:%d\r\nUser-Agent: RT-Thread HTTP Agent\r\nConnection: close\r\n\r\n";
rt_mq_t update_radio_mq = RT_NULL;
static struct rtgui_view* update_radio_list_view = RT_NULL;
typedef enum
{
UPDATE_RAIDO_LIST_PROC,
UPDATE_RADIO_LIST_CONNECT_FAILED,
UPDATE_RADIO_LIST_SUCCEED,
} UPDATE_RADIO_LIST_STATE;
static UPDATE_RADIO_LIST_STATE update_radio_list_state;
int radio_list_update_servicer_connect(struct sockaddr_in* server, char* host_addr, const char* url)
{
@ -39,6 +55,7 @@ int radio_list_update_servicer_connect(struct sockaddr_in* server, char* host_a
peer_handle = connect( socket_handle, (struct sockaddr *) server, sizeof(*server));
if ( peer_handle < 0 )
{
closesocket(socket_handle);
rt_kprintf( "RLUS: CONNECT FAILED %i\n", peer_handle );
return -1;
}
@ -66,8 +83,11 @@ int radio_list_update_servicer_connect(struct sockaddr_in* server, char* host_a
rc = http_read_line(peer_handle, mimeBuffer, 100);
rt_kprintf(">>%s", mimeBuffer);
if ( rc < 0 ) return rc;
if ( rc < 0 )
{
closesocket(peer_handle);
return rc;
}
// End of headers is a blank line. exit.
if (rc == 0) break;
if ((rc == 2) && (mimeBuffer[0] == '\r')) break;
@ -78,6 +98,7 @@ int radio_list_update_servicer_connect(struct sockaddr_in* server, char* host_a
if(rc)
{
rt_kprintf("HTTP: status code = %d!\n", rc);
closesocket(peer_handle);
return -rc;
}
}
@ -88,6 +109,7 @@ int radio_list_update_servicer_connect(struct sockaddr_in* server, char* host_a
if (strstr(mimeBuffer, "text/plain") == RT_NULL)
{
rt_kprintf("radio list update content is not text/plain.\n");
closesocket(peer_handle);
return -1;
}
}
@ -98,6 +120,7 @@ int radio_list_update_servicer_connect(struct sockaddr_in* server, char* host_a
if (strstr(mimeBuffer, "text/plain") == RT_NULL)
{
rt_kprintf("radio list update content is not text/plain.\n");
closesocket(peer_handle);
return -1;
}
}
@ -105,9 +128,10 @@ int radio_list_update_servicer_connect(struct sockaddr_in* server, char* host_a
return peer_handle;
}
int radio_list_update_servicer_session_open(char* url)
{
int peer_handle = 0;
int peer_handle = -1;
struct sockaddr_in server;
char *request, host_addr[32];
@ -124,7 +148,7 @@ int radio_list_update_servicer_session_open(char* url)
return peer_handle;
}
void update_radio_list(char* url)
int update_radio_list(char* url)
{
char *buf = NULL;
int peer_handle = 0;
@ -135,13 +159,13 @@ void update_radio_list(char* url)
if(peer_handle < 0)
{
return;
return -1;
}
fd = open(RADIO_FN, O_WRONLY | O_CREAT | O_TRUNC, 0);
if(fd < 0)
{
return;
return -1;
}
buf = rt_malloc (512);
while ( 1 )
@ -160,11 +184,13 @@ void update_radio_list(char* url)
}
rt_free(buf);
closesocket(peer_handle);
if(close(fd) == 0)
{
rt_kprintf("Update radio list succeed \r\n");
}
return 0;
}
void update_radio_list_req(void)
@ -174,6 +200,7 @@ void update_radio_list_req(void)
extern rt_mq_t player_thread_mq;
char* ch = RT_NULL;
update_radio_list_state = UPDATE_RAIDO_LIST_PROC;
request.type = PLAYER_REQUEST_UPDATE_RADIO_LIST;
ch = strncpy(request.fn, RADIO_LIST_UPDATE_URL, strlen(RADIO_LIST_UPDATE_URL));
@ -187,6 +214,8 @@ void update_radio_thread(void* parameter)
rt_err_t result;
struct player_request request;
rt_thread_t update_radio_list_thread;
while(1)
{
/* get request from message queue */
@ -196,11 +225,25 @@ void update_radio_thread(void* parameter)
{
switch (request.type)
{
case PLAYER_REQUEST_UPDATE_RADIO_LIST:
if ((strstr(request.fn, "http://") == request.fn ||
(strstr(request.fn, "HTTP://") == request.fn )))
{
update_radio_list(request.fn);
struct rtgui_event_command ecmd;
if(update_radio_list(request.fn)==0)
{
update_radio_list_state = UPDATE_RADIO_LIST_SUCCEED;
}
else
{
update_radio_list_state = UPDATE_RADIO_LIST_CONNECT_FAILED;
}
RTGUI_EVENT_COMMAND_INIT(&ecmd);
ecmd.type = RTGUI_EVENT_PAINT;
ecmd.command_id = PLAYER_REQUEST_UPDATE_RADIO_LIST;
rtgui_thread_send(rt_thread_find("ply_ui"), &ecmd.parent, sizeof(ecmd));
}
break;
}
@ -212,3 +255,114 @@ void update_radio_thread(void* parameter)
}
}
void drawing_update_state_info(struct rtgui_widget* widget)
{
char* line;
struct rtgui_dc* dc;
struct rtgui_rect rect;
line = rtgui_malloc(256);
if (line == RT_NULL) return ;
dc = rtgui_dc_begin_drawing(widget);
if (dc == RT_NULL)
{
rt_free(line);
return;
}
rtgui_widget_get_rect(widget, &rect);
/* fill background */
rtgui_dc_fill_rect(dc, &rect);
rect.y2 = rect.y1 + 18;
switch(update_radio_list_state)
{
case UPDATE_RAIDO_LIST_PROC:
sprintf(line, "正在更新电台列表......");
break ;
case UPDATE_RADIO_LIST_CONNECT_FAILED:
sprintf(line, "更新电台列表失败,请返回");
break;
case UPDATE_RADIO_LIST_SUCCEED:
sprintf(line, "更新电台列表成功,请返回");
break;
}
rtgui_dc_draw_text(dc, line, &rect);
rtgui_dc_end_drawing(dc);
rtgui_free(line);
}
static rt_bool_t update_radio_list_view_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
{
switch (event->type)
{
case RTGUI_EVENT_COMMAND:
{
struct rtgui_event_command* ecmd = (struct rtgui_event_command*)event;
switch(ecmd->type)
{
case RTGUI_EVENT_PAINT:
drawing_update_state_info(widget);
return RT_FALSE;
}
return RT_FALSE;
}
case RTGUI_EVENT_PAINT:
{
drawing_update_state_info(widget);
return RT_FALSE;
}
case RTGUI_EVENT_KBD:
{
struct rtgui_event_kbd* ekbd;
ekbd = (struct rtgui_event_kbd*)event;
if (ekbd->type == RTGUI_KEYDOWN && ekbd->key == RTGUIK_RETURN)
{
rtgui_workbench_t* workbench;
workbench = RTGUI_WORKBENCH(RTGUI_WIDGET(update_radio_list_view)->parent);
rtgui_workbench_remove_view(workbench, update_radio_list_view);
rtgui_view_destroy(update_radio_list_view);
update_radio_list_view = RT_NULL;
}
}
return RT_FALSE;
}
/* use parent event handler */
return rtgui_view_event_handler(widget, event);
}
rtgui_view_t *update_radio_list_view_create(rtgui_workbench_t* workbench)
{
if (update_radio_list_view != RT_NULL)
{
rtgui_view_show(update_radio_list_view, RT_FALSE);
}
else
{
/* create a view */
update_radio_list_view = rtgui_view_create("update radio list Info");
/* set view event handler */
rtgui_widget_set_event_handler(RTGUI_WIDGET(update_radio_list_view), update_radio_list_view_event_handler);
/* this view can be focused */
RTGUI_WIDGET(update_radio_list_view)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
/* add view to workbench */
rtgui_workbench_add_view(workbench, update_radio_list_view);
}
return update_radio_list_view;
}