winlivebg_test1

view src/main.c @ 2:a9025f31ae2d

sortof works, testing with colcycle hack
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 28 Oct 2019 16:07:25 +0200
parents aa6a9521b088
children
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include <windows.h>
4 #include <GL/gl.h>
6 #define WCNAME "livebg_test"
8 int colcycle_init(void);
9 void colcycle_cleanup(void);
10 void colcycle_draw(long time_msec);
12 int scr_width, scr_height;
13 long upd_interval;
15 void draw(void);
16 HWND create_window(int width, int height);
17 int init_gl(void);
18 long CALLBACK handle_events(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam);
20 HWND win;
21 HDC dc;
22 HGLRC ctx;
23 int wcreg, quit;
24 unsigned long start_time;
26 int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprev, char *cmdline, int cmdshow)
27 {
28 MSG msg;
30 if(!(win = create_window(400, 300))) {
31 return 1;
32 }
33 if(init_gl() == -1) {
34 DestroyWindow(win);
35 goto done;
36 }
37 if(colcycle_init() == -1) {
38 wglMakeCurrent(0, 0);
39 wglDeleteContext(ctx);
40 DestroyWindow(win);
41 goto done;
42 }
44 start_time = timeGetTime();
46 for(;;) {
47 while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
48 TranslateMessage(&msg);
49 DispatchMessage(&msg);
51 if(quit) goto done;
52 }
54 draw();
55 }
56 done:
58 if(wcreg) {
59 UnregisterClass(WCNAME, hinst);
60 }
61 return 0;
62 }
64 void draw(void)
65 {
66 long now, wait_msec;
67 long tmsec = timeGetTime() - start_time;
69 colcycle_draw(tmsec);
71 now = timeGetTime() - start_time;
72 wait_msec = upd_interval / 1000 - (now - tmsec);
73 if(wait_msec > 0) {
74 fprintf(stderr, "wait_msec: %ld\n", wait_msec);
75 Sleep(wait_msec);
76 }
78 SwapBuffers(dc);
79 }
81 HWND fadewin;
83 int CALLBACK match_win(HWND win, LPARAM lparam)
84 {
85 HWND shwin = FindWindowEx(win, 0, "SHELLDLL_DefView", 0);
86 if(shwin) {
87 fadewin = FindWindowEx(0, shwin, "WorkerW", 0);
88 }
89 return 1;
90 }
92 HWND create_window(int width, int height)
93 {
94 HINSTANCE hinst;
95 HWND win, parent = 0;
96 HWND pgman_win;
97 unsigned int style;
98 RECT rect;
100 hinst = GetModuleHandle(0);
102 if(!wcreg) {
103 WNDCLASSEX wc;
104 memset(&wc, 0, sizeof wc);
105 wc.cbSize = sizeof wc;
106 wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
107 wc.lpfnWndProc = handle_events;
108 wc.hInstance = hinst;
109 wc.hIcon = wc.hIconSm = LoadIcon(0, IDI_APPLICATION);
110 wc.hCursor = LoadCursor(0, IDC_ARROW);
111 wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
112 wc.lpszClassName = WCNAME;
113 RegisterClassEx(&wc);
114 wcreg = 1;
115 }
117 if((pgman_win = FindWindow("Progman", 0))) {
118 parent = pgman_win;
119 GetWindowRect(pgman_win, &rect);
120 width = rect.right - rect.left;
121 height = rect.bottom - rect.top;
123 /*
124 SendMessage(pgman_win, 0x52c, 0, 0);
126 fadewin = 0;
127 EnumWindows(match_win, 0);
128 if(fadewin) {
129 //ShowWindow(fadewin, 0);
130 }
131 */
132 }
134 style = parent ? WS_CHILD : WS_POPUP;
135 if(!(win = CreateWindow(WCNAME, WCNAME, style | WS_VISIBLE, 0, 0, width, height,
136 parent, 0, hinst, 0))) {
137 fprintf(stderr, "failed to create window\n");
138 return 0;
139 }
140 dc = GetDC(win);
142 scr_width = width;
143 scr_height = height;
144 return win;
145 }
147 int init_gl(void)
148 {
149 int pixfmt;
150 PIXELFORMATDESCRIPTOR pfd;
152 memset(&pfd, 0, sizeof pfd);
153 pfd.nSize = sizeof pfd;
154 pfd.nVersion = 1;
155 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
156 pfd.iPixelType = PFD_TYPE_RGBA;
157 pfd.cColorBits = 24;
158 pfd.cDepthBits = 32;
159 pfd.iLayerType = PFD_MAIN_PLANE;
161 if(!(pixfmt = ChoosePixelFormat(dc, &pfd))) {
162 fprintf(stderr, "failed to find a suitable pixel format\n");
163 return -1;
164 }
165 SetPixelFormat(dc, pixfmt, &pfd);
167 if(!(ctx = wglCreateContext(dc))) {
168 fprintf(stderr, "failed to create an OpenGL context\n");
169 return -1;
170 }
171 wglMakeCurrent(dc, ctx);
172 return 0;
173 }
175 long CALLBACK handle_events(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam)
176 {
177 switch(msg) {
178 case WM_PAINT:
179 ValidateRect(win, 0);
180 break;
182 case WM_CLOSE:
183 colcycle_cleanup();
184 wglMakeCurrent(0, 0);
185 wglDeleteContext(ctx);
186 DestroyWindow(win);
187 break;
189 case WM_DESTROY:
190 PostQuitMessage(0);
191 break;
193 case WM_QUIT:
194 quit = 1;
195 break;
197 default:
198 return DefWindowProc(win, msg, wparam, lparam);
199 }
200 return 0;
201 }