File: resolution.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 (481 lines) | stat: -rw-r--r-- 12,466 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*********************************************************
 * Copyright (C) 2005 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.
 *
 *********************************************************/

/*
 * resolution.c --
 *
 *     Set of functions to handle guest screen resizing for
 *     vmware-{user,guestd}.
 */

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

#include "vmware.h"
#include "vm_app.h"
#include "debug.h"
#include "rpcout.h"
#include "str.h"
#include "strutil.h"

#include "resolutionInt.h"
#include "resolution.h"


/*
 * Internal global variables
 */


/*
 * Describes current state of the library.
 */
ResolutionInfoType resolutionInfo = { .initialized = FALSE };


/*
 * Local function prototypes
 */

static Bool ResolutionResolutionSetCB(RpcInData *data);
static Bool ResolutionDisplayTopologySetCB(RpcInData *data);


/*
 * Global function definitions
 */


/*
 *-----------------------------------------------------------------------------
 *
 * Resolution_Init --
 *
 *      Initialize the guest resolution library.
 *
 * Results:
 *      TRUE on success, FALSE on failure (bad arguments?).
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

Bool
Resolution_Init(const char *tcloChannel,
                        // IN: TCLO channel name; used during capability registration
                        //     to tell the VMX whether Resolution_Set is being handled
                        //     by VMwareService/guestd or VMwareUser/vmware-user.
                InitHandle handle)
                        // IN: Back-end specific handle, if needed.  E.g., in the X11
                        //     case, this refers to the X11 display handle.
{
   // Shorter-named convenience alias.  I expect this to be optimized out.
   ResolutionInfoType *resInfo = &resolutionInfo;

   ASSERT(resInfo->initialized == FALSE);
   ASSERT((strcmp(tcloChannel, TOOLS_DAEMON_NAME) == 0) ||
          (strcmp(tcloChannel, TOOLS_DND_NAME)) == 0);

   /*
    * Blank out the resolutionInfo field, then copy user's arguments.
    */
   memset(resInfo, 0, sizeof *resInfo);

   strncpy(resInfo->tcloChannel, tcloChannel, sizeof resInfo->tcloChannel);
   resInfo->tcloChannel[sizeof resInfo->tcloChannel - 1] = '\0';

   if (!ResolutionBackendInit(handle)) {
      return FALSE;
   }

   resInfo->initialized = TRUE;

   return TRUE;
}


/*
 *-----------------------------------------------------------------------------
 *
 * Resolution_Cleanup --
 *
 *      Shutdown the library, free resources, etc.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      Resolution_* calls will fail until user next calls Resolution_Init().
 *
 *-----------------------------------------------------------------------------
 */

void
Resolution_Cleanup(void)
{
   // Shorter-named convenience alias.  I expect this to be optimized out.
   ResolutionInfoType *resInfo = &resolutionInfo;

   if (!resInfo->initialized) {
      return;
   }

   Resolution_UnregisterCaps();
   Resolution_CleanupBackdoor();
   ResolutionBackendCleanup();

   ASSERT(!resInfo->cbResolutionRegistered);
   ASSERT(!resInfo->cbTopologyRegistered);
   ASSERT(!resInfo->rpcIn);
}


/*-----------------------------------------------------------------------------
 *
 * Resolution_InitBackdoor --
 *
 *      Register RpcIn callbacks for supported/available RpcIn commands.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      Resolution_Set & DisplayTopology_Set callbacks may be registered.
 *
 *-----------------------------------------------------------------------------
 */

