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