loudmouth/src/lm-misc.c
changeset 10 59927b2d3b75
parent 0 d0f3a028347a
equal deleted inserted replaced
0:d0f3a028347a 10:59927b2d3b75
     1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
       
     2 /*
       
     3  * Copyright (C) 2006 Imendio AB
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or
       
     6  * modify it under the terms of the GNU Lesser General Public License as
       
     7  * published by the Free Software Foundation; either version 2 of the
       
     8  * License, or (at your option) any later version.
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    13  * Lesser General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU Lesser General Public
       
    16  * License along with this program; if not, write to the
       
    17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    18  * Boston, MA 02111-1307, USA.
       
    19  */
       
    20 
       
    21 #include <config.h>
       
    22 
       
    23 #include <string.h>
       
    24 
       
    25 #include "lm-misc.h"
       
    26 
       
    27 #ifdef EMULATOR
       
    28 #include "libloudmouth_wsd_solution.h"
       
    29 GET_STATIC_ARRAY_FROM_TLS(buf, lm_misc, gchar)
       
    30   #define buf (GET_WSD_VAR_NAME(buf, lm_misc, s)())
       
    31   
       
    32 
       
    33 #endif
       
    34 
       
    35 static void
       
    36 misc_setup_source (GMainContext *context,
       
    37 		   GSource      *source,
       
    38 		   GSourceFunc   function,
       
    39 		   gpointer      data)
       
    40 {
       
    41 	g_source_set_callback (source, (GSourceFunc)function, data, NULL);
       
    42 	g_source_attach (source, context);
       
    43 	g_source_unref (source);
       
    44 }
       
    45 
       
    46 GSource *
       
    47 lm_misc_add_io_watch (GMainContext *context,
       
    48 		      GIOChannel   *channel,
       
    49 		      GIOCondition  condition,
       
    50 		      GIOFunc       function,
       
    51 		      gpointer      data)
       
    52 {
       
    53 	GSource *source;
       
    54                                                                                 
       
    55 	g_return_val_if_fail (channel != NULL, 0);
       
    56                                                                                 
       
    57 	source = g_io_create_watch (channel, condition);
       
    58 	misc_setup_source (context, source, (GSourceFunc) function, data);
       
    59 
       
    60 	return source;
       
    61 }
       
    62 
       
    63 GSource *
       
    64 lm_misc_add_idle (GMainContext *context,
       
    65 		  GSourceFunc   function,
       
    66 		  gpointer      data)
       
    67 {
       
    68 	GSource *source;
       
    69                                                                                 
       
    70 	g_return_val_if_fail (function != NULL, 0);
       
    71                                                                                 
       
    72 	source = g_idle_source_new ();
       
    73 	misc_setup_source (context, source, function, data);
       
    74   
       
    75 	return source;
       
    76 }
       
    77 
       
    78 GSource *
       
    79 lm_misc_add_timeout (GMainContext *context,
       
    80 		     guint         interval,
       
    81 		     GSourceFunc   function,
       
    82 		     gpointer      data)
       
    83 {
       
    84 	GSource *source;
       
    85                                                                                 
       
    86 	g_return_val_if_fail (function != NULL, 0);
       
    87                                                                                 
       
    88 	source = g_timeout_source_new (interval);
       
    89 	misc_setup_source (context, source, function, data);
       
    90   
       
    91 	return source;
       
    92 }
       
    93 
       
    94 const char *
       
    95 lm_misc_io_condition_to_str  (GIOCondition condition)
       
    96 {
       
    97 #ifndef EMULATOR
       
    98 	static char buf[256];
       
    99 #endif
       
   100 
       
   101 	buf[0] = '\0';
       
   102 
       
   103 	if(condition & G_IO_ERR)
       
   104 		strcat(buf, "G_IO_ERR ");
       
   105 	if(condition & G_IO_HUP)
       
   106 		strcat(buf, "G_IO_HUP ");
       
   107 	if(condition & G_IO_NVAL)
       
   108 		strcat(buf, "G_IO_NVAL ");
       
   109 	if(condition & G_IO_IN)
       
   110 		strcat(buf, "G_IO_IN ");
       
   111 	if(condition & G_IO_OUT)
       
   112 		strcat(buf, "G_IO_OUT ");
       
   113 
       
   114 	return buf;
       
   115 }
       
   116