Changeset 3468:c2bff8bedf7c in roaraudio for libroarpulse/mainloop.c


Ignore:
Timestamp:
02/14/10 00:40:31 (14 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

wrote io event API

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libroarpulse/mainloop.c

    r3463 r3468  
    3939#include <libroarpulse/libroarpulse.h> 
    4040 
     41#define MAX_IO_EVENTS    8 
     42 
     43struct pa_io_event { 
     44 int used; 
     45 pa_mainloop_api *api; 
     46 int fd; 
     47 pa_io_event_flags_t events; 
     48 pa_io_event_cb_t cb; 
     49 void *userdata; 
     50 pa_io_event_destroy_cb_t destroy; 
     51}; 
     52 
    4153struct pa_mainloop { 
    4254 pa_mainloop_api api; 
     
    4557 int             quit; 
    4658 int             quitval; 
     59 pa_io_event     io_event[MAX_IO_EVENTS]; 
    4760}; 
    4861 
    49 struct pa_io_event { 
    50  pa_mainloop_api *api; 
    51  int fd; 
    52  pa_io_event_flags_t events; 
    53  pa_io_event_cb_t cb; 
    54  void *userdata; 
    55 }; 
     62// IO EVENTS: 
     63static void _roar_pa_io_enable(pa_io_event* e, pa_io_event_flags_t events); 
     64 
     65/** Create a new IO event source object */ 
     66static pa_io_event* _roar_pa_io_new(pa_mainloop_api*a, int fd, 
     67                                    pa_io_event_flags_t events, pa_io_event_cb_t cb, void *userdata) { 
     68 pa_mainloop * mainloop = a->userdata; 
     69 pa_io_event * ret = NULL; 
     70 int i; 
     71 
     72 for (i = 0; i < MAX_IO_EVENTS; i++) { 
     73  if ( mainloop->io_event[i].used == 0 ) { 
     74   ret = &(mainloop->io_event[i]); 
     75  } 
     76 } 
     77 
     78 if ( ret == NULL ) 
     79  return NULL; 
     80 
     81 ret->used = 1; 
     82 
     83 ret->api      = a; 
     84 ret->fd       = fd; 
     85 ret->cb       = cb; 
     86 ret->userdata = userdata; 
     87 
     88 // Callbacks: 
     89 ret->destroy  = NULL; 
     90 
     91 _roar_pa_io_enable(ret, events); 
     92 
     93 return ret; 
     94} 
     95/** Enable or disable IO events on this object */  
     96static void _roar_pa_io_enable(pa_io_event* e, pa_io_event_flags_t events) { 
     97 if ( e == NULL ) 
     98  return; 
     99 
     100 e->events = events; 
     101} 
     102 
     103/** Free a IO event source object */ 
     104static void _roar_pa_io_free(pa_io_event* e) { 
     105 if ( e == NULL ) 
     106  return; 
     107 
     108 if ( e->destroy != NULL ) 
     109  e->destroy(e->api, e, e->userdata); 
     110 
     111 e->used = 0; 
     112} 
     113/** Set a function that is called when the IO event source is destroyed. Use this to free the userdata argument if required */ 
     114static void _roar_pa_io_set_destroy(pa_io_event *e, pa_io_event_destroy_cb_t cb) { 
     115 if ( e == NULL ) 
     116  return; 
     117 
     118 e->destroy = cb; 
     119} 
     120 
     121 
     122 
     123// API: 
    56124 
    57125/** Allocate a new main loop object */ 
     
    64132 memset(m, 0, sizeof(pa_mainloop)); 
    65133 
    66  m->api.userdata = m; 
     134 m->api.userdata       = m; 
     135 m->api.io_new         = _roar_pa_io_new; 
     136 m->api.io_enable      = _roar_pa_io_enable; 
     137 m->api.io_free        = _roar_pa_io_free; 
     138 m->api.io_set_destroy = _roar_pa_io_set_destroy; 
    67139 
    68140 return m; 
Note: See TracChangeset for help on using the changeset viewer.