bump up version number
[mancala] / src / main.c
1 /*
2 *
3 *  This file is part of Mancala
4 *
5 *  Copyright (C) 2009 Reto Zingg
6 *
7 *  Some of the code is based on the examples on:
8 *  http://wiki.maemo.org/Documentation/Maemo_5_Developer_Guide
9 *
10 *  This program is free software; you can redistribute it and/or modify it
11 *  under the terms of the GNU General Public License as published by the
12 *  Free Software Foundation; either version 2, or (at your option) any
13 *  later version.
14 *
15 *  This program is distributed in the hope that it will be useful, but
16 *  WITHOUT ANY WARRANTY; without even the implied warranty of
17 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 *  General Public License for more details, a copy of which may be found in
19 *  the file COPYING provided in the main directory of this release.
20 *
21 */
22
23 /*********************************************************************
24 **
25 ** NOT used at the moment...
26 **
27 *********************************************************************/
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <hildon/hildon.h>
34 #include <libosso.h>
35 #include <gtk/gtk.h>
36
37
38 #include "play.h"
39
40 #define OSSO_SERVICE "com.nokia.mancala"
41
42 osso_context_t *ossoContext = NULL;
43
44 gint
45 dbus_callback (const gchar *interface, const gchar *method,
46                 GArray *arguments, gpointer data,
47                 osso_rpc_t *retval)
48 {
49         printf ("Msg dbus: %s, %s\n", interface, method);
50
51         if (!strcmp (method, "top_application"))
52                 gtk_window_present (GTK_WINDOW (data));
53
54         retval->type = DBUS_TYPE_INVALID;
55         return OSSO_OK;
56 }
57
58
59 void play_wrapper (GtkWidget *win)
60 {
61
62         fprintf(stderr, "Button start-play pressed\n");
63         if (play() > 0){
64                 fprintf(stderr, "start play failed\n");
65         }
66         else{
67                 fprintf(stderr, "successfull played...\n");
68         }
69
70         osso_application_top(ossoContext, OSSO_SERVICE, NULL);
71
72 }
73
74 static HildonAppMenu* create_menu (GtkWidget *win)
75         {
76                 int i;
77                 gchar *command_id;
78                 GtkWidget * button;
79                 HildonAppMenu *menu = HILDON_APP_MENU (hildon_app_menu_new ());
80                 for (i = 1; i <= 2; i++) {
81                         /* Create menu entries */
82                         button = hildon_gtk_button_new (HILDON_SIZE_AUTO);
83                         command_id = g_strdup_printf ("Menu command %d", i);
84                         gtk_button_set_label (GTK_BUTTON (button), command_id);
85
86                         /* Attach callback to clicked signal */
87                         switch (i) {
88                                 // Start game
89                                 case 1:
90                                         command_id = g_strdup_printf ("Play");
91                                         gtk_button_set_label (GTK_BUTTON (button), command_id);
92                                         g_signal_connect_after (button, "clicked",
93                                                 G_CALLBACK (play_wrapper),win);
94                                 break;
95                                 // Show help
96                                 case 2:
97                                         command_id = g_strdup_printf ("Quit");
98                                         gtk_button_set_label (GTK_BUTTON (button), command_id);
99                                         g_signal_connect_after (button, "clicked",
100                                                 G_CALLBACK (gtk_main_quit), NULL);
101                                 break;
102                         }
103                         /* Add entry to the view menu */
104                         hildon_app_menu_append (menu, GTK_BUTTON (button));
105                 }
106
107         gtk_widget_show_all (GTK_WIDGET (menu));
108         return menu;
109 }
110
111
112 int main (int argc, char **argv) {
113
114         osso_return_t ret;
115
116         g_print("Initializing LibOSSO\n");
117         ossoContext = osso_initialize(OSSO_SERVICE, "0.1", FALSE, NULL);
118         if (ossoContext == NULL) {
119                 g_error("Failed to initialize LibOSSO\n");
120         }
121
122         GtkWidget *win;
123         GtkWidget *textbox;
124         GtkTextBuffer *textbox_buffer;
125         gchar *textbox_text;
126         GtkBox *vbox;
127         HildonAppMenu *menu;
128         PangoFontDescription *font_desc;
129         GtkWidget * button;
130         gchar *button_lable_text;
131         GtkWidget *pannable_text;
132
133         hildon_gtk_init (&argc, &argv);
134         win = hildon_window_new ();
135
136         /* Create and pack labels */     
137         textbox_text = g_strdup_printf("Welcome to Mancala, the ancient African game of skill!\n\nMancala is a very simple, easy-to-learn game. Each player begins with a horizontal row of holes or pits filled with a certain number of stones. At either end of the board are the players' home pits, which are used to keep score. In this case, the human player has the left home pit and the upper row holes. A move is made by clicking into the hole you wish to move from. The stones are then picked up and distributed, one to each hole, moving toward your home pit is reached, and then moving through your opponent's row, bypassing his/her home, and moving back towards in a circle (counterclockwise), until all the stones have been distributed.\n\nIf the last stone is placed in your home, you receive an extra turn. If the last stone lands in an empty hole on your side, you 'capture' the stones in the opposite hole on your opponent's side, moving them to your home.\n\nThe game ends when one player cannot play (ie. there are no stones on his/her side of the board.  The player who has the most stones on his/her *side* wins.");
138
139         textbox = hildon_text_view_new();
140         // gtk_text_view_set_editable(GTK_TEXT_VIEW(textbox), FALSE);
141         // gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textbox), FALSE);
142         gtk_text_view_set_indent(GTK_TEXT_VIEW(textbox), 10);
143         gtk_text_view_set_left_margin(GTK_TEXT_VIEW(textbox), 10);
144         gtk_text_view_set_right_margin(GTK_TEXT_VIEW(textbox), 10);
145         gtk_text_view_set_pixels_above_lines(GTK_TEXT_VIEW(textbox), 3);
146         gtk_text_view_set_pixels_below_lines(GTK_TEXT_VIEW(textbox), 2);
147         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textbox), GTK_WRAP_WORD);
148         // gtk_widget_set_sensitive(textbox, FALSE);
149         font_desc = pango_font_description_from_string ("vera 18");
150         gtk_widget_modify_font(textbox, font_desc);
151         pango_font_description_free (font_desc);
152
153         textbox_buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (textbox));
154
155         gtk_text_buffer_set_text(textbox_buffer, textbox_text, -1);
156
157
158         pannable_text = hildon_pannable_area_new();
159         hildon_pannable_area_add_with_viewport (HILDON_PANNABLE_AREA(pannable_text), textbox);
160
161
162         vbox = GTK_BOX (gtk_vbox_new (FALSE, 2));
163
164         button = hildon_button_new (HILDON_SIZE_AUTO | HILDON_SIZE_FINGER_HEIGHT,
165                                         HILDON_BUTTON_ARRANGEMENT_VERTICAL);
166         button_lable_text = g_strdup_printf ("Play");
167         gtk_button_set_label (GTK_BUTTON (button), button_lable_text);
168         g_signal_connect_after (button, "clicked",
169                                  G_CALLBACK (play_wrapper),win);
170
171         gtk_box_pack_start (vbox, button, FALSE, TRUE, 1);
172         gtk_box_pack_start (vbox, pannable_text, TRUE, TRUE, 1);
173
174         /* Create menu */
175         menu = create_menu (win);
176
177         /* Attach menu to the window */
178         hildon_window_set_app_menu (HILDON_WINDOW (win), menu);
179
180         /* Add label's box to window */
181         gtk_container_add (GTK_CONTAINER (win), GTK_WIDGET (vbox));
182
183         g_signal_connect (win, "delete_event", G_CALLBACK (gtk_main_quit), NULL);
184
185         gtk_widget_show_all (win);
186
187         ret = osso_rpc_set_default_cb_f (ossoContext, dbus_callback, GTK_WINDOW(win));
188         if (ret != OSSO_OK)
189         {
190                 fprintf (stderr, "osso_rpc_set_default_cb_f failed: %d.\n", ret);
191                 exit (1);
192         }
193
194         gtk_main ();
195
196         osso_deinitialize(ossoContext);
197         ossoContext = NULL;
198
199         return 0;
200 }
201
202