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
|
//
// Copyright Jon © TURNEY 2012
//
// This file is part of XtoW.
//
// XtoW is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// XtoW 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
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with XtoW. If not, see <http://www.gnu.org/licenses/>.
//
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <xcwm/xcwm.h>
#include "debug.h"
#include "global.h"
#include "wndproc.h"
#define WM_XCWM_CREATE WM_USER
#define WM_XCWM_DESTROY (WM_USER+1)
xcwm_context_t *context;
DWORD msgPumpThread;
static void
eventHandler(const xcwm_event_t *event)
{
xcwm_window_t *window = xcwm_event_get_window(event);
xcwm_event_type_t type = xcwm_event_get_type(event);
DEBUG("event %d, window 0x%08x, XID 0x%08x\n", type, window, window->window_id);
switch (type)
{
case XCWM_EVENT_WINDOW_CREATE:
/*
Windows windows are owned by the thread they are created by,
and only the thread which owns the window can receive messages for it.
So, we must arrange for the thread which we want to run the Windows
message pump in to create the window...
*/
PostThreadMessage(msgPumpThread, WM_XCWM_CREATE, 0, (LPARAM)window);
break;
case XCWM_EVENT_WINDOW_DESTROY:
/*
Only the owner thread is allowed to destroy a window
*/
PostThreadMessage(msgPumpThread, WM_XCWM_DESTROY, 0, (LPARAM)window);
break;
case XCWM_EVENT_WINDOW_NAME:
UpdateName(window);
break;
case XCWM_EVENT_WINDOW_DAMAGE:
UpdateImage(window);
break;
case XCWM_EVENT_WINDOW_EXPOSE: // I don't think this event could ever be needed and is a mistake
break;
}
free((void *)event);
}
static void
help(void)
{
fprintf(stderr, "usage: xtow [options]\n");
fprintf(stderr, "-display dpy display to manage windows on\n");
fprintf(stderr, "-help\n");
exit(0);
}
int main(int argc, char **argv)
{
char *screen = NULL;
while (1)
{
static struct option long_options[] =
{
{ "display", required_argument, 0, 'd' },
{ "help", no_argument, 0, 'h' },
{0, 0, 0, 0 },
};
int option_index = 0;
int c = getopt_long_only(argc, argv, "d:h", long_options, &option_index);
if (c == -1)
break;
switch (c)
{
case 'd':
screen = optarg;
break;
case 'h':
default:
help();
}
}
if (optind < argc)
help();
DEBUG("screen is '%s'\n", screen);
// ensure this thread has a message queue
MSG msg;
PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
msgPumpThread = GetCurrentThreadId();
// create the global xcwm context
context = xcwm_context_open(screen);
// spawn the event loop thread, and set the callback function
xcwm_event_start_loop(context, eventHandler);
// pump windows message queue
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
if (msg.message == WM_XCWM_CREATE)
winCreateWindowsWindow((xcwm_window_t *)msg.lParam);
else if (msg.message == WM_XCWM_DESTROY)
winDestroyWindowsWindow((xcwm_window_t *)msg.lParam);
else
DispatchMessage(&msg);
}
// Shutdown:
// if we got WM_QUIT, message loop exits
// if server died, we get a error from xcb
// if window station is shutting down, we get WM_ENDSESSION
// if we got a signal...
xcwm_context_close(context);
}
|