reorganize source a bit, building but not rendering LogoBoxes

This commit is contained in:
Josh Holtrop committed 2011-10-11 11:35:23 -04:00
1 parent 00b9723aac
commit 55bf10c282
12 files changed
+191 -72

No files matched your search

+171
View File
@@ -0,0 +1,171 @@
#include "GnomeScreensaver.h"
#define MAX_ASPECT (16.0/9.0*1.02)
static gboolean
expose (GtkWidget *da, GdkEventExpose *event, GnomeScreensaver *gs)
{
return gs->exposeCB(da, event);
}
static gboolean
configure (GtkWidget *da, GdkEventConfigure *event, GnomeScreensaver *gs)
{
return gs->configureCB(da, event);
}
static gboolean
update (void *gs)
{
return ((GnomeScreensaver *)gs)->updateCB();
}
GnomeScreensaver::GnomeScreensaver(int *argc, char ***argv,
bool (*configure_callback)(GnomeScreensaver &),
bool (*expose_callback)(GnomeScreensaver &),
bool (*update_callback)(GnomeScreensaver &),
int32_t interval_msec)
: m_configure_callback(configure_callback),
m_expose_callback(expose_callback),
m_update_callback(update_callback)
{
gchar *geometry = NULL;
GOptionEntry options[] = {
{"geometry", 0, 0, G_OPTION_ARG_STRING, &geometry,
N_("The initial size and position of window"),
N_("WIDTHxHEIGHT+X+Y")},
{NULL}
};
GtkWidget *window;
GdkGLConfig *glconfig;
GtkStateType state;
GError *error = NULL;
struct timeval tv;
gettimeofday(&tv, NULL);
m_start_msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
m_n_monitors = 1;
gtk_init_with_args (argc, argv,
_(""),
options, NULL, &error);
gtk_gl_init(argc, argv);
if (error != NULL)
{
g_printerr (_("%s. See --help for usage information.\n"),
_(error->message));
g_error_free (error);
}
window = gs_theme_window_new ();
g_signal_connect (G_OBJECT (window), "delete-event",
G_CALLBACK (gtk_main_quit), NULL);
m_drawing_area = gtk_drawing_area_new ();
glconfig = gdk_gl_config_new_by_mode( (GdkGLConfigMode)
( GDK_GL_MODE_RGB |
GDK_GL_MODE_DEPTH |
GDK_GL_MODE_DOUBLE ) );
if (!glconfig)
{
g_assert_not_reached();
}
if (!gtk_widget_set_gl_capability(m_drawing_area, glconfig, NULL, TRUE,
GDK_GL_RGBA_TYPE))
{
g_assert_not_reached();
}
g_signal_connect(m_drawing_area, "configure-event",
G_CALLBACK(configure), this);
g_signal_connect(m_drawing_area, "expose-event",
G_CALLBACK(expose), this);
gtk_widget_show (m_drawing_area);
gtk_container_add (GTK_CONTAINER (window), m_drawing_area);
if ((geometry == NULL)
|| !gtk_window_parse_geometry (GTK_WINDOW (window), geometry))
{
gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
}
gtk_widget_show (window);
gtk_timeout_add(interval_msec, update, this);
}
uint64_t GnomeScreensaver::getTicks()
{
struct timeval tv;
gettimeofday(&tv, NULL);
uint64_t msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
return msec - m_start_msec;
}
gboolean GnomeScreensaver::exposeCB(GtkWidget *da, GdkEventExpose *event)
{
GdkGLContext *glcontext = gtk_widget_get_gl_context(da);
GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(da);
if (!gdk_gl_drawable_gl_begin(gldrawable, glcontext))
{
g_assert_not_reached();
}
gboolean rc = m_expose_callback(*this);
if (gdk_gl_drawable_is_double_buffered(gldrawable))
gdk_gl_drawable_swap_buffers(gldrawable);
else
glFlush();
gdk_gl_drawable_gl_end(gldrawable);
return rc;
}
gboolean GnomeScreensaver::configureCB(GtkWidget *da, GdkEventConfigure *event)
{
GdkGLContext *glcontext = gtk_widget_get_gl_context(da);
GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(da);
if (!gdk_gl_drawable_gl_begin(gldrawable, glcontext))
{
g_assert_not_reached();
}
m_width = da->allocation.width;
m_height = da->allocation.height;
for (m_n_monitors = 1;
(double)m_width/(double)m_n_monitors/(double)m_height > MAX_ASPECT;
m_n_monitors++)
;
m_aspect_ratio = (float)m_width / (float)m_height;
gboolean rc = m_configure_callback(*this);
gdk_gl_drawable_gl_end(gldrawable);
return rc;
}
gboolean GnomeScreensaver::updateCB()
{
gboolean rc = m_update_callback(*this);
if (rc)
{
gtk_widget_queue_draw(m_drawing_area);
}
return rc;
}
+53
View File
@@ -0,0 +1,53 @@
#ifndef GNOMESCREENSAVER_H
#define GNOMESCREENSAVER_H
#include <time.h>
#include <sys/time.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#include <gdk/gdkgl.h>
#include <gtk/gtk.h>
#include <gtk/gtkgl.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "gs-theme-window.h"
class GnomeScreensaver
{
public:
GnomeScreensaver(int *argc, char ***argv,
bool (*configure_callback)(GnomeScreensaver &),
bool (*expose_callback)(GnomeScreensaver &),
bool (*update_callback)(GnomeScreensaver &),
int32_t interval_msec);
uint64_t getTicks();
void mainloop() { gtk_main(); }
gboolean configureCB(GtkWidget *da, GdkEventConfigure *event);
gboolean exposeCB(GtkWidget *da, GdkEventExpose *event);
gboolean updateCB();
int getWidth() { return m_width; }
int getHeight() { return m_height; }
float getAspectRatio() { return m_aspect_ratio; }
int getNumMonitors() { return m_n_monitors; }
protected:
GtkWidget *m_drawing_area;
uint64_t m_start_msec;
bool (*m_configure_callback)(GnomeScreensaver &);
bool (*m_expose_callback)(GnomeScreensaver &);
bool (*m_update_callback)(GnomeScreensaver &);
int m_width, m_height;
float m_aspect_ratio;
int m_n_monitors;
};
#endif /* GNOMESCREENSAVER_H */
+102
View File
@@ -0,0 +1,102 @@
/*
* Author: Josh Holtrop
* DornerWorks ScreenSaver
* This module can be used to create "LogoBox" objects
* which consist of a 3D DW logo
*/
#include <GL/gl.h>
#include <iostream>
#include "wfobj/WFObj.h"
#include "LogoBox.h"
#include "cfs.h"
#include "glslUtil/glslUtil.h"
#include "WFObjLoadFile.h"
using namespace std;
static WFObj obj;
static GLuint program;
static GLint ambient_loc, diffuse_loc, specular_loc, shininess_loc;
enum Locations {
LOC_POSITION,
LOC_NORMAL
};
static float _width, _depth, _height;
static bool loaded = false;
/*
* construct a LogoBox object
* The first time the constructor is called it loads the
* Alias Wavefront object model. Subsequent calls will
* reuse the same module.
*/
LogoBox::LogoBox()
{
if (!loaded)
{
if (obj.load("logo/dwlogo.obj", WFObjLoadFile))
{
const float * aabb = obj.getAABB();
float c_x = (aabb[0] + aabb[3]) / 2.0f;
float c_y = (aabb[1] + aabb[4]) / 2.0f;
float c_z = (aabb[2] + aabb[5]) / 2.0f;
_width = aabb[3] - aabb[0];
_depth = aabb[4] - aabb[1];
_height = aabb[5] - aabb[2];
loaded = loadShaders();
}
else
{
cerr << "Error loading dwlogo.obj" << endl;
}
}
m_width = _width;
m_depth = _depth;
m_height = _height;
}
bool LogoBox::loadShaders()
{
const guAttribBinding bindings[] = {
{LOC_POSITION, "pos"},
{LOC_NORMAL, "normal"},
{0, NULL}
};
const char *v_shader_src = (const char *) getFile("shaders/obj.vp", NULL);
const char *f_shader_src = (const char *) getFile("shaders/obj.fp", NULL);
if (v_shader_src == NULL || f_shader_src == NULL)
{
cerr << "Error reading shader source files" << endl;
return false;
}
program = guMakeProgramFromSource(v_shader_src, f_shader_src, bindings);
if (program == 0)
{
cerr << "Error creating shaders." << endl;
return false;
}
ambient_loc = glGetUniformLocation(program, "ambient");
diffuse_loc = glGetUniformLocation(program, "diffuse");
specular_loc = glGetUniformLocation(program, "specular");
shininess_loc = glGetUniformLocation(program, "shininess");
glUseProgram(program);
/* set up a default material */
glUniform4f(ambient_loc, 0.2, 0.2, 0.2, 1.0);
glUniform4f(diffuse_loc, 1.0, 0.6, 0.0, 1.0);
glUniform4f(specular_loc, 1.0, 1.0, 1.0, 1.0);
glUniform1f(shininess_loc, 85.0);
}
void LogoBox::draw()
{
if (!loaded)
return;
}
+25
View File
@@ -0,0 +1,25 @@
/*
* Author: Josh Holtrop
* DornerWorks screensaver
* The LogoBox class
*/
#ifndef LOGOBOX_H
#define LOGOBOX_H
#include <GL/gl.h>
class LogoBox
{
public:
LogoBox();
void draw();
float getWidth() const { return m_width; }
float getDepth() const { return m_depth; }
float getHeight() const { return m_height; }
protected:
bool loadShaders();
float m_width, m_depth, m_height;
};
#endif
+16
View File
@@ -0,0 +1,16 @@
#include <string.h> /* memcpy() */
#include "WFObjLoadFile.h"
#include "cfs.h"
bool WFObjLoadFile(const char *fname, WFObj::Buffer & buff)
{
unsigned int length;
const unsigned char *dat = getFile(fname, &length);
if (dat == NULL)
return false;
buff.alloc(length);
memcpy(buff.data, dat, length);
return true;
}
+9
View File
@@ -0,0 +1,9 @@
#ifndef WFOBJLOADFILE_H
#define WFOBJLOADFILE_H
#include "wfobj/WFObj.h"
bool WFObjLoadFile(const char *fname, WFObj::Buffer & buff);
#endif /* WFOBJLOADFILE_H */
+206
View File
@@ -0,0 +1,206 @@
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 8; tab-width: 8 -*-
*
* gs-theme-window.c - special toplevel for screensavers
*
* Copyright (C) 2005 Ray Strode <rstrode@redhat.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2, or (at your option) any later version.
*
* This program 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 Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Originally written by: Ray Strode <rstrode@redhat.com>
*/
#include <errno.h>
#include <stdlib.h>
#include <glib.h>
#include <glib-object.h>
#include <glib/gi18n.h>
#include <gdk/gdkx.h>
#include <gtk/gtk.h>
#include "gs-theme-window.h"
static void gs_theme_window_finalize (GObject *object);
static void gs_theme_window_real_realize (GtkWidget *widget);
static GObjectClass *parent_class = NULL;
G_DEFINE_TYPE (GSThemeWindow, gs_theme_window, GTK_TYPE_WINDOW)
#define MIN_SIZE 10
static void
gs_theme_window_class_init (GSThemeWindowClass *klass)
{
GObjectClass *object_class;
GtkWidgetClass *widget_class;
object_class = G_OBJECT_CLASS (klass);
widget_class = GTK_WIDGET_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
object_class->finalize = gs_theme_window_finalize;
widget_class->realize = gs_theme_window_real_realize;
}
static void
force_no_pixmap_background (GtkWidget *widget)
{
static gboolean first_time = TRUE;
if (first_time) {
gtk_rc_parse_string ("\n"
" style \"gs-theme-engine-style\"\n"
" {\n"
" bg_pixmap[NORMAL] = \"<none>\"\n"
" bg_pixmap[ACTIVE] = \"<none>\"\n"
" bg_pixmap[PRELIGHT] = \"<none>\"\n"
" bg_pixmap[SELECTED] = \"<none>\"\n"
" bg_pixmap[INSENSITIVE] = \"<none>\"\n"
" bg[NORMAL] = \"#000000\"\n"
" bg[ACTIVE] = \"#000000\"\n"
" bg[PRELIGHT] = \"#000000\"\n"
" bg[SELECTED] = \"#000000\"\n"
" bg[INSENSITIVE] = \"#000000\"\n"
" }\n"
" widget \"gs-window*\" style : highest \"gs-theme-engine-style\"\n"
"\n");
first_time = FALSE;
}
gtk_widget_set_name (widget, "gs-window");
}
static void
gs_theme_window_init (GSThemeWindow *window)
{
force_no_pixmap_background (GTK_WIDGET (window));
}
static void
gs_theme_window_finalize (GObject *object)
{
GSThemeWindow *window;
GObjectClass *parent_class;
window = GS_THEME_WINDOW (object);
parent_class = G_OBJECT_CLASS (gs_theme_window_parent_class);
if (parent_class->finalize != NULL)
parent_class->finalize (object);
}
static void
gs_theme_window_real_realize (GtkWidget *widget)
{
GdkWindow *window;
Window remote_xwindow;
GtkRequisition requisition;
GtkAllocation allocation;
const char *preview_xid;
int x;
int y;
int width;
int height;
int event_mask;
event_mask = 0;
window = NULL;
preview_xid = g_getenv ("XSCREENSAVER_WINDOW");
if (preview_xid != NULL) {
char *end;
remote_xwindow = (Window) strtoul (preview_xid, &end, 0);
if ((remote_xwindow != 0) && (end != NULL) &&
((*end == ' ') || (*end == '\0')) &&
((remote_xwindow < G_MAXULONG) || (errno != ERANGE))) {
window = gdk_window_foreign_new (remote_xwindow);
if (window != NULL) {
/* This is a kludge; we need to set the same
* flags gs-window-x11.c does, to ensure they
* don't get unset by gtk_window_map() later.
*/
gtk_window_set_decorated (GTK_WINDOW (widget), FALSE);
gtk_window_set_skip_taskbar_hint (GTK_WINDOW (widget), TRUE);
gtk_window_set_skip_pager_hint (GTK_WINDOW (widget), TRUE);
gtk_window_set_keep_above (GTK_WINDOW (widget), TRUE);
gtk_window_fullscreen (GTK_WINDOW (widget));
event_mask = GDK_EXPOSURE_MASK | GDK_STRUCTURE_MASK;
gtk_widget_set_events (widget, gtk_widget_get_events (widget) | event_mask);
}
}
}
if (window == NULL) {
GtkWidgetClass *parent_class;
parent_class = GTK_WIDGET_CLASS (gs_theme_window_parent_class);
if (parent_class->realize != NULL)
parent_class->realize (widget);
return;
}
gtk_style_set_background (widget->style,
window,
GTK_STATE_NORMAL);
gdk_window_set_decorations (window, (GdkWMDecoration) 0);
gdk_window_set_events (window, gdk_window_get_events (window) | event_mask);
widget->window = window;
gdk_window_set_user_data (window, widget);
GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
gdk_window_get_geometry (window, &x, &y, &width, &height, NULL);
if (width < MIN_SIZE || height < MIN_SIZE) {
g_critical ("This window is way too small to use");
exit (1);
}
gtk_widget_size_request (widget, &requisition);
allocation.x = x;
allocation.y = y;
allocation.width = width;
allocation.height = height;
gtk_widget_size_allocate (widget, &allocation);
gtk_window_resize (GTK_WINDOW (widget), width, height);
}
GtkWidget *
gs_theme_window_new (void)
{
GSThemeWindow *window;
window = g_object_new (GS_TYPE_THEME_WINDOW,
"type", GTK_WINDOW_TOPLEVEL,
NULL);
return GTK_WIDGET (window);
}
+72
View File
@@ -0,0 +1,72 @@
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 8; tab-width: 8 -*-
*
* gs-theme-window.h - special toplevel for screensavers
*
* Copyright (C) 2005 Ray Strode <rstrode@redhat.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2, or (at your option) any later version.
*
* This program 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 Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Originally written by: Ray Strode <rstrode@redhat.com>
*/
#ifndef GS_THEME_WINDOW_H
#define GS_THEME_WINDOW_H
#include <glib.h>
#include <glib-object.h>
#include <gtk/gtk.h>
G_BEGIN_DECLS
#define GS_TYPE_THEME_WINDOW (gs_theme_window_get_type ())
#define GS_THEME_WINDOW(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GS_TYPE_THEME_WINDOW, GSThemeWindow))
#define GS_THEME_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GS_TYPE_THEME_WINDOW, GSThemeWindowClass))
#define GS_IS_WINDOW(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GS_TYPE_THEME_WINDOW))
#define GS_IS_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GS_TYPE_THEME_WINDOW))
#define GS_THEME_WINDOW_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GS_TYPE_THEME_WINDOW, GSThemeWindowClass))
typedef struct _GSThemeWindow GSThemeWindow;
typedef struct _GSThemeWindowClass GSThemeWindowClass;
struct _GSThemeWindow
{
GtkWindow parent;
/*< private >*/
/* reserved for priv pointer */
gpointer reserved;
};
struct _GSThemeWindowClass
{
GtkWindowClass parent_class;
/* for signals later if needed */
gpointer reserved_1;
gpointer reserved_2;
gpointer reserved_3;
gpointer reserved_4;
};
#ifndef GS_HIDE_FUNCTION_DECLARATIONS
GType gs_theme_window_get_type (void);
GtkWidget *gs_theme_window_new (void);
#endif
G_END_DECLS
#endif /* GS_THEME_WINDOW_H */