2004-07-21 22:45:03 +00:00
|
|
|
#include <stdlib.h>
|
2004-10-13 20:20:19 +00:00
|
|
|
#include <stdio.h>
|
2004-11-17 21:02:46 +00:00
|
|
|
#include <setjmp.h>
|
2004-07-21 22:45:03 +00:00
|
|
|
#include <unistd.h>
|
2004-11-17 21:02:46 +00:00
|
|
|
#include <pthread.h>
|
2002-11-12 20:59:00 +00:00
|
|
|
#include "x_opengl.h"
|
|
|
|
|
2004-07-21 22:45:03 +00:00
|
|
|
#include "boinc_gl.h"
|
2004-07-15 21:54:48 +00:00
|
|
|
#include "boinc_api.h"
|
2002-11-12 20:59:00 +00:00
|
|
|
#include "graphics_api.h"
|
2004-07-15 21:54:48 +00:00
|
|
|
#include "app_ipc.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
#define BOINC_WINDOW_CLASS_NAME "BOINC_app"
|
|
|
|
|
2004-10-13 22:52:37 +00:00
|
|
|
#define TIMER_INTERVAL_MSEC 30
|
|
|
|
|
2004-07-15 21:54:48 +00:00
|
|
|
static bool visible = true;
|
|
|
|
static int current_graphics_mode = MODE_HIDE_GRAPHICS;
|
2004-10-13 20:20:19 +00:00
|
|
|
static int acked_graphics_mode;
|
|
|
|
static int xpos = 100, ypos = 100;
|
|
|
|
static int clicked_button;
|
|
|
|
static int win=0;
|
2004-07-15 21:54:48 +00:00
|
|
|
extern void graphics_thread_init();
|
2004-10-13 20:20:19 +00:00
|
|
|
static void set_mode(int mode);
|
2004-07-15 21:54:48 +00:00
|
|
|
|
|
|
|
void keyboardU(unsigned char key, int x, int y) {
|
2004-10-13 20:20:19 +00:00
|
|
|
// This callback is invoked when a user presses a key.
|
|
|
|
//
|
|
|
|
if (current_graphics_mode == MODE_FULLSCREEN) {
|
|
|
|
set_mode(MODE_HIDE_GRAPHICS);
|
|
|
|
}
|
2002-11-12 20:59:00 +00:00
|
|
|
}
|
|
|
|
|
2004-07-15 21:54:48 +00:00
|
|
|
void mouse_click(int button, int state, int x, int y){
|
2004-10-13 20:20:19 +00:00
|
|
|
clicked_button = button;
|
|
|
|
if (current_graphics_mode == MODE_FULLSCREEN) {
|
|
|
|
set_mode(MODE_HIDE_GRAPHICS);
|
|
|
|
} else {
|
|
|
|
if (state) {
|
|
|
|
boinc_app_mouse_button(x, y, button, false);
|
|
|
|
} else {
|
|
|
|
boinc_app_mouse_button(x, y, button, true);
|
|
|
|
}
|
|
|
|
}
|
2004-07-15 21:54:48 +00:00
|
|
|
}
|
2002-11-12 20:59:00 +00:00
|
|
|
|
2004-07-15 21:54:48 +00:00
|
|
|
void mouse_click_move(int x, int y){
|
2004-10-13 20:20:19 +00:00
|
|
|
if (current_graphics_mode == MODE_FULLSCREEN){
|
|
|
|
set_mode(MODE_HIDE_GRAPHICS);
|
|
|
|
} else if (clicked_button == 2){
|
|
|
|
boinc_app_mouse_move(x, y, false, false, true);
|
|
|
|
} else if (clicked_button == 1){
|
|
|
|
boinc_app_mouse_move(x, y, false, true, false);
|
|
|
|
} else if (clicked_button == 0){
|
|
|
|
boinc_app_mouse_move(x, y, true, false, false);
|
|
|
|
} else{
|
|
|
|
boinc_app_mouse_move(x, y, false, false, false);
|
|
|
|
}
|
2004-07-15 21:54:48 +00:00
|
|
|
}
|
2002-11-12 20:59:00 +00:00
|
|
|
|
2004-07-15 21:54:48 +00:00
|
|
|
void keyboardD(unsigned char key, int x, int y) {
|
2004-10-13 20:20:19 +00:00
|
|
|
// This callback is invoked when a user presses a key.
|
|
|
|
//
|
|
|
|
if (current_graphics_mode == MODE_FULLSCREEN) {
|
|
|
|
set_mode(MODE_HIDE_GRAPHICS);
|
|
|
|
}
|
2004-07-15 21:54:48 +00:00
|
|
|
}
|
2002-11-12 20:59:00 +00:00
|
|
|
|
2004-10-13 22:52:37 +00:00
|
|
|
static void maybe_render() {
|
|
|
|
int width, height;
|
|
|
|
if (visible && (current_graphics_mode != MODE_HIDE_GRAPHICS)) {
|
|
|
|
width = glutGet(GLUT_WINDOW_WIDTH);
|
|
|
|
height = glutGet(GLUT_WINDOW_HEIGHT);
|
|
|
|
if (throttled_app_render(width, height, dtime())) {
|
|
|
|
glutSwapBuffers();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void close_func() {
|
|
|
|
if (boinc_is_standalone()) {
|
|
|
|
exit(0);
|
|
|
|
} else {
|
|
|
|
set_mode(MODE_HIDE_GRAPHICS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-15 21:54:48 +00:00
|
|
|
static void make_new_window(int mode){
|
2004-11-18 01:30:37 +00:00
|
|
|
char* args[] = {"foobar", 0};
|
|
|
|
int one=1;
|
2004-11-18 22:18:00 +00:00
|
|
|
static bool first = false;
|
2004-11-18 01:30:37 +00:00
|
|
|
|
2004-10-13 20:20:19 +00:00
|
|
|
if (mode == MODE_WINDOW || mode == MODE_FULLSCREEN){
|
2004-11-18 22:18:00 +00:00
|
|
|
if (first) {
|
|
|
|
glutInit(&one, args);
|
|
|
|
first = false;
|
|
|
|
}
|
2004-10-13 20:20:19 +00:00
|
|
|
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
|
|
|
|
glutInitWindowPosition(xpos, ypos);
|
2004-10-13 22:52:37 +00:00
|
|
|
glutInitWindowSize(600, 400);
|
2004-10-13 20:20:19 +00:00
|
|
|
boinc_get_init_data(aid);
|
|
|
|
if (!strlen(aid.app_name)) {
|
|
|
|
strcpy(aid.app_name, "BOINC Application");
|
|
|
|
}
|
|
|
|
win = glutCreateWindow(aid.app_name);
|
2004-10-14 22:01:05 +00:00
|
|
|
|
2004-10-13 20:20:19 +00:00
|
|
|
glutReshapeFunc(app_graphics_resize);
|
|
|
|
glutKeyboardFunc(keyboardD);
|
|
|
|
glutKeyboardUpFunc(keyboardU);
|
|
|
|
glutMouseFunc(mouse_click);
|
|
|
|
glutMotionFunc(mouse_click_move);
|
2004-10-13 22:52:37 +00:00
|
|
|
glutDisplayFunc(maybe_render);
|
2004-10-13 20:20:19 +00:00
|
|
|
|
|
|
|
app_graphics_init();
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
|
|
|
|
if (mode == MODE_FULLSCREEN) {
|
|
|
|
glutFullScreen();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-07-21 22:45:03 +00:00
|
|
|
|
2004-10-13 20:20:19 +00:00
|
|
|
void KillWindow() {
|
|
|
|
if (win) {
|
|
|
|
glutDestroyWindow(win);
|
|
|
|
win = 0;
|
|
|
|
}
|
2004-07-15 21:54:48 +00:00
|
|
|
}
|
2002-11-12 20:59:00 +00:00
|
|
|
|
2004-07-15 21:54:48 +00:00
|
|
|
void set_mode(int mode) {
|
2004-10-13 20:20:19 +00:00
|
|
|
if (mode == current_graphics_mode) return;
|
|
|
|
KillWindow();
|
2004-07-15 21:54:48 +00:00
|
|
|
current_graphics_mode = mode;
|
2004-10-13 20:20:19 +00:00
|
|
|
if (mode != MODE_HIDE_GRAPHICS) {
|
|
|
|
make_new_window(mode);
|
|
|
|
}
|
2004-10-14 22:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void wait_for_initial_message() {
|
|
|
|
app_client_shm->shm->graphics_reply.send_msg(
|
|
|
|
xml_graphics_modes[MODE_HIDE_GRAPHICS]
|
|
|
|
);
|
|
|
|
acked_graphics_mode = MODE_HIDE_GRAPHICS;
|
|
|
|
while (1) {
|
|
|
|
if (app_client_shm->shm->graphics_request.has_msg()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sleep(1);
|
2004-10-13 20:20:19 +00:00
|
|
|
}
|
2004-07-15 21:54:48 +00:00
|
|
|
}
|
2002-11-12 20:59:00 +00:00
|
|
|
|
2004-10-13 20:20:19 +00:00
|
|
|
static void timer_handler(int) {
|
|
|
|
char buf[MSG_CHANNEL_SIZE];
|
2004-11-17 19:19:26 +00:00
|
|
|
GRAPHICS_MSG m;
|
2004-10-13 20:20:19 +00:00
|
|
|
|
2004-10-13 22:52:37 +00:00
|
|
|
int new_mode;
|
2004-10-13 20:20:19 +00:00
|
|
|
if (app_client_shm) {
|
|
|
|
if (app_client_shm->shm->graphics_request.get_msg(buf)) {
|
2004-11-17 19:19:26 +00:00
|
|
|
app_client_shm->decode_graphics_msg(buf, m);
|
|
|
|
switch (m.mode) {
|
2004-10-13 20:20:19 +00:00
|
|
|
case MODE_REREAD_PREFS:
|
|
|
|
//only reread graphics prefs if we have a window open
|
|
|
|
//
|
|
|
|
switch (current_graphics_mode){
|
|
|
|
case MODE_WINDOW:
|
|
|
|
case MODE_FULLSCREEN:
|
|
|
|
app_graphics_reread_prefs();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MODE_HIDE_GRAPHICS:
|
|
|
|
case MODE_WINDOW:
|
|
|
|
case MODE_FULLSCREEN:
|
|
|
|
case MODE_BLANKSCREEN:
|
2004-11-17 19:19:26 +00:00
|
|
|
set_mode(m.mode);
|
2004-10-13 20:20:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (acked_graphics_mode != current_graphics_mode) {
|
|
|
|
bool sent = app_client_shm->shm->graphics_reply.send_msg(
|
|
|
|
xml_graphics_modes[current_graphics_mode]
|
|
|
|
);
|
|
|
|
if (sent) acked_graphics_mode = current_graphics_mode;
|
|
|
|
}
|
2002-11-12 20:59:00 +00:00
|
|
|
}
|
2004-10-13 22:52:37 +00:00
|
|
|
maybe_render();
|
|
|
|
glutTimerFunc(TIMER_INTERVAL_MSEC, timer_handler, 0);
|
|
|
|
}
|
|
|
|
|
2004-11-17 21:02:46 +00:00
|
|
|
jmp_buf jbuf;
|
|
|
|
pthread_t graphics_thread;
|
|
|
|
|
|
|
|
void restart() {
|
|
|
|
if (pthread_equal(pthread_self(), graphics_thread)) {
|
2004-11-17 22:11:17 +00:00
|
|
|
atexit(restart);
|
2004-11-17 21:02:46 +00:00
|
|
|
longjmp(jbuf, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void xwin_graphics_event_loop() {
|
|
|
|
graphics_thread = pthread_self();
|
2004-10-13 22:52:37 +00:00
|
|
|
if (boinc_is_standalone()) {
|
|
|
|
set_mode(MODE_WINDOW);
|
|
|
|
} else {
|
2004-10-14 22:01:05 +00:00
|
|
|
wait_for_initial_message();
|
|
|
|
timer_handler(0);
|
2004-11-18 01:30:37 +00:00
|
|
|
atexit(restart);
|
2002-11-12 20:59:00 +00:00
|
|
|
}
|
2004-11-17 21:02:46 +00:00
|
|
|
int retval = setjmp(jbuf);
|
|
|
|
if (retval) {
|
2004-11-18 22:18:00 +00:00
|
|
|
//fprintf(stderr, "graphics thread restarted\n"); fflush(stderr);
|
2004-11-17 21:02:46 +00:00
|
|
|
set_mode(MODE_HIDE_GRAPHICS);
|
|
|
|
}
|
2004-10-13 20:20:19 +00:00
|
|
|
glutTimerFunc(TIMER_INTERVAL_MSEC, timer_handler, 0);
|
2004-10-13 22:52:37 +00:00
|
|
|
glutMainLoop();
|
2002-11-12 20:59:00 +00:00
|
|
|
}
|