File: toolboxDevices.c

package info (click to toggle)
open-vm-tools 1%3A8.4.2-261024-1%2Bbuild1
  • links: PTS, VCS
  • area: contrib
  • in suites: squeeze-lts
  • size: 20,376 kB
  • ctags: 30,043
  • sloc: ansic: 164,785; sh: 10,713; cpp: 6,525; makefile: 3,386
file content (215 lines) | stat: -rw-r--r-- 7,088 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*********************************************************
 * Copyright (C) 2004 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation version 2.1 and no later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *********************************************************/

/*
 * toolboxDevices.c --
 *
 *     The devices tab for the linux gtk toolbox
 */

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "toolboxGtkInt.h"
#include "removable_device.h"
#include "guestApp.h"
#include "eventManager.h"

/*
 * Globals
 */
static GtkWidget *deviceLabel;
static GtkWidget *deviceScrollwin;

/*
 *-----------------------------------------------------------------------------
 *
 * Devices_DevicesLoop  --
 *
 *      Event Manager function for tracking the list of removable device
 *      and their connected/dissconnected state from the vmx. This function
 *      polls the backdoor for the current state of removable devices and 
 *      updates the widgets in the Devices tab accordingly. It temporarily
 *      blocks signal handlers because those handlers are meant for user
 *      interaction not for reacting to progammatic toggling of the buttons.
 *
 * Results:
 *      TRUE.
 *
 * Side effects:
 *      The Devices tab UI will change.
 *
 *-----------------------------------------------------------------------------
 */

static Bool
Devices_UpdateLoop(void* clientData)
{
   int i;
   Bool atLeastOne = FALSE;
   GtkWidget **btnArray = (GtkWidget **)clientData;

   for (i=0; i<MAX_DEVICES; i++) {
      RD_Info info;
      if (GuestApp_GetDeviceInfo(i, &info) && strlen(info.name) > 0) {
         gtk_widget_show(btnArray[i]);
         gtk_label_set_text(GTK_LABEL(GTK_BIN(btnArray[i])->child), 
                            info.name);
         gtk_signal_handler_block_by_func(GTK_OBJECT(btnArray[i]),
                                          GTK_SIGNAL_FUNC(Devices_OnDeviceToggled),
                                          GINT_TO_POINTER(i));
         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(btnArray[i]), info.enabled);
         gtk_signal_handler_unblock_by_func(GTK_OBJECT(btnArray[i]),
                                            GTK_SIGNAL_FUNC(Devices_OnDeviceToggled),
                                            GINT_TO_POINTER(i));
         atLeastOne = TRUE;
      }
      else {
         gtk_widget_hide(btnArray[i]);
      }
   }

   if (atLeastOne) {
      gtk_widget_show(deviceScrollwin);
   } else {
      gtk_label_set_text(GTK_LABEL(deviceLabel), 
                         "No removable devices are available. Either this\nvirtual machine has no removable devices or its\nconfiguration does not allow you to connect and\ndisconnect them.");
      gtk_widget_hide(deviceScrollwin);
   }

   EventManager_Add(gEventQueue, DEVICES_POLL_TIME, Devices_UpdateLoop,
                    clientData);   
   return TRUE;
}


/*
 *-----------------------------------------------------------------------------
 *
 * Devices_Create  --
 *
 *      Create, layout, and init the Devices tab UI and all its widgets.
 *
 * Results:
 *      The Devices tab widget (it's a vbox).
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

GtkWidget*
Devices_Create(GtkWidget* mainWnd)
{
   GtkWidget* devicestab;
   GtkWidget* vbox;
   GtkWidget **deviceBtns;

   int i;

   /*
    * We don't free this, but it's not really a leak because Devices_Create should
    * only be called once per app invocation
    */
   deviceBtns = calloc(MAX_DEVICES, sizeof (GtkWidget *));

   devicestab = gtk_vbox_new (FALSE, 10);
   gtk_widget_show(devicestab);
   gtk_container_set_border_width(GTK_CONTAINER(devicestab), 10);

   deviceLabel = gtk_label_new("Check a device to connect it to the virtual machine");
   gtk_widget_show(deviceLabel);
   gtk_box_pack_start(GTK_BOX(devicestab), deviceLabel, FALSE, FALSE, 0);
   gtk_label_set_justify(GTK_LABEL(deviceLabel), GTK_JUSTIFY_LEFT);
   gtk_misc_set_alignment(GTK_MISC(deviceLabel), 0, 0);

   deviceScrollwin = gtk_scrolled_window_new(NULL, NULL);
   gtk_widget_show(deviceScrollwin);
   gtk_box_pack_start(GTK_BOX(devicestab), deviceScrollwin, TRUE, TRUE, 0);
   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(deviceScrollwin), 
                                  GTK_POLICY_AUTOMATIC,
                                  GTK_POLICY_AUTOMATIC);

   vbox = gtk_vbox_new(FALSE,0);
   gtk_widget_show(vbox);
   gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(deviceScrollwin), 
                                         vbox);

   for (i=0; i<MAX_DEVICES; i++) {
      deviceBtns[i] = gtk_check_button_new_with_label("none");         
      gtk_box_pack_start(GTK_BOX(vbox), deviceBtns[i], FALSE, FALSE, 0);
      gtk_label_set_justify(GTK_LABEL(GTK_BIN(GTK_BUTTON(deviceBtns[i]))->child),
                            GTK_JUSTIFY_LEFT);
      gtk_signal_connect(GTK_OBJECT(deviceBtns[i]), "toggled",
                         GTK_SIGNAL_FUNC(Devices_OnDeviceToggled), GINT_TO_POINTER(i));
   }

   if (deviceBtns[0] != NULL) {
      Devices_UpdateLoop((void *)deviceBtns);
   }

   return devicestab;
}


/*
 *-----------------------------------------------------------------------------
 *
 * Devices_OnDeviceToggled --
 *
 *      Callback for the gtk signal "toggled" on Devices tab checkboxes.
 *      Sends the new state thru the backdoor, causing the vmx to connect
 *      or disconnect the device
 * Results:
 *      None.
 *
 * Side effects:
 *      The vmx should connect/disconnect the removable device. There will
 *      be a lot of side effects, depending on the device.
 *
 *-----------------------------------------------------------------------------
 */

void
Devices_OnDeviceToggled(gpointer btn,  // IN: button which was toggled
                        gpointer data) // IN: device id
{
   int dev_id;
   Bool enabled;
   RD_Info info;

   dev_id = GPOINTER_TO_INT(data);
   enabled = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(btn));

   if (GuestApp_GetDeviceInfo(dev_id, &info)) {
      if (info.enabled != enabled) {
         char msg[64];
         snprintf(msg, sizeof(msg), "Unable to %s device. Do you want to retry?\n",
                  enabled ? "connect" : "disconnect");
         while (!GuestApp_SetDeviceState(dev_id, enabled) &&
                ToolsMain_YesNoBox("Error", msg))
            ;
      }
   } else {
      ToolsMain_MsgBox("Error", "Unable to get device info");
   }

   return;
}