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 13 : _gpgme_user_io_cb_handler (void *data, int fd)
43 : {
44 13 : gpgme_error_t err = 0;
45 13 : gpgme_error_t op_err = 0;
46 13 : struct tag *tag = (struct tag *) data;
47 : gpgme_ctx_t ctx;
48 :
49 13 : assert (data);
50 13 : ctx = tag->ctx;
51 13 : assert (ctx);
52 :
53 13 : LOCK (ctx->lock);
54 13 : if (ctx->canceled)
55 0 : err = gpg_error (GPG_ERR_CANCELED);
56 13 : UNLOCK (ctx->lock);
57 :
58 13 : if (! err)
59 13 : err = _gpgme_run_io_cb (&ctx->fdt.fds[tag->idx], 0, &op_err);
60 13 : if (err || op_err)
61 0 : _gpgme_cancel_with_err (ctx, err, op_err);
62 : else
63 : {
64 : unsigned int i;
65 :
66 24 : for (i = 0; i < ctx->fdt.size; i++)
67 23 : if (ctx->fdt.fds[i].fd != -1)
68 12 : break;
69 :
70 13 : if (i == ctx->fdt.size)
71 : {
72 : struct gpgme_io_event_done_data done_data;
73 :
74 1 : done_data.err = 0;
75 1 : done_data.op_err = 0;
76 1 : _gpgme_engine_io_event (ctx->engine, GPGME_EVENT_DONE, &done_data);
77 : }
78 : }
79 13 : return 0;
80 : }
81 :
82 :
83 : /* Register the file descriptor FD with the handler FNC (which gets
84 : FNC_DATA as its first argument) for the direction DIR. DATA should
85 : be the context for which the fd is added. R_TAG will hold the tag
86 : that can be used to remove the fd. */
87 : gpgme_error_t
88 3 : _gpgme_wait_user_add_io_cb (void *data, int fd, int dir, gpgme_io_cb_t fnc,
89 : void *fnc_data, void **r_tag)
90 : {
91 3 : gpgme_ctx_t ctx = (gpgme_ctx_t) data;
92 : struct tag *tag;
93 : gpgme_error_t err;
94 :
95 3 : assert (ctx);
96 3 : err = _gpgme_add_io_cb (data, fd, dir, fnc, fnc_data, r_tag);
97 3 : if (err)
98 0 : return err;
99 3 : tag = *r_tag;
100 3 : assert (tag);
101 3 : err = (*ctx->io_cbs.add) (ctx->io_cbs.add_priv, fd, dir,
102 : _gpgme_user_io_cb_handler, *r_tag,
103 : &tag->user_tag);
104 3 : if (err)
105 0 : _gpgme_remove_io_cb (*r_tag);
106 3 : return err;
107 : }
108 :
109 :
110 : void
111 3 : _gpgme_wait_user_remove_io_cb (void *data)
112 : {
113 3 : struct tag *tag = (struct tag *) data;
114 : gpgme_ctx_t ctx;
115 :
116 3 : assert (tag);
117 3 : ctx = tag->ctx;
118 :
119 3 : (*ctx->io_cbs.remove) (tag->user_tag);
120 3 : _gpgme_remove_io_cb (data);
121 3 : }
122 :
123 :
124 : void
125 2 : _gpgme_wait_user_event_cb (void *data, gpgme_event_io_t type, void *type_data)
126 : {
127 2 : gpgme_ctx_t ctx = data;
128 :
129 2 : if (ctx->io_cbs.event)
130 2 : (*ctx->io_cbs.event) (ctx->io_cbs.event_priv, type, type_data);
131 2 : }
|