1 | /** @file
|
---|
2 | *
|
---|
3 | * Guest client: display auto-resize.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | /** @todo this should probably be replaced by something IPRT */
|
---|
23 | /* For system() and WEXITSTATUS() */
|
---|
24 | #include <stdlib.h>
|
---|
25 | #include <sys/types.h>
|
---|
26 | #include <sys/wait.h>
|
---|
27 | #include <errno.h>
|
---|
28 |
|
---|
29 | #include <X11/Xlib.h>
|
---|
30 |
|
---|
31 | #include <iprt/assert.h>
|
---|
32 | #include <iprt/thread.h>
|
---|
33 | #include <VBox/log.h>
|
---|
34 | #include <VBox/VBoxGuest.h>
|
---|
35 |
|
---|
36 | #include "VBoxClient.h"
|
---|
37 |
|
---|
38 | static int initAutoResize()
|
---|
39 | {
|
---|
40 | int rc = VINF_SUCCESS, rcSystem, rcErrno;
|
---|
41 |
|
---|
42 | LogFlowFunc(("\n"));
|
---|
43 | rcSystem = system("VBoxRandR --test");
|
---|
44 | if (-1 == rcSystem)
|
---|
45 | {
|
---|
46 | rcErrno = errno;
|
---|
47 | rc = RTErrConvertFromErrno(rcErrno);
|
---|
48 | }
|
---|
49 | if (RT_SUCCESS(rc))
|
---|
50 | {
|
---|
51 | if (0 != WEXITSTATUS(rcSystem))
|
---|
52 | rc = VERR_NOT_SUPPORTED;
|
---|
53 | }
|
---|
54 | if (RT_SUCCESS(rc))
|
---|
55 | rc = VbglR3CtlFilterMask(VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST, 0);
|
---|
56 | LogFlowFunc(("returning %Rrc\n", rc));
|
---|
57 | return rc;
|
---|
58 | }
|
---|
59 |
|
---|
60 | void cleanupAutoResize(void)
|
---|
61 | {
|
---|
62 | LogFlowFunc(("\n"));
|
---|
63 | VbglR3CtlFilterMask(0, VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST);
|
---|
64 | LogFlowFunc(("returning\n"));
|
---|
65 | }
|
---|
66 |
|
---|
67 | /** This thread just runs a dummy X11 event loop to be sure that we get
|
---|
68 | * terminated should the X server exit. */
|
---|
69 | static int x11ConnectionMonitor(RTTHREAD, void *)
|
---|
70 | {
|
---|
71 | XEvent ev;
|
---|
72 | Display *pDisplay = XOpenDisplay(NULL);
|
---|
73 | while (true)
|
---|
74 | XNextEvent(pDisplay, &ev);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Display change request monitor thread function.
|
---|
79 | * Before entering the loop, we re-read the last request
|
---|
80 | * received, and if the first one received inside the
|
---|
81 | * loop is identical we ignore it, because it is probably
|
---|
82 | * stale.
|
---|
83 | */
|
---|
84 | int runAutoResize()
|
---|
85 | {
|
---|
86 | LogFlowFunc(("\n"));
|
---|
87 | uint32_t cx0 = 0, cy0 = 0, cBits0 = 0, iDisplay0 = 0;
|
---|
88 | int rc = RTThreadCreate(NULL, x11ConnectionMonitor, NULL, 0,
|
---|
89 | RTTHREADTYPE_INFREQUENT_POLLER, 0, "X11 monitor");
|
---|
90 | if (RT_FAILURE(rc))
|
---|
91 | return rc;
|
---|
92 | VbglR3GetLastDisplayChangeRequest(&cx0, &cy0, &cBits0, &iDisplay0);
|
---|
93 | while (true)
|
---|
94 | {
|
---|
95 | uint32_t cx = 0, cy = 0, cBits = 0, iDisplay = 0;
|
---|
96 | rc = VbglR3DisplayChangeWaitEvent(&cx, &cy, &cBits, &iDisplay);
|
---|
97 | /* Ignore the request if it is stale */
|
---|
98 | if ((cx != cx0) || (cy != cy0) || RT_FAILURE(rc))
|
---|
99 | {
|
---|
100 | /* If we are not stopping, sleep for a bit to avoid using up too
|
---|
101 | much CPU while retrying. */
|
---|
102 | if (RT_FAILURE(rc))
|
---|
103 | RTThreadYield();
|
---|
104 | else
|
---|
105 | system("VBoxRandR");
|
---|
106 | }
|
---|
107 | /* We do not want to ignore any further requests. */
|
---|
108 | cx0 = 0;
|
---|
109 | cy0 = 0;
|
---|
110 | }
|
---|
111 | LogFlowFunc(("returning VINF_SUCCESS\n"));
|
---|
112 | return VINF_SUCCESS;
|
---|
113 | }
|
---|
114 |
|
---|
115 | class AutoResizeService : public VBoxClient::Service
|
---|
116 | {
|
---|
117 | public:
|
---|
118 | virtual const char *getPidFilePath()
|
---|
119 | {
|
---|
120 | return ".vboxclient-autoresize.pid";
|
---|
121 | }
|
---|
122 | virtual int run()
|
---|
123 | {
|
---|
124 | int rc = initAutoResize();
|
---|
125 | if (RT_SUCCESS(rc))
|
---|
126 | rc = runAutoResize();
|
---|
127 | return rc;
|
---|
128 | }
|
---|
129 | virtual void cleanup()
|
---|
130 | {
|
---|
131 | cleanupAutoResize();
|
---|
132 | }
|
---|
133 | };
|
---|
134 |
|
---|
135 | VBoxClient::Service *VBoxClient::GetAutoResizeService()
|
---|
136 | {
|
---|
137 | return new AutoResizeService;
|
---|
138 | }
|
---|