1
// This file is part of BOINC.
2
// http://boinc.berkeley.edu
3
// Copyright (C) 2008 University of California
5
// BOINC is free software; you can redistribute it and/or modify it
6
// under the terms of the GNU Lesser General Public License
7
// as published by the Free Software Foundation,
8
// either version 3 of the License, or (at your option) any later version.
10
// BOINC is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
// See the GNU Lesser General Public License for more details.
15
// You should have received a copy of the GNU Lesser General Public License
16
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
18
// unix-specific graphics stuff
35
#include "boinc_glut.h"
36
#include "boinc_api.h"
37
#include "graphics2.h"
38
#include "diagnostics.h"
40
#define TIMER_INTERVAL_MSEC 30
42
static int xpos = 100, ypos = 100, width = 600, height = 400;
43
static int clicked_button;
45
static int checkparentcounter=0;
48
#include <sys/param.h> // for MAXPATHLEN
50
static bool need_show = false;
55
void boinc_close_window_and_quit(const char* p) {
56
fprintf(stderr, "Quitting: %s\n", p);
60
void boinc_close_window_and_quit_aux() {
61
boinc_close_window_and_quit("GLUT close");
64
// This callback is invoked when a user presses a key.
66
void keyboardD(unsigned char key, int /*x*/, int /*y*/) {
68
boinc_close_window_and_quit("key down");
70
boinc_app_key_press((int) key, 0);
74
void keyboardU(unsigned char key, int /*x*/, int /*y*/) {
76
boinc_close_window_and_quit("key up");
78
boinc_app_key_release((int) key, 0);
82
void mouse_click(int button, int state, int x, int y){
83
clicked_button = button;
85
boinc_close_window_and_quit("mouse click");
88
boinc_app_mouse_button(x, y, button, false);
90
boinc_app_mouse_button(x, y, button, true);
95
void mouse_click_move(int x, int y){
97
boinc_close_window_and_quit("mouse move");
98
} else if (clicked_button == 2){
99
boinc_app_mouse_move(x, y, false, false, true);
100
} else if (clicked_button == 1){
101
boinc_app_mouse_move(x, y, false, true, false);
102
} else if (clicked_button == 0){
103
boinc_app_mouse_move(x, y, true, false, false);
105
boinc_app_mouse_move(x, y, false, false, false);
109
static void maybe_render() {
110
int new_xpos, new_ypos, new_width, new_height;
111
static int size_changed = 0;
113
new_xpos = glutGet(GLUT_WINDOW_X);
114
new_ypos = glutGet(GLUT_WINDOW_Y);
115
new_width = glutGet(GLUT_WINDOW_WIDTH);
116
new_height = glutGet(GLUT_WINDOW_HEIGHT);
119
if (throttled_app_render(new_width, new_height, dtime())) {
122
// If user has changed window size, wait until it stops
123
// changing and then write the new dimensions to file
124
if ((new_xpos != xpos) || (new_ypos != ypos) ||
125
(new_width != width) || (new_height != height)
133
if (size_changed && (++size_changed > 10)) {
135
FILE *f = boinc_fopen("gfx_info", "w");
137
// ToDo: change this to XML
138
fprintf(f, "%d %d %d %d\n", xpos, ypos, width, height);
142
} // End if (new size != previous size) else
143
} // End if (! fullscreen)
145
MacGLUTFix(fullscreen);
154
static void make_window() {
155
char window_title[256];
156
get_window_title(window_title, 256);
158
win = glutCreateWindow(window_title);
159
glutReshapeFunc(app_graphics_resize);
160
glutKeyboardFunc(keyboardD);
161
glutKeyboardUpFunc(keyboardU);
162
glutMouseFunc(mouse_click);
163
glutMotionFunc(mouse_click_move);
164
glutDisplayFunc(maybe_render);
165
glEnable(GL_DEPTH_TEST);
170
glutWMCloseFunc(boinc_close_window_and_quit_aux); // Enable the window's close box
172
// Show window only after a successful call to throttled_app_render();
173
// this avoids momentary display of old image when screensaver restarts
174
// which made image appear to "jump."
183
static void boinc_glut_init(int *argc, char** argv) {
186
FILE *f = boinc_fopen("gfx_info", "r");
188
// ToDo: change this to XML parsing
189
fscanf(f, "%d %d %d %d\n", &xpos, &ypos, &width, &height);
193
glutInit (argc, argv);
194
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_ALPHA);
195
glutInitWindowPosition(xpos, ypos);
196
glutInitWindowSize(width, height);
199
static void timer_handler(int) {
201
// When running under a V5 client, the worker app launches the graphics app
202
// so this code kills the graphics when the worker application exits.
203
// Under a V6 client, the Manager or Screensaver launched the graphics app
204
// so this code kills the graphics when the Manager or Screensaver exits.
205
if (--checkparentcounter < 0) {
206
// Check approximately twice per second if parent process still running
207
checkparentcounter = 500 / TIMER_INTERVAL_MSEC;
208
if (getppid() == 1) {
209
// Quit graphics application if parent process no longer running
210
boinc_close_window_and_quit("parent dead");
213
glutTimerFunc(TIMER_INTERVAL_MSEC, timer_handler, 0);
216
void boinc_graphics_loop(int argc, char** argv) {
217
if (!diagnostics_is_initialized()) {
218
boinc_init_graphics_diagnostics(BOINC_DIAG_DEFAULTS);
222
char dir [MAXPATHLEN];
223
getcwd(dir, MAXPATHLEN);
225
for (int i=1; i<argc; i++) {
226
if (!strcmp(argv[i], "--fullscreen")) {
230
boinc_glut_init(&argc, argv);
232
glutTimerFunc(TIMER_INTERVAL_MSEC, timer_handler, 0);
234
// Apparently glut changed our working directory in OS 10.3.9
1
// This file is part of BOINC.
2
// http://boinc.berkeley.edu
3
// Copyright (C) 2008 University of California
5
// BOINC is free software; you can redistribute it and/or modify it
6
// under the terms of the GNU Lesser General Public License
7
// as published by the Free Software Foundation,
8
// either version 3 of the License, or (at your option) any later version.
10
// BOINC is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
// See the GNU Lesser General Public License for more details.
15
// You should have received a copy of the GNU Lesser General Public License
16
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
18
// unix-specific graphics stuff
35
#include "boinc_glut.h"
36
#include "boinc_api.h"
37
#include "graphics2.h"
38
#include "diagnostics.h"
40
#define TIMER_INTERVAL_MSEC 30
42
static int xpos = 100, ypos = 100, width = 600, height = 400;
43
static int clicked_button;
45
static int checkparentcounter=0;
48
#include <sys/param.h> // for MAXPATHLEN
50
static bool need_show = false;
55
void boinc_close_window_and_quit(const char* p) {
57
fprintf(stderr, "%s Quitting: %s\n", boinc_msg_prefix(buf, sizeof(buf)), p);
61
void boinc_close_window_and_quit_aux() {
62
boinc_close_window_and_quit("GLUT close");
65
// This callback is invoked when a user presses a key.
67
void keyboardD(unsigned char key, int /*x*/, int /*y*/) {
69
boinc_close_window_and_quit("key down");
71
boinc_app_key_press((int) key, 0);
75
void keyboardU(unsigned char key, int /*x*/, int /*y*/) {
77
boinc_close_window_and_quit("key up");
79
boinc_app_key_release((int) key, 0);
83
void mouse_click(int button, int state, int x, int y){
84
clicked_button = button;
86
boinc_close_window_and_quit("mouse click");
89
boinc_app_mouse_button(x, y, button, false);
91
boinc_app_mouse_button(x, y, button, true);
96
void mouse_click_move(int x, int y){
98
boinc_close_window_and_quit("mouse move");
99
} else if (clicked_button == 2){
100
boinc_app_mouse_move(x, y, false, false, true);
101
} else if (clicked_button == 1){
102
boinc_app_mouse_move(x, y, false, true, false);
103
} else if (clicked_button == 0){
104
boinc_app_mouse_move(x, y, true, false, false);
106
boinc_app_mouse_move(x, y, false, false, false);
110
static void maybe_render() {
111
int new_xpos, new_ypos, new_width, new_height;
112
static int size_changed = 0;
114
new_xpos = glutGet(GLUT_WINDOW_X);
115
new_ypos = glutGet(GLUT_WINDOW_Y);
116
new_width = glutGet(GLUT_WINDOW_WIDTH);
117
new_height = glutGet(GLUT_WINDOW_HEIGHT);
120
if (throttled_app_render(new_width, new_height, dtime())) {
123
// If user has changed window size, wait until it stops
124
// changing and then write the new dimensions to file
125
if ((new_xpos != xpos) || (new_ypos != ypos) ||
126
(new_width != width) || (new_height != height)
134
if (size_changed && (++size_changed > 10)) {
136
FILE *f = boinc_fopen("gfx_info", "w");
138
// ToDo: change this to XML
139
fprintf(f, "%d %d %d %d\n", xpos, ypos, width, height);
143
} // End if (new size != previous size) else
144
} // End if (! fullscreen)
146
MacGLUTFix(fullscreen);
155
static void make_window(const char* title) {
156
char window_title[256];
158
strcpy(window_title, title);
160
get_window_title(window_title, 256);
163
win = glutCreateWindow(window_title);
164
glutReshapeFunc(app_graphics_resize);
165
glutKeyboardFunc(keyboardD);
166
glutKeyboardUpFunc(keyboardU);
167
glutMouseFunc(mouse_click);
168
glutMotionFunc(mouse_click_move);
169
glutDisplayFunc(maybe_render);
170
glEnable(GL_DEPTH_TEST);
175
glutWMCloseFunc(boinc_close_window_and_quit_aux); // Enable the window's close box
177
// Show window only after a successful call to throttled_app_render();
178
// this avoids momentary display of old image when screensaver restarts
179
// which made image appear to "jump."
188
static void boinc_glut_init(int *argc, char** argv) {
191
FILE *f = boinc_fopen("gfx_info", "r");
193
// ToDo: change this to XML parsing
194
fscanf(f, "%d %d %d %d\n", &xpos, &ypos, &width, &height);
198
glutInit (argc, argv);
199
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_ALPHA);
200
glutInitWindowPosition(xpos, ypos);
201
glutInitWindowSize(width, height);
204
static void timer_handler(int) {
206
// When running under a V5 client, the worker app launches the graphics app
207
// so this code kills the graphics when the worker application exits.
208
// Under a V6 client, the Manager or Screensaver launched the graphics app
209
// so this code kills the graphics when the Manager or Screensaver exits.
210
if (--checkparentcounter < 0) {
211
// Check approximately twice per second if parent process still running
212
checkparentcounter = 500 / TIMER_INTERVAL_MSEC;
213
if (getppid() == 1) {
214
// Quit graphics application if parent process no longer running
215
boinc_close_window_and_quit("parent dead");
218
glutTimerFunc(TIMER_INTERVAL_MSEC, timer_handler, 0);
221
void boinc_graphics_loop(int argc, char** argv, const char* title) {
222
if (!diagnostics_is_initialized()) {
223
boinc_init_graphics_diagnostics(BOINC_DIAG_DEFAULTS);
227
char dir [MAXPATHLEN];
228
getcwd(dir, MAXPATHLEN);
230
for (int i=1; i<argc; i++) {
231
if (!strcmp(argv[i], "--fullscreen")) {
235
boinc_glut_init(&argc, argv);
237
glutTimerFunc(TIMER_INTERVAL_MSEC, timer_handler, 0);
239
// Apparently glut changed our working directory in OS 10.3.9