| 1 | /* |
|---|
| 2 | * neercs console-based window manager |
|---|
| 3 | * Copyright (c) 2009 Jean-Yves Lamoureux <jylam@lnxscene.org> |
|---|
| 4 | * All Rights Reserved |
|---|
| 5 | * |
|---|
| 6 | * $Id: py_module.c 3996 2009-11-22 12:41:45Z jylam $ |
|---|
| 7 | * |
|---|
| 8 | * This program is free software. It comes without any warranty, to |
|---|
| 9 | * the extent permitted by applicable law. You can redistribute it |
|---|
| 10 | * and/or modify it under the terms of the Do What The Fuck You Want |
|---|
| 11 | * To Public License, Version 2, as published by Sam Hocevar. See |
|---|
| 12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
|---|
| 13 | */ |
|---|
| 14 | |
|---|
| 15 | #include "config.h" |
|---|
| 16 | |
|---|
| 17 | #ifdef USE_PYTHON |
|---|
| 18 | |
|---|
| 19 | #include <Python.h> |
|---|
| 20 | #include "py_module.h" |
|---|
| 21 | |
|---|
| 22 | #include <stdio.h> |
|---|
| 23 | #include <stdlib.h> |
|---|
| 24 | #include <string.h> |
|---|
| 25 | #include <unistd.h> |
|---|
| 26 | |
|---|
| 27 | #include <fcntl.h> |
|---|
| 28 | #include <signal.h> |
|---|
| 29 | #include <sys/ioctl.h> |
|---|
| 30 | #include <sys/socket.h> |
|---|
| 31 | #include <sys/time.h> |
|---|
| 32 | #include <time.h> |
|---|
| 33 | #include <sys/wait.h> |
|---|
| 34 | #include <sys/types.h> |
|---|
| 35 | #include <caca.h> |
|---|
| 36 | |
|---|
| 37 | #include "neercs.h" |
|---|
| 38 | |
|---|
| 39 | /* FIXME : Find a way to pass a user pointer to PyModuleDef or something */ |
|---|
| 40 | static struct screen_list *screen_list; |
|---|
| 41 | |
|---|
| 42 | PyObject *PyInit_neercs(void); |
|---|
| 43 | void removeTrailingStuff(char *b); |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | static void addVariableFromConfig(PyObject * dictionary, |
|---|
| 47 | const char *varname, const char *configname) |
|---|
| 48 | { |
|---|
| 49 | char *v = get_config(configname)->get(screen_list); |
|---|
| 50 | if (v != NULL) |
|---|
| 51 | { |
|---|
| 52 | PyObject *value = Py_BuildValue("s", v); |
|---|
| 53 | PyDict_SetItemString(dictionary, varname, value); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | debug("py get '%s' to '%s'\n", varname, |
|---|
| 57 | get_config(configname)->get(screen_list)); |
|---|
| 58 | |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | void removeTrailingStuff(char *b) |
|---|
| 62 | { |
|---|
| 63 | if(!b) return; |
|---|
| 64 | if(b[0]=='\'') |
|---|
| 65 | { |
|---|
| 66 | memmove(b, &b[1], strlen(b)-1); |
|---|
| 67 | b[strlen(b)-2] = 0; |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | void setExportedValues(PyObject * dictionary) |
|---|
| 72 | { |
|---|
| 73 | struct config_line *config_option = get_config_option(); |
|---|
| 74 | int i = 0; |
|---|
| 75 | |
|---|
| 76 | while (strncmp(config_option[i].name, "last", strlen("last"))) |
|---|
| 77 | { |
|---|
| 78 | /* Get variable */ |
|---|
| 79 | PyObject *res = |
|---|
| 80 | PyDict_GetItemString(dictionary, config_option[i].name); |
|---|
| 81 | |
|---|
| 82 | /* Got it */ |
|---|
| 83 | if (res) |
|---|
| 84 | { |
|---|
| 85 | |
|---|
| 86 | /* Get object representation |
|---|
| 87 | * FIXME : find a way to check object's type */ |
|---|
| 88 | PyObject *str = PyObject_Repr(res); |
|---|
| 89 | |
|---|
| 90 | /* Make sure it's a string */ |
|---|
| 91 | char *err = |
|---|
| 92 | PyBytes_AS_STRING(PyUnicode_AsEncodedString |
|---|
| 93 | (str, "utf-8", "Error ~")); |
|---|
| 94 | /* FIXME leak leak leak */ |
|---|
| 95 | char *s = strdup(err); |
|---|
| 96 | |
|---|
| 97 | if (s != NULL) |
|---|
| 98 | { |
|---|
| 99 | /* Representation can include '' around strings */ |
|---|
| 100 | removeTrailingStuff(s); |
|---|
| 101 | get_config(config_option[i].name)->set(s, screen_list); |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | i++; |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | void getExportedValues(PyObject * dictionary) |
|---|
| 109 | { |
|---|
| 110 | struct config_line *config_option = get_config_option(); |
|---|
| 111 | int i = 0; |
|---|
| 112 | while (strncmp(config_option[i].name, "last", strlen("last"))) |
|---|
| 113 | { |
|---|
| 114 | addVariableFromConfig(dictionary, config_option[i].name, |
|---|
| 115 | config_option[i].name); |
|---|
| 116 | i++; |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | static PyObject *neercs_get(PyObject * self, PyObject * args) |
|---|
| 122 | { |
|---|
| 123 | char *s = NULL; |
|---|
| 124 | |
|---|
| 125 | debug("Get using list at %p", screen_list); |
|---|
| 126 | |
|---|
| 127 | if (!PyArg_ParseTuple(args, "s", &s)) |
|---|
| 128 | { |
|---|
| 129 | PyErr_SetString(PyExc_ValueError, "Can't parse argument"); |
|---|
| 130 | debug("py Can't parse"); |
|---|
| 131 | return NULL; |
|---|
| 132 | } |
|---|
| 133 | debug("py Argument : '%s'", s); |
|---|
| 134 | struct config_line *c = get_config(s); |
|---|
| 135 | |
|---|
| 136 | if (c) |
|---|
| 137 | return Py_BuildValue("s", c->get(screen_list)); |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | PyErr_SetString(PyExc_ValueError, |
|---|
| 141 | "Can't get value for specified variable"); |
|---|
| 142 | return NULL; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | static PyObject *neercs_version(PyObject * self, PyObject * args) |
|---|
| 146 | { |
|---|
| 147 | return Py_BuildValue("s", PACKAGE_VERSION); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | static PyMethodDef NeercsMethods[] = { |
|---|
| 151 | {"version", neercs_version, METH_NOARGS, "Return the neercs version."}, |
|---|
| 152 | {"get", neercs_get, METH_VARARGS, |
|---|
| 153 | "Return the specified variable's value."}, |
|---|
| 154 | {NULL, NULL, 0, NULL} |
|---|
| 155 | }; |
|---|
| 156 | |
|---|
| 157 | static PyModuleDef NeercsModule = { |
|---|
| 158 | PyModuleDef_HEAD_INIT, "neercs", NULL, -1, NeercsMethods, |
|---|
| 159 | NULL, NULL, NULL, NULL |
|---|
| 160 | }; |
|---|
| 161 | |
|---|
| 162 | PyObject *PyInit_neercs(void) |
|---|
| 163 | { |
|---|
| 164 | PyObject *o = PyModule_Create(&NeercsModule); |
|---|
| 165 | PyImport_AppendInittab("neercs", &PyInit_neercs); |
|---|
| 166 | return o; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | void initNeercsModule(struct screen_list *sl) |
|---|
| 170 | { |
|---|
| 171 | screen_list = sl; |
|---|
| 172 | PyInit_neercs(); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | #endif |
|---|