Changeset 4048
- Timestamp:
- Nov 26, 2009, 6:20:33 PM (13 years ago)
- Location:
- neercs/trunk
- Files:
-
- 2 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
neercs/trunk/XCode/XCode.xcodeproj/project.pbxproj
r4047 r4048 19 19 E6BC2F7E10BD8C9100CED875 /* py_module.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = py_module.c; path = ../src/python/py_module.c; sourceTree = SOURCE_ROOT; }; 20 20 E6BC2F7F10BD8C9100CED875 /* py_module.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = py_module.h; path = ../src/python/py_module.h; sourceTree = SOURCE_ROOT; }; 21 E6BC2FBF10BEE46A00CED875 /* widgets.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = widgets.h; path = ../src/widgets.h; sourceTree = SOURCE_ROOT; }; 22 E6BC2FC010BEE46A00CED875 /* widgets.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = widgets.c; path = ../src/widgets.c; sourceTree = SOURCE_ROOT; }; 21 23 E6DB671810B19E2200B6F924 /* attach.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = attach.c; path = ../src/attach.c; sourceTree = SOURCE_ROOT; }; 22 24 E6DB671910B19E2200B6F924 /* configuration.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = configuration.c; path = ../src/configuration.c; sourceTree = SOURCE_ROOT; }; … … 94 96 E6DB672510B19E2200B6F924 /* neercsrc */, 95 97 E6DB671F10B19E2200B6F924 /* Makefile.am */, 98 E6BC2FBF10BEE46A00CED875 /* widgets.h */, 99 E6BC2FC010BEE46A00CED875 /* widgets.c */, 96 100 ); 97 101 name = src; -
neercs/trunk/src/Makefile.am
r4040 r4048 5 5 term.c grab.c effects.c wm.c screensaver.c attach.c \ 6 6 configuration.c mytrace.c mytrace.h input.c lock.c server.c \ 7 client.c screen_list.c help.c python/interpreter.c python/py_module.c 7 client.c screen_list.c help.c python/interpreter.c python/py_module.c \ 8 widgets.c 8 9 neercs_CFLAGS = @CACA_CFLAGS@ @PYTHON3_CFLAGS@ 9 10 neercs_LDADD = @CACA_LIBS@ @UTIL_LIBS@ @PAM_LIBS@ @PYTHON3_LIBS@ -
neercs/trunk/src/input.c
r4036 r4048 182 182 case 'e': 183 183 case 0x05: 184 185 184 screen_list->python_command = 1; 186 screen_list->interpreter_props.x = 0;187 screen_list->interpreter_props.y = 0;188 185 break; 189 186 #endif -
neercs/trunk/src/neercs.h
r4046 r4048 19 19 #include <stdint.h> 20 20 #include <caca.h> 21 22 #include "widgets.h" 21 23 22 24 enum wm_types … … 149 151 { 150 152 /* Input box */ 151 int x, y; 152 int size; 153 char *command; 154 char *output_err; 155 char *output_res; 153 struct input_box *box; 156 154 }; 157 155 -
neercs/trunk/src/python/interpreter.c
r4046 r4048 38 38 #include "py_module.h" 39 39 40 static void add_char(struct screen_list *sl, unsigned int c);41 static void del_char(struct screen_list *sl);42 40 static int python_execute(struct screen_list *sl); 43 41 char *getStringFromPyObject(PyObject * p); 44 42 static char *getPythonError(void); 45 43 46 static void add_char(struct screen_list *sl, unsigned int c)47 {48 /* FIXME handle return values */49 if (!sl->interpreter_props.command)50 {51 sl->interpreter_props.size = 1;52 sl->interpreter_props.x = 0;53 sl->interpreter_props.command = (char *)malloc(2);54 sl->interpreter_props.command[0] = 0;55 }56 else57 {58 sl->interpreter_props.command =59 (char *)realloc(sl->interpreter_props.command,60 sl->interpreter_props.size + 1);61 }62 memmove(&sl->interpreter_props.command[sl->interpreter_props.x + 1],63 &sl->interpreter_props.command[sl->interpreter_props.x],64 (sl->interpreter_props.size - sl->interpreter_props.x));65 66 sl->interpreter_props.command[sl->interpreter_props.x] = c;67 sl->interpreter_props.x++;68 sl->interpreter_props.size++;69 }70 71 static void del_char(struct screen_list *sl)72 {73 if (sl->interpreter_props.x < 1)74 return;75 if (sl->interpreter_props.size > 1)76 sl->interpreter_props.size--;77 else78 return;79 80 memcpy(&sl->interpreter_props.command[sl->interpreter_props.x - 1],81 &sl->interpreter_props.command[sl->interpreter_props.x],82 sl->interpreter_props.size - sl->interpreter_props.x);83 84 sl->interpreter_props.command =85 (char *)realloc(sl->interpreter_props.command,86 sl->interpreter_props.size);87 88 if (sl->interpreter_props.x)89 sl->interpreter_props.x--;90 sl->interpreter_props.command[sl->interpreter_props.size - 1] = 0;91 }92 44 93 45 94 46 int python_command_handle_key(struct screen_list *screen_list, unsigned int c) 95 47 { 96 if (c == CACA_KEY_ESCAPE) 48 49 int ret = widget_ibox_handle_key(screen_list->interpreter_props.box, c); 50 51 if (ret == INPUT_BOX_ESC) 97 52 { 98 if (screen_list->interpreter_props.command) 99 { 100 free(screen_list->interpreter_props.command); 101 screen_list->interpreter_props.command = NULL; 102 } 53 widget_ibox_destroy(screen_list->interpreter_props.box); 54 screen_list->interpreter_props.box = NULL; 103 55 screen_list->python_command = 0; 104 screen_list->changed = 1;105 return 1;106 56 } 107 else if (c == CACA_KEY_LEFT) 108 { 109 if (screen_list->interpreter_props.x) 110 screen_list->interpreter_props.x--; 111 } 112 else if (c == CACA_KEY_RIGHT) 113 { 114 if (screen_list->interpreter_props.x < 115 screen_list->interpreter_props.size - 1) 116 screen_list->interpreter_props.x++; 117 } 118 else if (c == CACA_KEY_RETURN) 57 else if (ret == INPUT_BOX_RET) 119 58 { 120 59 python_execute(screen_list); 121 60 } 122 else 123 { 124 if (c >= ' ' && c < 127) 125 add_char(screen_list, c); 126 else if (c == 8) 127 { 128 del_char(screen_list); 129 } 130 screen_list->changed = 1; 131 return 0; 132 } 61 screen_list->changed = 1; 62 133 63 return 0; 134 64 } … … 136 66 void draw_python_command(struct screen_list *screen_list) 137 67 { 138 i nt w = 65, h = 6;139 int x = (caca_get_canvas_width(screen_list->cv) - w) / 2;140 int y = (caca_get_canvas_height(screen_list->cv) - h) / 2;68 if (!screen_list->interpreter_props.box) 69 screen_list->interpreter_props.box = 70 widget_ibox_init(screen_list->cv, 65, 6); 141 71 142 caca_set_color_ansi(screen_list->cv, CACA_BLUE, CACA_BLUE); 143 caca_fill_box(screen_list->cv, x, y, w, h, '#'); 144 caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_BLUE); 145 caca_draw_cp437_box(screen_list->cv, x, y, w, h); 146 caca_printf(screen_list->cv, x, y, "Execute command"); 147 148 caca_printf(screen_list->cv, x + 2, y + 2, 149 "[___________________________________________________________]"); 150 151 if (screen_list->interpreter_props.command) 152 { 153 caca_printf(screen_list->cv, x + 3, y + 2, 154 "%s", screen_list->interpreter_props.command); 155 caca_gotoxy(screen_list->cv, 156 x + 3 + screen_list->interpreter_props.x, y + 2); 157 } 158 else 159 { 160 caca_gotoxy(screen_list->cv, x + 3, y + 2); 161 } 162 163 if (screen_list->interpreter_props.output_err) 164 { 165 caca_set_color_ansi(screen_list->cv, CACA_RED, CACA_BLUE); 166 caca_printf(screen_list->cv, x + 2, y + 4, 167 screen_list->interpreter_props.output_err); 168 } 169 if (screen_list->interpreter_props.output_res) 170 { 171 caca_set_color_ansi(screen_list->cv, CACA_LIGHTGREEN, CACA_BLUE); 172 caca_printf(screen_list->cv, x + 2, y + 4, 173 screen_list->interpreter_props.output_res); 174 } 72 widget_ibox_draw(screen_list->interpreter_props.box); 175 73 } 176 74 … … 178 76 int python_init(struct screen_list *sl) 179 77 { 180 sl->interpreter_props.output_err = NULL;181 sl->interpreter_props.output_res = NULL;182 78 Py_Initialize(); 183 184 79 initNeercsModule(sl); 185 80 186 81 return 0; 187 82 } … … 189 84 int python_close(struct screen_list *sl) 190 85 { 191 if (sl->interpreter_props.output_err) 192 free(sl->interpreter_props.output_err); 193 if (sl->interpreter_props.output_res) 194 free(sl->interpreter_props.output_res); 195 sl->interpreter_props.output_err = NULL; 196 sl->interpreter_props.output_res = NULL; 86 widget_ibox_destroy(sl->interpreter_props.box); 87 sl->interpreter_props.box = NULL; 88 197 89 Py_Finalize(); 198 90 return 0; … … 202 94 static int python_execute(struct screen_list *sl) 203 95 { 204 if (!sl->interpreter_props.command || sl->interpreter_props.size < 1) 96 char *command = widget_ibox_get_text(sl->interpreter_props.box); 97 if (!command || strlen(command) < 1) 205 98 return -1; 206 99 int err = 0; 207 100 208 debug("py Executing '%s'\n", sl->interpreter_props.command);101 debug("py Executing '%s'\n", command); 209 102 210 if (sl->interpreter_props.output_err) 211 { 212 free(sl->interpreter_props.output_err); 213 sl->interpreter_props.output_err = NULL; 214 } 215 if (sl->interpreter_props.output_res) 216 { 217 free(sl->interpreter_props.output_res); 218 sl->interpreter_props.output_res = NULL; 219 } 220 103 221 104 PyObject *pModule, *pName, *pFunc; 222 105 … … 225 108 if (!pName) 226 109 { 227 sl->interpreter_props.output_err = getPythonError();110 widget_ibox_set_error(sl->interpreter_props.box, getPythonError()); 228 111 err = 1; 229 112 debug("py Error 1\n"); 230 113 goto end; 231 114 } 232 115 233 116 pModule = PyImport_Import(pName); 234 117 Py_DECREF(pName); … … 236 119 if (pModule != NULL) 237 120 { 238 PyObject * 239 121 PyObject *dictionary = PyModule_GetDict(pModule); 122 240 123 getExportedValues(dictionary); 241 242 PyObject *o = PyRun_String(sl->interpreter_props.command, Py_single_input, dictionary, NULL); 243 if(!o) 124 125 PyObject *o = 126 PyRun_String(command, Py_single_input, 127 dictionary, NULL); 128 if (!o) 244 129 { 245 sl->interpreter_props.output_err = getPythonError();130 widget_ibox_set_error(sl->interpreter_props.box, getPythonError()); 246 131 err = 1; 247 132 } … … 249 134 { 250 135 setExportedValues(dictionary); 251 252 sl->interpreter_props.output_res = getStringFromPyObject(o);136 137 widget_ibox_set_msg(sl->interpreter_props.box, getStringFromPyObject(o)); 253 138 err = 1; 254 139 } 255 256 140 goto end; 141 257 142 } 258 143 else 259 144 { 260 sl->interpreter_props.output_err = getPythonError();145 widget_ibox_set_error(sl->interpreter_props.box, getPythonError()); 261 146 err = 1; 262 debug("py: %s\n", sl->interpreter_props.output_err);263 147 goto end; 264 148 } 265 149 266 150 end: 267 151 268 152 Py_XDECREF(pFunc); 269 153 Py_DECREF(pModule); 270 154 271 155 if (!err) 272 156 { 273 free(sl->interpreter_props.command); 274 sl->interpreter_props.command = NULL; 275 sl->interpreter_props.size = 0; 276 sl->interpreter_props.x = 0; 157 widget_ibox_destroy(sl->interpreter_props.box); 277 158 sl->python_command = 0; 278 159 } … … 293 174 294 175 int r = asprintf(&err, "%s", evalue); 295 176 (void)r; 296 177 return err; 297 178 } 298 179 299 180 char *getStringFromPyObject(PyObject * p) 300 181 { 301 182 PyObject *str = PyObject_Repr(p); 302 183 char *err = PyBytes_AS_STRING(PyUnicode_AsEncodedString(str, "utf-8", 303 184 "Error ~")); 304 185 305 186 char *ret = strdup(err); 306 187 return ret; -
neercs/trunk/src/screen_list.c
r4033 r4048 98 98 99 99 screen_list->python_command = 0; 100 screen_list->interpreter_props.x = 0; 101 screen_list->interpreter_props.y = 0; 102 screen_list->interpreter_props.command = NULL; 103 screen_list->interpreter_props.size = 0; 100 screen_list->interpreter_props.box = NULL; 104 101 105 102 screen_list->recurrent_list = NULL;
Note: See TracChangeset
for help on using the changeset viewer.