Imported Upstream version 3.7.3
[platform/upstream/python-gobject.git] / gi / _glib / pygsource.c
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2  * pygtk- Python bindings for the GTK toolkit.
3  * Copyright (C) 1998-2003  James Henstridge
4  * Copyright (C) 2005       Oracle
5  *
6  * Author: Manish Singh <manish.singh@oracle.com>
7  *
8  *   pygsource.c: GSource wrapper
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
23  * USA
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #  include <config.h>
28 #endif
29
30 #include <Python.h>
31 #include <structmember.h> /* for PyMemberDef */
32 #include "pyglib.h"
33 #include "pyglib-private.h"
34 #include "pygsource.h"
35
36 /* glib.PollFD */
37
38 PYGLIB_DEFINE_TYPE("gi._glib.PollFD", PyGPollFD_Type, PyGPollFD)
39
40 static PyMemberDef pyg_poll_fd_members[] = {
41     { "fd",      T_INT,    offsetof(PyGPollFD, pollfd.fd),      READONLY },
42     { "events",  T_USHORT, offsetof(PyGPollFD, pollfd.events),  READONLY },
43     { "revents", T_USHORT, offsetof(PyGPollFD, pollfd.revents), READONLY },
44     { NULL, 0, 0, 0 }
45 };
46
47 static void
48 pyg_poll_fd_dealloc(PyGPollFD *self)
49 {
50     Py_XDECREF(self->fd_obj);
51     PyObject_DEL(self);
52 }
53
54 static PyObject *
55 pyg_poll_fd_repr(PyGPollFD *self)
56 {
57     return PYGLIB_PyUnicode_FromFormat("<GPollFD %d (%d) at 0x%lx>",
58                                  self->pollfd.fd, self->pollfd.events,
59                                  (long)self);
60 }
61
62 static int
63 pyg_poll_fd_init(PyGPollFD *self, PyObject *args, PyObject *kwargs)
64 {
65     static char *kwlist[] = { "fd", "events", NULL };
66     PyObject *o;
67     gint fd;
68     gushort events;
69
70     if (!PyArg_ParseTupleAndKeywords(args, kwargs,
71                                      "OH:gi._glib.PollFD.__init__", kwlist,
72                                      &o, &events))
73         return -1;
74
75     fd = PyObject_AsFileDescriptor(o);
76     if (fd == -1)
77         return -1;
78
79     self->pollfd.fd = fd;
80     self->pollfd.events = events;
81     self->pollfd.revents = 0;
82
83     Py_INCREF(o);
84     self->fd_obj = o;
85
86     return 0;
87 }
88
89 void
90 pyglib_source_register_types(PyObject *d)
91 {
92     PyGPollFD_Type.tp_dealloc = (destructor)pyg_poll_fd_dealloc;
93     PyGPollFD_Type.tp_repr = (reprfunc)pyg_poll_fd_repr;
94     PyGPollFD_Type.tp_flags = Py_TPFLAGS_DEFAULT;
95     PyGPollFD_Type.tp_members = pyg_poll_fd_members;
96     PyGPollFD_Type.tp_init = (initproc)pyg_poll_fd_init;
97     PYGLIB_REGISTER_TYPE(d, PyGPollFD_Type, "PollFD");
98 }