gam-resource-manager: adjust to updated proxied call callback signature.
[profile/ivi/murphy.git] / src / breedline / breedline-glib.c
1 /*
2  * Copyright (c) 2012, Intel Corporation
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *   * Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *   * Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *   * Neither the name of Intel Corporation nor the names of its contributors
14  *     may be used to endorse or promote products derived from this software
15  *     without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include <poll.h>
34 #include <sys/poll.h>
35 #include <glib.h>
36
37 #include <breedline/breedline.h>
38
39 typedef struct {
40     GIOChannel *ioc;
41     guint       iow;
42     void      (*cb)(int fd, int events, void *user_data);
43     void       *user_data;
44 } watch_t;
45
46
47 static gboolean io_cb(GIOChannel *ioc, GIOCondition events, void *user_data)
48 {
49     watch_t *w  = (watch_t *)user_data;
50     int      e  = 0;
51     int      fd = g_io_channel_unix_get_fd(ioc);
52
53     if (events & G_IO_IN)
54         e |= POLLIN;
55     if (events & G_IO_HUP)
56         e |= POLLHUP;
57
58     w->cb(fd, e, w->user_data);
59
60     return TRUE;
61 }
62
63
64 static void *add_watch(void *mlp, int fd,
65                        void (*cb)(int fd, int events, void *user_data),
66                        void *user_data)
67 {
68     GIOCondition  events = G_IO_IN | G_IO_HUP;
69     watch_t      *w;
70
71     (void)mlp;
72
73     w = malloc(sizeof(*w));
74
75     if (w != NULL) {
76         memset(w, 0, sizeof(*w));
77         w->cb        = cb;
78         w->user_data = user_data;
79         w->ioc       = g_io_channel_unix_new(fd);
80
81         if (w->ioc != NULL) {
82             w->iow = g_io_add_watch(w->ioc, events, io_cb, w);
83
84             if (w->iow != 0)
85                 return w;
86
87             g_io_channel_unref(w->ioc);
88         }
89
90         free(w);
91     }
92
93     return NULL;
94 }
95
96
97 static void del_watch(void *wp)
98 {
99     watch_t *w = (watch_t *)wp;
100
101     if (w != NULL) {
102         g_source_remove(w->iow);
103         g_io_channel_unref(w->ioc);
104
105         free(w);
106     }
107 }
108
109
110 static brl_mainloop_ops_t ml_ops = {
111     .add_watch = add_watch,
112     .del_watch = del_watch
113 };
114
115
116 brl_t *brl_create_with_glib(int fd, const char *prompt, GMainLoop *ml,
117                               brl_line_cb_t cb, void *user_data)
118 {
119     brl_t *brl;
120
121     brl = brl_create(fd, prompt);
122
123     if (brl != NULL) {
124         if (brl_use_mainloop(brl, ml, &ml_ops, cb, user_data) == 0)
125             return brl;
126         else
127             brl_destroy(brl);
128     }
129
130     return NULL;
131 }