Line data Source code
1 : /* wait-user.c
2 : Copyright (C) 2000 Werner Koch (dd9jn)
3 : Copyright (C) 2001, 2002, 2003, 2004, 2005 g10 Code GmbH
4 :
5 : This file is part of GPGME.
6 :
7 : GPGME is free software; you can redistribute it and/or modify it
8 : under the terms of the GNU Lesser General Public License as
9 : published by the Free Software Foundation; either version 2.1 of
10 : the License, or (at your option) any later version.
11 :
12 : GPGME is distributed in the hope that it will be useful, but
13 : WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : Lesser General Public License for more details.
16 :
17 : You should have received a copy of the GNU Lesser General Public
18 : License along with this program; if not, write to the Free Software
19 : Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 : 02111-1307, USA. */
21 :
22 : #if HAVE_CONFIG_H
23 : #include <config.h>
24 : #endif
25 : #include <assert.h>
26 :
27 : #include "gpgme.h"
28 : #include "context.h"
29 : #include "priv-io.h"
30 : #include "wait.h"
31 : #include "ops.h"
32 : #include "debug.h"
33 :
34 :
35 : /* The user event loops are used for all asynchronous operations for
36 : which a user callback is defined. */
37 :
38 :
39 : /* Internal I/O Callbacks. */
40 :
41 : gpgme_error_t
42 0 : _gpgme_user_io_cb_handler (void *data, int fd)
43 : {
44 0 : gpgme_error_t err = 0;
45 0 : gpgme_error_t op_err = 0;
46 0 : struct tag *tag = (struct tag *) data;
47 : gpgme_ctx_t ctx;
48 :
49 : (void)fd;
50 :
51 0 : assert (data);
52 0 : ctx = tag->ctx;
53 0 : assert (ctx);
54 :
55 0 : LOCK (ctx->lock);
56 0 : if (ctx->canceled)
57 0 : err = gpg_error (GPG_ERR_CANCELED);
58 0 : UNLOCK (ctx->lock);
59 :
60 0 : if (! err)
61 0 : err = _gpgme_run_io_cb (&ctx->fdt.fds[tag->idx], 0, &op_err);
62 0 : if (err || op_err)
63 0 : _gpgme_cancel_with_err (ctx, err, op_err);
64 : else
65 : {
66 : unsigned int i;
67 :
68 0 : for (i = 0; i < ctx->fdt.size; i++)
69 0 : if (ctx->fdt.fds[i].fd != -1)
70 0 : break;
71 :
72 0 : if (i == ctx->fdt.size)
73 : {
74 : struct gpgme_io_event_done_data done_data;
75 :
76 0 : done_data.err = 0;
77 0 : done_data.op_err = 0;
78 0 : _gpgme_engine_io_event (ctx->engine, GPGME_EVENT_DONE, &done_data);
79 : }
80 : }
81 0 : return 0;
82 : }
83 :
84 :
85 : /* Register the file descriptor FD with the handler FNC (which gets
86 : FNC_DATA as its first argument) for the direction DIR. DATA should
87 : be the context for which the fd is added. R_TAG will hold the tag
88 : that can be used to remove the fd. */
89 : gpgme_error_t
90 0 : _gpgme_wait_user_add_io_cb (void *data, int fd, int dir, gpgme_io_cb_t fnc,
91 : void *fnc_data, void **r_tag)
92 : {
93 0 : gpgme_ctx_t ctx = (gpgme_ctx_t) data;
94 : struct tag *tag;
95 : gpgme_error_t err;
96 :
97 0 : assert (ctx);
98 0 : err = _gpgme_add_io_cb (data, fd, dir, fnc, fnc_data, r_tag);
99 0 : if (err)
100 0 : return err;
101 0 : tag = *r_tag;
102 0 : assert (tag);
103 0 : err = (*ctx->io_cbs.add) (ctx->io_cbs.add_priv, fd, dir,
104 : _gpgme_user_io_cb_handler, *r_tag,
105 : &tag->user_tag);
106 0 : if (err)
107 0 : _gpgme_remove_io_cb (*r_tag);
108 0 : return err;
109 : }
110 :
111 :
112 : void
113 0 : _gpgme_wait_user_remove_io_cb (void *data)
114 : {
115 0 : struct tag *tag = (struct tag *) data;
116 : gpgme_ctx_t ctx;
117 :
118 0 : assert (tag);
119 0 : ctx = tag->ctx;
120 :
121 0 : (*ctx->io_cbs.remove) (tag->user_tag);
122 0 : _gpgme_remove_io_cb (data);
123 0 : }
124 :
125 :
126 : void
127 0 : _gpgme_wait_user_event_cb (void *data, gpgme_event_io_t type, void *type_data)
128 : {
129 0 : gpgme_ctx_t ctx = data;
130 :
131 0 : if (ctx->io_cbs.event)
132 0 : (*ctx->io_cbs.event) (ctx->io_cbs.event_priv, type, type_data);
133 0 : }
|