menuapitest.c

Go to the documentation of this file.
00001 /*
00002  * File Name: menuapi.c
00003  */
00004 
00005 /*
00006  * This file is part of popupmenu.
00007  *
00008  * popupmenu is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * popupmenu is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 #include "config.h"
00028 
00029 #include <glib.h>
00030 #include <stdlib.h> 
00031 #include <getopt.h>
00032 #include <string.h>
00033 
00034 #include <liberipc/eripc.h>
00035 
00036 #define DBUS_SERVICE            "com.irexnet.popupmenu"
00037 
00038 static gboolean verbose_flag = FALSE;
00039 int activated_handler = -1;
00040 eripc_context_t *eripcContext = NULL;
00041 
00042 enum cmd_type
00043 {
00044   CMD_UNKNOWN,
00045   CMD_ADD,
00046   CMD_REMOVE,
00047   CMD_SET_STATE,
00048   CMD_SHOW,
00049   CMD_HIDE,
00050   NUM_CMD
00051 };
00052 
00053 enum prop_type
00054 {
00055   PROP_UNKNOWN,
00056   PROP_GROUP,
00057   PROP_STATUSITEM,
00058   PROP_MENUITEM,
00059   PROP_MENU,
00060   NUM_PROP
00061 };
00062 
00063 static const gchar* eripcErrorStr(eripc_error_t errorcode) 
00064 {
00065     switch (errorcode) 
00066     {
00067     case ERIPC_ERROR_SUCCESS:
00068         return "No error (ERIPC_ERROR_SUCCESS)";
00069     case ERIPC_ERROR:
00070         return "Some kind of error occurred (ERIPC_ERROR)";
00071     case ERIPC_ERROR_INVALID:
00072         return "At least one parameter is invalid (ERIPC_ERROR_INVALID)";
00073     case ERIPC_ERROR_OOM:
00074         return "Out of memory error (ERIPC_ERROR_OOM)";
00075     case ERIPC_ERROR_TIMEOUT:
00076         return "Timeout error (ERIPC_ERROR_TIMEOUT)";
00077     default:
00078         return "Unknown/Undefined";
00079     }
00080 }
00081 
00082 /*
00083 static void add_item(eripc_context_t *ctx, const char *name, const char *parent,
00084                     const char *label, const char *img, const char *img_selected, const char *img_disabled) 
00085 {
00086     eripc_error_t result;
00087     
00088     g_return_if_fail(name != NULL);
00089     g_return_if_fail(parent != NULL);
00090     g_return_if_fail(label != NULL);
00091     g_return_if_fail(img != NULL);
00092     g_return_if_fail(img_selected != NULL);
00093     g_return_if_fail(img_disabled != NULL);
00094 
00095     result = eripc_send_varargs_and_wait(ctx,
00096                                 NULL,
00097                                 ERIPC_BUS_SESSION,
00098                                 DBUS_SERVICE,
00099                                 "addItem",
00100                                 ERIPC_TYPE_STRING, name,
00101                                 ERIPC_TYPE_STRING, parent,
00102                                 ERIPC_TYPE_STRING, label,
00103                                 ERIPC_TYPE_STRING, img,
00104                                 ERIPC_TYPE_STRING, img_selected,
00105                                 ERIPC_TYPE_STRING, img_disabled,
00106                                 ERIPC_TYPE_INVALID);
00107 
00108     if (result != ERIPC_ERROR_SUCCESS) 
00109     {
00110         g_error("Error launching the RPC (%s)\n", eripcErrorStr(result));
00111     } 
00112 }
00113 
00114 
00115 static void add_menu(eripc_context_t *ctx, const char *name, const char *group1,
00116                     const char *group2, const char *group3, const char *group4) 
00117 {
00118     eripc_error_t result;
00119     
00120     g_return_if_fail(name != NULL);
00121     g_return_if_fail(group1 != NULL);
00122     g_return_if_fail(group2 != NULL);
00123     g_return_if_fail(group3 != NULL);
00124     g_return_if_fail(group4 != NULL);
00125 
00126     result = eripc_send_varargs_and_wait(ctx,
00127                                 NULL,
00128                                 ERIPC_BUS_SESSION,
00129                                 DBUS_SERVICE,
00130                                 "addMenu",
00131                                 ERIPC_TYPE_STRING, name,
00132                                 ERIPC_TYPE_STRING, group1,
00133                                 ERIPC_TYPE_STRING, group2,
00134                                 ERIPC_TYPE_STRING, group3,
00135                                 ERIPC_TYPE_STRING, group4,
00136                                 ERIPC_TYPE_INVALID);
00137 
00138     if (result != ERIPC_ERROR_SUCCESS) 
00139     {
00140         g_error("Error launching the RPC (%s)\n", eripcErrorStr(result));
00141     } 
00142 }
00143 */
00144 
00145 static void set_item_state(eripc_context_t *ctx, const char *iname, const char *pname, const char *state) 
00146 {
00147     eripc_error_t result;
00148     
00149     g_return_if_fail(pname != NULL);
00150     g_return_if_fail(state != NULL);
00151     
00152     result = eripc_send_varargs_and_wait(ctx,
00153                                 NULL,
00154                                 ERIPC_BUS_SESSION,
00155                                 DBUS_SERVICE,
00156                                 "setItemState",
00157                                 ERIPC_TYPE_STRING, iname,
00158                                 ERIPC_TYPE_STRING, pname,
00159                                 ERIPC_TYPE_STRING, state,
00160                                 ERIPC_TYPE_INVALID);
00161 
00162     if (result != ERIPC_ERROR_SUCCESS) 
00163    {
00164         g_error("Error launching the RPC (%s)\n", eripcErrorStr(result));
00165     }
00166 }
00167 
00168 
00169 static void set_group_state(eripc_context_t *ctx, const char *pname, const char *state) 
00170 {
00171     eripc_error_t result;
00172     
00173     g_return_if_fail(pname != NULL);
00174     g_return_if_fail(state != NULL);
00175     
00176     result = eripc_send_varargs_and_wait(ctx,
00177                                 NULL,
00178                                 ERIPC_BUS_SESSION,
00179                                 DBUS_SERVICE,
00180                                 "setGroupState",
00181                                 ERIPC_TYPE_STRING, pname,
00182                                 ERIPC_TYPE_STRING, state,
00183                                 ERIPC_TYPE_INVALID);
00184 
00185     if (result != ERIPC_ERROR_SUCCESS) 
00186    {
00187         g_error("Error launching the RPC (%s)\n", eripcErrorStr(result));
00188     }
00189 }
00190 
00191 static void set_popup_state(eripc_context_t *ctx, const char *state) 
00192 {
00193     eripc_error_t result;
00194     
00195     g_return_if_fail(state != NULL);
00196   
00197     result = eripc_send_varargs_and_wait(ctx,
00198                                 NULL,
00199                                 ERIPC_BUS_SESSION,
00200                                 DBUS_SERVICE,
00201                                 "setPopupState",
00202                                 ERIPC_TYPE_STRING, state,
00203                                 ERIPC_TYPE_INVALID);
00204 
00205     if (result != ERIPC_ERROR_SUCCESS) 
00206    {
00207         g_error("Error launching the RPC (%s)\n", eripcErrorStr(result));
00208     }
00209 }
00210 
00211 
00212 static void set_statusitem_state(eripc_context_t *ctx, const char *iname, const char *state) 
00213 {
00214     eripc_error_t result;
00215     
00216     g_return_if_fail(iname != NULL);
00217     g_return_if_fail(state != NULL);
00218 
00219     result = eripc_send_varargs_and_wait(ctx,
00220                                 NULL,
00221                                 ERIPC_BUS_SESSION,
00222                                 DBUS_SERVICE,
00223                                 "setStatusItemState",
00224                                 ERIPC_TYPE_STRING, iname,
00225                                 ERIPC_TYPE_STRING, state,
00226                                 ERIPC_TYPE_INVALID);
00227 
00228     if (result != ERIPC_ERROR_SUCCESS) 
00229    {
00230         g_error("Error launching the RPC (%s)\n", eripcErrorStr(result));
00231     }
00232 }
00233 
00234 
00235 static void show_statusitem(eripc_context_t *ctx, const char *iname) 
00236 {
00237     eripc_error_t result;
00238     
00239     g_return_if_fail(iname != NULL);
00240     
00241     result = eripc_send_varargs_and_wait(ctx,
00242                                 NULL,
00243                                 ERIPC_BUS_SESSION,
00244                                 DBUS_SERVICE,
00245                                 "showStatusItem",
00246                                 ERIPC_TYPE_STRING, iname,
00247                                 ERIPC_TYPE_INVALID);
00248 
00249     if (result != ERIPC_ERROR_SUCCESS) 
00250    {
00251         g_error("Error launching the RPC (%s)\n", eripcErrorStr(result));
00252     }
00253 }
00254 
00255 
00256 static void hide_statusitem(eripc_context_t *ctx, const char *iname) 
00257 {
00258     eripc_error_t result;
00259     
00260     g_return_if_fail(iname != NULL);
00261 
00262     result = eripc_send_varargs_and_wait(ctx,
00263                                 NULL,
00264                                 ERIPC_BUS_SESSION,
00265                                 DBUS_SERVICE,
00266                                 "hideStatusItem",
00267                                 ERIPC_TYPE_STRING, iname,
00268                                 ERIPC_TYPE_INVALID);
00269 
00270     if (result != ERIPC_ERROR_SUCCESS) 
00271    {
00272         g_error("Error launching the RPC (%s)\n", eripcErrorStr(result));
00273     }
00274 }
00275 
00276 
00277 static void remove_group(eripc_context_t *ctx, const char *name) 
00278 {
00279     eripc_error_t result;
00280     
00281     g_return_if_fail(name != NULL);
00282 
00283     result = eripc_send_varargs_and_wait(ctx,
00284                                 NULL,
00285                                 ERIPC_BUS_SESSION,
00286                                 DBUS_SERVICE,
00287                                 "removeGroup",
00288                                 ERIPC_TYPE_STRING, name,
00289                                 ERIPC_TYPE_INVALID);
00290 
00291     if (result != ERIPC_ERROR_SUCCESS) 
00292    {
00293         g_error("Error launching the RPC (%s)\n", eripcErrorStr(result));
00294     }
00295 }
00296 
00297 
00298 static void remove_item(eripc_context_t *ctx, const char *name, const char *parent) 
00299 {
00300     eripc_error_t result;
00301     
00302     g_return_if_fail(name != NULL);
00303 
00304     result = eripc_send_varargs_and_wait(ctx,
00305                                 NULL,
00306                                 ERIPC_BUS_SESSION,
00307                                 DBUS_SERVICE,
00308                                 "removeItem",
00309                                 ERIPC_TYPE_STRING, name,
00310                                 ERIPC_TYPE_STRING, parent,
00311                                 ERIPC_TYPE_INVALID);
00312 
00313     if (result != ERIPC_ERROR_SUCCESS) 
00314    {
00315         g_error("Error launching the RPC (%s)\n", eripcErrorStr(result));
00316     }
00317 }
00318 
00319 static void remove_menu(eripc_context_t *ctx, const char *name) 
00320 {
00321     eripc_error_t result;
00322     
00323     g_return_if_fail(name != NULL);
00324     
00325     result = eripc_send_varargs_and_wait(ctx,
00326                                 NULL,
00327                                 ERIPC_BUS_SESSION,
00328                                 DBUS_SERVICE,
00329                                 "removeMenu",
00330                                 ERIPC_TYPE_STRING, name,
00331                                 ERIPC_TYPE_INVALID);
00332 
00333     if (result != ERIPC_ERROR_SUCCESS) 
00334    {
00335         g_error("Error launching the RPC (%s)\n", eripcErrorStr(result));
00336     }
00337 }
00338 
00339 
00340 static void show_menu(eripc_context_t *ctx, const char *name) 
00341 {
00342     eripc_error_t result;
00343     
00344     g_return_if_fail(name != NULL);
00345         
00346     result = eripc_send_varargs_and_wait(ctx,
00347                                 NULL,
00348                                 ERIPC_BUS_SESSION,
00349                                 DBUS_SERVICE,
00350                                 "showMenu",
00351                                 ERIPC_TYPE_STRING, name,
00352                                 ERIPC_TYPE_INVALID);
00353 
00354     if (result != ERIPC_ERROR_SUCCESS) 
00355     {
00356         g_error("Error launching the RPC (%s)\n", eripcErrorStr(result));
00357     }
00358 }
00359 
00360 void usage()
00361 {
00362     g_print("Usage: %s -r -s -n newstate -i item -g group -m menu -t statusitem\n", "menuapitest");
00363     g_print("\t-r --remove\t\tRemove command, followed by item, group or menu\n");
00364     g_print("\t-s --show\t\tShow command, followed by menu\n");
00365     g_print("\t-n --newstate <newstate>\tSet new state to:\n");
00366     g_print("\t\t\titem:\tNORMAL, SELECTED, DISABLED\n");
00367     g_print("\t\t\tgroup:\tNORMAL, DISABLED\n");
00368     g_print("\t\t\tmenu:\tSHOW, HIDE, TOGGLE\n");
00369     g_print("\t\t\tstatus:\tname of status\n");
00370     g_print("\t-i --item=<item>\tItem to operate command (group must be specified first)\n");
00371     g_print("\t-g --group=<group>\tGroup to operate command\n");
00372     g_print("\t-m --menu[=<menu>]\tMenu to operate command (menu name not required for 'newstate')\n");
00373     g_print("\t-t --statusitem=<statusitem>\tStatus item to operate command\n");
00374     g_print("Example: %s --show --menu=uds_menu_reading\n", "menuapitest");
00375     g_print("Example: %s --newstate=SELECTED --group=system --item=rotate_screen\n", "menuapitest");
00376     g_print("Example: %s --newstate=cha040 --statusitem=battery\n", "menuapitest");
00377     g_print("Example: %s --newstate=TOGGLE --menu\n", "menuapitest");
00378 }
00379 
00380 int main (int argc, char **argv)
00381 {
00382     int c;
00383     
00384     enum cmd_type command = CMD_UNKNOWN;
00385     enum prop_type property = PROP_UNKNOWN;
00386     GMainLoop *gloop = NULL;
00387     GString *iname = NULL;
00388     GString *pname = NULL;
00389     GString *state = NULL;
00390 
00391     gloop = g_main_loop_new(NULL, FALSE);
00392     eripcContext = eripc_init("menuapitest", "1.0", NULL);
00393     if (eripcContext == NULL) 
00394     {
00395        g_error("Failed to initialize IPC API\n");
00396     }
00397 
00398     eripc_set_timeout(eripcContext, 2000);
00399 
00400     while (1)
00401     {
00402        static struct option long_options[] =
00403          {
00404            /* These options set a flag. */
00405            {"verbose", no_argument,       &verbose_flag, TRUE},
00406            /* These options don't set a flag.
00407               We distinguish them by their indices. */
00408            {"remove",   no_argument,       NULL, 'r'},
00409            {"show",     no_argument,       NULL, 's'},
00410            {"hide",     no_argument,       NULL, 'h'},
00411            {"item",     required_argument, NULL, 'i'},
00412            {"group",    required_argument, NULL, 'g'},
00413            {"menu",     optional_argument, NULL, 'm'},
00414            {"newstate", required_argument, NULL, 'n'},
00415            {"statusitem",required_argument,NULL, 't'},
00416            {0, 0, 0, 0}
00417          };
00418        /* getopt_long stores the option index here. */
00419        int option_index = 0;
00420  
00421         c = getopt_long (argc, argv, "rshi:g:n:t:m::", long_options, &option_index);
00422  
00423        /* Detect the end of the options. */
00424        if (c == -1)
00425          break;
00426  
00427        switch (c)
00428          {
00429          case 0:
00430            /* If this option set a flag, do nothing else now. */
00431            if (long_options[option_index].flag != 0)
00432              break;
00433            g_print("option %s", long_options[option_index].name);
00434            if (optarg)
00435              g_print(" with arg %s", optarg);
00436            g_print("\n");
00437            break;
00438  
00439          case 'r':
00440            command = CMD_REMOVE;
00441            g_print("option -r\n");
00442            break;
00443  
00444          case 's':
00445            command = CMD_SHOW;
00446            g_print("option -s\n");
00447            break;
00448  
00449          case 'h':
00450            command = CMD_HIDE;
00451            g_print("option -h\n");
00452            break;
00453                      
00454          case 'i':
00455            if (pname != NULL)
00456            {
00457              g_print("option -i with value `%s'\n", optarg);
00458              property = PROP_MENUITEM;
00459              iname = g_string_new(optarg);
00460            }
00461            else 
00462            {
00463              g_error("option -g must be set before -i\n");
00464            }
00465            break;
00466  
00467          case 't':
00468            g_print("option -t with value `%s'\n", optarg);
00469            property = PROP_STATUSITEM;
00470            iname = g_string_new(optarg);
00471            break;
00472                      
00473          case 'g':
00474            g_print("option -g with value `%s'\n", optarg);
00475            property = PROP_GROUP;
00476            pname = g_string_new(optarg);
00477            break;
00478  
00479          case 'm':
00480            pname = g_string_new(optarg);
00481            if (optarg != NULL)
00482            {
00483                 g_print("option -m with value `%s'\n", optarg);
00484            }
00485            else
00486            {
00487                 g_print("option -m\n");
00488            }
00489                      
00490            if (property==PROP_UNKNOWN) 
00491            {
00492                property = PROP_MENU;
00493            }
00494            break;
00495  
00496          case 'n':
00497            g_print("option -n with value `%s'\n", optarg);
00498            command = CMD_SET_STATE;
00499            state = g_string_new(optarg);
00500            break;
00501  
00502          case '?':
00503            /* getopt_long already printed an error message. */
00504            usage();
00505            break;
00506  
00507          default:
00508            usage();
00509            break;
00510          }
00511     }
00512  
00513    if (verbose_flag)
00514      g_print("verbose flag is set");
00515  
00516    if (optind < argc)
00517      {
00518        g_print("unsupported options: ");
00519        while (optind < argc)
00520          g_print("%s ", argv[optind++]);
00521        g_print("\n");
00522      }
00523  
00524     switch (command)
00525     {
00526     case CMD_REMOVE:
00527       switch (property)
00528       {
00529       case PROP_MENUITEM:
00530         remove_item(eripcContext, iname->str, pname->str);
00531         break;
00532       case PROP_GROUP:
00533         remove_group(eripcContext, pname->str);
00534         break;
00535       case PROP_MENU:
00536         remove_menu(eripcContext, pname->str);
00537         break;
00538       default:
00539         break;
00540       }
00541       break;
00542         
00543     case CMD_SET_STATE:
00544       switch (property)
00545       {
00546       case PROP_STATUSITEM:
00547         set_statusitem_state(eripcContext, iname->str, state->str);
00548         break;
00549       case PROP_MENUITEM:
00550         set_item_state(eripcContext, iname->str, pname->str, state->str);
00551         break;
00552       case PROP_GROUP:
00553         set_group_state(eripcContext, pname->str, state->str);
00554         break;
00555       case PROP_MENU:
00556         set_popup_state(eripcContext, state->str);
00557         break;
00558       default:
00559         break;
00560       }
00561       break;
00562         
00563     case CMD_SHOW:
00564       switch (property)
00565       {
00566       case PROP_MENU:
00567         show_menu(eripcContext, pname->str);     
00568         break;
00569       case PROP_STATUSITEM:
00570         show_statusitem(eripcContext, iname->str);
00571         break;
00572       default:
00573         break;
00574       }
00575       break;
00576         
00577     case CMD_HIDE:
00578       switch (property)
00579       {
00580       case PROP_STATUSITEM:
00581         hide_statusitem(eripcContext, iname->str);
00582         break;
00583       default:
00584         break;
00585       }
00586       break;
00587         
00588     default:
00589       break;
00590     }
00591     
00592     eripcContext = NULL;
00593     return EXIT_SUCCESS;    
00594 }
Generated by  doxygen 1.6.2-20100208