Changeset 3462:c8823247312c in roaraudio for libroarpulse


Ignore:
Timestamp:
02/13/10 22:22:42 (10 years ago)
Author:
phi
Branch:
default
Message:

wrote basic framework

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libroarpulse/mainloop.c

    r3461 r3462  
    3939#include <libroarpulse/libroarpulse.h> 
    4040 
     41struct pa_mainloop { 
     42 pa_mainloop_api api; 
     43 pa_poll_func    poll_func; 
     44 void          * poll_userdata; 
     45 int             quit; 
     46 int             quitval; 
     47}; 
     48 
    4149/** Allocate a new main loop object */ 
    42 pa_mainloop *pa_mainloop_new(void); 
     50pa_mainloop *pa_mainloop_new(void) { 
     51 pa_mainloop * m = roar_mm_malloc(sizeof(pa_mainloop)); 
     52 
     53 if ( m == NULL ) 
     54  return NULL; 
     55 
     56 memset(m, 0, sizeof(pa_mainloop)); 
     57 
     58 m->api.userdata = m; 
     59 
     60 return m; 
     61} 
    4362 
    4463/** Free a main loop object */ 
    45 void pa_mainloop_free(pa_mainloop* m); 
     64void pa_mainloop_free(pa_mainloop* m) { 
     65 if ( m == NULL ) 
     66  return; 
     67 
     68 roar_mm_free(m); 
     69} 
    4670 
    4771/** Prepare for a single iteration of the main loop. Returns a negative value 
    4872on error or exit request. timeout specifies a maximum timeout for the subsequent 
    4973poll, or -1 for blocking behaviour. .*/ 
    50 int pa_mainloop_prepare(pa_mainloop *m, int timeout); 
     74int pa_mainloop_prepare(pa_mainloop *m, int timeout) { 
     75 if ( m == NULL ) 
     76  return -1; 
     77 
     78 m->quit = 1; 
     79 
     80 if ( m->quit ) 
     81  return -2; 
     82 
     83 return -1; 
     84} 
    5185 
    5286/** Execute the previously prepared poll. Returns a negative value on error.*/ 
    53 int pa_mainloop_poll(pa_mainloop *m); 
     87int pa_mainloop_poll(pa_mainloop *m) { 
     88 if ( m == NULL ) 
     89  return -1; 
     90 
     91 if ( m->quit ) 
     92  return -2; 
     93 
     94 return -1; 
     95} 
    5496 
    5597/** Dispatch timeout, io and deferred events from the previously executed poll. Returns 
    5698a negative value on error. On success returns the number of source dispatched. */ 
    57 int pa_mainloop_dispatch(pa_mainloop *m); 
     99int pa_mainloop_dispatch(pa_mainloop *m) { 
     100 if ( m == NULL ) 
     101  return -1; 
     102 
     103 if ( m->quit ) 
     104  return -2; 
     105 
     106 return -1; 
     107} 
    58108 
    59109/** Return the return value as specified with the main loop's quit() routine. */ 
    60 int pa_mainloop_get_retval(pa_mainloop *m); 
     110int pa_mainloop_get_retval(pa_mainloop *m) { 
     111 if ( m == NULL ) 
     112  return -1; 
     113 
     114 return m->quitval; 
     115} 
    61116 
    62117/** Run a single iteration of the main loop. This is a convenience function 
     
    66121specified with the main loop's quit() routine in the integer variable retval points 
    67122to. On success returns the number of sources dispatched in this iteration. */ 
    68 int pa_mainloop_iterate(pa_mainloop *m, int block, int *retval); 
     123int pa_mainloop_iterate(pa_mainloop *m, int block, int *retval) { 
     124 int r; 
     125 
     126 if ( m == NULL ) 
     127  return -1; 
     128 
     129 r = pa_mainloop_prepare(m, block ? -1 : 0); 
     130 
     131 if ( r > 0 ) 
     132  r = pa_mainloop_poll(m); 
     133 
     134 if ( r > 0 ) 
     135  r = pa_mainloop_dispatch(m); 
     136 
     137 if ( r == -2 && retval != NULL ) { 
     138  *retval = m->quitval; 
     139 } 
     140 
     141 return r; 
     142} 
    69143 
    70144/** Run unlimited iterations of the main loop object until the main loop's quit() routine is called. */ 
    71 int pa_mainloop_run(pa_mainloop *m, int *retval); 
     145int pa_mainloop_run(pa_mainloop *m, int *retval) { 
     146 int r = 1; 
     147 
     148 if ( m == NULL ) 
     149  return -1; 
     150 
     151 while (!(m->quit) && r > 0) { 
     152  r = pa_mainloop_iterate(m, 1, retval); 
     153 } 
     154 
     155 if ( r == -2 ) 
     156  return 1; 
     157 
     158 if ( r < 0 ) 
     159  return -1; 
     160 
     161 return 0; 
     162} 
    72163 
    73164/** Return the abstract main loop abstraction layer vtable for this main loop. */ 
    74 pa_mainloop_api* pa_mainloop_get_api(pa_mainloop*m); 
     165pa_mainloop_api* pa_mainloop_get_api(pa_mainloop*m) { 
     166 if ( m == NULL ) 
     167  return NULL; 
     168 
     169 return &(m->api); 
     170} 
    75171 
    76172/** Shutdown the main loop */ 
    77 void pa_mainloop_quit(pa_mainloop *m, int r); 
     173void pa_mainloop_quit(pa_mainloop *m, int r) { 
     174 if ( m == NULL ) 
     175  return; 
     176 
     177 m->quitval = r; 
     178 m->quit    = 1; 
     179} 
    78180 
    79181/** Interrupt a running poll (for threaded systems) */ 
    80182void pa_mainloop_wakeup(pa_mainloop *m); 
    81183 
    82 /** Generic prototype of a poll() like function */ 
    83 typedef int (*pa_poll_func)(struct pollfd *ufds, unsigned long nfds, int timeout, void*userdata); 
     184/** Change the poll() implementation */ 
     185void pa_mainloop_set_poll_func(pa_mainloop *m, pa_poll_func poll_func, void *userdata) { 
     186 if ( m == NULL ) 
     187  return; 
    84188 
    85 /** Change the poll() implementation */ 
    86 void pa_mainloop_set_poll_func(pa_mainloop *m, pa_poll_func poll_func, void *userdata); 
     189 m->poll_func     = poll_func; 
     190 m->poll_userdata = userdata; 
     191} 
    87192 
    88193//ll 
Note: See TracChangeset for help on using the changeset viewer.