void
Resolution_InitBackdoor(RpcIn *rpcIn)
{
   // Shorter-named convenience alias.  I expect this to be optimized out.
   ResolutionInfoType *resInfo = &resolutionInfo;

   ASSERT(resInfo->initialized);
   ASSERT(rpcIn);

   resInfo->rpcIn = rpcIn;

   if (resInfo->canSetResolution) {
      RpcIn_RegisterCallbackEx(resInfo->rpcIn, "Resolution_Set",
                               ResolutionResolutionSetCB, NULL);
      resInfo->cbResolutionRegistered = TRUE;
   }

   if (resInfo->canSetTopology) {
      RpcIn_RegisterCallbackEx(resInfo->rpcIn, "DisplayTopology_Set",
                               ResolutionDisplayTopologySetCB, NULL);
      resInfo->cbTopologyRegistered = TRUE;
   }
}


/*
 *-----------------------------------------------------------------------------
 *
 * Resolution_CleanupBackdoor --
 *
 *      Unregisters RpcIn callbacks.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      Resolution callbacks are removed.
 *
 *-----------------------------------------------------------------------------
 */

void
Resolution_CleanupBackdoor(void)
{
   // Shorter-named convenience alias.  I expect this to be optimized out.
   ResolutionInfoType *resInfo = &resolutionInfo;

   if (!resInfo->initialized || !resInfo->rpcIn) {
      return;
   }

   if (resInfo->cbResolutionRegistered) {
      RpcIn_UnregisterCallback(resInfo->rpcIn, "Resolution_Set");
      resInfo->cbResolutionRegistered = FALSE;
   }

   if (resInfo->cbTopologyRegistered) {
      RpcIn_UnregisterCallback(resInfo->rpcIn, "DisplayTopology_Set");
      resInfo->cbTopologyRegistered = FALSE;
   }

   resInfo->rpcIn = FALSE;
}


/*
 *-----------------------------------------------------------------------------
 *
 * Resolution_RegisterCaps --
 *
 *      Register the "Resolution_Set" capability. Sometimes this needs to
 *      be done separately from the TCLO callback registration, so we
 *      provide it separately here.
 *
 * Results:
 *      TRUE on success
 *      FALSE on failure
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

Bool
Resolution_RegisterCaps(void)
{
   // Shorter-named convenience alias.  I expect this to be optimized out.
   ResolutionInfoType *resInfo = &resolutionInfo;

   if (!resInfo->initialized) {
      return FALSE;
   }

   if (resInfo->canSetResolution) {
      if (!RpcOut_sendOne(NULL, NULL, "tools.capability.resolution_set 1")) {
         Debug("%s: Unable to register resolution set capability\n",
               __FUNCTION__);
         return FALSE;
      }

      if (!RpcOut_sendOne(NULL, NULL, "tools.capability.resolution_server %s 1",
                          resInfo->tcloChannel)) {
         Debug("%s: Unable to register resolution server capability\n",
               __FUNCTION__);

         /*
          * Note that we do not return false so that we stay backwards
          * compatible with old vmx code (Workstation 6/ESX 3.5) that doesn't
          * handle resolution_server.
          */
      }
   }

   if (resInfo->canSetTopology) {
      if (!RpcOut_sendOne(NULL, NULL, "tools.capability.display_topology_set 2")) {
         Debug("%s: Unable to register topology set capability\n",
               __FUNCTION__);
      }

      if (!RpcOut_sendOne(NULL, NULL, "tools.capability.display_global_offset 1")) {
         Debug("%s: Unable to register topology global offset capability\n",
               __FUNCTION__);
         /*
          * Ignore failures - host may not support these RPCs.
          */
      }
   }

   return TRUE;
}




