]> Pileus Git - aweather/blob - src/main.c
OpenGL debugging
[aweather] / src / main.c
1 /*
2  * Copyright (C) 2009-2011 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #define _XOPEN_SOURCE
19 #include <sys/time.h>
20 #include <config.h>
21 #include <gtk/gtk.h>
22 #include <gtk/gtkgl.h>
23 #include <glib/gstdio.h>
24
25 #include <grits.h>
26
27 #include "aweather-gui.h"
28 #include "aweather-location.h"
29
30 static gint log_levels = 0;
31
32 static int int2log(int level) {
33         level = G_LOG_LEVEL_ERROR << level;
34         level = (level<<1) - 1;
35         level = level & G_LOG_LEVEL_MASK;
36         return level;
37 }
38
39 static void log_func(const gchar *log_domain, GLogLevelFlags log_level,
40               const gchar *message, gpointer udata)
41 {
42         if (log_level & log_levels) {
43                 if (log_level == G_LOG_LEVEL_DEBUG)
44                         g_fprintf(stderr, "DEBUG: (%p) %s\n",
45                                         g_thread_self(), message);
46                 else
47                         g_log_default_handler(log_domain, log_level, message, udata);
48         }
49 }
50
51 static void xdg_open(GtkLinkButton *button, const gchar *link, gpointer user_data)
52 {
53         gchar *argv[] = {"xdg-open", (gchar*)link, NULL};
54         g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL);
55 }
56
57 static void on_log_level_changed(GtkSpinButton *spinner, AWeatherGui *self)
58 {
59         g_message("main: log_level_changed");
60         log_levels = int2log(gtk_spin_button_get_value_as_int(spinner));
61 }
62
63 static void set_location_time(AWeatherGui *gui, char *site, char *time)
64 {
65         /* Set time
66          *   Do this before setting setting location
67          *   so that it doesn't refresh twice */
68         int year, mon, day, hour, min;
69         sscanf(time, "%d-%d-%d %d:%d", &year, &mon, &day, &hour, &min);
70         time_t sec = mktime(&(struct tm){0, year-1900, mon-1, day, hour, min});
71         if (sec > 0)
72                 grits_viewer_set_time(gui->viewer, sec);
73         g_debug("date = [%s] == %lu\n", time, sec);
74
75         /* Set location */
76         for (city_t *city = cities; city->type; city++) {
77                 if (city->type == LOCATION_CITY && g_str_equal(city->code, site)) {
78                         grits_viewer_set_location(gui->viewer,
79                                 city->pos.lat, city->pos.lon, EARTH_R/35);
80                         break;
81                 }
82         }
83 }
84
85 static void set_toggle_action(AWeatherGui *gui, const char *action, gboolean enabled)
86 {
87         GObject *object = aweather_gui_get_object(gui, action);
88         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(object), enabled);
89 }
90
91 /********
92  * Main *
93  ********/
94 int main(int argc, char *argv[])
95 {
96         /* Defaults */
97         gint     debug      = 2; // G_LOG_LEVEL_WARNING
98         gchar   *site       = "";
99         gchar   *time       = "";
100         gboolean autoupdate = FALSE;
101         gboolean offline    = FALSE;
102         gboolean fullscreen = FALSE;
103
104         /* Arguments */
105         gint     opt_debug      = -1;
106         gchar   *opt_site       = NULL;
107         gchar   *opt_time       = NULL;
108         gboolean opt_offline    = FALSE;
109         gboolean opt_autoupdate = FALSE;
110         gboolean opt_fullscreen = FALSE;
111         GOptionEntry entries[] = {
112                 //long         short flg type                 location         description                 arg desc
113                 {"debug",      'd',  0,  G_OPTION_ARG_INT,    &opt_debug,      "Change default log level", "[0-5]"},
114                 {"site",       's',  0,  G_OPTION_ARG_STRING, &opt_site,       "Set initial site",         "SITE"},
115                 {"time",       't',  0,  G_OPTION_ARG_STRING, &opt_time,       "Set initial date/time",    "DATE"},
116                 {"offline",    'o',  0,  G_OPTION_ARG_NONE,   &opt_offline,    "Run in offline mode",      NULL},
117                 {"autoupdate", 'a',  0,  G_OPTION_ARG_NONE,   &opt_autoupdate, "Auto update radar",        NULL},
118                 {"fullscreen", 'f',  0,  G_OPTION_ARG_NONE,   &opt_fullscreen, "Open in fullscreen mode",  NULL},
119                 {NULL}
120         };
121         (void)entries;
122
123         /* Init */
124         GError *error = NULL;
125
126         gtk_init(&argc, &argv);
127
128         gtk_gl_init(&argc, &argv);
129
130         g_message("trying to get context - 1: %p\n",
131                         gdk_gl_config_new_by_mode(
132                                 GDK_GL_MODE_RGB    |
133                                 GDK_GL_MODE_DEPTH  |
134                                 GDK_GL_MODE_DOUBLE));
135
136         g_message("trying to get context - 2: %p\n",
137                         gdk_gl_config_new_by_mode(
138                                 GDK_GL_MODE_RGB    |
139                                 GDK_GL_MODE_DEPTH  |
140                                 GDK_GL_MODE_DOUBLE |
141                                 GDK_GL_MODE_ALPHA));
142
143         g_message("trying to get context - 3: %p\n",
144                         gdk_gl_config_new_by_mode(
145                                 GDK_GL_MODE_RGBA   |
146                                 GDK_GL_MODE_DEPTH  |
147                                 GDK_GL_MODE_DOUBLE |
148                                 GDK_GL_MODE_ALPHA));
149
150         grits_init(&argc, &argv);
151
152         g_thread_init(NULL);
153         gdk_threads_init();
154
155         g_message("trying to get context - 4: %p\n",
156                         gdk_gl_config_new_by_mode(
157                                 GDK_GL_MODE_RGB    |
158                                 GDK_GL_MODE_DEPTH  |
159                                 GDK_GL_MODE_DOUBLE));
160
161         g_message("trying to get context - 5: %p\n",
162                         gdk_gl_config_new_by_mode(
163                                 GDK_GL_MODE_RGB    |
164                                 GDK_GL_MODE_DEPTH  |
165                                 GDK_GL_MODE_DOUBLE |
166                                 GDK_GL_MODE_ALPHA));
167
168         g_message("trying to get context - 6: %p\n",
169                         gdk_gl_config_new_by_mode(
170                                 GDK_GL_MODE_RGBA   |
171                                 GDK_GL_MODE_DEPTH  |
172                                 GDK_GL_MODE_DOUBLE |
173                                 GDK_GL_MODE_ALPHA));
174
175         /* Use external handler for link buttons */
176         gtk_link_button_set_uri_hook(xdg_open, NULL, NULL);
177
178         /* Setup debug level for aweather_gui_new */
179         g_log_set_handler(NULL, G_LOG_LEVEL_MASK, log_func, NULL);
180         log_levels = int2log(opt_debug >= 0 ? opt_debug : debug);
181
182         /* Set up AWeather */
183         gdk_threads_enter();
184         /* Pre-load some types for gtkbuilder */
185         GRITS_TYPE_OPENGL;
186         AWEATHER_TYPE_GUI;
187         GtkBuilder *builder = gtk_builder_new();
188         if (!gtk_builder_add_from_file(builder, PKGDATADIR "/main.ui", &error))
189                 g_error("Failed to create gtk builder: %s", error->message);
190         AWeatherGui *gui = AWEATHER_GUI(gtk_builder_get_object(builder, "main_window"));
191         g_signal_connect(gui, "destroy", gtk_main_quit, NULL);
192         GObject *action = aweather_gui_get_object(gui, "prefs_general_log");
193         g_signal_connect(action, "changed", G_CALLBACK(on_log_level_changed), NULL);
194
195         /* Finish setting up options */
196         GError *err = NULL;
197         gint     prefs_debug      = grits_prefs_get_integer(gui->prefs, "aweather/log_level",    &err);
198         gchar   *prefs_site       = grits_prefs_get_string(gui->prefs,  "aweather/initial_site", NULL);
199         gboolean prefs_offline    = grits_prefs_get_boolean(gui->prefs, "grits/offline",         NULL);
200         gint     prefs_autoupdate = grits_prefs_get_boolean(gui->prefs, "aweather/update_enab",  NULL);
201
202         debug      = (opt_debug >= 0 ? opt_debug   :
203                       err == NULL    ? prefs_debug : debug);
204         site       = (opt_site       ?: prefs_site       ?: site);
205         time       = (opt_time       ?:                     time);
206         offline    = (opt_offline    ?: prefs_offline    ?: offline);
207         autoupdate = (opt_autoupdate ?: prefs_autoupdate ?: autoupdate);
208         fullscreen = (opt_fullscreen ?:                     fullscreen);
209
210         log_levels = int2log(debug);
211         set_location_time(gui, site, time);
212         grits_viewer_set_offline(gui->viewer, offline);
213         set_toggle_action(gui, "update",     autoupdate);
214         set_toggle_action(gui, "fullscreen", fullscreen);
215         g_free(prefs_site);
216
217         /* Done with init, show gui */
218         gtk_widget_show_all(GTK_WIDGET(gui));
219         set_toggle_action(gui, "fullscreen", fullscreen); // Resest widget hiding
220         gtk_main();
221         gdk_threads_leave();
222         gdk_display_close(gdk_display_get_default());
223         return 0;
224 }