Compare commits
2 Commits
f004d06316
...
6394aeec89
Author | SHA1 | Date |
---|---|---|
nekobit | 6394aeec89 | |
nekobit | 6360a8167e |
|
@ -96,13 +96,17 @@ int mstdnt_await(mastodont_t* mstdnt,
|
|||
* \remark Usually required if you want to interface Mastodont with
|
||||
* application event loops (i.e. GTK, QT, EFL)
|
||||
*
|
||||
* \param set Reference ptr to set
|
||||
* \param nfds Number of file descriptors to check for reading.
|
||||
* \param read Reference ptr to read set
|
||||
* \param write Reference ptr to write set
|
||||
* \param error Reference ptr to error set
|
||||
* \param nfds Number of file descriptors to check for reading.
|
||||
* \return 0 on success, 1 on error
|
||||
*/
|
||||
int
|
||||
mstdnt_get_fds(mastodont_t* mstdnt,
|
||||
fd_set* set,
|
||||
fd_set* read,
|
||||
fd_set* write,
|
||||
fd_set* error,
|
||||
int* nfds);
|
||||
|
||||
void mstdnt_request_cb_cleanup(mstdnt_request_cb_data* data);
|
||||
|
|
114
src/fetch.c
114
src/fetch.c
|
@ -104,9 +104,11 @@ int mstdnt_fetch_curl_async(mastodont_t* mstdnt,
|
|||
printf("error %s\n", curl_multi_strerror(res));
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* int running; */
|
||||
/* res = curl_multi_perform(mstdnt->curl, &running); */
|
||||
|
||||
// TODO add option to "queue" and not perform a request
|
||||
// Get her running...
|
||||
int running;
|
||||
res = curl_multi_perform(mstdnt->curl, &running);
|
||||
/* if (res != CURLM_OK) */
|
||||
/* { */
|
||||
/* printf("error %s\n", curl_multi_strerror(res)); */
|
||||
|
@ -118,15 +120,17 @@ int mstdnt_fetch_curl_async(mastodont_t* mstdnt,
|
|||
|
||||
int
|
||||
mstdnt_get_fds(mastodont_t* mstdnt,
|
||||
fd_set* set,
|
||||
fd_set* read_set,
|
||||
fd_set* write_set,
|
||||
fd_set* error_set,
|
||||
int* nfds)
|
||||
{
|
||||
assert(mstdnt && nfds);
|
||||
|
||||
return curl_multi_fdset(mstdnt->curl,
|
||||
set,
|
||||
NULL,
|
||||
NULL,
|
||||
read_set,
|
||||
write_set,
|
||||
error_set,
|
||||
nfds) != CURLM_OK;
|
||||
}
|
||||
|
||||
|
@ -153,66 +157,68 @@ int mstdnt_await(mastodont_t* mstdnt,
|
|||
}
|
||||
|
||||
int numfds;
|
||||
int running = 1;
|
||||
int running;
|
||||
// Data used with response, must keep it with request
|
||||
struct mstdnt_fetch_data* data;
|
||||
// Data that the user will work with
|
||||
mstdnt_request_cb_data* results = calloc(1, sizeof(mstdnt_request_cb_data));
|
||||
|
||||
// Check if our socket is done
|
||||
// BUG: Reusing data structures if multiple transfers in place
|
||||
do
|
||||
{
|
||||
// TODO error check
|
||||
curl_multi_perform(mstdnt->curl, &running);
|
||||
res = curl_multi_perform(mstdnt->curl, &running);
|
||||
|
||||
if (running)
|
||||
res = curl_multi_poll(mstdnt->curl, fds, nfds, 1000, &numfds);
|
||||
|
||||
res = curl_multi_poll(mstdnt->curl, fds, nfds, 1000, &numfds);
|
||||
|
||||
while ((msg = curl_multi_info_read(mstdnt->curl, &msgs_left)) != NULL)
|
||||
{
|
||||
if (msg->msg == CURLMSG_DONE)
|
||||
{
|
||||
// Get easy info
|
||||
curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &data);
|
||||
// Setup
|
||||
results->fetch_data = data; // So we can clean it up
|
||||
results->storage.needs_cleanup = 0;
|
||||
|
||||
// Get json
|
||||
if (_mstdnt_json_init(&(results->root),
|
||||
data,
|
||||
&(results->storage)))
|
||||
{
|
||||
res = 1;
|
||||
goto cleanup_res;
|
||||
}
|
||||
|
||||
// Pass data to json callback, so it can store it's data
|
||||
if (data->json_cb)
|
||||
res = data->json_cb(results->storage.root,
|
||||
data->json_args,
|
||||
results);
|
||||
|
||||
// Call the actual callback
|
||||
res = data->callback(results, data->callback_args);
|
||||
|
||||
cleanup_res:
|
||||
/* The response of the callback is important!
|
||||
* If the user returns the below response, then the request
|
||||
* must be cleaned up manually by them */
|
||||
if (res != MSTDNT_REQUEST_DATA_NOCLEANUP)
|
||||
{
|
||||
// Will cleanup fetch too
|
||||
mstdnt_request_cb_cleanup(results);
|
||||
}
|
||||
// We can clean the handle up though
|
||||
curl_multi_remove_handle(mstdnt->curl, msg->easy_handle);
|
||||
curl_easy_cleanup(msg->easy_handle);
|
||||
}
|
||||
}
|
||||
|
||||
if (res) break;
|
||||
}
|
||||
while (/* opt == MSTDNT_AWAIT_ALL && msgs_left */ running);
|
||||
|
||||
while ((msg = curl_multi_info_read(mstdnt->curl, &msgs_left)) != NULL)
|
||||
{
|
||||
if (msg->msg == CURLMSG_DONE)
|
||||
{
|
||||
// Get easy info
|
||||
curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &data);
|
||||
// Setup
|
||||
results->fetch_data = data; // So we can clean it up
|
||||
results->storage.needs_cleanup = 0;
|
||||
|
||||
// Get json
|
||||
if (_mstdnt_json_init(&(results->root),
|
||||
data,
|
||||
&(results->storage)))
|
||||
{
|
||||
res = 1;
|
||||
goto cleanup_res;
|
||||
}
|
||||
|
||||
// Pass data to json callback, so it can store it's data
|
||||
if (data->json_cb)
|
||||
res = data->json_cb(results->storage.root,
|
||||
data->json_args,
|
||||
results);
|
||||
|
||||
// Call the actual callback
|
||||
res = data->callback(results, data->callback_args);
|
||||
|
||||
cleanup_res:
|
||||
/* The response of the callback is important!
|
||||
* If the user returns the below response, then the request
|
||||
* must be cleaned up manually by them */
|
||||
if (res != MSTDNT_REQUEST_DATA_NOCLEANUP)
|
||||
{
|
||||
// Will cleanup fetch too
|
||||
mstdnt_request_cb_cleanup(results);
|
||||
}
|
||||
// We can clean the handle up though
|
||||
curl_multi_remove_handle(mstdnt->curl, msg->easy_handle);
|
||||
curl_easy_cleanup(msg->easy_handle);
|
||||
}
|
||||
}
|
||||
|
||||
// Put revents back for callee
|
||||
if (extra_fds)
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
|
||||
#include <Efl_Ui.h>
|
||||
|
||||
void update_mstdnt_fds(void);
|
||||
|
||||
Eo* textbox_instance;
|
||||
Eo* fd;
|
||||
Eo* posts;
|
||||
|
@ -18,11 +20,28 @@ mastodont_t mstdnt;
|
|||
static void
|
||||
gui_quit_cb(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED)
|
||||
{
|
||||
mstdnt_cleanup(&data);
|
||||
mstdnt_cleanup(&mstdnt);
|
||||
mstdnt_global_curl_cleanup();
|
||||
efl_exit(0);
|
||||
}
|
||||
|
||||
static void
|
||||
gui_create_status(struct mstdnt_status* status)
|
||||
{
|
||||
Eo* root = efl_add(EFL_UI_BOX_CLASS,
|
||||
efl_name_set(efl_added, "Status"));
|
||||
|
||||
//Eo* avatar = efl_add();
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
static void
|
||||
gui_add_status(struct mstdnt_status* status)
|
||||
{
|
||||
efl_pack_end(gui_create_status(status));
|
||||
}
|
||||
|
||||
int
|
||||
tl_callback(mstdnt_request_cb_data* cb_data, void* args)
|
||||
{
|
||||
|
@ -31,6 +50,7 @@ tl_callback(mstdnt_request_cb_data* cb_data, void* args)
|
|||
for (int i = 0; i < statuses->len; ++i)
|
||||
{
|
||||
struct mstdnt_status* status = statuses->statuses + i;
|
||||
gui_add_status(status);
|
||||
}
|
||||
|
||||
return MSTDNT_REQUEST_DONE;
|
||||
|
@ -39,36 +59,69 @@ tl_callback(mstdnt_request_cb_data* cb_data, void* args)
|
|||
static void
|
||||
gui_fetch_posts(void* data EINA_UNUSED, const Efl_Event* event EINA_UNUSED)
|
||||
{
|
||||
printf("Got %s...\n", efl_text_get(textbox_instance));
|
||||
|
||||
struct mstdnt_args m_args = {
|
||||
.url = NULL,
|
||||
.url = efl_text_get(textbox_instance),
|
||||
.token = NULL,
|
||||
.flags = 0,
|
||||
};
|
||||
|
||||
mstdnt_timeline_public(&mstdnt, &m_args, tl_callback, NULL, (struct mstdnt_timeline_args){.limit=20});
|
||||
mstdnt_timeline_public(&mstdnt, &m_args, tl_callback, NULL,
|
||||
(struct mstdnt_timeline_args){.limit=20});
|
||||
|
||||
mstdnt_await(&data, 0, NULL, 0);
|
||||
update_mstdnt_fds();
|
||||
}
|
||||
|
||||
#if 0
|
||||
static void
|
||||
update_mstdnt_fds(void)
|
||||
mstdnt_results_cb(void* data EINA_UNUSED, const Efl_Event* event EINA_UNUSED)
|
||||
{
|
||||
printf("Mastodont data ready to be read!\n");
|
||||
mstdnt_await(&mstdnt, 0, NULL, 0);
|
||||
}
|
||||
|
||||
static void
|
||||
mstdnt_update_cb(void* data EINA_UNUSED, const Efl_Event* event EINA_UNUSED)
|
||||
{
|
||||
int running;
|
||||
curl_multi_perform(mstdnt.curl, &running);
|
||||
}
|
||||
|
||||
void
|
||||
update_mstdnt_fds()
|
||||
{
|
||||
int nfds;
|
||||
// Get
|
||||
fd_set set;
|
||||
fd_set read;
|
||||
fd_set write;
|
||||
fd_set error;
|
||||
FD_ZERO(&read);
|
||||
FD_ZERO(&write);
|
||||
FD_ZERO(&error);
|
||||
|
||||
mstdnt_get_fds(&mstdnt, &set, &nfds);
|
||||
mstdnt_get_fds(&mstdnt, &read, &write, &error, &nfds);
|
||||
|
||||
for (int i = 0; i < nfds; ++i)
|
||||
for (int i = 0; i <= nfds; ++i)
|
||||
{
|
||||
fd = efl_add(EFL_LOOP_FD_CLASS,
|
||||
efl_main_loop_get(),
|
||||
efl_loop_fd_set(efl_added, set.fd_array[i]),
|
||||
efl_event_callback_add(efl_added, EFL_LOOP_FD_EVENT_READ, _eo_read_cb, &set));
|
||||
printf("FD_ISSET: %i\n", i);
|
||||
if (FD_ISSET(i, &read))
|
||||
{
|
||||
efl_add(EFL_LOOP_FD_CLASS,
|
||||
efl_main_loop_get(),
|
||||
efl_loop_fd_set(efl_added, i),
|
||||
efl_event_callback_add(efl_added, EFL_LOOP_FD_EVENT_READ, mstdnt_results_cb, &i));
|
||||
}
|
||||
#if 0
|
||||
if (FD_ISSET(i, &write))
|
||||
{
|
||||
efl_add(EFL_LOOP_FD_CLASS,
|
||||
efl_main_loop_get(),
|
||||
efl_loop_fd_set(efl_added, i),
|
||||
efl_event_callback_add(efl_added, EFL_LOOP_FD_EVENT_WRITE, mstdnt_update_cb, &i));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static void
|
||||
gui_setup(void)
|
||||
|
@ -78,7 +131,7 @@ gui_setup(void)
|
|||
win = efl_add(EFL_UI_WIN_CLASS,
|
||||
efl_main_loop_get(),
|
||||
efl_ui_win_type_set(efl_added, EFL_UI_WIN_TYPE_BASIC),
|
||||
efl_text_set(efl_added, "Hello World"),
|
||||
efl_text_set(efl_added, "mastodont-c test"),
|
||||
efl_ui_win_autodel_set(efl_added, EINA_TRUE),
|
||||
efl_event_callback_add(efl_added,
|
||||
EFL_UI_WIN_EVENT_DELETE_REQUEST,
|
||||
|
@ -88,7 +141,7 @@ gui_setup(void)
|
|||
box = efl_add(EFL_UI_BOX_CLASS,
|
||||
win,
|
||||
efl_content_set(win, efl_added),
|
||||
efl_gfx_hint_size_min_set(efl_added, EINA_SIZE2D(460, 640)));
|
||||
efl_gfx_hint_size_min_set(efl_added, EINA_SIZE2D(360, 440)));
|
||||
|
||||
posts = efl_add(EFL_UI_LIST_CLASS,
|
||||
box,
|
||||
|
@ -99,16 +152,16 @@ gui_setup(void)
|
|||
|
||||
textbox_instance = efl_add(EFL_UI_TEXTBOX_CLASS,
|
||||
box,
|
||||
efl_gfx_hint_align_set(efl_added, 0.5, 0.5),
|
||||
efl_gfx_hint_weight_set(efl_added, 1.0, 0.1),
|
||||
efl_pack(box, efl_added));
|
||||
|
||||
efl_add(EFL_UI_BUTTON_CLASS,
|
||||
box,
|
||||
efl_text_set(efl_added, "Fetch posts asynchronously"),
|
||||
efl_gfx_hint_weight_set(efl_added, 1.0, 0.5),
|
||||
efl_gfx_hint_weight_set(efl_added, 1.0, 0.2),
|
||||
efl_pack(box, efl_added),
|
||||
efl_event_callback_add(efl_added, EFL_INPUT_EVENT_CLICKED,
|
||||
gui_fetch_posts, efl_added));
|
||||
gui_fetch_posts, NULL));
|
||||
}
|
||||
|
||||
EAPI_MAIN void
|
||||
|
|
Loading…
Reference in New Issue