1 /**
2 * This file is a part of the Cairo-Dock project
3 *
4 * Copyright : (C) see the 'copyright' file.
5 * E-mail    : see the 'copyright' file.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 *
12 * This program 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 * You should have received a copy of the GNU General Public License
17 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include <stdlib.h>
21 #include <libxklavier/xklavier.h>
22 #include <gdk/gdkx.h>
23 
24 #include "applet-config.h"
25 #include "applet-xklavier.h"
26 #include "applet-notifications.h"
27 #include "applet-struct.h"
28 #include "applet-draw.h"
29 #include "applet-init.h"
30 
31 
32 CD_APPLET_DEFINITION (N_("keyboard indicator"),
33 	2, 0, 0,
34 	CAIRO_DOCK_CATEGORY_APPLET_SYSTEM,
35 	N_("This applet lets you control the keyboard layout.\n\
36 	It can also display the current num and caps lock.\n\
37 	Left-click to switch to the next layout\n\
38 	Scroll up/down to select the previous/next layout\n\
39 	Right-click gives you access to the list of available layouts."),
40 	"Fabounet (Fabrice Rey)")
41 
_load_bg_image(void)42 static void _load_bg_image (void)
43 {
44 	int iWidth, iHeight;
45 	CD_APPLET_GET_MY_ICON_EXTENT (&iWidth, &iHeight);
46 	cairo_dock_unload_image_buffer (&myData.bgImage);
47 	cairo_dock_load_image_buffer (&myData.bgImage,
48 		myConfig.cBackgroundImage,
49 		iWidth,
50 		iHeight,
51 		0);
52 }
53 
54 //\___________ Here is where you initiate your applet. myConfig is already set at this point, and also myIcon, myContainer, myDock, myDesklet (and myDrawContext if you're in dock mode). The macro CD_APPLET_MY_CONF_FILE and CD_APPLET_MY_KEY_FILE can give you access to the applet's conf-file and its corresponding key-file (also available during reload). If you're in desklet mode, myDrawContext is still NULL, and myIcon's buffers has not been filled, because you may not need them then (idem when reloading).
_init(gpointer data)55 static gboolean _init (gpointer data)
56 {
57 	cd_debug ("INIT XKBD");
58 	g_return_val_if_fail (myApplet != NULL, FALSE);
59 	Display *pDisplay = gdk_x11_get_default_xdisplay ();
60 	cd_xkbd_init (pDisplay);
61 	return FALSE;
62 }
63 
on_style_changed(GldiModuleInstance * myApplet)64 static gboolean on_style_changed (GldiModuleInstance *myApplet)
65 {
66 	cd_debug ("Keyboard Indic: style is changing");
67 
68 	if (myConfig.textDescription.cFont == NULL)  // default font -> reload our text description
69 	{
70 		gldi_text_description_set_font (&myConfig.textDescription, NULL);
71 		myConfig.textDescription.iSize = (int) myIcon->image.iHeight * myConfig.fTextRatio;
72 	}
73 	cd_xkbd_update_icon (NULL, NULL, TRUE);  // redraw in case the font or the text color has changed
74 
75 	return GLDI_NOTIFICATION_LET_PASS;
76 }
77 
78 CD_APPLET_INIT_BEGIN
79 	if (myDesklet)
80 	{
81 		CD_APPLET_SET_DESKLET_RENDERER ("Simple");  // set a desklet renderer.
82 	}
83 
84 	myConfig.textDescription.iSize = (int) myIcon->image.iHeight * myConfig.fTextRatio;
85 
86 	CD_APPLET_REGISTER_FOR_CLICK_EVENT;
87 	CD_APPLET_REGISTER_FOR_BUILD_MENU_EVENT;
88 	CD_APPLET_REGISTER_FOR_SCROLL_EVENT;
89 	gldi_object_register_notification (&myDesktopMgr,
90 		NOTIFICATION_KBD_STATE_CHANGED,
91 		(GldiNotificationFunc) cd_xkbd_keyboard_state_changed,
92 		GLDI_RUN_AFTER,
93 		myApplet);
94 	gldi_object_register_notification (&myStyleMgr,
95 		NOTIFICATION_STYLE_CHANGED,
96 		(GldiNotificationFunc) on_style_changed,
97 		GLDI_RUN_AFTER, myApplet);
98 
99 	// shortkey
100 	myData.pKeyBinding = CD_APPLET_BIND_KEY (myConfig.cShortkey,
101 		D_("Switch keyboard language"),
102 		"Configuration", "shortkey",
103 		(CDBindkeyHandler) cd_xkbd_on_keybinding_pull);
104 
105 	_load_bg_image ();
106 
107 
108 	if (cairo_dock_is_loading ())
109 		g_timeout_add_seconds (1, _init, NULL);
110 	else
111 		_init (NULL);
112 CD_APPLET_INIT_END
113 
114 
115 //\___________ Here is where you stop your applet. myConfig and myData are still valid, but will be reseted to 0 at the end of the function. In the end, your applet will go back to its original state, as if it had never been activated.
116 CD_APPLET_STOP_BEGIN
117 	CD_APPLET_UNREGISTER_FOR_CLICK_EVENT;
118 	CD_APPLET_UNREGISTER_FOR_BUILD_MENU_EVENT;
119 	CD_APPLET_UNREGISTER_FOR_SCROLL_EVENT;
120 	gldi_object_remove_notification (&myDesktopMgr,
121                 NOTIFICATION_KBD_STATE_CHANGED,
122 		(GldiNotificationFunc) cd_xkbd_keyboard_state_changed,
123 		myApplet);
124 	CD_APPLET_REMOVE_TRANSITION_ON_MY_ICON;
125 
126 	cd_xkbd_stop ();
127 
128 	gldi_object_unref (GLDI_OBJECT(myData.pKeyBinding));
129 CD_APPLET_STOP_END
130 
131 
132 //\___________ The reload occurs in 2 occasions : when the user changes the applet's config, and when the user reload the cairo-dock's config or modify the desklet's size. The macro CD_APPLET_MY_CONFIG_CHANGED can tell you this. myConfig has already been reloaded at this point if you're in the first case, myData is untouched. You also have the macro CD_APPLET_MY_CONTAINER_TYPE_CHANGED that can tell you if you switched from dock/desklet to desklet/dock mode.
133 CD_APPLET_RELOAD_BEGIN
134 	myConfig.textDescription.iSize = (int) myIcon->image.iHeight * myConfig.fTextRatio;
135 
136 	if (CD_APPLET_MY_CONFIG_CHANGED)
137 	{
138 		if (myDesklet && CD_APPLET_MY_CONTAINER_TYPE_CHANGED)  // we are now in a desklet, set a renderer.
139 		{
140 			CD_APPLET_SET_DESKLET_RENDERER ("Simple");
141 		}
142 
143 		CD_APPLET_REMOVE_TRANSITION_ON_MY_ICON;  // prudence.
144 		_load_bg_image ();
145 
146 		// emblems
147 		gboolean bCustomEmblems = (myConfig.cEmblemCapsLock || myConfig.cEmblemNumLock); // has emblem
148 		if (myData.cEmblemCapsLock || myData.cEmblemNumLock)  // current emblems may have changed -> reset them
149 		{
150 			g_free (myData.cEmblemCapsLock);
151 			g_free (myData.cEmblemNumLock);
152 			myData.cEmblemCapsLock = NULL;
153 			myData.cEmblemNumLock = NULL;
154 			bCustomEmblems = TRUE; // had emblem
155 		}
156 		if (!myConfig.bShowKbdIndicator || bCustomEmblems)  // no more indicators or emblems need to be reloaded
157 		{
158 			CD_APPLET_REMOVE_OVERLAY_ON_MY_ICON (CAIRO_OVERLAY_UPPER_RIGHT);
159 			CD_APPLET_REMOVE_OVERLAY_ON_MY_ICON (CAIRO_OVERLAY_UPPER_LEFT);
160 			myData.iPreviousIndic = 0;
161 		}
162 
163 		//\_____________ On declenche le redessin de l'icone.
164 		cd_xkbd_force_redraw ();
165 
166 		gldi_shortkey_rebind (myData.pKeyBinding, myConfig.cShortkey, NULL);
167 	}
168 	else
169 	{
170 		cd_xkbd_force_redraw ();
171 	}
172 CD_APPLET_RELOAD_END
173