| 1 | /* |
|---|
| 2 | * neercs console-based window manager |
|---|
| 3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
|---|
| 4 | * 2008 Jean-Yves Lamoureux <jylam@lnxscene.org> |
|---|
| 5 | * All Rights Reserved |
|---|
| 6 | * |
|---|
| 7 | * $Id: help.c 3996 2009-11-22 12:41:45Z jylam $ |
|---|
| 8 | * |
|---|
| 9 | * This program is free software. It comes without any warranty, to |
|---|
| 10 | * the extent permitted by applicable law. You can redistribute it |
|---|
| 11 | * and/or modify it under the terms of the Do What The Fuck You Want |
|---|
| 12 | * To Public License, Version 2, as published by Sam Hocevar. See |
|---|
| 13 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | #include "config.h" |
|---|
| 17 | |
|---|
| 18 | #ifdef USE_PYTHON |
|---|
| 19 | |
|---|
| 20 | #include <Python.h> |
|---|
| 21 | |
|---|
| 22 | #include <stdio.h> |
|---|
| 23 | #include <stdlib.h> |
|---|
| 24 | #include <string.h> |
|---|
| 25 | #include <caca.h> |
|---|
| 26 | #include <caca.h> |
|---|
| 27 | #include <time.h> |
|---|
| 28 | #include <sys/wait.h> |
|---|
| 29 | #include <sys/types.h> |
|---|
| 30 | |
|---|
| 31 | #include "neercs.h" |
|---|
| 32 | |
|---|
| 33 | void add_char(struct screen_list *sl, unsigned int c); |
|---|
| 34 | void del_char(struct screen_list *sl); |
|---|
| 35 | void python_execute(struct screen_list *sl); |
|---|
| 36 | |
|---|
| 37 | void add_char(struct screen_list *sl, unsigned int c) |
|---|
| 38 | { |
|---|
| 39 | /* FIXME handle return values */ |
|---|
| 40 | if (!sl->command_props.command) |
|---|
| 41 | { |
|---|
| 42 | sl->command_props.size = 1; |
|---|
| 43 | sl->command_props.x = 0; |
|---|
| 44 | sl->command_props.command = (char *)malloc(2); |
|---|
| 45 | sl->command_props.command[0] = 0; |
|---|
| 46 | } |
|---|
| 47 | else |
|---|
| 48 | { |
|---|
| 49 | sl->command_props.command = |
|---|
| 50 | (char *)realloc(sl->command_props.command, |
|---|
| 51 | sl->command_props.size + 1); |
|---|
| 52 | } |
|---|
| 53 | memmove(&sl->command_props.command[sl->command_props.x + 1], |
|---|
| 54 | &sl->command_props.command[sl->command_props.x], |
|---|
| 55 | (sl->command_props.size - sl->command_props.x)); |
|---|
| 56 | |
|---|
| 57 | sl->command_props.command[sl->command_props.x] = c; |
|---|
| 58 | sl->command_props.x++; |
|---|
| 59 | sl->command_props.size++; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | void del_char(struct screen_list *sl) |
|---|
| 63 | { |
|---|
| 64 | if(sl->command_props.x<1) return; |
|---|
| 65 | if (sl->command_props.size > 1) |
|---|
| 66 | sl->command_props.size--; |
|---|
| 67 | else |
|---|
| 68 | return; |
|---|
| 69 | |
|---|
| 70 | memcpy(&sl->command_props.command[sl->command_props.x - 1], |
|---|
| 71 | &sl->command_props.command[sl->command_props.x], |
|---|
| 72 | sl->command_props.size - sl->command_props.x); |
|---|
| 73 | |
|---|
| 74 | sl->command_props.command = |
|---|
| 75 | (char *)realloc(sl->command_props.command, sl->command_props.size); |
|---|
| 76 | |
|---|
| 77 | if (sl->command_props.x) |
|---|
| 78 | sl->command_props.x--; |
|---|
| 79 | sl->command_props.command[sl->command_props.size-1] = 0; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | int python_command_handle_key(struct screen_list *screen_list, unsigned int c) |
|---|
| 83 | { |
|---|
| 84 | if (c == CACA_KEY_ESCAPE) |
|---|
| 85 | { |
|---|
| 86 | screen_list->python_command = 0; |
|---|
| 87 | screen_list->changed = 1; |
|---|
| 88 | return 1; |
|---|
| 89 | } |
|---|
| 90 | else if(c == CACA_KEY_LEFT) |
|---|
| 91 | { |
|---|
| 92 | if(screen_list->command_props.x) |
|---|
| 93 | screen_list->command_props.x--; |
|---|
| 94 | } |
|---|
| 95 | else if(c == CACA_KEY_RIGHT) |
|---|
| 96 | { |
|---|
| 97 | if(screen_list->command_props.x < screen_list->command_props.size-1) |
|---|
| 98 | screen_list->command_props.x++; |
|---|
| 99 | } |
|---|
| 100 | else if(c == CACA_KEY_RETURN) |
|---|
| 101 | { |
|---|
| 102 | python_execute(screen_list); |
|---|
| 103 | } |
|---|
| 104 | else |
|---|
| 105 | { |
|---|
| 106 | if (c >= ' ' && c < 127) |
|---|
| 107 | add_char(screen_list, c); |
|---|
| 108 | else if (c == 8) |
|---|
| 109 | { |
|---|
| 110 | del_char(screen_list); |
|---|
| 111 | } |
|---|
| 112 | screen_list->changed = 1; |
|---|
| 113 | return 0; |
|---|
| 114 | } |
|---|
| 115 | return 0; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | void draw_python_command(struct screen_list *screen_list) |
|---|
| 119 | { |
|---|
| 120 | int w = 65, h = 5; |
|---|
| 121 | int x = (caca_get_canvas_width(screen_list->cv) - w) / 2; |
|---|
| 122 | int y = (caca_get_canvas_height(screen_list->cv) - h) / 2; |
|---|
| 123 | |
|---|
| 124 | caca_set_color_ansi(screen_list->cv, CACA_BLUE, CACA_BLUE); |
|---|
| 125 | caca_fill_box(screen_list->cv, x, y, w, h, '#'); |
|---|
| 126 | caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_BLUE); |
|---|
| 127 | caca_draw_cp437_box(screen_list->cv, x, y, w, h); |
|---|
| 128 | caca_printf(screen_list->cv, x, y, "Execute command"); |
|---|
| 129 | |
|---|
| 130 | caca_printf(screen_list->cv, x + 2, y + 2, |
|---|
| 131 | "[___________________________________________________________]"); |
|---|
| 132 | if (screen_list->command_props.command) |
|---|
| 133 | { |
|---|
| 134 | caca_printf(screen_list->cv, x + 3, y + 2, |
|---|
| 135 | "%s", screen_list->command_props.command); |
|---|
| 136 | caca_gotoxy(screen_list->cv, |
|---|
| 137 | x + 3 + screen_list->command_props.x, y + 2); |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | void python_execute(struct screen_list *sl) |
|---|
| 143 | { |
|---|
| 144 | debug("Executing '%s'\n", sl->command_props.command); |
|---|
| 145 | |
|---|
| 146 | if(sl->command_props.command); |
|---|
| 147 | free(sl->command_props.command); |
|---|
| 148 | sl->command_props.command = NULL; |
|---|
| 149 | sl->command_props.size = 0; |
|---|
| 150 | sl->command_props.x = 0; |
|---|
| 151 | sl->python_command = 0; |
|---|
| 152 | sl->changed = 1; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | #endif |
|---|