/*
 *-----------------------------------------------------------------------------
 *
 * Resolution_UnregisterCaps --
 *
 *      Unregister the "Resolution_Set" and "DisplayTopology_Set" capabilities.
 *
 * Results:
 *      TRUE on success
 *      FALSE on failure
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

Bool
Resolution_UnregisterCaps(void)
{
   // Shorter-named convenience alias.  I expect this to be optimized out.
   ResolutionInfoType *resInfo = &resolutionInfo;

   /*
    * RpcIn doesn't have an unregister facility, so all we need to do
    * here is unregister the capability.
    */

   if (!RpcOut_sendOne(NULL, NULL, "tools.capability.resolution_set 0")) {
      Debug("%s: Unable to unregister ResolutionSet capability\n",
	    __FUNCTION__);
      return FALSE;
   }

   if (!RpcOut_sendOne(NULL, NULL, "tools.capability.resolution_server %s 0",
		       resInfo->tcloChannel)) {
      Debug("%s: Unable to unregister resolution server capability\n",
	    __FUNCTION__);

      /*
       * Don't return false here so that an older vmx (Workstation 6/ESX 3.5)
       * that that supports resolution_set and not resolution_server will
       * still work.
       */
   }

   if (!RpcOut_sendOne(NULL, NULL, "tools.capability.display_topology_set 0") ||
       !RpcOut_sendOne(NULL, NULL, "tools.capability.display_global_offset 0")) {
      Debug("%s: Unable to unregister TopologySet capability\n",
	    __FUNCTION__);
      /*
       * Ignore failures - host may not support these RPCs.
       */
   }

   return TRUE;
}


/*
 * Local function definitions
 */


/*
 *-----------------------------------------------------------------------------
 *
 * ResolutionResolutionSetCB  --
 *
 *      Handler for TCLO 'Resolution_Set'.
 *
 * Results:
 *      TRUE if we can reply, FALSE otherwise.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

static Bool
ResolutionResolutionSetCB(RpcInData *data)      // IN/OUT:
{
   uint32 width = 0 ;
   uint32 height = 0;
   unsigned int index = 0;
   Bool retval = FALSE;

   /* parse the width and height */
   if (!StrUtil_GetNextUintToken(&width, &index, data->args, " ")) {
      goto invalid_arguments;
   }
   if (!StrUtil_GetNextUintToken(&height, &index, data->args, "")) {
      goto invalid_arguments;
   }

   retval = ResolutionSetResolution(width, height);

invalid_arguments:
   return RPCIN_SETRETVALS(data, retval ? "" : "Invalid arguments", retval);
}


/*
 *-----------------------------------------------------------------------------
 *
 * ResolutionDisplayTopologySetCB  --
 *
 *      Handler for TCLO 'DisplayTopology_Set'.
 *
 *      Routine unmarshals RPC arguments and passes over to back-end
 *      TopologySet().
 *
 * Results:
 *      TRUE if we can reply, FALSE otherwise.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

static Bool
ResolutionDisplayTopologySetCB(RpcInData *data) // IN/OUT:
{
   DisplayTopologyInfo *displays = NULL;
   unsigned int count, i;
   Bool success = FALSE;
   const char *p;

   /*
    * The argument string will look something like:
    *   <count> [ , <x> <y> <w> <h> ] * count.
    *
    * e.g.
    *    3 , 0 0 640 480 , 640 0 800 600 , 0 480 640 480
    */

   if (sscanf(data->args, "%u", &count) != 1) {
      return RPCIN_SETRETVALS(data,
                              "Invalid arguments. Expected \"count\"",
                              FALSE);
   }



   displays = malloc(sizeof *displays * count);
   if (!displays) {
      RPCIN_SETRETVALS(data,
                       "Failed to alloc buffer for display info",
                       FALSE);
      goto out;
   }

   for (p = data->args, i = 0; i < count; i++) {
      p = strchr(p, ',');
      if (!p) {
         RPCIN_SETRETVALS(data,
                          "Expected comma separated display list",
                          FALSE);
         goto out;
      }
      p++; /* Skip past the , */

      if (sscanf(p, " %d %d %d %d ", &displays[i].x,
                 &displays[i].y, &displays[i].width, &displays[i].height) != 4) {
         RPCIN_SETRETVALS(data,
                          "Expected x, y, w, h in display entry",
                          FALSE);
         goto out;
      }
   }

   success = ResolutionSetTopology(count, displays);

   RPCIN_SETRETVALS(data, success ? "" : "ResolutionSetTopology failed", success);

out:
   free(displays);
   return success;
}