/frame.c

https://gitlab.com/ibiscybernetics/ratpoison_minus · C · 302 lines · 228 code · 42 blank · 32 comment · 35 complexity · e96e0a2ac2799cb5d4d08fbb5524d7d4 MD5 · raw file

  1. /* functions that manipulate the frame structure.
  2. * Copyright (C) 2000, 2001, 2002, 2003, 2004 Shawn Betts <sabetts@vcn.bc.ca>
  3. *
  4. * This file is part of ratpoison.
  5. *
  6. * ratpoison is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2, or (at your option)
  9. * any later version.
  10. *
  11. * ratpoison is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this software; see the file COPYING. If not, write to
  18. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307 USA
  20. */
  21. #include "ratpoison.h"
  22. #include <string.h>
  23. int
  24. frame_left (rp_frame *frame)
  25. {
  26. return frame->x;
  27. }
  28. int
  29. frame_top (rp_frame *frame)
  30. {
  31. return frame->y;
  32. }
  33. int
  34. frame_right (rp_frame *frame)
  35. {
  36. return frame->x + frame->width;
  37. }
  38. int
  39. frame_bottom (rp_frame *frame)
  40. {
  41. return frame->y + frame->height;
  42. }
  43. int
  44. frame_width(rp_frame *frame)
  45. {
  46. return frame->width;
  47. }
  48. int
  49. frame_height(rp_frame *frame)
  50. {
  51. return frame->height;
  52. }
  53. void
  54. frame_resize_left (rp_frame *frame, int amount)
  55. {
  56. frame->x -= amount;
  57. frame->width += amount;
  58. }
  59. void
  60. frame_resize_right (rp_frame *frame, int amount)
  61. {
  62. frame->width += amount;
  63. }
  64. void
  65. frame_resize_up (rp_frame *frame, int amount)
  66. {
  67. frame->y -= amount;
  68. frame->height += amount;
  69. }
  70. void
  71. frame_resize_down (rp_frame *frame, int amount)
  72. {
  73. frame->height += amount;
  74. }
  75. void
  76. frame_move_left (rp_frame *frame, int amount)
  77. {
  78. frame->x -= amount;
  79. }
  80. void
  81. frame_move_right (rp_frame *frame, int amount)
  82. {
  83. frame->x += amount;
  84. }
  85. void
  86. frame_move_up (rp_frame *frame, int amount)
  87. {
  88. frame->y -= amount;
  89. }
  90. void
  91. frame_move_down (rp_frame *frame, int amount)
  92. {
  93. frame->y += amount;
  94. }
  95. static void
  96. init_frame (rp_frame *f)
  97. {
  98. f->number = 0;
  99. f->x = 0;
  100. f->y = 0;
  101. f->width = 0;
  102. f->height = 0;
  103. f->win_number = 0;
  104. f->last_access = 0;
  105. f->dedicated = 0;
  106. }
  107. rp_frame *
  108. frame_new (rp_screen *s)
  109. {
  110. rp_frame *f;
  111. f = xmalloc (sizeof (rp_frame));
  112. init_frame(f);
  113. f->number = numset_request (s->frames_numset);
  114. return f;
  115. }
  116. void
  117. frame_free (rp_screen *s, rp_frame *f)
  118. {
  119. numset_release (s->frames_numset, f->number);
  120. free (f);
  121. }
  122. rp_frame *
  123. frame_copy (rp_frame *frame)
  124. {
  125. rp_frame *copy;
  126. copy = xmalloc (sizeof (rp_frame));
  127. copy->number = frame->number;
  128. copy->x = frame->x;
  129. copy->y = frame->y;
  130. copy->width = frame->width;
  131. copy->height = frame->height;
  132. copy->win_number = frame->win_number;
  133. copy->last_access = frame->last_access;
  134. return copy;
  135. }
  136. char *
  137. frame_dump (rp_frame *frame, rp_screen *screen)
  138. {
  139. rp_window *win;
  140. char *tmp;
  141. struct sbuf *s;
  142. /* rather than use win_number, use the X11 window ID. */
  143. win = find_window_number (frame->win_number);
  144. s = sbuf_new (0);
  145. sbuf_printf (s, "(frame :number %d :x %d :y %d :width %d :height %d :screenw %d :screenh %d :window %ld :last-access %d :dedicated %d)",
  146. frame->number,
  147. frame->x,
  148. frame->y,
  149. frame->width,
  150. frame->height,
  151. screen->width,
  152. screen->height,
  153. win ? win->w:0,
  154. frame->last_access,
  155. frame->dedicated);
  156. /* Extract the string and return it, and don't forget to free s. */
  157. tmp = sbuf_get (s);
  158. free (s);
  159. return tmp;
  160. }
  161. /* Used only by frame_read */
  162. #define read_slot(x) do { tmp = strtok_ws (NULL); x = strtol(tmp,NULL,10); } while(0)
  163. rp_frame *
  164. frame_read (char *str, rp_screen *screen)
  165. {
  166. Window w = 0L;
  167. rp_window *win;
  168. rp_frame *f;
  169. char *tmp, *d;
  170. int s_width = -1;
  171. int s_height = -1;
  172. /* Create a blank frame. */
  173. f = xmalloc (sizeof (rp_frame));
  174. init_frame(f);
  175. PRINT_DEBUG(("parsing '%s'\n", str));
  176. d = xstrdup(str);
  177. tmp = strtok_ws (d);
  178. /* Verify it starts with '(frame ' */
  179. if (strcmp(tmp, "(frame"))
  180. {
  181. PRINT_DEBUG(("Doesn't start with '(frame '\n"));
  182. free (d);
  183. free (f);
  184. return NULL;
  185. }
  186. /* NOTE: there is no check to make sure each field was filled in. */
  187. tmp = strtok_ws(NULL);
  188. while (tmp)
  189. {
  190. if (!strcmp(tmp, ":number"))
  191. read_slot(f->number);
  192. else if (!strcmp(tmp, ":x"))
  193. read_slot(f->x);
  194. else if (!strcmp(tmp, ":y"))
  195. read_slot(f->y);
  196. else if (!strcmp(tmp, ":width"))
  197. read_slot(f->width);
  198. else if (!strcmp(tmp, ":height"))
  199. read_slot(f->height);
  200. else if (!strcmp(tmp, ":screenw"))
  201. read_slot(s_width);
  202. else if (!strcmp(tmp, ":screenh"))
  203. read_slot(s_height);
  204. else if (!strcmp(tmp, ":window"))
  205. read_slot(w);
  206. else if (!strcmp(tmp, ":last-access"))
  207. read_slot(f->last_access);
  208. else if (!strcmp(tmp, ":dedicated")) {
  209. /* f->dedicated is unsigned, so read into local variable. */
  210. long dedicated;
  211. read_slot(dedicated);
  212. if (dedicated <= 0)
  213. f->dedicated = 0;
  214. else
  215. f->dedicated = 1;
  216. }
  217. else if (!strcmp(tmp, ")"))
  218. break;
  219. else
  220. PRINT_ERROR(("Unknown slot %s\n", tmp));
  221. /* Read the next token. */
  222. tmp = strtok_ws(NULL);
  223. }
  224. if (tmp)
  225. PRINT_ERROR(("Frame has trailing garbage\n"));
  226. free (d);
  227. /* adjust x, y, width and height to a possible screen size change */
  228. if (s_width > 0)
  229. {
  230. f->x = (f->x*screen->width)/s_width;
  231. f->width = (f->width*screen->width)/s_width;
  232. }
  233. if (s_height > 0)
  234. {
  235. f->y = (f->y*screen->height)/s_height;
  236. f->height = (f->height*screen->height)/s_height;
  237. }
  238. /* Perform some integrity checks on what we got and fix any
  239. problems. */
  240. if (f->number <= 0)
  241. f->number = 0;
  242. if (f->x <= 0)
  243. f->x = 0;
  244. if (f->y <= 0)
  245. f->y = 0;
  246. if (f->width <= defaults.window_border_width*2)
  247. f->width = defaults.window_border_width*2 + 1;
  248. if (f->height <= defaults.window_border_width*2)
  249. f->height = defaults.window_border_width*2 + 1;
  250. if (f->last_access < 0)
  251. f->last_access = 0;
  252. /* Find the window with the X11 window ID. */
  253. win = find_window_in_list (w, &rp_mapped_window);
  254. if (win)
  255. f->win_number = win->number;
  256. else
  257. f->win_number = EMPTY;
  258. return f;
  259. }
  260. #undef read_slot