1 /*
2  * Copyright (C) 2002 2003 2004 2005 2006 2007 2008 2009 2010, Magnus Hjorth
3  * Copyright (C) 2011 2012, Magnus Hjorth
4  *
5  * This file is part of mhWaveEdit.
6  *
7  * mhWaveEdit is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * mhWaveEdit is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with mhWaveEdit; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21 
22 /* This contains the main procedure and some other useful procedures and macros. */
23 
24 #ifndef MAIN_H_INCLUDED
25 #define MAIN_H_INCLUDED
26 
27 #include <stdio.h>
28 #include <gtk/gtk.h>
29 #ifdef HAVE_SYS_TIME_H
30 #include <sys/time.h>
31 #endif
32 #ifdef HAVE_SYS_TYPES_H
33 #include <sys/types.h>
34 #endif
35 #ifdef HAVE_TIME_H
36 #include <time.h>
37 #endif
38 #include <string.h>
39 
40 /* GTK compatibility stuff */
41 #if GTK_MAJOR_VERSION == 1
42 #define GTK_CLASS_TYPE(klass) (((GtkObjectClass *)klass)->type)
43 #define gtk_style_get_font(style) ((style)->font)
44 #define GTK_WIN_POS_CENTER_ON_PARENT GTK_WIN_POS_CENTER
45 #define gtk_container_get_children gtk_container_children
46 #define gtk_bin_get_child(bin) ((bin)->child)
47 #define gtk_widget_set_size_request gtk_widget_set_usize
48 #define gtk_entry_set_width_chars(e,w) gtk_widget_set_usize(GTK_WIDGET(e),w*10,-1)
49 void gdk_gc_set_rgb_fg_color(GdkGC *gc, GdkColor *clr);
50 #define gtk_adjustment_get_lower(adj) ((adj)->lower)
51 #define gtk_adjustment_get_upper(adj) ((adj)->upper)
52 #else
53 #define GTK_WINDOW_DIALOG GTK_WINDOW_TOPLEVEL
54 #define gtk_object_class_add_signals(x,y,z)
55 #endif
56 
57 #if GTK_MAJOR_VERSION == 1 || (GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 18)
58 void gtk_widget_set_has_window(GtkWidget *w, gboolean has_window);
59 void gtk_widget_set_can_focus(GtkWidget *w, gboolean can_focus);
60 #endif
61 
62 /* Global stuff */
63 #define PROGRAM_VERSION_STRING PACKAGE " " VERSION
64 
65 /* Permissions for creating ~/.mhwaveedit directory */
66 #define CONFDIR_PERMISSION 0755
67 
68 /* Various portability stuff */
69 #ifndef HAVE_CEILL
70 #define ceill(x) ((long)ceil(x))
71 #endif
72 #ifndef HAVE_FSEEKO
73 #define fseeko(s,o,w) fseek(s,(long)(o),w)
74 #endif
75 #ifndef HAVE_FTELLO
76 #define ftello(s) ((off_t)ftell(s))
77 #endif
78 
79 
80 /* Useful macros and functions */
81 
82 #define ARRAY_LENGTH(arr) (sizeof(arr)/sizeof((arr)[0]))
83 #define ARRAY_END(arr) (arr + ARRAY_LENGTH(arr))
84 
85 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
86 #define IS_BIGENDIAN FALSE
87 #else
88 #define IS_BIGENDIAN TRUE
89 #endif
90 
91 #if SIZEOF_OFF_T > 4
92 #define OFF_T_FORMAT "lld"
93 #define OFF_T_FTYPE long long
94 #else
95 #define OFF_T_FORMAT "ld"
96 #define OFF_T_FTYPE long
97 #endif
98 
99 #define BOOLEQ(x,y) (((x) && (y)) || (!(x) && !(y)))
100 #define XOR(x,y) (((x) && !(y)) || (!(x) && (y)))
101 
102 extern gboolean quitflag;
103 extern GdkPixmap *icon;
104 
105 extern gboolean quality_mode;
106 
107 extern gchar *driver_option;
108 
109 extern gboolean idle_work_flag;
110 
111 
112 /* Various functions */
113 
114 void byteswap(void *buffer, int element_size, int buffer_size);
115 
116 int timeval_subtract(GTimeVal *result, GTimeVal *x, GTimeVal *y);
117 float timeval_divide(GTimeVal *x, GTimeVal *y);
118 void timeval_divide_float(GTimeVal *result, GTimeVal *x, gfloat y);
119 
120 gboolean free2(gpointer key, gpointer value, gpointer user_data);
121 
122 gchar *get_home_directory(void);
123 
124 void do_yield(gboolean may_sleep);
125 
126 int hexval(gchar chr);
127 
128 void launch_mixer(void);
129 
130 GtkLabel *attach_label(gchar *text, GtkWidget *table, guint y, guint x);
131 
132 enum Color {
133      BLACK=0, WHITE, BACKGROUND, WAVE1, WAVE2, CURSOR, MARK, SELECTION,
134      PROGRESS, BARS, BUFPOS, LAST_COLOR
135 };
136 
137 #define FIRST_CUSTOM_COLOR BACKGROUND
138 #define CUSTOM_COLOR_COUNT (LAST_COLOR-FIRST_CUSTOM_COLOR)
139 
140 extern GdkColor factory_default_colors[];
141 extern gchar *color_names[];
142 extern gchar *color_inifile_entry[];
143 
144 GdkColor *get_color(enum Color c);
145 GdkGC *get_gc(enum Color c, GtkWidget *wid);
146 void set_custom_colors(GdkColor *c);
147 void save_colors(void);
148 
149 gchar *channel_name(guint chan, guint total);
150 gchar channel_char(guint chan);
151 gchar *channel_format_name(guint chans);
152 
153 
154 gchar *namepart(gchar *filename);
155 
156 #define TIMEMODE_REAL    0
157 #define TIMEMODE_REALLONG 1
158 #define TIMEMODE_SAMPLES 2
159 #define TIMEMODE_24FPS 3
160 #define TIMEMODE_25FPS 4
161 #define TIMEMODE_NTSC 5
162 #define TIMEMODE_30FPS 6
163 
164 /* Determines how long time a certain number of samples represents, and returns
165    it as a text string.
166 
167      samplerate - The sample rate for which we should determine the time
168        samplerate member is used.)
169      samples - The number of samples to measure.
170      samplemax - Maximum value that needs to be displayed in the same format. If                 unknown, use the same value as for samples.
171      timebuf - The buffer to store the text string into (should be at least 50
172        bytes)
173 
174      return value - timebuf
175 */
176 
177 extern guint default_time_mode;
178 extern guint default_timescale_mode;
179 
180 gchar *get_time(guint32 samplerate, off_t samples, off_t samplemax,
181 		gchar *timebuf, gint timemode);
182 /* Returns time always in short format H'MM:SS or MM:SS */
183 gchar *get_time_s(guint32 samplerate, off_t samples, off_t samplemax,
184 		  gchar *timebuf);
185 /* Returns time always in long time format H'MM:SS.mmmm */
186 gchar *get_time_l(guint32 samplerate, off_t samples, off_t samplemax,
187 		  gchar *timebuf);
188 
189 /* Returns time in same format as get_time, but with sub-seconds removed */
190 gchar *get_time_head(guint32 samplerate, off_t samples, off_t samplemax,
191 		     gchar *timebuf, int timemode);
192 /* Returns time in same format as get_time, but with only sub-second part */
193 gchar *get_time_tail(guint32 samplerate, off_t samples, off_t samplemax,
194 		     gchar *timebuf, int timemode);
195 
196 /* Find a number of even sample points around start_samp and end_samp
197  * suitable for marking out on a time scale with major and minor ticks.
198  *
199  * At least two major points are always returned, one before/on start_samp and
200  * one after/on end_samp.
201  * *npoints and *nminorpoints should be set to the maximum allowed number of
202  * points, and will be set to the number of stored points on return.
203  *
204  * Returns a hint on how the text should be drawn:
205  * 0 - Both major and minor points should have text from the get_time_head
206  *     function.
207  * 1 - Major points should have text from the get_time_head function, and
208  *     minor points should have text from thh get_time_tail function.
209  */
210 guint find_timescale_points(guint32 samplerate,
211 			    off_t start_samp, off_t end_samp,
212 			    off_t *points, int *npoints,
213 			    off_t *midpoints, int *nmidpoints,
214 			    off_t *minorpoints, int *nminorpoints,
215 			    int timemode);
216 
217 
218 /* Converts a string in the form: [H'][MM:]SS[.mmmm] to a seconds value.
219  * Returns negative value if the input is invalid. */
220 gfloat parse_time(gchar *timestr);
221 
222 /* Lookup all keys corresponding to a certain value in a hash table. */
223 GSList *hash_table_lookup_keys(GHashTable *hash_table, gconstpointer value);
224 
225 /* Parse a geometry string in the form  x_y_w_h */
226 gboolean parse_geom(gchar *str, GtkAllocation *result);
227 /* Get a geometry string from a window (it must be realized) */
228 gchar *get_geom(GtkWindow *window);
229 
230 /* Geometry stack functions */
231 GSList *geometry_stack_from_inifile(gchar *ininame);
232 void geometry_stack_save_to_inifile(gchar *ininame, GSList *stack);
233 
234 /* Returns TRUE on success */
235 gboolean geometry_stack_pop(GSList **stackp, gchar **extra, GtkWindow *wnd);
236 void geometry_stack_push(GtkWindow *w, gchar *extra, GSList **stackp);
237 
238 /*
239  * Tries to translate the string s.
240  *
241  * If the string gets translated, returns the translated string.
242  * If the string doesn't get translated, returns a pointer to after the first
243  * pipe char ('|') in the string.
244  *
245  * This is used when you want different translations for the same string,
246  * depending on context. "X|Z" and "Y|Z" both return "Z" if not translated,
247  * but can be translated to different things.
248  */
249 char *translate_strip(const char *s);
250 
251 void format_float(float f, char *r, int maxsz);
252 
253 
254 /* Variables that choose dither methods. Doesn't really belong in this
255  * file, but I'm lazy.. */
256 extern int dither_editing;
257 extern int dither_playback;
258 
259 
260 
261 #endif
262