/* * menu_commands.c * This file is part of LCDd, the lcdproc server. * * This file is released under the GNU General Public License. Refer to the * COPYING file distributed with this package. * * Copyright (c) 1999, William Ferrell, Scott Scriven * 2002, Joris Robijn * * * This contains definitions for all the functions which clients can run. * The functions here are to be called only from parse.c's interpreter. * * The client's available function set is defined here, as is the syntax * for each command. * * This particular file defines actions concerning client supplied menus. * */ #include #include #include #include #include #include #include #include "shared/report.h" #include "shared/sockets.h" #include "menu.h" #include "menuitem.h" #include "menuscreens.h" #include "client.h" /* Local functions */ MenuEventFunc(menu_commands_handler); /************************************************************************* * menu_add_item_func * * Adds an item to a menu * * Usage: menu_add_item [] * You should use "" as id for the client's main menu. This menu will be * created automatically when you add an item to it the first time. * You (currently?) cannot create a menu in the main level yourself. * The names you use for items should be unique for your client. * The text is the visible text for the item. * * The following types are available: * - menu * - action * - checkbox * - ring (a kind of listbox of one line) * - slider * - numeric * - alpha */ int menu_add_item_func (Client * c, int argc, char **argv) { char * menu_id; char * item_id; char * text = NULL; Menu * menu = NULL; MenuItem * item; MenuItemType itemtype; if (!c->ack) return 1; if (!c->name) { sock_send_string (c->sock, "huh? You need to give your client a name first\n"); return 0; } if ((argc < 4 )) { sock_send_string (c->sock, "huh? Usage: menu_add_item []\n"); return 0; } menu_id = argv[1]; item_id = argv[2]; /* Does the client have a menu already ? */ if (!c->menu) { /* We need to create it */ report( RPT_INFO, "Client [%d] is using the menu", c->sock ); c->menu = menu_create ("_client_menu_", menu_commands_handler, c->name, c); menu_add_item (main_menu, c->menu); } if ( menu_id[0] == 0 ) { /* No menu specified = client's main menu */ menu = c->menu; } else { /* A specified menu */ menu = menu_find_item (c->menu, menu_id, true); } if (!menu) { sock_send_string (c->sock, "huh? Cannot find menu id\n"); return 0; } item = menu_find_item (c->menu, item_id, true); if (item) { sock_send_string (c->sock, "huh? Item id already in use\n"); return 0; } /* Find menuitem type */ itemtype = menuitem_typename_to_type (argv[3]); if (itemtype == -1) { sock_send_string (c->sock, "huh? Invalid menuitem type\n"); return 0; } /* Is a text given ? */ if (argc >= 5) { text = argv[4]; } else { text = ""; } /* Create the menuitem */ switch (itemtype) { case MENUITEM_MENU: item = menu_create (item_id, menu_commands_handler, text, c); break; case MENUITEM_ACTION: item = menuitem_create_action (item_id, menu_commands_handler, text, MENURESULT_NONE); break; case MENUITEM_CHECKBOX: item = menuitem_create_checkbox (item_id, menu_commands_handler, text, false, false); break; case MENUITEM_RING: item = menuitem_create_ring (item_id, menu_commands_handler, text, "", 0); break; case MENUITEM_SLIDER: item = menuitem_create_slider (item_id, menu_commands_handler, text, "", "", 0, 100, 1, 25); break; case MENUITEM_NUMERIC: item = menuitem_create_numeric (item_id, menu_commands_handler, text, 0, 100, 0); break; case MENUITEM_ALPHA: item = menuitem_create_alpha (item_id, menu_commands_handler, text, 0, 0, 10, true, false, true, "-./", ""); break; } menu_add_item (menu, item); menuscreen_inform_item_modified (menu); sock_send_string(c->sock, "success\n"); return 0; } /************************************************************************* * menu_del_item_func * * Deletes an item from a menu * * Usage: menu_del_item * The given item in the given menu will be deleted. If you have deleted all * the items from your client menu, that menu will automatically be removed. */ int menu_del_item_func (Client * c, int argc, char **argv) { Menu * menu; MenuItem * item; char * menu_id; char * item_id; if (!c->ack) return 1; if ((argc < 3 )) { sock_send_string (c->sock, "huh? Usage: menu_del_item \n"); return 0; } menu_id = argv[1]; item_id = argv[2]; /* Does the client have a menu already ? */ if (!c->menu) { sock_send_string (c->sock, "huh? Client has no menu\n"); return 0; } if ( menu_id[0] == 0 ) { /* No menu specified = client's main menu */ menu = c->menu; } else { /* A specified menu */ menu = menu_find_item (c->menu, menu_id, true); } if (!menu) { sock_send_string (c->sock, "huh? Cannot find menu id\n"); return 0; } item = menu_find_item (c->menu, item_id, true); if (!item) { sock_send_string (c->sock, "huh? Cannot find item\n"); return 0; } menuscreen_inform_item_destruction (item); menu_remove_item (menu, item); menuscreen_inform_item_modified (item->parent); menuitem_destroy (item); /* Was it the last item in the client's menu ? */ if (menu_getfirst_item(c->menu) == NULL) { menuscreen_inform_item_destruction (c->menu); menu_remove_item (main_menu, c->menu); menuscreen_inform_item_modified (main_menu); menu_destroy (c->menu); c->menu = NULL; } sock_send_string(c->sock, "success\n"); return 0; } /************************************************************************** * menu_set_item_func * Sets the info about a menu item * * For example, text displayed, value, etc... * * Usage: menu_set_item {